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 CFXJS_Engine::DefineObjConst(int nObjDefnID, const wchar_t* sConstName, v8::Local<v8::Value> pDefault) { v8::Isolate::Scope isolate_scope(m_isolate); v8::HandleScope handle_scope(m_isolate); CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); pObjDef->GetInstanceTemplate()->Set(m_isolate, bsConstName.c_str(), pDefault); }
void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, int nObjDefnID, v8::NamedPropertyQueryCallback pPropQurey, v8::NamedPropertyGetterCallback pPropGet, v8::NamedPropertySetterCallback pPropPut, v8::NamedPropertyDeleterCallback pPropDel) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); }
static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( v8::Isolate* pIsolate) { int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) return pObjDef->GetInstanceTemplate(); } if (!g_DefaultGlobalObjectTemplate) { g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; g_DefaultGlobalObjectTemplate->Reset(pIsolate, v8::ObjectTemplate::New(pIsolate)); } return g_DefaultGlobalObjectTemplate->Get(pIsolate); }
void FXJS_DefineObjProperty(v8::Isolate* pIsolate, int nObjDefnID, const wchar_t* sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); pObjDef->GetInstanceTemplate()->SetAccessor( v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), pPropGet, pPropPut); }
void FXJS_DefineObjMethod(v8::Isolate* pIsolate, int nObjDefnID, 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(); CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); pObjDef->GetInstanceTemplate()->Set( v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), v8::FunctionTemplate::New(pIsolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature()), v8::ReadOnly); }
static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( v8::Isolate* pIsolate) { int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) return pObjDef->GetInstanceTemplate(); } if (!g_DefaultGlobalObjectTemplate) { v8::Local<v8::ObjectTemplate> hGlobalTemplate = v8::ObjectTemplate::New(pIsolate); hGlobalTemplate->Set( v8::Symbol::GetToStringTag(pIsolate), v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) .ToLocalChecked()); g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>(pIsolate, hGlobalTemplate); } return g_DefaultGlobalObjectTemplate->Get(pIsolate); }
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; }