仅标头库,用于将 C++ 类和函数公开到 V8 中,以便在 JavaScript 代码中使用它们。 v8pp 需要支持 C++17 的编译器。该库已经过测试:
该库有一组可以使用 CMake 配置、构建和运行的测试:
~/v8pp$ mkdir out; cd out
~/v8pp/out$ cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON ..
~/v8pp/out$ make
~/v8pp/out$ ctest -V
可以使用 cmake 命令列出项目选项的完整列表:
~/v8pp/out$ cmake -LH ..
其中一些可能是:
// 构建文档 BUILD_DOCUMENTATION:BOOL=OFF
// 构建共享库 BUILD_SHARED_LIBS:BOOL=ON
// 构建并运行测试 BUILD_TESTING:BOOL=OFF
// 仅标头库 V8PP_HEADER_ONLY:BOOL=0
// v8::隔离数据槽号,在v8pp中用于共享数据 V8PP_ISOLATE_DATA_SLOT:STRING=0
// v8pp插件初始化过程名称 V8PP_PLUGIN_INIT_PROC_NAME:STRING=v8pp_module_init
// v8pp插件文件名后缀 V8PP_PLUGIN_SUFFIX:STRING=.dylib
// 使用新的 V8 ABI 和 V8_COMPRESS_POINTERS 和 V8_31BIT_SMIS_ON_64BIT_ARCH V8_COMPRESS_POINTERS:BOOL=ON
v8pp 支持 6.3 之后的 V8 版本,API 中使用v8::Isolate
。绑定的目标有2个:
v8pp::module
, v8::ObjectTemplate
的包装类v8pp::class_
, v8::FunctionTemplate
的模板类包装器它们都需要一个指向v8::Isolate
实例的指针。它们允许从 C++ 代码中绑定变量、函数、常量等项目与函数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> X_class (isolate);
X_class
// specify X constructor signature
.ctor< int , bool >()
// 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());
之后绑定将在 JavaScript 中可用:
mylib . var = mylib . PI + mylib . fun ( ) ;
var x = new mylib . X ( 1 , true ) ;
mylib . prop = x . prop + x . fun ( ) ;
该库适合制作 Node.js 和 io.js 插件。请参阅插件文档。
void RegisterModule (v8::Local<v8::Object> 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
- 一个静态库,用于添加多个全局函数(加载/需要 v8 JavaScript 上下文。require require()
是一个用于从共享库加载插件的系统。test
- 用于在提供 v8pp 模块加载功能的上下文中运行 JavaScript 文件的二进制文件。 # include < iostream >
# include < v8pp/module.hpp >
namespace console {
void log (v8::FunctionCallbackInfo<v8::Value> const & args)
{
v8::HandleScope handle_scope (args. GetIsolate ());
for ( int i = 0 ; i < args. Length (); ++i)
{
if (i > 0 ) std::cout << ' ' ;
v8::String::Utf8Value str (args[i]);
std::cout << *str;
}
std::cout << std::endl;
}
v8::Local<v8::Value> init (v8::Isolate* isolate)
{
v8pp::module m (isolate);
m. function ( " log " , & log );
return m. new_instance ();
}
} // namespace console
V8PP_PLUGIN_INIT (v8::Isolate* isolate)
{
return console::init (isolate);
}
# include < v8pp/module.hpp >
# include < v8pp/class.hpp >
# include < fstream >
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<v8::Value> 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<v8::Value> const & args)
{
v8::HandleScope scope (args. GetIsolate ());
for ( int i = 0 ; i < args. Length (); ++i)
{
if (i > 0 ) stream_ << ' ' ;
v8::String::Utf8Value str (args[i]);
stream_ << *str;
}
}
void println (v8::FunctionCallbackInfo<v8::Value> const & args)
{
print (args);
stream_ << std::endl;
}
};
class file_reader : public file_base
{
public:
explicit file_reader ( char const * path)
{
open (path);
}
bool open ( const char * path)
{
stream_. open (path, std::ios_base::in);
return stream_. good ();
}
v8::Local<v8::Value> getline (v8::Isolate* isolate)
{
if ( stream_. good () && ! stream_. eof ())
{
std::string line;
std::getline (stream_, line);
return v8pp::to_v8 (isolate, line);
}
else
{
return v8::Undefined (isolate);
}
}
};
v8::Local<v8::Value> init (v8::Isolate* isolate)
{
v8::EscapableHandleScope scope (isolate);
// file_base binding, no .ctor() specified, object creation disallowed in JavaScript
v8pp::class_<file_base> file_base_class (isolate);
file_base_class
. function ( " close " , &file_base:: close )
. function ( " good " , &file_base::good)
. function ( " is_open " , &file_base::is_open)
. function ( " eof " , &file_base::eof)
;
// .ctor<> template arguments declares types of file_writer constructor
// file_writer inherits from file_base_class
v8pp::class_<file_writer> file_writer_class (isolate);
file_writer_class
. ctor <v8::FunctionCallbackInfo<v8::Value> const &>()
. inherit <file_base>()
. function ( " open " , &file_writer:: open )
. function ( " print " , &file_writer::print)
. function ( " println " , &file_writer::println)
;
// .ctor<> template arguments declares types of file_reader constructor.
// file_base inherits from file_base_class
v8pp::class_<file_reader> file_reader_class (isolate);
file_reader_class
. ctor < char const *>()
. inherit <file_base>()
. function ( " open " , &file_reader:: open )
. function ( " getln " , &file_reader:: getline )
;
// Create a module to add classes and functions to and return a
// new instance of the module to be embedded into the v8 context
v8pp::module m (isolate);
m. function ( " rename " , & rename )
. class_ ( " writer " , file_writer_class)
. class_ ( " reader " , file_reader_class)
;
return scope. Escape (m. new_instance ());
}
} // namespace file
V8PP_PLUGIN_INIT (v8::Isolate* isolate)
{
return file::init (isolate);
}
# include < v8pp/context.hpp >
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 " );
// Load the file module from the class binding example and the
// console module.
var file = require ( 'file' ) ,
console = require ( 'console' )
var writer = new file . writer ( "file" )
if ( writer . is_open ( ) ) {
writer . println ( "some text" )
writer . close ( )
if ( ! file . rename ( "file" , "newfile" ) )
console . log ( "could not rename file" )
}
else console . log ( "could not open `file'" )
console . log ( "exit" )
// Memory for C++ class will remain when JavaScript object is deleted.
// Useful for classes you only wish to inject.
typedef v8pp::class_<my_class> my_class_wrapper;
v8::Local<v8::Value> val = my_class_wrapper::reference_external(isolate, & my_class::instance ());
// Assuming my_class::instance() returns reference to class
// Memory for c++ object will be reclaimed by JavaScript using "delete" when
// JavaScript class is deleted.
typedef v8pp::class_<my_class> my_class_wrapper;
v8::Local<v8::Value> val = my_class_wrapper::import_external(isolate, new my_class);
该库使用多个预处理器宏,在v8pp/config.hpp
文件中定义:
V8PP_ISOLATE_DATA_SLOT
- v8::Isolate 数据槽号,用于存储 v8pp 内部数据V8PP_PLUGIN_INIT_PROC_NAME
- 应从 v8pp 插件导出的插件初始化过程名称。V8PP_PLUGIN_SUFFIX
- 如果require()
中使用的插件名称不以其结尾,则将添加插件文件名后缀。V8PP_HEADER_ONLY
- 使用仅标头实现,默认启用。