JS: use symbol instead of string property to represent Java exception wrapper

This commit is contained in:
Alexey Andreev 2020-09-09 19:28:16 +03:00
parent fcc1fb7690
commit 306c78459c
6 changed files with 25 additions and 22 deletions

View File

@ -427,10 +427,12 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost {
for (Map.Entry<? extends String, ? extends TeaVMEntryPoint> 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) {

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;