compiledEval.cpp 72 KB

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