compiledEval.cpp 71 KB

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