Skip to content

Commit

Permalink
wasm gc: support primitive array conversion to JavaScript in JSO
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Sep 28, 2024
1 parent bbf4576 commit 5ad4496
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var TeaVM = TeaVM || {};
TeaVM.wasm = function() {
let exports;
let getGlobalName = function(name) {
return eval("return " + {name});
return eval(name);
}
function defaults(imports) {
let stderr = "";
Expand Down
1 change: 0 additions & 1 deletion jso/impl/src/main/java/org/teavm/jso/impl/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,6 @@ public static native JSObject construct(JSObject cls, JSObject a, JSObject b, JS

@InjectedBy(JSNativeInjector.class)
@NoSideEffects
@Import(name = "global", module = "teavmJso")
public static native JSObject global(String name);

@InjectedBy(JSNativeInjector.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ WasmFunction getFunctionConstructor(WasmGCIntrinsicContext context, int index) {
if (function == null) {
var extern = WasmType.SpecialReferenceKind.EXTERN.asNonNullType();
var constructorParamTypes = new WasmType[index + 1];
Arrays.fill(constructorParamTypes, extern);
Arrays.fill(constructorParamTypes, WasmType.Reference.EXTERN);
var functionType = context.functionTypes().of(extern, constructorParamTypes);
function = new WasmFunction(functionType);
function.setName(context.names().topLevel("teavm.js:createFunction" + index));
Expand All @@ -44,11 +44,9 @@ WasmFunction getFunctionConstructor(WasmGCIntrinsicContext context, int index) {
WasmFunction getFunctionCaller(WasmGCIntrinsicContext context, int index) {
var function = callers[index];
if (function == null) {
var extern = WasmType.SpecialReferenceKind.EXTERN.asNonNullType();
var paramTypes = new WasmType[index + 1];
Arrays.fill(paramTypes, extern);
paramTypes[0] = WasmType.Reference.EXTERN;
var functionType = context.functionTypes().of(extern, paramTypes);
Arrays.fill(paramTypes, WasmType.Reference.EXTERN);
var functionType = context.functionTypes().of(WasmType.Reference.EXTERN, paramTypes);
function = new WasmFunction(functionType);
function.setName(context.names().topLevel("teavm.js:callFunction" + index));
function.setImportModule("teavmJso");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
import org.teavm.ast.InvocationExpr;
import org.teavm.backend.wasm.intrinsics.gc.WasmGCIntrinsic;
import org.teavm.backend.wasm.intrinsics.gc.WasmGCIntrinsicContext;
import org.teavm.backend.wasm.model.WasmFunction;
import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmCall;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.jso.JSObject;
import org.teavm.jso.impl.JS;
import org.teavm.model.MethodReference;

class WasmGCJSIntrinsic implements WasmGCIntrinsic {
private WasmFunction globalFunction;

@Override
public WasmExpression apply(InvocationExpr invocation, WasmGCIntrinsicContext context) {
switch (invocation.getMethod().getName()) {
Expand All @@ -35,8 +42,26 @@ public WasmExpression apply(InvocationExpr invocation, WasmGCIntrinsicContext co
var function = context.functions().forStaticMethod(JS_TO_STRING);
return new WasmCall(function, context.generate(invocation.getArguments().get(0)));
}
case "global": {
var stringToJs = context.functions().forStaticMethod(STRING_TO_JS);
var name = new WasmCall(stringToJs, context.generate(invocation.getArguments().get(0)));
return new WasmCall(getGlobalFunction(context), name);
}
default:
throw new IllegalArgumentException();
}
}

private WasmFunction getGlobalFunction(WasmGCIntrinsicContext context) {
if (globalFunction == null) {
globalFunction = new WasmFunction(context.functionTypes().of(WasmType.Reference.EXTERN,
WasmType.Reference.EXTERN));
globalFunction.setName(context.names().suggestForMethod(new MethodReference(JS.class,
"global", String.class, JSObject.class)));
globalFunction.setImportName("global");
globalFunction.setImportModule("teavmJso");
context.module().functions.add(globalFunction);
}
return globalFunction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.teavm.backend.wasm.generate.gc.classes.WasmGCCustomTypeMapperFactoryContext;
import org.teavm.backend.wasm.model.WasmType;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSArray;

class WasmGCJSTypeMapper implements WasmGCCustomTypeMapper, WasmGCCustomTypeMapperFactory {
@Override
public WasmType map(String className) {
if (className.equals(JSObject.class.getName())) {
return WasmType.SpecialReferenceKind.EXTERN.asNonNullType();
if (className.equals(JSObject.class.getName())
|| className.equals(JSArray.class.getName())) {
return WasmType.Reference.EXTERN;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public static void install(TeaVMHost host, TeaVMWasmGCHost wasmGCHost, JSBodyRep
wasmGCHost.addIntrinsic(new MethodReference(JS.class, "wrap", String.class, JSObject.class), jsIntrinsic);
wasmGCHost.addIntrinsic(new MethodReference(JS.class, "unwrapString", JSObject.class, String.class),
jsIntrinsic);
wasmGCHost.addIntrinsic(new MethodReference(JS.class, "global", String.class, JSObject.class), jsIntrinsic);
}
}
4 changes: 0 additions & 4 deletions tests/src/test/java/org/teavm/jso/test/ConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void convertsPrimitivesToJava() {
}

@Test
@SkipPlatform(TestPlatform.WEBASSEMBLY_GC)
public void convertsPrimitiveArraysToJavaScript() {
assertEquals("true:2:3:64:4:5.5:6.5:foo", combinePrimitiveArrays(new boolean[] { true }, new byte[] { 2 },
new short[] { 3 }, new char[] { '@' }, new int[] { 4 }, new float[] { 5.5F }, new double[] { 6.5 },
Expand All @@ -84,7 +83,6 @@ public void convertsPrimitiveArraysToJava() {
}

@Test
@SkipPlatform(TestPlatform.WEBASSEMBLY_GC)
public void convertsPrimitiveArrays2ToJavaScript() {
assertEquals("true:2:3:64:4:5.5:6.5:foo", combinePrimitiveArrays2(new boolean[][] {{ true }},
new byte[][] {{ 2 }}, new short[][] {{ 3 }}, new char[][] {{ '@' }}, new int[][] {{ 4 }},
Expand All @@ -111,7 +109,6 @@ public void convertsPrimitiveArrays2ToJava() {
}

@Test
@SkipPlatform(TestPlatform.WEBASSEMBLY_GC)
public void convertsPrimitiveArrays4ToJavaScript() {
assertEquals("true:2:3:64:4:5.5:6.5:foo", combinePrimitiveArrays4(new boolean[][][][] {{{{ true }}}},
new byte[][][][] {{{{ 2 }}}}, new short[][][][] {{{{ 3 }}}}, new char[][][][] {{{{ '@' }}}},
Expand Down Expand Up @@ -153,7 +150,6 @@ public void convertsArrayOfJSObject() {
}

@Test
@SkipPlatform(TestPlatform.WEBASSEMBLY_GC)
public void copiesArray() {
int[] array = { 23 };
assertEquals(24, mutate(array));
Expand Down

0 comments on commit 5ad4496

Please sign in to comment.