Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

QuickJS C++ wrapper

NotificationsYou must be signed in to change notification settings

ftk/quickjspp

Repository files navigation

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>classMyClass{public:MyClass() {}MyClass(std::vector<int>) {}double member_variable =5.5;    std::stringmember_function(const std::string& s) {return"Hello," + s; }};voidprintln(qjs::rest<std::string> args) {for (autoconst & arg : args) std::cout << arg <<"";    std::cout <<"\n";}intmain(){    qjs::Runtime runtime;    qjs::Contextcontext(runtime);try    {// export classes as a moduleauto& 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(R"xxx(            import * as my from 'MyModule';            globalThis.my = my;)xxx","<import>", JS_EVAL_TYPE_MODULE);// evaluate js code        context.eval(R"xxx(            let v1 = new my.MyClass();            v1.member_variable = 1;            let v2 = new my.MyClassA([1,2,3]);            function my_callback(str) {              my.println("at callback:", v2.member_function(str));            })xxx");// callbackauto 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;return1;    }}

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 underCC0. QuickJS is licensed underMIT.


[8]ページ先頭

©2009-2025 Movatter.jp