compiledEval.cpp 65 KB

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