{"id":31059948,"url":"https://github.com/developer239/cpp-v8-typescript-extensions","last_synced_at":"2025-09-15T09:55:06.716Z","repository":{"id":305664987,"uuid":"1023529487","full_name":"developer239/cpp-v8-typescript-extensions","owner":"developer239","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-21T11:20:12.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-21T12:12:16.608Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developer239.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-21T09:48:48.000Z","updated_at":"2025-07-21T11:20:16.000Z","dependencies_parsed_at":"2025-07-21T12:12:59.390Z","dependency_job_id":null,"html_url":"https://github.com/developer239/cpp-v8-typescript-extensions","commit_stats":null,"previous_names":["developer239/cpp-v8-typescript-extensions"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/developer239/cpp-v8-typescript-extensions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer239%2Fcpp-v8-typescript-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer239%2Fcpp-v8-typescript-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer239%2Fcpp-v8-typescript-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer239%2Fcpp-v8-typescript-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developer239","download_url":"https://codeload.github.com/developer239/cpp-v8-typescript-extensions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer239%2Fcpp-v8-typescript-extensions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275239392,"owners_count":25429489,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-15T02:00:09.272Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-09-15T09:55:03.780Z","updated_at":"2025-09-15T09:55:06.707Z","avatar_url":"https://github.com/developer239.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# V8 TypeScript C++ Integration Demo\n\nThis project demonstrates the integration of Google's V8 JavaScript engine with C++ applications, showcasing how to create native bindings that allow TypeScript/JavaScript code to interact seamlessly with C++ objects.\n\n## Overview\n\nThe demo implements a coffee machine simulation where C++ classes are exposed to JavaScript through V8 bindings. This pattern is useful for applications that need to provide scripting capabilities with modern JavaScript features.\n\n## Key Features\n\n### Native C++ Object Integration\nC++ objects are wrapped using `shared_ptr` for proper memory management. V8's garbage collector handles JavaScript references while the wrapper ensures C++ objects are properly destroyed.\n\n```cpp\n// C++ class\nclass Recipe {\n    Recipe(std::string_view name, int strength, int waterAmount, int brewTime);\n    const std::string\u0026 getName() const noexcept;\n    int getStrength() const noexcept;\n    int getBrewTime() const noexcept;\n};\n```\n\n```typescript\n// TypeScript usage\nconst espresso = new Recipe(\"Espresso\", 100, 30, 2000);\nconst result = await coffeeMachine.brew(espresso);\n```\n\n### Asynchronous Operations\nThe binding layer converts C++ operations into JavaScript promises, enabling async/await patterns:\n\n```typescript\nasync function brewCoffee(recipe: Recipe) {\n    coffeeMachine.turnOn();\n    await wait(500);  // Promise-based delay\n    const result = await coffeeMachine.brew(recipe);  // C++ operation as Promise\n    coffeeMachine.turnOff();\n}\n```\n\n### Type Safety\nAuto-generated TypeScript definitions provide compile-time type checking and IDE support:\n\n```typescript\ndeclare class CoffeeMachine {\n    constructor(name: string);\n    turnOn(): void;\n    turnOff(): void;\n    brew(recipe: Recipe): Promise\u003cstring\u003e;\n    getName(): string;\n}\n```\n\n## V8 vs Lua Comparison\n\n### When to Use V8\n\nV8 is particularly suitable when:\n\n1. **Modern JavaScript Features Required**\n   - Native async/await support\n   - ES6+ features (destructuring, arrow functions, classes)\n   - Built-in JSON handling\n   - Array methods (map, filter, reduce)\n\n2. **TypeScript Integration Desired**\n   - Static type checking\n   - Superior IDE support with IntelliSense\n   - Refactoring capabilities\n   - Type definitions for APIs\n\n3. **Complex Object Interactions**\n   - Direct mapping of C++ objects to JavaScript\n   - Shared pointer management for object lifecycle\n   - Natural OOP patterns\n\n4. **Developer Ecosystem**\n   - Team familiar with JavaScript/TypeScript\n   - Need to share code between frontend and embedded scripting\n   - Access to npm ecosystem (with appropriate bridging)\n\n### When to Consider Lua\n\nLua remains advantageous for:\n\n1. **Resource Constraints**\n   - Minimal memory footprint (~200KB vs V8's ~10MB)\n   - Faster startup time\n   - Lower CPU overhead\n\n2. **Simplicity Requirements**\n   - Simpler integration API\n   - Fewer dependencies\n   - Easier to embed in restricted environments\n\n3. **Legacy Systems**\n   - Existing Lua codebase\n   - Well-established Lua bindings\n   - Team expertise in Lua\n\n### Performance Considerations\n\n| Aspect | V8 | Lua |\n|--------|----|----|\n| Memory Usage | ~10MB baseline | ~200KB baseline |\n| Startup Time | 50-100ms | \u003c5ms |\n| JIT Compilation | Advanced (TurboFan) | Simple (LuaJIT) |\n| Async Support | Native | Coroutine-based |\n| Object Passing | Direct with wrapping | Table conversion |\n\n## Building and Running\n\n### Prerequisites\n- CMake 3.20+\n- C++20 compatible compiler\n- V8 development libraries\n- Node.js (for TypeScript compilation via esbuild)\n\n### Build Instructions\n```bash\nmkdir build \u0026\u0026 cd build\ncmake ..\nmake\n./v8_demo\n```\n\n```bash\ncd scripts\nnpx -p typescript tsc\n```\n\n## Implementation Notes\n\n### Memory Management\nThe binding layer uses `shared_ptr` throughout because V8's garbage collector controls object lifetime from the JavaScript side. Multiple JavaScript references may exist to the same C++ object, making `unique_ptr` unsuitable.\n\n### Thread Safety\nThe current implementation uses synchronous delays for simplicity. Production systems should integrate with proper event loops (libuv, custom event loop) for true asynchronous operations.\n\n### Error Handling\nV8 exceptions are properly propagated to JavaScript as Promise rejections or thrown errors, maintaining JavaScript error handling paradigms.\n\n## Use Cases\n\nThis integration pattern is valuable for:\n- Game engines requiring scriptable behavior\n- Embedded applications with user-defined logic\n- Desktop applications with plugin systems\n- IoT devices with customizable automation\n\n## Future Enhancements\n\n- Integration with libuv for proper async I/O\n- Performance profiling integration\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper239%2Fcpp-v8-typescript-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeveloper239%2Fcpp-v8-typescript-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper239%2Fcpp-v8-typescript-extensions/lists"}