Add JavaScript Support For C++

In this article I will describe a method of adding support for JavaScript scripts in C ++ application using the Tiny-JS library.

Tiny-JS is a library for embedding in C ++, provides the execution of JavaScript code, with support for bindings (the ability to call C ++ code from a script)

At first I wanted to use the popular ChaiScript library, Duktape or include the Lua, but due to dependencies and possible difficulties in porting to different platforms, it was decided to find a simple, minimal but powerful MIT JS lib, meets these criteria Tiny-JS. The only disadvantage of this library in the absence of support/development from the developer, but it is fairly simple code that allows you to take the support, if required.

Download Tiny-JS from the repository:
https://github.com/gfwilliams/tiny-js

Next, add Tiny-JS headers:

 
#include "tiny-js/TinyJS.h" 
#include "tiny-js/TinyJS_Functions.h" 

Add .cpp TinyJS files to build stage, then you can start writing load and run scripts.

Lib usage examples available in the repository:
https://github.com/gfwilliams/tiny-js/blob/master/Script.cpp
https://github.com/gfwilliams/tiny-js/blob/wiki/CodeExamples.md

An example of a class handler implementation can be found in the project SpaceJaguar:
https://gitlab.com/demensdeum/space-jaguar-action-rpg/-/blob/master/project/src/Controllers/SpaceJaguarScriptController/SpaceJaguarScriptController.h
https://gitlab.com/demensdeum/space-jaguar-action-rpg/-/blob/master/project/src/Controllers/SpaceJaguarScriptController/SpaceJaguarScriptController.cpp

Game script example integrated into application:
https://gitlab.com/demensdeum/space-jaguar-action-rpg/-/blob/master/project/resources/com.demensdeum.spacejaguaractionrpg.scripts.sceneController.js

References

https://github.com/gfwilliams/tiny-js
https://github.com/dbohdan/embedded-scripting-languages
https://github.com/AlexKotik/embeddable-scripting-languages

0