compiledEval.cc 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "collection/findIterator.h"
  26. #include "io/resource/resourceManager.h"
  27. #include "string/findMatch.h"
  28. #include "string/stringUnit.h"
  29. #include "console/consoleInternal.h"
  30. #include "io/fileStream.h"
  31. #include "console/compiler.h"
  32. #include "sim/simBase.h"
  33. #include "network/netStringTable.h"
  34. #include "component/dynamicConsoleMethodComponent.h"
  35. #include "string/stringStack.h"
  36. #include "messaging/message.h"
  37. #include "memory/frameAllocator.h"
  38. #include "debug/telnetDebugger.h"
  39. #ifndef _REMOTE_DEBUGGER_BASE_H_
  40. #include "debug/remote/RemoteDebuggerBase.h"
  41. #endif
  42. using namespace Compiler;
  43. enum EvalConstants {
  44. MaxStackSize = 1024,
  45. MethodOnComponent = -2
  46. };
  47. namespace Con
  48. {
  49. // Current script file name and root, these are registered as
  50. // console variables.
  51. extern StringTableEntry gCurrentFile;
  52. extern StringTableEntry gCurrentRoot;
  53. }
  54. F64 floatStack[MaxStackSize];
  55. S64 intStack[MaxStackSize];
  56. StringStack STR;
  57. U32 FLT = 0;
  58. U32 UINT = 0;
  59. static const char *getNamespaceList(Namespace *ns)
  60. {
  61. U32 size = 1;
  62. Namespace * walk;
  63. for(walk = ns; walk; walk = walk->mParent)
  64. size += dStrlen(walk->mName) + 4;
  65. char *ret = Con::getReturnBuffer(size);
  66. ret[0] = 0;
  67. for(walk = ns; walk; walk = walk->mParent)
  68. {
  69. dStrcat(ret, walk->mName);
  70. if(walk->mParent)
  71. dStrcat(ret, " -> ");
  72. }
  73. return ret;
  74. }
  75. //------------------------------------------------------------
  76. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  77. {
  78. F64 val = dAtof(str);
  79. if(val != 0)
  80. return val;
  81. else if(!dStricmp(str, "true"))
  82. return 1;
  83. else if(!dStricmp(str, "false"))
  84. return 0;
  85. else if(file)
  86. {
  87. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  88. return 0;
  89. }
  90. return 0;
  91. }
  92. //------------------------------------------------------------
  93. namespace Con
  94. {
  95. char *getReturnBuffer(U32 bufferSize)
  96. {
  97. return STR.getReturnBuffer(bufferSize);
  98. }
  99. char *getReturnBuffer( const char *stringToCopy )
  100. {
  101. char *ret = STR.getReturnBuffer( dStrlen( stringToCopy ) + 1 );
  102. dStrcpy( ret, stringToCopy );
  103. ret[dStrlen( stringToCopy )] = '\0';
  104. return ret;
  105. }
  106. char *getArgBuffer(U32 bufferSize)
  107. {
  108. return STR.getArgBuffer(bufferSize);
  109. }
  110. char *getFloatArg(F64 arg)
  111. {
  112. char *ret = STR.getArgBuffer(32);
  113. dSprintf(ret, 32, "%g", arg);
  114. return ret;
  115. }
  116. char *getIntArg(S32 arg)
  117. {
  118. char *ret = STR.getArgBuffer(32);
  119. dSprintf(ret, 32, "%d", arg);
  120. return ret;
  121. }
  122. char* getBoolArg(bool arg)
  123. {
  124. char *ret = STR.getArgBuffer(32);
  125. dSprintf(ret, 32, "%d", arg);
  126. return ret;
  127. }
  128. }
  129. //------------------------------------------------------------
  130. inline void ExprEvalState::setCurVarName(StringTableEntry name)
  131. {
  132. if(name[0] == '$')
  133. currentVariable = globalVars.lookup(name);
  134. else if(stack.size())
  135. currentVariable = stack.last()->lookup(name);
  136. if(!currentVariable && gWarnUndefinedScriptVariables)
  137. Con::warnf(ConsoleLogEntry::Script, "Variable referenced before assignment: %s", name);
  138. }
  139. inline void ExprEvalState::setCurVarNameCreate(StringTableEntry name)
  140. {
  141. if(name[0] == '$')
  142. currentVariable = globalVars.add(name);
  143. else if(stack.size())
  144. currentVariable = stack.last()->add(name);
  145. else
  146. {
  147. currentVariable = NULL;
  148. Con::warnf(ConsoleLogEntry::Script, "Accessing local variable in global scope... failed: %s", name);
  149. }
  150. }
  151. //------------------------------------------------------------
  152. inline S32 ExprEvalState::getIntVariable()
  153. {
  154. return currentVariable ? currentVariable->getIntValue() : 0;
  155. }
  156. inline F64 ExprEvalState::getFloatVariable()
  157. {
  158. return currentVariable ? currentVariable->getFloatValue() : 0;
  159. }
  160. inline const char *ExprEvalState::getStringVariable()
  161. {
  162. return currentVariable ? currentVariable->getStringValue() : "";
  163. }
  164. //------------------------------------------------------------
  165. inline void ExprEvalState::setIntVariable(S32 val)
  166. {
  167. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  168. currentVariable->setIntValue(val);
  169. }
  170. inline void ExprEvalState::setFloatVariable(F64 val)
  171. {
  172. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  173. currentVariable->setFloatValue((F32)val);
  174. }
  175. inline void ExprEvalState::setStringVariable(const char *val)
  176. {
  177. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  178. currentVariable->setStringValue(val);
  179. }
  180. //------------------------------------------------------------
  181. void CodeBlock::getFunctionArgs(char buffer[1024], U32 ip)
  182. {
  183. U32 fnArgc = code[ip + 5];
  184. buffer[0] = 0;
  185. for(U32 i = 0; i < fnArgc; i++)
  186. {
  187. StringTableEntry var = U32toSTE(code[ip + i + 6]);
  188. // Add a comma so it looks nice!
  189. if(i != 0)
  190. dStrcat(buffer, ", ");
  191. dStrcat(buffer, "var ");
  192. // Try to capture junked parameters
  193. if(var[0])
  194. dStrcat(buffer, var+1);
  195. else
  196. dStrcat(buffer, "JUNK");
  197. }
  198. }
  199. // Returns, in 'val', the specified component of a string.
  200. static void getUnit(const char *string, U32 index, const char *set, char val[], S32 len)
  201. {
  202. U32 sz;
  203. while(index--)
  204. {
  205. if(!*string)
  206. return;
  207. sz = dStrcspn(string, set);
  208. if (string[sz] == 0)
  209. return;
  210. string += (sz + 1);
  211. }
  212. sz = dStrcspn(string, set);
  213. if (sz == 0)
  214. return;
  215. if( ( sz + 1 ) > (U32)len )
  216. return;
  217. dStrncpy(val, string, sz);
  218. val[sz] = '\0';
  219. }
  220. // Copies a string, replacing the (space separated) specified component. The
  221. // return value is stored in 'val'.
  222. static void setUnit(const char *string, U32 index, const char *replace, const char *set, char val[], S32 len)
  223. {
  224. U32 sz;
  225. const char *start = string;
  226. if( ( dStrlen(string) + dStrlen(replace) + 1 ) > (U32)len )
  227. return;
  228. U32 padCount = 0;
  229. while(index--)
  230. {
  231. sz = dStrcspn(string, set);
  232. if(string[sz] == 0)
  233. {
  234. string += sz;
  235. padCount = index + 1;
  236. break;
  237. }
  238. else
  239. string += (sz + 1);
  240. }
  241. // copy first chunk
  242. sz = string-start;
  243. dStrncpy(val, start, sz);
  244. for(U32 i = 0; i < padCount; i++)
  245. val[sz++] = set[0];
  246. // replace this unit
  247. val[sz] = '\0';
  248. dStrcat(val, replace);
  249. // copy remaining chunks
  250. sz = dStrcspn(string, set); // skip chunk we're replacing
  251. if(!sz && !string[sz])
  252. return;
  253. string += sz;
  254. dStrcat(val, string);
  255. return;
  256. }
  257. //-----------------------------------------------------------------------------
  258. static bool isDigitsOnly( const char* pString )
  259. {
  260. // Sanity.
  261. AssertFatal( pString != NULL, "isDigits() - Cannot check a NULL string." );
  262. const char* pDigitCursor = pString;
  263. if ( *pDigitCursor == 0 )
  264. return false;
  265. // Check for digits only.
  266. do
  267. {
  268. if ( dIsdigit( *pDigitCursor++ ) )
  269. continue;
  270. return false;
  271. }
  272. while( *pDigitCursor != 0 );
  273. return true;
  274. }
  275. //-----------------------------------------------------------------------------
  276. static const StringTableEntry _xyzw[] =
  277. {
  278. StringTable->insert( "x" ),
  279. StringTable->insert( "y" ),
  280. StringTable->insert( "z" ),
  281. StringTable->insert( "w" )
  282. };
  283. static const StringTableEntry _rgba[] =
  284. {
  285. StringTable->insert( "r" ),
  286. StringTable->insert( "g" ),
  287. StringTable->insert( "b" ),
  288. StringTable->insert( "a" )
  289. };
  290. static const StringTableEntry _size[] =
  291. {
  292. StringTable->insert( "width" ),
  293. StringTable->insert( "height" )
  294. };
  295. static const StringTableEntry _count = StringTable->insert( "count" );
  296. //-----------------------------------------------------------------------------
  297. // Gets a component of an object's field value or a variable and returns it in val.
  298. static void getFieldComponent( SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField, char* val, const U32 bufferSize )
  299. {
  300. const char* prevVal = NULL;
  301. // Grab value from object.
  302. if( object && field )
  303. prevVal = object->getDataField( field, array );
  304. // Otherwise, grab from the string stack. The value coming in will always
  305. // be a string because that is how multi-component variables are handled.
  306. else
  307. prevVal = STR.getStringValue();
  308. // Make sure we got a value.
  309. if ( prevVal && *prevVal )
  310. {
  311. if ( subField == _count )
  312. dSprintf( val, bufferSize, "%d", StringUnit::getUnitCount( prevVal, " \t\n" ) );
  313. else if ( subField == _xyzw[0] || subField == _rgba[0] || subField == _size[0] )
  314. dStrncpy( val, StringUnit::getUnit( prevVal, 0, " \t\n"), bufferSize );
  315. else if ( subField == _xyzw[1] || subField == _rgba[1] || subField == _size[1] )
  316. dStrncpy( val, StringUnit::getUnit( prevVal, 1, " \t\n"), bufferSize );
  317. else if ( subField == _xyzw[2] || subField == _rgba[2] )
  318. dStrncpy( val, StringUnit::getUnit( prevVal, 2, " \t\n"), bufferSize );
  319. else if ( subField == _xyzw[3] || subField == _rgba[3] )
  320. dStrncpy( val, StringUnit::getUnit( prevVal, 3, " \t\n"), bufferSize );
  321. else if ( *subField == '_' && isDigitsOnly(subField+1) )
  322. dStrncpy( val, StringUnit::getUnit( prevVal, dAtoi(subField+1), " \t\n"), bufferSize );
  323. else
  324. val[0] = 0;
  325. }
  326. else
  327. val[0] = 0;
  328. }
  329. //-----------------------------------------------------------------------------
  330. // Sets a component of an object's field value based on the sub field. 'x' will
  331. // set the first field, 'y' the second, and 'z' the third.
  332. static void setFieldComponent( SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField )
  333. {
  334. // Copy the current string value
  335. char strValue[1024];
  336. dStrncpy( strValue, STR.getStringValue(), sizeof(strValue) );
  337. char val[1024] = "";
  338. const U32 bufferSize = sizeof(val);
  339. const char* prevVal = NULL;
  340. // Set the value on an object field.
  341. if( object && field )
  342. prevVal = object->getDataField( field, array );
  343. // Set the value on a variable.
  344. else if( gEvalState.currentVariable )
  345. prevVal = gEvalState.getStringVariable();
  346. // Ensure that the variable has a value
  347. if (!prevVal)
  348. return;
  349. if ( subField == _xyzw[0] || subField == _rgba[0] || subField == _size[0] )
  350. dStrncpy( val, StringUnit::setUnit( prevVal, 0, strValue, " \t\n"), bufferSize );
  351. else if ( subField == _xyzw[1] || subField == _rgba[1] || subField == _size[1] )
  352. dStrncpy( val, StringUnit::setUnit( prevVal, 1, strValue, " \t\n"), bufferSize );
  353. else if ( subField == _xyzw[2] || subField == _rgba[2] )
  354. dStrncpy( val, StringUnit::setUnit( prevVal, 2, strValue, " \t\n"), bufferSize );
  355. else if ( subField == _xyzw[3] || subField == _rgba[3] )
  356. dStrncpy( val, StringUnit::setUnit( prevVal, 3, strValue, " \t\n"), bufferSize );
  357. else if ( *subField == '_' && isDigitsOnly(subField+1) )
  358. dStrncpy( val, StringUnit::setUnit( prevVal, dAtoi(subField+1), strValue, " \t\n"), bufferSize );
  359. if ( val[0] != 0 )
  360. {
  361. // Update the field or variable.
  362. if( object && field )
  363. object->setDataField( field, 0, val );
  364. else if( gEvalState.currentVariable )
  365. gEvalState.setStringVariable( val );
  366. }
  367. }
  368. const char *CodeBlock::exec(U32 ip, const char *functionName, Namespace *thisNamespace, U32 argc, const char **argv, bool noCalls, StringTableEntry packageName, S32 setFrame)
  369. {
  370. #ifdef TORQUE_DEBUG
  371. U32 stackStart = STR.mStartStackSize;
  372. #endif
  373. static char traceBuffer[1024];
  374. U32 i;
  375. incRefCount();
  376. F64 *curFloatTable;
  377. char *curStringTable;
  378. STR.clearFunctionOffset();
  379. StringTableEntry thisFunctionName = NULL;
  380. bool popFrame = false;
  381. if(argv)
  382. {
  383. // assume this points into a function decl:
  384. U32 fnArgc = code[ip + 5];
  385. thisFunctionName = U32toSTE(code[ip]);
  386. argc = getMin(argc-1, fnArgc); // argv[0] is func name
  387. if(gEvalState.traceOn)
  388. {
  389. traceBuffer[0] = 0;
  390. dStrcat(traceBuffer, "Entering ");
  391. if(packageName)
  392. {
  393. dStrcat(traceBuffer, "[");
  394. dStrcat(traceBuffer, packageName);
  395. dStrcat(traceBuffer, "]");
  396. }
  397. if(thisNamespace && thisNamespace->mName)
  398. {
  399. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  400. "%s::%s(", thisNamespace->mName, thisFunctionName);
  401. }
  402. else
  403. {
  404. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  405. "%s(", thisFunctionName);
  406. }
  407. for(i = 0; i < argc; i++)
  408. {
  409. dStrcat(traceBuffer, argv[i+1]);
  410. if(i != argc - 1)
  411. dStrcat(traceBuffer, ", ");
  412. }
  413. dStrcat(traceBuffer, ")");
  414. Con::printf("%s", traceBuffer);
  415. }
  416. gEvalState.pushFrame(thisFunctionName, thisNamespace);
  417. popFrame = true;
  418. for(i = 0; i < argc; i++)
  419. {
  420. StringTableEntry var = U32toSTE(code[ip + i + 6]);
  421. gEvalState.setCurVarNameCreate(var);
  422. gEvalState.setStringVariable(argv[i+1]);
  423. }
  424. ip = ip + fnArgc + 6;
  425. curFloatTable = functionFloats;
  426. curStringTable = functionStrings;
  427. }
  428. else
  429. {
  430. curFloatTable = globalFloats;
  431. curStringTable = globalStrings;
  432. // Do we want this code to execute using a new stack frame?
  433. if (setFrame < 0)
  434. {
  435. gEvalState.pushFrame(NULL, NULL);
  436. popFrame = true;
  437. }
  438. else if (!gEvalState.stack.empty())
  439. {
  440. // We want to copy a reference to an existing stack frame
  441. // on to the top of the stack. Any change that occurs to
  442. // the locals during this new frame will also occur in the
  443. // original frame.
  444. S32 stackIndex = gEvalState.stack.size() - setFrame - 1;
  445. gEvalState.pushFrameRef( stackIndex );
  446. popFrame = true;
  447. }
  448. }
  449. // Grab the state of the telenet debugger here once
  450. // so that the push and pop frames are always balanced.
  451. const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
  452. if ( telDebuggerOn && setFrame < 0 )
  453. TelDebugger->pushStackFrame();
  454. // Notify the remote debugger.
  455. RemoteDebuggerBase* pRemoteDebugger = RemoteDebuggerBase::getRemoteDebugger();
  456. if ( pRemoteDebugger != NULL && setFrame < 0 )
  457. pRemoteDebugger->pushStackFrame();
  458. StringTableEntry var, objParent;
  459. U32 failJump;
  460. StringTableEntry fnName;
  461. StringTableEntry fnNamespace, fnPackage;
  462. SimObject *currentNewObject = 0;
  463. StringTableEntry prevField = NULL;
  464. StringTableEntry curField = NULL;
  465. SimObject *prevObject = NULL;
  466. SimObject *curObject = NULL;
  467. SimObject *saveObject=NULL;
  468. Namespace::Entry *nsEntry;
  469. Namespace *ns;
  470. const char* curFNDocBlock = NULL;
  471. const char* curNSDocBlock = NULL;
  472. const S32 nsDocLength = 128;
  473. char nsDocBlockClass[nsDocLength];
  474. U32 callArgc;
  475. const char **callArgv;
  476. static char curFieldArray[256];
  477. static char prevFieldArray[256];
  478. CodeBlock *saveCodeBlock = smCurrentCodeBlock;
  479. smCurrentCodeBlock = this;
  480. if(this->name)
  481. {
  482. Con::gCurrentFile = this->name;
  483. Con::gCurrentRoot = mRoot;
  484. }
  485. const char * val;
  486. // The frame temp is used by the variable accessor ops (OP_SAVEFIELD_* and
  487. // OP_LOADFIELD_*) to store temporary values for the fields.
  488. static S32 VAL_BUFFER_SIZE = 1024;
  489. FrameTemp<char> valBuffer( VAL_BUFFER_SIZE );
  490. for(;;)
  491. {
  492. U32 instruction = code[ip++];
  493. breakContinue:
  494. switch(instruction)
  495. {
  496. case OP_FUNC_DECL:
  497. if(!noCalls)
  498. {
  499. fnName = U32toSTE(code[ip]);
  500. fnNamespace = U32toSTE(code[ip+1]);
  501. fnPackage = U32toSTE(code[ip+2]);
  502. bool hasBody = bool(code[ip+3]);
  503. Namespace::unlinkPackages();
  504. ns = Namespace::find(fnNamespace, fnPackage);
  505. ns->addFunction(fnName, this, hasBody ? ip : 0, curFNDocBlock ? dStrdup( curFNDocBlock ) : NULL );// if no body, set the IP to 0
  506. if( curNSDocBlock )
  507. {
  508. if( fnNamespace == StringTable->lookup( nsDocBlockClass ) )
  509. {
  510. char *usageStr = dStrdup( curNSDocBlock );
  511. usageStr[dStrlen(usageStr)] = '\0';
  512. ns->mUsage = usageStr;
  513. ns->mCleanUpUsage = true;
  514. curNSDocBlock = NULL;
  515. }
  516. }
  517. Namespace::relinkPackages();
  518. // If we had a docblock, it's definitely not valid anymore, so clear it out.
  519. curFNDocBlock = NULL;
  520. //Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
  521. }
  522. ip = code[ip + 4];
  523. break;
  524. case OP_CREATE_OBJECT:
  525. {
  526. // Read some useful info.
  527. objParent = U32toSTE(code[ip ]);
  528. bool isDataBlock = code[ip + 1];
  529. bool isInternal = code[ip + 2];
  530. bool isMessage = code[ip + 3];
  531. failJump = code[ip + 4];
  532. // If we don't allow calls, we certainly don't allow creating objects!
  533. // Moved this to after failJump is set. Engine was crashing when
  534. // noCalls = true and an object was being created at the beginning of
  535. // a file. ADL.
  536. if(noCalls)
  537. {
  538. ip = failJump;
  539. break;
  540. }
  541. // Get the constructor information off the stack.
  542. STR.getArgcArgv(NULL, &callArgc, &callArgv, true);
  543. // Con::printf("Creating object...");
  544. // objectName = argv[1]...
  545. currentNewObject = NULL;
  546. // Are we creating a datablock? If so, deal with case where we override
  547. // an old one.
  548. if(isDataBlock)
  549. {
  550. // Con::printf(" - is a datablock");
  551. // Find the old one if any.
  552. SimObject *db = Sim::getDataBlockGroup()->findObject(callArgv[2]);
  553. // Make sure we're not changing types on ourselves...
  554. if(db && dStricmp(db->getClassName(), callArgv[1]))
  555. {
  556. Con::errorf(ConsoleLogEntry::General, "Cannot re-declare data block %s with a different class.", callArgv[2]);
  557. ip = failJump;
  558. break;
  559. }
  560. // If there was one, set the currentNewObject and move on.
  561. if(db)
  562. currentNewObject = db;
  563. }
  564. if(!currentNewObject)
  565. {
  566. // Well, looks like we have to create a new object.
  567. ConsoleObject *object = ConsoleObject::create(callArgv[1]);
  568. // Deal with failure!
  569. if(!object)
  570. {
  571. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", getFileLine(ip-1), callArgv[1]);
  572. ip = failJump;
  573. break;
  574. }
  575. // Do special datablock init if appropros
  576. if(isDataBlock)
  577. {
  578. SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(object);
  579. if(dataBlock)
  580. {
  581. dataBlock->assignId();
  582. }
  583. else
  584. {
  585. // They tried to make a non-datablock with a datablock keyword!
  586. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", getFileLine(ip-1), callArgv[1]);
  587. // Clean up...
  588. delete object;
  589. ip = failJump;
  590. break;
  591. }
  592. }
  593. // Finally, set currentNewObject to point to the new one.
  594. currentNewObject = dynamic_cast<SimObject *>(object);
  595. // Deal with the case of a non-SimObject.
  596. if(!currentNewObject)
  597. {
  598. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", getFileLine(ip-1), callArgv[1]);
  599. delete object;
  600. ip = failJump;
  601. break;
  602. }
  603. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  604. // [tom, 9/8/2006] it is inheritance if it's a message ... muwahahah!
  605. if(!isMessage && *objParent)
  606. {
  607. // Find it!
  608. SimObject *parent;
  609. if(Sim::findObject(objParent, parent))
  610. {
  611. // Con::printf(" - Parent object found: %s", parent->getClassName());
  612. // and suck the juices from it!
  613. currentNewObject->assignFieldsFrom(parent);
  614. }
  615. else
  616. Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", getFileLine(ip-1), objParent, callArgv[1]);
  617. // Mm! Juices!
  618. }
  619. // If a name was passed, assign it.
  620. if(callArgv[2][0])
  621. {
  622. if(! isMessage)
  623. {
  624. if(! isInternal)
  625. currentNewObject->assignName(callArgv[2]);
  626. else
  627. currentNewObject->setInternalName(callArgv[2]);
  628. }
  629. else
  630. {
  631. Message *msg = dynamic_cast<Message *>(currentNewObject);
  632. if(msg)
  633. {
  634. msg->setClassNamespace(callArgv[2]);
  635. msg->setSuperClassNamespace(objParent);
  636. }
  637. else
  638. {
  639. Con::errorf(ConsoleLogEntry::General, "%s: Attempting to use newmsg on non-message type %s", getFileLine(ip-1), callArgv[1]);
  640. delete currentNewObject;
  641. currentNewObject = NULL;
  642. ip = failJump;
  643. break;
  644. }
  645. }
  646. }
  647. // Do the constructor parameters.
  648. if(!currentNewObject->processArguments(callArgc-3, callArgv+3))
  649. {
  650. delete currentNewObject;
  651. currentNewObject = NULL;
  652. ip = failJump;
  653. break;
  654. }
  655. // If it's not a datablock, allow people to modify bits of it.
  656. if(!isDataBlock)
  657. {
  658. currentNewObject->setModStaticFields(true);
  659. currentNewObject->setModDynamicFields(true);
  660. }
  661. }
  662. // Advance the IP past the create info...
  663. ip += 5;
  664. break;
  665. }
  666. case OP_ADD_OBJECT:
  667. {
  668. // See OP_SETCURVAR for why we do this.
  669. curFNDocBlock = NULL;
  670. curNSDocBlock = NULL;
  671. // Do we place this object at the root?
  672. bool placeAtRoot = code[ip++];
  673. // Con::printf("Adding object %s", currentNewObject->getName());
  674. // Make sure it wasn't already added, then add it.
  675. if (currentNewObject == NULL)
  676. {
  677. break;
  678. }
  679. if(currentNewObject->isProperlyAdded() == false)
  680. {
  681. bool ret = false;
  682. Message *msg = dynamic_cast<Message *>(currentNewObject);
  683. if(msg)
  684. {
  685. SimObjectId id = Message::getNextMessageID();
  686. if(id != 0xffffffff)
  687. ret = currentNewObject->registerObject(id);
  688. else
  689. Con::errorf("%s: No more object IDs available for messages", getFileLine(ip-2));
  690. }
  691. else
  692. ret = currentNewObject->registerObject();
  693. if(! ret)
  694. {
  695. // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
  696. Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", getFileLine(ip-2), currentNewObject->getName(), currentNewObject->getClassName());
  697. delete currentNewObject;
  698. ip = failJump;
  699. break;
  700. }
  701. }
  702. // Are we dealing with a datablock?
  703. SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(currentNewObject);
  704. static char errorBuffer[256];
  705. // If so, preload it.
  706. if(dataBlock && !dataBlock->preload(true, errorBuffer))
  707. {
  708. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", getFileLine(ip-2),
  709. currentNewObject->getName(), errorBuffer);
  710. dataBlock->deleteObject();
  711. ip = failJump;
  712. break;
  713. }
  714. // What group will we be added to, if any?
  715. U32 groupAddId = (U32)intStack[UINT];
  716. SimGroup *grp = NULL;
  717. SimSet *set = NULL;
  718. SimComponent *comp = NULL;
  719. bool isMessage = dynamic_cast<Message *>(currentNewObject) != NULL;
  720. if(!placeAtRoot || !currentNewObject->getGroup())
  721. {
  722. if(! isMessage)
  723. {
  724. if(! placeAtRoot)
  725. {
  726. // Otherwise just add to the requested group or set.
  727. if(!Sim::findObject(groupAddId, grp))
  728. if(!Sim::findObject(groupAddId, comp))
  729. Sim::findObject(groupAddId, set);
  730. }
  731. if(placeAtRoot || comp != NULL)
  732. {
  733. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  734. const char *addGroupName = Con::getVariable("instantGroup");
  735. if(!Sim::findObject(addGroupName, grp))
  736. Sim::findObject(RootGroupId, grp);
  737. }
  738. if(comp)
  739. {
  740. SimComponent *newComp = dynamic_cast<SimComponent *>(currentNewObject);
  741. if(newComp)
  742. {
  743. if(! comp->addComponent(newComp))
  744. Con::errorf("%s: Unable to add component %s, template not loaded?", getFileLine(ip-2), currentNewObject->getName() ? currentNewObject->getName() : currentNewObject->getIdString());
  745. }
  746. }
  747. }
  748. // If we didn't get a group, then make sure we have a pointer to
  749. // the rootgroup.
  750. if(!grp)
  751. Sim::findObject(RootGroupId, grp);
  752. // add to the parent group
  753. grp->addObject(currentNewObject);
  754. // add to any set we might be in
  755. if(set)
  756. set->addObject(currentNewObject);
  757. }
  758. // store the new object's ID on the stack (overwriting the group/set
  759. // id, if one was given, otherwise getting pushed)
  760. if(placeAtRoot)
  761. intStack[UINT] = currentNewObject->getId();
  762. else
  763. intStack[++UINT] = currentNewObject->getId();
  764. break;
  765. }
  766. case OP_END_OBJECT:
  767. {
  768. // If we're not to be placed at the root, make sure we clean up
  769. // our group reference.
  770. bool placeAtRoot = code[ip++];
  771. if(!placeAtRoot)
  772. UINT--;
  773. break;
  774. }
  775. case OP_JMPIFFNOT:
  776. if(floatStack[FLT--])
  777. {
  778. ip++;
  779. break;
  780. }
  781. ip = code[ip];
  782. break;
  783. case OP_JMPIFNOT:
  784. if(intStack[UINT--])
  785. {
  786. ip++;
  787. break;
  788. }
  789. ip = code[ip];
  790. break;
  791. case OP_JMPIFF:
  792. if(!floatStack[FLT--])
  793. {
  794. ip++;
  795. break;
  796. }
  797. ip = code[ip];
  798. break;
  799. case OP_JMPIF:
  800. if(!intStack[UINT--])
  801. {
  802. ip ++;
  803. break;
  804. }
  805. ip = code[ip];
  806. break;
  807. case OP_JMPIFNOT_NP:
  808. if(intStack[UINT])
  809. {
  810. UINT--;
  811. ip++;
  812. break;
  813. }
  814. ip = code[ip];
  815. break;
  816. case OP_JMPIF_NP:
  817. if(!intStack[UINT])
  818. {
  819. UINT--;
  820. ip++;
  821. break;
  822. }
  823. ip = code[ip];
  824. break;
  825. case OP_JMP:
  826. ip = code[ip];
  827. break;
  828. case OP_RETURN:
  829. goto execFinished;
  830. case OP_CMPEQ:
  831. intStack[UINT+1] = bool(floatStack[FLT] == floatStack[FLT-1]);
  832. UINT++;
  833. FLT -= 2;
  834. break;
  835. case OP_CMPGR:
  836. intStack[UINT+1] = bool(floatStack[FLT] > floatStack[FLT-1]);
  837. UINT++;
  838. FLT -= 2;
  839. break;
  840. case OP_CMPGE:
  841. intStack[UINT+1] = bool(floatStack[FLT] >= floatStack[FLT-1]);
  842. UINT++;
  843. FLT -= 2;
  844. break;
  845. case OP_CMPLT:
  846. intStack[UINT+1] = bool(floatStack[FLT] < floatStack[FLT-1]);
  847. UINT++;
  848. FLT -= 2;
  849. break;
  850. case OP_CMPLE:
  851. intStack[UINT+1] = bool(floatStack[FLT] <= floatStack[FLT-1]);
  852. UINT++;
  853. FLT -= 2;
  854. break;
  855. case OP_CMPNE:
  856. intStack[UINT+1] = bool(floatStack[FLT] != floatStack[FLT-1]);
  857. UINT++;
  858. FLT -= 2;
  859. break;
  860. case OP_XOR:
  861. intStack[UINT-1] = intStack[UINT] ^ intStack[UINT-1];
  862. UINT--;
  863. break;
  864. case OP_MOD:
  865. if( intStack[UINT-1] != 0 )
  866. intStack[UINT-1] = intStack[UINT] % intStack[UINT-1];
  867. else
  868. intStack[UINT-1] = 0;
  869. UINT--;
  870. break;
  871. case OP_BITAND:
  872. intStack[UINT-1] = intStack[UINT] & intStack[UINT-1];
  873. UINT--;
  874. break;
  875. case OP_BITOR:
  876. intStack[UINT-1] = intStack[UINT] | intStack[UINT-1];
  877. UINT--;
  878. break;
  879. case OP_NOT:
  880. intStack[UINT] = !intStack[UINT];
  881. break;
  882. case OP_NOTF:
  883. intStack[UINT+1] = !floatStack[FLT];
  884. FLT--;
  885. UINT++;
  886. break;
  887. case OP_ONESCOMPLEMENT:
  888. intStack[UINT] = ~intStack[UINT];
  889. break;
  890. case OP_SHR:
  891. intStack[UINT-1] = intStack[UINT] >> intStack[UINT-1];
  892. UINT--;
  893. break;
  894. case OP_SHL:
  895. intStack[UINT-1] = intStack[UINT] << intStack[UINT-1];
  896. UINT--;
  897. break;
  898. case OP_AND:
  899. intStack[UINT-1] = intStack[UINT] && intStack[UINT-1];
  900. UINT--;
  901. break;
  902. case OP_OR:
  903. intStack[UINT-1] = intStack[UINT] || intStack[UINT-1];
  904. UINT--;
  905. break;
  906. case OP_ADD:
  907. floatStack[FLT-1] = floatStack[FLT] + floatStack[FLT-1];
  908. FLT--;
  909. break;
  910. case OP_SUB:
  911. floatStack[FLT-1] = floatStack[FLT] - floatStack[FLT-1];
  912. FLT--;
  913. break;
  914. case OP_MUL:
  915. floatStack[FLT-1] = floatStack[FLT] * floatStack[FLT-1];
  916. FLT--;
  917. break;
  918. case OP_DIV:
  919. floatStack[FLT-1] = floatStack[FLT] / floatStack[FLT-1];
  920. FLT--;
  921. break;
  922. case OP_NEG:
  923. floatStack[FLT] = -floatStack[FLT];
  924. break;
  925. case OP_SETCURVAR:
  926. var = U32toSTE(code[ip]);
  927. ip++;
  928. // If a variable is set, then these must be NULL. It is necessary
  929. // to set this here so that the vector parser can appropriately
  930. // identify whether it's dealing with a vector.
  931. prevField = NULL;
  932. prevObject = NULL;
  933. curObject = NULL;
  934. gEvalState.setCurVarName(var);
  935. // In order to let docblocks work properly with variables, we have
  936. // clear the current docblock when we do an assign. This way it
  937. // won't inappropriately carry forward to following function decls.
  938. curFNDocBlock = NULL;
  939. curNSDocBlock = NULL;
  940. break;
  941. case OP_SETCURVAR_CREATE:
  942. var = U32toSTE(code[ip]);
  943. ip++;
  944. // See OP_SETCURVAR
  945. prevField = NULL;
  946. prevObject = NULL;
  947. curObject = NULL;
  948. gEvalState.setCurVarNameCreate(var);
  949. // See OP_SETCURVAR for why we do this.
  950. curFNDocBlock = NULL;
  951. curNSDocBlock = NULL;
  952. break;
  953. case OP_SETCURVAR_ARRAY:
  954. var = STR.getSTValue();
  955. // See OP_SETCURVAR
  956. prevField = NULL;
  957. prevObject = NULL;
  958. curObject = NULL;
  959. gEvalState.setCurVarName(var);
  960. // See OP_SETCURVAR for why we do this.
  961. curFNDocBlock = NULL;
  962. curNSDocBlock = NULL;
  963. break;
  964. case OP_SETCURVAR_ARRAY_CREATE:
  965. var = STR.getSTValue();
  966. // See OP_SETCURVAR
  967. prevField = NULL;
  968. prevObject = NULL;
  969. curObject = NULL;
  970. gEvalState.setCurVarNameCreate(var);
  971. // See OP_SETCURVAR for why we do this.
  972. curFNDocBlock = NULL;
  973. curNSDocBlock = NULL;
  974. break;
  975. case OP_LOADVAR_UINT:
  976. intStack[UINT+1] = gEvalState.getIntVariable();
  977. UINT++;
  978. break;
  979. case OP_LOADVAR_FLT:
  980. floatStack[FLT+1] = gEvalState.getFloatVariable();
  981. FLT++;
  982. break;
  983. case OP_LOADVAR_STR:
  984. val = gEvalState.getStringVariable();
  985. STR.setStringValue(val);
  986. break;
  987. case OP_SAVEVAR_UINT:
  988. gEvalState.setIntVariable((S32)intStack[UINT]);
  989. break;
  990. case OP_SAVEVAR_FLT:
  991. gEvalState.setFloatVariable(floatStack[FLT]);
  992. break;
  993. case OP_SAVEVAR_STR:
  994. gEvalState.setStringVariable(STR.getStringValue());
  995. break;
  996. case OP_SETCUROBJECT:
  997. // Save the previous object for parsing vector fields.
  998. prevObject = curObject;
  999. val = STR.getStringValue();
  1000. // Sim::findObject will sometimes find valid objects from
  1001. // multi-component strings. This makes sure that doesn't
  1002. // happen.
  1003. for( const char* check = val; *check; check++ )
  1004. {
  1005. if( *check == ' ' )
  1006. {
  1007. val = "";
  1008. break;
  1009. }
  1010. }
  1011. curObject = Sim::findObject(val);
  1012. break;
  1013. case OP_SETCUROBJECT_INTERNAL:
  1014. ++ip; // To skip the recurse flag if the object wasnt found
  1015. if(curObject)
  1016. {
  1017. SimGroup *group = dynamic_cast<SimGroup *>(curObject);
  1018. if(group)
  1019. {
  1020. StringTableEntry intName = StringTable->insert(STR.getStringValue());
  1021. bool recurse = code[ip-1];
  1022. SimObject *obj = group->findObjectByInternalName(intName, recurse);
  1023. intStack[UINT+1] = obj ? obj->getId() : 0;
  1024. UINT++;
  1025. }
  1026. else
  1027. {
  1028. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-group %s of class %s.", getFileLine(ip-2), curObject->getName(), curObject->getClassName());
  1029. intStack[UINT] = 0;
  1030. }
  1031. }
  1032. break;
  1033. case OP_SETCUROBJECT_NEW:
  1034. curObject = currentNewObject;
  1035. break;
  1036. case OP_SETCURFIELD:
  1037. // Save the previous field for parsing vector fields.
  1038. prevField = curField;
  1039. dStrcpy( prevFieldArray, curFieldArray );
  1040. curField = U32toSTE(code[ip]);
  1041. curFieldArray[0] = 0;
  1042. ip++;
  1043. break;
  1044. case OP_SETCURFIELD_ARRAY:
  1045. dStrcpy(curFieldArray, STR.getStringValue());
  1046. break;
  1047. case OP_LOADFIELD_UINT:
  1048. if(curObject)
  1049. intStack[UINT+1] = U32(dAtoi(curObject->getDataField(curField, curFieldArray)));
  1050. else
  1051. {
  1052. // The field is not being retrieved from an object. Maybe it's
  1053. // a special accessor?
  1054. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer, VAL_BUFFER_SIZE );
  1055. intStack[UINT+1] = dAtoi( valBuffer );
  1056. }
  1057. UINT++;
  1058. break;
  1059. case OP_LOADFIELD_FLT:
  1060. if(curObject)
  1061. floatStack[FLT+1] = dAtof(curObject->getDataField(curField, curFieldArray));
  1062. else
  1063. {
  1064. // The field is not being retrieved from an object. Maybe it's
  1065. // a special accessor?
  1066. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer, VAL_BUFFER_SIZE );
  1067. floatStack[FLT+1] = dAtof( valBuffer );
  1068. }
  1069. FLT++;
  1070. break;
  1071. case OP_LOADFIELD_STR:
  1072. if(curObject)
  1073. {
  1074. val = curObject->getDataField(curField, curFieldArray);
  1075. STR.setStringValue( val );
  1076. }
  1077. else
  1078. {
  1079. // The field is not being retrieved from an object. Maybe it's
  1080. // a special accessor?
  1081. getFieldComponent( prevObject, prevField, prevFieldArray, curField, valBuffer, VAL_BUFFER_SIZE );
  1082. STR.setStringValue( valBuffer );
  1083. }
  1084. break;
  1085. case OP_SAVEFIELD_UINT:
  1086. STR.setIntValue((U32)intStack[UINT]);
  1087. if(curObject)
  1088. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1089. else
  1090. {
  1091. // The field is not being set on an object. Maybe it's
  1092. // a special accessor?
  1093. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1094. prevObject = NULL;
  1095. }
  1096. break;
  1097. case OP_SAVEFIELD_FLT:
  1098. STR.setFloatValue(floatStack[FLT]);
  1099. if(curObject)
  1100. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1101. else
  1102. {
  1103. // The field is not being set on an object. Maybe it's
  1104. // a special accessor?
  1105. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1106. prevObject = NULL;
  1107. }
  1108. break;
  1109. case OP_SAVEFIELD_STR:
  1110. if(curObject)
  1111. curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  1112. else
  1113. {
  1114. // The field is not being set on an object. Maybe it's
  1115. // a special accessor?
  1116. setFieldComponent( prevObject, prevField, prevFieldArray, curField );
  1117. prevObject = NULL;
  1118. }
  1119. break;
  1120. case OP_STR_TO_UINT:
  1121. intStack[UINT+1] = STR.getIntValue();
  1122. UINT++;
  1123. break;
  1124. case OP_STR_TO_FLT:
  1125. floatStack[FLT+1] = STR.getFloatValue();
  1126. FLT++;
  1127. break;
  1128. case OP_STR_TO_NONE:
  1129. // This exists simply to deal with certain typecast situations.
  1130. break;
  1131. case OP_FLT_TO_UINT:
  1132. intStack[UINT+1] = (S64)floatStack[FLT];
  1133. FLT--;
  1134. UINT++;
  1135. break;
  1136. case OP_FLT_TO_STR:
  1137. STR.setFloatValue(floatStack[FLT]);
  1138. FLT--;
  1139. break;
  1140. case OP_FLT_TO_NONE:
  1141. FLT--;
  1142. break;
  1143. case OP_UINT_TO_FLT:
  1144. floatStack[FLT+1] = (F64)intStack[UINT];
  1145. UINT--;
  1146. FLT++;
  1147. break;
  1148. case OP_UINT_TO_STR:
  1149. STR.setIntValue((U32)intStack[UINT]);
  1150. UINT--;
  1151. break;
  1152. case OP_UINT_TO_NONE:
  1153. UINT--;
  1154. break;
  1155. case OP_LOADIMMED_UINT:
  1156. intStack[UINT+1] = code[ip++];
  1157. UINT++;
  1158. break;
  1159. case OP_LOADIMMED_FLT:
  1160. floatStack[FLT+1] = curFloatTable[code[ip]];
  1161. ip++;
  1162. FLT++;
  1163. break;
  1164. case OP_TAG_TO_STR:
  1165. code[ip-1] = OP_LOADIMMED_STR;
  1166. // it's possible the string has already been converted
  1167. if(U8(curStringTable[code[ip]]) != StringTagPrefixByte)
  1168. {
  1169. U32 id = GameAddTaggedString(curStringTable + code[ip]);
  1170. dSprintf(curStringTable + code[ip] + 1, 7, "%d", id);
  1171. *(curStringTable + code[ip]) = StringTagPrefixByte;
  1172. }
  1173. case OP_LOADIMMED_STR:
  1174. STR.setStringValue(curStringTable + code[ip++]);
  1175. break;
  1176. case OP_DOCBLOCK_STR:
  1177. {
  1178. // If the first word of the doc is '\class' or '@class', then this
  1179. // is a namespace doc block, otherwise it is a function doc block.
  1180. const char* docblock = curStringTable + code[ip++];
  1181. const char* sansClass = dStrstr( docblock, "@class" );
  1182. if( !sansClass )
  1183. sansClass = dStrstr( docblock, "\\class" );
  1184. if( sansClass )
  1185. {
  1186. // Don't save the class declaration. Scan past the 'class'
  1187. // keyword and up to the first whitespace.
  1188. sansClass += 7;
  1189. S32 index = 0;
  1190. while( ( *sansClass != ' ' ) && ( *sansClass != '\n' ) && *sansClass && ( index < ( nsDocLength - 1 ) ) )
  1191. {
  1192. nsDocBlockClass[index++] = *sansClass;
  1193. sansClass++;
  1194. }
  1195. nsDocBlockClass[index] = '\0';
  1196. curNSDocBlock = sansClass + 1;
  1197. }
  1198. else
  1199. curFNDocBlock = docblock;
  1200. }
  1201. break;
  1202. case OP_LOADIMMED_IDENT:
  1203. STR.setStringValue(U32toSTE(code[ip++]));
  1204. break;
  1205. case OP_CALLFUNC_RESOLVE:
  1206. // This deals with a function that is potentially living in a namespace.
  1207. fnNamespace = U32toSTE(code[ip+1]);
  1208. fnName = U32toSTE(code[ip]);
  1209. // Try to look it up.
  1210. ns = Namespace::find(fnNamespace);
  1211. nsEntry = ns->lookup(fnName);
  1212. if(!nsEntry)
  1213. {
  1214. ip+= 3;
  1215. Con::warnf(ConsoleLogEntry::General,
  1216. "%s: Unable to find function %s%s%s",
  1217. getFileLine(ip-4), fnNamespace ? fnNamespace : "",
  1218. fnNamespace ? "::" : "", fnName);
  1219. STR.popFrame();
  1220. break;
  1221. }
  1222. // Now, rewrite our code a bit (ie, avoid future lookups) and fall
  1223. // through to OP_CALLFUNC
  1224. code[ip+1] = *((U32 *) &nsEntry);
  1225. code[ip-1] = OP_CALLFUNC;
  1226. case OP_CALLFUNC:
  1227. {
  1228. // This routingId is set when we query the object as to whether
  1229. // it handles this method. It is set to an enum from the table
  1230. // above indicating whether it handles it on a component it owns
  1231. // or just on the object.
  1232. S32 routingId = 0;
  1233. fnName = U32toSTE(code[ip]);
  1234. //if this is called from inside a function, append the ip and codeptr
  1235. if (!gEvalState.stack.empty())
  1236. {
  1237. gEvalState.stack.last()->code = this;
  1238. gEvalState.stack.last()->ip = ip - 1;
  1239. }
  1240. U32 callType = code[ip+2];
  1241. ip += 3;
  1242. STR.getArgcArgv(fnName, &callArgc, &callArgv);
  1243. if(callType == FuncCallExprNode::FunctionCall)
  1244. {
  1245. nsEntry = *((Namespace::Entry **) &code[ip-2]);
  1246. ns = NULL;
  1247. }
  1248. else if(callType == FuncCallExprNode::MethodCall)
  1249. {
  1250. saveObject = gEvalState.thisObject;
  1251. gEvalState.thisObject = Sim::findObject(callArgv[1]);
  1252. if(!gEvalState.thisObject)
  1253. {
  1254. gEvalState.thisObject = 0;
  1255. Con::warnf(ConsoleLogEntry::General,"%s: Unable to find object: '%s' attempting to call function '%s'", getFileLine(ip-4), callArgv[1], fnName);
  1256. STR.popFrame(); // [neo, 5/7/2007 - #2974]
  1257. break;
  1258. }
  1259. bool handlesMethod = gEvalState.thisObject->handlesConsoleMethod(fnName,&routingId);
  1260. if( handlesMethod && routingId == MethodOnComponent )
  1261. {
  1262. DynamicConsoleMethodComponent *pComponent = dynamic_cast<DynamicConsoleMethodComponent*>( gEvalState.thisObject );
  1263. if( pComponent )
  1264. pComponent->callMethodArgList( callArgc, callArgv, false );
  1265. }
  1266. ns = gEvalState.thisObject->getNamespace();
  1267. if(ns)
  1268. nsEntry = ns->lookup(fnName);
  1269. else
  1270. nsEntry = NULL;
  1271. }
  1272. else // it's a ParentCall
  1273. {
  1274. if(thisNamespace)
  1275. {
  1276. ns = thisNamespace->mParent;
  1277. if(ns)
  1278. nsEntry = ns->lookup(fnName);
  1279. else
  1280. nsEntry = NULL;
  1281. }
  1282. else
  1283. {
  1284. ns = NULL;
  1285. nsEntry = NULL;
  1286. }
  1287. }
  1288. S32 nsType = -1;
  1289. S32 nsMinArgs = 0;
  1290. S32 nsMaxArgs = 0;
  1291. Namespace::Entry::CallbackUnion * nsCb = NULL;
  1292. //Namespace::Entry::CallbackUnion cbu;
  1293. const char * nsUsage = NULL;
  1294. if (nsEntry)
  1295. {
  1296. nsType = nsEntry->mType;
  1297. nsMinArgs = nsEntry->mMinArgs;
  1298. nsMaxArgs = nsEntry->mMaxArgs;
  1299. nsCb = &nsEntry->cb;
  1300. nsUsage = nsEntry->mUsage;
  1301. routingId = 0;
  1302. }
  1303. if(!nsEntry || noCalls)
  1304. {
  1305. if(!noCalls && !( routingId == MethodOnComponent ) )
  1306. {
  1307. Con::warnf(ConsoleLogEntry::General,"%s: Unknown command %s.", getFileLine(ip-4), fnName);
  1308. if(callType == FuncCallExprNode::MethodCall)
  1309. {
  1310. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1311. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1312. gEvalState.thisObject->getId(), getNamespaceList(ns) );
  1313. }
  1314. }
  1315. STR.popFrame();
  1316. STR.setStringValue("");
  1317. break;
  1318. }
  1319. if(nsEntry->mType == Namespace::Entry::ScriptFunctionType)
  1320. {
  1321. const char *ret = "";
  1322. if(nsEntry->mFunctionOffset)
  1323. ret = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage);
  1324. STR.popFrame();
  1325. STR.setStringValue(ret);
  1326. }
  1327. else
  1328. {
  1329. const char* nsName = ns? ns->mName: "";
  1330. if((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1331. {
  1332. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments.", getFileLine(ip-4), nsName, fnName);
  1333. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip-4), nsEntry->mUsage);
  1334. STR.popFrame();
  1335. }
  1336. else
  1337. {
  1338. switch(nsEntry->mType)
  1339. {
  1340. case Namespace::Entry::StringCallbackType:
  1341. {
  1342. const char *ret = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1343. STR.popFrame();
  1344. if(ret != STR.getStringValue())
  1345. STR.setStringValue(ret);
  1346. else
  1347. STR.setLen(dStrlen(ret));
  1348. break;
  1349. }
  1350. case Namespace::Entry::IntCallbackType:
  1351. {
  1352. S32 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1353. STR.popFrame();
  1354. if(code[ip] == OP_STR_TO_UINT)
  1355. {
  1356. ip++;
  1357. intStack[++UINT] = result;
  1358. break;
  1359. }
  1360. else if(code[ip] == OP_STR_TO_FLT)
  1361. {
  1362. ip++;
  1363. floatStack[++FLT] = result;
  1364. break;
  1365. }
  1366. else if(code[ip] == OP_STR_TO_NONE)
  1367. ip++;
  1368. else
  1369. STR.setIntValue(result);
  1370. break;
  1371. }
  1372. case Namespace::Entry::FloatCallbackType:
  1373. {
  1374. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1375. STR.popFrame();
  1376. if(code[ip] == OP_STR_TO_UINT)
  1377. {
  1378. ip++;
  1379. intStack[++UINT] = (S64)result;
  1380. break;
  1381. }
  1382. else if(code[ip] == OP_STR_TO_FLT)
  1383. {
  1384. ip++;
  1385. floatStack[++FLT] = result;
  1386. break;
  1387. }
  1388. else if(code[ip] == OP_STR_TO_NONE)
  1389. ip++;
  1390. else
  1391. STR.setFloatValue(result);
  1392. break;
  1393. }
  1394. case Namespace::Entry::VoidCallbackType:
  1395. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1396. if(code[ip] != OP_STR_TO_NONE)
  1397. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip-4), fnName, functionName);
  1398. STR.popFrame();
  1399. STR.setStringValue("");
  1400. break;
  1401. case Namespace::Entry::BoolCallbackType:
  1402. {
  1403. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1404. STR.popFrame();
  1405. if(code[ip] == OP_STR_TO_UINT)
  1406. {
  1407. ip++;
  1408. intStack[++UINT] = result;
  1409. break;
  1410. }
  1411. else if(code[ip] == OP_STR_TO_FLT)
  1412. {
  1413. ip++;
  1414. floatStack[++FLT] = result;
  1415. break;
  1416. }
  1417. else if(code[ip] == OP_STR_TO_NONE)
  1418. ip++;
  1419. else
  1420. STR.setIntValue(result);
  1421. break;
  1422. }
  1423. }
  1424. }
  1425. }
  1426. if(callType == FuncCallExprNode::MethodCall)
  1427. gEvalState.thisObject = saveObject;
  1428. break;
  1429. }
  1430. case OP_ADVANCE_STR:
  1431. STR.advance();
  1432. break;
  1433. case OP_ADVANCE_STR_APPENDCHAR:
  1434. STR.advanceChar(code[ip++]);
  1435. break;
  1436. case OP_ADVANCE_STR_COMMA:
  1437. STR.advanceChar('_');
  1438. break;
  1439. case OP_ADVANCE_STR_NUL:
  1440. STR.advanceChar(0);
  1441. break;
  1442. case OP_REWIND_STR:
  1443. STR.rewind();
  1444. break;
  1445. case OP_TERMINATE_REWIND_STR:
  1446. STR.rewindTerminate();
  1447. break;
  1448. case OP_COMPARE_STR:
  1449. intStack[++UINT] = STR.compare();
  1450. break;
  1451. case OP_PUSH:
  1452. STR.push();
  1453. break;
  1454. case OP_PUSH_FRAME:
  1455. STR.pushFrame();
  1456. break;
  1457. case OP_BREAK:
  1458. {
  1459. //append the ip and codeptr before managing the breakpoint!
  1460. AssertFatal( !gEvalState.stack.empty(), "Empty eval stack on break!");
  1461. gEvalState.stack.last()->code = this;
  1462. gEvalState.stack.last()->ip = ip - 1;
  1463. U32 breakLine;
  1464. findBreakLine(ip-1, breakLine, instruction);
  1465. if(!breakLine)
  1466. goto breakContinue;
  1467. TelDebugger->executionStopped(this, breakLine);
  1468. // Notify the remote debugger.
  1469. if ( pRemoteDebugger != NULL )
  1470. pRemoteDebugger->executionStopped(this, breakLine);
  1471. goto breakContinue;
  1472. }
  1473. case OP_INVALID:
  1474. default:
  1475. // error!
  1476. goto execFinished;
  1477. }
  1478. }
  1479. execFinished:
  1480. if ( telDebuggerOn && setFrame < 0 )
  1481. TelDebugger->popStackFrame();
  1482. // Notify the remote debugger.
  1483. if ( pRemoteDebugger != NULL && setFrame < 0 )
  1484. pRemoteDebugger->popStackFrame();
  1485. if ( popFrame )
  1486. gEvalState.popFrame();
  1487. if(argv)
  1488. {
  1489. if(gEvalState.traceOn)
  1490. {
  1491. traceBuffer[0] = 0;
  1492. dStrcat(traceBuffer, "Leaving ");
  1493. if(packageName)
  1494. {
  1495. dStrcat(traceBuffer, "[");
  1496. dStrcat(traceBuffer, packageName);
  1497. dStrcat(traceBuffer, "]");
  1498. }
  1499. if(thisNamespace && thisNamespace->mName)
  1500. {
  1501. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1502. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, STR.getStringValue());
  1503. }
  1504. else
  1505. {
  1506. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1507. "%s() - return %s", thisFunctionName, STR.getStringValue());
  1508. }
  1509. Con::printf("%s", traceBuffer);
  1510. }
  1511. }
  1512. else
  1513. {
  1514. delete[] const_cast<char*>(globalStrings);
  1515. delete[] globalFloats;
  1516. globalStrings = NULL;
  1517. globalFloats = NULL;
  1518. }
  1519. smCurrentCodeBlock = saveCodeBlock;
  1520. if(saveCodeBlock && saveCodeBlock->name)
  1521. {
  1522. Con::gCurrentFile = saveCodeBlock->name;
  1523. Con::gCurrentRoot = saveCodeBlock->mRoot;
  1524. }
  1525. decRefCount();
  1526. #ifdef TORQUE_DEBUG
  1527. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  1528. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  1529. #endif
  1530. return STR.getStringValue();
  1531. }
  1532. //------------------------------------------------------------