compiledEval.cpp 64 KB

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