Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptInterface.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
* @file | * @file | ||||
* Abstractions of various SpiderMonkey features. | * Abstractions of various SpiderMonkey features. | ||||
* Engine code should be using functions of these interfaces rather than | * Engine code should be using functions of these interfaces rather than | ||||
* directly accessing the underlying JS api. | * directly accessing the underlying JS api. | ||||
*/ | */ | ||||
struct ScriptInterface_impl | struct ScriptInterface_impl | ||||
{ | { | ||||
ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context); | ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context, JS::Compartment* compartment); | ||||
~ScriptInterface_impl(); | ~ScriptInterface_impl(); | ||||
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted | // Take care to keep this declaration before heap rooted members. Destructors of heap rooted | ||||
// members have to be called before the context destructor. | // members have to be called before the context destructor. | ||||
std::shared_ptr<ScriptContext> m_context; | std::shared_ptr<ScriptContext> m_context; | ||||
friend ScriptRequest; | friend ScriptRequest; | ||||
private: | private: | ||||
▲ Show 20 Lines • Show All 231 Lines • ▼ Show 20 Lines | bool ScriptInterface::Math_random(JSContext* cx, uint argc, JS::Value* vp) | ||||
double r; | double r; | ||||
if (!ScriptInterface::CmptPrivate::GetScriptInterface(cx).MathRandom(r)) | if (!ScriptInterface::CmptPrivate::GetScriptInterface(cx).MathRandom(r)) | ||||
return false; | return false; | ||||
args.rval().setNumber(r); | args.rval().setNumber(r); | ||||
return true; | return true; | ||||
} | } | ||||
ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context) : | ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context, JS::Compartment* compartment) : | ||||
m_context(context), m_cx(context->GetGeneralJSContext()), m_glob(context->GetGeneralJSContext()), m_nativeScope(context->GetGeneralJSContext()) | m_context(context), m_cx(context->GetGeneralJSContext()), m_glob(context->GetGeneralJSContext()), m_nativeScope(context->GetGeneralJSContext()) | ||||
{ | { | ||||
JS::RealmCreationOptions creationOpt; | JS::RealmCreationOptions creationOpt; | ||||
// Keep JIT code during non-shrinking GCs. This brings a quite big performance improvement. | // Keep JIT code during non-shrinking GCs. This brings a quite big performance improvement. | ||||
creationOpt.setPreserveJitCode(true); | creationOpt.setPreserveJitCode(true); | ||||
// Enable uneval | // Enable uneval | ||||
creationOpt.setToSourceEnabled(true); | creationOpt.setToSourceEnabled(true); | ||||
if (compartment) | |||||
creationOpt.setExistingCompartment(compartment); | |||||
else | |||||
// This is the default behaviour. | |||||
creationOpt.setNewCompartmentAndZone(); | |||||
JS::RealmOptions opt(creationOpt, JS::RealmBehaviors{}); | JS::RealmOptions opt(creationOpt, JS::RealmBehaviors{}); | ||||
m_glob = JS_NewGlobalObject(m_cx, &global_class, nullptr, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt); | m_glob = JS_NewGlobalObject(m_cx, &global_class, nullptr, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt); | ||||
JSAutoRealm autoRealm(m_cx, m_glob); | JSAutoRealm autoRealm(m_cx, m_glob); | ||||
ENSURE(JS::InitRealmStandardClasses(m_cx)); | ENSURE(JS::InitRealmStandardClasses(m_cx)); | ||||
Show All 17 Lines | |||||
} | } | ||||
ScriptInterface_impl::~ScriptInterface_impl() | ScriptInterface_impl::~ScriptInterface_impl() | ||||
{ | { | ||||
m_context->UnRegisterRealm(JS::GetObjectRealmOrNull(m_glob)); | m_context->UnRegisterRealm(JS::GetObjectRealmOrNull(m_glob)); | ||||
} | } | ||||
ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugName, const std::shared_ptr<ScriptContext>& context) : | ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugName, const std::shared_ptr<ScriptContext>& context) : | ||||
m(std::make_unique<ScriptInterface_impl>(nativeScopeName, context)) | m(std::make_unique<ScriptInterface_impl>(nativeScopeName, context, nullptr)) | ||||
{ | { | ||||
// Profiler stats table isn't thread-safe, so only enable this on the main thread | // Profiler stats table isn't thread-safe, so only enable this on the main thread | ||||
if (Threading::IsMainThread()) | if (Threading::IsMainThread()) | ||||
{ | { | ||||
if (g_ScriptStatsTable) | if (g_ScriptStatsTable) | ||||
g_ScriptStatsTable->Add(this, debugName); | g_ScriptStatsTable->Add(this, debugName); | ||||
} | } | ||||
ScriptRequest rq(this); | ScriptRequest rq(this); | ||||
m_CmptPrivate.pScriptInterface = this; | m_CmptPrivate.pScriptInterface = this; | ||||
JS::SetRealmPrivate(JS::GetObjectRealmOrNull(rq.glob), (void*)&m_CmptPrivate); | JS::SetRealmPrivate(JS::GetObjectRealmOrNull(rq.glob), (void*)&m_CmptPrivate); | ||||
} | } | ||||
ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugName, const ScriptInterface& neighbor) | |||||
{ | |||||
ScriptRequest nrq(neighbor); | |||||
JS::Compartment* comp = JS::GetCompartmentForRealm(JS::GetCurrentRealmOrNull(nrq.cx)); | |||||
m = std::make_unique<ScriptInterface_impl>(nativeScopeName, neighbor.GetContext(), comp); | |||||
// Profiler stats table isn't thread-safe, so only enable this on the main thread | |||||
if (Threading::IsMainThread()) | |||||
{ | |||||
if (g_ScriptStatsTable) | |||||
g_ScriptStatsTable->Add(this, debugName); | |||||
} | |||||
ScriptRequest rq(this); | |||||
m_CmptPrivate.pScriptInterface = this; | |||||
JS::SetRealmPrivate(JS::GetObjectRealmOrNull(rq.glob), (void*)&m_CmptPrivate); | |||||
} | |||||
ScriptInterface::~ScriptInterface() | ScriptInterface::~ScriptInterface() | ||||
{ | { | ||||
if (Threading::IsMainThread()) | if (Threading::IsMainThread()) | ||||
{ | { | ||||
if (g_ScriptStatsTable) | if (g_ScriptStatsTable) | ||||
g_ScriptStatsTable->Remove(this); | g_ScriptStatsTable->Remove(this); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 342 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator