compiledEval.cpp 73 KB

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