compiledEval.cpp 65 KB

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