浏览代码

LibJS: Make SwitchStatement::execute() return undefined for empty blocks

Previously SwitchStatement::execute() would return <empty> when hitting
break, continue or empty consequent block. This was not in line with
the standard.
Marcin Gasperowicz 4 年之前
父节点
当前提交
624ceec04f
共有 1 个文件被更改,包括 7 次插入5 次删除
  1. 7 5
      Userland/Libraries/LibJS/AST.cpp

+ 7 - 5
Userland/Libraries/LibJS/AST.cpp

@@ -2075,6 +2075,7 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
         return {};
         return {};
 
 
     bool falling_through = false;
     bool falling_through = false;
+    auto last_value = js_undefined();
 
 
     for (auto& switch_case : m_cases) {
     for (auto& switch_case : m_cases) {
         if (!falling_through && switch_case.test()) {
         if (!falling_through && switch_case.test()) {
@@ -2087,24 +2088,25 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
         falling_through = true;
         falling_through = true;
 
 
         for (auto& statement : switch_case.consequent()) {
         for (auto& statement : switch_case.consequent()) {
-            auto last_value = statement.execute(interpreter, global_object);
+            auto value = statement.execute(interpreter, global_object);
+            if (!value.is_empty())
+                last_value = value;
             if (interpreter.exception())
             if (interpreter.exception())
                 return {};
                 return {};
             if (interpreter.vm().should_unwind()) {
             if (interpreter.vm().should_unwind()) {
                 if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
                 if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
                     // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
                     // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
-                    return {};
+                    return last_value;
                 } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
                 } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
                     interpreter.vm().stop_unwind();
                     interpreter.vm().stop_unwind();
-                    return {};
+                    return last_value;
                 } else {
                 } else {
                     return last_value;
                     return last_value;
                 }
                 }
             }
             }
         }
         }
     }
     }
-
-    return js_undefined();
+    return last_value;
 }
 }
 
 
 Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const
 Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const