当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《在V8javascript引擎中,如何创建一个为每个实例重用ObjectTemplate的构造函数?》经验,为你挑选了1个好方法。

我有工作代码,我可以根据需要创建尽可能多的Point对象,但每次调用构造函数时它都会重新创建对象模板,这看起来可能是错误的.

Local 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& args) {
    HandleScope scope(args.GetIsolate());

    // this bit should probably be cached somehow
    Local 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 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()字段,但是它只允许一个Value类型,而ObjectTemplate的类型是Template,而不是Value.

任何有关正确方法的帮助将不胜感激.



1> xaxxon..:

我终于明白了.

在JavaScript中,当你通过FunctionTemplate增加一个功能,然后把它作为一个构造函数(例如new MyFunction),然后在你的C++回调的args.This()将是由使用创建新对象FunctionTemplateInstanceTemplate对象模板.

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

// create the function template and tell it the callback to use
Local 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 x_context = Context::New(isolate, NULL, global_templ);

然后,对于实际功能:

void v8_Point(const v8::FunctionCallbackInfo& 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 property,
               const PropertyCallbackInfo& info) {
  Local 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 wrap = Local::Cast(self->GetInternalField(0));
  void* ptr = wrap->Value();
  int value = static_cast(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 property, Local value,
               const PropertyCallbackInfo& info) {
  Local 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 wrap = Local::Cast(self->GetInternalField(0));
  void* ptr = wrap->Value();
  static_cast(ptr)->x_ = value->Int32Value();
}


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

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

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

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

请记住,v8不会垃圾收集少量内存,因此您可能永远不会看到它.此外,仅仅因为您的程序结束并不意味着GC将运行.你可以使用isolate-> AdjustAmountOfExternalAllocatedMemory(length); 告诉v8你已经分配的内存大小(它在计算中包含了这个内容,当时有太多的内存在使用并且GC需要运行)并且你可以使用它 isolate->IdleNotificationDeadline(1);来给GC一个运行的机会(虽然它可能选择不).

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有