1 Star 1 Fork 0

ScSofts/quickjspp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
ftk- ftk Fix #13 45edd65 5年前
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

QuickJSPP is QuickJS wrapper for C++. It allows you to easily embed Javascript engine into your program.

QuickJS is a small and embeddable Javascript engine. It supports the ES2020 specification including modules, asynchronous generators and proxies. More info: https://bellard.org/quickjs/

Example

#include "quickjspp.hpp"
#include <iostream>

class MyClass
{
public:
    MyClass() {}
    MyClass(std::vector<int>) {}

    double member_variable = 5.5;
    std::string member_function(const std::string& s) { return "Hello, " + s; }
};

void println(const std::string& str) { std::cout << str << std::endl; }

int main()
{
    qjs::Runtime runtime;
    qjs::Context context(runtime);
    try
    {
        // export classes as a module
        auto& module = context.addModule("MyModule");
        module.function<&println>("println");
        module.class_<MyClass>("MyClass")
                .constructor<>()
                .constructor<std::vector<int>>("MyClassA")
                .fun<&MyClass::member_variable>("member_variable")
                .fun<&MyClass::member_function>("member_function");
        // import module
        context.eval("import * as my from 'MyModule'; globalThis.my = my;", "<import>", JS_EVAL_TYPE_MODULE);
        // evaluate js code
        context.eval("let v1 = new my.MyClass();" "\n"
                     "v1.member_variable = 1;" "\n"
                     "let v2 = new my.MyClassA([1,2,3]);" "\n"
                     "function my_callback(str) {" "\n"
                     "  my.println(v2.member_function(str));" "\n"
                     "}" "\n"
        );

        // callback
        auto cb = (std::function<void(const std::string&)>) context.eval("my_callback");
        cb("world");
    }
    catch(qjs::exception)
    {
        auto exc = context.getException();
        std::cerr << (std::string) exc << std::endl;
        if((bool) exc["stack"])
            std::cerr << (std::string) exc["stack"] << std::endl;
        return 1;
    }
}

Installation

QuickJSPP is header-only - put quickjspp.hpp into your include search path. Compiler that supports C++17 or later is required. The program needs to be linked against QuickJS. Sample CMake project files are provided.

License

QuickJSPP is licensed under CC0. QuickJS is licensed under MIT.

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitnow/quickjspp.git
git@gitee.com:gitnow/quickjspp.git
gitnow
quickjspp
quickjspp
master

搜索帮助