compiledEval.cpp 73 KB

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