compiledEval.cpp 71 KB

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