compiledEval.cpp 64 KB

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