compiledEval.cpp 73 KB

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