compiledEval.cpp 70 KB

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