compiledEval.cpp 74 KB

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