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

来源:互联网 发布:淘宝自动发货软件源码 编辑:程序博客网 时间:2024/05/21 18:41
 

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

标签: v8
 951人阅读 评论(0) 收藏 举报
 分类:
[cpp] view plain copy
  1. #include <v8.h>  
  2. using namespace v8;   
  3.   
  4. void test(const v8::FunctionCallbackInfo<Value>& args)  
  5. {  
  6.     printf("Hello Headool\n");  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     Isolate* isolate = Isolate::GetCurrent();  
  12.     HandleScope handleScope(isolate);  
  13.     Handle<ObjectTemplate> global = ObjectTemplate::New();  
  14.     global->Set(String::New("test"), FunctionTemplate::New(test));  
  15.     Handle<Context> context = Context::New(isolate, NULL, global);  
  16.     Context::Scope context_scope(context);  
  17.     Handle<Script> script = Script::Compile(String::New("test();"));  
  18.     script->Run();  
  19.     return 0;  
  20. }  

留意如下几点:

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

[cpp] view plain copy
  1. void test(const v8::FunctionCallbackInfo<Value>& args)  
[cpp] view plain copy
  1. Handle<Context> context = Context::New(isolate, NULL, global);  
[cpp] view plain copy
  1. global->Set(String::New("test"), FunctionTemplate::New(test));  
0 0