compiledEval.cpp 64 KB

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