Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-future-proof invocations of Emscripten dynCalls #232

Closed
juj opened this issue Mar 13, 2025 · 2 comments · Fixed by #233
Closed

Fix non-future-proof invocations of Emscripten dynCalls #232

juj opened this issue Mar 13, 2025 · 2 comments · Fixed by #233
Assignees
Labels
enhancement New feature or request Unity SDK

Comments

@juj
Copy link

juj commented Mar 13, 2025

Two problems with .jslib files in the project:

  1. The code will override the Emscripten system Runtime object. (fortunately Unity today does not use any Emscripten constructs that would refer to the Runtime namespace, but that is not guaranteed)
  2. The code invoking dynCalls is not compatible with future Emscripten versions (3.1.38 and newer), because legacy dynCall() functions in new Emscripten versions require declaring a __deps directive (which was not present).

To fix both these issues, the following diff can be applied:

diff --git a/Assets/Thirdweb/Plugins/ThreadingPatcher/Plugins/SystemThreadingTimer.jslib b/Assets/Thirdweb/Plugins/ThreadingPatcher/Plugins/SystemThreadingTimer.jslib
index 5c75db8..af011ec 100644
--- a/Assets/Thirdweb/Plugins/ThreadingPatcher/Plugins/SystemThreadingTimer.jslib
+++ b/Assets/Thirdweb/Plugins/ThreadingPatcher/Plugins/SystemThreadingTimer.jslib
@@ -15,7 +15,7 @@ var SystemThreadingTimerLib = {
 		setTimeout(function()
 		{
 			if (id === vars.currentCallbackId)
-				Runtime.dynCall('v', vars.callback);
+				{{{ makeDynCall("v", "vars.callback") }}}();
 		},
 		interval);
 	}
diff --git a/Assets/Thirdweb/Plugins/WalletConnectUnity/com.walletconnect.core/Runtime/External/NativeWebSocket/WebSocket.jslib b/Assets/Thirdweb/Plugins/WalletConnectUnity/com.walletconnect.core/Runtime/External/NativeWebSocket/WebSocket.jslib
index 052f213..4fbe458 100644
--- a/Assets/Thirdweb/Plugins/WalletConnectUnity/com.walletconnect.core/Runtime/External/NativeWebSocket/WebSocket.jslib
+++ b/Assets/Thirdweb/Plugins/WalletConnectUnity/com.walletconnect.core/Runtime/External/NativeWebSocket/WebSocket.jslib
@@ -150,7 +150,7 @@ var LibraryWebSocket = {
 				console.log("[JSLIB WebSocket] Connected.");
 
 			if (webSocketState.onOpen)
-				Module.dynCall_vi(webSocketState.onOpen, instanceId);
+				{{{ makeDynCall("vi", "webSocketState.onOpen") }}}(instanceId);
 
 		};
 
@@ -170,7 +170,7 @@ var LibraryWebSocket = {
 				HEAPU8.set(dataBuffer, buffer);
 
 				try {
-					Module.dynCall_viii(webSocketState.onMessage, instanceId, buffer, dataBuffer.length);
+					{{{ makeDynCall("viii", "webSocketState.onMessage") }}}(instanceId, buffer, dataBuffer.length);
 				} finally {
 					_free(buffer);
 				}
@@ -182,7 +182,7 @@ var LibraryWebSocket = {
 				HEAPU8.set(dataBuffer, buffer);
 
 				try {
-					Module.dynCall_viii(webSocketState.onMessage, instanceId, buffer, dataBuffer.length);
+					{{{ makeDynCall("viii", "webSocketState.onMessage") }}}(instanceId, buffer, dataBuffer.length);
 				} finally {
 					_free(buffer);
 				}
@@ -204,7 +204,7 @@ var LibraryWebSocket = {
 				stringToUTF8(msg, buffer, length);
 
 				try {
-					Module.dynCall_vii(webSocketState.onError, instanceId, buffer);
+					{{{ makeDynCall("vii", "webSocketState.onError") }}}(instanceId, buffer);
 				} finally {
 					_free(buffer);
 				}
@@ -219,7 +219,7 @@ var LibraryWebSocket = {
 				console.log("[JSLIB WebSocket] Closed.");
 
 			if (webSocketState.onClose)
-				Module.dynCall_vii(webSocketState.onClose, instanceId, ev.code);
+					{{{ makeDynCall("vii", "webSocketState.onClose") }}}(instanceId, ev.code);
 
 			delete instance.ws;
 
diff --git a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/Mobile/WebGLInputMobile.jslib b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/Mobile/WebGLInputMobile.jslib
index c3fc619..8fdfde3 100644
--- a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/Mobile/WebGLInputMobile.jslib
+++ b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/Mobile/WebGLInputMobile.jslib
@@ -6,7 +6,7 @@ var WebGLInputMobile = {
 
         document.body.addEventListener("touchend", function () {
             document.body.removeEventListener("touchend", arguments.callee);
-            Runtime.dynCall("vi", touchend, [id]);
+            {{{ makeDynCall("vi", "touchend") }}}(id);
         });
 
         return id;
@@ -14,7 +14,7 @@ var WebGLInputMobile = {
     WebGLInputMobileOnFocusOut: function (id, focusout) {
         document.body.addEventListener("focusout", function () {
             document.body.removeEventListener("focusout", arguments.callee);
-            Runtime.dynCall("vi", focusout, [id]);
+            {{{ makeDynCall("vi", "focusout") }}}(id);
         });
     },
 }
diff --git a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/WebGLInput.jslib b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/WebGLInput.jslib
index a17092d..62055cb 100644
--- a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/WebGLInput.jslib
+++ b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLInput/WebGLInput.jslib
@@ -1,19 +1,6 @@
 var WebGLInput = {
     $instances: [],
     WebGLInputInit : function() {
-        // use WebAssembly.Table : makeDynCall
-        // when enable. dynCall is undefined
-        if(typeof dynCall === "undefined")
-        {
-            // make Runtime.dynCall to undefined
-            Runtime = { dynCall : undefined }
-        }
-        else
-        {
-            // Remove the `Runtime` object from "v1.37.27: 12/24/2017"
-            // if Runtime not defined. create and add functon!!
-            if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
-        }
     },
     WebGLInputCreate: function (canvasId, x, y, width, height, fontsize, text, placeholder, isMultiLine, isPassword, isHidden, isMobile) {
 
@@ -112,7 +99,7 @@ var WebGLInput = {
                     input.setSelectionRange(start + 1, start + 1);
                     input.oninput();	// call oninput to exe ValueChange function!!
                 } else {
-                    (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]) : {{{ makeDynCall("vii", "cb") }}}(id, e.shiftKey ? -1 : 1);
+                    {{{ makeDynCall("vii", "cb") }}}(id, e.shiftKey ? -1 : 1);
                 }
             }
         });
@@ -124,13 +111,13 @@ var WebGLInput = {
     WebGLInputOnFocus: function (id, cb) {
         var input = instances[id];
         input.onfocus = function () {
-            (!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
+            {{{ makeDynCall("vi", "cb") }}}(id);
         };
     },
     WebGLInputOnBlur: function (id, cb) {
         var input = instances[id];
         input.onblur = function () {
-            (!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
+            {{{ makeDynCall("vi", "cb") }}}(id);
         };
     },
     WebGLInputIsFocus: function (id) {
@@ -143,7 +130,7 @@ var WebGLInput = {
             var bufferSize = lengthBytesUTF8(returnStr) + 1;
             var buffer = _malloc(bufferSize);
             stringToUTF8(returnStr, buffer, bufferSize);
-            (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
+            {{{ makeDynCall("vii", "cb") }}}(id, buffer);
         };
     },
     WebGLInputOnEditEnd:function(id, cb){
@@ -153,7 +140,7 @@ var WebGLInput = {
             var bufferSize = lengthBytesUTF8(returnStr) + 1;
             var buffer = _malloc(bufferSize);
             stringToUTF8(returnStr, buffer, bufferSize);
-            (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
+            {{{ makeDynCall("vii", "cb") }}}(id, buffer);
         };
     },
     WebGLInputOnKeyboardEvent:function(id, cb){
@@ -167,7 +154,7 @@ var WebGLInput = {
                 var shift = e.shiftKey ? 1 : 0;
                 var ctrl = e.ctrlKey ? 1 : 0;
                 var alt = e.altKey ? 1 : 0;
-                (!!Runtime.dynCall) ? Runtime.dynCall("viiiiiii", cb, [id, mode, key, code, shift, ctrl, alt]) : {{{ makeDynCall("viiiiiii", "cb") }}}(id, mode, key, code, shift, ctrl, alt);
+                {{{ makeDynCall("viiiiiii", "cb") }}}(id, mode, key, code, shift, ctrl, alt);
             }
         }
         input.addEventListener('keydown', function(e) { func(1, e); });
diff --git a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLWindow/WebGLWindow.jslib b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLWindow/WebGLWindow.jslib
index 42bec93..a81860f 100644
--- a/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLWindow/WebGLWindow.jslib
+++ b/Assets/Thirdweb/Plugins/WebGLInputCopy/WebGLWindow/WebGLWindow.jslib
@@ -1,18 +1,9 @@
+#if parseInt(EMSCRIPTEN_VERSION.split('.')[0]) < 2 || (parseInt(EMSCRIPTEN_VERSION.split('.')[0]) == 2 && parseInt(EMSCRIPTEN_VERSION.split('.')[1]) < 0) || (parseInt(EMSCRIPTEN_VERSION.split('.')[0]) == 2 && parseInt(EMSCRIPTEN_VERSION.split('.')[1]) == 0 && parseInt(EMSCRIPTEN_VERSION.split('.')[2]) < 3)
+#error "ThirdWeb plugin requires building with Emscripten 2.0.3 and Unity 2021.2 or newer. Please update"
+#endif
+
 var WebGLWindow = {
     WebGLWindowInit : function() {
-        // use WebAssembly.Table : makeDynCall
-        // when enable. dynCall is undefined
-        if(typeof dynCall === "undefined")
-        {
-            // make Runtime.dynCall to undefined
-            Runtime = { dynCall : undefined }
-        }
-        else
-        {
-            // Remove the `Runtime` object from "v1.37.27: 12/24/2017"
-            // if Runtime not defined. create and add functon!!
-            if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
-        }
     },
     WebGLWindowGetCanvasName: function() {
         var elements = document.getElementsByTagName('canvas');
@@ -33,17 +24,17 @@ var WebGLWindow = {
     },
     WebGLWindowOnFocus: function (cb) {
         window.addEventListener('focus', function () {
-            (!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
+            {{{ makeDynCall("v", "cb") }}}();
         });
     },
     WebGLWindowOnBlur: function (cb) {
         window.addEventListener('blur', function () {
-            (!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
+            {{{ makeDynCall("v", "cb") }}}();
         });
     },
     WebGLWindowOnResize: function(cb) {
         window.addEventListener('resize', function () {
-            (!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
+            {{{ makeDynCall("v", "cb") }}}();
         });
     },
     WebGLWindowInjectFullscreen : function () {

The syntax {{{ makeDynCall('signature', 'functionCallback') }}}(...args); works in

  • Emscripten 2.0.3 and newer (starting at this commit that first appeared in Emscripten 2.0.3 in September 2020)
  • Unity 2021.2 and newer (Unity 2021.2 first updated to Emscripten 2.0.19.5-unity)

Reading the main readme file of this repository, it seems that only Unity 2022 and newer are supported by this plugin, so the above changes will be safe in all supported Unity versions, and will be future compatible with Unity 6 and newer, with best performance in Unity's WebAssembly 2023 feature set and without.

Copy link

linear bot commented Mar 13, 2025

@0xFirekeeper 0xFirekeeper added enhancement New feature or request Unity SDK labels Mar 13, 2025 — with Linear
@0xFirekeeper 0xFirekeeper self-assigned this Mar 13, 2025
@0xFirekeeper 0xFirekeeper linked a pull request Mar 13, 2025 that will close this issue
@0xFirekeeper
Copy link
Member

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Unity SDK
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants