Skip to content
/ TypeScriptXX Public template

🧷 Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.

License

Notifications You must be signed in to change notification settings

TheLartians/TypeScriptXX

Repository files navigation

Actions Status Actions Status Actions Status Actions Status codecov

TypeScriptXX

About

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.

Usage

Transpile to Lua and run natively

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"

Transpile to JavaScript and run in node.js

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 

How it works

The example C++ library is exposed to TypeScript using Glue bindings.

The projects contains four standalone subprojects:

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.