我有工作代码,我可以根据需要创建尽可能多的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()字段,但是它只允许一个Value类型,而ObjectTemplate的类型是Template,而不是Value。
任何有关正确方法的帮助将不胜感激。
答案 0 :(得分:6)
我终于明白了。
在javascript中,当您通过FunctionTemplate添加函数然后将其作为构造函数(例如new MyFunction
)调用时,那么在您的c ++回调中,args.This()
将是由使用创建的新对象FunctionTemplate
&#39; InstanceTemplate
对象模板。
// 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 ++对象,但是我没有时间将整个答案放在这里。您必须通过在堆上创建混合字段(结构很好)来将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-&gt; AdjustAmountOfExternalAllocatedMemory(length);告诉v8你已经分配的内存大小(在计算中包含了这个内容,关于何时使用的内存太多而GC需要运行)你可以使用isolate->IdleNotificationDeadline(1);
来给GC一个运行的机会(尽管它可能选择不运行)。