compiledEval.cpp 65 KB

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