如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器

How do I create wrapper for a v8::Persistent<v8::Function> to a c++ function

本文关键字:v8 c++ 函数 包装 创建 gt ersistent lt Function      更新时间:2023-10-16

我有一个v8::Persistent<v8::Function>,我需要把它变成一个void(__cdecl*)函数。有人建议我使用包装器,但我不确定如何做到这一点。

例如这样:

v8::Persistent<v8::Function> func = ...;
v8::Persistent<v8::Context> context = ...;
v8::Isolate* isolate = ...;
void cpp_func(...) {
v8::Local<v8::Context> ctx = v8::Local<v8::Context>::New(isolate, context);
// The "this" inside the JavaScript function:
v8::Local<v8::Object> arg_this = ctx->Global();
// Arguments to the JavaScript function, of type `v8::Local<v8::Value>[]`.
int argc = 0;
int argv = nullptr;
func->Call(ctx, arg_this, argc, argv);
}

V8 test-api.cc 中还有更多的例子。