compiledEval.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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. currentNewObject = NULL;
  748. ip = failJump;
  749. break;
  750. }
  751. // Set the declaration line
  752. currentNewObject->setDeclarationLine(lineNumber);
  753. // Set the file that this object was created in
  754. currentNewObject->setFilename(name);
  755. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  756. if(*objParent)
  757. {
  758. // Find it!
  759. SimObject *parent;
  760. if(Sim::findObject(objParent, parent))
  761. {
  762. // Con::printf(" - Parent object found: %s", parent->getClassName());
  763. currentNewObject->setCopySource( parent );
  764. currentNewObject->assignFieldsFrom( parent );
  765. }
  766. else
  767. {
  768. if ( Con::gObjectCopyFailures == -1 )
  769. Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", getFileLine(ip), objParent, (const char*)callArgv[1]);
  770. else
  771. ++Con::gObjectCopyFailures;
  772. // Fail to create the object.
  773. delete object;
  774. currentNewObject = NULL;
  775. ip = failJump;
  776. break;
  777. }
  778. }
  779. // If a name was passed, assign it.
  780. if( objectName[ 0 ] )
  781. {
  782. if( !isInternal )
  783. currentNewObject->assignName( objectName );
  784. else
  785. currentNewObject->setInternalName( objectName );
  786. // Set the original name
  787. currentNewObject->setOriginalName( objectName );
  788. }
  789. // Prevent stack value corruption
  790. CSTK.pushFrame();
  791. STR.pushFrame();
  792. // --
  793. // Do the constructor parameters.
  794. if(!currentNewObject->processArguments(callArgc-3, callArgv+3))
  795. {
  796. delete currentNewObject;
  797. currentNewObject = NULL;
  798. ip = failJump;
  799. // Prevent stack value corruption
  800. CSTK.popFrame();
  801. STR.popFrame();
  802. // --
  803. break;
  804. }
  805. // Prevent stack value corruption
  806. CSTK.popFrame();
  807. STR.popFrame();
  808. // --
  809. // If it's not a datablock, allow people to modify bits of it.
  810. if(!isDataBlock)
  811. {
  812. currentNewObject->setModStaticFields(true);
  813. currentNewObject->setModDynamicFields(true);
  814. }
  815. }
  816. // Advance the IP past the create info...
  817. ip += 7;
  818. break;
  819. }
  820. case OP_ADD_OBJECT:
  821. {
  822. // See OP_SETCURVAR for why we do this.
  823. curFNDocBlock = NULL;
  824. curNSDocBlock = NULL;
  825. // Do we place this object at the root?
  826. bool placeAtRoot = code[ip++];
  827. // Con::printf("Adding object %s", currentNewObject->getName());
  828. // Prevent stack value corruption
  829. CSTK.pushFrame();
  830. STR.pushFrame();
  831. // --
  832. // Make sure it wasn't already added, then add it.
  833. if(currentNewObject->isProperlyAdded() == false)
  834. {
  835. bool ret = false;
  836. Message *msg = dynamic_cast<Message *>(currentNewObject);
  837. if(msg)
  838. {
  839. SimObjectId id = Message::getNextMessageID();
  840. if(id != 0xffffffff)
  841. ret = currentNewObject->registerObject(id);
  842. else
  843. Con::errorf("%s: No more object IDs available for messages", getFileLine(ip));
  844. }
  845. else
  846. ret = currentNewObject->registerObject();
  847. if(! ret)
  848. {
  849. // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
  850. Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", getFileLine(ip), currentNewObject->getName(), currentNewObject->getClassName());
  851. delete currentNewObject;
  852. currentNewObject = NULL;
  853. ip = failJump;
  854. // Prevent stack value corruption
  855. CSTK.popFrame();
  856. STR.popFrame();
  857. // --
  858. break;
  859. }
  860. }
  861. // Are we dealing with a datablock?
  862. SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(currentNewObject);
  863. static String errorStr;
  864. // If so, preload it.
  865. if(dataBlock && !dataBlock->preload(true, errorStr))
  866. {
  867. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", getFileLine(ip),
  868. currentNewObject->getName(), errorStr.c_str());
  869. dataBlock->deleteObject();
  870. currentNewObject = NULL;
  871. ip = failJump;
  872. // Prevent stack value corruption
  873. CSTK.popFrame();
  874. STR.popFrame();
  875. // --
  876. break;
  877. }
  878. // What group will we be added to, if any?
  879. U32 groupAddId = intStack[_UINT];
  880. SimGroup *grp = NULL;
  881. SimSet *set = NULL;
  882. bool isMessage = dynamic_cast<Message *>(currentNewObject) != NULL;
  883. if(!placeAtRoot || !currentNewObject->getGroup())
  884. {
  885. if(! isMessage)
  886. {
  887. if(! placeAtRoot)
  888. {
  889. // Otherwise just add to the requested group or set.
  890. if(!Sim::findObject(groupAddId, grp))
  891. Sim::findObject(groupAddId, set);
  892. }
  893. if(placeAtRoot)
  894. {
  895. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  896. if( Con::gInstantGroup.isEmpty()
  897. || !Sim::findObject( Con::gInstantGroup, grp ) )
  898. grp = Sim::getRootGroup();
  899. }
  900. }
  901. // If we didn't get a group, then make sure we have a pointer to
  902. // the rootgroup.
  903. if(!grp)
  904. grp = Sim::getRootGroup();
  905. // add to the parent group
  906. grp->addObject(currentNewObject);
  907. // If for some reason the add failed, add the object to the
  908. // root group so it won't leak.
  909. if( !currentNewObject->getGroup() )
  910. Sim::getRootGroup()->addObject( currentNewObject );
  911. // add to any set we might be in
  912. if(set)
  913. set->addObject(currentNewObject);
  914. }
  915. // store the new object's ID on the stack (overwriting the group/set
  916. // id, if one was given, otherwise getting pushed)
  917. if(placeAtRoot)
  918. intStack[_UINT] = currentNewObject->getId();
  919. else
  920. intStack[++_UINT] = currentNewObject->getId();
  921. // Prevent stack value corruption
  922. CSTK.popFrame();
  923. STR.popFrame();
  924. // --
  925. break;
  926. }
  927. case OP_END_OBJECT:
  928. {
  929. // If we're not to be placed at the root, make sure we clean up
  930. // our group reference.
  931. bool placeAtRoot = code[ip++];
  932. if(!placeAtRoot)
  933. _UINT--;
  934. break;
  935. }
  936. case OP_FINISH_OBJECT:
  937. {
  938. if (currentNewObject)
  939. currentNewObject->onPostAdd();
  940. //Assert( objectCreationStackIndex >= 0 );
  941. // Restore the object info from the stack [7/9/2007 Black]
  942. currentNewObject = objectCreationStack[ --objectCreationStackIndex ].newObject;
  943. failJump = objectCreationStack[ objectCreationStackIndex ].failJump;
  944. break;
  945. }
  946. case OP_JMPIFFNOT:
  947. if(floatStack[_FLT--])
  948. {
  949. ip++;
  950. break;
  951. }
  952. ip = code[ip];
  953. break;
  954. case OP_JMPIFNOT:
  955. if(intStack[_UINT--])
  956. {
  957. ip++;
  958. break;
  959. }
  960. ip = code[ip];
  961. break;
  962. case OP_JMPIFF:
  963. if(!floatStack[_FLT--])
  964. {
  965. ip++;
  966. break;
  967. }
  968. ip = code[ip];
  969. break;
  970. case OP_JMPIF:
  971. if(!intStack[_UINT--])
  972. {
  973. ip ++;
  974. break;
  975. }
  976. ip = code[ip];
  977. break;
  978. case OP_JMPIFNOT_NP:
  979. if(intStack[_UINT])
  980. {
  981. _UINT--;
  982. ip++;
  983. break;
  984. }
  985. ip = code[ip];
  986. break;
  987. case OP_JMPIF_NP:
  988. if(!intStack[_UINT])
  989. {
  990. _UINT--;
  991. ip++;
  992. break;
  993. }
  994. ip = code[ip];
  995. break;
  996. case OP_JMP:
  997. ip = code[ip];
  998. break;
  999. // This fixes a bug when not explicitly returning a value.
  1000. case OP_RETURN_VOID:
  1001. STR.setStringValue("");
  1002. // We're falling thru here on purpose.
  1003. case OP_RETURN:
  1004. retValue = STR.getStringValuePtr();
  1005. if( iterDepth > 0 )
  1006. {
  1007. // Clear iterator state.
  1008. while( iterDepth > 0 )
  1009. {
  1010. iterStack[ -- _ITER ].mIsStringIter = false;
  1011. -- iterDepth;
  1012. }
  1013. STR.rewind();
  1014. STR.setStringValue( StringStackPtrRef(retValue).getPtr(&STR) ); // Not nice but works.
  1015. retValue = STR.getStringValuePtr();
  1016. }
  1017. // Previously the return value was on the stack and would be returned using STR.getStringValue().
  1018. // Now though we need to wrap it in a ConsoleValueRef
  1019. returnValue.value = CSTK.pushStringStackPtr(retValue);
  1020. goto execFinished;
  1021. case OP_RETURN_FLT:
  1022. if( iterDepth > 0 )
  1023. {
  1024. // Clear iterator state.
  1025. while( iterDepth > 0 )
  1026. {
  1027. iterStack[ -- _ITER ].mIsStringIter = false;
  1028. -- iterDepth;
  1029. }
  1030. }
  1031. returnValue.value = CSTK.pushFLT(floatStack[_FLT]);
  1032. _FLT--;
  1033. goto execFinished;
  1034. case OP_RETURN_UINT:
  1035. if( iterDepth > 0 )
  1036. {
  1037. // Clear iterator state.
  1038. while( iterDepth > 0 )
  1039. {
  1040. iterStack[ -- _ITER ].mIsStringIter = false;
  1041. -- iterDepth;
  1042. }
  1043. }
  1044. returnValue.value = CSTK.pushUINT(intStack[_UINT]);
  1045. _UINT--;
  1046. goto execFinished;
  1047. case OP_CMPEQ:
  1048. intStack[_UINT+1] = bool(floatStack[_FLT] == floatStack[_FLT-1]);
  1049. _UINT++;
  1050. _FLT -= 2;
  1051. break;
  1052. case OP_CMPGR:
  1053. intStack[_UINT+1] = bool(floatStack[_FLT] > floatStack[_FLT-1]);
  1054. _UINT++;
  1055. _FLT -= 2;
  1056. break;
  1057. case OP_CMPGE:
  1058. intStack[_UINT+1] = bool(floatStack[_FLT] >= floatStack[_FLT-1]);
  1059. _UINT++;
  1060. _FLT -= 2;
  1061. break;
  1062. case OP_CMPLT:
  1063. intStack[_UINT+1] = bool(floatStack[_FLT] < floatStack[_FLT-1]);
  1064. _UINT++;
  1065. _FLT -= 2;
  1066. break;
  1067. case OP_CMPLE:
  1068. intStack[_UINT+1] = bool(floatStack[_FLT] <= floatStack[_FLT-1]);
  1069. _UINT++;
  1070. _FLT -= 2;
  1071. break;
  1072. case OP_CMPNE:
  1073. intStack[_UINT+1] = bool(floatStack[_FLT] != floatStack[_FLT-1]);
  1074. _UINT++;
  1075. _FLT -= 2;
  1076. break;
  1077. case OP_XOR:
  1078. intStack[_UINT-1] = intStack[_UINT] ^ intStack[_UINT-1];
  1079. _UINT--;
  1080. break;
  1081. case OP_MOD:
  1082. if( intStack[_UINT-1] != 0 )
  1083. intStack[_UINT-1] = intStack[_UINT] % intStack[_UINT-1];
  1084. else
  1085. intStack[_UINT-1] = 0;
  1086. _UINT--;
  1087. break;
  1088. case OP_BITAND:
  1089. intStack[_UINT-1] = intStack[_UINT] & intStack[_UINT-1];
  1090. _UINT--;
  1091. break;
  1092. case OP_BITOR:
  1093. intStack[_UINT-1] = intStack[_UINT] | intStack[_UINT-1];
  1094. _UINT--;
  1095. break;
  1096. case OP_NOT:
  1097. intStack[_UINT] = !intStack[_UINT];
  1098. break;
  1099. case OP_NOTF:
  1100. intStack[_UINT+1] = !floatStack[_FLT];
  1101. _FLT--;
  1102. _UINT++;
  1103. break;
  1104. case OP_ONESCOMPLEMENT:
  1105. intStack[_UINT] = ~intStack[_UINT];
  1106. break;
  1107. case OP_SHR:
  1108. intStack[_UINT-1] = intStack[_UINT] >> intStack[_UINT-1];
  1109. _UINT--;
  1110. break;
  1111. case OP_SHL:
  1112. intStack[_UINT-1] = intStack[_UINT] << intStack[_UINT-1];
  1113. _UINT--;
  1114. break;
  1115. case OP_AND:
  1116. intStack[_UINT-1] = intStack[_UINT] && intStack[_UINT-1];
  1117. _UINT--;
  1118. break;
  1119. case OP_OR:
  1120. intStack[_UINT-1] = intStack[_UINT] || intStack[_UINT-1];
  1121. _UINT--;
  1122. break;
  1123. case OP_ADD:
  1124. floatStack[_FLT-1] = floatStack[_FLT] + floatStack[_FLT-1];
  1125. _FLT--;
  1126. break;
  1127. case OP_SUB:
  1128. floatStack[_FLT-1] = floatStack[_FLT] - floatStack[_FLT-1];
  1129. _FLT--;
  1130. break;
  1131. case OP_MUL:
  1132. floatStack[_FLT-1] = floatStack[_FLT] * floatStack[_FLT-1];
  1133. _FLT--;
  1134. break;
  1135. case OP_DIV:
  1136. floatStack[_FLT-1] = floatStack[_FLT] / floatStack[_FLT-1];
  1137. _FLT--;
  1138. break;
  1139. case OP_NEG:
  1140. floatStack[_FLT] = -floatStack[_FLT];
  1141. break;
  1142. case OP_SETCURVAR:
  1143. var = CodeToSTE(code, ip);
  1144. ip += 2;
  1145. // If a variable is set, then these must be NULL. It is necessary
  1146. // to set this here so that the vector parser can appropriately
  1147. // identify whether it's dealing with a vector.
  1148. prevField = NULL;
  1149. prevObject = NULL;
  1150. curObject = NULL;
  1151. gEvalState.setCurVarName(var);
  1152. // In order to let docblocks work properly with variables, we have
  1153. // clear the current docblock when we do an assign. This way it
  1154. // won't inappropriately carry forward to following function decls.
  1155. curFNDocBlock = NULL;
  1156. curNSDocBlock = NULL;
  1157. break;
  1158. case OP_SETCURVAR_CREATE:
  1159. var = CodeToSTE(code, ip);
  1160. ip += 2;
  1161. // See OP_SETCURVAR
  1162. prevField = NULL;
  1163. prevObject = NULL;
  1164. curObject = NULL;
  1165. gEvalState.setCurVarNameCreate(var);
  1166. // See OP_SETCURVAR for why we do this.
  1167. curFNDocBlock = NULL;
  1168. curNSDocBlock = NULL;
  1169. break;
  1170. case OP_SETCURVAR_ARRAY:
  1171. var = STR.getSTValue();
  1172. // See OP_SETCURVAR
  1173. prevField = NULL;
  1174. prevObject = NULL;
  1175. curObject = NULL;
  1176. gEvalState.setCurVarName(var);
  1177. // See OP_SETCURVAR for why we do this.
  1178. curFNDocBlock = NULL;
  1179. curNSDocBlock = NULL;
  1180. break;
  1181. case OP_SETCURVAR_ARRAY_CREATE:
  1182. var = STR.getSTValue();
  1183. // See OP_SETCURVAR
  1184. prevField = NULL;
  1185. prevObject = NULL;
  1186. curObject = NULL;
  1187. gEvalState.setCurVarNameCreate(var);
  1188. // See OP_SETCURVAR for why we do this.
  1189. curFNDocBlock = NULL;
  1190. curNSDocBlock = NULL;
  1191. break;
  1192. case OP_LOADVAR_UINT:
  1193. intStack[_UINT+1] = gEvalState.getIntVariable();
  1194. _UINT++;
  1195. break;
  1196. case OP_LOADVAR_FLT:
  1197. floatStack[_FLT+1] = gEvalState.getFloatVariable();
  1198. _FLT++;
  1199. break;
  1200. case OP_LOADVAR_STR:
  1201. val = gEvalState.getStringVariable();
  1202. STR.setStringValue(val);
  1203. break;
  1204. case OP_LOADVAR_VAR:
  1205. // Sets current source of OP_SAVEVAR_VAR
  1206. gEvalState.copyVariable = gEvalState.currentVariable;
  1207. break;
  1208. case OP_SAVEVAR_UINT:
  1209. gEvalState.setIntVariable(intStack[_UINT]);
  1210. break;
  1211. case OP_SAVEVAR_FLT:
  1212. gEvalState.setFloatVariable(floatStack[_FLT]);
  1213. break;
  1214. case OP_SAVEVAR_STR:
  1215. gEvalState.setStringVariable(STR.getStringValue());
  1216. break;
  1217. case OP_SAVEVAR_VAR:
  1218. // this basically handles %var1 = %var2
  1219. gEvalState.setCopyVariable();
  1220. break;
  1221. case OP_SETCUROBJECT:
  1222. // Save the previous object for parsing vector fields.
  1223. prevObject = curObject;
  1224. val = STR.getStringValue();
  1225. // Sim::findObject will sometimes find valid objects from
  1226. // multi-component strings. This makes sure that doesn't
  1227. // happen.
  1228. for( const char* check = val; *check; check++ )
  1229. {
  1230. if( *check == ' ' )
  1231. {
  1232. val = "";
  1233. break;
  1234. }
  1235. }
  1236. curObject = Sim::findObject(val);
  1237. break;
  1238. case OP_SETCUROBJECT_INTERNAL:
  1239. ++ip; // To skip the recurse flag if the object wasn't found
  1240. if(curObject)
  1241. {
  1242. SimSet *set = dynamic_cast<SimSet *>(curObject);
  1243. if(set)
  1244. {
  1245. StringTableEntry intName = StringTable->insert(STR.getStringValue());
  1246. bool recurse = code[ip-1];
  1247. SimObject *obj = set->findObjectByInternalName(intName, recurse);
  1248. intStack[_UINT+1] = obj ? obj->getId() : 0;
  1249. _UINT++;
  1250. }
  1251. else
  1252. {
  1253. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-set %s of class %s.", getFileLine(ip-2), curObject->getName(), curObject->getClassName());
  1254. intStack[_UINT] = 0;
  1255. }
  1256. }
  1257. break;
  1258. case OP_SETCUROBJECT_NEW:
  1259. curObject = currentNewObject;
  1260. break;
  1261. case OP_SETCURFIELD:
  1262. // Save the previous field for parsing vector fields.
  1263. prevField = curField;
  1264. dStrcpy( prevFieldArray, curFieldArray );
  1265. curField = CodeToSTE(code, ip);
  1266. curFieldArray[0] = 0;
  1267. ip += 2;
  1268. break;
  1269. case OP_SETCURFIELD_ARRAY:
  1270. dStrcpy(curFieldArray, STR.getStringValue());
  1271. break;
  1272. case OP_SETCURFIELD_TYPE:
  1273. if(curObject)
  1274. curObject->setDataFieldType(code[ip], curField, curFieldArray);
  1275. ip++;
  1276. break;
  1277. case OP_LOADFIELD_UINT:
  1278. if(curObject)
  1279. intStack[_UINT+1] = U32(dAtoi(curObject->getDataField(curField, curFieldArray)));
  1280. else
  1281. {
  1282. // The field is not being retrieved from an object. Maybe it's
  1283. // a special accessor?
  1284. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer );
  1285. intStack[_UINT+1] = dAtoi( valBuffer );
  1286. }
  1287. _UINT++;
  1288. break;
  1289. case OP_LOADFIELD_FLT:
  1290. if(curObject)
  1291. floatStack[_FLT+1] = dAtof(curObject->getDataField(curField, curFieldArray));
  1292. else
  1293. {
  1294. // The field is not being retrieved from an object. Maybe it's
  1295. // a special accessor?
  1296. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer );
  1297. floatStack[_FLT+1] = dAtof( valBuffer );
  1298. }
  1299. _FLT++;
  1300. break;
  1301. case OP_LOADFIELD_STR:
  1302. if(curObject)
  1303. {
  1304. val = curObject->getDataField(curField, curFieldArray);
  1305. STR.setStringValue( val );
  1306. }
  1307. else
  1308. {
  1309. // The field is not being retrieved from an object. Maybe it's
  1310. // a special accessor?
  1311. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer );
  1312. STR.setStringValue( valBuffer );
  1313. }
  1314. break;
  1315. case OP_SAVEFIELD_UINT:
  1316. STR.setIntValue(intStack[_UINT]);
  1317. if(curObject)
  1318. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1319. else
  1320. {
  1321. // The field is not being set on an object. Maybe it's
  1322. // a special accessor?
  1323. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1324. prevObject = NULL;
  1325. }
  1326. break;
  1327. case OP_SAVEFIELD_FLT:
  1328. STR.setFloatValue(floatStack[_FLT]);
  1329. if(curObject)
  1330. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1331. else
  1332. {
  1333. // The field is not being set on an object. Maybe it's
  1334. // a special accessor?
  1335. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1336. prevObject = NULL;
  1337. }
  1338. break;
  1339. case OP_SAVEFIELD_STR:
  1340. if(curObject)
  1341. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1342. else
  1343. {
  1344. // The field is not being set on an object. Maybe it's
  1345. // a special accessor?
  1346. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1347. prevObject = NULL;
  1348. }
  1349. break;
  1350. case OP_STR_TO_UINT:
  1351. intStack[_UINT+1] = STR.getIntValue();
  1352. _UINT++;
  1353. break;
  1354. case OP_STR_TO_FLT:
  1355. floatStack[_FLT+1] = STR.getFloatValue();
  1356. _FLT++;
  1357. break;
  1358. case OP_STR_TO_NONE:
  1359. // This exists simply to deal with certain typecast situations.
  1360. break;
  1361. case OP_FLT_TO_UINT:
  1362. intStack[_UINT+1] = (S64)floatStack[_FLT];
  1363. _FLT--;
  1364. _UINT++;
  1365. break;
  1366. case OP_FLT_TO_STR:
  1367. STR.setFloatValue(floatStack[_FLT]);
  1368. _FLT--;
  1369. break;
  1370. case OP_FLT_TO_NONE:
  1371. _FLT--;
  1372. break;
  1373. case OP_UINT_TO_FLT:
  1374. floatStack[_FLT+1] = (F32)intStack[_UINT];
  1375. _UINT--;
  1376. _FLT++;
  1377. break;
  1378. case OP_UINT_TO_STR:
  1379. STR.setIntValue(intStack[_UINT]);
  1380. _UINT--;
  1381. break;
  1382. case OP_UINT_TO_NONE:
  1383. _UINT--;
  1384. break;
  1385. case OP_COPYVAR_TO_NONE:
  1386. gEvalState.copyVariable = NULL;
  1387. break;
  1388. case OP_LOADIMMED_UINT:
  1389. intStack[_UINT+1] = code[ip++];
  1390. _UINT++;
  1391. break;
  1392. case OP_LOADIMMED_FLT:
  1393. floatStack[_FLT+1] = curFloatTable[code[ip]];
  1394. ip++;
  1395. _FLT++;
  1396. break;
  1397. case OP_TAG_TO_STR:
  1398. code[ip-1] = OP_LOADIMMED_STR;
  1399. // it's possible the string has already been converted
  1400. if(U8(curStringTable[code[ip]]) != StringTagPrefixByte)
  1401. {
  1402. U32 id = GameAddTaggedString(curStringTable + code[ip]);
  1403. dSprintf(curStringTable + code[ip] + 1, 7, "%d", id);
  1404. *(curStringTable + code[ip]) = StringTagPrefixByte;
  1405. }
  1406. case OP_LOADIMMED_STR:
  1407. STR.setStringValue(curStringTable + code[ip++]);
  1408. break;
  1409. case OP_DOCBLOCK_STR:
  1410. {
  1411. // If the first word of the doc is '\class' or '@class', then this
  1412. // is a namespace doc block, otherwise it is a function doc block.
  1413. const char* docblock = curStringTable + code[ip++];
  1414. const char* sansClass = dStrstr( docblock, "@class" );
  1415. if( !sansClass )
  1416. sansClass = dStrstr( docblock, "\\class" );
  1417. if( sansClass )
  1418. {
  1419. // Don't save the class declaration. Scan past the 'class'
  1420. // keyword and up to the first whitespace.
  1421. sansClass += 7;
  1422. S32 index = 0;
  1423. while( ( *sansClass != ' ' ) && ( *sansClass != '\n' ) && *sansClass && ( index < ( nsDocLength - 1 ) ) )
  1424. {
  1425. nsDocBlockClass[index++] = *sansClass;
  1426. sansClass++;
  1427. }
  1428. nsDocBlockClass[index] = '\0';
  1429. curNSDocBlock = sansClass + 1;
  1430. }
  1431. else
  1432. curFNDocBlock = docblock;
  1433. }
  1434. break;
  1435. case OP_LOADIMMED_IDENT:
  1436. STR.setStringValue(CodeToSTE(code, ip));
  1437. ip += 2;
  1438. break;
  1439. case OP_CALLFUNC_RESOLVE:
  1440. // This deals with a function that is potentially living in a namespace.
  1441. fnNamespace = CodeToSTE(code, ip+2);
  1442. fnName = CodeToSTE(code, ip);
  1443. // Try to look it up.
  1444. ns = Namespace::find(fnNamespace);
  1445. nsEntry = ns->lookup(fnName);
  1446. if(!nsEntry)
  1447. {
  1448. ip+= 5;
  1449. Con::warnf(ConsoleLogEntry::General,
  1450. "%s: Unable to find function %s%s%s",
  1451. getFileLine(ip-7), fnNamespace ? fnNamespace : "",
  1452. fnNamespace ? "::" : "", fnName);
  1453. STR.popFrame();
  1454. CSTK.popFrame();
  1455. break;
  1456. }
  1457. #ifdef COMPILER_OPTIMIZE_FUNCTION_CALLS
  1458. // Now fall through to OP_CALLFUNC...
  1459. // Now, rewrite our code a bit (ie, avoid future lookups) and fall
  1460. // through to OP_CALLFUNC
  1461. #ifdef TORQUE_CPU_X64
  1462. *((U64*)(code+ip+2)) = ((U64)nsEntry);
  1463. #else
  1464. code[ip+2] = ((U32)nsEntry);
  1465. #endif
  1466. code[ip-1] = OP_CALLFUNC;
  1467. #endif
  1468. case OP_CALLFUNC:
  1469. {
  1470. // This routingId is set when we query the object as to whether
  1471. // it handles this method. It is set to an enum from the table
  1472. // above indicating whether it handles it on a component it owns
  1473. // or just on the object.
  1474. S32 routingId = 0;
  1475. fnName = CodeToSTE(code, ip);
  1476. //if this is called from inside a function, append the ip and codeptr
  1477. if( gEvalState.getStackDepth() > 0 )
  1478. {
  1479. gEvalState.getCurrentFrame().code = this;
  1480. gEvalState.getCurrentFrame().ip = ip - 1;
  1481. }
  1482. U32 callType = code[ip+4];
  1483. ip += 5;
  1484. CSTK.getArgcArgv(fnName, &callArgc, &callArgv);
  1485. const char *componentReturnValue = "";
  1486. if(callType == FuncCallExprNode::FunctionCall)
  1487. {
  1488. if( !nsEntry )
  1489. {
  1490. #ifdef COMPILER_OPTIMIZE_FUNCTION_CALLS
  1491. #ifdef TORQUE_CPU_X64
  1492. nsEntry = ((Namespace::Entry *) *((U64*)(code+ip-3)));
  1493. #else
  1494. nsEntry = ((Namespace::Entry *) *(code+ip-3));
  1495. #endif
  1496. #else
  1497. nsEntry = Namespace::global()->lookup( fnName );
  1498. #endif
  1499. ns = NULL;
  1500. }
  1501. ns = NULL;
  1502. }
  1503. else if(callType == FuncCallExprNode::MethodCall)
  1504. {
  1505. saveObject = gEvalState.thisObject;
  1506. gEvalState.thisObject = Sim::findObject((const char*)callArgv[1]);
  1507. if(!gEvalState.thisObject)
  1508. {
  1509. // Go back to the previous saved object.
  1510. gEvalState.thisObject = saveObject;
  1511. Con::warnf(ConsoleLogEntry::General,"%s: Unable to find object: '%s' attempting to call function '%s'", getFileLine(ip-4), (const char*)callArgv[1], fnName);
  1512. STR.popFrame();
  1513. CSTK.popFrame();
  1514. STR.setStringValue("");
  1515. break;
  1516. }
  1517. bool handlesMethod = gEvalState.thisObject->handlesConsoleMethod(fnName,&routingId);
  1518. if( handlesMethod && routingId == MethodOnComponent )
  1519. {
  1520. ICallMethod *pComponent = dynamic_cast<ICallMethod *>( gEvalState.thisObject );
  1521. if( pComponent )
  1522. componentReturnValue = pComponent->callMethodArgList( callArgc, callArgv, false );
  1523. }
  1524. ns = gEvalState.thisObject->getNamespace();
  1525. if(ns)
  1526. nsEntry = ns->lookup(fnName);
  1527. else
  1528. nsEntry = NULL;
  1529. }
  1530. else // it's a ParentCall
  1531. {
  1532. if(thisNamespace)
  1533. {
  1534. ns = thisNamespace->mParent;
  1535. if(ns)
  1536. nsEntry = ns->lookup(fnName);
  1537. else
  1538. nsEntry = NULL;
  1539. }
  1540. else
  1541. {
  1542. ns = NULL;
  1543. nsEntry = NULL;
  1544. }
  1545. }
  1546. Namespace::Entry::CallbackUnion * nsCb = NULL;
  1547. const char * nsUsage = NULL;
  1548. if (nsEntry)
  1549. {
  1550. nsCb = &nsEntry->cb;
  1551. nsUsage = nsEntry->mUsage;
  1552. routingId = 0;
  1553. }
  1554. if(!nsEntry || noCalls)
  1555. {
  1556. if(!noCalls && !( routingId == MethodOnComponent ) )
  1557. {
  1558. Con::warnf(ConsoleLogEntry::General,"%s: Unknown command %s.", getFileLine(ip-6), fnName);
  1559. if(callType == FuncCallExprNode::MethodCall)
  1560. {
  1561. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1562. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1563. gEvalState.thisObject->getId(), Con::getNamespaceList(ns) );
  1564. }
  1565. }
  1566. STR.popFrame();
  1567. CSTK.popFrame();
  1568. if( routingId == MethodOnComponent )
  1569. STR.setStringValue( componentReturnValue );
  1570. else
  1571. STR.setStringValue( "" );
  1572. break;
  1573. }
  1574. if(nsEntry->mType == Namespace::Entry::ConsoleFunctionType)
  1575. {
  1576. ConsoleValueRef ret;
  1577. if(nsEntry->mFunctionOffset)
  1578. ret = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage);
  1579. STR.popFrame();
  1580. // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
  1581. if(code[ip] == OP_STR_TO_UINT)
  1582. {
  1583. ip++;
  1584. intStack[++_UINT] = (U32)((S32)ret);
  1585. }
  1586. else if(code[ip] == OP_STR_TO_FLT)
  1587. {
  1588. ip++;
  1589. floatStack[++_FLT] = (F32)ret;
  1590. }
  1591. else if(code[ip] == OP_STR_TO_NONE)
  1592. {
  1593. STR.setStringValue(ret.getStringValue());
  1594. ip++;
  1595. }
  1596. else
  1597. STR.setStringValue((const char*)ret);
  1598. // This will clear everything including returnValue
  1599. CSTK.popFrame();
  1600. //STR.clearFunctionOffset();
  1601. }
  1602. else
  1603. {
  1604. const char* nsName = ns? ns->mName: "";
  1605. #ifndef TORQUE_DEBUG
  1606. // [tom, 12/13/2006] This stops tools functions from working in the console,
  1607. // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
  1608. if(nsEntry->mToolOnly && ! Con::isCurrentScriptToolScript())
  1609. {
  1610. Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", getFileLine(ip-6), nsName, fnName);
  1611. }
  1612. else
  1613. #endif
  1614. if((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1615. {
  1616. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
  1617. getFileLine(ip-6), nsName, fnName,
  1618. callArgc, nsEntry->mMinArgs, nsEntry->mMaxArgs);
  1619. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip-6), nsEntry->mUsage);
  1620. STR.popFrame();
  1621. CSTK.popFrame();
  1622. }
  1623. else
  1624. {
  1625. switch(nsEntry->mType)
  1626. {
  1627. case Namespace::Entry::StringCallbackType:
  1628. {
  1629. const char *ret = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1630. STR.popFrame();
  1631. CSTK.popFrame();
  1632. if(ret != STR.getStringValue())
  1633. STR.setStringValue(ret);
  1634. //else
  1635. // STR.setLen(dStrlen(ret));
  1636. break;
  1637. }
  1638. case Namespace::Entry::IntCallbackType:
  1639. {
  1640. S32 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1641. STR.popFrame();
  1642. CSTK.popFrame();
  1643. if(code[ip] == OP_STR_TO_UINT)
  1644. {
  1645. ip++;
  1646. intStack[++_UINT] = result;
  1647. break;
  1648. }
  1649. else if(code[ip] == OP_STR_TO_FLT)
  1650. {
  1651. ip++;
  1652. floatStack[++_FLT] = result;
  1653. break;
  1654. }
  1655. else if(code[ip] == OP_STR_TO_NONE)
  1656. ip++;
  1657. else
  1658. STR.setIntValue(result);
  1659. break;
  1660. }
  1661. case Namespace::Entry::FloatCallbackType:
  1662. {
  1663. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1664. STR.popFrame();
  1665. CSTK.popFrame();
  1666. if(code[ip] == OP_STR_TO_UINT)
  1667. {
  1668. ip++;
  1669. intStack[++_UINT] = (S64)result;
  1670. break;
  1671. }
  1672. else if(code[ip] == OP_STR_TO_FLT)
  1673. {
  1674. ip++;
  1675. floatStack[++_FLT] = result;
  1676. break;
  1677. }
  1678. else if(code[ip] == OP_STR_TO_NONE)
  1679. ip++;
  1680. else
  1681. STR.setFloatValue(result);
  1682. break;
  1683. }
  1684. case Namespace::Entry::VoidCallbackType:
  1685. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1686. if( code[ ip ] != OP_STR_TO_NONE && Con::getBoolVariable( "$Con::warnVoidAssignment", true ) )
  1687. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip-6), fnName, functionName);
  1688. STR.popFrame();
  1689. CSTK.popFrame();
  1690. STR.setStringValue("");
  1691. break;
  1692. case Namespace::Entry::BoolCallbackType:
  1693. {
  1694. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1695. STR.popFrame();
  1696. CSTK.popFrame();
  1697. if(code[ip] == OP_STR_TO_UINT)
  1698. {
  1699. ip++;
  1700. intStack[++_UINT] = result;
  1701. break;
  1702. }
  1703. else if(code[ip] == OP_STR_TO_FLT)
  1704. {
  1705. ip++;
  1706. floatStack[++_FLT] = result;
  1707. break;
  1708. }
  1709. else if(code[ip] == OP_STR_TO_NONE)
  1710. ip++;
  1711. else
  1712. STR.setIntValue(result);
  1713. break;
  1714. }
  1715. }
  1716. }
  1717. }
  1718. if(callType == FuncCallExprNode::MethodCall)
  1719. gEvalState.thisObject = saveObject;
  1720. break;
  1721. }
  1722. case OP_ADVANCE_STR:
  1723. STR.advance();
  1724. break;
  1725. case OP_ADVANCE_STR_APPENDCHAR:
  1726. STR.advanceChar(code[ip++]);
  1727. break;
  1728. case OP_ADVANCE_STR_COMMA:
  1729. STR.advanceChar('_');
  1730. break;
  1731. case OP_ADVANCE_STR_NUL:
  1732. STR.advanceChar(0);
  1733. break;
  1734. case OP_REWIND_STR:
  1735. STR.rewind();
  1736. break;
  1737. case OP_TERMINATE_REWIND_STR:
  1738. STR.rewindTerminate();
  1739. break;
  1740. case OP_COMPARE_STR:
  1741. intStack[++_UINT] = STR.compare();
  1742. break;
  1743. case OP_PUSH:
  1744. STR.push();
  1745. CSTK.pushStringStackPtr(STR.getPreviousStringValuePtr());
  1746. break;
  1747. case OP_PUSH_UINT:
  1748. CSTK.pushUINT(intStack[_UINT]);
  1749. _UINT--;
  1750. break;
  1751. case OP_PUSH_FLT:
  1752. CSTK.pushFLT(floatStack[_FLT]);
  1753. _FLT--;
  1754. break;
  1755. case OP_PUSH_VAR:
  1756. if (gEvalState.currentVariable)
  1757. CSTK.pushValue(gEvalState.currentVariable->value);
  1758. else
  1759. CSTK.pushString("");
  1760. break;
  1761. case OP_PUSH_FRAME:
  1762. STR.pushFrame();
  1763. CSTK.pushFrame();
  1764. break;
  1765. case OP_ASSERT:
  1766. {
  1767. if( !intStack[_UINT--] )
  1768. {
  1769. const char *message = curStringTable + code[ip];
  1770. U32 breakLine, inst;
  1771. findBreakLine( ip - 1, breakLine, inst );
  1772. if ( PlatformAssert::processAssert( PlatformAssert::Fatal,
  1773. name ? name : "eval",
  1774. breakLine,
  1775. message ) )
  1776. {
  1777. if ( TelDebugger && TelDebugger->isConnected() && breakLine > 0 )
  1778. {
  1779. TelDebugger->breakProcess();
  1780. }
  1781. else
  1782. Platform::debugBreak();
  1783. }
  1784. }
  1785. ip++;
  1786. break;
  1787. }
  1788. case OP_BREAK:
  1789. {
  1790. //append the ip and codeptr before managing the breakpoint!
  1791. AssertFatal( gEvalState.getStackDepth() > 0, "Empty eval stack on break!");
  1792. gEvalState.getCurrentFrame().code = this;
  1793. gEvalState.getCurrentFrame().ip = ip - 1;
  1794. U32 breakLine;
  1795. findBreakLine(ip-1, breakLine, instruction);
  1796. if(!breakLine)
  1797. goto breakContinue;
  1798. TelDebugger->executionStopped(this, breakLine);
  1799. goto breakContinue;
  1800. }
  1801. case OP_ITER_BEGIN_STR:
  1802. {
  1803. iterStack[ _ITER ].mIsStringIter = true;
  1804. /* fallthrough */
  1805. }
  1806. case OP_ITER_BEGIN:
  1807. {
  1808. StringTableEntry varName = CodeToSTE(code, ip);
  1809. U32 failIp = code[ ip + 2 ];
  1810. IterStackRecord& iter = iterStack[ _ITER ];
  1811. iter.mVariable = gEvalState.getCurrentFrame().add( varName );
  1812. if( iter.mIsStringIter )
  1813. {
  1814. iter.mData.mStr.mString = STR.getStringValuePtr();
  1815. iter.mData.mStr.mIndex = 0;
  1816. }
  1817. else
  1818. {
  1819. // Look up the object.
  1820. SimSet* set;
  1821. if( !Sim::findObject( STR.getStringValue(), set ) )
  1822. {
  1823. Con::errorf( ConsoleLogEntry::General, "No SimSet object '%s'", STR.getStringValue() );
  1824. Con::errorf( ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?" );
  1825. ip = failIp;
  1826. continue;
  1827. }
  1828. // Set up.
  1829. iter.mData.mObj.mSet = set;
  1830. iter.mData.mObj.mIndex = 0;
  1831. }
  1832. _ITER ++;
  1833. iterDepth ++;
  1834. STR.push();
  1835. ip += 3;
  1836. break;
  1837. }
  1838. case OP_ITER:
  1839. {
  1840. U32 breakIp = code[ ip ];
  1841. IterStackRecord& iter = iterStack[ _ITER - 1 ];
  1842. if( iter.mIsStringIter )
  1843. {
  1844. const char* str = StringStackPtrRef(iter.mData.mStr.mString).getPtr(&STR);
  1845. U32 startIndex = iter.mData.mStr.mIndex;
  1846. U32 endIndex = startIndex;
  1847. // Break if at end.
  1848. if( !str[ startIndex ] )
  1849. {
  1850. ip = breakIp;
  1851. continue;
  1852. }
  1853. // Find right end of current component.
  1854. if( !dIsspace( str[ endIndex ] ) )
  1855. do ++ endIndex;
  1856. while( str[ endIndex ] && !dIsspace( str[ endIndex ] ) );
  1857. // Extract component.
  1858. if( endIndex != startIndex )
  1859. {
  1860. char savedChar = str[ endIndex ];
  1861. const_cast< char* >( str )[ endIndex ] = '\0'; // We are on the string stack so this is okay.
  1862. iter.mVariable->setStringValue( &str[ startIndex ] );
  1863. const_cast< char* >( str )[ endIndex ] = savedChar;
  1864. }
  1865. else
  1866. iter.mVariable->setStringValue( "" );
  1867. // Skip separator.
  1868. if( str[ endIndex ] != '\0' )
  1869. ++ endIndex;
  1870. iter.mData.mStr.mIndex = endIndex;
  1871. }
  1872. else
  1873. {
  1874. U32 index = iter.mData.mObj.mIndex;
  1875. SimSet* set = iter.mData.mObj.mSet;
  1876. if( index >= set->size() )
  1877. {
  1878. ip = breakIp;
  1879. continue;
  1880. }
  1881. iter.mVariable->setIntValue( set->at( index )->getId() );
  1882. iter.mData.mObj.mIndex = index + 1;
  1883. }
  1884. ++ ip;
  1885. break;
  1886. }
  1887. case OP_ITER_END:
  1888. {
  1889. -- _ITER;
  1890. -- iterDepth;
  1891. STR.rewind();
  1892. iterStack[ _ITER ].mIsStringIter = false;
  1893. break;
  1894. }
  1895. case OP_INVALID:
  1896. default:
  1897. // error!
  1898. goto execFinished;
  1899. }
  1900. }
  1901. execFinished:
  1902. if ( telDebuggerOn && setFrame < 0 )
  1903. TelDebugger->popStackFrame();
  1904. if ( popFrame )
  1905. gEvalState.popFrame();
  1906. if(argv)
  1907. {
  1908. if(gEvalState.traceOn)
  1909. {
  1910. traceBuffer[0] = 0;
  1911. dStrcat(traceBuffer, "Leaving ");
  1912. if(packageName)
  1913. {
  1914. dStrcat(traceBuffer, "[");
  1915. dStrcat(traceBuffer, packageName);
  1916. dStrcat(traceBuffer, "]");
  1917. }
  1918. if(thisNamespace && thisNamespace->mName)
  1919. {
  1920. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1921. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, STR.getStringValue());
  1922. }
  1923. else
  1924. {
  1925. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1926. "%s() - return %s", thisFunctionName, STR.getStringValue());
  1927. }
  1928. Con::printf("%s", traceBuffer);
  1929. }
  1930. }
  1931. smCurrentCodeBlock = saveCodeBlock;
  1932. if(saveCodeBlock && saveCodeBlock->name)
  1933. {
  1934. Con::gCurrentFile = saveCodeBlock->name;
  1935. Con::gCurrentRoot = saveCodeBlock->modPath;
  1936. }
  1937. decRefCount();
  1938. #ifdef TORQUE_VALIDATE_STACK
  1939. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  1940. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  1941. #endif
  1942. return returnValue;
  1943. }
  1944. //------------------------------------------------------------