Skip to content

Commit

Permalink
Use foreach instead of while
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeom committed Feb 14, 2025
1 parent 91e8d92 commit e5d8fa5
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 272 deletions.
8 changes: 4 additions & 4 deletions JS-Interpreters/ecmaref6/section 12/section_12.14.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 @@ -289,7 +289,7 @@ function DestructuringAssignmentRestElement(param, value, scope) {
/* 3. Let n=0; */
n := 0.;
/* 4. Repeat while iteratorRecord.[[done]] is false, */
while (iteratorRecord.done == false) {
while (!(iteratorRecord.done)) {
/* 4a. Let next be IteratorStep(iteratorRecord.[[iterator]]). */
next := IteratorStep(iteratorRecord.iterator);
/* 4b. If next is an abrupt completion, set iteratorRecord.[[done]] to true. */
Expand Down
17 changes: 7 additions & 10 deletions JS-Interpreters/ecmaref6/section 14/section_14.5.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 @@ -209,20 +209,17 @@ function BindingClassDeclarationEvaluation(ClassDeclaration, scope) {
function getSeparateMethods(body) {
constructor := 'empty;
regularMethods := [];
i := 0;
len := l_len(body);
while (i < len) {
e := l_nth(body, i);
foreach (e : body) {
if (e.type == "MethodDefinition") {
if (e.key.name == "constructor")
constructor := e;
else
regularMethods := l_add (regularMethods, e);
regularMethods := l_prepend(e, regularMethods);
} else if (e.type == "Property") {
regularMethods := l_add (regularMethods, e);
regularMethods := l_prepend(e, regularMethods);
}
i := i + 1;
}
regularMethods := l_reverse(regularMethods);
return { constructor: constructor, regularMethods: regularMethods };
}

28 changes: 5 additions & 23 deletions JS-Interpreters/ecmaref6/section 15/section_15.10.esl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function RegExpPrototypeExec(global, this, NewTarget, strict, args) {
/* 8. Let matchSucceeded be false. */
matchSucceeded := false;
/* 9. Repeat, while matchSucceeded is false */
while (matchSucceeded == false) {
while (!matchSucceeded) {
/* a. If i < 0 or i > length, then */
if ((i < 0) || (i > length)) {
/* i. Call the [[Put]] internal method of R with arguments "lastIndex", 0, and true. */
Expand Down Expand Up @@ -923,29 +923,19 @@ function CharacterClassUnion(cs1, cs2) {
if (cs2.positive != null) {
if (cs1.positive == null)
cs1.positive := {};
i := 0;
list := obj_fields cs2.positive;
len := l_len(list);
while (i < len) {
P := l_nth (list, i);
foreach (P : obj_fields cs2.positive) {
CP := cs2.positive[P];
cs1.positive[P] := CP;
i := i + 1;
}
}

/* Union for negative: */
if (cs2.negative != null) {
if (cs1.negative == null)
cs1.negative := {};
i := 0;
list := obj_fields cs2.negative;
len := l_len(list);
while (i < len) {
P := l_nth (list, i);
foreach (P : obj_fields cs2.negative) {
CP := cs2.negative[P];
cs1.negative[P] := CP;
i := i + 1;
}
}

Expand Down Expand Up @@ -1238,13 +1228,9 @@ function JS_Interpreter_RegEx(re, flags) {
| { type: "CharacterClass", negative: true, expressions: res} -> {
cs := EmptyCharacterClass(true);
ic := flags.ic;
len := l_len(res);
i := 0;
while (i < len) {
el := l_nth(res, i);
foreach (el : res) {
cs' := JS_Interpreter_CharacterClassElement(el, ic, true);
CharacterClassUnion(cs, cs');
i := i + 1;
}

m := lambda (st, k) [cs] {
Expand All @@ -1263,13 +1249,9 @@ function JS_Interpreter_RegEx(re, flags) {
| { type: "CharacterClass", expressions: res} -> {
cs := EmptyCharacterClass(false);
ic := flags.ic;
len := l_len(res);
i := 0;
while (i < len) {
el := l_nth(res, i);
foreach (el : res) {
cs' := JS_Interpreter_CharacterClassElement(el, ic, false);
CharacterClassUnion(cs, cs');
i := i + 1;
}
m := lambda (st, k) [cs] {
c := stateGetChar(st);
Expand Down
69 changes: 27 additions & 42 deletions JS-Interpreters/ecmaref6/section 18/section_18.2.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 @@ -139,14 +139,10 @@ function EvalDeclarationInstantiation(body, varEnv, lexEnv, strict, isGlobalEnvR
/* a. If varEnvRec is a global Environment Record, then */
if (isGlobalEnvRec) {
/* i. For each name in varNames, do */
i := 0;
varNames_l := l_len(varNames);
/* 1. If varEnvRec.HasLexicalDeclaration(name) is true, throw a SyntaxError exception. */
while (i < varNames_l) {
name := l_nth(varNames, i);
foreach (name : varNames) {
if ({varEnvRec.HasBinding}(varEnvRec, name))
throw SyntaxErrorConstructorInternal();
i := i + 1;
}
/* NOTE: eval will not create a global var declaration that would be shadowed by a global lexical declaration. */
}
Expand Down Expand Up @@ -185,19 +181,16 @@ function EvalDeclarationInstantiation(body, varEnv, lexEnv, strict, isGlobalEnvR
/* 9. Let declaredVarNames be an empty List. */
declaredVarNames := [];
/* 10. For each d in varDeclarations, do */
varNames_l := l_len(varNames);
i := 0;
while (i < varNames_l) {
vn := l_nth(varNames, i);
foreach (vn : varNames) {
if (!(in_list(vn, declaredFunctionNames))) {
if (isGlobalEnvRec) {
/* TODO */
}
if (!(in_list(vn, declaredVarNames)))
declaredVarNames := l_add(declaredVarNames, vn);
declaredVarNames := l_prepend(vn, declaredVarNames);
}
i := i + 1;
}
declaredVarNames := l_reverse(declaredVarNames);
/* 11. NOTE: No abnormal terminations occur after this algorithm step unless
varEnvRec is a global Environment Record and the global object is a
Proxy exotic object. */
Expand All @@ -216,35 +209,28 @@ function EvalDeclarationInstantiation(body, varEnv, lexEnv, strict, isGlobalEnvR
}

/* 14. For each production f in functionsToInitialize, do */
functionsToInitialize_l := l_len(functionsToInitialize);
i := 0;
while (i < functionsToInitialize_l) {
f := l_nth(functionsToInitialize, i);
fn := f.id.name;
fo := JS_Interpreter_FunctionDeclaration(f, lexEnv);
if (isGlobalEnvRec) {
/* TODO */
status := {varEnvRec.CreateMutableBinding}(varEnvRec, fn, true);
{varEnvRec.SetMutableBinding}(varEnvRec, fn, fo, false);
@ReturnIfAbrupt(status);
} else {
bindingExists := {varEnvRec.HasBinding}(varEnvRec, fn);
if (!(bindingExists)) {
status := {varEnvRec.CreateMutableBinding}(varEnvRec, fn, true);
assert (!(isAnAbruptCompletion(status)));
status := {varEnvRec.InitializeBinding}(varEnvRec, fn, fo);
} else {
status := {varEnvRec.SetMutableBinding}(varEnvRec, fn, fo, false);
}
}
@ReturnIfAbrupt(status);
i := i + 1;
foreach (f : functionsToInitialize) {
fn := f.id.name;
fo := JS_Interpreter_FunctionDeclaration(f, lexEnv);
if (isGlobalEnvRec) {
/* TODO */
status := {varEnvRec.CreateMutableBinding}(varEnvRec, fn, true);
{varEnvRec.SetMutableBinding}(varEnvRec, fn, fo, false);
@ReturnIfAbrupt(status);
} else {
bindingExists := {varEnvRec.HasBinding}(varEnvRec, fn);
if (!(bindingExists)) {
status := {varEnvRec.CreateMutableBinding}(varEnvRec, fn, true);
assert (!(isAnAbruptCompletion(status)));
status := {varEnvRec.InitializeBinding}(varEnvRec, fn, fo);
} else {
status := {varEnvRec.SetMutableBinding}(varEnvRec, fn, fo, false);
}
}
@ReturnIfAbrupt(status);
}
/* 15. For each String vn in declaredVarNames, in list order do */
varNames_l := l_len(declaredVarNames);
i := 0;
while (i < varNames_l) {
vn := l_nth(declaredVarNames, i);
foreach (vn : declaredVarNames) {
/* If varEnvRec is a global Environment Record, then */
if (isGlobalEnvRec) {
/* TODO */
Expand All @@ -258,7 +244,6 @@ function EvalDeclarationInstantiation(body, varEnv, lexEnv, strict, isGlobalEnvR
assert(!(isAnAbruptCompletion(status)));
}
}
i := i + 1;
}
/* 16. Return NormalCompletion(empty) */
return NormalCompletion('empty);
Expand Down
8 changes: 1 addition & 7 deletions JS-Interpreters/ecmaref6/section 19/section_19.1.esl
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@ function ObjectAssign(globalObject, this, NewTarget, strict, params) {
}

/* Repeat for each element nextKey of keys in List order, */
j := 0;
kLen := l_len(keys);

while (j < kLen) {
nextKey := l_nth(keys, j);
foreach(nextKey : keys) {
/* Let desc be from.[[GetOwnProperty]](nextKey). */
desc := {from.GetOwnProperty}(from, nextKey);
/* ReturnIfAbrupt(desc). */
Expand All @@ -272,8 +268,6 @@ function ObjectAssign(globalObject, this, NewTarget, strict, params) {
/* ReturnIfAbrupt(status). */
@ReturnIfAbrupt(status);
}

j := j + 1;
}
}

Expand Down
32 changes: 6 additions & 26 deletions JS-Interpreters/ecmaref6/section 19/section_19.4.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 @@ -157,9 +157,9 @@ function SymbolConstructor(global, this, NewTarget, strict, args) {
if (NewTarget != 'undefined)
throw TypeErrorConstructorInternal();
/* 2. If description is undefined, let descString be undefined. */
if (description == null || description == 'undefined)
descString := 'undefined;
else
if (description == null || description == 'undefined)
descString := 'undefined;
else
descString := ToString(description);
/* 3. ReturnIfAbrupt(descString). */
@ReturnIfAbrupt(descString);
Expand Down Expand Up @@ -343,23 +343,3 @@ function getIsConcatSpreadablePropertyName () {
function getHasInstancePropertyName() {
return getWellKnownSymbol("Symbol.hasInstance");
}

function filterSymbolProperties(list) {
newList := [];
len := l_len(list);
i := 0;
while (i < len) {
elem := l_nth (list, i);
if (!(checkSymbolProperties(elem)))
newList := l_add (newList, elem);
i := i + 1;
}
return newList;
}

function checkSymbolProperties(property) {
if (s_len_u(property) < 7)
return false;
res := s_substr_u (property, 0, 7);
return (res == "Symbol(");
}
Loading

0 comments on commit e5d8fa5

Please sign in to comment.