您的位置:首页 > Web前端 > JavaScript

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

2013-11-10 10:11 393 查看
#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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: