v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, IJS_Runtime* pIRuntime, int nObjDefnID) { v8::Isolate::Scope isolate_scope(pIsolate); v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); if (nObjDefnID == -1) { v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); v8::Local<v8::Object> obj; if (!objTempl->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>(); return obj; } FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); if (!pData) return v8::Local<v8::Object>(); if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) return v8::Local<v8::Object>(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); v8::Local<v8::Object> obj; if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>(); obj->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(nObjDefnID)); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(pIRuntime, obj); return obj; }
void FXJS_InitializeRuntime(v8::Isolate* pIsolate, IFXJS_Runtime* pFXRuntime, IFXJS_Context* context, v8::Global<v8::Context>& v8PersistentContext) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); v8::Global<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pIsolate); v8::Local<v8::Context> v8Context = v8::Context::New( pIsolate, NULL, v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); v8::Context::Scope context_scope(v8Context); FXJS_PerIsolateData::SetUp(pIsolate); v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); CFX_WideString ws = CFX_WideString(pObjDef->objName); CFX_ByteString bs = ws.UTF8Encode(); v8::Local<v8::String> objName = v8::String::NewFromUtf8(pIsolate, bs.c_str(), v8::NewStringType::kNormal, bs.GetLength()).ToLocalChecked(); if (pObjDef->objType == FXJS_DYNAMIC) { // Document is set as global object, need to construct it first. if (ws.Equal(L"Document")) { v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked() ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(context, v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked(), v8Context->Global() ->GetPrototype() ->ToObject(v8Context) .ToLocalChecked()); } } else { v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); v8Context->Global()->Set(v8Context, objName, obj).FromJust(); pObjDef->m_StaticObj.Reset(pIsolate, obj); } } v8PersistentContext.Reset(pIsolate, v8Context); }
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); }
v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, IFXJS_Context* pJSContext, int nObjDefnID) { v8::Isolate::Scope isolate_scope(pIsolate); v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); if (nObjDefnID == -1) { v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); v8::Local<v8::Object> obj; if (objTempl->NewInstance(context).ToLocal(&obj)) return obj; return v8::Local<v8::Object>(); } FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); if (!pData) return v8::Local<v8::Object>(); if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) return v8::Local<v8::Object>(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); v8::Local<v8::Object> obj; if (!objTemp->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>(); obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor( pJSContext, obj, context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); return obj; }
v8::Local<v8::Object> CFXJS_Engine::NewFxDynamicObj(int nObjDefnID, bool bStatic) { v8::Isolate::Scope isolate_scope(m_isolate); v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); if (nObjDefnID == -1) { v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate); v8::Local<v8::Object> obj; if (!objTempl->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>(); return obj; } FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); if (!pData) return v8::Local<v8::Object>(); if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate)) return v8::Local<v8::Object>(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); v8::Local<v8::Object> obj; if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>(); CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); obj->SetAlignedPointerInInternalField(0, pPerObjData); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(this, obj); if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap) { FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pPerObjData, obj); } return obj; }