在 V8 javascript 引擎中,如何为每个实例创建一个重用 ObjectTemplate 的构造函数?

In the V8 javascript engine, how to make a constructor function that re-uses an ObjectTemplate for each instance?

我有工作代码,可以根据需要创建任意数量的 Point 对象,但每次调用构造函数时它都会重新创建对象模板,这似乎可能是错误的。

Local<ObjectTemplate> global_templ = ObjectTemplate::New(isolate);

// make the Point constructor function available to JS
global_templ->Set(v8::String::NewFromUtf8(isolate, "Point"), FunctionTemplate::New(isolate, v8_Point));

然后是构造函数本身:

void v8_Point(const v8::FunctionCallbackInfo<v8::Value>& args) {
    HandleScope scope(args.GetIsolate());

    // this bit should probably be cached somehow
    Local<ObjectTemplate> point_template = ObjectTemplate::New(args.GetIsolate());
    point_template->SetInternalFieldCount(1);

    point_template->SetAccessor(String::NewFromUtf8(args.GetIsolate(), "x"), GetPointX, SetPointX);
    point_template->SetAccessor(String::NewFromUtf8(args.GetIsolate(), "y"), GetPointY, SetPointY);
    // end section to be cached

    Local<Object> obj = point_template->NewInstance();
    Point * p = new Point(1,1);
    obj->SetInternalField(0, External::New(args.GetIsolate(), p));
    args.GetReturnValue().Set(obj);
}

但似乎我应该能够传入 point_template 对象,而不是每次都重新创建它。我看到 args 中有一个 Data() 字段,但它只允许一个值类型,而一个 ObjectTemplate 是模板类型,而不是值类型。

如果您能以正确的方式做到这一点,我们将不胜感激。

我终于想通了。

在 javascript 中,当您通过 FunctionTemplate 添加一个函数,然后将其作为构造函数调用时(例如 new MyFunction),那么在您的 c++ 回调中,args.This() 将是一个使用 FunctionTemplateInstanceTemplate 对象模板创建的新对象。

// Everything has to go in a single global template (as I understand)
Local<ObjectTemplate> global_templ = ObjectTemplate::New(isolate);

// create the function template and tell it the callback to use
Local<FunctionTemplate> point_constructor = FunctionTemplate::New(isolate, v8_Point);

// set the internal field count so our actual c++ object can tag along
//   with the javascript object so our accessors can use it
point_constructor->InstanceTemplate()->SetInternalFieldCount(1);

// associate getters and setters for the 'x' field on point
point_constructor->InstanceTemplate()->SetAccessor(String::NewFromUtf8(isolate, "x"), GetPointX, SetPointX);

... add any other function and object templates to the global template ...

// add the global template to the context our javascript will run in
Local<Context> x_context = Context::New(isolate, NULL, global_templ);

那么,对于实际的功能:

void v8_Point(const v8::FunctionCallbackInfo<v8::Value>& args) {

    // (just an example of a handy utility function)
    // whether or not it was called as "new Point()" or just "Point()"
    printf("Is constructor call: %s\n", args.IsConstructCall()?"yes":"no");

    // create your c++ object that will follow the javascript object around 
    // make sure not to make it on the stack or it won't be around later when you need it
    Point * p = new Point();

    // another handy helper function example
    // see how the internal field count is what it was set to earlier
    //   in the InstanceTemplate
    printf("Internal field count: %d\n",args.This()->InternalFieldCount()); // this prints the value '1'

    // put the new Point object into the internal field
    args.This()->SetInternalField(0, External::New(args.GetIsolate(), p));

    // return the new object back to the javascript caller
    args.GetReturnValue().Set(args.This());
}

现在,当您编写 getter 和 setter 时,您可以访问它们主体中的实际 c++ 对象:

void GetPointX(Local<String> property,
               const PropertyCallbackInfo<Value>& info) {
  Local<Object> self = info.Holder();

  // This is where we take the actual c++ object that was embedded
  //   into the javascript object and get it back to a useable c++ object
  Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
  void* ptr = wrap->Value();
  int value = static_cast<Point*>(ptr)->x_; //x_ is the name of the field in the c++ object

  // return the value back to javascript
  info.GetReturnValue().Set(value);
}

void SetPointX(Local<String> property, Local<Value> value,
               const PropertyCallbackInfo<void>& info) {
  Local<Object> self = info.Holder();

  // same concept here as in the "getter" above where you get access
  //   to the actual c++ object and then set the value from javascript
  //   into the actual c++ object field
  Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
  void* ptr = wrap->Value();
  static_cast<Point*>(ptr)->x_ = value->Int32Value();
}

几乎所有这些都来自这里:https://developers.google.com/v8/embed?hl=en#accessing-dynamic-variables

除了它没有讨论以可重复的方式制作对象的正确方法。

我想出了如何清理内部字段中的 c++ 对象,但我没有时间将整个答案放在这里。您必须通过在具有全局对象和指向您的 c++ 对象的指针的堆上创建一个混合字段(一个结构很好),将一个全局对象传递到您的弱回调中。然后您可以删除您的 c++ 对象,在您的 Global 上调用 Reset() ,然后删除整个对象。我会尝试添加实际代码,但可能会忘记。

这是一个很好的来源:https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/d8.cc&l=1064 第 1400-1441 行是您想要的。 (编辑:行号现在似乎是错误的 - 也许上面的 link 已经改变了?)

请记住,v8 不会对少量内存进行垃圾回收,因此您可能永远看不到它。此外,仅仅因为您的程序结束并不意味着 GC 将 运行。您可以使用 isolate->AdjustAmountOfExternalAllocatedMemory(length);告诉 v8 您分配的内存大小(它在计算何时使用了太多内存并且 GC 需要 运行)时可以使用 isolate->IdleNotificationDeadline(1); 来提供GC 有机会 运行(尽管它可能选择不这样做)。