在这篇文章中,我将描述一种使用 Tiny-JS 库向 C++ 应用程序添加对 JavaScript 脚本的支持的方法。
Tiny-JS 是一个嵌入 C++ 的库,提供 JavaScript 代码的执行,并支持绑定(能够从脚本调用 C++ 代码)
起初我想使用流行的库 ChaiScript、Duktape 或 connect Lua,但由于依赖关系以及移植到不同平台可能存在的困难,所以决定寻找一个简单、最小但功能强大的 MIT Tiny- JS 符合这些标准。这个库的唯一缺点是缺乏作者的支持/开发,但它的代码非常简单,这允许您在必要时接管支持。
从存储库下载 Tiny-JS:
https://github.com/gfwilliams/tiny-js
接下来,将 Tiny-JS 标头添加到负责脚本的代码中:
#include "tiny-js/TinyJS.h"
#include "tiny-js/TinyJS_Functions.h"
将TinyJS .cpp文件添加到构建阶段,然后就可以开始编写加载和运行脚本了。
存储库中提供了使用该库的示例:
https://github.com/gfwilliams/tiny-js/blob/master/Script.cpp
https://github.com/gfwilliams/tiny-js/blob/wiki/CodeExamples.md
处理程序类的实现示例可以在 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
来源
https://github.com/gfwilliams/tiny-js
https://github.com/dbohdan/embedded-scripting-languages
https://github.com/AlexKotik/embeddable-scripting-languages
