From 306c78459ceccf623807dc3c48527dba51e41dc1 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 9 Sep 2020 19:28:16 +0300 Subject: [PATCH] JS: use symbol instead of string property to represent Java exception wrapper --- .../backend/javascript/JavaScriptTarget.java | 4 +++- .../org/teavm/backend/javascript/runtime.js | 15 ++++++--------- tests/src/test/js/frame.js | 7 ++++--- tests/src/test/js/src/run-tests.js | 7 ++++--- .../src/main/resources/teavm-htmlunit-adapter.js | 7 ++++--- .../junit/src/main/resources/teavm-run-test.html | 7 ++++--- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java index 06939910d..037415775 100644 --- a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java +++ b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java @@ -427,10 +427,12 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { for (Map.Entry entry : controller.getEntryPoints().entrySet()) { - sourceWriter.append("").append(entry.getKey()).ws().append("=").ws(); + sourceWriter.append(entry.getKey()).ws().append("=").ws(); MethodReference ref = entry.getValue().getMethod(); sourceWriter.append("$rt_mainStarter(").appendMethodBody(ref); sourceWriter.append(");").newLine(); + sourceWriter.append(entry.getKey()).append(".").append("javaException").ws().append("=").ws() + .append("$rt_javaException;").newLine(); } for (RendererListener listener : rendererListeners) { diff --git a/core/src/main/resources/org/teavm/backend/javascript/runtime.js b/core/src/main/resources/org/teavm/backend/javascript/runtime.js index c925e1d15..aa5514158 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/backend/javascript/runtime.js @@ -255,7 +255,7 @@ function $rt_voidcls() { function $rt_throw(ex) { throw $rt_exception(ex); } -var $rt_exceptionTag = Symbol("teavmException") +var $rt_javaExceptionProp = Symbol("javaException") function $rt_exception(ex) { var err = ex.$jsException; if (!err) { @@ -263,8 +263,7 @@ function $rt_exception(ex) { if (typeof Error.captureStackTrace === "function") { Error.captureStackTrace(err); } - err.$javaException = ex; - err[$rt_exceptionTag] = true; + err[$rt_javaExceptionProp] = ex; ex.$jsException = err; $rt_fillStack(err, ex); } @@ -650,18 +649,16 @@ function $rt_intBitsToFloat(n) { } function $rt_javaException(e) { - return e instanceof Error && $rt_exceptionTag in e - && typeof e.$javaException === 'object' ? e.$javaException : null; + return e instanceof Error && typeof e[$rt_javaExceptionProp] === 'object' ? e[$rt_javaExceptionProp] : null; } function $rt_jsException(e) { return typeof e.$jsException === 'object' ? e.$jsException : null; } function $rt_wrapException(err) { - var ex = err.$javaException; - if (!ex || !($rt_exceptionTag in err)) { + var ex = err[$rt_javaExceptionProp]; + if (!ex) { ex = $rt_createException($rt_str("(JavaScript) " + err.toString())); - err.$javaException = ex; - err[$rt_exceptionTag] = true; + err[$rt_javaExceptionProp] = ex; ex.$jsException = err; $rt_fillStack(err, ex); } diff --git a/tests/src/test/js/frame.js b/tests/src/test/js/frame.js index 5ce2fa112..524398d32 100644 --- a/tests/src/test/js/frame.js +++ b/tests/src/test/js/frame.js @@ -73,9 +73,10 @@ function launchTest(argument, callback) { function buildErrorMessage(e) { let stack = ""; - if (e.$javaException && e.$javaException.constructor.$meta) { - stack = e.$javaException.constructor.$meta.name + ": "; - stack += e.$javaException.getMessage(); + var je = main.javaException(e); + if (je && je.constructor.$meta) { + stack = je.constructor.$meta.name + ": "; + stack += je.getMessage(); stack += "\n"; } stack += e.stack; diff --git a/tests/src/test/js/src/run-tests.js b/tests/src/test/js/src/run-tests.js index e9a793c4b..db75e7d43 100644 --- a/tests/src/test/js/src/run-tests.js +++ b/tests/src/test/js/src/run-tests.js @@ -273,9 +273,10 @@ function runTeaVM() { function makeErrorMessage(message, e) { message.status = "failed"; - if (e.$javaException) { - message.className = e.$javaException.constructor.name; - message.message = e.$javaException.getMessage(); + var je = main.javaException(e); + if (je) { + message.className = je.constructor.name; + message.message = je.getMessage(); } else { message.className = Object.getPrototypeOf(e).name; message.message = e.message; diff --git a/tools/junit/src/main/resources/teavm-htmlunit-adapter.js b/tools/junit/src/main/resources/teavm-htmlunit-adapter.js index 665a29526..61bc3d7a2 100644 --- a/tools/junit/src/main/resources/teavm-htmlunit-adapter.js +++ b/tools/junit/src/main/resources/teavm-htmlunit-adapter.js @@ -14,9 +14,10 @@ function runMain(argument, stackDecoder, callback) { function makeErrorMessage(message, e) { message.status = "exception"; - if (e.$javaException) { - message.className = e.$javaException.constructor.name; - message.message = e.$javaException.getMessage(); + var je = main.javaException(e); + if (je) { + message.className = je.constructor.name; + message.message = je.getMessage(); } else { message.className = Object.getPrototypeOf(e).name; message.message = e.message; diff --git a/tools/junit/src/main/resources/teavm-run-test.html b/tools/junit/src/main/resources/teavm-run-test.html index 3033a70a5..401a69867 100644 --- a/tools/junit/src/main/resources/teavm-run-test.html +++ b/tools/junit/src/main/resources/teavm-run-test.html @@ -14,9 +14,10 @@ function buildErrorMessage(e) { var stack = ""; - if (e.$javaException && e.$javaException.constructor.$meta) { - stack = e.$javaException.constructor.$meta.name + ": "; - stack += e.$javaException.getMessage() || ""; + var je = main.javaException(e); + if (je && je.constructor.$meta) { + stack = je.constructor.$meta.name + ": "; + stack += je.getMessage() || ""; stack += "\n"; } stack += e.stack;