jso: fix issues with JSSymbol

Inspired by PR #949
This commit is contained in:
Alexey Andreev 2024-10-22 19:54:08 +02:00
parent 7a2cf555e3
commit 17b110debe
2 changed files with 73 additions and 3 deletions

View File

@ -17,22 +17,28 @@ package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects; import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSIndexer; import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
import org.teavm.jso.JSPrimitiveType; import org.teavm.jso.JSPrimitiveType;
@JSPrimitiveType("symbol") @JSPrimitiveType("symbol")
@JSClass
public class JSSymbol<T> implements JSObject { public class JSSymbol<T> implements JSObject {
private JSSymbol() { private JSSymbol() {
} }
@JSBody(params = "name", script = "return Symbol(name);") @JSBody(params = "name", script = "return Symbol(name);")
@NoSideEffects
public static native <T> JSSymbol<T> create(String name); public static native <T> JSSymbol<T> create(String name);
@JSIndexer @JSBody(params = "obj", script = "return obj[this];")
@NoSideEffects
public native T get(Object obj); public native T get(Object obj);
@JSIndexer @JSBody(params = {"obj", "value"}, script = "obj[this] = value;")
@NoSideEffects @NoSideEffects
public native void set(Object obj, T value); public native void set(Object obj, T value);
@JSBody(params = "obj", script = "return obj[this] !== void 0;")
public native boolean presentIn(Object obj);
} }

View File

@ -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<Object> symbol, Object o, JSObject value);
@JSBody(params = { "symbol", "o" }, script = "return o[symbol];")
private static native JSObject readSymbol(JSSymbol<Object> symbol, Object o);
}