compiledEval.cpp 74 KB

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