compiledEval.cpp
Engine/source/console/compiledEval.cpp
Namespaces:
namespace
This namespace contains the core of the console functionality.
Public Functions
consoleStringToNumber(const char * str, StringTableEntry file, U32 line)
Detailed Description
Public Functions
consoleStringToNumber(const char * str, StringTableEntry file, U32 line)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "console/console.h" 26 27#include "console/ast.h" 28#include "core/tAlgorithm.h" 29 30#include "core/strings/findMatch.h" 31#include "core/strings/stringUnit.h" 32#include "console/consoleInternal.h" 33#include "core/stream/fileStream.h" 34#include "console/compiler.h" 35 36#include "console/simBase.h" 37#include "console/telnetDebugger.h" 38#include "sim/netStringTable.h" 39#include "console/ICallMethod.h" 40#include "console/stringStack.h" 41#include "util/messaging/message.h" 42#include "core/frameAllocator.h" 43 44#include "console/codeInterpreter.h" 45#include "console/returnBuffer.h" 46 47#ifndef TORQUE_TGB_ONLY 48#include "materials/materialDefinition.h" 49#include "materials/materialManager.h" 50#endif 51 52using namespace Compiler; 53 54namespace Con 55{ 56 // Current script file name and root, these are registered as 57 // console variables. 58 extern StringTableEntry gCurrentFile; 59 extern StringTableEntry gCurrentRoot; 60 extern S32 gObjectCopyFailures; 61} 62 63namespace Con 64{ 65 const char *getNamespaceList(Namespace *ns) 66 { 67 U32 size = 1; 68 Namespace * walk; 69 for (walk = ns; walk; walk = walk->mParent) 70 size += dStrlen(walk->mName) + 4; 71 char *ret = Con::getReturnBuffer(size); 72 ret[0] = 0; 73 for (walk = ns; walk; walk = walk->mParent) 74 { 75 dStrcat(ret, walk->mName, size); 76 if (walk->mParent) 77 dStrcat(ret, " -> ", size); 78 } 79 return ret; 80 } 81} 82 83//------------------------------------------------------------ 84 85F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line) 86{ 87 F64 val = dAtof(str); 88 if (val != 0) 89 return val; 90 else if (!dStricmp(str, "true")) 91 return 1; 92 else if (!dStricmp(str, "false")) 93 return 0; 94 else if (file) 95 { 96 Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line); 97 return 0; 98 } 99 return 0; 100} 101 102//------------------------------------------------------------ 103 104namespace Con 105{ 106 ReturnBuffer retBuffer; 107 108 char *getReturnBuffer(U32 bufferSize) 109 { 110 return retBuffer.getBuffer(bufferSize); 111 } 112 113 char *getReturnBuffer(const char *stringToCopy) 114 { 115 U32 len = dStrlen(stringToCopy) + 1; 116 char *ret = retBuffer.getBuffer(len); 117 dMemcpy(ret, stringToCopy, len); 118 return ret; 119 } 120 121 char* getReturnBuffer(const String& str) 122 { 123 const U32 size = str.size(); 124 char* ret = retBuffer.getBuffer(size); 125 dMemcpy(ret, str.c_str(), size); 126 return ret; 127 } 128 129 char* getReturnBuffer(const StringBuilder& str) 130 { 131 char* buffer = Con::getReturnBuffer(str.length() + 1); 132 str.copy(buffer); 133 buffer[str.length()] = '\0'; 134 135 return buffer; 136 } 137 138 char *getArgBuffer(U32 bufferSize) 139 { 140 return STR.getArgBuffer(bufferSize); 141 } 142 143 char *getFloatArg(F64 arg) 144 { 145 char *ret = STR.getArgBuffer(32); 146 dSprintf(ret, 32, "%g", arg); 147 return ret; 148 } 149 150 char *getIntArg(S32 arg) 151 { 152 char *ret = STR.getArgBuffer(32); 153 dSprintf(ret, 32, "%d", arg); 154 return ret; 155 } 156 157 char* getBoolArg(bool arg) 158 { 159 char *ret = STR.getArgBuffer(32); 160 dSprintf(ret, 32, "%d", arg); 161 return ret; 162 } 163 164 char *getStringArg(const char *arg) 165 { 166 U32 len = dStrlen(arg) + 1; 167 char *ret = STR.getArgBuffer(len); 168 dMemcpy(ret, arg, len); 169 return ret; 170 } 171 172 char* getStringArg(const String& arg) 173 { 174 const U32 size = arg.size(); 175 char* ret = STR.getArgBuffer(size); 176 dMemcpy(ret, arg.c_str(), size); 177 return ret; 178 } 179} 180 181//------------------------------------------------------------ 182 183void ExprEvalState::setCurVarName(StringTableEntry name) 184{ 185 if (name[0] == '$') 186 currentVariable = globalVars.lookup(name); 187 else if (getStackDepth() > 0) 188 currentVariable = getCurrentFrame().lookup(name); 189 if (!currentVariable && gWarnUndefinedScriptVariables) 190 Con::warnf(ConsoleLogEntry::Script, "Variable referenced before assignment: %s", name); 191} 192 193void ExprEvalState::setCurVarNameCreate(StringTableEntry name) 194{ 195 if (name[0] == '$') 196 currentVariable = globalVars.add(name); 197 else if (getStackDepth() > 0) 198 currentVariable = getCurrentFrame().add(name); 199 else 200 { 201 currentVariable = NULL; 202 Con::warnf(ConsoleLogEntry::Script, "Accessing local variable in global scope... failed: %s", name); 203 } 204} 205 206//------------------------------------------------------------ 207 208S32 ExprEvalState::getIntVariable() 209{ 210 return currentVariable ? currentVariable->getIntValue() : 0; 211} 212 213F64 ExprEvalState::getFloatVariable() 214{ 215 return currentVariable ? currentVariable->getFloatValue() : 0; 216} 217 218const char *ExprEvalState::getStringVariable() 219{ 220 return currentVariable ? currentVariable->getStringValue() : ""; 221} 222 223//------------------------------------------------------------ 224 225void ExprEvalState::setIntVariable(S32 val) 226{ 227 AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!"); 228 currentVariable->setIntValue(val); 229} 230 231void ExprEvalState::setFloatVariable(F64 val) 232{ 233 AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!"); 234 currentVariable->setFloatValue(val); 235} 236 237void ExprEvalState::setStringVariable(const char *val) 238{ 239 AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!"); 240 currentVariable->setStringValue(val); 241} 242 243void ExprEvalState::setStringStackPtrVariable(StringStackPtr str) 244{ 245 AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!"); 246 currentVariable->setStringStackPtrValue(str); 247} 248 249void ExprEvalState::setCopyVariable() 250{ 251 if (copyVariable) 252 { 253 switch (copyVariable->value.type) 254 { 255 case ConsoleValue::TypeInternalInt: 256 currentVariable->setIntValue(copyVariable->getIntValue()); 257 break; 258 case ConsoleValue::TypeInternalFloat: 259 currentVariable->setFloatValue(copyVariable->getFloatValue()); 260 break; 261 default: 262 currentVariable->setStringValue(copyVariable->getStringValue()); 263 break; 264 } 265 } 266} 267 268//------------------------------------------------------------ 269 270 271ConsoleValueRef CodeBlock::exec(U32 ip, const char *functionName, Namespace *thisNamespace, U32 argc, ConsoleValueRef *argv, bool noCalls, StringTableEntry packageName, S32 setFrame) 272{ 273 CodeInterpreter interpreter(this); 274 return interpreter.exec(ip, functionName, thisNamespace, argc, argv, noCalls, packageName, setFrame); 275} 276 277//------------------------------------------------------------ 278