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