compiledEval.cpp 70 KB

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