Skip to content

Commit 6c91d9b

Browse files
committed
WebGL tweak for Unity 6.3
1. Added updateMemoryViews hook (lines 98-110) - Now hooks both memory update functions 2. Created getWasmTable() helper (lines 59-65) - Tries multiple sources for the WASM table 3. Updated addFunction/removeFunction - Uses the new robust table lookup 4. Updated getWasmTableEntry - Uses the helper for fallback --- - Tencent/puerts#2179 - https://openupm.com/packages/com.phasetwo.puertsfix.webgl/ - https://discussions.unity.com/t/makedyncall-replacing-dyncall-in-unity-6/1543088 - https://docs.unity3d.com/6000.0/Documentation/ScriptReference/PlayerSettings.WebGL-webAssemblyTable.html
1 parent f10521c commit 6c91d9b

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

Puerts.WebGL/Runtime/Plugins/WebGL/puerts.jslib

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,31 @@ var exportDLL = {
5656
var _getTempRet0 = () => __tempRet0 | 0
5757
}
5858

59-
if (typeof addFunction === "undefined") {
60-
const table = Module.wasmTable || wasmTable // whichever Unity exposes
61-
var addFunction = function (jsFunc /*, sig */) {
62-
const idx = table.length
63-
table.grow(1)
64-
table.set(idx, jsFunc)
65-
return idx
66-
}
67-
}
68-
if (typeof removeFunction === "undefined") {
69-
const table = Module.wasmTable || wasmTable
70-
var removeFunction = function (idx) {
71-
// Emscripten no longer shrinks the table – just replace entry with a noop
72-
table.set(idx, () => {})
73-
}
59+
// Unity 6.3+ compatibility: helper to get wasmTable from multiple sources
60+
var getWasmTable = function() {
61+
if (typeof wasmTable !== 'undefined') return wasmTable;
62+
if (Module && Module.wasmTable) return Module.wasmTable;
63+
if (Module && Module.asm && Module.asm.__indirect_function_table) return Module.asm.__indirect_function_table;
64+
return null;
65+
};
66+
67+
if (typeof addFunction === "undefined") {
68+
var addFunction = function (jsFunc /*, sig */) {
69+
const table = getWasmTable();
70+
if (!table) throw new Error('wasmTable not found for addFunction');
71+
const idx = table.length;
72+
table.grow(1);
73+
table.set(idx, jsFunc);
74+
return idx;
75+
}
76+
}
77+
if (typeof removeFunction === "undefined") {
78+
var removeFunction = function (idx) {
79+
const table = getWasmTable();
80+
if (!table) return; // silently fail if no table
81+
// Emscripten no longer shrinks the table – just replace entry with a noop
82+
table.set(idx, () => {});
83+
}
7484
}
7585

7686
let oldUpdateGlobalBufferAndViews = updateGlobalBufferAndViews;
@@ -85,6 +95,21 @@ var exportDLL = {
8595
);
8696
}
8797

98+
// Unity 6.3+ may call updateMemoryViews directly instead of updateGlobalBufferAndViews
99+
if (typeof updateMemoryViews === "function") {
100+
let oldUpdateMemoryViews = updateMemoryViews;
101+
updateMemoryViews = function (buf) {
102+
oldUpdateMemoryViews(buf);
103+
global.PuertsWebGL.updateGlobalBufferAndViews(
104+
HEAP8,
105+
HEAPU8,
106+
HEAP32,
107+
HEAPF32,
108+
HEAPF64
109+
);
110+
}
111+
}
112+
88113
global.PuertsWebGL.Init({
89114
UTF8ToString,
90115
UTF16ToString,
@@ -99,7 +124,11 @@ var exportDLL = {
99124
stackSave,
100125
stackRestore,
101126
getWasmTableEntry: (typeof getWasmTableEntry != 'undefined') ? getWasmTableEntry : function(funcPtr) {
102-
return wasmTable.get(funcPtr);
127+
var table = getWasmTable();
128+
if (table) {
129+
return table.get(funcPtr);
130+
}
131+
throw new Error('wasmTable not found - Unity 6.3 may require WebAssembly.Table to be disabled in Player Settings');
103132
},
104133
addFunction,
105134
removeFunction,

0 commit comments

Comments
 (0)