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

 

posted on 2017-06-23 16:54  Nick.Zeng  阅读(303)  评论(0编辑  收藏  举报

导航