2
0

compiledEval.cpp 70 KB

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