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

2023-11-09

标签: v8
2013-11-10 10:11  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));  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

v8学习---添加js全局函数 的相关文章

随机推荐