【问题标题】:How do I create wrapper for a v8::Persistent<v8::Function> to a c++ function如何为 v8::Persistent<v8::Function> 创建包装器到 c++ 函数
【发布时间】:2020-01-30 14:23:05
【问题描述】:

我有一个v8::Persistent&lt;v8::Function&gt;,需要将其转换为void(__cdecl*) 函数。有人建议我使用包装器,但我不确定如何做到这一点。

【问题讨论】:

标签: c++ wrapper v8


【解决方案1】:

例如这样:

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 中还有更多示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多