soui使用v8脚本

 

1. v8来自扫地僧的miniblink49中v8_5_1 ;

2. 目前只导出了一个SMessageBox函数;

3. uires:

  a.uires.idx中添加选中的内容;

  

      b. uires目录下增加js/v8_test.js文件

       

      c. dlg_main.xml中增加

      

     

4. 导出函数代码:

   

 1 void SScriptModule_v8::executeScriptBuffer( const char* buff, size_t sz )
 2     {
 3         Isolate::Scope isolate_scope(m_isolate);
 4 
 5         // 创建一个分配在栈上的handle scope.
 6         static HandleScope handle_scope(m_isolate);
 7         m_handle_scope = &handle_scope;
 8 
 9         //
10         Local<ObjectTemplate> object_template = ObjectTemplate::New(m_isolate);
11         m_execute_context = Context::New(m_isolate, NULL, object_template);
12 
13         // 关联context
14         Context::Scope context_scope(m_execute_context);
15 
16         //
17         Local<String> source = String::NewFromUtf8(m_isolate, buff,
18             NewStringType::kNormal).ToLocalChecked();
19 
20         v8::Local<v8::String> name =
21             v8::String::NewFromUtf8(m_isolate, "unname_soui",
22             v8::NewStringType::kNormal).ToLocalChecked();
23         v8::ScriptOrigin origin(name);
24 
25         v8::Local<v8::Script> script;
26         Script::Compile(m_execute_context, source, &origin).ToLocal(&script);
27         if (script.IsEmpty())
28         {
29             STRACE(_T("Compile v8 script error!\n"));
30             return;
31         }
32 
33         script->Run();
34 
35         //导出一个函数
36         Local<ObjectTemplate> drawer_template = ObjectTemplate::New();
37         drawer_template->SetInternalFieldCount(1);
38 
39         Local<String> str1 = String::NewFromUtf8(m_isolate, "SMessageBox", NewStringType::kNormal)
40             .ToLocalChecked();
41 
42         drawer_template->Set(str1, FunctionTemplate::New(m_isolate, EXP_SMessageBox));
43 
44         
45         m_fun_obj = drawer_template->NewInstance();
46         
47         m_fun_obj->SetInternalField(0, External::New(m_isolate, NULL));
48         Local<Object> gObj = m_execute_context->Global();
49 
50         Maybe<bool> bSet = gObj->Set(m_execute_context, 
51             String::NewFromUtf8(m_isolate, "global", 
52             NewStringType::kNormal).ToLocalChecked(), m_fun_obj);
53 
54         return;
55     }

 

 

5. 测试Demo:https://pan.baidu.com/s/1bo3JmYb            pbpj

 

转载于:https://www.cnblogs.com/gray1121/p/7070755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值