存储从 FunctionCallbackInfo 检索到的函数
storing a function that was retrieved from FunctionCallbackInfo
我非常想做一个 AddInputEvent 但是,一个月后,找不到一种方法来转换本地“来自 FunctionCallbackInfo 的函数”(我会只需将此 argf) 调用到持久函数中,这样垃圾收集就不会擦除指针。
我能找到的大多数 stakeoverflow 线程和示例代码只是说使用本地函数来转换 argf;然后将其放入 Persistent New。这导致 error: cannot convert 'v8::Local<v8::Function>' to 'v8::Function*'
这是代码,不太清楚为什么我不能转换它
class inputevnt_feld{
public:
char* call_on;
v8::Persistent<v8::Function> func;
};
int entvcount = -1;
vector<inputevnt_feld> event_calls; //this is pretty much a array of events that we can call later
// in js looks like this "AddInputEvent("string", function);"
void AddInputEvent( const v8::FunctionCallbackInfo<v8::Value>& args ) {
v8::HandleScope handle_scope(args.GetIsolate());
//gotta make sure that we ain't letting in some trojan horse that has nothing in it
if (args[1]->IsFunction() && args[0]->IsString()) {
inputevnt_feld newt;
//converts js string to char array
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
const char* cstr = ToCString(str);
newt.call_on = (char*)cstr;
//here is where the problem is with function casting
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
newt.func = v8::Persistent<v8::Function>::New(args.GetIsolate(), callback);
//push the new stuff in to even array
event_calls.push_back(newt);
//getting vector array size is too much for my smol brain
//so I'ma just do this myself
entvcount++;
//cout << event_calls[entvcount].call_on << endl; //debug
}
}
Most stakeoverflow threads and example code I can find just say to Cast argf with a Local Function; then to throw that in to a Persistent New
是的,没错。如果你知道如何阅读它,C++ 类型系统是你了解细节的朋友。
如果您查看 v8::PersistentBase<T>::New
的定义,您会发现它需要一个 T*
(对于它的模板类型 T
)。如果您查看 v8::Local<T>
class,您会发现从中获取 T*
的一种方法是使用其 operator*
。这导致:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>::New(..., *callback);
或者,您可以直接使用 Persistent
构造函数,并在不先取消引用的情况下将其传递给 Local
:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>(..., callback);
这两个选项是完全等价的。就我个人而言,我更喜欢后者,因为拼写所需的字符略少,但这确实是唯一的区别。
(您当前发布的代码做了其他事情:它忽略了强制转换的结果并将原始的 args[1]
直接传递给 Persistent::New
—— 这是行不通的。)
我非常想做一个 AddInputEvent 但是,一个月后,找不到一种方法来转换本地“来自 FunctionCallbackInfo 的函数”(我会只需将此 argf) 调用到持久函数中,这样垃圾收集就不会擦除指针。
我能找到的大多数 stakeoverflow 线程和示例代码只是说使用本地函数来转换 argf;然后将其放入 Persistent New。这导致 error: cannot convert 'v8::Local<v8::Function>' to 'v8::Function*'
这是代码,不太清楚为什么我不能转换它
class inputevnt_feld{
public:
char* call_on;
v8::Persistent<v8::Function> func;
};
int entvcount = -1;
vector<inputevnt_feld> event_calls; //this is pretty much a array of events that we can call later
// in js looks like this "AddInputEvent("string", function);"
void AddInputEvent( const v8::FunctionCallbackInfo<v8::Value>& args ) {
v8::HandleScope handle_scope(args.GetIsolate());
//gotta make sure that we ain't letting in some trojan horse that has nothing in it
if (args[1]->IsFunction() && args[0]->IsString()) {
inputevnt_feld newt;
//converts js string to char array
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
const char* cstr = ToCString(str);
newt.call_on = (char*)cstr;
//here is where the problem is with function casting
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
newt.func = v8::Persistent<v8::Function>::New(args.GetIsolate(), callback);
//push the new stuff in to even array
event_calls.push_back(newt);
//getting vector array size is too much for my smol brain
//so I'ma just do this myself
entvcount++;
//cout << event_calls[entvcount].call_on << endl; //debug
}
}
Most stakeoverflow threads and example code I can find just say to Cast argf with a Local Function; then to throw that in to a Persistent New
是的,没错。如果你知道如何阅读它,C++ 类型系统是你了解细节的朋友。
如果您查看 v8::PersistentBase<T>::New
的定义,您会发现它需要一个 T*
(对于它的模板类型 T
)。如果您查看 v8::Local<T>
class,您会发现从中获取 T*
的一种方法是使用其 operator*
。这导致:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>::New(..., *callback);
或者,您可以直接使用 Persistent
构造函数,并在不先取消引用的情况下将其传递给 Local
:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>(..., callback);
这两个选项是完全等价的。就我个人而言,我更喜欢后者,因为拼写所需的字符略少,但这确实是唯一的区别。
(您当前发布的代码做了其他事情:它忽略了强制转换的结果并将原始的 args[1]
直接传递给 Persistent::New
—— 这是行不通的。)