void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, const wchar_t* sConstName, v8::Local<v8::Value> pDefault) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); GetGlobalObjectTemplate(pIsolate)->Set( v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), pDefault, v8::ReadOnly); }
void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); GetGlobalObjectTemplate(pIsolate)->Set( v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); }
void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName, v8::FunctionCallback pConstGetter) { v8::Isolate::Scope isolate_scope(m_isolate); v8::HandleScope handle_scope(m_isolate); CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(m_isolate, pConstGetter); fun->RemovePrototype(); GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty( v8::String::NewFromUtf8(m_isolate, bsConst.c_str(), v8::NewStringType::kNormal) .ToLocalChecked(), fun); }
void CFXJS_Engine::DefineGlobalMethod(const wchar_t* sMethodName, v8::FunctionCallback pMethodCall) { v8::Isolate::Scope isolate_scope(m_isolate); v8::HandleScope handle_scope(m_isolate); CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(m_isolate, pMethodCall); fun->RemovePrototype(); GetGlobalObjectTemplate(m_isolate)->Set( v8::String::NewFromUtf8(m_isolate, bsMethodName.c_str(), v8::NewStringType::kNormal) .ToLocalChecked(), fun, v8::ReadOnly); }
void FXJS_InitializeRuntime( v8::Isolate* pIsolate, IJS_Runtime* pIRuntime, v8::Global<v8::Context>* pV8PersistentContext, std::vector<v8::Global<v8::Object>*>* pStaticObjects) { if (pIsolate == g_isolate) ++g_isolate_ref_count; v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); v8::Local<v8::Context> v8Context = v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); v8::Context::Scope context_scope(v8Context); FXJS_PerIsolateData::SetUp(pIsolate); v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); pStaticObjects->resize(maxID + 1); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked() ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(pIRuntime, v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked()); } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); v8::Local<v8::String> m_ObjName = v8::String::NewFromUtf8(pIsolate, bs.c_str(), v8::NewStringType::kNormal, bs.GetLength()).ToLocalChecked(); v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); } } pV8PersistentContext->Reset(pIsolate, v8Context); }
void CFXJS_Engine::InitializeEngine() { if (m_isolate == g_isolate) ++g_isolate_ref_count; v8::Isolate::Scope isolate_scope(m_isolate); v8::HandleScope handle_scope(m_isolate); // This has to happen before we call GetGlobalObjectTemplate because that // method gets the PerIsolateData from m_isolate. FXJS_PerIsolateData::SetUp(m_isolate); v8::Local<v8::Context> v8Context = v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate)); v8::Context::Scope context_scope(v8Context); v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); m_StaticObjects.resize(maxID + 1); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked() ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(this, v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked()); } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); v8::Local<v8::String> m_ObjName = v8::String::NewFromUtf8(m_isolate, bs.c_str(), v8::NewStringType::kNormal, bs.GetLength()) .ToLocalChecked(); v8::Local<v8::Object> obj = NewFxDynamicObj(i, true); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj); } } m_V8PersistentContext.Reset(m_isolate, v8Context); }