From 17b110debeb08bc41fab5671cf993365eb1cfee1 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 22 Oct 2024 19:54:08 +0200 Subject: [PATCH] jso: fix issues with JSSymbol Inspired by PR #949 --- .../java/org/teavm/jso/core/JSSymbol.java | 12 +++- .../org/teavm/jso/core/test/JSSymbolTest.java | 64 +++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 tests/src/test/java/org/teavm/jso/core/test/JSSymbolTest.java diff --git a/jso/apis/src/main/java/org/teavm/jso/core/JSSymbol.java b/jso/apis/src/main/java/org/teavm/jso/core/JSSymbol.java index 7a44c6ada..a4295d385 100644 --- a/jso/apis/src/main/java/org/teavm/jso/core/JSSymbol.java +++ b/jso/apis/src/main/java/org/teavm/jso/core/JSSymbol.java @@ -17,22 +17,28 @@ package org.teavm.jso.core; import org.teavm.interop.NoSideEffects; import org.teavm.jso.JSBody; -import org.teavm.jso.JSIndexer; +import org.teavm.jso.JSClass; import org.teavm.jso.JSObject; import org.teavm.jso.JSPrimitiveType; @JSPrimitiveType("symbol") +@JSClass public class JSSymbol implements JSObject { private JSSymbol() { } @JSBody(params = "name", script = "return Symbol(name);") + @NoSideEffects public static native JSSymbol create(String name); - @JSIndexer + @JSBody(params = "obj", script = "return obj[this];") + @NoSideEffects public native T get(Object obj); - @JSIndexer + @JSBody(params = {"obj", "value"}, script = "obj[this] = value;") @NoSideEffects public native void set(Object obj, T value); + + @JSBody(params = "obj", script = "return obj[this] !== void 0;") + public native boolean presentIn(Object obj); } diff --git a/tests/src/test/java/org/teavm/jso/core/test/JSSymbolTest.java b/tests/src/test/java/org/teavm/jso/core/test/JSSymbolTest.java new file mode 100644 index 000000000..439e44925 --- /dev/null +++ b/tests/src/test/java/org/teavm/jso/core/test/JSSymbolTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2024 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.core.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.core.JSObjects; +import org.teavm.jso.core.JSString; +import org.teavm.jso.core.JSSymbol; +import org.teavm.jso.core.JSUndefined; +import org.teavm.junit.SkipJVM; +import org.teavm.junit.TeaVMTestRunner; + +@RunWith(TeaVMTestRunner.class) +@SkipJVM +public class JSSymbolTest { + @Test + public void getWorks() { + var s = JSSymbol.create("customString"); + var o1 = JSObjects.create(); + var o2 = JSObjects.create(); + writeSymbol(s, o1, JSString.valueOf("works")); + + assertEquals(JSString.valueOf("works"), s.get(o1)); + assertTrue(s.presentIn(o1)); + assertEquals(JSUndefined.instance(), s.get(o2)); + assertFalse(s.presentIn(o2)); + } + + @Test + public void setWorks() { + var s = JSSymbol.create("customString"); + var o1 = JSObjects.create(); + var o2 = JSObjects.create(); + + s.set(o1, JSString.valueOf("works")); + assertEquals(JSString.valueOf("works"), readSymbol(s, o1)); + assertEquals(JSUndefined.instance(), readSymbol(s, o2)); + } + + @JSBody(params = { "symbol", "o", "value" }, script = "o[symbol] = value;") + private static native void writeSymbol(JSSymbol symbol, Object o, JSObject value); + + @JSBody(params = { "symbol", "o" }, script = "return o[symbol];") + private static native JSObject readSymbol(JSSymbol symbol, Object o); +}