compiledEval.cpp 65 KB

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