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

 

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));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值