compiledEval.cpp 70 KB

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