Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptInterface.cpp
Show All 15 Lines | |||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "FunctionWrapper.h" | #include "FunctionWrapper.h" | ||||
#include "ScriptContext.h" | #include "ScriptContext.h" | ||||
#include "ScriptExtraHeaders.h" | #include "ScriptExtraHeaders.h" | ||||
#include "ScriptInterface.h" | #include "ScriptInterface.h" | ||||
#include "ScriptModuleLoader.h" | |||||
#include "ScriptStats.h" | #include "ScriptStats.h" | ||||
#include "StructuredClone.h" | #include "StructuredClone.h" | ||||
#include "lib/debug.h" | #include "lib/debug.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
Show All 29 Lines | struct ScriptInterface_impl | ||||
friend ScriptRequest; | friend ScriptRequest; | ||||
private: | private: | ||||
JSContext* m_cx; | JSContext* m_cx; | ||||
JS::PersistentRootedObject m_glob; // global scope object | JS::PersistentRootedObject m_glob; // global scope object | ||||
public: | public: | ||||
boost::rand48* m_rng; | boost::rand48* m_rng; | ||||
JS::PersistentRootedObject m_nativeScope; // native function scope object | JS::PersistentRootedObject m_nativeScope; // native function scope object | ||||
std::unique_ptr<ScriptModuleLoader> m_moduleLoader; | |||||
vladislavbelov: Must be `std::unique_ptr`. And wrong CC btw. | |||||
}; | }; | ||||
/** | /** | ||||
* Constructor for ScriptRequest - here because it needs access into ScriptInterface_impl. | * Constructor for ScriptRequest - here because it needs access into ScriptInterface_impl. | ||||
*/ | */ | ||||
ScriptRequest::ScriptRequest(const ScriptInterface& scriptInterface) : | ScriptRequest::ScriptRequest(const ScriptInterface& scriptInterface) : | ||||
cx(scriptInterface.m->m_cx), | cx(scriptInterface.m->m_cx), | ||||
glob(scriptInterface.m->m_glob), | glob(scriptInterface.m->m_glob), | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | bool ScriptInterface::Math_random(JSContext* cx, uint argc, JS::Value* vp) | ||||
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, JS::Compartment* compartment) : | 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()), m_moduleLoader(std::make_unique<ScriptModuleLoader>()) | ||||
{ | { | ||||
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) | if (compartment) | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | JSContext* ScriptInterface::GetGeneralJSContext() const | ||||
return m->m_context->GetGeneralJSContext(); | return m->m_context->GetGeneralJSContext(); | ||||
} | } | ||||
std::shared_ptr<ScriptContext> ScriptInterface::GetContext() const | std::shared_ptr<ScriptContext> ScriptInterface::GetContext() const | ||||
{ | { | ||||
return m->m_context; | return m->m_context; | ||||
} | } | ||||
const ScriptModuleLoader& ScriptInterface::GetScriptModuleLoader() const | |||||
{ | |||||
return *(m->m_moduleLoader); | |||||
} | |||||
void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const | void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const | ||||
{ | { | ||||
ScriptRequest rq(this); | ScriptRequest rq(this); | ||||
if (!ctor.isObject()) | if (!ctor.isObject()) | ||||
{ | { | ||||
LOGERROR("CallConstructor: ctor is not an object"); | LOGERROR("CallConstructor: ctor is not an object"); | ||||
out.setNull(); | out.setNull(); | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 205 Lines • ▼ Show 20 Lines | if (ret != PSRETURN_OK) | ||||
return false; | return false; | ||||
} | } | ||||
CStr code = file.DecodeUTF8(); // assume it's UTF-8 | CStr code = file.DecodeUTF8(); // assume it's UTF-8 | ||||
return LoadGlobalScript(path, code); | return LoadGlobalScript(path, code); | ||||
} | } | ||||
bool ScriptInterface::LoadESModule(const VfsPath& path) const | |||||
{ | |||||
ScriptRequest rq(this); | |||||
return m->m_moduleLoader->Load(rq.cx, path); | |||||
} | |||||
bool ScriptInterface::Eval(const char* code) const | bool ScriptInterface::Eval(const char* code) const | ||||
{ | { | ||||
ScriptRequest rq(this); | ScriptRequest rq(this); | ||||
JS::RootedValue rval(rq.cx); | JS::RootedValue rval(rq.cx); | ||||
JS::CompileOptions opts(rq.cx); | JS::CompileOptions opts(rq.cx); | ||||
opts.setFileAndLine("(eval)", 1); | opts.setFileAndLine("(eval)", 1); | ||||
JS::SourceText<mozilla::Utf8Unit> src; | JS::SourceText<mozilla::Utf8Unit> src; | ||||
Show All 22 Lines |
Wildfire Games · Phabricator
Must be std::unique_ptr. And wrong CC btw.