Skip to content

Commit

Permalink
jso: fix issues with JSSymbol
Browse files Browse the repository at this point in the history
Inspired by PR #949
  • Loading branch information
konsoletyper committed Oct 22, 2024
1 parent 7a2cf55 commit 17b110d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
12 changes: 9 additions & 3 deletions jso/apis/src/main/java/org/teavm/jso/core/JSSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@

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<T> implements JSObject {
private JSSymbol() {
}

@JSBody(params = "name", script = "return Symbol(name);")
@NoSideEffects
public static native <T> JSSymbol<T> 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);
}
64 changes: 64 additions & 0 deletions tests/src/test/java/org/teavm/jso/core/test/JSSymbolTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 17b110d

Please sign in to comment.