compiledEval.cpp 73 KB

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