compiledEval.cc 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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 = CodeToSTE(code, ip + (i*2) + 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 = (U32)(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 + 2 + 6];
  385. thisFunctionName = CodeToSTE(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 = CodeToSTE(code, ip + (2 + 6 + 1) + (i * 2));
  421. gEvalState.setCurVarNameCreate(var);
  422. gEvalState.setStringVariable(argv[i+1]);
  423. }
  424. ip = ip + (fnArgc * 2) + (2 + 6 + 1);
  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 = CodeToSTE(code, ip);
  500. fnNamespace = CodeToSTE(code, ip+2);
  501. fnPackage = CodeToSTE(code, ip+4);
  502. bool hasBody = bool(code[ip+6]);
  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 + 7];
  523. break;
  524. case OP_CREATE_OBJECT:
  525. {
  526. // Read some useful info.
  527. objParent = CodeToSTE(code, ip);
  528. bool isDataBlock = code[ip + 2];
  529. bool isInternal = code[ip + 3];
  530. bool isMessage = code[ip + 4];
  531. failJump = code[ip + 5];
  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 += 6;
  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 = CodeToSTE(code, ip);
  927. ip += 2;
  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 = CodeToSTE(code, ip);
  943. ip += 2;
  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 = CodeToSTE(code, ip);
  1041. curFieldArray[0] = 0;
  1042. ip += 2;
  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(CodeToSTE(code, ip));
  1204. ip += 2;
  1205. break;
  1206. case OP_CALLFUNC_RESOLVE:
  1207. // This deals with a function that is potentially living in a namespace.
  1208. fnNamespace = CodeToSTE(code, ip+2);
  1209. fnName = CodeToSTE(code, ip);
  1210. // Try to look it up.
  1211. ns = Namespace::find(fnNamespace);
  1212. nsEntry = ns->lookup(fnName);
  1213. if(!nsEntry)
  1214. {
  1215. ip+= 5;
  1216. Con::warnf(ConsoleLogEntry::General,
  1217. "%s: Unable to find function %s%s%s",
  1218. getFileLine(ip-4), fnNamespace ? fnNamespace : "",
  1219. fnNamespace ? "::" : "", fnName);
  1220. STR.popFrame();
  1221. break;
  1222. }
  1223. // Now, rewrite our code a bit (ie, avoid future lookups) and fall
  1224. // through to OP_CALLFUNC
  1225. #ifdef TORQUE_CPU_X64
  1226. *((U64*)(code+ip+2)) = ((U64)nsEntry);
  1227. #else
  1228. code[ip+2] = ((U32)nsEntry);
  1229. #endif
  1230. code[ip-1] = OP_CALLFUNC;
  1231. case OP_CALLFUNC:
  1232. {
  1233. // This routingId is set when we query the object as to whether
  1234. // it handles this method. It is set to an enum from the table
  1235. // above indicating whether it handles it on a component it owns
  1236. // or just on the object.
  1237. S32 routingId = 0;
  1238. fnName = CodeToSTE(code, ip);
  1239. //if this is called from inside a function, append the ip and codeptr
  1240. if (!gEvalState.stack.empty())
  1241. {
  1242. gEvalState.stack.last()->code = this;
  1243. gEvalState.stack.last()->ip = ip - 1;
  1244. }
  1245. U32 callType = code[ip+4];
  1246. ip += 5;
  1247. STR.getArgcArgv(fnName, &callArgc, &callArgv);
  1248. if(callType == FuncCallExprNode::FunctionCall)
  1249. {
  1250. #ifdef TORQUE_CPU_X64
  1251. nsEntry = ((Namespace::Entry *) *((U64*)(code+ip-3)));
  1252. #else
  1253. nsEntry = ((Namespace::Entry *) *(code+ip-3));
  1254. #endif
  1255. ns = NULL;
  1256. }
  1257. else if(callType == FuncCallExprNode::MethodCall)
  1258. {
  1259. saveObject = gEvalState.thisObject;
  1260. gEvalState.thisObject = Sim::findObject(callArgv[1]);
  1261. if(!gEvalState.thisObject)
  1262. {
  1263. gEvalState.thisObject = 0;
  1264. Con::warnf(ConsoleLogEntry::General,"%s: Unable to find object: '%s' attempting to call function '%s'", getFileLine(ip-6), callArgv[1], fnName);
  1265. STR.popFrame(); // [neo, 5/7/2007 - #2974]
  1266. STR.setStringValue("");
  1267. break;
  1268. }
  1269. bool handlesMethod = gEvalState.thisObject->handlesConsoleMethod(fnName,&routingId);
  1270. if( handlesMethod && routingId == MethodOnComponent )
  1271. {
  1272. DynamicConsoleMethodComponent *pComponent = dynamic_cast<DynamicConsoleMethodComponent*>( gEvalState.thisObject );
  1273. if( pComponent )
  1274. pComponent->callMethodArgList( callArgc, callArgv, false );
  1275. }
  1276. ns = gEvalState.thisObject->getNamespace();
  1277. if(ns)
  1278. nsEntry = ns->lookup(fnName);
  1279. else
  1280. nsEntry = NULL;
  1281. }
  1282. else // it's a ParentCall
  1283. {
  1284. if(thisNamespace)
  1285. {
  1286. ns = thisNamespace->mParent;
  1287. if(ns)
  1288. nsEntry = ns->lookup(fnName);
  1289. else
  1290. nsEntry = NULL;
  1291. }
  1292. else
  1293. {
  1294. ns = NULL;
  1295. nsEntry = NULL;
  1296. }
  1297. }
  1298. S32 nsType = -1;
  1299. S32 nsMinArgs = 0;
  1300. S32 nsMaxArgs = 0;
  1301. Namespace::Entry::CallbackUnion * nsCb = NULL;
  1302. //Namespace::Entry::CallbackUnion cbu;
  1303. const char * nsUsage = NULL;
  1304. if (nsEntry)
  1305. {
  1306. nsType = nsEntry->mType;
  1307. nsMinArgs = nsEntry->mMinArgs;
  1308. nsMaxArgs = nsEntry->mMaxArgs;
  1309. nsCb = &nsEntry->cb;
  1310. nsUsage = nsEntry->mUsage;
  1311. routingId = 0;
  1312. }
  1313. if(!nsEntry || noCalls)
  1314. {
  1315. if(!noCalls && !( routingId == MethodOnComponent ) )
  1316. {
  1317. Con::warnf(ConsoleLogEntry::General,"%s: Unknown command %s.", getFileLine(ip-6), fnName);
  1318. if(callType == FuncCallExprNode::MethodCall)
  1319. {
  1320. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1321. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1322. gEvalState.thisObject->getId(), getNamespaceList(ns) );
  1323. }
  1324. }
  1325. STR.popFrame();
  1326. STR.setStringValue("");
  1327. break;
  1328. }
  1329. if(nsEntry->mType == Namespace::Entry::ScriptFunctionType)
  1330. {
  1331. const char *ret = "";
  1332. if(nsEntry->mFunctionOffset)
  1333. ret = nsEntry->mCode->exec(nsEntry->mFunctionOffset, fnName, nsEntry->mNamespace, callArgc, callArgv, false, nsEntry->mPackage);
  1334. STR.popFrame();
  1335. STR.setStringValue(ret);
  1336. }
  1337. else
  1338. {
  1339. const char* nsName = ns? ns->mName: "";
  1340. if((nsEntry->mMinArgs && S32(callArgc) < nsEntry->mMinArgs) || (nsEntry->mMaxArgs && S32(callArgc) > nsEntry->mMaxArgs))
  1341. {
  1342. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments.", getFileLine(ip-6), nsName, fnName);
  1343. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", getFileLine(ip-4), nsEntry->mUsage);
  1344. STR.popFrame();
  1345. }
  1346. else
  1347. {
  1348. switch(nsEntry->mType)
  1349. {
  1350. case Namespace::Entry::StringCallbackType:
  1351. {
  1352. const char *ret = nsEntry->cb.mStringCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1353. STR.popFrame();
  1354. if(ret != STR.getStringValue())
  1355. STR.setStringValue(ret);
  1356. else
  1357. STR.setLen(dStrlen(ret));
  1358. break;
  1359. }
  1360. case Namespace::Entry::IntCallbackType:
  1361. {
  1362. S32 result = nsEntry->cb.mIntCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1363. STR.popFrame();
  1364. if(code[ip] == OP_STR_TO_UINT)
  1365. {
  1366. ip++;
  1367. intStack[++UINT] = result;
  1368. break;
  1369. }
  1370. else if(code[ip] == OP_STR_TO_FLT)
  1371. {
  1372. ip++;
  1373. floatStack[++FLT] = result;
  1374. break;
  1375. }
  1376. else if(code[ip] == OP_STR_TO_NONE)
  1377. ip++;
  1378. else
  1379. STR.setIntValue(result);
  1380. break;
  1381. }
  1382. case Namespace::Entry::FloatCallbackType:
  1383. {
  1384. F64 result = nsEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1385. STR.popFrame();
  1386. if(code[ip] == OP_STR_TO_UINT)
  1387. {
  1388. ip++;
  1389. intStack[++UINT] = (S64)result;
  1390. break;
  1391. }
  1392. else if(code[ip] == OP_STR_TO_FLT)
  1393. {
  1394. ip++;
  1395. floatStack[++FLT] = result;
  1396. break;
  1397. }
  1398. else if(code[ip] == OP_STR_TO_NONE)
  1399. ip++;
  1400. else
  1401. STR.setFloatValue(result);
  1402. break;
  1403. }
  1404. case Namespace::Entry::VoidCallbackType:
  1405. nsEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1406. if(code[ip] != OP_STR_TO_NONE)
  1407. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", getFileLine(ip-6), fnName, functionName);
  1408. STR.popFrame();
  1409. STR.setStringValue("");
  1410. break;
  1411. case Namespace::Entry::BoolCallbackType:
  1412. {
  1413. bool result = nsEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, callArgc, callArgv);
  1414. STR.popFrame();
  1415. if(code[ip] == OP_STR_TO_UINT)
  1416. {
  1417. ip++;
  1418. intStack[++UINT] = result;
  1419. break;
  1420. }
  1421. else if(code[ip] == OP_STR_TO_FLT)
  1422. {
  1423. ip++;
  1424. floatStack[++FLT] = result;
  1425. break;
  1426. }
  1427. else if(code[ip] == OP_STR_TO_NONE)
  1428. ip++;
  1429. else
  1430. STR.setIntValue(result);
  1431. break;
  1432. }
  1433. }
  1434. }
  1435. }
  1436. if(callType == FuncCallExprNode::MethodCall)
  1437. gEvalState.thisObject = saveObject;
  1438. break;
  1439. }
  1440. case OP_ADVANCE_STR:
  1441. STR.advance();
  1442. break;
  1443. case OP_ADVANCE_STR_APPENDCHAR:
  1444. STR.advanceChar(code[ip++]);
  1445. break;
  1446. case OP_ADVANCE_STR_COMMA:
  1447. STR.advanceChar('_');
  1448. break;
  1449. case OP_ADVANCE_STR_NUL:
  1450. STR.advanceChar(0);
  1451. break;
  1452. case OP_REWIND_STR:
  1453. STR.rewind();
  1454. break;
  1455. case OP_TERMINATE_REWIND_STR:
  1456. STR.rewindTerminate();
  1457. break;
  1458. case OP_COMPARE_STR:
  1459. intStack[++UINT] = STR.compare();
  1460. break;
  1461. case OP_PUSH:
  1462. STR.push();
  1463. break;
  1464. case OP_PUSH_FRAME:
  1465. STR.pushFrame();
  1466. break;
  1467. case OP_BREAK:
  1468. {
  1469. //append the ip and codeptr before managing the breakpoint!
  1470. AssertFatal( !gEvalState.stack.empty(), "Empty eval stack on break!");
  1471. gEvalState.stack.last()->code = this;
  1472. gEvalState.stack.last()->ip = ip - 1;
  1473. U32 breakLine;
  1474. findBreakLine(ip-1, breakLine, instruction);
  1475. if(!breakLine)
  1476. goto breakContinue;
  1477. TelDebugger->executionStopped(this, breakLine);
  1478. // Notify the remote debugger.
  1479. if ( pRemoteDebugger != NULL )
  1480. pRemoteDebugger->executionStopped(this, breakLine);
  1481. goto breakContinue;
  1482. }
  1483. case OP_INVALID:
  1484. default:
  1485. // error!
  1486. goto execFinished;
  1487. }
  1488. }
  1489. execFinished:
  1490. if ( telDebuggerOn && setFrame < 0 )
  1491. TelDebugger->popStackFrame();
  1492. // Notify the remote debugger.
  1493. if ( pRemoteDebugger != NULL && setFrame < 0 )
  1494. pRemoteDebugger->popStackFrame();
  1495. if ( popFrame )
  1496. gEvalState.popFrame();
  1497. if(argv)
  1498. {
  1499. if(gEvalState.traceOn)
  1500. {
  1501. traceBuffer[0] = 0;
  1502. dStrcat(traceBuffer, "Leaving ");
  1503. if(packageName)
  1504. {
  1505. dStrcat(traceBuffer, "[");
  1506. dStrcat(traceBuffer, packageName);
  1507. dStrcat(traceBuffer, "]");
  1508. }
  1509. if(thisNamespace && thisNamespace->mName)
  1510. {
  1511. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1512. "%s::%s() - return %s", thisNamespace->mName, thisFunctionName, STR.getStringValue());
  1513. }
  1514. else
  1515. {
  1516. dSprintf(traceBuffer + dStrlen(traceBuffer), sizeof(traceBuffer) - dStrlen(traceBuffer),
  1517. "%s() - return %s", thisFunctionName, STR.getStringValue());
  1518. }
  1519. Con::printf("%s", traceBuffer);
  1520. }
  1521. }
  1522. else
  1523. {
  1524. delete[] const_cast<char*>(globalStrings);
  1525. delete[] globalFloats;
  1526. globalStrings = NULL;
  1527. globalFloats = NULL;
  1528. }
  1529. smCurrentCodeBlock = saveCodeBlock;
  1530. if(saveCodeBlock && saveCodeBlock->name)
  1531. {
  1532. Con::gCurrentFile = saveCodeBlock->name;
  1533. Con::gCurrentRoot = saveCodeBlock->mRoot;
  1534. }
  1535. decRefCount();
  1536. #ifdef TORQUE_DEBUG
  1537. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  1538. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  1539. #endif
  1540. return STR.getStringValue();
  1541. }
  1542. //------------------------------------------------------------