compiledEval.cpp 73 KB

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