GitHub 您所在的位置:网站首页 functioncallbackinfo GitHub

GitHub

#GitHub| 来源: 网络整理| 查看: 265

Build status NPM Join the chat at https://gitter.im/pmed/v8pp

v8pp

Header-only library to expose C++ classes and functions into V8 to use them in JavaScript code. v8pp requires a compiler with C++17 support. The library has been tested on:

Microsoft Visual C++ 2019 (Windows 10) GCC 5.4.0 (Ubuntu 16.04) Clang 5.0.0 (Ubuntu 16.04) Building and testing

The library has a set of tests that can be configured, built, and run with CMake:

~/v8pp$ mkdir out; cd out ~/v8pp/out$ cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON .. ~/v8pp/out$ make ~/v8pp/out$ ctest -V

The full list of project options can be listed with cmake command:

~/v8pp/out$ cmake -LH ..

Some of them could be:

// Build documentation BUILD_DOCUMENTATION:BOOL=OFF

// Build shared library BUILD_SHARED_LIBS:BOOL=ON

// Build and run tests BUILD_TESTING:BOOL=OFF

// Header-only library V8PP_HEADER_ONLY:BOOL=0

// v8::Isolate data slot number, used in v8pp for shared data V8PP_ISOLATE_DATA_SLOT:STRING=0

// v8pp plugin initialization procedure name V8PP_PLUGIN_INIT_PROC_NAME:STRING=v8pp_module_init

// v8pp plugin filename suffix V8PP_PLUGIN_SUFFIX:STRING=.dylib

// Use new V8 ABI with V8_COMPRESS_POINTERS and V8_31BIT_SMIS_ON_64BIT_ARCH V8_COMPRESS_POINTERS:BOOL=ON

Binding example

v8pp supports V8 versions after 6.3 with v8::Isolate usage in API. There are 2 targets for binding:

v8pp::module, a wrapper class around v8::ObjectTemplate v8pp::class_, a template class wrapper around v8::FunctionTemplate

Both of them require a pointer to v8::Isolate instance. They allows to bind from C++ code such items as variables, functions, constants with a function set(name, item):

v8::Isolate* isolate; int var; int get_var() { return var + 1; } void set_var(int x) { var = x + 1; } struct X { X(int v, bool u) : var(v) {} int var; int get() const { return var; } void set(int x) { var = x; } }; // bind free variables and functions v8pp::module mylib(isolate); mylib // set read-only attribute .const_("PI", 3.1415) // set variable available in JavaScript with name `var` .var("var", var) // set function get_var as `fun` .function("fun", &get_var) // set property `prop` with getter get_var() and setter set_var() .property("prop", get_var, set_var); // bind class v8pp::class_ X_class(isolate); X_class // specify X constructor signature .ctor() // bind variable .var("var", &X::var) // bind function .function("fun", &X::set) // bind read-only property .property("prop",&X::get); // set class into the module template mylib.class_("X", X_class); // set bindings in global object as `mylib` isolate->GetCurrentContext()->Global()->Set( v8::String::NewFromUtf8(isolate, "mylib"), mylib.new_instance());

After that bindings will be available in JavaScript:

mylib.var = mylib.PI + mylib.fun(); var x = new mylib.X(1, true); mylib.prop = x.prop + x.fun(); Node.js and io.js addons

The library is suitable to make Node.js and io.js addons. See addons document.

void RegisterModule(v8::Local exports) { v8pp::module addon(v8::Isolate::GetCurrent()); // set bindings... addon .function("fun", &function) .class_("cls", my_class) ; // set bindings as exports object prototype exports->SetPrototype(addon.new_instance()); } v8pp also provides v8pp - a static library to add several global functions (load/require to the v8 JavaScript context. require() is a system for loading plugins from shared libraries. test - A binary for running JavaScript files in a context which has v8pp module loading functions provided. v8pp module example #include #include namespace console { void log(v8::FunctionCallbackInfo const& args) { v8::HandleScope handle_scope(args.GetIsolate()); for (int i = 0; i < args.Length(); ++i) { if (i > 0) std::cout #include #include #include namespace file { bool rename(char const* src, char const* dest) { return std::rename(src, dest) == 0; } class file_base { public: bool is_open() const { return stream_.is_open(); } bool good() const { return stream_.good(); } bool eof() const { return stream_.eof(); } void close() { stream_.close(); } protected: std::fstream stream_; }; class file_writer : public file_base { public: explicit file_writer(v8::FunctionCallbackInfo const& args) { if (args.Length() == 1) { v8::String::Utf8Value str(args[0]); open(*str); } } bool open(char const* path) { stream_.open(path, std::ios_base::out); return stream_.good(); } void print(v8::FunctionCallbackInfo const& args) { v8::HandleScope scope(args.GetIsolate()); for (int i = 0; i < args.Length(); ++i) { if (i > 0) stream_ #include v8pp::context context; context.set_lib_path("path/to/plugins/lib"); // script can now use require() function. An application // that uses v8pp::context must link against v8pp library. v8::HandleScope scope(context.isolate()); context.run_file("some_file.js"); Using require() from JavaScript


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有