Skip to content

Commit

Permalink
Simplify JS_Interpreter_StmtList
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeom committed Feb 7, 2025
1 parent 8feb254 commit 2672d26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
42 changes: 13 additions & 29 deletions JS-Interpreters/ecmaref6/esl_interpreter.esl
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 17 additions & 4 deletions JS-Interpreters/ecmaref6/main.esl
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
Expand All @@ -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));
}
Expand Down

0 comments on commit 2672d26

Please sign in to comment.