compiledEval.cpp 67 KB

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