SpiderMonkey-related changes in preparation for the upgrade to SpiderMonkey 45, refs #4893.

- Remove JSVAL_ZERO and JSVAL_NULL
https://bugzilla.mozilla.org/show_bug.cgi?id=1177825
- Remove *_TO_JSVAL https://bugzilla.mozilla.org/show_bug.cgi?id=1177892
- Drop support for parent object in the deserializer
https://bugzilla.mozilla.org/show_bug.cgi?id=1136345,
https://bugzilla.mozilla.org/show_bug.cgi?id=805052
- Correctly use boolean values in JS::RuntimeOptionsRef
- Use JS_FN instead of JS_FS: in future versions, JS_FS is not public
because it isn't supposed to be used in JSAPI code
- Allow to select flags for global objects, and correctly mark progress
bars in the loading screen as not readonly+permanent
- Remove empty JSI_props in IGUIObject

Reviewed By: wraitii, elexis
Differential Revision: https://code.wildfiregames.com/D1716
This was SVN commit r22052.
This commit is contained in:
Nicolas Auvray 2019-01-13 16:37:41 +00:00
parent 651cf8b364
commit db5d4bb5f1
10 changed files with 53 additions and 60 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -33,7 +33,7 @@ JSClass JSI_GUISize::JSI_class = {
JSFunctionSpec JSI_GUISize::JSI_methods[] =
{
JS_FS("toString", JSI_GUISize::toString, 0, 0),
JS_FN("toString", JSI_GUISize::toString, 0, 0),
JS_FS_END
};
@ -57,7 +57,7 @@ bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp)
}
else if (args.length() == 4)
{
JS::RootedValue zero(cx, JSVAL_ZERO);
JS::RootedValue zero(cx, JS::NumberValue(0));
JS_SetProperty(cx, obj, "left", args[0]);
JS_SetProperty(cx, obj, "top", args[1]);
JS_SetProperty(cx, obj, "right", args[2]);
@ -69,7 +69,7 @@ bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp)
}
else
{
JS::RootedValue zero(cx, JSVAL_ZERO);
JS::RootedValue zero(cx, JS::NumberValue(0));
JS_SetProperty(cx, obj, "left", zero);
JS_SetProperty(cx, obj, "top", zero);
JS_SetProperty(cx, obj, "right", zero);
@ -141,7 +141,7 @@ JSClass JSI_GUIColor::JSI_class = {
JSFunctionSpec JSI_GUIColor::JSI_methods[] =
{
JS_FS("toString", JSI_GUIColor::toString, 0, 0),
JS_FN("toString", JSI_GUIColor::toString, 0, 0),
JS_FS_END
};
@ -210,7 +210,7 @@ JSClass JSI_GUIMouse::JSI_class = {
JSFunctionSpec JSI_GUIMouse::JSI_methods[] =
{
JS_FS("toString", JSI_GUIMouse::toString, 0, 0),
JS_FN("toString", JSI_GUIMouse::toString, 0, 0),
JS_FS_END
};

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -39,17 +39,12 @@ JSClass JSI_IGUIObject::JSI_class = {
nullptr, nullptr, JSI_IGUIObject::construct, nullptr
};
JSPropertySpec JSI_IGUIObject::JSI_props[] =
{
{ 0 }
};
JSFunctionSpec JSI_IGUIObject::JSI_methods[] =
{
JS_FS("toString", JSI_IGUIObject::toString, 0, 0),
JS_FS("focus", JSI_IGUIObject::focus, 0, 0),
JS_FS("blur", JSI_IGUIObject::blur, 0, 0),
JS_FS("getComputedSize", JSI_IGUIObject::getComputedSize, 0, 0),
JS_FN("toString", JSI_IGUIObject::toString, 0, 0),
JS_FN("focus", JSI_IGUIObject::focus, 0, 0),
JS_FN("blur", JSI_IGUIObject::blur, 0, 0),
JS_FN("getComputedSize", JSI_IGUIObject::getComputedSize, 0, 0),
JS_FS_END
};
@ -635,7 +630,7 @@ bool JSI_IGUIObject::construct(JSContext* cx, uint argc, JS::Value* vp)
void JSI_IGUIObject::init(ScriptInterface& scriptInterface)
{
scriptInterface.DefineCustomObjectType(&JSI_class, construct, 1, JSI_props, JSI_methods, NULL, NULL);
scriptInterface.DefineCustomObjectType(&JSI_class, construct, 1, nullptr, JSI_methods, nullptr, nullptr);
}
bool JSI_IGUIObject::toString(JSContext* cx, uint UNUSED(argc), JS::Value* vp)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -23,7 +23,6 @@
namespace JSI_IGUIObject
{
extern JSClass JSI_class;
extern JSPropertySpec JSI_props[];
extern JSFunctionSpec JSI_methods[];
bool getProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp);
bool setProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool UNUSED(strict), JS::MutableHandleValue vp);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -183,8 +183,8 @@ retry:
// display progress / description in loading screen
void GUI_DisplayLoadProgress(int percent, const wchar_t* pending_task)
{
g_GUI->GetActiveGUI()->GetScriptInterface()->SetGlobal("g_Progress", percent, true);
g_GUI->GetActiveGUI()->GetScriptInterface()->SetGlobal("g_LoadDescription", pending_task, true);
g_GUI->GetActiveGUI()->GetScriptInterface()->SetGlobal("g_Progress", percent, true, false, true);
g_GUI->GetActiveGUI()->GetScriptInterface()->SetGlobal("g_LoadDescription", pending_task, true, false, true);
g_GUI->GetActiveGUI()->SendEventToAll("progress");
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -328,7 +328,7 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
const OsPath replayFile = GetDirectoryName() / directory / L"commands.txt";
if (!FileExists(replayFile))
return JSVAL_NULL;
return JS::NullValue();
// Get file size and modification date
CFileInfo fileInfo;
@ -336,7 +336,7 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
const off_t fileSize = fileInfo.Size();
if (fileSize == 0)
return JSVAL_NULL;
return JS::NullValue();
std::ifstream* replayStream = new std::ifstream(OsString(replayFile).c_str());
@ -345,14 +345,14 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
{
LOGERROR("Couldn't open %s.", replayFile.string8().c_str());
SAFE_DELETE(replayStream);
return JSVAL_NULL;
return JS::NullValue();
}
if (type != "start")
{
LOGWARNING("The replay %s doesn't begin with 'start'!", replayFile.string8().c_str());
SAFE_DELETE(replayStream);
return JSVAL_NULL;
return JS::NullValue();
}
// Parse header / first line
@ -365,14 +365,14 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
{
LOGERROR("Couldn't parse replay header of %s", replayFile.string8().c_str());
SAFE_DELETE(replayStream);
return JSVAL_NULL;
return JS::NullValue();
}
// Ensure "turn" after header
if (!(*replayStream >> type).good() || type != "turn")
{
SAFE_DELETE(replayStream);
return JSVAL_NULL; // there are no turns at all
return JS::NullValue(); // there are no turns at all
}
// Don't process files of rejoined clients
@ -381,7 +381,7 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
if (turn != 0)
{
SAFE_DELETE(replayStream);
return JSVAL_NULL;
return JS::NullValue();
}
int duration = getReplayDuration(replayStream, replayFile, fileSize);
@ -390,7 +390,7 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
// Ensure minimum duration
if (duration < minimumReplayDuration)
return JSVAL_NULL;
return JS::NullValue();
// Return the actual data
JS::RootedValue replayData(cx);
@ -497,7 +497,7 @@ bool VisualReplay::HasReplayMetadata(const OsPath& directoryName)
JS::Value VisualReplay::GetReplayMetadata(ScriptInterface::CxPrivate* pCxPrivate, const OsPath& directoryName)
{
if (!HasReplayMetadata(directoryName))
return JSVAL_NULL;
return JS::NullValue();
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -99,7 +99,7 @@ JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const
BuildDirEntListState state(cx);
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags);
return OBJECT_TO_JSVAL(state.filename_array);
return JS::ObjectValue(*state.filename_array);
}
// Return true iff the file exits
@ -157,7 +157,7 @@ JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const s
CVFSFile file;
if (file.Load(g_VFS, filename) != PSRETURN_OK)
return JSVAL_NULL;
return JS::NullValue();
CStr contents = file.DecodeUTF8(); // assume it's UTF-8

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -86,7 +86,6 @@ JSClass global_class = {
void ErrorReporter(JSContext* cx, const char* message, JSErrorReport* report)
{
std::stringstream msg;
bool isWarning = JSREPORT_IS_WARNING(report->flags);
msg << (isWarning ? "JavaScript warning: " : "JavaScript error: ");
@ -364,10 +363,11 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const sh
JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_ION_ENABLE, 1);
JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1);
JS::RuntimeOptionsRef(m_cx).setExtraWarnings(1)
.setWerror(0)
.setVarObjFix(1)
.setStrictMode(1);
JS::RuntimeOptionsRef(m_cx)
.setExtraWarnings(true)
.setWerror(false)
.setVarObjFix(true)
.setStrictMode(true);
JS::CompartmentOptions opt;
opt.setVersion(JSVERSION_LATEST);
@ -602,7 +602,7 @@ JS::Value ScriptInterface::GetGlobalObject() const
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);
JS::RootedObject global(m->m_cx, m->m_glob);
@ -618,9 +618,13 @@ bool ScriptInterface::SetGlobal_(const char* name, JS::HandleValue value, bool r
}
}
bool ok = JS_DefineProperty(m->m_cx, global, name, value, JSPROP_ENUMERATE | JSPROP_READONLY
| JSPROP_PERMANENT);
return ok;
uint attrs = 0;
if (constant)
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

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -142,7 +142,7 @@ public:
* to set an already-defined value will fail.
*/
template<typename T>
bool SetGlobal(const char* name, const T& value, bool replace = false);
bool SetGlobal(const char* name, const T& value, bool replace = false, bool constant = true, bool enumerate = true);
/**
* Set the named property on the given object.
@ -362,7 +362,7 @@ private:
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const;
bool Eval_(const char* code, JS::MutableHandleValue ret) const;
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const;
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace);
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate);
bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool readonly, bool enumerate) const;
bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool readonly, bool enumerate) const;
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate) const;
@ -488,12 +488,12 @@ inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSCon
}
template<typename T>
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace)
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace, bool constant, bool enumerate)
{
JSAutoRequest rq(GetContext());
JS::RootedValue val(GetContext());
AssignOrToJSVal(GetContext(), &val, value);
return SetGlobal_(name, val, replace);
return SetGlobal_(name, val, replace, constant, enumerate);
}
template<typename T>

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -291,7 +291,7 @@ JS::Value CMessageTerrainChanged::ToJSVal(const ScriptInterface& scriptInterface
SET_MSG_PROPERTY(j0);
SET_MSG_PROPERTY(i1);
SET_MSG_PROPERTY(j1);
return OBJECT_TO_JSVAL(obj);
return JS::ObjectValue(*obj);
}
CMessage* CMessageTerrainChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val)
@ -331,7 +331,7 @@ CMessage* CMessageVisibilityChanged::FromJSVal(const ScriptInterface& scriptInte
JS::Value CMessageWaterChanged::ToJSVal(const ScriptInterface& scriptInterface) const
{
TOJSVAL_SETUP();
return OBJECT_TO_JSVAL(obj);
return JS::ObjectValue(*obj);
}
CMessage* CMessageWaterChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val))

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -167,12 +167,7 @@ JS::Value CStdDeserializer::ReadScriptVal(const char* UNUSED(name), JS::HandleOb
if (!proto)
throw PSERROR_Deserialize_ScriptError("Failed to find serializable prototype for object");
JS::RootedObject parent(cx, JS_GetParent(proto));
if (!proto || !parent)
throw PSERROR_Deserialize_ScriptError();
// TODO: Remove support for parent since this is dropped upstream SpiderMonkey
obj.set(JS_NewObjectWithGivenProto(cx, nullptr, proto, parent));
obj.set(JS_NewObjectWithGivenProto(cx, nullptr, proto));
if (!obj)
throw PSERROR_Deserialize_ScriptError("JS_NewObject failed");