compiledEval.cpp 70 KB

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