From 2672d2661e7eeabfacfc1988fce596bdac3b1966 Mon Sep 17 00:00:00 2001 From: Filipe Marques Date: Thu, 6 Feb 2025 11:35:11 +0000 Subject: [PATCH] Simplify `JS_Interpreter_StmtList` --- JS-Interpreters/ecmaref6/esl_interpreter.esl | 42 ++++++-------------- JS-Interpreters/ecmaref6/main.esl | 21 ++++++++-- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/JS-Interpreters/ecmaref6/esl_interpreter.esl b/JS-Interpreters/ecmaref6/esl_interpreter.esl index cd9df45cc..5d6928b7f 100644 --- a/JS-Interpreters/ecmaref6/esl_interpreter.esl +++ b/JS-Interpreters/ecmaref6/esl_interpreter.esl @@ -1,15 +1,15 @@ /* Copyright (C) 2022-2025 formalsec programmers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -1258,7 +1258,7 @@ function JS_Interpreter_Stmt(s, scope) { /* 13.2.13 Block */ /* Block : { } */ - if (l_len(StatementList) == 0) { + if (StatementList == []) { /* 1. Return NormalCompletion(empty). */ return NormalCompletion('empty); } @@ -1867,31 +1867,15 @@ function JS_Interpreter_LexicalDecl(LexicalDeclaration, scope) { } function JS_Interpreter_StmtList(stmts, scope) { - /* StatementList : StatementList StatementListItem */ - if (l_len(stmts) == 1) { - Statement := l_nth(stmts, 0); - s := JS_Interpreter_Stmt(Statement, scope) catch Interpreter_Statement_Guard; - return s; - } - - i := (l_len(stmts)) - 1; - Statement := l_nth(stmts, i); - StatementList := []; - while (i > 0) { - i := i - 1; - StatementList := l_prepend(l_nth(stmts, i), StatementList); - } - - /* 1. Let sl be the result of evaluating StatementList. */ - sl := JS_Interpreter_StmtList(StatementList, scope); - /* 2. ReturnIfAbrupt(sl). */ - copy := sl; - @ReturnIfAbrupt(copy); - /* 3. Let s be the result of evaluating StatementListItem. */ - s := JS_Interpreter_Stmt(Statement, scope) catch Interpreter_Statement_Guard; - /* 4. Return Completion(UpdateEmpty(s, sl.[[value]])). */ - return Completion(UpdateEmpty(s, getCompletionValue(sl))); - + last_completion := NormalCompletion('empty); + foreach (stmt : stmts) { + s := JS_Interpreter_Stmt(stmt, scope) catch Interpreter_Statement_Guard; + if (isAnAbruptCompletion(s)) { + return UpdateEmpty(s, getCompletionValue(last_completion)); + } + last_completion := UpdateEmpty(s, getCompletionValue(last_completion)); + } + return last_completion; } function JS_Interpreter_BinExpr(e, scope) { diff --git a/JS-Interpreters/ecmaref6/main.esl b/JS-Interpreters/ecmaref6/main.esl index 1e2435950..f1b189389 100644 --- a/JS-Interpreters/ecmaref6/main.esl +++ b/JS-Interpreters/ecmaref6/main.esl @@ -1,15 +1,15 @@ /* Copyright (C) 2022-2025 formalsec programmers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -22,9 +22,22 @@ function init() { return; } +function stringOfError(error : object) : string { + name := Get(error, "name"); + @ReturnIfAbrupt(name); + if (name == 'undefined) + name := "Error"; + + message := Get(error, "message"); + @ReturnIfAbrupt(message); + if (message == 'undefined) + return "Uncaught " + name; + return "Uncaught " + name + ": " + message; +} + function ECMAScriptReturn(ret) { c_value := getCompletionValue(ret); - if ((typeof c_value == "object") &&& ("ErrorData" in_obj c_value) ) { + if ((typeof c_value == "object") &&& ("ErrorData" in_obj c_value)) { error_name := {c_value.Get}(c_value, "name", c_value); return newCompletion(getCompletionType(ret), error_name, getCompletionTarget(ret)); }