compiledEval.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Copyright (c) 2015 Faust Logic, Inc.
  4. // Copyright (c) 2021 TGEMIT Authors & Contributors
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. // IN THE SOFTWARE.
  23. //-----------------------------------------------------------------------------
  24. #include "platform/platform.h"
  25. #include "console/console.h"
  26. #include "console/ast.h"
  27. #include "core/tAlgorithm.h"
  28. #include "core/strings/findMatch.h"
  29. #include "core/strings/stringUnit.h"
  30. #include "console/consoleInternal.h"
  31. #include "core/stream/fileStream.h"
  32. #include "console/compiler.h"
  33. #include "console/simBase.h"
  34. #include "console/telnetDebugger.h"
  35. #include "sim/netStringTable.h"
  36. #include "console/ICallMethod.h"
  37. #include "console/stringStack.h"
  38. #include "util/messaging/message.h"
  39. #include "core/frameAllocator.h"
  40. #include "console/returnBuffer.h"
  41. #include "console/consoleValueStack.h"
  42. #ifndef TORQUE_TGB_ONLY
  43. #include "materials/materialDefinition.h"
  44. #include "materials/materialManager.h"
  45. #endif
  46. using namespace Compiler;
  47. enum EvalConstants
  48. {
  49. MaxStackSize = 1024,
  50. FieldBufferSizeString = 2048,
  51. FieldBufferSizeNumeric = 128,
  52. MethodOnComponent = -2
  53. };
  54. /// Frame data for a foreach/foreach$ loop.
  55. struct IterStackRecord
  56. {
  57. /// If true, this is a foreach$ loop; if not, it's a foreach loop.
  58. bool mIsStringIter;
  59. /// The iterator variable.
  60. Dictionary::Entry* mVariable;
  61. /// Information for an object iterator loop.
  62. struct ObjectPos
  63. {
  64. /// The set being iterated over.
  65. SimSet* mSet;
  66. /// Current index in the set.
  67. U32 mIndex;
  68. };
  69. /// Information for a string iterator loop.
  70. struct StringPos
  71. {
  72. /// The raw string data on the string stack.
  73. const char* mString;
  74. /// Current parsing position.
  75. U32 mIndex;
  76. };
  77. union
  78. {
  79. ObjectPos mObj;
  80. StringPos mStr;
  81. } mData;
  82. };
  83. ConsoleValueStack<4096> gCallStack;
  84. StringStack STR;
  85. U32 _FLT = 0; ///< Stack pointer for floatStack.
  86. U32 _UINT = 0; ///< Stack pointer for intStack.
  87. U32 _ITER = 0; ///< Stack pointer for iterStack.
  88. IterStackRecord iterStack[MaxStackSize];
  89. F64 floatStack[MaxStackSize];
  90. S64 intStack[MaxStackSize];
  91. char curFieldArray[256];
  92. char prevFieldArray[256];
  93. namespace Con
  94. {
  95. // Current script file name and root, these are registered as
  96. // console variables.
  97. extern StringTableEntry gCurrentFile;
  98. extern StringTableEntry gCurrentRoot;
  99. }
  100. namespace Con
  101. {
  102. const char *getNamespaceList(Namespace *ns)
  103. {
  104. U32 size = 1;
  105. Namespace * walk;
  106. for (walk = ns; walk; walk = walk->mParent)
  107. size += dStrlen(walk->mName) + 4;
  108. char *ret = Con::getReturnBuffer(size);
  109. ret[0] = 0;
  110. for (walk = ns; walk; walk = walk->mParent)
  111. {
  112. dStrcat(ret, walk->mName, size);
  113. if (walk->mParent)
  114. dStrcat(ret, " -> ", size);
  115. }
  116. return ret;
  117. }
  118. }
  119. // Gets a component of an object's field value or a variable and returns it
  120. // in val.
  121. static void getFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField, char val[])
  122. {
  123. const char* prevVal = NULL;
  124. // Grab value from object.
  125. if (object && field)
  126. prevVal = object->getDataField(field, array);
  127. // Otherwise, grab from the string stack. The value coming in will always
  128. // be a string because that is how multicomponent variables are handled.
  129. else
  130. prevVal = STR.getStringValue();
  131. // Make sure we got a value.
  132. if (prevVal && *prevVal)
  133. {
  134. static const StringTableEntry xyzw[] =
  135. {
  136. StringTable->insert("x"),
  137. StringTable->insert("y"),
  138. StringTable->insert("z"),
  139. StringTable->insert("w")
  140. };
  141. static const StringTableEntry rgba[] =
  142. {
  143. StringTable->insert("r"),
  144. StringTable->insert("g"),
  145. StringTable->insert("b"),
  146. StringTable->insert("a")
  147. };
  148. // Translate xyzw and rgba into the indexed component
  149. // of the variable or field.
  150. if (subField == xyzw[0] || subField == rgba[0])
  151. dStrcpy(val, StringUnit::getUnit(prevVal, 0, " \t\n"), 128);
  152. else if (subField == xyzw[1] || subField == rgba[1])
  153. dStrcpy(val, StringUnit::getUnit(prevVal, 1, " \t\n"), 128);
  154. else if (subField == xyzw[2] || subField == rgba[2])
  155. dStrcpy(val, StringUnit::getUnit(prevVal, 2, " \t\n"), 128);
  156. else if (subField == xyzw[3] || subField == rgba[3])
  157. dStrcpy(val, StringUnit::getUnit(prevVal, 3, " \t\n"), 128);
  158. else
  159. val[0] = 0;
  160. }
  161. else
  162. val[0] = 0;
  163. }
  164. // Sets a component of an object's field value based on the sub field. 'x' will
  165. // set the first field, 'y' the second, and 'z' the third.
  166. static void setFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField)
  167. {
  168. // Copy the current string value
  169. char strValue[1024];
  170. dStrncpy(strValue, STR.getStringValue(), 1024);
  171. char val[1024] = "";
  172. const char* prevVal = NULL;
  173. // Set the value on an object field.
  174. if (object && field)
  175. prevVal = object->getDataField(field, array);
  176. // Set the value on a variable.
  177. else if (gEvalState.currentVariable)
  178. prevVal = gEvalState.getStringVariable();
  179. // Ensure that the variable has a value
  180. if (!prevVal)
  181. return;
  182. static const StringTableEntry xyzw[] =
  183. {
  184. StringTable->insert("x"),
  185. StringTable->insert("y"),
  186. StringTable->insert("z"),
  187. StringTable->insert("w")
  188. };
  189. static const StringTableEntry rgba[] =
  190. {
  191. StringTable->insert("r"),
  192. StringTable->insert("g"),
  193. StringTable->insert("b"),
  194. StringTable->insert("a")
  195. };
  196. // Insert the value into the specified
  197. // component of the string.
  198. if (subField == xyzw[0] || subField == rgba[0])
  199. dStrcpy(val, StringUnit::setUnit(prevVal, 0, strValue, " \t\n"), 128);
  200. else if (subField == xyzw[1] || subField == rgba[1])
  201. dStrcpy(val, StringUnit::setUnit(prevVal, 1, strValue, " \t\n"), 128);
  202. else if (subField == xyzw[2] || subField == rgba[2])
  203. dStrcpy(val, StringUnit::setUnit(prevVal, 2, strValue, " \t\n"), 128);
  204. else if (subField == xyzw[3] || subField == rgba[3])
  205. dStrcpy(val, StringUnit::setUnit(prevVal, 3, strValue, " \t\n"), 128);
  206. if (val[0] != 0)
  207. {
  208. // Update the field or variable.
  209. if (object && field)
  210. object->setDataField(field, 0, val);
  211. else if (gEvalState.currentVariable)
  212. gEvalState.setStringVariable(val);
  213. }
  214. }
  215. //------------------------------------------------------------
  216. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  217. {
  218. F64 val = dAtof(str);
  219. if (val != 0)
  220. return val;
  221. else if (!dStricmp(str, "true"))
  222. return 1;
  223. else if (!dStricmp(str, "false"))
  224. return 0;
  225. else if (file)
  226. {
  227. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  228. return 0;
  229. }
  230. return 0;
  231. }
  232. //------------------------------------------------------------
  233. namespace Con
  234. {
  235. ReturnBuffer retBuffer;
  236. char *getReturnBuffer(U32 bufferSize)
  237. {
  238. return retBuffer.getBuffer(bufferSize);
  239. }
  240. char *getReturnBuffer(const char *stringToCopy)
  241. {
  242. U32 len = dStrlen(stringToCopy) + 1;
  243. char *ret = retBuffer.getBuffer(len);
  244. dMemcpy(ret, stringToCopy, len);
  245. return ret;
  246. }
  247. char* getReturnBuffer(const String& str)
  248. {
  249. const U32 size = str.size();
  250. char* ret = retBuffer.getBuffer(size);
  251. dMemcpy(ret, str.c_str(), size);
  252. return ret;
  253. }
  254. char* getReturnBuffer(const StringBuilder& str)
  255. {
  256. char* buffer = Con::getReturnBuffer(str.length() + 1);
  257. str.copy(buffer);
  258. buffer[str.length()] = '\0';
  259. return buffer;
  260. }
  261. char *getArgBuffer(U32 bufferSize)
  262. {
  263. return STR.getArgBuffer(bufferSize);
  264. }
  265. char *getFloatArg(F64 arg)
  266. {
  267. char *ret = STR.getArgBuffer(32);
  268. dSprintf(ret, 32, "%g", arg);
  269. return ret;
  270. }
  271. char *getIntArg(S32 arg)
  272. {
  273. char *ret = STR.getArgBuffer(32);
  274. dSprintf(ret, 32, "%d", arg);
  275. return ret;
  276. }
  277. char* getBoolArg(bool arg)
  278. {
  279. char *ret = STR.getArgBuffer(32);
  280. dSprintf(ret, 32, "%d", arg);
  281. return ret;
  282. }
  283. char *getStringArg(const char *arg)
  284. {
  285. U32 len = dStrlen(arg) + 1;
  286. char *ret = STR.getArgBuffer(len);
  287. dMemcpy(ret, arg, len);
  288. return ret;
  289. }
  290. char* getStringArg(const String& arg)
  291. {
  292. const U32 size = arg.size();
  293. char* ret = STR.getArgBuffer(size);
  294. dMemcpy(ret, arg.c_str(), size);
  295. return ret;
  296. }
  297. }
  298. //------------------------------------------------------------
  299. void ExprEvalState::setCurVarName(StringTableEntry name)
  300. {
  301. if (name[0] == '$')
  302. currentVariable = globalVars.lookup(name);
  303. else if (getStackDepth() > 0)
  304. currentVariable = getCurrentFrame().lookup(name);
  305. if (!currentVariable && gWarnUndefinedScriptVariables)
  306. Con::warnf(ConsoleLogEntry::Script, "Variable referenced before assignment: %s", name);
  307. }
  308. void ExprEvalState::setCurVarNameCreate(StringTableEntry name)
  309. {
  310. if (name[0] == '$')
  311. currentVariable = globalVars.add(name);
  312. else if (getStackDepth() > 0)
  313. currentVariable = getCurrentFrame().add(name);
  314. else
  315. {
  316. currentVariable = NULL;
  317. Con::warnf(ConsoleLogEntry::Script, "Accessing local variable in global scope... failed: %s", name);
  318. }
  319. }
  320. //------------------------------------------------------------
  321. S32 ExprEvalState::getIntVariable()
  322. {
  323. return currentVariable ? currentVariable->getIntValue() : 0;
  324. }
  325. F64 ExprEvalState::getFloatVariable()
  326. {
  327. return currentVariable ? currentVariable->getFloatValue() : 0;
  328. }
  329. const char *ExprEvalState::getStringVariable()
  330. {
  331. return currentVariable ? currentVariable->getStringValue() : "";
  332. }
  333. //------------------------------------------------------------
  334. void ExprEvalState::setIntVariable(S32 val)
  335. {
  336. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  337. currentVariable->setIntValue(val);
  338. }
  339. void ExprEvalState::setFloatVariable(F64 val)
  340. {
  341. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  342. currentVariable->setFloatValue(val);
  343. }
  344. void ExprEvalState::setStringVariable(const char *val)
  345. {
  346. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  347. currentVariable->setStringValue(val);
  348. }
  349. //-----------------------------------------------------------------------------
  350. U32 gExecCount = 0;
  351. ConsoleValue CodeBlock::exec(U32 ip, const char* functionName, Namespace* thisNamespace, U32 argc, ConsoleValue* argv, bool noCalls, StringTableEntry packageName, S32 setFrame)
  352. {
  353. #ifdef TORQUE_DEBUG
  354. U32 stackStart = STR.mStartStackSize;
  355. gExecCount++;
  356. #endif
  357. const dsize_t TRACE_BUFFER_SIZE = 1024;
  358. static char traceBuffer[TRACE_BUFFER_SIZE];
  359. U32 i;
  360. U32 iterDepth = 0;
  361. ConsoleValue returnValue;
  362. incRefCount();
  363. F64* curFloatTable;
  364. char* curStringTable;
  365. S32 curStringTableLen = 0; //clint to ensure we dont overwrite it
  366. STR.clearFunctionOffset();
  367. StringTableEntry thisFunctionName = NULL;
  368. bool popFrame = false;
  369. if (argv)
  370. {
  371. // assume this points into a function decl:
  372. U32 fnArgc = code[ip + 2 + 6];
  373. U32 regCount = code[ip + 2 + 7];
  374. thisFunctionName = CodeToSTE(code, ip);
  375. S32 wantedArgc = getMin(argc - 1, fnArgc); // argv[0] is func name
  376. if (gEvalState.traceOn)
  377. {
  378. traceBuffer[0] = 0;
  379. dStrcat(traceBuffer, "Entering ", TRACE_BUFFER_SIZE);
  380. if (packageName)
  381. {
  382. dStrcat(traceBuffer, "[", TRACE_BUFFER_SIZE);
  383. dStrcat(traceBuffer, packageName, TRACE_BUFFER_SIZE);
  384. dStrcat(traceBuffer, "]", TRACE_BUFFER_SIZE);
  385. }
  386. if (thisNamespace && thisNamespace->mName)
  387. {
  388. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  389. "%s::%s(", thisNamespace->mName, thisFunctionName);
  390. }
  391. else
  392. {
  393. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  394. "%s(", thisFunctionName);
  395. }
  396. for (i = 0; i < wantedArgc; i++)
  397. {
  398. dStrcat(traceBuffer, argv[i + 1].getString(), TRACE_BUFFER_SIZE);
  399. if (i != wantedArgc - 1)
  400. dStrcat(traceBuffer, ", ", TRACE_BUFFER_SIZE);
  401. }
  402. dStrcat(traceBuffer, ")", TRACE_BUFFER_SIZE);
  403. Con::printf("%s", traceBuffer);
  404. }
  405. gEvalState.pushFrame(thisFunctionName, thisNamespace, regCount);
  406. popFrame = true;
  407. for (i = 0; i < wantedArgc; i++)
  408. {
  409. S32 reg = code[ip + (2 + 6 + 1 + 1) + i];
  410. ConsoleValue& value = argv[i + 1];
  411. switch (value.getType())
  412. {
  413. case ConsoleValueType::cvString:
  414. gEvalState.setLocalStringVariable(reg, value.getString(), dStrlen(value.getString()));
  415. break;
  416. case ConsoleValueType::cvInteger:
  417. gEvalState.setLocalIntVariable(reg, value.getInt());
  418. break;
  419. case ConsoleValueType::cvFloat:
  420. gEvalState.setLocalFloatVariable(reg, value.getFloat());
  421. break;
  422. case ConsoleValueType::cvSTEntry:
  423. gEvalState.setLocalStringTableEntryVariable(reg, value.getString());
  424. break;
  425. default:
  426. AssertFatal(false, avar("Invalid local variable type. Type was: %i", value.getType()));
  427. }
  428. }
  429. ip = ip + fnArgc + (2 + 6 + 1 + 1);
  430. curFloatTable = functionFloats;
  431. curStringTable = functionStrings;
  432. curStringTableLen = functionStringsMaxLen;
  433. }
  434. else
  435. {
  436. curFloatTable = globalFloats;
  437. curStringTable = globalStrings;
  438. curStringTableLen = globalStringsMaxLen;
  439. // Do we want this code to execute using a new stack frame?
  440. if (setFrame < 0)
  441. {
  442. gEvalState.pushFrame(NULL, NULL, 0);
  443. gCallStack.pushFrame(0);
  444. popFrame = true;
  445. }
  446. else if (!gEvalState.stack.empty())
  447. {
  448. // We want to copy a reference to an existing stack frame
  449. // on to the top of the stack. Any change that occurs to
  450. // the locals during this new frame will also occur in the
  451. // original frame.
  452. S32 stackIndex = gEvalState.stack.size() - setFrame - 1;
  453. gEvalState.pushFrameRef(stackIndex);
  454. popFrame = true;
  455. }
  456. }
  457. // Grab the state of the telenet debugger here once
  458. // so that the push and pop frames are always balanced.
  459. const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
  460. if (telDebuggerOn && setFrame < 0)
  461. TelDebugger->pushStackFrame();
  462. StringTableEntry var, objParent;
  463. U32 failJump;
  464. StringTableEntry fnName;
  465. StringTableEntry fnNamespace, fnPackage;
  466. static const U32 objectCreationStackSize = 32;
  467. U32 objectCreationStackIndex = 0;
  468. struct {
  469. SimObject* newObject;
  470. U32 failJump;
  471. } objectCreationStack[objectCreationStackSize];
  472. SimObject* currentNewObject = 0;
  473. StringTableEntry prevField = NULL;
  474. StringTableEntry curField = NULL;
  475. SimObject* prevObject = NULL;
  476. SimObject* curObject = NULL;
  477. SimObject* saveObject = NULL;
  478. Namespace::Entry* nsEntry;
  479. Namespace* ns;
  480. const char* curFNDocBlock = NULL;
  481. const char* curNSDocBlock = NULL;
  482. const S32 nsDocLength = 128;
  483. char nsDocBlockClass[nsDocLength];
  484. S32 callArgc;
  485. ConsoleValue* callArgv;
  486. static char curFieldArray[256];
  487. static char prevFieldArray[256];
  488. CodeBlock* saveCodeBlock = smCurrentCodeBlock;
  489. smCurrentCodeBlock = this;
  490. if (this->name)
  491. {
  492. Con::gCurrentFile = this->name;
  493. Con::gCurrentRoot = this->modPath;
  494. }
  495. const char* val;
  496. S32 reg;
  497. // The frame temp is used by the variable accessor ops (OP_SAVEFIELD_* and
  498. // OP_LOADFIELD_*) to store temporary values for the fields.
  499. static S32 VAL_BUFFER_SIZE = 1024;
  500. FrameTemp<char> valBuffer(VAL_BUFFER_SIZE);
  501. for (;;)
  502. {
  503. U32 instruction = code[ip++];
  504. breakContinue:
  505. switch (instruction)
  506. {
  507. case OP_FUNC_DECL:
  508. if (!noCalls)
  509. {
  510. fnName = CodeToSTE(code, ip);
  511. fnNamespace = CodeToSTE(code, ip + 2);
  512. fnPackage = CodeToSTE(code, ip + 4);
  513. bool hasBody = (code[ip + 6] & 0x01) != 0;
  514. U32 lineNumber = code[ip + 6] >> 1;
  515. Namespace::unlinkPackages();
  516. if (fnNamespace == NULL && fnPackage == NULL)
  517. ns = Namespace::global();
  518. else
  519. ns = Namespace::find(fnNamespace, fnPackage);
  520. ns->addFunction(fnName, this, hasBody ? ip : 0);// if no body, set the IP to 0
  521. if (curNSDocBlock)
  522. {
  523. if (fnNamespace == StringTable->lookup(nsDocBlockClass))
  524. {
  525. char* usageStr = dStrdup(curNSDocBlock);
  526. usageStr[dStrlen(usageStr)] = '\0';
  527. ns->mUsage = usageStr;
  528. ns->mCleanUpUsage = true;
  529. curNSDocBlock = NULL;
  530. }
  531. }
  532. Namespace::relinkPackages();
  533. // If we had a docblock, it's definitely not valid anymore, so clear it out.
  534. curFNDocBlock = NULL;
  535. //Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
  536. }
  537. ip = code[ip + 7];
  538. break;
  539. case OP_CREATE_OBJECT:
  540. {
  541. // Read some useful info.
  542. objParent = CodeToSTE(code, ip);
  543. bool isDataBlock = code[ip + 2];
  544. bool isInternal = code[ip + 3];
  545. bool isSingleton = code[ip + 4];
  546. U32 lineNumber = code[ip + 5];
  547. failJump = code[ip + 6];
  548. // If we don't allow calls, we certainly don't allow creating objects!
  549. // Moved this to after failJump is set. Engine was crashing when
  550. // noCalls = true and an object was being created at the beginning of
  551. // a file. ADL.
  552. if (noCalls)
  553. {
  554. ip = failJump;
  555. break;
  556. }
  557. // Push the old info to the stack
  558. //Assert( objectCreationStackIndex < objectCreationStackSize );
  559. objectCreationStack[objectCreationStackIndex].newObject = currentNewObject;
  560. objectCreationStack[objectCreationStackIndex++].failJump = failJump;
  561. // Get the constructor information off the stack.
  562. gCallStack.argvc(NULL, callArgc, &callArgv);
  563. AssertFatal(callArgc - 3 >= 0, avar("Call Arg needs at least 3, only has %d", callArgc));
  564. const char* objectName = callArgv[2].getString();
  565. // Con::printf("Creating object...");
  566. // objectName = argv[1]...
  567. currentNewObject = NULL;
  568. // Are we creating a datablock? If so, deal with case where we override
  569. // an old one.
  570. if (isDataBlock)
  571. {
  572. // Con::printf(" - is a datablock");
  573. // Find the old one if any.
  574. SimObject* db = Sim::getDataBlockGroup()->findObject(objectName);
  575. // Make sure we're not changing types on ourselves...
  576. if (db && dStricmp(db->getClassName(), callArgv[1].getString()))
  577. {
  578. Con::errorf(ConsoleLogEntry::General, "Cannot re-declare data block %s with a different class.", objectName);
  579. ip = failJump;
  580. gCallStack.popFrame();
  581. break;
  582. }
  583. // If there was one, set the currentNewObject and move on.
  584. if (db)
  585. currentNewObject = db;
  586. }
  587. else if (!isInternal)
  588. {
  589. AbstractClassRep* rep = AbstractClassRep::findClassRep(objectName);
  590. if (rep != NULL)
  591. {
  592. Con::errorf(ConsoleLogEntry::General, "%s: Cannot name object [%s] the same name as a script class.",
  593. getFileLine(ip), objectName);
  594. ip = failJump;
  595. gCallStack.popFrame();
  596. break;
  597. }
  598. SimObject* obj = Sim::findObject((const char*)objectName);
  599. if (obj)
  600. {
  601. if (isSingleton)
  602. {
  603. // Make sure we're not trying to change types
  604. if (dStricmp(obj->getClassName(), callArgv[1].getString()) != 0)
  605. {
  606. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s] with a different class [%s] - was [%s].",
  607. getFileLine(ip), objectName, callArgv[1].getString(), obj->getClassName());
  608. ip = failJump;
  609. gCallStack.popFrame();
  610. break;
  611. }
  612. // We're creating a singleton, so use the found object instead of creating a new object.
  613. currentNewObject = obj;
  614. Con::warnf("%s: Singleton Object was already created with name %s. Using existing object.",
  615. getFileLine(ip), objectName);
  616. }
  617. }
  618. }
  619. gCallStack.popFrame();
  620. if (!currentNewObject)
  621. {
  622. // Well, looks like we have to create a new object.
  623. ConsoleObject* object = ConsoleObject::create(callArgv[1].getString());
  624. // Deal with failure!
  625. if (!object)
  626. {
  627. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", getFileLine(ip - 1), callArgv[1].getString());
  628. ip = failJump;
  629. break;
  630. }
  631. // Do special datablock init if appropros
  632. if (isDataBlock)
  633. {
  634. SimDataBlock* dataBlock = dynamic_cast<SimDataBlock*>(object);
  635. if (dataBlock)
  636. {
  637. dataBlock->assignId();
  638. }
  639. else
  640. {
  641. // They tried to make a non-datablock with a datablock keyword!
  642. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", getFileLine(ip - 1), callArgv[1].getString());
  643. // Clean up...
  644. delete object;
  645. ip = failJump;
  646. break;
  647. }
  648. }
  649. // Finally, set currentNewObject to point to the new one.
  650. currentNewObject = dynamic_cast<SimObject*>(object);
  651. // Deal with the case of a non-SimObject.
  652. if (!currentNewObject)
  653. {
  654. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", getFileLine(ip - 1), callArgv[1].getString());
  655. delete object;
  656. ip = failJump;
  657. break;
  658. }
  659. // Set the declaration line
  660. currentNewObject->setDeclarationLine(lineNumber);
  661. // Set the file that this object was created in
  662. currentNewObject->setFilename(this->name);
  663. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  664. if (*objParent)
  665. {
  666. // Find it!
  667. SimObject* parent;
  668. if (Sim::findObject(objParent, parent))
  669. {
  670. // Con::printf(" - Parent object found: %s", parent->getClassName());
  671. currentNewObject->setCopySource(parent);
  672. currentNewObject->assignFieldsFrom(parent);
  673. // copy any substitution statements
  674. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  675. if (parent_db)
  676. {
  677. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(currentNewObject);
  678. if (currentNewObject_db)
  679. currentNewObject_db->copySubstitutionsFrom(parent_db);
  680. }
  681. }
  682. else
  683. {
  684. Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", getFileLine(ip - 1), objParent, callArgv[1].getString());
  685. delete object;
  686. currentNewObject = NULL;
  687. ip = failJump;
  688. break;
  689. }
  690. }
  691. // If a name was passed, assign it.
  692. if (objectName[0])
  693. {
  694. if (!isInternal)
  695. currentNewObject->assignName(objectName);
  696. else
  697. currentNewObject->setInternalName(objectName);
  698. // Set the original name
  699. currentNewObject->setOriginalName( objectName );
  700. }
  701. // Do the constructor parameters.
  702. if (!currentNewObject->processArguments(callArgc - 3, callArgv + 3))
  703. {
  704. delete currentNewObject;
  705. currentNewObject = NULL;
  706. ip = failJump;
  707. break;
  708. }
  709. // If it's not a datablock, allow people to modify bits of it.
  710. if (!isDataBlock)
  711. {
  712. currentNewObject->setModStaticFields(true);
  713. currentNewObject->setModDynamicFields(true);
  714. }
  715. }
  716. else
  717. {
  718. currentNewObject->reloadReset(); // AFX (reload-reset)
  719. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  720. if (*objParent)
  721. {
  722. // Find it!
  723. SimObject* parent;
  724. if (Sim::findObject(objParent, parent))
  725. {
  726. // Con::printf(" - Parent object found: %s", parent->getClassName());
  727. // temporarily block name change
  728. SimObject::preventNameChanging = true;
  729. currentNewObject->setCopySource(parent);
  730. currentNewObject->assignFieldsFrom(parent);
  731. // restore name changing
  732. SimObject::preventNameChanging = false;
  733. // copy any substitution statements
  734. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  735. if (parent_db)
  736. {
  737. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(currentNewObject);
  738. if (currentNewObject_db)
  739. currentNewObject_db->copySubstitutionsFrom(parent_db);
  740. }
  741. }
  742. else
  743. {
  744. Con::errorf(ConsoleLogEntry::General, "%d: Unable to find parent object %s for %s.", lineNumber, objParent, callArgv[1].getString());
  745. }
  746. }
  747. }
  748. // Advance the IP past the create info...
  749. ip += 7;
  750. break;
  751. }
  752. case OP_ADD_OBJECT:
  753. {
  754. // See OP_SETCURVAR for why we do this.
  755. curFNDocBlock = NULL;
  756. curNSDocBlock = NULL;
  757. // Do we place this object at the root?
  758. bool placeAtRoot = code[ip++];
  759. // Con::printf("Adding object %s", currentNewObject->getName());
  760. // Make sure it wasn't already added, then add it.
  761. if (currentNewObject == NULL)
  762. {
  763. break;
  764. }
  765. bool isMessage = dynamic_cast<Message*>(currentNewObject) != NULL;
  766. if (currentNewObject->isProperlyAdded() == false)
  767. {
  768. bool ret = false;
  769. if (isMessage)
  770. {
  771. SimObjectId id = Message::getNextMessageID();
  772. if (id != 0xffffffff)
  773. ret = currentNewObject->registerObject(id);
  774. else
  775. Con::errorf("%s: No more object IDs available for messages", getFileLine(ip));
  776. }
  777. else
  778. ret = currentNewObject->registerObject();
  779. if (!ret)
  780. {
  781. // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
  782. Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", getFileLine(ip - 2), currentNewObject->getName(), currentNewObject->getClassName());
  783. delete currentNewObject;
  784. ip = failJump;
  785. break;
  786. }
  787. }
  788. // Are we dealing with a datablock?
  789. SimDataBlock* dataBlock = dynamic_cast<SimDataBlock*>(currentNewObject);
  790. String errorStr;
  791. // If so, preload it.
  792. if (dataBlock && !dataBlock->preload(true, errorStr))
  793. {
  794. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", getFileLine(ip - 2),
  795. currentNewObject->getName(), errorStr.c_str());
  796. dataBlock->deleteObject();
  797. ip = failJump;
  798. break;
  799. }
  800. // What group will we be added to, if any?
  801. U32 groupAddId = (U32)intStack[_UINT];
  802. SimGroup* grp = NULL;
  803. SimSet* set = NULL;
  804. if (!placeAtRoot || !currentNewObject->getGroup())
  805. {
  806. if (!isMessage)
  807. {
  808. if (!placeAtRoot)
  809. {
  810. // Otherwise just add to the requested group or set.
  811. if (!Sim::findObject(groupAddId, grp))
  812. Sim::findObject(groupAddId, set);
  813. }
  814. if (placeAtRoot)
  815. {
  816. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  817. if (Con::gInstantGroup.isEmpty() || !Sim::findObject(Con::gInstantGroup, grp))
  818. grp = Sim::getRootGroup();
  819. }
  820. }
  821. // If we didn't get a group, then make sure we have a pointer to
  822. // the rootgroup.
  823. if (!grp)
  824. grp = Sim::getRootGroup();
  825. // add to the parent group
  826. grp->addObject(currentNewObject);
  827. // If for some reason the add failed, add the object to the
  828. // root group so it won't leak.
  829. if (currentNewObject->getGroup() == NULL)
  830. Sim::getRootGroup()->addObject(currentNewObject);
  831. // add to any set we might be in
  832. if (set)
  833. set->addObject(currentNewObject);
  834. }
  835. // store the new object's ID on the stack (overwriting the group/set
  836. // id, if one was given, otherwise getting pushed)
  837. S32 id = currentNewObject->getId();
  838. if (placeAtRoot)
  839. intStack[_UINT] = id;
  840. else
  841. intStack[++_UINT] = id;
  842. break;
  843. }
  844. case OP_END_OBJECT:
  845. {
  846. // If we're not to be placed at the root, make sure we clean up
  847. // our group reference.
  848. bool placeAtRoot = code[ip++];
  849. if (!placeAtRoot)
  850. _UINT--;
  851. break;
  852. }
  853. case OP_FINISH_OBJECT:
  854. {
  855. if (currentNewObject)
  856. currentNewObject->onPostAdd();
  857. //Assert( objectCreationStackIndex >= 0 );
  858. // Restore the object info from the stack [7/9/2007 Black]
  859. currentNewObject = objectCreationStack[--objectCreationStackIndex].newObject;
  860. failJump = objectCreationStack[objectCreationStackIndex].failJump;
  861. break;
  862. }
  863. case OP_JMPIFFNOT:
  864. if (floatStack[_FLT--])
  865. {
  866. ip++;
  867. break;
  868. }
  869. ip = code[ip];
  870. break;
  871. case OP_JMPIFNOT:
  872. if (intStack[_UINT--])
  873. {
  874. ip++;
  875. break;
  876. }
  877. ip = code[ip];
  878. break;
  879. case OP_JMPIFF:
  880. if (!floatStack[_FLT--])
  881. {
  882. ip++;
  883. break;
  884. }
  885. ip = code[ip];
  886. break;
  887. case OP_JMPIF:
  888. if (!intStack[_UINT--])
  889. {
  890. ip++;
  891. break;
  892. }
  893. ip = code[ip];
  894. break;
  895. case OP_JMPIFNOT_NP:
  896. if (intStack[_UINT])
  897. {
  898. _UINT--;
  899. ip++;
  900. break;
  901. }
  902. ip = code[ip];
  903. break;
  904. case OP_JMPIF_NP:
  905. if (!intStack[_UINT])
  906. {
  907. _UINT--;
  908. ip++;
  909. break;
  910. }
  911. ip = code[ip];
  912. break;
  913. case OP_JMP:
  914. ip = code[ip];
  915. break;
  916. // This fixes a bug when not explicitly returning a value.
  917. case OP_RETURN_VOID:
  918. STR.setStringValue("");
  919. // We're falling thru here on purpose.
  920. case OP_RETURN:
  921. if (iterDepth > 0)
  922. {
  923. // Clear iterator state.
  924. while (iterDepth > 0)
  925. {
  926. iterStack[--_ITER].mIsStringIter = false;
  927. --iterDepth;
  928. }
  929. const char* retVal = STR.getStringValue();
  930. STR.rewind();
  931. returnValue.setString(retVal, STR.mLen);
  932. STR.setStringValue(retVal); // Not nice but works.
  933. }
  934. goto execFinished;
  935. case OP_RETURN_FLT:
  936. if (iterDepth > 0)
  937. {
  938. // Clear iterator state.
  939. while (iterDepth > 0)
  940. {
  941. iterStack[--_ITER].mIsStringIter = false;
  942. --iterDepth;
  943. }
  944. }
  945. returnValue.setFloat(floatStack[_FLT]);
  946. _FLT--;
  947. goto execFinished;
  948. case OP_RETURN_UINT:
  949. if (iterDepth > 0)
  950. {
  951. // Clear iterator state.
  952. while (iterDepth > 0)
  953. {
  954. iterStack[--_ITER].mIsStringIter = false;
  955. --iterDepth;
  956. }
  957. }
  958. returnValue.setInt(intStack[_UINT]);
  959. _UINT--;
  960. goto execFinished;
  961. case OP_CMPEQ:
  962. intStack[_UINT + 1] = bool(floatStack[_FLT] == floatStack[_FLT - 1]);
  963. _UINT++;
  964. _FLT -= 2;
  965. break;
  966. case OP_CMPGR:
  967. intStack[_UINT + 1] = bool(floatStack[_FLT] > floatStack[_FLT - 1]);
  968. _UINT++;
  969. _FLT -= 2;
  970. break;
  971. case OP_CMPGE:
  972. intStack[_UINT + 1] = bool(floatStack[_FLT] >= floatStack[_FLT - 1]);
  973. _UINT++;
  974. _FLT -= 2;
  975. break;
  976. case OP_CMPLT:
  977. intStack[_UINT + 1] = bool(floatStack[_FLT] < floatStack[_FLT - 1]);
  978. _UINT++;
  979. _FLT -= 2;
  980. break;
  981. case OP_CMPLE:
  982. intStack[_UINT + 1] = bool(floatStack[_FLT] <= floatStack[_FLT - 1]);
  983. _UINT++;
  984. _FLT -= 2;
  985. break;
  986. case OP_CMPNE:
  987. intStack[_UINT + 1] = bool(floatStack[_FLT] != floatStack[_FLT - 1]);
  988. _UINT++;
  989. _FLT -= 2;
  990. break;
  991. case OP_XOR:
  992. intStack[_UINT - 1] = intStack[_UINT] ^ intStack[_UINT - 1];
  993. _UINT--;
  994. break;
  995. case OP_MOD:
  996. if (intStack[_UINT - 1] != 0)
  997. intStack[_UINT - 1] = intStack[_UINT] % intStack[_UINT - 1];
  998. else
  999. intStack[_UINT - 1] = 0;
  1000. _UINT--;
  1001. break;
  1002. case OP_BITAND:
  1003. intStack[_UINT - 1] = intStack[_UINT] & intStack[_UINT - 1];
  1004. _UINT--;
  1005. break;
  1006. case OP_BITOR:
  1007. intStack[_UINT - 1] = intStack[_UINT] | intStack[_UINT - 1];
  1008. _UINT--;
  1009. break;
  1010. case OP_NOT:
  1011. intStack[_UINT] = !intStack[_UINT];
  1012. break;
  1013. case OP_NOTF:
  1014. intStack[_UINT + 1] = !floatStack[_FLT];
  1015. _FLT--;
  1016. _UINT++;
  1017. break;
  1018. case OP_ONESCOMPLEMENT:
  1019. intStack[_UINT] = ~intStack[_UINT];
  1020. break;
  1021. case OP_SHR:
  1022. intStack[_UINT - 1] = intStack[_UINT] >> intStack[_UINT - 1];
  1023. _UINT--;
  1024. break;
  1025. case OP_SHL:
  1026. intStack[_UINT - 1] = intStack[_UINT] << intStack[_UINT - 1];
  1027. _UINT--;
  1028. break;
  1029. case OP_AND:
  1030. intStack[_UINT - 1] = intStack[_UINT] && intStack[_UINT - 1];
  1031. _UINT--;
  1032. break;
  1033. case OP_OR:
  1034. intStack[_UINT - 1] = intStack[_UINT] || intStack[_UINT - 1];
  1035. _UINT--;
  1036. break;
  1037. case OP_ADD:
  1038. floatStack[_FLT - 1] = floatStack[_FLT] + floatStack[_FLT - 1];
  1039. _FLT--;
  1040. break;
  1041. case OP_SUB:
  1042. floatStack[_FLT - 1] = floatStack[_FLT] - floatStack[_FLT - 1];
  1043. _FLT--;
  1044. break;
  1045. case OP_MUL:
  1046. floatStack[_FLT - 1] = floatStack[_FLT] * floatStack[_FLT - 1];
  1047. _FLT--;
  1048. break;
  1049. case OP_DIV:
  1050. floatStack[_FLT - 1] = floatStack[_FLT] / floatStack[_FLT - 1];
  1051. _FLT--;
  1052. break;
  1053. case OP_NEG:
  1054. floatStack[_FLT] = -floatStack[_FLT];
  1055. break;
  1056. case OP_INC:
  1057. reg = code[ip++];
  1058. gEvalState.setLocalFloatVariable(reg, gEvalState.getLocalFloatVariable(reg) + 1.0);
  1059. break;
  1060. case OP_SETCURVAR:
  1061. var = CodeToSTE(code, ip);
  1062. ip += 2;
  1063. // If a variable is set, then these must be NULL. It is necessary
  1064. // to set this here so that the vector parser can appropriately
  1065. // identify whether it's dealing with a vector.
  1066. prevField = NULL;
  1067. prevObject = NULL;
  1068. curObject = NULL;
  1069. gEvalState.setCurVarName(var);
  1070. // In order to let docblocks work properly with variables, we have
  1071. // clear the current docblock when we do an assign. This way it
  1072. // won't inappropriately carry forward to following function decls.
  1073. curFNDocBlock = NULL;
  1074. curNSDocBlock = NULL;
  1075. break;
  1076. case OP_SETCURVAR_CREATE:
  1077. var = CodeToSTE(code, ip);
  1078. ip += 2;
  1079. // See OP_SETCURVAR
  1080. prevField = NULL;
  1081. prevObject = NULL;
  1082. curObject = NULL;
  1083. gEvalState.setCurVarNameCreate(var);
  1084. // See OP_SETCURVAR for why we do this.
  1085. curFNDocBlock = NULL;
  1086. curNSDocBlock = NULL;
  1087. break;
  1088. case OP_SETCURVAR_ARRAY:
  1089. var = STR.getSTValue();
  1090. // See OP_SETCURVAR
  1091. prevField = NULL;
  1092. prevObject = NULL;
  1093. curObject = NULL;
  1094. gEvalState.setCurVarName(var);
  1095. // See OP_SETCURVAR for why we do this.
  1096. curFNDocBlock = NULL;
  1097. curNSDocBlock = NULL;
  1098. break;
  1099. case OP_SETCURVAR_ARRAY_CREATE:
  1100. var = STR.getSTValue();
  1101. // See OP_SETCURVAR
  1102. prevField = NULL;
  1103. prevObject = NULL;
  1104. curObject = NULL;
  1105. gEvalState.setCurVarNameCreate(var);
  1106. // See OP_SETCURVAR for why we do this.
  1107. curFNDocBlock = NULL;
  1108. curNSDocBlock = NULL;
  1109. break;
  1110. case OP_LOADVAR_UINT:
  1111. intStack[_UINT + 1] = gEvalState.getIntVariable();
  1112. _UINT++;
  1113. break;
  1114. case OP_LOADVAR_FLT:
  1115. floatStack[_FLT + 1] = gEvalState.getFloatVariable();
  1116. _FLT++;
  1117. break;
  1118. case OP_LOADVAR_STR:
  1119. val = gEvalState.getStringVariable();
  1120. STR.setStringValue(val);
  1121. break;
  1122. case OP_SAVEVAR_UINT:
  1123. gEvalState.setIntVariable((S32)intStack[_UINT]);
  1124. break;
  1125. case OP_SAVEVAR_FLT:
  1126. gEvalState.setFloatVariable(floatStack[_FLT]);
  1127. break;
  1128. case OP_SAVEVAR_STR:
  1129. gEvalState.setStringVariable(STR.getStringValue());
  1130. break;
  1131. case OP_LOAD_LOCAL_VAR_UINT:
  1132. reg = code[ip++];
  1133. intStack[_UINT + 1] = gEvalState.getLocalIntVariable(reg);
  1134. _UINT++;
  1135. break;
  1136. case OP_LOAD_LOCAL_VAR_FLT:
  1137. reg = code[ip++];
  1138. floatStack[_FLT + 1] = gEvalState.getLocalFloatVariable(reg);
  1139. _FLT++;
  1140. break;
  1141. case OP_LOAD_LOCAL_VAR_STR:
  1142. reg = code[ip++];
  1143. val = gEvalState.getLocalStringVariable(reg);
  1144. STR.setStringValue(val);
  1145. break;
  1146. case OP_SAVE_LOCAL_VAR_UINT:
  1147. reg = code[ip++];
  1148. gEvalState.setLocalIntVariable(reg, (S32)intStack[_UINT]);
  1149. break;
  1150. case OP_SAVE_LOCAL_VAR_FLT:
  1151. reg = code[ip++];
  1152. gEvalState.setLocalFloatVariable(reg, floatStack[_FLT]);
  1153. break;
  1154. case OP_SAVE_LOCAL_VAR_STR:
  1155. reg = code[ip++];
  1156. gEvalState.setLocalStringVariable(reg, STR.getStringValue(), (S32)STR.mLen);
  1157. break;
  1158. case OP_SETCUROBJECT:
  1159. // Save the previous object for parsing vector fields.
  1160. prevObject = curObject;
  1161. val = STR.getStringValue();
  1162. // Sim::findObject will sometimes find valid objects from
  1163. // multi-component strings. This makes sure that doesn't
  1164. // happen.
  1165. for (const char* check = val; *check; check++)
  1166. {
  1167. if (*check == ' ')
  1168. {
  1169. val = "";
  1170. break;
  1171. }
  1172. }
  1173. curObject = Sim::findObject(val);
  1174. break;
  1175. case OP_SETCUROBJECT_INTERNAL:
  1176. ++ip; // To skip the recurse flag if the object wasnt found
  1177. if (curObject)
  1178. {
  1179. SimGroup* group = dynamic_cast<SimGroup*>(curObject);
  1180. if (group)
  1181. {
  1182. StringTableEntry intName = StringTable->insert(STR.getStringValue());
  1183. bool recurse = code[ip - 1];
  1184. SimObject* obj = group->findObjectByInternalName(intName, recurse);
  1185. intStack[_UINT + 1] = obj ? obj->getId() : 0;
  1186. _UINT++;
  1187. }
  1188. else
  1189. {
  1190. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-group %s of class %s.", getFileLine(ip - 2), curObject->getName(), curObject->getClassName());
  1191. intStack[_UINT] = 0;
  1192. }
  1193. }
  1194. break;
  1195. case OP_SETCUROBJECT_NEW:
  1196. curObject = currentNewObject;
  1197. break;
  1198. case OP_SETCURFIELD:
  1199. // Save the previous field for parsing vector fields.
  1200. prevField = curField;
  1201. dStrcpy(prevFieldArray, curFieldArray, 256);
  1202. curField = CodeToSTE(code, ip);
  1203. curFieldArray[0] = 0;
  1204. ip += 2;
  1205. break;
  1206. case OP_SETCURFIELD_ARRAY:
  1207. dStrcpy(curFieldArray, STR.getStringValue(), 256);
  1208. break;
  1209. case OP_SETCURFIELD_TYPE:
  1210. if(curObject)
  1211. curObject->setDataFieldType(code[ip], curField, curFieldArray);
  1212. ip++;
  1213. break;
  1214. case OP_LOADFIELD_UINT:
  1215. if (curObject)
  1216. intStack[_UINT + 1] = U32(dAtoi(curObject->getDataField(curField, curFieldArray)));
  1217. else
  1218. {
  1219. // The field is not being retrieved from an object. Maybe it's
  1220. // a special accessor?
  1221. char buff[FieldBufferSizeNumeric];
  1222. memset(buff, 0, sizeof(buff));
  1223. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1224. intStack[_UINT + 1] = dAtoi(buff);
  1225. }
  1226. _UINT++;
  1227. break;
  1228. case OP_LOADFIELD_FLT:
  1229. if (curObject)
  1230. floatStack[_FLT + 1] = dAtof(curObject->getDataField(curField, curFieldArray));
  1231. else
  1232. {
  1233. // The field is not being retrieved from an object. Maybe it's
  1234. // a special accessor?
  1235. char buff[FieldBufferSizeNumeric];
  1236. memset(buff, 0, sizeof(buff));
  1237. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1238. floatStack[_FLT + 1] = dAtof(buff);
  1239. }
  1240. _FLT++;
  1241. break;
  1242. case OP_LOADFIELD_STR:
  1243. if (curObject)
  1244. {
  1245. val = curObject->getDataField(curField, curFieldArray);
  1246. STR.setStringValue(val);
  1247. }
  1248. else
  1249. {
  1250. // The field is not being retrieved from an object. Maybe it's
  1251. // a special accessor?
  1252. char buff[FieldBufferSizeString];
  1253. memset(buff, 0, sizeof(buff));
  1254. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1255. STR.setStringValue(buff);
  1256. }
  1257. break;
  1258. case OP_SAVEFIELD_UINT:
  1259. STR.setIntValue((U32)intStack[_UINT]);
  1260. if (curObject)
  1261. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1262. else
  1263. {
  1264. // The field is not being set on an object. Maybe it's
  1265. // a special accessor?
  1266. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1267. prevObject = NULL;
  1268. }
  1269. break;
  1270. case OP_SAVEFIELD_FLT:
  1271. STR.setFloatValue(floatStack[_FLT]);
  1272. if (curObject)
  1273. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1274. else
  1275. {
  1276. // The field is not being set on an object. Maybe it's
  1277. // a special accessor?
  1278. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1279. prevObject = NULL;
  1280. }
  1281. break;
  1282. case OP_SAVEFIELD_STR:
  1283. if (curObject)
  1284. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1285. else
  1286. {
  1287. // The field is not being set on an object. Maybe it's
  1288. // a special accessor?
  1289. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1290. prevObject = NULL;
  1291. }
  1292. break;
  1293. case OP_STR_TO_UINT:
  1294. intStack[_UINT + 1] = STR.getIntValue();
  1295. _UINT++;
  1296. break;
  1297. case OP_STR_TO_FLT:
  1298. floatStack[_FLT + 1] = STR.getFloatValue();
  1299. _FLT++;
  1300. break;
  1301. case OP_STR_TO_NONE:
  1302. // This exists simply to deal with certain typecast situations.
  1303. break;
  1304. case OP_FLT_TO_UINT:
  1305. intStack[_UINT + 1] = (S64)floatStack[_FLT];
  1306. _FLT--;
  1307. _UINT++;
  1308. break;
  1309. case OP_FLT_TO_STR:
  1310. STR.setFloatValue(floatStack[_FLT]);
  1311. _FLT--;
  1312. break;
  1313. case OP_FLT_TO_NONE:
  1314. _FLT--;
  1315. break;
  1316. case OP_UINT_TO_FLT:
  1317. floatStack[_FLT + 1] = (F64)intStack[_UINT];
  1318. _UINT--;
  1319. _FLT++;
  1320. break;
  1321. case OP_UINT_TO_STR:
  1322. STR.setIntValue((U32)intStack[_UINT]);
  1323. _UINT--;
  1324. break;
  1325. case OP_UINT_TO_NONE:
  1326. _UINT--;
  1327. break;
  1328. case OP_LOADIMMED_UINT:
  1329. intStack[_UINT + 1] = code[ip++];
  1330. _UINT++;
  1331. break;
  1332. case OP_LOADIMMED_FLT:
  1333. floatStack[_FLT + 1] = curFloatTable[code[ip]];
  1334. ip++;
  1335. _FLT++;
  1336. break;
  1337. case OP_TAG_TO_STR:
  1338. code[ip - 1] = OP_LOADIMMED_STR;
  1339. // it's possible the string has already been converted
  1340. if (U8(curStringTable[code[ip]]) != StringTagPrefixByte)
  1341. {
  1342. U32 id = GameAddTaggedString(curStringTable + code[ip]);
  1343. dSprintf(curStringTable + code[ip] + 1, 7, "%d", id);
  1344. *(curStringTable + code[ip]) = StringTagPrefixByte;
  1345. }
  1346. case OP_LOADIMMED_STR:
  1347. STR.setStringValue(curStringTable + code[ip++]);
  1348. break;
  1349. case OP_DOCBLOCK_STR:
  1350. {
  1351. // If the first word of the doc is '\class' or '@class', then this
  1352. // is a namespace doc block, otherwise it is a function doc block.
  1353. const char* docblock = curStringTable + code[ip++];
  1354. const char* sansClass = dStrstr(docblock, "@class");
  1355. if (!sansClass)
  1356. sansClass = dStrstr(docblock, "\\class");
  1357. if (sansClass)
  1358. {
  1359. // Don't save the class declaration. Scan past the 'class'
  1360. // keyword and up to the first whitespace.
  1361. sansClass += 7;
  1362. S32 index = 0;
  1363. while ((*sansClass != ' ') && (*sansClass != '\n') && *sansClass && (index < (nsDocLength - 1)))
  1364. {
  1365. nsDocBlockClass[index++] = *sansClass;
  1366. sansClass++;
  1367. }
  1368. nsDocBlockClass[index] = '\0';
  1369. curNSDocBlock = sansClass + 1;
  1370. }
  1371. else
  1372. curFNDocBlock = docblock;
  1373. }
  1374. break;
  1375. case OP_LOADIMMED_IDENT:
  1376. STR.setStringValue(CodeToSTE(code, ip));
  1377. ip += 2;
  1378. break;
  1379. case OP_CALLFUNC:
  1380. {
  1381. // This routingId is set when we query the object as to whether
  1382. // it handles this method. It is set to an enum from the table
  1383. // above indicating whether it handles it on a component it owns
  1384. // or just on the object.
  1385. S32 routingId = 0;
  1386. fnName = CodeToSTE(code, ip);
  1387. fnNamespace = CodeToSTE(code, ip + 2);
  1388. U32 callType = code[ip + 4];
  1389. //if this is called from inside a function, append the ip and codeptr
  1390. if (!gEvalState.stack.empty())
  1391. {
  1392. gEvalState.stack.last()->code = this;
  1393. gEvalState.stack.last()->ip = ip - 1;
  1394. }
  1395. ip += 5;
  1396. gCallStack.argvc(fnName, callArgc, &callArgv);
  1397. if (callType == FuncCallExprNode::FunctionCall)
  1398. {
  1399. // Note: This works even if the function was in a package. Reason being is when
  1400. // activatePackage() is called, it swaps the namespaceEntry into the global namespace
  1401. // (and reverts it when deactivatePackage is called). Method or Static related ones work
  1402. // as expected, as the namespace is resolved on the fly.
  1403. nsEntry = Namespace::global()->lookup(fnName);
  1404. if (!nsEntry)
  1405. {
  1406. Con::warnf(ConsoleLogEntry::General,
  1407. "%s: Unable to find function %s",
  1408. getFileLine(ip - 4), fnName);
  1409. //STR.popFrame();
  1410. gCallStack.popFrame();
  1411. break;
  1412. }
  1413. }
  1414. else if (callType == FuncCallExprNode::StaticCall)
  1415. {
  1416. // Try to look it up.
  1417. ns = Namespace::find(fnNamespace);
  1418. nsEntry = ns->lookup(fnName);
  1419. if (!nsEntry)
  1420. {
  1421. Con::warnf(ConsoleLogEntry::General,
  1422. "%s: Unable to find function %s%s%s",
  1423. getFileLine(ip - 4), fnNamespace ? fnNamespace : "",
  1424. fnNamespace ? "::" : "", fnName);
  1425. //STR.popFrame();
  1426. gCallStack.popFrame();
  1427. break;
  1428. }
  1429. }
  1430. else if (callType == FuncCallExprNode::MethodCall)
  1431. {
  1432. saveObject = gEvalState.thisObject;
  1433. // Optimization: If we're an integer, we can lookup the value by SimObjectId
  1434. const ConsoleValue& simObjectLookupValue = callArgv[1];
  1435. if (simObjectLookupValue.getType() == ConsoleValueType::cvInteger)
  1436. gEvalState.thisObject = Sim::findObject(static_cast<SimObjectId>(simObjectLookupValue.getInt()));
  1437. else
  1438. gEvalState.thisObject = Sim::findObject(simObjectLookupValue.getString());
  1439. if (gEvalState.thisObject == NULL)
  1440. {
  1441. Con::warnf(
  1442. ConsoleLogEntry::General,
  1443. "%s: Unable to find object: '%s' attempting to call function '%s'",
  1444. getFileLine(ip - 6),
  1445. simObjectLookupValue.getString(),
  1446. fnName
  1447. );
  1448. //STR.popFrame();
  1449. gCallStack.popFrame();
  1450. STR.setStringValue("");
  1451. break;
  1452. }
  1453. ns = gEvalState.thisObject->getNamespace();
  1454. if (ns)
  1455. nsEntry = ns->lookup(fnName);
  1456. else
  1457. nsEntry = NULL;
  1458. }
  1459. else // it's a ParentCall
  1460. {
  1461. if (thisNamespace)
  1462. {
  1463. ns = thisNamespace->mParent;
  1464. if (ns)
  1465. nsEntry = ns->lookup(fnName);
  1466. else
  1467. nsEntry = NULL;
  1468. }
  1469. else
  1470. {
  1471. ns = NULL;
  1472. nsEntry = NULL;
  1473. }
  1474. }
  1475. if (!nsEntry || noCalls)
  1476. {
  1477. if (!noCalls)
  1478. {
  1479. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", getFileLine(ip - 4), fnName);
  1480. if (callType == FuncCallExprNode::MethodCall)
  1481. {
  1482. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1483. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1484. gEvalState.thisObject->getId(), Con::getNamespaceList(ns));
  1485. }
  1486. }
  1487. gCallStack.popFrame();
  1488. STR.setStringValue("");
  1489. STR.setStringValue("");
  1490. break;
  1491. }
  1492. if (nsEntry->mType == Namespace::Entry::ConsoleFunctionType)
  1493. {
  1494. if (nsEntry->mFunctionOffset)
  1495. {
  1496. const char* ret = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage).getString();
  1497. STR.setStringValue(ret);
  1498. }
  1499. else // no body
  1500. STR.setStringValue("");
  1501. gCallStack.popFrame();
  1502. }
  1503. else
  1504. {
  1505. if ((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1506. {
  1507. const char* nsName = ns ? ns->mName : "";
  1508. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments.", getFileLine(ip - 4), nsName, fnName);
  1509. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip - 4), nsEntry->mUsage);
  1510. gCallStack.popFrame();
  1511. STR.setStringValue("");
  1512. }
  1513. else
  1514. {
  1515. switch (nsEntry->mType)
  1516. {
  1517. case Namespace::Entry::StringCallbackType:
  1518. {
  1519. const char* ret = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1520. gCallStack.popFrame();
  1521. if (ret != STR.getStringValue())
  1522. STR.setStringValue(ret);
  1523. else
  1524. STR.setLen(dStrlen(ret));
  1525. break;
  1526. }
  1527. case Namespace::Entry::IntCallbackType:
  1528. {
  1529. S32 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1530. gCallStack.popFrame();
  1531. if (code[ip] == OP_STR_TO_UINT)
  1532. {
  1533. ip++;
  1534. intStack[++_UINT] = result;
  1535. break;
  1536. }
  1537. else if (code[ip] == OP_STR_TO_FLT)
  1538. {
  1539. ip++;
  1540. floatStack[++_FLT] = result;
  1541. break;
  1542. }
  1543. else if (code[ip] == OP_STR_TO_NONE)
  1544. ip++;
  1545. else
  1546. STR.setIntValue(result);
  1547. break;
  1548. }
  1549. case Namespace::Entry::FloatCallbackType:
  1550. {
  1551. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1552. gCallStack.popFrame();
  1553. if (code[ip] == OP_STR_TO_UINT)
  1554. {
  1555. ip++;
  1556. intStack[++_UINT] = (S64)result;
  1557. break;
  1558. }
  1559. else if (code[ip] == OP_STR_TO_FLT)
  1560. {
  1561. ip++;
  1562. floatStack[++_FLT] = result;
  1563. break;
  1564. }
  1565. else if (code[ip] == OP_STR_TO_NONE)
  1566. ip++;
  1567. else
  1568. STR.setFloatValue(result);
  1569. break;
  1570. }
  1571. case Namespace::Entry::VoidCallbackType:
  1572. {
  1573. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1574. if (code[ip] != OP_STR_TO_NONE)
  1575. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip - 4), fnName, functionName);
  1576. gCallStack.popFrame();
  1577. STR.setStringValue("");
  1578. break;
  1579. }
  1580. case Namespace::Entry::BoolCallbackType:
  1581. {
  1582. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1583. gCallStack.popFrame();
  1584. if (code[ip] == OP_STR_TO_UINT)
  1585. {
  1586. ip++;
  1587. intStack[++_UINT] = result;
  1588. break;
  1589. }
  1590. else if (code[ip] == OP_STR_TO_FLT)
  1591. {
  1592. ip++;
  1593. floatStack[++_FLT] = result;
  1594. break;
  1595. }
  1596. else if (code[ip] == OP_STR_TO_NONE)
  1597. ip++;
  1598. else
  1599. STR.setIntValue(result);
  1600. break;
  1601. }
  1602. }
  1603. }
  1604. }
  1605. if (callType == FuncCallExprNode::MethodCall)
  1606. gEvalState.thisObject = saveObject;
  1607. break;
  1608. }
  1609. case OP_ADVANCE_STR:
  1610. STR.advance();
  1611. break;
  1612. case OP_ADVANCE_STR_APPENDCHAR:
  1613. STR.advanceChar(code[ip++]);
  1614. break;
  1615. case OP_ADVANCE_STR_COMMA:
  1616. STR.advanceChar('_');
  1617. break;
  1618. case OP_ADVANCE_STR_NUL:
  1619. STR.advanceChar(0);
  1620. break;
  1621. case OP_REWIND_STR:
  1622. STR.rewind();
  1623. break;
  1624. case OP_TERMINATE_REWIND_STR:
  1625. STR.rewindTerminate();
  1626. break;
  1627. case OP_COMPARE_STR:
  1628. intStack[++_UINT] = STR.compare();
  1629. break;
  1630. case OP_PUSH:
  1631. gCallStack.pushString(STR.getStringValue(), STR.mLen);
  1632. break;
  1633. case OP_PUSH_UINT:
  1634. gCallStack.pushInt((U32)intStack[_UINT--]);
  1635. break;
  1636. case OP_PUSH_FLT:
  1637. gCallStack.pushFloat(floatStack[_FLT--]);
  1638. break;
  1639. case OP_PUSH_FRAME:
  1640. gCallStack.pushFrame(code[ip++]);
  1641. break;
  1642. case OP_ASSERT:
  1643. {
  1644. if (!intStack[_UINT--])
  1645. {
  1646. const char* message = curStringTable + code[ip];
  1647. U32 breakLine, inst;
  1648. findBreakLine(ip - 1, breakLine, inst);
  1649. if (PlatformAssert::processAssert(PlatformAssert::Fatal,
  1650. name ? name : "eval",
  1651. breakLine,
  1652. message))
  1653. {
  1654. if (TelDebugger && TelDebugger->isConnected() && breakLine > 0)
  1655. {
  1656. TelDebugger->breakProcess();
  1657. }
  1658. else
  1659. Platform::debugBreak();
  1660. }
  1661. }
  1662. ip++;
  1663. break;
  1664. }
  1665. case OP_BREAK:
  1666. {
  1667. //append the ip and codeptr before managing the breakpoint!
  1668. AssertFatal(!gEvalState.stack.empty(), "Empty eval stack on break!");
  1669. gEvalState.stack.last()->code = this;
  1670. gEvalState.stack.last()->ip = ip - 1;
  1671. U32 breakLine;
  1672. findBreakLine(ip - 1, breakLine, instruction);
  1673. if (!breakLine)
  1674. goto breakContinue;
  1675. TelDebugger->executionStopped(this, breakLine);
  1676. goto breakContinue;
  1677. }
  1678. case OP_ITER_BEGIN_STR:
  1679. {
  1680. iterStack[_ITER].mIsStringIter = true;
  1681. /* fallthrough */
  1682. }
  1683. case OP_ITER_BEGIN:
  1684. {
  1685. StringTableEntry varName = CodeToSTE(code, ip);
  1686. U32 failIp = code[ip + 2];
  1687. IterStackRecord& iter = iterStack[_ITER];
  1688. iter.mVariable = gEvalState.getCurrentFrame().add(varName);
  1689. if (iter.mIsStringIter)
  1690. {
  1691. iter.mData.mStr.mString = STR.getStringValue();
  1692. iter.mData.mStr.mIndex = 0;
  1693. }
  1694. else
  1695. {
  1696. // Look up the object.
  1697. SimSet* set;
  1698. if (!Sim::findObject(STR.getStringValue(), set))
  1699. {
  1700. Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", STR.getStringValue());
  1701. Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
  1702. ip = failIp;
  1703. continue;
  1704. }
  1705. // Set up.
  1706. iter.mData.mObj.mSet = set;
  1707. iter.mData.mObj.mIndex = 0;
  1708. }
  1709. _ITER++;
  1710. iterDepth++;
  1711. STR.push();
  1712. ip += 3;
  1713. break;
  1714. }
  1715. case OP_ITER:
  1716. {
  1717. U32 breakIp = code[ip];
  1718. IterStackRecord& iter = iterStack[_ITER - 1];
  1719. if (iter.mIsStringIter)
  1720. {
  1721. const char* str = iter.mData.mStr.mString;
  1722. U32 startIndex = iter.mData.mStr.mIndex;
  1723. U32 endIndex = startIndex;
  1724. // Break if at end.
  1725. if (!str[startIndex])
  1726. {
  1727. ip = breakIp;
  1728. continue;
  1729. }
  1730. // Find right end of current component.
  1731. if (!dIsspace(str[endIndex]))
  1732. do ++endIndex;
  1733. while (str[endIndex] && !dIsspace(str[endIndex]));
  1734. // Extract component.
  1735. if (endIndex != startIndex)
  1736. {
  1737. char savedChar = str[endIndex];
  1738. const_cast<char*>(str)[endIndex] = '\0'; // We are on the string stack so this is okay.
  1739. iter.mVariable->setStringValue(&str[startIndex]);
  1740. const_cast<char*>(str)[endIndex] = savedChar;
  1741. }
  1742. else
  1743. iter.mVariable->setStringValue("");
  1744. // Skip separator.
  1745. if (str[endIndex] != '\0')
  1746. ++endIndex;
  1747. iter.mData.mStr.mIndex = endIndex;
  1748. }
  1749. else
  1750. {
  1751. U32 index = iter.mData.mObj.mIndex;
  1752. SimSet* set = iter.mData.mObj.mSet;
  1753. if (index >= set->size())
  1754. {
  1755. ip = breakIp;
  1756. continue;
  1757. }
  1758. iter.mVariable->setIntValue(set->at(index)->getId());
  1759. iter.mData.mObj.mIndex = index + 1;
  1760. }
  1761. ++ip;
  1762. break;
  1763. }
  1764. case OP_ITER_END:
  1765. {
  1766. --_ITER;
  1767. --iterDepth;
  1768. STR.rewind();
  1769. iterStack[_ITER].mIsStringIter = false;
  1770. break;
  1771. }
  1772. case OP_INVALID:
  1773. default:
  1774. // error!
  1775. goto execFinished;
  1776. }
  1777. }
  1778. execFinished:
  1779. if (telDebuggerOn && setFrame < 0)
  1780. TelDebugger->popStackFrame();
  1781. if (popFrame)
  1782. {
  1783. gEvalState.popFrame();
  1784. }
  1785. if (argv)
  1786. {
  1787. if (gEvalState.traceOn)
  1788. {
  1789. traceBuffer[0] = 0;
  1790. dStrcat(traceBuffer, "Leaving ", TRACE_BUFFER_SIZE);
  1791. if (packageName)
  1792. {
  1793. dStrcat(traceBuffer, "[", TRACE_BUFFER_SIZE);
  1794. dStrcat(traceBuffer, packageName, TRACE_BUFFER_SIZE);
  1795. dStrcat(traceBuffer, "]", TRACE_BUFFER_SIZE);
  1796. }
  1797. if (thisNamespace && thisNamespace->mName)
  1798. {
  1799. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1800. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, returnValue.getString());
  1801. }
  1802. else
  1803. {
  1804. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1805. "%s() - return %s", thisFunctionName, returnValue.getString());
  1806. }
  1807. Con::printf("%s", traceBuffer);
  1808. }
  1809. }
  1810. else
  1811. {
  1812. delete[] const_cast<char*>(globalStrings);
  1813. delete[] globalFloats;
  1814. globalStrings = NULL;
  1815. globalFloats = NULL;
  1816. }
  1817. smCurrentCodeBlock = saveCodeBlock;
  1818. if (saveCodeBlock && saveCodeBlock->name)
  1819. {
  1820. Con::gCurrentFile = saveCodeBlock->name;
  1821. Con::gCurrentRoot = saveCodeBlock->modPath;
  1822. }
  1823. decRefCount();
  1824. #ifdef TORQUE_DEBUG
  1825. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  1826. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  1827. #endif
  1828. return std::move(returnValue);
  1829. }
  1830. //------------------------------------------------------------