compiledEval.cpp 70 KB

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