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 for (Map.Entry<? extends String, ? extends TeaVMEntryPoint> entry
: controller.getEntryPoints().entrySet()) { : controller.getEntryPoints().entrySet()) {
sourceWriter.append("").append(entry.getKey()).ws().append("=").ws(); sourceWriter.append(entry.getKey()).ws().append("=").ws();
MethodReference ref = entry.getValue().getMethod(); MethodReference ref = entry.getValue().getMethod();
sourceWriter.append("$rt_mainStarter(").appendMethodBody(ref); sourceWriter.append("$rt_mainStarter(").appendMethodBody(ref);
sourceWriter.append(");").newLine(); sourceWriter.append(");").newLine();
sourceWriter.append(entry.getKey()).append(".").append("javaException").ws().append("=").ws()
.append("$rt_javaException;").newLine();
} }
for (RendererListener listener : rendererListeners) { for (RendererListener listener : rendererListeners) {

View File

@ -255,7 +255,7 @@ function $rt_voidcls() {
function $rt_throw(ex) { function $rt_throw(ex) {
throw $rt_exception(ex); throw $rt_exception(ex);
} }
var $rt_exceptionTag = Symbol("teavmException") var $rt_javaExceptionProp = Symbol("javaException")
function $rt_exception(ex) { function $rt_exception(ex) {
var err = ex.$jsException; var err = ex.$jsException;
if (!err) { if (!err) {
@ -263,8 +263,7 @@ function $rt_exception(ex) {
if (typeof Error.captureStackTrace === "function") { if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(err); Error.captureStackTrace(err);
} }
err.$javaException = ex; err[$rt_javaExceptionProp] = ex;
err[$rt_exceptionTag] = true;
ex.$jsException = err; ex.$jsException = err;
$rt_fillStack(err, ex); $rt_fillStack(err, ex);
} }
@ -650,18 +649,16 @@ function $rt_intBitsToFloat(n) {
} }
function $rt_javaException(e) { function $rt_javaException(e) {
return e instanceof Error && $rt_exceptionTag in e return e instanceof Error && typeof e[$rt_javaExceptionProp] === 'object' ? e[$rt_javaExceptionProp] : null;
&& typeof e.$javaException === 'object' ? e.$javaException : null;
} }
function $rt_jsException(e) { function $rt_jsException(e) {
return typeof e.$jsException === 'object' ? e.$jsException : null; return typeof e.$jsException === 'object' ? e.$jsException : null;
} }
function $rt_wrapException(err) { function $rt_wrapException(err) {
var ex = err.$javaException; var ex = err[$rt_javaExceptionProp];
if (!ex || !($rt_exceptionTag in err)) { if (!ex) {
ex = $rt_createException($rt_str("(JavaScript) " + err.toString())); ex = $rt_createException($rt_str("(JavaScript) " + err.toString()));
err.$javaException = ex; err[$rt_javaExceptionProp] = ex;
err[$rt_exceptionTag] = true;
ex.$jsException = err; ex.$jsException = err;
$rt_fillStack(err, ex); $rt_fillStack(err, ex);
} }

View File

@ -73,9 +73,10 @@ function launchTest(argument, callback) {
function buildErrorMessage(e) { function buildErrorMessage(e) {
let stack = ""; let stack = "";
if (e.$javaException && e.$javaException.constructor.$meta) { var je = main.javaException(e);
stack = e.$javaException.constructor.$meta.name + ": "; if (je && je.constructor.$meta) {
stack += e.$javaException.getMessage(); stack = je.constructor.$meta.name + ": ";
stack += je.getMessage();
stack += "\n"; stack += "\n";
} }
stack += e.stack; stack += e.stack;

View File

@ -273,9 +273,10 @@ function runTeaVM() {
function makeErrorMessage(message, e) { function makeErrorMessage(message, e) {
message.status = "failed"; message.status = "failed";
if (e.$javaException) { var je = main.javaException(e);
message.className = e.$javaException.constructor.name; if (je) {
message.message = e.$javaException.getMessage(); message.className = je.constructor.name;
message.message = je.getMessage();
} else { } else {
message.className = Object.getPrototypeOf(e).name; message.className = Object.getPrototypeOf(e).name;
message.message = e.message; message.message = e.message;

View File

@ -14,9 +14,10 @@ function runMain(argument, stackDecoder, callback) {
function makeErrorMessage(message, e) { function makeErrorMessage(message, e) {
message.status = "exception"; message.status = "exception";
if (e.$javaException) { var je = main.javaException(e);
message.className = e.$javaException.constructor.name; if (je) {
message.message = e.$javaException.getMessage(); message.className = je.constructor.name;
message.message = je.getMessage();
} else { } else {
message.className = Object.getPrototypeOf(e).name; message.className = Object.getPrototypeOf(e).name;
message.message = e.message; message.message = e.message;

View File

@ -14,9 +14,10 @@
function buildErrorMessage(e) { function buildErrorMessage(e) {
var stack = ""; var stack = "";
if (e.$javaException && e.$javaException.constructor.$meta) { var je = main.javaException(e);
stack = e.$javaException.constructor.$meta.name + ": "; if (je && je.constructor.$meta) {
stack += e.$javaException.getMessage() || ""; stack = je.constructor.$meta.name + ": ";
stack += je.getMessage() || "";
stack += "\n"; stack += "\n";
} }
stack += e.stack; stack += e.stack;