compiledEval.cpp 65 KB

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