- #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));
-