From 551f0505c721d46033017d879dbb6349378d7464 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 2 Oct 2024 14:54:40 +0200 Subject: [PATCH] wasm gc: fix support of cast/instanceof for JS types; fix accessing top-level declarations --- .../resources/org/teavm/backend/wasm/wasm-gc-runtime.js | 9 ++++++--- jso/impl/src/main/java/org/teavm/jso/impl/JS.java | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/main/resources/org/teavm/backend/wasm/wasm-gc-runtime.js b/core/src/main/resources/org/teavm/backend/wasm/wasm-gc-runtime.js index 1daabcfa4..683090835 100644 --- a/core/src/main/resources/org/teavm/backend/wasm/wasm-gc-runtime.js +++ b/core/src/main/resources/org/teavm/backend/wasm/wasm-gc-runtime.js @@ -156,8 +156,8 @@ TeaVM.wasm = function() { appendToArray: (array, e) => array.push(e), unwrapBoolean: value => value ? 1 : 0, wrapBoolean: value => !!value, - getProperty: (obj, prop) => obj[prop], - getPropertyPure: (obj, prop) => obj[prop], + getProperty: (obj, prop) => obj !== null ? obj[prop] : getGlobalName(prop), + getPropertyPure: (obj, prop) => obj !== null ? obj[prop] : getGlobalName(prop), setProperty: (obj, prop, value) => obj[prop] = value, setPropertyPure: (obj, prop) => obj[prop] = value, global: getGlobalName, @@ -265,6 +265,8 @@ TeaVM.wasm = function() { } }, isPrimitive: (value, type) => typeof value === type, + instanceOf: (value, type) => value instanceof type, + instanceOfOrNull: (value, type) => value === null || value instanceof type, sameRef: (a, b) => a === b, hashCode: (obj) => { if (typeof obj === "object" || typeof obj === "function" || typeof obj === "symbol") { @@ -293,7 +295,8 @@ TeaVM.wasm = function() { for (let i = 0; i < 32; ++i) { imports.teavmJso["createFunction" + i] = (...args) => new Function(...args); imports.teavmJso["callFunction" + i] = (fn, ...args) => fn(...args); - imports.teavmJso["callMethod" + i] = (instance, method, ...args) => instance[method](...args); + imports.teavmJso["callMethod" + i] = (instance, method, ...args) => + instance !== null ? instance[method](...args) : getGlobalName(method)(...args); imports.teavmJso["construct" + i] = (constructor, ...args) => new constructor(...args); } } diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JS.java b/jso/impl/src/main/java/org/teavm/jso/impl/JS.java index 807c4483c..6922fdbe2 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JS.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JS.java @@ -756,10 +756,12 @@ public final class JS { @InjectedBy(JSNativeInjector.class) @NoSideEffects + @Import(name = "instanceOf", module = "teavmJso") public static native boolean instanceOf(JSObject obj, JSObject cls); @InjectedBy(JSNativeInjector.class) @NoSideEffects + @Import(name = "instanceOfOrNull", module = "teavmJso") public static native boolean instanceOfOrNull(JSObject obj, JSObject cls); @InjectedBy(JSNativeInjector.class)