Bläddra i källkod

Merge pull request #1289 from marauder2k9-torque/script-bug-ifstatmt-

if statements
Brian Roberts 1 år sedan
förälder
incheckning
62f3b93ff9

+ 1 - 0
Engine/source/console/console.h

@@ -246,6 +246,7 @@ public:
          return s == StringTable->EmptyString() ? 0 : dAtoi(s);
       if (type == ConsoleValueType::cvString)
          return dStrcmp(s, "") == 0 ? 0 : dAtoi(s);
+
       return dAtoi(getConsoleData());
    }
 

+ 13 - 3
Engine/source/console/torquescript/astNodes.cpp

@@ -200,7 +200,9 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
    U32 endifIp, elseIp;
    addBreakLine(codeStream);
 
-   if (testExpr->getPreferredType() == TypeReqUInt)
+   TypeReq testType = testExpr->getPreferredType();
+
+   if (testType == TypeReqUInt)
    {
       integer = true;
    }
@@ -209,8 +211,16 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
       integer = false;
    }
 
-   ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
-   codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT);
+   if (testType == TypeReqString || testType == TypeReqNone)
+   {
+      ip = testExpr->compile(codeStream, ip, TypeReqString);
+      codeStream.emit(OP_JMPNOTSTRING);
+   }
+   else
+   {
+      ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
+      codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT);
+   }
 
    if (elseBlock)
    {

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

@@ -1144,6 +1144,15 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
             ip++;
             break;
          }
+
+         ip = code[ip];
+         break;
+      case OP_JMPNOTSTRING:
+         if (stack[_STK--].getBool())
+         {
+            ip++;
+            break;
+         }
          ip = code[ip];
          break;
       case OP_JMPIFF:

+ 1 - 0
Engine/source/console/torquescript/compiler.h

@@ -62,6 +62,7 @@ namespace Compiler
 
       OP_JMPIFFNOT,
       OP_JMPIFNOT,
+      OP_JMPNOTSTRING,
       OP_JMPIFF,
       OP_JMPIF,
       OP_JMPIFNOT_NP,

+ 17 - 3
Engine/source/core/strings/stringFunctions.h

@@ -259,9 +259,23 @@ extern S32        dStrcmp(const UTF16 *str1, const UTF16 *str2);
 extern S32        dStrnatcmp( const char* str1, const char* str2 );
 extern S32        dStrnatcasecmp( const char* str1, const char* str2 );
 
-inline bool dAtob(const char *str)
-{
-   return !dStricmp(str, "true") || dAtof(str);
+inline bool dAtob(const char* str)
+{
+   if (str && str[0] != '\0')
+   {
+      if (dStricmp(str, "0") == 0)
+         return false;
+      if (dStricmp(str, "0.0") == 0)
+         return false;
+      if (dStricmp(str, "0.0f") == 0)
+         return false;
+      if (dStricmp(str, "null") == 0)
+         return false;
+      if (dStricmp(str, "false") == 0)
+         return false;
+      return true;
+   }
+   return false;
 }
 
 bool dStrEqual(const char* str1, const char* str2);