compiledEval.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  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. // If requested stack frame isn't available, request a new one
  440. // (this prevents assert failures when creating local
  441. // variables without a stack frame)
  442. if (gEvalState.getStackDepth() <= setFrame)
  443. setFrame = -1;
  444. // Do we want this code to execute using a new stack frame?
  445. if (setFrame < 0)
  446. {
  447. gEvalState.pushFrame(NULL, NULL, 0);
  448. gCallStack.pushFrame(0);
  449. popFrame = true;
  450. }
  451. else
  452. {
  453. // We want to copy a reference to an existing stack frame
  454. // on to the top of the stack. Any change that occurs to
  455. // the locals during this new frame will also occur in the
  456. // original frame.
  457. S32 stackIndex = gEvalState.getTopOfStack() - setFrame - 1;
  458. gEvalState.pushFrameRef(stackIndex);
  459. popFrame = true;
  460. }
  461. }
  462. // Grab the state of the telenet debugger here once
  463. // so that the push and pop frames are always balanced.
  464. const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
  465. if (telDebuggerOn && setFrame < 0)
  466. TelDebugger->pushStackFrame();
  467. StringTableEntry var, objParent;
  468. U32 failJump;
  469. StringTableEntry fnName;
  470. StringTableEntry fnNamespace, fnPackage;
  471. static const U32 objectCreationStackSize = 32;
  472. U32 objectCreationStackIndex = 0;
  473. struct {
  474. SimObject* newObject;
  475. U32 failJump;
  476. } objectCreationStack[objectCreationStackSize];
  477. SimObject* currentNewObject = 0;
  478. StringTableEntry prevField = NULL;
  479. StringTableEntry curField = NULL;
  480. SimObject* prevObject = NULL;
  481. SimObject* curObject = NULL;
  482. SimObject* saveObject = NULL;
  483. Namespace::Entry* nsEntry;
  484. Namespace* ns;
  485. const char* curFNDocBlock = NULL;
  486. const char* curNSDocBlock = NULL;
  487. const S32 nsDocLength = 128;
  488. char nsDocBlockClass[nsDocLength];
  489. S32 callArgc;
  490. ConsoleValue* callArgv;
  491. static char curFieldArray[256];
  492. static char prevFieldArray[256];
  493. CodeBlock* saveCodeBlock = smCurrentCodeBlock;
  494. smCurrentCodeBlock = this;
  495. if (this->name)
  496. {
  497. Con::gCurrentFile = this->name;
  498. Con::gCurrentRoot = this->modPath;
  499. }
  500. const char* val;
  501. S32 reg;
  502. // The frame temp is used by the variable accessor ops (OP_SAVEFIELD_* and
  503. // OP_LOADFIELD_*) to store temporary values for the fields.
  504. static S32 VAL_BUFFER_SIZE = 1024;
  505. FrameTemp<char> valBuffer(VAL_BUFFER_SIZE);
  506. for (;;)
  507. {
  508. U32 instruction = code[ip++];
  509. breakContinue:
  510. switch (instruction)
  511. {
  512. case OP_FUNC_DECL:
  513. if (!noCalls)
  514. {
  515. fnName = CodeToSTE(code, ip);
  516. fnNamespace = CodeToSTE(code, ip + 2);
  517. fnPackage = CodeToSTE(code, ip + 4);
  518. bool hasBody = (code[ip + 6] & 0x01) != 0;
  519. U32 lineNumber = code[ip + 6] >> 1;
  520. Namespace::unlinkPackages();
  521. if (fnNamespace == NULL && fnPackage == NULL)
  522. ns = Namespace::global();
  523. else
  524. ns = Namespace::find(fnNamespace, fnPackage);
  525. ns->addFunction(fnName, this, hasBody ? ip : 0);// if no body, set the IP to 0
  526. if (curNSDocBlock)
  527. {
  528. if (fnNamespace == StringTable->lookup(nsDocBlockClass))
  529. {
  530. char* usageStr = dStrdup(curNSDocBlock);
  531. usageStr[dStrlen(usageStr)] = '\0';
  532. ns->mUsage = usageStr;
  533. ns->mCleanUpUsage = true;
  534. curNSDocBlock = NULL;
  535. }
  536. }
  537. Namespace::relinkPackages();
  538. // If we had a docblock, it's definitely not valid anymore, so clear it out.
  539. curFNDocBlock = NULL;
  540. //Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
  541. }
  542. ip = code[ip + 7];
  543. break;
  544. case OP_CREATE_OBJECT:
  545. {
  546. // Read some useful info.
  547. objParent = CodeToSTE(code, ip);
  548. bool isDataBlock = code[ip + 2];
  549. bool isInternal = code[ip + 3];
  550. bool isSingleton = code[ip + 4];
  551. U32 lineNumber = code[ip + 5];
  552. failJump = code[ip + 6];
  553. // If we don't allow calls, we certainly don't allow creating objects!
  554. // Moved this to after failJump is set. Engine was crashing when
  555. // noCalls = true and an object was being created at the beginning of
  556. // a file. ADL.
  557. if (noCalls)
  558. {
  559. ip = failJump;
  560. break;
  561. }
  562. // Push the old info to the stack
  563. //Assert( objectCreationStackIndex < objectCreationStackSize );
  564. objectCreationStack[objectCreationStackIndex].newObject = currentNewObject;
  565. objectCreationStack[objectCreationStackIndex++].failJump = failJump;
  566. // Get the constructor information off the stack.
  567. gCallStack.argvc(NULL, callArgc, &callArgv);
  568. AssertFatal(callArgc - 3 >= 0, avar("Call Arg needs at least 3, only has %d", callArgc));
  569. const char* objectName = callArgv[2].getString();
  570. // Con::printf("Creating object...");
  571. // objectName = argv[1]...
  572. currentNewObject = NULL;
  573. // Are we creating a datablock? If so, deal with case where we override
  574. // an old one.
  575. if (isDataBlock)
  576. {
  577. // Con::printf(" - is a datablock");
  578. // Find the old one if any.
  579. SimObject* db = Sim::getDataBlockGroup()->findObject(objectName);
  580. // Make sure we're not changing types on ourselves...
  581. if (db && dStricmp(db->getClassName(), callArgv[1].getString()))
  582. {
  583. Con::errorf(ConsoleLogEntry::General, "Cannot re-declare data block %s with a different class.", objectName);
  584. ip = failJump;
  585. gCallStack.popFrame();
  586. break;
  587. }
  588. // If there was one, set the currentNewObject and move on.
  589. if (db)
  590. currentNewObject = db;
  591. }
  592. else if (!isInternal)
  593. {
  594. AbstractClassRep* rep = AbstractClassRep::findClassRep(objectName);
  595. if (rep != NULL)
  596. {
  597. Con::errorf(ConsoleLogEntry::General, "%s: Cannot name object [%s] the same name as a script class.",
  598. getFileLine(ip), objectName);
  599. ip = failJump;
  600. gCallStack.popFrame();
  601. break;
  602. }
  603. SimObject* obj = Sim::findObject((const char*)objectName);
  604. if (obj)
  605. {
  606. if (isSingleton)
  607. {
  608. // Make sure we're not trying to change types
  609. if (dStricmp(obj->getClassName(), callArgv[1].getString()) != 0)
  610. {
  611. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s] with a different class [%s] - was [%s].",
  612. getFileLine(ip), objectName, callArgv[1].getString(), obj->getClassName());
  613. ip = failJump;
  614. gCallStack.popFrame();
  615. break;
  616. }
  617. // We're creating a singleton, so use the found object instead of creating a new object.
  618. currentNewObject = obj;
  619. Con::warnf("%s: Singleton Object was already created with name %s. Using existing object.",
  620. getFileLine(ip), objectName);
  621. }
  622. }
  623. }
  624. gCallStack.popFrame();
  625. if (!currentNewObject)
  626. {
  627. // Well, looks like we have to create a new object.
  628. ConsoleObject* object = ConsoleObject::create(callArgv[1].getString());
  629. // Deal with failure!
  630. if (!object)
  631. {
  632. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", getFileLine(ip - 1), callArgv[1].getString());
  633. ip = failJump;
  634. break;
  635. }
  636. // Do special datablock init if appropros
  637. if (isDataBlock)
  638. {
  639. SimDataBlock* dataBlock = dynamic_cast<SimDataBlock*>(object);
  640. if (dataBlock)
  641. {
  642. dataBlock->assignId();
  643. }
  644. else
  645. {
  646. // They tried to make a non-datablock with a datablock keyword!
  647. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", getFileLine(ip - 1), callArgv[1].getString());
  648. // Clean up...
  649. delete object;
  650. ip = failJump;
  651. break;
  652. }
  653. }
  654. // Finally, set currentNewObject to point to the new one.
  655. currentNewObject = dynamic_cast<SimObject*>(object);
  656. // Deal with the case of a non-SimObject.
  657. if (!currentNewObject)
  658. {
  659. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", getFileLine(ip - 1), callArgv[1].getString());
  660. delete object;
  661. ip = failJump;
  662. break;
  663. }
  664. // Set the declaration line
  665. currentNewObject->setDeclarationLine(lineNumber);
  666. // Set the file that this object was created in
  667. currentNewObject->setFilename(this->name);
  668. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  669. if (*objParent)
  670. {
  671. // Find it!
  672. SimObject* parent;
  673. if (Sim::findObject(objParent, parent))
  674. {
  675. // Con::printf(" - Parent object found: %s", parent->getClassName());
  676. currentNewObject->setCopySource(parent);
  677. currentNewObject->assignFieldsFrom(parent);
  678. // copy any substitution statements
  679. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  680. if (parent_db)
  681. {
  682. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(currentNewObject);
  683. if (currentNewObject_db)
  684. currentNewObject_db->copySubstitutionsFrom(parent_db);
  685. }
  686. }
  687. else
  688. {
  689. Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", getFileLine(ip - 1), objParent, callArgv[1].getString());
  690. delete object;
  691. currentNewObject = NULL;
  692. ip = failJump;
  693. break;
  694. }
  695. }
  696. // If a name was passed, assign it.
  697. if (objectName[0])
  698. {
  699. if (!isInternal)
  700. currentNewObject->assignName(objectName);
  701. else
  702. currentNewObject->setInternalName(objectName);
  703. // Set the original name
  704. currentNewObject->setOriginalName( objectName );
  705. }
  706. // Do the constructor parameters.
  707. if (!currentNewObject->processArguments(callArgc - 3, callArgv + 3))
  708. {
  709. delete currentNewObject;
  710. currentNewObject = NULL;
  711. ip = failJump;
  712. break;
  713. }
  714. // If it's not a datablock, allow people to modify bits of it.
  715. if (!isDataBlock)
  716. {
  717. currentNewObject->setModStaticFields(true);
  718. currentNewObject->setModDynamicFields(true);
  719. }
  720. }
  721. else
  722. {
  723. currentNewObject->reloadReset(); // AFX (reload-reset)
  724. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  725. if (*objParent)
  726. {
  727. // Find it!
  728. SimObject* parent;
  729. if (Sim::findObject(objParent, parent))
  730. {
  731. // Con::printf(" - Parent object found: %s", parent->getClassName());
  732. // temporarily block name change
  733. SimObject::preventNameChanging = true;
  734. currentNewObject->setCopySource(parent);
  735. currentNewObject->assignFieldsFrom(parent);
  736. // restore name changing
  737. SimObject::preventNameChanging = false;
  738. // copy any substitution statements
  739. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  740. if (parent_db)
  741. {
  742. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(currentNewObject);
  743. if (currentNewObject_db)
  744. currentNewObject_db->copySubstitutionsFrom(parent_db);
  745. }
  746. }
  747. else
  748. {
  749. Con::errorf(ConsoleLogEntry::General, "%d: Unable to find parent object %s for %s.", lineNumber, objParent, callArgv[1].getString());
  750. }
  751. }
  752. }
  753. // Advance the IP past the create info...
  754. ip += 7;
  755. break;
  756. }
  757. case OP_ADD_OBJECT:
  758. {
  759. // See OP_SETCURVAR for why we do this.
  760. curFNDocBlock = NULL;
  761. curNSDocBlock = NULL;
  762. // Do we place this object at the root?
  763. bool placeAtRoot = code[ip++];
  764. // Con::printf("Adding object %s", currentNewObject->getName());
  765. // Make sure it wasn't already added, then add it.
  766. if (currentNewObject == NULL)
  767. {
  768. break;
  769. }
  770. bool isMessage = dynamic_cast<Message*>(currentNewObject) != NULL;
  771. if (currentNewObject->isProperlyAdded() == false)
  772. {
  773. bool ret = false;
  774. if (isMessage)
  775. {
  776. SimObjectId id = Message::getNextMessageID();
  777. if (id != 0xffffffff)
  778. ret = currentNewObject->registerObject(id);
  779. else
  780. Con::errorf("%s: No more object IDs available for messages", getFileLine(ip));
  781. }
  782. else
  783. ret = currentNewObject->registerObject();
  784. if (!ret)
  785. {
  786. // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
  787. Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", getFileLine(ip - 2), currentNewObject->getName(), currentNewObject->getClassName());
  788. delete currentNewObject;
  789. ip = failJump;
  790. break;
  791. }
  792. }
  793. // Are we dealing with a datablock?
  794. SimDataBlock* dataBlock = dynamic_cast<SimDataBlock*>(currentNewObject);
  795. String errorStr;
  796. // If so, preload it.
  797. if (dataBlock && !dataBlock->preload(true, errorStr))
  798. {
  799. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", getFileLine(ip - 2),
  800. currentNewObject->getName(), errorStr.c_str());
  801. dataBlock->deleteObject();
  802. ip = failJump;
  803. break;
  804. }
  805. // What group will we be added to, if any?
  806. U32 groupAddId = (U32)intStack[_UINT];
  807. SimGroup* grp = NULL;
  808. SimSet* set = NULL;
  809. if (!placeAtRoot || !currentNewObject->getGroup())
  810. {
  811. if (!isMessage)
  812. {
  813. if (!placeAtRoot)
  814. {
  815. // Otherwise just add to the requested group or set.
  816. if (!Sim::findObject(groupAddId, grp))
  817. Sim::findObject(groupAddId, set);
  818. }
  819. if (placeAtRoot)
  820. {
  821. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  822. if (Con::gInstantGroup.isEmpty() || !Sim::findObject(Con::gInstantGroup, grp))
  823. grp = Sim::getRootGroup();
  824. }
  825. }
  826. // If we didn't get a group, then make sure we have a pointer to
  827. // the rootgroup.
  828. if (!grp)
  829. grp = Sim::getRootGroup();
  830. // add to the parent group
  831. grp->addObject(currentNewObject);
  832. // If for some reason the add failed, add the object to the
  833. // root group so it won't leak.
  834. if (currentNewObject->getGroup() == NULL)
  835. Sim::getRootGroup()->addObject(currentNewObject);
  836. // add to any set we might be in
  837. if (set)
  838. set->addObject(currentNewObject);
  839. }
  840. // store the new object's ID on the stack (overwriting the group/set
  841. // id, if one was given, otherwise getting pushed)
  842. S32 id = currentNewObject->getId();
  843. if (placeAtRoot)
  844. intStack[_UINT] = id;
  845. else
  846. intStack[++_UINT] = id;
  847. break;
  848. }
  849. case OP_END_OBJECT:
  850. {
  851. // If we're not to be placed at the root, make sure we clean up
  852. // our group reference.
  853. bool placeAtRoot = code[ip++];
  854. if (!placeAtRoot)
  855. _UINT--;
  856. break;
  857. }
  858. case OP_FINISH_OBJECT:
  859. {
  860. if (currentNewObject)
  861. currentNewObject->onPostAdd();
  862. //Assert( objectCreationStackIndex >= 0 );
  863. // Restore the object info from the stack [7/9/2007 Black]
  864. currentNewObject = objectCreationStack[--objectCreationStackIndex].newObject;
  865. failJump = objectCreationStack[objectCreationStackIndex].failJump;
  866. break;
  867. }
  868. case OP_JMPIFFNOT:
  869. if (floatStack[_FLT--])
  870. {
  871. ip++;
  872. break;
  873. }
  874. ip = code[ip];
  875. break;
  876. case OP_JMPIFNOT:
  877. if (intStack[_UINT--])
  878. {
  879. ip++;
  880. break;
  881. }
  882. ip = code[ip];
  883. break;
  884. case OP_JMPIFF:
  885. if (!floatStack[_FLT--])
  886. {
  887. ip++;
  888. break;
  889. }
  890. ip = code[ip];
  891. break;
  892. case OP_JMPIF:
  893. if (!intStack[_UINT--])
  894. {
  895. ip++;
  896. break;
  897. }
  898. ip = code[ip];
  899. break;
  900. case OP_JMPIFNOT_NP:
  901. if (intStack[_UINT])
  902. {
  903. _UINT--;
  904. ip++;
  905. break;
  906. }
  907. ip = code[ip];
  908. break;
  909. case OP_JMPIF_NP:
  910. if (!intStack[_UINT])
  911. {
  912. _UINT--;
  913. ip++;
  914. break;
  915. }
  916. ip = code[ip];
  917. break;
  918. case OP_JMP:
  919. ip = code[ip];
  920. break;
  921. // This fixes a bug when not explicitly returning a value.
  922. case OP_RETURN_VOID:
  923. STR.setStringValue("");
  924. // We're falling thru here on purpose.
  925. case OP_RETURN:
  926. {
  927. if (iterDepth > 0)
  928. {
  929. // Clear iterator state.
  930. while (iterDepth > 0)
  931. {
  932. iterStack[--_ITER].mIsStringIter = false;
  933. --iterDepth;
  934. }
  935. const char* retVal = STR.getStringValue();
  936. STR.rewind();
  937. STR.setStringValue(retVal); // Not nice but works.
  938. }
  939. returnValue.setString(STR.getStringValue(), STR.mLen);
  940. goto execFinished;
  941. }
  942. case OP_RETURN_FLT:
  943. if (iterDepth > 0)
  944. {
  945. // Clear iterator state.
  946. while (iterDepth > 0)
  947. {
  948. iterStack[--_ITER].mIsStringIter = false;
  949. --iterDepth;
  950. }
  951. }
  952. returnValue.setFloat(floatStack[_FLT]);
  953. _FLT--;
  954. goto execFinished;
  955. case OP_RETURN_UINT:
  956. if (iterDepth > 0)
  957. {
  958. // Clear iterator state.
  959. while (iterDepth > 0)
  960. {
  961. iterStack[--_ITER].mIsStringIter = false;
  962. --iterDepth;
  963. }
  964. }
  965. returnValue.setInt(intStack[_UINT]);
  966. _UINT--;
  967. goto execFinished;
  968. case OP_CMPEQ:
  969. intStack[_UINT + 1] = bool(floatStack[_FLT] == floatStack[_FLT - 1]);
  970. _UINT++;
  971. _FLT -= 2;
  972. break;
  973. case OP_CMPGR:
  974. intStack[_UINT + 1] = bool(floatStack[_FLT] > floatStack[_FLT - 1]);
  975. _UINT++;
  976. _FLT -= 2;
  977. break;
  978. case OP_CMPGE:
  979. intStack[_UINT + 1] = bool(floatStack[_FLT] >= floatStack[_FLT - 1]);
  980. _UINT++;
  981. _FLT -= 2;
  982. break;
  983. case OP_CMPLT:
  984. intStack[_UINT + 1] = bool(floatStack[_FLT] < floatStack[_FLT - 1]);
  985. _UINT++;
  986. _FLT -= 2;
  987. break;
  988. case OP_CMPLE:
  989. intStack[_UINT + 1] = bool(floatStack[_FLT] <= floatStack[_FLT - 1]);
  990. _UINT++;
  991. _FLT -= 2;
  992. break;
  993. case OP_CMPNE:
  994. intStack[_UINT + 1] = bool(floatStack[_FLT] != floatStack[_FLT - 1]);
  995. _UINT++;
  996. _FLT -= 2;
  997. break;
  998. case OP_XOR:
  999. intStack[_UINT - 1] = intStack[_UINT] ^ intStack[_UINT - 1];
  1000. _UINT--;
  1001. break;
  1002. case OP_MOD:
  1003. if (intStack[_UINT - 1] != 0)
  1004. intStack[_UINT - 1] = intStack[_UINT] % intStack[_UINT - 1];
  1005. else
  1006. intStack[_UINT - 1] = 0;
  1007. _UINT--;
  1008. break;
  1009. case OP_BITAND:
  1010. intStack[_UINT - 1] = intStack[_UINT] & intStack[_UINT - 1];
  1011. _UINT--;
  1012. break;
  1013. case OP_BITOR:
  1014. intStack[_UINT - 1] = intStack[_UINT] | intStack[_UINT - 1];
  1015. _UINT--;
  1016. break;
  1017. case OP_NOT:
  1018. intStack[_UINT] = !intStack[_UINT];
  1019. break;
  1020. case OP_NOTF:
  1021. intStack[_UINT + 1] = !floatStack[_FLT];
  1022. _FLT--;
  1023. _UINT++;
  1024. break;
  1025. case OP_ONESCOMPLEMENT:
  1026. intStack[_UINT] = ~intStack[_UINT];
  1027. break;
  1028. case OP_SHR:
  1029. intStack[_UINT - 1] = intStack[_UINT] >> intStack[_UINT - 1];
  1030. _UINT--;
  1031. break;
  1032. case OP_SHL:
  1033. intStack[_UINT - 1] = intStack[_UINT] << intStack[_UINT - 1];
  1034. _UINT--;
  1035. break;
  1036. case OP_AND:
  1037. intStack[_UINT - 1] = intStack[_UINT] && intStack[_UINT - 1];
  1038. _UINT--;
  1039. break;
  1040. case OP_OR:
  1041. intStack[_UINT - 1] = intStack[_UINT] || intStack[_UINT - 1];
  1042. _UINT--;
  1043. break;
  1044. case OP_ADD:
  1045. floatStack[_FLT - 1] = floatStack[_FLT] + floatStack[_FLT - 1];
  1046. _FLT--;
  1047. break;
  1048. case OP_SUB:
  1049. floatStack[_FLT - 1] = floatStack[_FLT] - floatStack[_FLT - 1];
  1050. _FLT--;
  1051. break;
  1052. case OP_MUL:
  1053. floatStack[_FLT - 1] = floatStack[_FLT] * floatStack[_FLT - 1];
  1054. _FLT--;
  1055. break;
  1056. case OP_DIV:
  1057. floatStack[_FLT - 1] = floatStack[_FLT] / floatStack[_FLT - 1];
  1058. _FLT--;
  1059. break;
  1060. case OP_NEG:
  1061. floatStack[_FLT] = -floatStack[_FLT];
  1062. break;
  1063. case OP_INC:
  1064. reg = code[ip++];
  1065. gEvalState.setLocalFloatVariable(reg, gEvalState.getLocalFloatVariable(reg) + 1.0);
  1066. break;
  1067. case OP_SETCURVAR:
  1068. var = CodeToSTE(code, ip);
  1069. ip += 2;
  1070. // If a variable is set, then these must be NULL. It is necessary
  1071. // to set this here so that the vector parser can appropriately
  1072. // identify whether it's dealing with a vector.
  1073. prevField = NULL;
  1074. prevObject = NULL;
  1075. curObject = NULL;
  1076. gEvalState.setCurVarName(var);
  1077. // In order to let docblocks work properly with variables, we have
  1078. // clear the current docblock when we do an assign. This way it
  1079. // won't inappropriately carry forward to following function decls.
  1080. curFNDocBlock = NULL;
  1081. curNSDocBlock = NULL;
  1082. break;
  1083. case OP_SETCURVAR_CREATE:
  1084. var = CodeToSTE(code, ip);
  1085. ip += 2;
  1086. // See OP_SETCURVAR
  1087. prevField = NULL;
  1088. prevObject = NULL;
  1089. curObject = NULL;
  1090. gEvalState.setCurVarNameCreate(var);
  1091. // See OP_SETCURVAR for why we do this.
  1092. curFNDocBlock = NULL;
  1093. curNSDocBlock = NULL;
  1094. break;
  1095. case OP_SETCURVAR_ARRAY:
  1096. var = STR.getSTValue();
  1097. // See OP_SETCURVAR
  1098. prevField = NULL;
  1099. prevObject = NULL;
  1100. curObject = NULL;
  1101. gEvalState.setCurVarName(var);
  1102. // See OP_SETCURVAR for why we do this.
  1103. curFNDocBlock = NULL;
  1104. curNSDocBlock = NULL;
  1105. break;
  1106. case OP_SETCURVAR_ARRAY_CREATE:
  1107. var = STR.getSTValue();
  1108. // See OP_SETCURVAR
  1109. prevField = NULL;
  1110. prevObject = NULL;
  1111. curObject = NULL;
  1112. gEvalState.setCurVarNameCreate(var);
  1113. // See OP_SETCURVAR for why we do this.
  1114. curFNDocBlock = NULL;
  1115. curNSDocBlock = NULL;
  1116. break;
  1117. case OP_LOADVAR_UINT:
  1118. intStack[_UINT + 1] = gEvalState.getIntVariable();
  1119. _UINT++;
  1120. break;
  1121. case OP_LOADVAR_FLT:
  1122. floatStack[_FLT + 1] = gEvalState.getFloatVariable();
  1123. _FLT++;
  1124. break;
  1125. case OP_LOADVAR_STR:
  1126. val = gEvalState.getStringVariable();
  1127. STR.setStringValue(val);
  1128. break;
  1129. case OP_SAVEVAR_UINT:
  1130. gEvalState.setIntVariable((S32)intStack[_UINT]);
  1131. break;
  1132. case OP_SAVEVAR_FLT:
  1133. gEvalState.setFloatVariable(floatStack[_FLT]);
  1134. break;
  1135. case OP_SAVEVAR_STR:
  1136. gEvalState.setStringVariable(STR.getStringValue());
  1137. break;
  1138. case OP_LOAD_LOCAL_VAR_UINT:
  1139. reg = code[ip++];
  1140. intStack[_UINT + 1] = gEvalState.getLocalIntVariable(reg);
  1141. _UINT++;
  1142. break;
  1143. case OP_LOAD_LOCAL_VAR_FLT:
  1144. reg = code[ip++];
  1145. floatStack[_FLT + 1] = gEvalState.getLocalFloatVariable(reg);
  1146. _FLT++;
  1147. break;
  1148. case OP_LOAD_LOCAL_VAR_STR:
  1149. reg = code[ip++];
  1150. val = gEvalState.getLocalStringVariable(reg);
  1151. STR.setStringValue(val);
  1152. break;
  1153. case OP_SAVE_LOCAL_VAR_UINT:
  1154. reg = code[ip++];
  1155. gEvalState.setLocalIntVariable(reg, (S32)intStack[_UINT]);
  1156. break;
  1157. case OP_SAVE_LOCAL_VAR_FLT:
  1158. reg = code[ip++];
  1159. gEvalState.setLocalFloatVariable(reg, floatStack[_FLT]);
  1160. break;
  1161. case OP_SAVE_LOCAL_VAR_STR:
  1162. reg = code[ip++];
  1163. gEvalState.setLocalStringVariable(reg, STR.getStringValue(), (S32)STR.mLen);
  1164. break;
  1165. case OP_SETCUROBJECT:
  1166. // Save the previous object for parsing vector fields.
  1167. prevObject = curObject;
  1168. val = STR.getStringValue();
  1169. // Sim::findObject will sometimes find valid objects from
  1170. // multi-component strings. This makes sure that doesn't
  1171. // happen.
  1172. for (const char* check = val; *check; check++)
  1173. {
  1174. if (*check == ' ')
  1175. {
  1176. val = "";
  1177. break;
  1178. }
  1179. }
  1180. curObject = Sim::findObject(val);
  1181. break;
  1182. case OP_SETCUROBJECT_INTERNAL:
  1183. ++ip; // To skip the recurse flag if the object wasnt found
  1184. if (curObject)
  1185. {
  1186. SimGroup* group = dynamic_cast<SimGroup*>(curObject);
  1187. if (group)
  1188. {
  1189. StringTableEntry intName = StringTable->insert(STR.getStringValue());
  1190. bool recurse = code[ip - 1];
  1191. SimObject* obj = group->findObjectByInternalName(intName, recurse);
  1192. intStack[_UINT + 1] = obj ? obj->getId() : 0;
  1193. _UINT++;
  1194. }
  1195. else
  1196. {
  1197. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-group %s of class %s.", getFileLine(ip - 2), curObject->getName(), curObject->getClassName());
  1198. intStack[_UINT] = 0;
  1199. }
  1200. }
  1201. break;
  1202. case OP_SETCUROBJECT_NEW:
  1203. curObject = currentNewObject;
  1204. break;
  1205. case OP_SETCURFIELD:
  1206. // Save the previous field for parsing vector fields.
  1207. prevField = curField;
  1208. dStrcpy(prevFieldArray, curFieldArray, 256);
  1209. curField = CodeToSTE(code, ip);
  1210. curFieldArray[0] = 0;
  1211. ip += 2;
  1212. break;
  1213. case OP_SETCURFIELD_ARRAY:
  1214. dStrcpy(curFieldArray, STR.getStringValue(), 256);
  1215. break;
  1216. case OP_SETCURFIELD_TYPE:
  1217. if(curObject)
  1218. curObject->setDataFieldType(code[ip], curField, curFieldArray);
  1219. ip++;
  1220. break;
  1221. case OP_LOADFIELD_UINT:
  1222. if (curObject)
  1223. intStack[_UINT + 1] = U32(dAtoi(curObject->getDataField(curField, curFieldArray)));
  1224. else
  1225. {
  1226. // The field is not being retrieved from an object. Maybe it's
  1227. // a special accessor?
  1228. char buff[FieldBufferSizeNumeric];
  1229. memset(buff, 0, sizeof(buff));
  1230. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1231. intStack[_UINT + 1] = dAtoi(buff);
  1232. }
  1233. _UINT++;
  1234. break;
  1235. case OP_LOADFIELD_FLT:
  1236. if (curObject)
  1237. floatStack[_FLT + 1] = dAtof(curObject->getDataField(curField, curFieldArray));
  1238. else
  1239. {
  1240. // The field is not being retrieved from an object. Maybe it's
  1241. // a special accessor?
  1242. char buff[FieldBufferSizeNumeric];
  1243. memset(buff, 0, sizeof(buff));
  1244. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1245. floatStack[_FLT + 1] = dAtof(buff);
  1246. }
  1247. _FLT++;
  1248. break;
  1249. case OP_LOADFIELD_STR:
  1250. if (curObject)
  1251. {
  1252. val = curObject->getDataField(curField, curFieldArray);
  1253. STR.setStringValue(val);
  1254. }
  1255. else
  1256. {
  1257. // The field is not being retrieved from an object. Maybe it's
  1258. // a special accessor?
  1259. char buff[FieldBufferSizeString];
  1260. memset(buff, 0, sizeof(buff));
  1261. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff);
  1262. STR.setStringValue(buff);
  1263. }
  1264. break;
  1265. case OP_SAVEFIELD_UINT:
  1266. STR.setIntValue((U32)intStack[_UINT]);
  1267. if (curObject)
  1268. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1269. else
  1270. {
  1271. // The field is not being set on an object. Maybe it's
  1272. // a special accessor?
  1273. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1274. prevObject = NULL;
  1275. }
  1276. break;
  1277. case OP_SAVEFIELD_FLT:
  1278. STR.setFloatValue(floatStack[_FLT]);
  1279. if (curObject)
  1280. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1281. else
  1282. {
  1283. // The field is not being set on an object. Maybe it's
  1284. // a special accessor?
  1285. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1286. prevObject = NULL;
  1287. }
  1288. break;
  1289. case OP_SAVEFIELD_STR:
  1290. if (curObject)
  1291. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1292. else
  1293. {
  1294. // The field is not being set on an object. Maybe it's
  1295. // a special accessor?
  1296. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1297. prevObject = NULL;
  1298. }
  1299. break;
  1300. case OP_STR_TO_UINT:
  1301. intStack[_UINT + 1] = STR.getIntValue();
  1302. _UINT++;
  1303. break;
  1304. case OP_STR_TO_FLT:
  1305. floatStack[_FLT + 1] = STR.getFloatValue();
  1306. _FLT++;
  1307. break;
  1308. case OP_STR_TO_NONE:
  1309. // This exists simply to deal with certain typecast situations.
  1310. break;
  1311. case OP_FLT_TO_UINT:
  1312. intStack[_UINT + 1] = (S64)floatStack[_FLT];
  1313. _FLT--;
  1314. _UINT++;
  1315. break;
  1316. case OP_FLT_TO_STR:
  1317. STR.setFloatValue(floatStack[_FLT]);
  1318. _FLT--;
  1319. break;
  1320. case OP_FLT_TO_NONE:
  1321. _FLT--;
  1322. break;
  1323. case OP_UINT_TO_FLT:
  1324. floatStack[_FLT + 1] = (F64)intStack[_UINT];
  1325. _UINT--;
  1326. _FLT++;
  1327. break;
  1328. case OP_UINT_TO_STR:
  1329. STR.setIntValue((U32)intStack[_UINT]);
  1330. _UINT--;
  1331. break;
  1332. case OP_UINT_TO_NONE:
  1333. _UINT--;
  1334. break;
  1335. case OP_LOADIMMED_UINT:
  1336. intStack[_UINT + 1] = code[ip++];
  1337. _UINT++;
  1338. break;
  1339. case OP_LOADIMMED_FLT:
  1340. floatStack[_FLT + 1] = curFloatTable[code[ip]];
  1341. ip++;
  1342. _FLT++;
  1343. break;
  1344. case OP_TAG_TO_STR:
  1345. code[ip - 1] = OP_LOADIMMED_STR;
  1346. // it's possible the string has already been converted
  1347. if (U8(curStringTable[code[ip]]) != StringTagPrefixByte)
  1348. {
  1349. U32 id = GameAddTaggedString(curStringTable + code[ip]);
  1350. dSprintf(curStringTable + code[ip] + 1, 7, "%d", id);
  1351. *(curStringTable + code[ip]) = StringTagPrefixByte;
  1352. }
  1353. case OP_LOADIMMED_STR:
  1354. STR.setStringValue(curStringTable + code[ip++]);
  1355. break;
  1356. case OP_DOCBLOCK_STR:
  1357. {
  1358. // If the first word of the doc is '\class' or '@class', then this
  1359. // is a namespace doc block, otherwise it is a function doc block.
  1360. const char* docblock = curStringTable + code[ip++];
  1361. const char* sansClass = dStrstr(docblock, "@class");
  1362. if (!sansClass)
  1363. sansClass = dStrstr(docblock, "\\class");
  1364. if (sansClass)
  1365. {
  1366. // Don't save the class declaration. Scan past the 'class'
  1367. // keyword and up to the first whitespace.
  1368. sansClass += 7;
  1369. S32 index = 0;
  1370. while ((*sansClass != ' ') && (*sansClass != '\n') && *sansClass && (index < (nsDocLength - 1)))
  1371. {
  1372. nsDocBlockClass[index++] = *sansClass;
  1373. sansClass++;
  1374. }
  1375. nsDocBlockClass[index] = '\0';
  1376. curNSDocBlock = sansClass + 1;
  1377. }
  1378. else
  1379. curFNDocBlock = docblock;
  1380. }
  1381. break;
  1382. case OP_LOADIMMED_IDENT:
  1383. STR.setStringValue(CodeToSTE(code, ip));
  1384. ip += 2;
  1385. break;
  1386. case OP_CALLFUNC:
  1387. {
  1388. // This routingId is set when we query the object as to whether
  1389. // it handles this method. It is set to an enum from the table
  1390. // above indicating whether it handles it on a component it owns
  1391. // or just on the object.
  1392. S32 routingId = 0;
  1393. fnName = CodeToSTE(code, ip);
  1394. fnNamespace = CodeToSTE(code, ip + 2);
  1395. U32 callType = code[ip + 4];
  1396. //if this is called from inside a function, append the ip and codeptr
  1397. if (!gEvalState.stack.empty())
  1398. {
  1399. gEvalState.stack.last()->code = this;
  1400. gEvalState.stack.last()->ip = ip - 1;
  1401. }
  1402. ip += 5;
  1403. gCallStack.argvc(fnName, callArgc, &callArgv);
  1404. if (callType == FuncCallExprNode::FunctionCall)
  1405. {
  1406. // Note: This works even if the function was in a package. Reason being is when
  1407. // activatePackage() is called, it swaps the namespaceEntry into the global namespace
  1408. // (and reverts it when deactivatePackage is called). Method or Static related ones work
  1409. // as expected, as the namespace is resolved on the fly.
  1410. nsEntry = Namespace::global()->lookup(fnName);
  1411. if (!nsEntry)
  1412. {
  1413. Con::warnf(ConsoleLogEntry::General,
  1414. "%s: Unable to find function %s",
  1415. getFileLine(ip - 4), fnName);
  1416. //STR.popFrame();
  1417. gCallStack.popFrame();
  1418. break;
  1419. }
  1420. }
  1421. else if (callType == FuncCallExprNode::StaticCall)
  1422. {
  1423. // Try to look it up.
  1424. ns = Namespace::find(fnNamespace);
  1425. nsEntry = ns->lookup(fnName);
  1426. if (!nsEntry)
  1427. {
  1428. Con::warnf(ConsoleLogEntry::General,
  1429. "%s: Unable to find function %s%s%s",
  1430. getFileLine(ip - 4), fnNamespace ? fnNamespace : "",
  1431. fnNamespace ? "::" : "", fnName);
  1432. //STR.popFrame();
  1433. gCallStack.popFrame();
  1434. break;
  1435. }
  1436. }
  1437. else if (callType == FuncCallExprNode::MethodCall)
  1438. {
  1439. saveObject = gEvalState.thisObject;
  1440. // Optimization: If we're an integer, we can lookup the value by SimObjectId
  1441. const ConsoleValue& simObjectLookupValue = callArgv[1];
  1442. if (simObjectLookupValue.getType() == ConsoleValueType::cvInteger)
  1443. gEvalState.thisObject = Sim::findObject(static_cast<SimObjectId>(simObjectLookupValue.getInt()));
  1444. else
  1445. gEvalState.thisObject = Sim::findObject(simObjectLookupValue.getString());
  1446. if (gEvalState.thisObject == NULL)
  1447. {
  1448. Con::warnf(
  1449. ConsoleLogEntry::General,
  1450. "%s: Unable to find object: '%s' attempting to call function '%s'",
  1451. getFileLine(ip - 6),
  1452. simObjectLookupValue.getString(),
  1453. fnName
  1454. );
  1455. //STR.popFrame();
  1456. gCallStack.popFrame();
  1457. STR.setStringValue("");
  1458. break;
  1459. }
  1460. ns = gEvalState.thisObject->getNamespace();
  1461. if (ns)
  1462. nsEntry = ns->lookup(fnName);
  1463. else
  1464. nsEntry = NULL;
  1465. }
  1466. else // it's a ParentCall
  1467. {
  1468. if (thisNamespace)
  1469. {
  1470. ns = thisNamespace->mParent;
  1471. if (ns)
  1472. nsEntry = ns->lookup(fnName);
  1473. else
  1474. nsEntry = NULL;
  1475. }
  1476. else
  1477. {
  1478. ns = NULL;
  1479. nsEntry = NULL;
  1480. }
  1481. }
  1482. if (!nsEntry || noCalls)
  1483. {
  1484. if (!noCalls)
  1485. {
  1486. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", getFileLine(ip - 4), fnName);
  1487. if (callType == FuncCallExprNode::MethodCall)
  1488. {
  1489. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1490. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1491. gEvalState.thisObject->getId(), Con::getNamespaceList(ns));
  1492. }
  1493. }
  1494. gCallStack.popFrame();
  1495. STR.setStringValue("");
  1496. STR.setStringValue("");
  1497. break;
  1498. }
  1499. if (nsEntry->mType == Namespace::Entry::ConsoleFunctionType)
  1500. {
  1501. if (nsEntry->mFunctionOffset)
  1502. {
  1503. ConsoleValue returnFromFn = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage);
  1504. STR.setStringValue(returnFromFn.getString());
  1505. }
  1506. else // no body
  1507. STR.setStringValue("");
  1508. gCallStack.popFrame();
  1509. }
  1510. else
  1511. {
  1512. if ((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1513. {
  1514. const char* nsName = ns ? ns->mName : "";
  1515. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments.", getFileLine(ip - 4), nsName, fnName);
  1516. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip - 4), nsEntry->mUsage);
  1517. gCallStack.popFrame();
  1518. STR.setStringValue("");
  1519. }
  1520. else
  1521. {
  1522. switch (nsEntry->mType)
  1523. {
  1524. case Namespace::Entry::StringCallbackType:
  1525. {
  1526. const char* ret = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1527. gCallStack.popFrame();
  1528. if (ret != STR.getStringValue())
  1529. STR.setStringValue(ret);
  1530. else
  1531. STR.setLen(dStrlen(ret));
  1532. break;
  1533. }
  1534. case Namespace::Entry::IntCallbackType:
  1535. {
  1536. S32 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1537. gCallStack.popFrame();
  1538. if (code[ip] == OP_STR_TO_UINT)
  1539. {
  1540. ip++;
  1541. intStack[++_UINT] = result;
  1542. break;
  1543. }
  1544. else if (code[ip] == OP_STR_TO_FLT)
  1545. {
  1546. ip++;
  1547. floatStack[++_FLT] = result;
  1548. break;
  1549. }
  1550. else if (code[ip] == OP_STR_TO_NONE)
  1551. ip++;
  1552. else
  1553. STR.setIntValue(result);
  1554. break;
  1555. }
  1556. case Namespace::Entry::FloatCallbackType:
  1557. {
  1558. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1559. gCallStack.popFrame();
  1560. if (code[ip] == OP_STR_TO_UINT)
  1561. {
  1562. ip++;
  1563. intStack[++_UINT] = (S64)result;
  1564. break;
  1565. }
  1566. else if (code[ip] == OP_STR_TO_FLT)
  1567. {
  1568. ip++;
  1569. floatStack[++_FLT] = result;
  1570. break;
  1571. }
  1572. else if (code[ip] == OP_STR_TO_NONE)
  1573. ip++;
  1574. else
  1575. STR.setFloatValue(result);
  1576. break;
  1577. }
  1578. case Namespace::Entry::VoidCallbackType:
  1579. {
  1580. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1581. if (code[ip] != OP_STR_TO_NONE)
  1582. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip - 4), fnName, functionName);
  1583. gCallStack.popFrame();
  1584. STR.setStringValue("");
  1585. break;
  1586. }
  1587. case Namespace::Entry::BoolCallbackType:
  1588. {
  1589. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1590. gCallStack.popFrame();
  1591. if (code[ip] == OP_STR_TO_UINT)
  1592. {
  1593. ip++;
  1594. intStack[++_UINT] = result;
  1595. break;
  1596. }
  1597. else if (code[ip] == OP_STR_TO_FLT)
  1598. {
  1599. ip++;
  1600. floatStack[++_FLT] = result;
  1601. break;
  1602. }
  1603. else if (code[ip] == OP_STR_TO_NONE)
  1604. ip++;
  1605. else
  1606. STR.setIntValue(result);
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. }
  1612. if (callType == FuncCallExprNode::MethodCall)
  1613. gEvalState.thisObject = saveObject;
  1614. break;
  1615. }
  1616. case OP_ADVANCE_STR:
  1617. STR.advance();
  1618. break;
  1619. case OP_ADVANCE_STR_APPENDCHAR:
  1620. STR.advanceChar(code[ip++]);
  1621. break;
  1622. case OP_ADVANCE_STR_COMMA:
  1623. STR.advanceChar('_');
  1624. break;
  1625. case OP_ADVANCE_STR_NUL:
  1626. STR.advanceChar(0);
  1627. break;
  1628. case OP_REWIND_STR:
  1629. STR.rewind();
  1630. break;
  1631. case OP_TERMINATE_REWIND_STR:
  1632. STR.rewindTerminate();
  1633. break;
  1634. case OP_COMPARE_STR:
  1635. intStack[++_UINT] = STR.compare();
  1636. break;
  1637. case OP_PUSH:
  1638. gCallStack.pushString(STR.getStringValue(), STR.mLen);
  1639. break;
  1640. case OP_PUSH_UINT:
  1641. gCallStack.pushInt((U32)intStack[_UINT--]);
  1642. break;
  1643. case OP_PUSH_FLT:
  1644. gCallStack.pushFloat(floatStack[_FLT--]);
  1645. break;
  1646. case OP_PUSH_FRAME:
  1647. gCallStack.pushFrame(code[ip++]);
  1648. break;
  1649. case OP_ASSERT:
  1650. {
  1651. if (!intStack[_UINT--])
  1652. {
  1653. const char* message = curStringTable + code[ip];
  1654. U32 breakLine, inst;
  1655. findBreakLine(ip - 1, breakLine, inst);
  1656. if (PlatformAssert::processAssert(PlatformAssert::Fatal,
  1657. name ? name : "eval",
  1658. breakLine,
  1659. message))
  1660. {
  1661. if (TelDebugger && TelDebugger->isConnected() && breakLine > 0)
  1662. {
  1663. TelDebugger->breakProcess();
  1664. }
  1665. else
  1666. Platform::debugBreak();
  1667. }
  1668. }
  1669. ip++;
  1670. break;
  1671. }
  1672. case OP_BREAK:
  1673. {
  1674. //append the ip and codeptr before managing the breakpoint!
  1675. AssertFatal(!gEvalState.stack.empty(), "Empty eval stack on break!");
  1676. gEvalState.stack.last()->code = this;
  1677. gEvalState.stack.last()->ip = ip - 1;
  1678. U32 breakLine;
  1679. findBreakLine(ip - 1, breakLine, instruction);
  1680. if (!breakLine)
  1681. goto breakContinue;
  1682. TelDebugger->executionStopped(this, breakLine);
  1683. goto breakContinue;
  1684. }
  1685. case OP_ITER_BEGIN_STR:
  1686. {
  1687. iterStack[_ITER].mIsStringIter = true;
  1688. /* fallthrough */
  1689. }
  1690. case OP_ITER_BEGIN:
  1691. {
  1692. StringTableEntry varName = CodeToSTE(code, ip);
  1693. U32 failIp = code[ip + 2];
  1694. IterStackRecord& iter = iterStack[_ITER];
  1695. iter.mVariable = gEvalState.getCurrentFrame().add(varName);
  1696. if (iter.mIsStringIter)
  1697. {
  1698. iter.mData.mStr.mString = STR.getStringValue();
  1699. iter.mData.mStr.mIndex = 0;
  1700. }
  1701. else
  1702. {
  1703. // Look up the object.
  1704. SimSet* set;
  1705. if (!Sim::findObject(STR.getStringValue(), set))
  1706. {
  1707. Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", STR.getStringValue());
  1708. Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
  1709. ip = failIp;
  1710. continue;
  1711. }
  1712. // Set up.
  1713. iter.mData.mObj.mSet = set;
  1714. iter.mData.mObj.mIndex = 0;
  1715. }
  1716. _ITER++;
  1717. iterDepth++;
  1718. STR.push();
  1719. ip += 3;
  1720. break;
  1721. }
  1722. case OP_ITER:
  1723. {
  1724. U32 breakIp = code[ip];
  1725. IterStackRecord& iter = iterStack[_ITER - 1];
  1726. if (iter.mIsStringIter)
  1727. {
  1728. const char* str = iter.mData.mStr.mString;
  1729. U32 startIndex = iter.mData.mStr.mIndex;
  1730. U32 endIndex = startIndex;
  1731. // Break if at end.
  1732. if (!str[startIndex])
  1733. {
  1734. ip = breakIp;
  1735. continue;
  1736. }
  1737. // Find right end of current component.
  1738. if (!dIsspace(str[endIndex]))
  1739. do ++endIndex;
  1740. while (str[endIndex] && !dIsspace(str[endIndex]));
  1741. // Extract component.
  1742. if (endIndex != startIndex)
  1743. {
  1744. char savedChar = str[endIndex];
  1745. const_cast<char*>(str)[endIndex] = '\0'; // We are on the string stack so this is okay.
  1746. iter.mVariable->setStringValue(&str[startIndex]);
  1747. const_cast<char*>(str)[endIndex] = savedChar;
  1748. }
  1749. else
  1750. iter.mVariable->setStringValue("");
  1751. // Skip separator.
  1752. if (str[endIndex] != '\0')
  1753. ++endIndex;
  1754. iter.mData.mStr.mIndex = endIndex;
  1755. }
  1756. else
  1757. {
  1758. U32 index = iter.mData.mObj.mIndex;
  1759. SimSet* set = iter.mData.mObj.mSet;
  1760. if (index >= set->size())
  1761. {
  1762. ip = breakIp;
  1763. continue;
  1764. }
  1765. iter.mVariable->setIntValue(set->at(index)->getId());
  1766. iter.mData.mObj.mIndex = index + 1;
  1767. }
  1768. ++ip;
  1769. break;
  1770. }
  1771. case OP_ITER_END:
  1772. {
  1773. --_ITER;
  1774. --iterDepth;
  1775. STR.rewind();
  1776. iterStack[_ITER].mIsStringIter = false;
  1777. break;
  1778. }
  1779. case OP_INVALID:
  1780. default:
  1781. // error!
  1782. goto execFinished;
  1783. }
  1784. }
  1785. execFinished:
  1786. if (telDebuggerOn && setFrame < 0)
  1787. TelDebugger->popStackFrame();
  1788. if (popFrame)
  1789. {
  1790. gEvalState.popFrame();
  1791. }
  1792. if (argv)
  1793. {
  1794. if (gEvalState.traceOn)
  1795. {
  1796. traceBuffer[0] = 0;
  1797. dStrcat(traceBuffer, "Leaving ", TRACE_BUFFER_SIZE);
  1798. if (packageName)
  1799. {
  1800. dStrcat(traceBuffer, "[", TRACE_BUFFER_SIZE);
  1801. dStrcat(traceBuffer, packageName, TRACE_BUFFER_SIZE);
  1802. dStrcat(traceBuffer, "]", TRACE_BUFFER_SIZE);
  1803. }
  1804. if (thisNamespace && thisNamespace->mName)
  1805. {
  1806. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1807. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, returnValue.getString());
  1808. }
  1809. else
  1810. {
  1811. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1812. "%s() - return %s", thisFunctionName, returnValue.getString());
  1813. }
  1814. Con::printf("%s", traceBuffer);
  1815. }
  1816. }
  1817. else
  1818. {
  1819. delete[] const_cast<char*>(globalStrings);
  1820. delete[] globalFloats;
  1821. globalStrings = NULL;
  1822. globalFloats = NULL;
  1823. }
  1824. smCurrentCodeBlock = saveCodeBlock;
  1825. if (saveCodeBlock && saveCodeBlock->name)
  1826. {
  1827. Con::gCurrentFile = saveCodeBlock->name;
  1828. Con::gCurrentRoot = saveCodeBlock->modPath;
  1829. }
  1830. decRefCount();
  1831. #ifdef TORQUE_DEBUG
  1832. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  1833. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  1834. #endif
  1835. return std::move(returnValue);
  1836. }
  1837. //------------------------------------------------------------