File 0003-Adapt-JS-API-to-ESR-140.patch of Package 0ad

From 2635c9064f5127b52147a302fad0946be5ff9ec1 Mon Sep 17 00:00:00 2001
From: Itms <itms@wildfiregames.com>
Date: Wed, 3 Sep 2025 12:03:53 +0200
Subject: [PATCH] Adapt JS API to ESR 140

---
 source/gui/ObjectBases/IGUIObject.cpp          | 3 ++-
 source/scriptinterface/Promises.cpp            | 5 +++--
 source/scriptinterface/Promises.h              | 2 +-
 source/scriptinterface/ScriptContext.cpp       | 2 +-
 source/scriptinterface/ScriptEngine.h          | 2 +-
 source/scriptinterface/ScriptInterface.cpp     | 3 ++-
 source/simulation2/scripting/ScriptComponent.h | 4 ++--
 source/simulation2/system/InterfaceScripted.h  | 6 +++---
 8 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/source/gui/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp
index ea20d2262d..aaa6b5995f 100644
--- a/source/gui/ObjectBases/IGUIObject.cpp
+++ b/source/gui/ObjectBases/IGUIObject.cpp
@@ -39,6 +39,7 @@
 #include <js/ComparisonOperators.h>
 #include <js/CompilationAndEvaluation.h>
 #include <js/CompileOptions.h>
+#include <js/EnvironmentChain.h>
 #include <js/GCAPI.h>
 #include <js/GCVector.h>
 #include <js/SourceText.h>
@@ -339,7 +340,7 @@ void IGUIObject::RegisterScriptHandler(const CStr& eventName, const CStr& Code,
 
 	JS::SourceText<mozilla::Utf8Unit> src;
 	ENSURE(src.init(rq.cx, Code.c_str(), Code.length(), JS::SourceOwnership::Borrowed));
-	JS::RootedObjectVector emptyScopeChain(rq.cx);
+	JS::EnvironmentChain emptyScopeChain{ rq.cx, JS::SupportUnscopables::No };
 	JS::RootedFunction func(rq.cx, JS::CompileFunction(rq.cx, emptyScopeChain, options, buf, paramCount, paramNames, src));
 	if (func == nullptr)
 	{
diff --git a/source/scriptinterface/Promises.cpp b/source/scriptinterface/Promises.cpp
index 303ab2678d..30777260dc 100644
--- a/source/scriptinterface/Promises.cpp
+++ b/source/scriptinterface/Promises.cpp
@@ -69,9 +69,10 @@ void JobQueue::runJobs(JSContext*)
 	}
 }
 
-JSObject* JobQueue::getIncumbentGlobal(JSContext* cx)
+bool JobQueue::getHostDefinedData(JSContext* cx, JS::MutableHandle<JSObject*> data) const
 {
-	return JS::CurrentGlobalOrNull(cx);
+	data.set(JS::CurrentGlobalOrNull(cx));
+	return true;
 }
 
 bool JobQueue::enqueuePromiseJob(JSContext* cx, JS::HandleObject, JS::HandleObject job, JS::HandleObject,
diff --git a/source/scriptinterface/Promises.h b/source/scriptinterface/Promises.h
index 4403e09797..b2e3cbe86a 100644
--- a/source/scriptinterface/Promises.h
+++ b/source/scriptinterface/Promises.h
@@ -40,7 +40,7 @@ public:
 	void runJobs(JSContext*) final;
 
 private:
-	JSObject* getIncumbentGlobal(JSContext* cx) final;
+	bool getHostDefinedData(JSContext* cx, JS::MutableHandle<JSObject*> data) const final;
 
 	bool enqueuePromiseJob(JSContext* cx, JS::HandleObject, JS::HandleObject job, JS::HandleObject,
 		JS::HandleObject) final;
diff --git a/source/scriptinterface/ScriptContext.cpp b/source/scriptinterface/ScriptContext.cpp
index 19cce2733d..77e264a24f 100644
--- a/source/scriptinterface/ScriptContext.cpp
+++ b/source/scriptinterface/ScriptContext.cpp
@@ -259,7 +259,7 @@ void ScriptContext::MaybeIncrementalGC()
 #endif
 
 		// There is a tradeoff between this time and the number of frames we must run GCs on, but overall we should prioritize smooth framerates.
-		const js::SliceBudget GCSliceTimeBudget = js::SliceBudget(js::TimeBudget(6)); // Milliseconds an incremental slice is allowed to run. SM respects this fairly well.
+		const JS::SliceBudget GCSliceTimeBudget = JS::SliceBudget(JS::TimeBudget(6)); // Milliseconds an incremental slice is allowed to run. SM respects this fairly well.
 
 		PrepareZonesForIncrementalGC();
 		if (!JS::IsIncrementalGCInProgress(m_cx))
diff --git a/source/scriptinterface/ScriptEngine.h b/source/scriptinterface/ScriptEngine.h
index 5a8f65f5ca..d954942883 100644
--- a/source/scriptinterface/ScriptEngine.h
+++ b/source/scriptinterface/ScriptEngine.h
@@ -24,7 +24,7 @@
 #include <js/Initialization.h>
 #include <list>
 
-#if MOZJS_MAJOR_VERSION != 128
+#if MOZJS_MAJOR_VERSION != 140
 #error Your compiler is trying to use an incorrect major version of the \
 SpiderMonkey library. The SpiderMonkey API is subject to changes, and the \
 game will not build with the selected version of the library. Make sure \
diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp
index 0ca2a0e2ab..869a4cfb06 100644
--- a/source/scriptinterface/ScriptInterface.cpp
+++ b/source/scriptinterface/ScriptInterface.cpp
@@ -43,6 +43,7 @@
 #include <js/ComparisonOperators.h>
 #include <js/CompilationAndEvaluation.h>
 #include <js/CompileOptions.h>
+#include <js/EnvironmentChain.h>
 #include <js/GCVector.h>
 #include <js/GlobalObject.h>
 #include <js/PropertyAndElement.h>
@@ -666,7 +667,7 @@ bool ScriptInterface::LoadScript(const VfsPath& filename, const std::string& cod
 
 	JS::SourceText<mozilla::Utf8Unit> src;
 	ENSURE(src.init(rq.cx, code.c_str(), code.length(), JS::SourceOwnership::Borrowed));
-	JS::RootedObjectVector emptyScopeChain(rq.cx);
+	JS::EnvironmentChain emptyScopeChain{ rq.cx, JS::SupportUnscopables::No };
 	JS::RootedFunction func(rq.cx, JS::CompileFunction(rq.cx, emptyScopeChain, options, NULL, 0, NULL, src));
 	if (func == nullptr)
 	{
diff --git a/source/simulation2/scripting/ScriptComponent.h b/source/simulation2/scripting/ScriptComponent.h
index e05f574f87..001d84ad98 100644
--- a/source/simulation2/scripting/ScriptComponent.h
+++ b/source/simulation2/scripting/ScriptComponent.h
@@ -37,8 +37,8 @@ class CComponentTypeScript
 public:
 	CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance);
 
-	JS::HandleValue GetInstance() const { return JS::HandleValue::fromMarkedLocation(m_Instance.address()); }
-	JS::MutableHandleValue GetMutInstance() { return JS::MutableHandleValue::fromMarkedLocation(const_cast<JS::Value*>(m_Instance.address())); }
+	JS::HandleValue GetInstance() const { return JS::HandleValue::fromMarkedLocation(m_Instance.unsafeAddress()); }
+	JS::MutableHandleValue GetMutInstance() { return JS::MutableHandleValue::fromMarkedLocation(const_cast<JS::Value*>(m_Instance.unsafeAddress())); }
 	static void Trace(JSTracer* trc, void* data);
 
 	void Init(CComponentManager& cmpMgr, const CParamNode& paramNode, entity_id_t ent);
diff --git a/source/simulation2/system/InterfaceScripted.h b/source/simulation2/system/InterfaceScripted.h
index 8068384b70..f7a6dc10c2 100644
--- a/source/simulation2/system/InterfaceScripted.h
+++ b/source/simulation2/system/InterfaceScripted.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2024 Wildfire Games.
+/* Copyright (C) 2025 Wildfire Games.
  * This file is part of 0 A.D.
  *
  * 0 A.D. is free software: you can redistribute it and/or modify
@@ -46,7 +46,7 @@
 	JS::HandleValue ICmp##iname::GetJSInstance() const \
 	{ \
 		if (m_CachedInstance) \
-			return JS::HandleValue::fromMarkedLocation(m_CachedInstance.address()); \
+			return JS::HandleValue::fromMarkedLocation(m_CachedInstance.unsafeAddress()); \
 		\
 		const ScriptInterface& si = GetSimContext().GetScriptInterface(); \
 		ScriptRequest rq(si); \
@@ -55,7 +55,7 @@
 		m_CachedInstance.setObject(*obj); \
 		\
 		GetSimContext().GetComponentManager().RegisterTrace(GetEntityId(), m_CachedInstance); \
-		return JS::HandleValue::fromMarkedLocation(m_CachedInstance.address()); \
+		return JS::HandleValue::fromMarkedLocation(m_CachedInstance.unsafeAddress()); \
 	} \
 	void RegisterComponentInterface_##iname(ScriptInterface& scriptInterface) { \
 		ICmp##iname::InterfaceInit(scriptInterface); \
openSUSE Build Service is sponsored by