1. v8來自掃地僧的miniblink49中v8_5_1 ;函數
2. 目前只導出了一個SMessageBox函數;測試
3. uires:ui
a.uires.idx中添加選中的內容;spa
b. uires目錄下增長js/v8_test.js文件code
c. dlg_main.xml中增長orm
4. 導出函數代碼:xml
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 pbpjblog