Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/ScriptInterface.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | JSClass global_class = { | ||||
nullptr, nullptr, | nullptr, nullptr, | ||||
nullptr, nullptr, nullptr, | nullptr, nullptr, nullptr, | ||||
nullptr, nullptr, nullptr, nullptr, | nullptr, nullptr, nullptr, nullptr, | ||||
JS_GlobalObjectTraceHook | JS_GlobalObjectTraceHook | ||||
}; | }; | ||||
void ErrorReporter(JSContext* cx, const char* message, JSErrorReport* report) | void ErrorReporter(JSContext* cx, const char* message, JSErrorReport* report) | ||||
{ | { | ||||
std::stringstream msg; | std::stringstream msg; | ||||
bool isWarning = JSREPORT_IS_WARNING(report->flags); | bool isWarning = JSREPORT_IS_WARNING(report->flags); | ||||
msg << (isWarning ? "JavaScript warning: " : "JavaScript error: "); | msg << (isWarning ? "JavaScript warning: " : "JavaScript error: "); | ||||
if (report->filename) | if (report->filename) | ||||
{ | { | ||||
msg << report->filename; | msg << report->filename; | ||||
msg << " line " << report->lineno << "\n"; | msg << " line " << report->lineno << "\n"; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 261 Lines • ▼ Show 20 Lines | ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const shared_ptr<ScriptRuntime>& runtime) : | ||||
JS_SetContextPrivate(m_cx, NULL); | JS_SetContextPrivate(m_cx, NULL); | ||||
JS_SetErrorReporter(m_runtime->m_rt, ErrorReporter); | JS_SetErrorReporter(m_runtime->m_rt, ErrorReporter); | ||||
JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_ION_ENABLE, 1); | JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_ION_ENABLE, 1); | ||||
JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1); | JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1); | ||||
JS::RuntimeOptionsRef(m_cx).setExtraWarnings(1) | JS::RuntimeOptionsRef(m_cx) | ||||
.setWerror(0) | .setExtraWarnings(true) | ||||
.setVarObjFix(1) | .setWerror(false) | ||||
.setStrictMode(1); | .setVarObjFix(true) | ||||
.setStrictMode(true); | |||||
JS::CompartmentOptions opt; | JS::CompartmentOptions opt; | ||||
opt.setVersion(JSVERSION_LATEST); | opt.setVersion(JSVERSION_LATEST); | ||||
// 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. | ||||
opt.setPreserveJitCode(true); | opt.setPreserveJitCode(true); | ||||
JSAutoRequest rq(m_cx); | JSAutoRequest rq(m_cx); | ||||
JS::RootedObject globalRootedVal(m_cx, JS_NewGlobalObject(m_cx, &global_class, NULL, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt)); | JS::RootedObject globalRootedVal(m_cx, JS_NewGlobalObject(m_cx, &global_class, NULL, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt)); | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
JS::Value ScriptInterface::GetGlobalObject() const | JS::Value ScriptInterface::GetGlobalObject() const | ||||
{ | { | ||||
JSAutoRequest rq(m->m_cx); | JSAutoRequest rq(m->m_cx); | ||||
return JS::ObjectValue(*JS::CurrentGlobalOrNull(m->m_cx)); | return JS::ObjectValue(*JS::CurrentGlobalOrNull(m->m_cx)); | ||||
} | } | ||||
bool ScriptInterface::SetGlobal_(const char* name, JS::HandleValue value, bool replace) | bool ScriptInterface::SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate) | ||||
{ | { | ||||
JSAutoRequest rq(m->m_cx); | JSAutoRequest rq(m->m_cx); | ||||
JS::RootedObject global(m->m_cx, m->m_glob); | JS::RootedObject global(m->m_cx, m->m_glob); | ||||
if (!replace) | if (!replace) | ||||
{ | { | ||||
bool found; | bool found; | ||||
if (!JS_HasProperty(m->m_cx, global, name, &found)) | if (!JS_HasProperty(m->m_cx, global, name, &found)) | ||||
return false; | return false; | ||||
if (found) | if (found) | ||||
{ | { | ||||
JS_ReportError(m->m_cx, "SetGlobal \"%s\" called multiple times", name); | JS_ReportError(m->m_cx, "SetGlobal \"%s\" called multiple times", name); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
bool ok = JS_DefineProperty(m->m_cx, global, name, value, JSPROP_ENUMERATE | JSPROP_READONLY | uint attrs = 0; | ||||
| JSPROP_PERMANENT); | if (constant) | ||||
return ok; | attrs |= JSPROP_READONLY | JSPROP_PERMANENT; | ||||
if (enumerate) | |||||
attrs |= JSPROP_ENUMERATE; | |||||
return JS_DefineProperty(m->m_cx, global, name, value, attrs); | |||||
} | } | ||||
bool ScriptInterface::SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool constant, bool enumerate) const | bool ScriptInterface::SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool constant, bool enumerate) const | ||||
{ | { | ||||
JSAutoRequest rq(m->m_cx); | JSAutoRequest rq(m->m_cx); | ||||
uint attrs = 0; | uint attrs = 0; | ||||
if (constant) | if (constant) | ||||
attrs |= JSPROP_READONLY | JSPROP_PERMANENT; | attrs |= JSPROP_READONLY | JSPROP_PERMANENT; | ||||
▲ Show 20 Lines • Show All 517 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator