Skip to content

Commit d110d7a

Browse files
kevinoconnor7copybara-github
authored andcommitted
[WASM] JsInterop - Emit an explicit error in debug if a type is used before load
Currently if a JS user attempts to use a WASM type before it has been loaded you just end up with a somewhat obscure undefined deref in the `Reflect` calls. Instead, for debug builds, we'll emit a much more explicit message about what has gone wrong. PiperOrigin-RevId: 926948343
1 parent bdcf1a2 commit d110d7a

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

jre/java/super-wasm/jsinterop_runtime.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414

1515
goog.module('j2wasm.JsInteropRuntime');
1616

17+
/**
18+
* @param {string} id
19+
* @return {!Function}
20+
*/
21+
function getWasmConstructor(id) {
22+
const ctors = /** @type {!Object<string, !Function>|undefined} */ (globalThis.j2wasmJsConstructors);
23+
if (goog.DEBUG) {
24+
if (!ctors || !ctors[id]) {
25+
throw new Error(
26+
`Attempted to use WASM type '${id}' before it has been loaded.`);
27+
}
28+
}
29+
return ctors[id];
30+
}
31+
1732
/**
1833
* Creates a proxy for the given exported constructor that intercepts
1934
* instantiation and static calls.
@@ -30,13 +45,13 @@ function constructorProxy(id) {
3045
if (newTarget !== proxy) {
3146
throw new TypeError('WASM types cannot be subtyped');
3247
}
33-
return new globalThis.j2wasmJsConstructors[id](...args);
48+
return new (getWasmConstructor(id))(...args);
3449
},
3550
get(target, property, receiver) {
36-
return Reflect.get(globalThis.j2wasmJsConstructors[id], property, receiver);
51+
return Reflect.get(getWasmConstructor(id), property, receiver);
3752
},
3853
set(target, property, value, receiver) {
39-
return Reflect.set(globalThis.j2wasmJsConstructors[id], property, value, receiver);
54+
return Reflect.set(getWasmConstructor(id), property, value, receiver);
4055
},
4156
});
4257
return proxy;

0 commit comments

Comments
 (0)