summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-28 20:16:40 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-28 23:11:18 +0200
commit8ad11df89f888102f03c68c55844c79e107d2b2a (patch)
tree3d63eb83d25966f2f338b511efa707f44c427584 /Libraries
parent5eaa7ff4068dd92ecca51305827bf627f7b7f005 (diff)
downloadserenity-8ad11df89f888102f03c68c55844c79e107d2b2a.zip
LibJS: Handle exception in for loop test execution
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/AST.cpp5
-rw-r--r--Libraries/LibJS/Tests/for-head-errors.js36
2 files changed, 40 insertions, 1 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index 40bb117080..5169a55ec7 100644
--- a/Libraries/LibJS/AST.cpp
+++ b/Libraries/LibJS/AST.cpp
@@ -236,9 +236,12 @@ Value ForStatement::execute(Interpreter& interpreter) const
}
if (m_test) {
- while (m_test->execute(interpreter).to_boolean()) {
+ while (true) {
+ auto test_result = m_test->execute(interpreter);
if (interpreter.exception())
return {};
+ if (!test_result.to_boolean())
+ break;
last_value = interpreter.run(*m_body);
if (interpreter.exception())
return {};
diff --git a/Libraries/LibJS/Tests/for-head-errors.js b/Libraries/LibJS/Tests/for-head-errors.js
new file mode 100644
index 0000000000..20ed79c07e
--- /dev/null
+++ b/Libraries/LibJS/Tests/for-head-errors.js
@@ -0,0 +1,36 @@
+load("test-common.js");
+
+try {
+ assertThrowsError(() => {
+ for (var i = foo; i < 100; ++i) {
+ assertNotReached();
+ }
+ }, {
+ error: ReferenceError,
+ message: "'foo' not known"
+ });
+
+ assertThrowsError(() => {
+ for (var i = 0; i < foo; ++i) {
+ assertNotReached();
+ }
+ }, {
+ error: ReferenceError,
+ message: "'foo' not known"
+ });
+
+ var loopCount = 0;
+ assertThrowsError(() => {
+ for (var i = 0; i < 100; ++foo) {
+ loopCount++;
+ }
+ }, {
+ error: ReferenceError,
+ message: "'foo' not known"
+ });
+ assert(loopCount === 1);
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}