v8学习---添加js全局函数

来源:互联网 发布:鼠标指针美化软件 编辑:程序博客网 时间:2024/05/01 00:26
#include <v8.h>using namespace v8; void test(const v8::FunctionCallbackInfo<Value>& args){    printf("Hello Headool\n");}int main(){    Isolate* isolate = Isolate::GetCurrent();    HandleScope handleScope(isolate);    Handle<ObjectTemplate> global = ObjectTemplate::New();    global->Set(String::New("test"), FunctionTemplate::New(test));    Handle<Context> context = Context::New(isolate, NULL, global);    Context::Scope context_scope(context);    Handle<Script> script = Script::Compile(String::New("test();"));    script->Run();    return 0;}

留意如下几点:

回调函数的类型为 void (*)(v8::FunctionCallbackInfo<v8::Value>&)或者 v8::Value (*)(v8::FunctionCallbackInfo<v8::Value>);

void test(const v8::FunctionCallbackInfo<Value>& args)
    Handle<Context> context = Context::New(isolate, NULL, global);
    global->Set(String::New("test"), FunctionTemplate::New(test));


原创粉丝点击