compiledEval.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  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. else 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. currentNewObject = NULL;
  935. ip = failJump;
  936. break;
  937. }
  938. }
  939. // Are we dealing with a datablock?
  940. SimDataBlock* dataBlock = dynamic_cast<SimDataBlock*>(currentNewObject);
  941. String errorStr;
  942. // If so, preload it.
  943. if (dataBlock && !dataBlock->preload(true, errorStr))
  944. {
  945. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", getFileLine(ip - 2),
  946. currentNewObject->getName(), errorStr.c_str());
  947. dataBlock->deleteObject();
  948. currentNewObject = NULL;
  949. ip = failJump;
  950. break;
  951. }
  952. // What group will we be added to, if any?
  953. U32 groupAddId = (U32)stack[_STK].getInt();
  954. SimGroup* grp = NULL;
  955. SimSet* set = NULL;
  956. if (!placeAtRoot || !currentNewObject->getGroup())
  957. {
  958. if (!isMessage)
  959. {
  960. if (!placeAtRoot)
  961. {
  962. // Otherwise just add to the requested group or set.
  963. if (!Sim::findObject(groupAddId, grp))
  964. Sim::findObject(groupAddId, set);
  965. }
  966. if (placeAtRoot)
  967. {
  968. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  969. if (Con::gInstantGroup.isEmpty() || !Sim::findObject(Con::gInstantGroup, grp))
  970. grp = Sim::getRootGroup();
  971. }
  972. }
  973. // If we didn't get a group, then make sure we have a pointer to
  974. // the rootgroup.
  975. if (!grp)
  976. grp = Sim::getRootGroup();
  977. // add to the parent group
  978. grp->addObject(currentNewObject);
  979. // If for some reason the add failed, add the object to the
  980. // root group so it won't leak.
  981. if (currentNewObject->getGroup() == NULL)
  982. Sim::getRootGroup()->addObject(currentNewObject);
  983. // add to any set we might be in
  984. if (set)
  985. set->addObject(currentNewObject);
  986. }
  987. // store the new object's ID on the stack (overwriting the group/set
  988. // id, if one was given, otherwise getting pushed)
  989. S32 id = currentNewObject->getId();
  990. if (placeAtRoot)
  991. stack[_STK].setInt(id);
  992. else
  993. stack[++_STK].setInt(id);
  994. break;
  995. }
  996. case OP_END_OBJECT:
  997. {
  998. // If we're not to be placed at the root, make sure we clean up
  999. // our group reference.
  1000. bool placeAtRoot = code[ip++];
  1001. if (!placeAtRoot)
  1002. _STK--;
  1003. break;
  1004. }
  1005. case OP_FINISH_OBJECT:
  1006. {
  1007. if (currentNewObject)
  1008. currentNewObject->onPostAdd();
  1009. AssertFatal( objectCreationStackIndex >= 0, "Object Stack is empty." );
  1010. // Restore the object info from the stack [7/9/2007 Black]
  1011. currentNewObject = objectCreationStack[--objectCreationStackIndex].newObject;
  1012. failJump = objectCreationStack[objectCreationStackIndex].failJump;
  1013. break;
  1014. }
  1015. case OP_JMPIFFNOT:
  1016. if (stack[_STK--].getFloat())
  1017. {
  1018. ip++;
  1019. break;
  1020. }
  1021. ip = code[ip];
  1022. break;
  1023. case OP_JMPIFNOT:
  1024. if (stack[_STK--].getInt())
  1025. {
  1026. ip++;
  1027. break;
  1028. }
  1029. ip = code[ip];
  1030. break;
  1031. case OP_JMPIFF:
  1032. if (!stack[_STK--].getFloat())
  1033. {
  1034. ip++;
  1035. break;
  1036. }
  1037. ip = code[ip];
  1038. break;
  1039. case OP_JMPIF:
  1040. if (!stack[_STK--].getFloat())
  1041. {
  1042. ip++;
  1043. break;
  1044. }
  1045. ip = code[ip];
  1046. break;
  1047. case OP_JMPIFNOT_NP:
  1048. if (stack[_STK].getInt())
  1049. {
  1050. _STK--;
  1051. ip++;
  1052. break;
  1053. }
  1054. ip = code[ip];
  1055. break;
  1056. case OP_JMPIF_NP:
  1057. if (!stack[_STK].getInt())
  1058. {
  1059. _STK--;
  1060. ip++;
  1061. break;
  1062. }
  1063. ip = code[ip];
  1064. break;
  1065. case OP_JMP:
  1066. ip = code[ip];
  1067. break;
  1068. case OP_RETURN_VOID:
  1069. {
  1070. if (iterDepth > 0)
  1071. {
  1072. // Clear iterator state.
  1073. while (iterDepth > 0)
  1074. {
  1075. iterStack[--_ITER].mIsStringIter = false;
  1076. --iterDepth;
  1077. _STK--; // this is a pop from foreach()
  1078. }
  1079. }
  1080. returnValue.setEmptyString();
  1081. goto execFinished;
  1082. }
  1083. case OP_RETURN:
  1084. {
  1085. returnValue = std::move(stack[_STK]);
  1086. _STK--;
  1087. // Clear iterator state.
  1088. while (iterDepth > 0)
  1089. {
  1090. iterStack[--_ITER].mIsStringIter = false;
  1091. --iterDepth;
  1092. _STK--;
  1093. }
  1094. goto execFinished;
  1095. }
  1096. case OP_RETURN_FLT:
  1097. returnValue.setFloat(stack[_STK].getFloat());
  1098. _STK--;
  1099. // Clear iterator state.
  1100. while (iterDepth > 0)
  1101. {
  1102. iterStack[--_ITER].mIsStringIter = false;
  1103. --iterDepth;
  1104. _STK--;
  1105. }
  1106. goto execFinished;
  1107. case OP_RETURN_UINT:
  1108. returnValue.setInt(stack[_STK].getInt());
  1109. _STK--;
  1110. // Clear iterator state.
  1111. while (iterDepth > 0)
  1112. {
  1113. iterStack[--_ITER].mIsStringIter = false;
  1114. --iterDepth;
  1115. _STK--;
  1116. }
  1117. goto execFinished;
  1118. case OP_CMPEQ:
  1119. doFloatMathOperation<FloatOperation::EQ>();
  1120. break;
  1121. case OP_CMPGR:
  1122. doFloatMathOperation<FloatOperation::GR>();
  1123. break;
  1124. case OP_CMPGE:
  1125. doFloatMathOperation<FloatOperation::GE>();
  1126. break;
  1127. case OP_CMPLT:
  1128. doFloatMathOperation<FloatOperation::LT>();
  1129. break;
  1130. case OP_CMPLE:
  1131. doFloatMathOperation<FloatOperation::LE>();
  1132. break;
  1133. case OP_CMPNE:
  1134. doFloatMathOperation<FloatOperation::NE>();
  1135. break;
  1136. case OP_XOR:
  1137. doIntOperation<IntegerOperation::Xor>();
  1138. break;
  1139. case OP_BITAND:
  1140. doIntOperation<IntegerOperation::BitAnd>();
  1141. break;
  1142. case OP_BITOR:
  1143. doIntOperation<IntegerOperation::BitOr>();
  1144. break;
  1145. case OP_NOT:
  1146. stack[_STK].setInt(!stack[_STK].getInt());
  1147. break;
  1148. case OP_NOTF:
  1149. stack[_STK].setInt(!stack[_STK].getFloat());
  1150. break;
  1151. case OP_ONESCOMPLEMENT:
  1152. stack[_STK].setInt(~stack[_STK].getInt());
  1153. break;
  1154. case OP_SHR:
  1155. doIntOperation<IntegerOperation::RShift>();
  1156. break;
  1157. case OP_SHL:
  1158. doIntOperation<IntegerOperation::LShift>();
  1159. break;
  1160. case OP_AND:
  1161. doIntOperation<IntegerOperation::LogicalAnd>();
  1162. break;
  1163. case OP_OR:
  1164. doIntOperation<IntegerOperation::LogicalOr>();
  1165. break;
  1166. case OP_ADD:
  1167. doFloatMathOperation<FloatOperation::Add>();
  1168. break;
  1169. case OP_SUB:
  1170. doFloatMathOperation<FloatOperation::Sub>();
  1171. break;
  1172. case OP_MUL:
  1173. doFloatMathOperation<FloatOperation::Mul>();
  1174. break;
  1175. case OP_DIV:
  1176. doFloatMathOperation<FloatOperation::Div>();
  1177. break;
  1178. case OP_MOD:
  1179. {
  1180. S64 divisor = stack[_STK - 1].getInt();
  1181. if (divisor != 0)
  1182. stack[_STK - 1].setInt(stack[_STK].getInt() % divisor);
  1183. else
  1184. stack[_STK - 1].setInt(0);
  1185. _STK--;
  1186. break;
  1187. }
  1188. case OP_NEG:
  1189. stack[_STK].setFloat(-stack[_STK].getFloat());
  1190. break;
  1191. case OP_INC:
  1192. reg = code[ip++];
  1193. currentRegister = reg;
  1194. gEvalState.setLocalFloatVariable(reg, gEvalState.getLocalFloatVariable(reg) + 1.0);
  1195. break;
  1196. case OP_SETCURVAR:
  1197. var = CodeToSTE(code, ip);
  1198. ip += 2;
  1199. // If a variable is set, then these must be NULL. It is necessary
  1200. // to set this here so that the vector parser can appropriately
  1201. // identify whether it's dealing with a vector.
  1202. prevField = NULL;
  1203. prevObject = NULL;
  1204. curObject = NULL;
  1205. // Used for local variable caching of what is active...when we
  1206. // set a global, we aren't active
  1207. currentRegister = -1;
  1208. gEvalState.setCurVarName(var);
  1209. // In order to let docblocks work properly with variables, we have
  1210. // clear the current docblock when we do an assign. This way it
  1211. // won't inappropriately carry forward to following function decls.
  1212. curFNDocBlock = NULL;
  1213. curNSDocBlock = NULL;
  1214. break;
  1215. case OP_SETCURVAR_CREATE:
  1216. var = CodeToSTE(code, ip);
  1217. ip += 2;
  1218. // See OP_SETCURVAR
  1219. prevField = NULL;
  1220. prevObject = NULL;
  1221. curObject = NULL;
  1222. // Used for local variable caching of what is active...when we
  1223. // set a global, we aren't active
  1224. currentRegister = -1;
  1225. gEvalState.setCurVarNameCreate(var);
  1226. // See OP_SETCURVAR for why we do this.
  1227. curFNDocBlock = NULL;
  1228. curNSDocBlock = NULL;
  1229. break;
  1230. case OP_SETCURVAR_ARRAY:
  1231. var = StringTable->insert(stack[_STK].getString());
  1232. // See OP_SETCURVAR
  1233. prevField = NULL;
  1234. prevObject = NULL;
  1235. curObject = NULL;
  1236. // Used for local variable caching of what is active...when we
  1237. // set a global, we aren't active
  1238. currentRegister = -1;
  1239. gEvalState.setCurVarName(var);
  1240. // See OP_SETCURVAR for why we do this.
  1241. curFNDocBlock = NULL;
  1242. curNSDocBlock = NULL;
  1243. break;
  1244. case OP_SETCURVAR_ARRAY_CREATE:
  1245. var = StringTable->insert(stack[_STK].getString());
  1246. // See OP_SETCURVAR
  1247. prevField = NULL;
  1248. prevObject = NULL;
  1249. curObject = NULL;
  1250. // Used for local variable caching of what is active...when we
  1251. // set a global, we aren't active
  1252. currentRegister = -1;
  1253. gEvalState.setCurVarNameCreate(var);
  1254. // See OP_SETCURVAR for why we do this.
  1255. curFNDocBlock = NULL;
  1256. curNSDocBlock = NULL;
  1257. break;
  1258. case OP_LOADVAR_UINT:
  1259. currentRegister = -1;
  1260. stack[_STK + 1].setInt(gEvalState.getIntVariable());
  1261. _STK++;
  1262. break;
  1263. case OP_LOADVAR_FLT:
  1264. currentRegister = -1;
  1265. stack[_STK + 1].setFloat(gEvalState.getFloatVariable());
  1266. _STK++;
  1267. break;
  1268. case OP_LOADVAR_STR:
  1269. currentRegister = -1;
  1270. stack[_STK + 1].setString(gEvalState.getStringVariable());
  1271. _STK++;
  1272. break;
  1273. case OP_SAVEVAR_UINT:
  1274. gEvalState.setIntVariable(stack[_STK].getInt());
  1275. break;
  1276. case OP_SAVEVAR_FLT:
  1277. gEvalState.setFloatVariable(stack[_STK].getFloat());
  1278. break;
  1279. case OP_SAVEVAR_STR:
  1280. gEvalState.setStringVariable(stack[_STK].getString());
  1281. break;
  1282. case OP_LOAD_LOCAL_VAR_UINT:
  1283. reg = code[ip++];
  1284. currentRegister = reg;
  1285. // See OP_SETCURVAR
  1286. prevField = NULL;
  1287. prevObject = NULL;
  1288. curObject = NULL;
  1289. stack[_STK + 1].setInt(gEvalState.getLocalIntVariable(reg));
  1290. _STK++;
  1291. break;
  1292. case OP_LOAD_LOCAL_VAR_FLT:
  1293. reg = code[ip++];
  1294. currentRegister = reg;
  1295. // See OP_SETCURVAR
  1296. prevField = NULL;
  1297. prevObject = NULL;
  1298. curObject = NULL;
  1299. stack[_STK + 1].setFloat(gEvalState.getLocalFloatVariable(reg));
  1300. _STK++;
  1301. break;
  1302. case OP_LOAD_LOCAL_VAR_STR:
  1303. reg = code[ip++];
  1304. currentRegister = reg;
  1305. // See OP_SETCURVAR
  1306. prevField = NULL;
  1307. prevObject = NULL;
  1308. curObject = NULL;
  1309. val = gEvalState.getLocalStringVariable(reg);
  1310. stack[_STK + 1].setString(val);
  1311. _STK++;
  1312. break;
  1313. case OP_SAVE_LOCAL_VAR_UINT:
  1314. reg = code[ip++];
  1315. currentRegister = reg;
  1316. // See OP_SETCURVAR
  1317. prevField = NULL;
  1318. prevObject = NULL;
  1319. curObject = NULL;
  1320. gEvalState.setLocalIntVariable(reg, stack[_STK].getInt());
  1321. break;
  1322. case OP_SAVE_LOCAL_VAR_FLT:
  1323. reg = code[ip++];
  1324. currentRegister = reg;
  1325. // See OP_SETCURVAR
  1326. prevField = NULL;
  1327. prevObject = NULL;
  1328. curObject = NULL;
  1329. gEvalState.setLocalFloatVariable(reg, stack[_STK].getFloat());
  1330. break;
  1331. case OP_SAVE_LOCAL_VAR_STR:
  1332. reg = code[ip++];
  1333. val = stack[_STK].getString();
  1334. currentRegister = reg;
  1335. // See OP_SETCURVAR
  1336. prevField = NULL;
  1337. prevObject = NULL;
  1338. curObject = NULL;
  1339. gEvalState.setLocalStringVariable(reg, val, (S32)dStrlen(val));
  1340. break;
  1341. case OP_SETCUROBJECT:
  1342. // Save the previous object for parsing vector fields.
  1343. prevObject = curObject;
  1344. val = stack[_STK].getString();
  1345. // Sim::findObject will sometimes find valid objects from
  1346. // multi-component strings. This makes sure that doesn't
  1347. // happen.
  1348. for (const char* check = val; *check; check++)
  1349. {
  1350. if (*check == ' ')
  1351. {
  1352. val = "";
  1353. break;
  1354. }
  1355. }
  1356. curObject = Sim::findObject(val);
  1357. break;
  1358. case OP_SETCUROBJECT_INTERNAL:
  1359. ++ip; // To skip the recurse flag if the object wasnt found
  1360. if (curObject)
  1361. {
  1362. SimGroup* group = dynamic_cast<SimGroup*>(curObject);
  1363. if (group)
  1364. {
  1365. StringTableEntry intName = StringTable->insert(stack[_STK].getString());
  1366. bool recurse = code[ip - 1];
  1367. SimObject* obj = group->findObjectByInternalName(intName, recurse);
  1368. stack[_STK].setInt(obj ? obj->getId() : 0);
  1369. }
  1370. else
  1371. {
  1372. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-group %s of class %s.", getFileLine(ip - 2), curObject->getName(), curObject->getClassName());
  1373. stack[_STK].setInt(0);
  1374. }
  1375. }
  1376. else
  1377. {
  1378. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use ->, but the group object wasn't found.", getFileLine(ip - 2));
  1379. stack[_STK].setInt(0);
  1380. }
  1381. break;
  1382. case OP_SETCUROBJECT_NEW:
  1383. curObject = currentNewObject;
  1384. break;
  1385. case OP_SETCURFIELD:
  1386. // Save the previous field for parsing vector fields.
  1387. prevField = curField;
  1388. dStrcpy(prevFieldArray, curFieldArray, 256);
  1389. curField = CodeToSTE(code, ip);
  1390. curFieldArray[0] = 0;
  1391. ip += 2;
  1392. break;
  1393. case OP_SETCURFIELD_ARRAY:
  1394. dStrcpy(curFieldArray, stack[_STK].getString(), 256);
  1395. break;
  1396. case OP_SETCURFIELD_TYPE:
  1397. if(curObject)
  1398. curObject->setDataFieldType(code[ip], curField, curFieldArray);
  1399. ip++;
  1400. break;
  1401. case OP_LOADFIELD_UINT:
  1402. if (curObject)
  1403. stack[_STK + 1].setInt(dAtol(curObject->getDataField(curField, curFieldArray)));
  1404. else
  1405. {
  1406. // The field is not being retrieved from an object. Maybe it's
  1407. // a special accessor?
  1408. char buff[FieldBufferSizeNumeric];
  1409. memset(buff, 0, sizeof(buff));
  1410. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff, currentRegister);
  1411. stack[_STK + 1].setInt(dAtol(buff));
  1412. }
  1413. _STK++;
  1414. break;
  1415. case OP_LOADFIELD_FLT:
  1416. if (curObject)
  1417. stack[_STK + 1].setFloat(dAtod(curObject->getDataField(curField, curFieldArray)));
  1418. else
  1419. {
  1420. // The field is not being retrieved from an object. Maybe it's
  1421. // a special accessor?
  1422. char buff[FieldBufferSizeNumeric];
  1423. memset(buff, 0, sizeof(buff));
  1424. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff, currentRegister);
  1425. stack[_STK + 1].setFloat(dAtod(buff));
  1426. }
  1427. _STK++;
  1428. break;
  1429. case OP_LOADFIELD_STR:
  1430. if (curObject)
  1431. {
  1432. val = curObject->getDataField(curField, curFieldArray);
  1433. stack[_STK + 1].setString(val);
  1434. }
  1435. else
  1436. {
  1437. // The field is not being retrieved from an object. Maybe it's
  1438. // a special accessor?
  1439. char buff[FieldBufferSizeString];
  1440. memset(buff, 0, sizeof(buff));
  1441. getFieldComponent(prevObject, prevField, prevFieldArray, curField, buff, currentRegister);
  1442. stack[_STK + 1].setString(buff);
  1443. }
  1444. _STK++;
  1445. break;
  1446. case OP_SAVEFIELD_UINT:
  1447. if (curObject)
  1448. curObject->setDataField(curField, curFieldArray, stack[_STK].getString());
  1449. else
  1450. {
  1451. // The field is not being set on an object. Maybe it's a special accessor?
  1452. setFieldComponent(prevObject, prevField, prevFieldArray, curField, currentRegister);
  1453. prevObject = NULL;
  1454. }
  1455. break;
  1456. case OP_SAVEFIELD_FLT:
  1457. if (curObject)
  1458. curObject->setDataField(curField, curFieldArray, stack[_STK].getString());
  1459. else
  1460. {
  1461. // The field is not being set on an object. Maybe it's a special accessor?
  1462. setFieldComponent(prevObject, prevField, prevFieldArray, curField, currentRegister);
  1463. prevObject = NULL;
  1464. }
  1465. break;
  1466. case OP_SAVEFIELD_STR:
  1467. if (curObject)
  1468. curObject->setDataField(curField, curFieldArray, stack[_STK].getString());
  1469. else
  1470. {
  1471. // The field is not being set on an object. Maybe it's a special accessor?
  1472. setFieldComponent(prevObject, prevField, prevFieldArray, curField, currentRegister);
  1473. prevObject = NULL;
  1474. }
  1475. break;
  1476. case OP_POP_STK:
  1477. _STK--;
  1478. break;
  1479. case OP_LOADIMMED_UINT:
  1480. stack[_STK + 1].setInt(code[ip++]);
  1481. _STK++;
  1482. break;
  1483. case OP_LOADIMMED_FLT:
  1484. stack[_STK + 1].setFloat(curFloatTable[code[ip++]]);
  1485. _STK++;
  1486. break;
  1487. case OP_TAG_TO_STR:
  1488. code[ip - 1] = OP_LOADIMMED_STR;
  1489. // it's possible the string has already been converted
  1490. if (U8(curStringTable[code[ip]]) != StringTagPrefixByte)
  1491. {
  1492. U32 id = GameAddTaggedString(curStringTable + code[ip]);
  1493. dSprintf(curStringTable + code[ip] + 1, 7, "%d", id);
  1494. *(curStringTable + code[ip]) = StringTagPrefixByte;
  1495. }
  1496. TORQUE_CASE_FALLTHROUGH;
  1497. case OP_LOADIMMED_STR:
  1498. stack[_STK + 1].setString(curStringTable + code[ip++]);
  1499. _STK ++;
  1500. break;
  1501. case OP_DOCBLOCK_STR:
  1502. {
  1503. // If the first word of the doc is '\class' or '@class', then this
  1504. // is a namespace doc block, otherwise it is a function doc block.
  1505. const char* docblock = curStringTable + code[ip++];
  1506. const char* sansClass = dStrstr(docblock, "@class");
  1507. if (!sansClass)
  1508. sansClass = dStrstr(docblock, "\\class");
  1509. if (sansClass)
  1510. {
  1511. // Don't save the class declaration. Scan past the 'class'
  1512. // keyword and up to the first whitespace.
  1513. sansClass += 7;
  1514. S32 index = 0;
  1515. while ((*sansClass != ' ') && (*sansClass != '\n') && *sansClass && (index < (nsDocLength - 1)))
  1516. {
  1517. nsDocBlockClass[index++] = *sansClass;
  1518. sansClass++;
  1519. }
  1520. nsDocBlockClass[index] = '\0';
  1521. curNSDocBlock = sansClass + 1;
  1522. }
  1523. else
  1524. curFNDocBlock = docblock;
  1525. }
  1526. break;
  1527. case OP_LOADIMMED_IDENT:
  1528. stack[_STK + 1].setString(CodeToSTE(code, ip));
  1529. _STK++;
  1530. ip += 2;
  1531. break;
  1532. case OP_CALLFUNC:
  1533. {
  1534. // This routingId is set when we query the object as to whether
  1535. // it handles this method. It is set to an enum from the table
  1536. // above indicating whether it handles it on a component it owns
  1537. // or just on the object.
  1538. S32 routingId = 0;
  1539. fnName = CodeToSTE(code, ip);
  1540. fnNamespace = CodeToSTE(code, ip + 2);
  1541. U32 callType = code[ip + 4];
  1542. //if this is called from inside a function, append the ip and codeptr
  1543. if (!gEvalState.stack.empty())
  1544. {
  1545. gEvalState.getCurrentFrame().code = this;
  1546. gEvalState.getCurrentFrame().ip = ip - 1;
  1547. }
  1548. ip += 5;
  1549. gCallStack.argvc(fnName, callArgc, &callArgv);
  1550. if (callType == FuncCallExprNode::FunctionCall)
  1551. {
  1552. // Note: This works even if the function was in a package. Reason being is when
  1553. // activatePackage() is called, it swaps the namespaceEntry into the global namespace
  1554. // (and reverts it when deactivatePackage is called). Method or Static related ones work
  1555. // as expected, as the namespace is resolved on the fly.
  1556. nsEntry = Namespace::global()->lookup(fnName);
  1557. if (!nsEntry)
  1558. {
  1559. Con::warnf(ConsoleLogEntry::General,
  1560. "%s: Unable to find function %s",
  1561. getFileLine(ip - 4), fnName);
  1562. gCallStack.popFrame();
  1563. stack[_STK + 1].setEmptyString();
  1564. _STK++;
  1565. break;
  1566. }
  1567. }
  1568. else if (callType == FuncCallExprNode::StaticCall)
  1569. {
  1570. // Try to look it up.
  1571. ns = Namespace::find(fnNamespace);
  1572. nsEntry = ns->lookup(fnName);
  1573. if (!nsEntry)
  1574. {
  1575. Con::warnf(ConsoleLogEntry::General,
  1576. "%s: Unable to find function %s%s%s",
  1577. getFileLine(ip - 4), fnNamespace ? fnNamespace : "",
  1578. fnNamespace ? "::" : "", fnName);
  1579. gCallStack.popFrame();
  1580. stack[_STK + 1].setEmptyString();
  1581. _STK++;
  1582. break;
  1583. }
  1584. }
  1585. else if (callType == FuncCallExprNode::MethodCall)
  1586. {
  1587. saveObject = gEvalState.thisObject;
  1588. // Optimization: If we're an integer, we can lookup the value by SimObjectId
  1589. const ConsoleValue& simObjectLookupValue = callArgv[1];
  1590. if (simObjectLookupValue.getType() == ConsoleValueType::cvInteger)
  1591. gEvalState.thisObject = Sim::findObject(static_cast<SimObjectId>(simObjectLookupValue.getFastInt()));
  1592. else
  1593. {
  1594. SimObject *foundObject = Sim::findObject(simObjectLookupValue.getString());
  1595. // Optimization: If we're not an integer, let's make it so that the fast path exists
  1596. // on the first argument of the method call (speeds up future usage of %this, for example)
  1597. if (foundObject != NULL)
  1598. callArgv[1].setInt(static_cast<S64>(foundObject->getId()));
  1599. gEvalState.thisObject = foundObject;
  1600. }
  1601. if (gEvalState.thisObject == NULL)
  1602. {
  1603. Con::warnf(
  1604. ConsoleLogEntry::General,
  1605. "%s: Unable to find object: '%s' attempting to call function '%s'",
  1606. getFileLine(ip - 6),
  1607. simObjectLookupValue.getString(),
  1608. fnName
  1609. );
  1610. gCallStack.popFrame();
  1611. stack[_STK + 1].setEmptyString();
  1612. _STK++;
  1613. break;
  1614. }
  1615. ns = gEvalState.thisObject->getNamespace();
  1616. if (ns)
  1617. nsEntry = ns->lookup(fnName);
  1618. else
  1619. nsEntry = NULL;
  1620. }
  1621. else // it's a ParentCall
  1622. {
  1623. if (thisNamespace)
  1624. {
  1625. ns = thisNamespace->mParent;
  1626. if (ns)
  1627. nsEntry = ns->lookup(fnName);
  1628. else
  1629. nsEntry = NULL;
  1630. }
  1631. else
  1632. {
  1633. ns = NULL;
  1634. nsEntry = NULL;
  1635. }
  1636. }
  1637. if (!nsEntry || noCalls)
  1638. {
  1639. if (!noCalls)
  1640. {
  1641. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", getFileLine(ip - 4), fnName);
  1642. if (callType == FuncCallExprNode::MethodCall)
  1643. {
  1644. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1645. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1646. gEvalState.thisObject->getId(), Con::getNamespaceList(ns));
  1647. }
  1648. }
  1649. gCallStack.popFrame();
  1650. stack[_STK + 1].setEmptyString();
  1651. _STK++;
  1652. break;
  1653. }
  1654. if (nsEntry->mType == Namespace::Entry::ConsoleFunctionType)
  1655. {
  1656. if (nsEntry->mFunctionOffset)
  1657. {
  1658. ConsoleValue returnFromFn = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage);
  1659. stack[_STK + 1] = std::move(returnFromFn);
  1660. }
  1661. else // no body
  1662. stack[_STK + 1].setEmptyString();
  1663. _STK++;
  1664. gCallStack.popFrame();
  1665. }
  1666. else
  1667. {
  1668. if ((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1669. {
  1670. const char* nsName = ns ? ns->mName : "";
  1671. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments.", getFileLine(ip - 4), nsName, fnName);
  1672. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip - 4), nsEntry->mUsage);
  1673. gCallStack.popFrame();
  1674. stack[_STK + 1].setEmptyString();
  1675. _STK++;
  1676. }
  1677. else
  1678. {
  1679. switch (nsEntry->mType)
  1680. {
  1681. case Namespace::Entry::StringCallbackType:
  1682. {
  1683. const char* result = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1684. gCallStack.popFrame();
  1685. stack[_STK + 1].setString(result);
  1686. _STK++;
  1687. break;
  1688. }
  1689. case Namespace::Entry::IntCallbackType:
  1690. {
  1691. S64 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1692. gCallStack.popFrame();
  1693. if (code[ip] == OP_POP_STK)
  1694. {
  1695. ip++;
  1696. break;
  1697. }
  1698. stack[_STK + 1].setInt(result);
  1699. _STK++;
  1700. break;
  1701. }
  1702. case Namespace::Entry::FloatCallbackType:
  1703. {
  1704. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1705. gCallStack.popFrame();
  1706. if (code[ip] == OP_POP_STK)
  1707. {
  1708. ip++;
  1709. break;
  1710. }
  1711. stack[_STK + 1].setFloat(result);
  1712. _STK++;
  1713. break;
  1714. }
  1715. case Namespace::Entry::VoidCallbackType:
  1716. {
  1717. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1718. gCallStack.popFrame();
  1719. if (code[ip] == OP_POP_STK)
  1720. {
  1721. ip++;
  1722. break;
  1723. }
  1724. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip - 4), fnName, functionName);
  1725. stack[_STK + 1].setEmptyString();
  1726. _STK++;
  1727. break;
  1728. }
  1729. case Namespace::Entry::BoolCallbackType:
  1730. {
  1731. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1732. gCallStack.popFrame();
  1733. if (code[ip] == OP_POP_STK)
  1734. {
  1735. ip++;
  1736. break;
  1737. }
  1738. stack[_STK + 1].setBool(result);
  1739. _STK++;
  1740. break;
  1741. }
  1742. }
  1743. }
  1744. }
  1745. if (callType == FuncCallExprNode::MethodCall)
  1746. gEvalState.thisObject = saveObject;
  1747. break;
  1748. }
  1749. case OP_ADVANCE_STR_APPENDCHAR:
  1750. {
  1751. char buff[2];
  1752. buff[0] = (char)code[ip++];
  1753. buff[1] = '\0';
  1754. S32 len;
  1755. const char* concat = tsconcat(stack[_STK].getString(), buff, len);
  1756. stack[_STK].setStringRef(concat, len);
  1757. break;
  1758. }
  1759. case OP_REWIND_STR:
  1760. TORQUE_CASE_FALLTHROUGH;
  1761. case OP_TERMINATE_REWIND_STR:
  1762. {
  1763. S32 len;
  1764. const char* concat = tsconcat(stack[_STK - 1].getString(), stack[_STK].getString(), len);
  1765. stack[_STK - 1].setStringRef(concat, len);
  1766. _STK--;
  1767. break;
  1768. }
  1769. case OP_COMPARE_STR:
  1770. stack[_STK - 1].setBool(!dStricmp(stack[_STK].getString(), stack[_STK - 1].getString()));
  1771. _STK--;
  1772. break;
  1773. case OP_PUSH:
  1774. gCallStack.push(std::move(stack[_STK--]));
  1775. break;
  1776. case OP_PUSH_FRAME:
  1777. gCallStack.pushFrame(code[ip++]);
  1778. break;
  1779. case OP_ASSERT:
  1780. {
  1781. if (!stack[_STK--].getBool())
  1782. {
  1783. const char* message = curStringTable + code[ip];
  1784. U32 breakLine, inst;
  1785. findBreakLine(ip - 1, breakLine, inst);
  1786. if (PlatformAssert::processAssert(PlatformAssert::Fatal,
  1787. name ? name : "eval",
  1788. breakLine,
  1789. message))
  1790. {
  1791. if (TelDebugger && TelDebugger->isConnected() && breakLine > 0)
  1792. {
  1793. TelDebugger->breakProcess();
  1794. }
  1795. else
  1796. Platform::debugBreak();
  1797. }
  1798. }
  1799. ip++;
  1800. break;
  1801. }
  1802. case OP_BREAK:
  1803. {
  1804. //append the ip and codeptr before managing the breakpoint!
  1805. AssertFatal(!gEvalState.stack.empty(), "Empty eval stack on break!");
  1806. gEvalState.getCurrentFrame().code = this;
  1807. gEvalState.getCurrentFrame().ip = ip - 1;
  1808. U32 breakLine;
  1809. findBreakLine(ip - 1, breakLine, instruction);
  1810. if (!breakLine)
  1811. goto breakContinue;
  1812. TelDebugger->executionStopped(this, breakLine);
  1813. goto breakContinue;
  1814. }
  1815. case OP_ITER_BEGIN_STR:
  1816. {
  1817. iterStack[_ITER].mIsStringIter = true;
  1818. TORQUE_CASE_FALLTHROUGH;
  1819. }
  1820. case OP_ITER_BEGIN:
  1821. {
  1822. bool isGlobal = code[ip];
  1823. U32 failIp = code[ip + (isGlobal ? 3 : 2)];
  1824. IterStackRecord& iter = iterStack[_ITER];
  1825. iter.mIsGlobalVariable = isGlobal;
  1826. if (isGlobal)
  1827. {
  1828. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1829. iter.mVar.mVariable = gEvalState.globalVars.add(varName);
  1830. }
  1831. else
  1832. {
  1833. iter.mVar.mRegister = code[ip + 1];
  1834. }
  1835. if (iter.mIsStringIter)
  1836. {
  1837. iter.mData.mStr.mString = stack[_STK].getString();
  1838. iter.mData.mStr.mIndex = 0;
  1839. }
  1840. else
  1841. {
  1842. // Look up the object.
  1843. SimSet* set;
  1844. if (!Sim::findObject(stack[_STK].getString(), set))
  1845. {
  1846. Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", stack[_STK].getString());
  1847. Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
  1848. ip = failIp;
  1849. continue;
  1850. }
  1851. // Set up.
  1852. iter.mData.mObj.mSet = set;
  1853. iter.mData.mObj.mIndex = 0;
  1854. }
  1855. _ITER++;
  1856. iterDepth++;
  1857. ip += isGlobal ? 4 : 3;
  1858. break;
  1859. }
  1860. case OP_ITER:
  1861. {
  1862. U32 breakIp = code[ip];
  1863. IterStackRecord& iter = iterStack[_ITER - 1];
  1864. if (iter.mIsStringIter)
  1865. {
  1866. const char* str = iter.mData.mStr.mString;
  1867. U32 startIndex = iter.mData.mStr.mIndex;
  1868. U32 endIndex = startIndex;
  1869. // Break if at end.
  1870. if (!str[startIndex])
  1871. {
  1872. ip = breakIp;
  1873. continue;
  1874. }
  1875. // Find right end of current component.
  1876. if (!dIsspace(str[endIndex]))
  1877. do ++endIndex;
  1878. while (str[endIndex] && !dIsspace(str[endIndex]));
  1879. // Extract component.
  1880. if (endIndex != startIndex)
  1881. {
  1882. char savedChar = str[endIndex];
  1883. const_cast<char*>(str)[endIndex] = '\0'; // We are on the string stack so this is okay.
  1884. if (iter.mIsGlobalVariable)
  1885. iter.mVar.mVariable->setStringValue(&str[startIndex]);
  1886. else
  1887. gEvalState.setLocalStringVariable(iter.mVar.mRegister, &str[startIndex], endIndex - startIndex);
  1888. const_cast<char*>(str)[endIndex] = savedChar;
  1889. }
  1890. else
  1891. {
  1892. if (iter.mIsGlobalVariable)
  1893. iter.mVar.mVariable->setStringValue("");
  1894. else
  1895. gEvalState.setLocalStringVariable(iter.mVar.mRegister, "", 0);
  1896. }
  1897. // Skip separator.
  1898. if (str[endIndex] != '\0')
  1899. ++endIndex;
  1900. iter.mData.mStr.mIndex = endIndex;
  1901. }
  1902. else
  1903. {
  1904. U32 index = iter.mData.mObj.mIndex;
  1905. SimSet* set = iter.mData.mObj.mSet;
  1906. if (index >= set->size())
  1907. {
  1908. ip = breakIp;
  1909. continue;
  1910. }
  1911. SimObjectId id = set->at(index)->getId();
  1912. if (iter.mIsGlobalVariable)
  1913. iter.mVar.mVariable->setIntValue(id);
  1914. else
  1915. gEvalState.setLocalIntVariable(iter.mVar.mRegister, id);
  1916. iter.mData.mObj.mIndex = index + 1;
  1917. }
  1918. ++ip;
  1919. break;
  1920. }
  1921. case OP_ITER_END:
  1922. {
  1923. --_ITER;
  1924. --iterDepth;
  1925. _STK--;
  1926. iterStack[_ITER].mIsStringIter = false;
  1927. break;
  1928. }
  1929. case OP_INVALID:
  1930. TORQUE_CASE_FALLTHROUGH;
  1931. default:
  1932. // error!
  1933. AssertISV(false, "Invalid OPCode Processed!");
  1934. goto execFinished;
  1935. }
  1936. }
  1937. execFinished:
  1938. if (telDebuggerOn && setFrame < 0)
  1939. TelDebugger->popStackFrame();
  1940. if (popFrame)
  1941. {
  1942. gEvalState.popFrame();
  1943. }
  1944. if (argv)
  1945. {
  1946. if (gEvalState.traceOn)
  1947. {
  1948. traceBuffer[0] = 0;
  1949. dStrcat(traceBuffer, "Leaving ", TRACE_BUFFER_SIZE);
  1950. if (packageName)
  1951. {
  1952. dStrcat(traceBuffer, "[", TRACE_BUFFER_SIZE);
  1953. dStrcat(traceBuffer, packageName, TRACE_BUFFER_SIZE);
  1954. dStrcat(traceBuffer, "]", TRACE_BUFFER_SIZE);
  1955. }
  1956. if (thisNamespace && thisNamespace->mName)
  1957. {
  1958. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1959. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, returnValue.getString());
  1960. }
  1961. else
  1962. {
  1963. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1964. "%s() - return %s", thisFunctionName, returnValue.getString());
  1965. }
  1966. Con::printf("%s", traceBuffer);
  1967. }
  1968. }
  1969. else
  1970. {
  1971. delete[] const_cast<char*>(globalStrings);
  1972. delete[] globalFloats;
  1973. globalStrings = NULL;
  1974. globalFloats = NULL;
  1975. }
  1976. smCurrentCodeBlock = saveCodeBlock;
  1977. if (saveCodeBlock && saveCodeBlock->name)
  1978. {
  1979. Con::gCurrentFile = saveCodeBlock->name;
  1980. Con::gCurrentRoot = saveCodeBlock->modPath;
  1981. }
  1982. decRefCount();
  1983. #ifdef TORQUE_DEBUG
  1984. AssertFatal(!(_STK > stackStart), "String stack not popped enough in script exec");
  1985. AssertFatal(!(_STK < stackStart), "String stack popped too much in script exec");
  1986. #endif
  1987. return std::move(returnValue);
  1988. }
  1989. //------------------------------------------------------------