浏览代码

Fix foreach over non-existant objects

Lukas Aldershaab 2 年之前
父节点
当前提交
c6047b24eb
共有 2 个文件被更改,包括 41 次插入0 次删除
  1. 2 0
      Engine/source/console/torquescript/compiledEval.cpp
  2. 39 0
      Engine/source/testing/ScriptTest.cpp

+ 2 - 0
Engine/source/console/torquescript/compiledEval.cpp

@@ -2121,6 +2121,8 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
                Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", stack[_STK].getString());
                Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
                ip = failIp;
+               // Pop the iterated value
+               _STK--;
                continue;
             }
 

+ 39 - 0
Engine/source/testing/ScriptTest.cpp

@@ -540,6 +540,45 @@ TEST_F(ScriptTest, ForEachLoop)
    )");
 
    ASSERT_EQ(forEachNestedReturn.getInt(), 42);
+
+
+   ConsoleValue forEachNonExistantObject = RunScript(R"(
+         $counter = 0;
+         foreach ($obj in NonExistantSimSet)
+         {
+            $counter++;
+         }
+
+         return $counter;
+   )");
+
+   ASSERT_EQ(forEachNonExistantObject.getInt(), 0);
+
+
+   ConsoleValue forEachOnZero = RunScript(R"(
+         $counter = 0;
+         foreach ($obj in 0)
+         {
+            $counter++;
+         }
+
+         return $counter;
+   )");
+
+   ASSERT_EQ(forEachOnZero.getInt(), 0);
+
+
+   ConsoleValue forEachOnEmptyString = RunScript(R"(
+         $counter = 0;
+         foreach ($obj in "")
+         {
+            $counter++;
+         }
+
+         return $counter;
+   )");
+
+   ASSERT_EQ(forEachOnEmptyString.getInt(), 0);
 }
 
 TEST_F(ScriptTest, TorqueScript_Array_Testing)