compiledEval.cpp 76 KB

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