This is a template project that uses the Glue library to add type-safe scripting capability to a C++ project. Scrips are coded in TypeScript, transpiled using tsc or TSTL and run in by node.js or an an embedded Lua engine. The built JavaScript target can also be run in a browser and used on a website. The TypeScript declarations are automatically generated and updated by CMake.
The following commands build the project and run the typescript script.
cmake -HstandaloneLua -Bbuild/lua
# Compiles C++ code, updates the declarations and transpiles TypeScript
cmake --build build/lua -j8
# runs the transpiled lua script at dist/lua/index.lua
./build/lua/TypeScriptXX --script dist/lua
Is is also possible to enable watch mode for hot reloading. This way any code changes to the TypeScript source will be transpiled and run automatically.
npx concurrently "npm run watch:lua" "./build/lua/TypeScriptXX -s dist/lua --watch"
First, install and activate the emsdk. The following commands build the project and run the typescript script.
emcmake cmake -HstandaloneJS -Bbuild/js
# Compiles C++ code, updates the declarations and transpiles TypeScript
cmake --build build/js -j8
# runs the transpiled lua script at dist/js/index.js
node ./build/js/TypeScriptXX.js --script dist/js
The example C++ library is exposed to TypeScript using Glue bindings.
The projects contains four standalone subprojects:
- test, that check the library code
- declarations, that simply prints the declarations
- standaloneLua, that uses LuaGlue to bind and run lua code.
- standaloneJS, that uses EmGlue to bind to a JavaScript runtime such as node.js.
After building, CMake will use the latter to ensure that the declarations at typescript/cpplib.d.ts
are up-to-date and then transpile the TypeScript code to Lua or JavaScript.