compiledEval.cpp 70 KB

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