在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()字段,但它只允许Value类型,而ObjectTemplate的类型是Template,而不是Value。
如果能以正确的方式提供帮助,我们将不胜感激。
我终于想通了。
在javascript中,当您通过FunctionTemplate添加一个函数,然后将其作为构造函数(例如new MyFunction
)调用时,那么在c++回调中,args.This()
将是使用FunctionTemplate
的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: %sn", 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: %dn",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-动态变量
只是它没有谈到以可重复的方式制作对象的正确方法。
我想好了如何清理内部字段中的c++对象,但我没有时间把全部答案放在这里。您必须通过在堆上创建一个混合字段(结构运行良好)将全局对象传递到弱回调中,该字段同时具有全局对象和指向c++对象的指针。然后,您可以删除您的c++对象,在全局上调用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一个运行的机会(尽管它可能选择不运行)。
- 如何使用CLion在Mac上创建一个新的.txt文件
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- r-在Rcpp和C++之间转换矢量(使用Rcpp::as或Rcpp:::wrap)是否会创建一个新的矢量并复制元素
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 使用函数创建一个新变量
- 当球弹跳时(使用坐标)找到一个新的角度
- 为什么reverse_iterator无法使用forward Itererator分配一个新值
- 使用std :: ifstream打开一个新文件
- 如何创建一个新变量并同时在std::tie中使用它
- 在 Xcode 中创建一个新的"Shell Tool"目标以供使用 Google 测试
- 你能不能使用 JNI 在从 java 调用的 c++ 函数中创建一个新的 JVM
- 使用 Emgu CV C++声明一个新点
- 需要使用 QFileDialog 保存一个新文件
- 使用memcpy移动数组并在给定位置插入一个新元素
- 我可以在构建一个新的变量模板参数时使用一个变量模板参数吗
- 如何使用命令行参数创建一个新进程,并将PID提供给父进程
- 如何使用c++在%APPDATA%中创建一个新文件夹
- 使用一个"新"分配四个对象数组
- 使用eclipse cdt在c++中包含一个新文件