Skip to content

Commit

Permalink
wasm gc: fix issue with JS exceptions, trying to propagate Java excep…
Browse files Browse the repository at this point in the history
…tion message to JS error message
  • Loading branch information
konsoletyper committed Oct 4, 2024
1 parent 753a028 commit 7aec076
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/teavm/backend/wasm/WasmGCTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ public void emit(ListableClassHolderSource classes, BuildTarget buildTarget, Str
removeStringEntryFunction.setExportName("reportGarbageCollectedString");
}

var exceptionMessageRef = new MethodReference(Throwable.class, "getMessage", Throwable.class);
if (controller.getDependencyInfo().getMethod(exceptionMessageRef) != null) {
var exceptionMessageFunction = declarationsGenerator.functions().forInstanceMethod(exceptionMessageRef);
exceptionMessageFunction.setExportName("exceptionMessage");
}

moduleGenerator.generate();
customGenerators.contributeToModule(module);
adjustModuleMemory(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;
import org.teavm.backend.wasm.WasmRuntime;
import org.teavm.backend.wasm.runtime.gc.WasmGCSupport;
import org.teavm.dependency.AbstractDependencyListener;
import org.teavm.dependency.DependencyAgent;
import org.teavm.dependency.DependencyAnalyzer;
import org.teavm.model.MethodReference;

Expand Down Expand Up @@ -105,6 +107,17 @@ private void contributeExceptionUtils() {
analyzer.linkMethod(new MethodReference(WasmGCSupport.class, "throwCloneNotSupportedException",
void.class)).use();
analyzer.linkMethod(new MethodReference(NullPointerException.class, "<init>", void.class)).use();

analyzer.addDependencyListener(new AbstractDependencyListener() {
@Override
public void classReached(DependencyAgent agent, String className) {
if (className.equals(Throwable.class.getName())) {
agent.linkMethod(new MethodReference(Throwable.class, "getMessage", String.class))
.propagate(0, Throwable.class)
.use();
}
}
});
}

private void contributeInitializerUtils() {
Expand Down
22 changes: 19 additions & 3 deletions core/src/main/resources/org/teavm/backend/wasm/wasm-gc-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ TeaVM.wasm = function() {
imports.teavmMath = Math;
}

let javaExceptionSymbol = Symbol("javaException");
class JavaError extends Error {
constructor(javaException) {
super();
this[javaExceptionSymbol] = javaException;
}
get message() {
let exceptionMessage = exports.exceptionMessage;
if (typeof exceptionMessage === "function") {
let message = exceptionMessage(this[javaExceptionSymbol]);
if (message != null) {
return stringToJava(message);
}
}
return "(could not fetch message)";
}
}

function dateImports(imports) {
imports.teavmDate = {
currentTimeMillis: () => new Date().getTime(),
Expand Down Expand Up @@ -119,7 +137,6 @@ TeaVM.wasm = function() {
let javaObjectSymbol = Symbol("javaObject");
let functionsSymbol = Symbol("functions");
let functionOriginSymbol = Symbol("functionOrigin");
let javaExceptionSymbol = Symbol("javaException");

let jsWrappers = new WeakMap();
let javaWrappers = new WeakMap();
Expand Down Expand Up @@ -179,9 +196,8 @@ TeaVM.wasm = function() {
return wrapper;
}
}
let wrapper = new Error();
let wrapper = new JavaError(javaException);
javaExceptionWrappers.set(javaException, new WeakRef(wrapper));
wrapper[javaExceptionSymbol] = javaException;
return wrapper;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
*/
package org.teavm.jso.impl;

@interface JSBodyDelegate {
public @interface JSBodyDelegate {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.teavm.dependency.MethodDependency;
import org.teavm.jso.JSObject;
import org.teavm.jso.impl.JS;
import org.teavm.jso.impl.JSBodyDelegate;
import org.teavm.jso.impl.JSWrapper;
import org.teavm.model.MethodReference;

Expand All @@ -33,15 +34,30 @@ public void started(DependencyAgent agent) {

@Override
public void methodReached(DependencyAgent agent, MethodDependency method) {
if (method.getMethod().getOwnerName().equals(JS.class.getName())) {
if (method.getMethod().getName().equals("jsArrayItem")) {
method.getVariable(1).getArrayItem().connect(method.getResult());
var methodReader = method.getMethod();
if (methodReader.getOwnerName().equals(JS.class.getName())) {
switch (methodReader.getName()) {
case "jsArrayItem":
method.getVariable(1).getArrayItem().connect(method.getResult());
break;
case "invoke":
case "apply":
case "construct":
case "get":
case "getPure":
case "set":
case "setPure":
case "global":
method.getThrown().propagate(agent.getType(WasmGCExceptionWrapper.class.getName()));
break;
}
} else if (method.getMethod().getOwnerName().equals(JSWrapper.class.getName())) {
if (method.getMethod().getName().equals("wrap")) {
} else if (methodReader.getOwnerName().equals(JSWrapper.class.getName())) {
if (methodReader.getName().equals("wrap")) {
agent.linkMethod(new MethodReference(JSWrapper.class, "createWrapper", JSObject.class, Object.class))
.use();
}
} else if (methodReader.getAnnotations().get(JSBodyDelegate.class.getName()) != null) {
method.getThrown().propagate(agent.getType(WasmGCExceptionWrapper.class.getName()));
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/src/test/java/org/teavm/jso/test/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.teavm.junit.EachTestCompiledSeparately;
import org.teavm.junit.OnlyPlatform;
import org.teavm.junit.SkipJVM;
import org.teavm.junit.SkipPlatform;
import org.teavm.junit.TeaVMTestRunner;
import org.teavm.junit.TestPlatform;

Expand Down Expand Up @@ -103,7 +102,6 @@ public void catchThrowableAsNativeException() {
}

@Test
@SkipPlatform(TestPlatform.WEBASSEMBLY_GC)
public void catchNativeExceptionAsRuntimeException() {
StringBuilder sb = new StringBuilder();
try {
Expand Down

0 comments on commit 7aec076

Please sign in to comment.