compiledEval.cpp 75 KB

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