{"id":15659370,"url":"https://github.com/thelartians/emglue","last_synced_at":"2025-05-05T19:27:27.684Z","repository":{"id":88218137,"uuid":"260930502","full_name":"TheLartians/EmGlue","owner":"TheLartians","description":"🕸️ Glue C++ to your browser! Universal bindings for JavaScript/Wasm using Glue and Embind.","archived":false,"fork":false,"pushed_at":"2024-01-07T16:48:30.000Z","size":52,"stargazers_count":21,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T23:03:41.961Z","etag":null,"topics":["bindings","cpp","embind","emscripten","glue","glue-bindings","javascript","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://github.com/TheLartians/Glue","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheLartians.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2020-05-03T13:45:16.000Z","updated_at":"2024-01-08T23:46:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"9db27aaa-5f81-4801-87f0-411a43130ab4","html_url":"https://github.com/TheLartians/EmGlue","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":"TheLartians/ModernCppStarter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEmGlue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEmGlue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEmGlue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEmGlue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheLartians","download_url":"https://codeload.github.com/TheLartians/EmGlue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252562172,"owners_count":21768250,"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","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":["bindings","cpp","embind","emscripten","glue","glue-bindings","javascript","wasm","webassembly"],"created_at":"2024-10-03T13:16:27.590Z","updated_at":"2025-05-05T19:27:27.645Z","avatar_url":"https://github.com/TheLartians.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Actions Status](https://github.com/TheLartians/EmGlue/workflows/Build/badge.svg)](https://github.com/TheLartians/EmGlue/actions)\n[![Actions Status](https://github.com/TheLartians/EmGlue/workflows/Style/badge.svg)](https://github.com/TheLartians/EmGlue/actions)\n\n# Glue bindings for JavaScript using Embind\n\nJavaScript (Wasm) bindings for [Glue](https://github.com/TheLartians/Glue) using Emscripten.\n\n## Usage\n\n### Example\n\nUsing EmGlue you can interact with JavaScript using a simple binding interface.\nThe following example illustrates the basic usage.\n\n```cpp\n#include \u003cglue/emscripten/state.h\u003e\n#include \u003cglue/class.h\u003e\n#include \u003ciostream\u003e\n\nvoid exampleBasics() {\n  glue::emscripten::State state;\n\n  // run JS code\n  state.run(\"console.log('Hello JavaScript!')\");\n\n  // extract values from JS\n  std::cout \u003c\u003c state.run(\"'Hello' + ' ' + 'C++!'\")-\u003eget\u003cstd::string\u003e() \u003c\u003c std::endl;\n\n  // extract maps\n  auto map = state.run(\"({a: 1, b: '2'})\").asMap();\n  map[\"a\"]-\u003eget\u003cint\u003e(); // -\u003e 1\n\n  // extract functions\n  auto f = state.run(\"(a,b) =\u003e { return a+b }\").asFunction();\n  f(3, 4).get\u003cint\u003e(); // -\u003e 7\n\n  // inject values\n  auto global = state.root();\n  global[\"x\"] = 42;\n  global[\"square\"] = [](double x){ return x*x; };\n  \n  // interact with JS directly\n  state.run(\"console.log('square(x) =',square(x))\");\n  \n  // or using Glue\n  global[\"console\"][\"log\"](\"square(x) =\", global[\"square\"](global[\"x\"]));\n}\n```\n\nClasses and inheritance are also supported.\n\n```cpp\nstruct A {\n  std::string member;\n  A(std::string m): member(m) {}\n  auto method() const { return \"member: \" + member; }\n};\n\nvoid exampleModules() {\n  glue::emscripten::State state;\n\n  // inject C++ classes and APIs into JavaScript\n  auto module = glue::createAnyMap();\n  \n  module[\"A\"] = glue::createClass\u003cA\u003e()\n    .addConstructor\u003cstd::string\u003e()\n    .addMember(\"member\", \u0026A::member)\n    .addMethod(\"method\", \u0026A::method)\n  ;\n\n  state.addModule(module, state.root());\n\n  state.run(\"a = new A('test');\");\n  state.run(\"console.log('a.member() =',a.member());\");\n  state.run(\"console.log('a.method() =', a.method());\");\n  \n  // there are no destructors in JavaScript \n  // -\u003e be sure to delete your instances after use!\n  state.run(\"a.delete(); a=undefined;\");\n}\n```\n\nCheck the [API](include/glue/emscripten/state.h) and [tests](test/source/state.cpp) for functionality and examples.\nSee [here](https://github.com/TheLartians/TypeScriptXX) for a full example project using automatic TypeScript declarations.\n\n### Adding to your project\n\nEmGlue can be easily integrated through [CPM.cmake](https://github.com/TheLartians/CPM.cmake).\nIf not available before, this will automatically add the Glue target as well.\n\n```cmake\nCPMAddPackage(\n  NAME EmGlue\n  VERSION 0.2\n  GITHUB_REPOSITORY TheLartians/EmGlue\n)\n\ntarget_link_libraries(myLibrary EmGlue)\n```\n\n### Build and run tests\n\nFirst, [install and activate](https://emscripten.org/docs/getting_started/downloads.html) the emsdk.\nThen run the following commands from the project's root.\n\n```bash\nemcmake cmake -Htest -Bbuild\ncmake --build build -j8\nnode build/EmGlueTests.js\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthelartians%2Femglue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthelartians%2Femglue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthelartians%2Femglue/lists"}