1
0
forked from 0ad/0ad

[SM60 2/2] Update to Spidermonkey 60 APIs

Two noteworthy changes:
- Proxies are update to the SM60 API, having an explicit reserved slot
and a private slot, in which the 'proxy data' and the C++ object are
stored. This fixes a debug assertion failure of SM52 (See bugs 1237504
and 1339411)
- The GC callback behaviour has changed slightly, and we should now only
look for GC_SLICE_BEGIN and GC_SLICE_END calls (Bug 1364547)

Other updates are minor:
- Bug 1339036: JSTYPE_VOID beomes JSTYPE_UNDEFINED
- Bug 1308236 - avoid ambiguous comparison by changing NULL to nullptr
- Bug 1421358, GC::reason::REFRESH_FRAME was removed. API is indicated
in jsapi.h so use that.
- Compartment behaviours update
- ClassOps changes (Bug 1389510 removed the getter/setter - 7c04ea0211 -
and bug 1370608 added one more before that so net minus one)
- Minor tests touchups again.

Tested by: SubitaNeo, Stan
Thanks to bellaz89 for the Shared Array fix

Closes #5859

Differential Revision: https://code.wildfiregames.com/D3116
This was SVN commit r24243.
This commit is contained in:
wraitii 2020-11-24 15:47:03 +00:00
parent f2cf2c97ad
commit d07f271d60
12 changed files with 41 additions and 54 deletions

View File

@ -240,7 +240,7 @@ public:
*/
const CGUIColor& GetPreDefinedColor(const CStr& name) const { return m_PreDefinedColors.at(name); }
const void* GetProxyData(const js::BaseProxyHandler* ptr) const { return m_ProxyData.at(ptr); }
void* GetProxyData(const js::BaseProxyHandler* ptr) { return m_ProxyData.at(ptr); }
shared_ptr<ScriptInterface> GetScriptInterface() { return m_ScriptInterface; };

View File

@ -456,9 +456,11 @@ void IGUIObject::CreateJSObject()
js::ProxyOptions options;
options.setClass(&JSI_GUIProxy<IGUIObject>::ClassDefinition());
JS::RootedValue cppObj(rq.cx);
JS::RootedValue cppObj(rq.cx), data(rq.cx);
cppObj.get().setPrivate(this);
data.get().setPrivate(GetGUI().GetProxyData(&JSI_GUIProxy<IGUIObject>::Singleton()));
m_JSObject.init(rq.cx, js::NewProxyObject(rq.cx, &JSI_GUIProxy<IGUIObject>::Singleton(), cppObj, nullptr, options));
JS_SetPrivate(m_JSObject.get(), this);
js::SetProxyReservedSlot(m_JSObject, 0, data);
}
JSObject* IGUIObject::GetJSObject()

View File

@ -261,9 +261,11 @@ void CText::CreateJSObject()
js::ProxyOptions options;
options.setClass(&JSI_GUIProxy<IGUIObject>::ClassDefinition());
JS::RootedValue cppObj(rq.cx);
JS::RootedValue cppObj(rq.cx), data(rq.cx);
cppObj.get().setPrivate(this);
data.get().setPrivate(GetGUI().GetProxyData(&JSI_GUIProxy<CText>::Singleton()));
m_JSObject.init(rq.cx, js::NewProxyObject(rq.cx, &JSI_GUIProxy<CText>::Singleton(), cppObj, nullptr, options));
JS_SetPrivate(m_JSObject.get(), this);
js::SetProxyReservedSlot(m_JSObject, 0, data);
}
void CText::getTextSize(ScriptInterface& scriptInterface, JS::MutableHandleValue ret)

View File

@ -44,9 +44,9 @@ namespace {
}
template <>
bool JSI_GUIProxy<CText>::funcGetter(CText* elem, const std::string& propName, JS::MutableHandleValue vp) const
bool JSI_GUIProxy<CText>::funcGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const
{
const SData& data = *static_cast<const SData*>(elem->GetGUI().GetProxyData(this));
const SData& data = *static_cast<const SData*>(js::GetProxyReservedSlot(proxy, 0).toPrivate());
if (propName == "toString")
return vp.setObjectOrNull(data.m_ToString), true;
if (propName == "focus")

View File

@ -64,7 +64,7 @@ protected:
// This handles returning function properties.
// Specialize this.
bool funcGetter(GUIObjectType* elem, const std::string& propName, JS::MutableHandleValue vp) const;
bool funcGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const;
protected:
// BaseProxyHandler interface below
@ -97,9 +97,9 @@ protected:
return true;
}
// Return nothing.
virtual bool enumerate(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::MutableHandleObject UNUSED(objp)) const override
virtual JSObject* enumerate(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy)) const override
{
return true;
return nullptr;
}
// Throw an exception is JS attempts to query the prototype.
virtual bool getPrototypeIfOrdinary(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), bool* UNUSED(isOrdinary), JS::MutableHandleObject UNUSED(protop)) const override

View File

@ -20,7 +20,7 @@
template <typename T>
js::Class& JSI_GUIProxy<T>::ClassDefinition()
{
static js::Class c = PROXY_CLASS_DEF("GUIObjectProxy", JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy));
static js::Class c = PROXY_CLASS_DEF("GUIObjectProxy", JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy) | JSCLASS_HAS_RESERVED_SLOTS(1));
return c;
}
@ -37,7 +37,7 @@ template<class OG, class R, void (R::*funcptr)(ScriptInterface&, JS::MutableHand
inline bool apply_to(JSContext* cx, uint argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
OG* e = static_cast<OG*>(JS_GetPrivate(args.thisv().toObjectOrNull()));
OG* e = static_cast<OG*>(js::GetProxyPrivate(args.thisv().toObjectOrNull()).toPrivate());
if (!e)
return false;
@ -53,7 +53,7 @@ bool JSI_GUIProxy<T>::get(JSContext* cx, JS::HandleObject proxy, JS::HandleValue
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
ScriptRequest rq(*pScriptInterface);
T* e = static_cast<T*>(JS_GetPrivate(proxy.get()));
T* e = static_cast<T*>(js::GetProxyPrivate(proxy.get()).toPrivate());
if (!e)
return false;
@ -66,7 +66,7 @@ bool JSI_GUIProxy<T>::get(JSContext* cx, JS::HandleObject proxy, JS::HandleValue
return false;
// Return function properties. Specializable.
if (funcGetter(e, propName, vp))
if (funcGetter(proxy, propName, vp))
return true;
// Use onWhatever to access event handlers
@ -121,7 +121,7 @@ template <typename T>
bool JSI_GUIProxy<T>::set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::HandleValue vp,
JS::HandleValue UNUSED(receiver), JS::ObjectOpResult& result) const
{
T* e = static_cast<T*>(JS_GetPrivate(proxy.get()));
T* e = static_cast<T*>(js::GetProxyPrivate(proxy.get()).toPrivate());
if (!e)
return result.fail(JSMSG_NOT_NONNULL_OBJECT);
@ -173,7 +173,7 @@ bool JSI_GUIProxy<T>::set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id
template<typename T>
bool JSI_GUIProxy<T>::delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::ObjectOpResult& result) const
{
T* e = static_cast<T*>(JS_GetPrivate(proxy.get()));
T* e = static_cast<T*>(js::GetProxyPrivate(proxy.get()).toPrivate());
if (!e)
return result.fail(JSMSG_NOT_NONNULL_OBJECT);

View File

@ -30,7 +30,7 @@ JSClassOps JSI_GUISize::JSI_classops = {
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, JSI_GUISize::construct, nullptr
nullptr, JSI_GUISize::construct, nullptr
};
JSFunctionSpec JSI_GUISize::JSI_methods[] =

View File

@ -43,9 +43,9 @@ namespace {
}
template <>
bool JSI_GUIProxy<IGUIObject>::funcGetter(IGUIObject* elem, const std::string& propName, JS::MutableHandleValue vp) const
bool JSI_GUIProxy<IGUIObject>::funcGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const
{
const SData& data = *static_cast<const SData*>(elem->GetGUI().GetProxyData(this));
const SData& data = *static_cast<const SData*>(js::GetProxyReservedSlot(proxy, 0).toPrivate());
if (propName == "toString")
return vp.setObjectOrNull(data.m_ToString), true;
if (propName == "focus")

View File

@ -27,15 +27,13 @@
void GCSliceCallbackHook(JSContext* UNUSED(cx), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc))
{
/*
* During non-incremental GC, the GC is bracketed by JSGC_CYCLE_BEGIN/END
* callbacks. During an incremental GC, the sequence of callbacks is as
* follows:
* JSGC_CYCLE_BEGIN, JSGC_SLICE_END (first slice)
* JSGC_SLICE_BEGIN, JSGC_SLICE_END (second slice)
* ...
* JSGC_SLICE_BEGIN, JSGC_CYCLE_END (last slice)
*/
/**
* From the GCAPI.h file:
* > During GC, the GC is bracketed by GC_CYCLE_BEGIN/END callbacks. Each
* > slice between those (whether an incremental or the sole non-incremental
* > slice) is bracketed by GC_SLICE_BEGIN/GC_SLICE_END.
* Thus, to safely monitor GCs, we need to profile SLICE_X calls.
*/
if (progress == JS::GC_SLICE_BEGIN)
@ -48,19 +46,7 @@ void GCSliceCallbackHook(JSContext* UNUSED(cx), JS::GCProgress progress, const J
{
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
g_Profiler.Stop();
g_Profiler2.RecordRegionLeave();
}
else if (progress == JS::GC_CYCLE_BEGIN)
{
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
g_Profiler.Start("GCSlice");
g_Profiler2.RecordRegionEnter("GCSlice");
}
else if (progress == JS::GC_CYCLE_END)
{
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
g_Profiler.Stop();
g_Profiler2.RecordRegionLeave();
g_Profiler2.RecordRegionLeave();
}
// The following code can be used to print some information aobut garbage collection
@ -219,7 +205,7 @@ void ScriptContext::MaybeIncrementalGC(double delay)
printf("Finishing incremental GC because gcBytes > m_ContextSize / 2. \n");
#endif
PrepareCompartmentsForIncrementalGC();
JS::FinishIncrementalGC(m_cx, JS::gcreason::REFRESH_FRAME);
JS::FinishIncrementalGC(m_cx, JS::gcreason::API);
}
else
{
@ -249,9 +235,9 @@ void ScriptContext::MaybeIncrementalGC(double delay)
#endif
PrepareCompartmentsForIncrementalGC();
if (!JS::IsIncrementalGCInProgress(m_cx))
JS::StartIncrementalGC(m_cx, GC_NORMAL, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget);
JS::StartIncrementalGC(m_cx, GC_NORMAL, JS::gcreason::API, GCSliceTimeBudget);
else
JS::IncrementalGCSlice(m_cx, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget);
JS::IncrementalGCSlice(m_cx, JS::gcreason::API, GCSliceTimeBudget);
}
m_LastGCBytes = gcBytes;
}
@ -262,7 +248,7 @@ void ScriptContext::ShrinkingGC()
{
JS_SetGCParameter(m_cx, JSGC_MODE, JSGC_MODE_ZONE);
JS::PrepareForFullGC(m_cx);
JS::GCForReason(m_cx, GC_SHRINK, JS::gcreason::REFRESH_FRAME);
JS::GCForReason(m_cx, GC_SHRINK, JS::gcreason::API);
JS_SetGCParameter(m_cx, JSGC_MODE, JSGC_MODE_INCREMENTAL);
}

View File

@ -98,7 +98,7 @@ JSClassOps global_classops = {
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr,
JS_GlobalObjectTraceHook
};
@ -327,10 +327,7 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const sh
JS::CompartmentCreationOptions creationOpt;
// Keep JIT code during non-shrinking GCs. This brings a quite big performance improvement.
creationOpt.setPreserveJitCode(true);
JS::CompartmentBehaviors behaviors;
behaviors.setVersion(JSVERSION_LATEST);
JS::CompartmentOptions opt(creationOpt, behaviors);
JS::CompartmentOptions opt(creationOpt, JS::CompartmentBehaviors{});
JSAutoRequest rq(m_cx);
m_glob = JS_NewGlobalObject(m_cx, &global_class, nullptr, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt);
@ -494,7 +491,7 @@ void ScriptInterface::DefineCustomObjectType(JSClass *clasp, JSNative constructo
ps, fs, // Properties, methods
static_ps, static_fs)); // Constructor properties, methods
if (obj == NULL)
if (obj == nullptr)
{
ScriptException::CatchPending(rq);
throw PSERROR_Scripting_DefineType_CreationFailed();

View File

@ -40,7 +40,7 @@ public:
ScriptInterface script("Test", "Test", g_ScriptContext);
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", "1+"));
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 1\nexpected expression, got end of script");
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 3\nexpected expression, got \'}\'");
}
void test_loadscript_strict_warning()
@ -57,7 +57,7 @@ public:
ScriptInterface script("Test", "Test", g_ScriptContext);
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", "with(1){}"));
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 1\nstrict mode code may not contain \'with\' statements");
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 2\nstrict mode code may not contain \'with\' statements");
}
void test_clone_basic()

View File

@ -68,7 +68,7 @@ void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val)
switch (JS_TypeOfValue(rq.cx, val))
{
case JSTYPE_VOID:
case JSTYPE_UNDEFINED:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID);
break;