codeInterpreter.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "console/codeInterpreter.h"
  27. #include "console/compiler.h"
  28. #include "console/simBase.h"
  29. #include "console/telnetDebugger.h"
  30. #include "sim/netStringTable.h"
  31. #include "console/ICallMethod.h"
  32. #include "console/stringStack.h"
  33. #include "util/messaging/message.h"
  34. #include "core/strings/findMatch.h"
  35. #include "core/strings/stringUnit.h"
  36. #include "console/console.h"
  37. #include "console/consoleInternal.h"
  38. //#define TORQUE_VALIDATE_STACK
  39. using namespace Compiler;
  40. enum EvalConstants
  41. {
  42. MaxStackSize = 1024,
  43. FieldBufferSizeString = 2048,
  44. FieldBufferSizeNumeric = 128,
  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. extern S32 gObjectCopyFailures;
  54. }
  55. // Gets a component of an object's field value or a variable and returns it
  56. // in val.
  57. static void getFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField, char val[])
  58. {
  59. const char* prevVal = NULL;
  60. // Grab value from object.
  61. if (object && field)
  62. prevVal = object->getDataField(field, array);
  63. // Otherwise, grab from the string stack. The value coming in will always
  64. // be a string because that is how multicomponent variables are handled.
  65. else
  66. prevVal = STR.getStringValue();
  67. // Make sure we got a value.
  68. if (prevVal && *prevVal)
  69. {
  70. static const StringTableEntry xyzw[] =
  71. {
  72. StringTable->insert("x"),
  73. StringTable->insert("y"),
  74. StringTable->insert("z"),
  75. StringTable->insert("w")
  76. };
  77. static const StringTableEntry rgba[] =
  78. {
  79. StringTable->insert("r"),
  80. StringTable->insert("g"),
  81. StringTable->insert("b"),
  82. StringTable->insert("a")
  83. };
  84. // Translate xyzw and rgba into the indexed component
  85. // of the variable or field.
  86. //
  87. // Review: Should we use strncpy to prevent a buffer overflow?
  88. if (subField == xyzw[0] || subField == rgba[0])
  89. dStrcpy(val, StringUnit::getUnit(prevVal, 0, " \t\n"));
  90. else if (subField == xyzw[1] || subField == rgba[1])
  91. dStrcpy(val, StringUnit::getUnit(prevVal, 1, " \t\n"));
  92. else if (subField == xyzw[2] || subField == rgba[2])
  93. dStrcpy(val, StringUnit::getUnit(prevVal, 2, " \t\n"));
  94. else if (subField == xyzw[3] || subField == rgba[3])
  95. dStrcpy(val, StringUnit::getUnit(prevVal, 3, " \t\n"));
  96. else
  97. val[0] = 0;
  98. }
  99. else
  100. val[0] = 0;
  101. }
  102. // Sets a component of an object's field value based on the sub field. 'x' will
  103. // set the first field, 'y' the second, and 'z' the third.
  104. static void setFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField)
  105. {
  106. // Copy the current string value
  107. char strValue[1024];
  108. dStrncpy(strValue, STR.getStringValue(), 1024);
  109. char val[1024] = "";
  110. const char* prevVal = NULL;
  111. // Set the value on an object field.
  112. if (object && field)
  113. prevVal = object->getDataField(field, array);
  114. // Set the value on a variable.
  115. else if (gEvalState.currentVariable)
  116. prevVal = gEvalState.getStringVariable();
  117. // Ensure that the variable has a value
  118. if (!prevVal)
  119. return;
  120. static const StringTableEntry xyzw[] =
  121. {
  122. StringTable->insert("x"),
  123. StringTable->insert("y"),
  124. StringTable->insert("z"),
  125. StringTable->insert("w")
  126. };
  127. static const StringTableEntry rgba[] =
  128. {
  129. StringTable->insert("r"),
  130. StringTable->insert("g"),
  131. StringTable->insert("b"),
  132. StringTable->insert("a")
  133. };
  134. // Insert the value into the specified
  135. // component of the string.
  136. //
  137. // Review: Should we use strncpy to prevent a buffer overflow?
  138. if (subField == xyzw[0] || subField == rgba[0])
  139. dStrcpy(val, StringUnit::setUnit(prevVal, 0, strValue, " \t\n"));
  140. else if (subField == xyzw[1] || subField == rgba[1])
  141. dStrcpy(val, StringUnit::setUnit(prevVal, 1, strValue, " \t\n"));
  142. else if (subField == xyzw[2] || subField == rgba[2])
  143. dStrcpy(val, StringUnit::setUnit(prevVal, 2, strValue, " \t\n"));
  144. else if (subField == xyzw[3] || subField == rgba[3])
  145. dStrcpy(val, StringUnit::setUnit(prevVal, 3, strValue, " \t\n"));
  146. if (val[0] != 0)
  147. {
  148. // Update the field or variable.
  149. if (object && field)
  150. object->setDataField(field, 0, val);
  151. else if (gEvalState.currentVariable)
  152. gEvalState.setStringVariable(val);
  153. }
  154. }
  155. extern ExprEvalState gEvalState;
  156. char sTraceBuffer[1024];
  157. StringStack STR;
  158. ConsoleValueStack CSTK;
  159. U32 _FLT = 0; ///< Stack pointer for floatStack.
  160. U32 _UINT = 0; ///< Stack pointer for intStack.
  161. U32 _ITER = 0; ///< Stack pointer for iterStack.
  162. IterStackRecord iterStack[MaxStackSize];
  163. F64 floatStack[MaxStackSize];
  164. S64 intStack[MaxStackSize];
  165. char curFieldArray[256];
  166. char prevFieldArray[256];
  167. typedef OPCodeReturn(CodeInterpreter::*OpFn)(U32&);
  168. static OpFn gOpCodeArray[MAX_OP_CODELEN];
  169. CodeInterpreter::CodeInterpreter(CodeBlock *cb) :
  170. mCodeBlock(cb),
  171. mIterDepth(0),
  172. mCurFloatTable(nullptr),
  173. mCurStringTable(nullptr),
  174. mThisFunctionName(nullptr),
  175. mPopFrame(false),
  176. mObjectCreationStackIndex(0),
  177. mCurrentNewObject(nullptr),
  178. mFailJump(0),
  179. mPrevField(nullptr),
  180. mCurField(nullptr),
  181. mPrevObject(nullptr),
  182. mCurObject(nullptr),
  183. mSaveObject(nullptr),
  184. mThisObject(nullptr),
  185. mNSEntry(nullptr),
  186. mCurFNDocBlock(nullptr),
  187. mCurNSDocBlock(nullptr),
  188. mCallArgc(0),
  189. mCallArgv(nullptr),
  190. mSaveCodeBlock(nullptr),
  191. mCurrentInstruction(0)
  192. {
  193. }
  194. CodeInterpreter::~CodeInterpreter()
  195. {
  196. }
  197. void CodeInterpreter::init()
  198. {
  199. gOpCodeArray[OP_FUNC_DECL] = &CodeInterpreter::op_func_decl;
  200. gOpCodeArray[OP_CREATE_OBJECT] = &CodeInterpreter::op_create_object;
  201. gOpCodeArray[OP_ADD_OBJECT] = &CodeInterpreter::op_add_object;
  202. gOpCodeArray[OP_END_OBJECT] = &CodeInterpreter::op_end_object;
  203. gOpCodeArray[OP_FINISH_OBJECT] = &CodeInterpreter::op_finish_object;
  204. gOpCodeArray[OP_JMPIFFNOT] = &CodeInterpreter::op_jmpiffnot;
  205. gOpCodeArray[OP_JMPIFNOT] = &CodeInterpreter::op_jmpifnot;
  206. gOpCodeArray[OP_JMPIFF] = &CodeInterpreter::op_jmpiff;
  207. gOpCodeArray[OP_JMPIF] = &CodeInterpreter::op_jmpif;
  208. gOpCodeArray[OP_JMPIFNOT_NP] = &CodeInterpreter::op_jmpifnot_np;
  209. gOpCodeArray[OP_JMPIF_NP] = &CodeInterpreter::op_jmpif_np;
  210. gOpCodeArray[OP_JMP] = &CodeInterpreter::op_jmp;
  211. gOpCodeArray[OP_RETURN] = &CodeInterpreter::op_return;
  212. gOpCodeArray[OP_RETURN_VOID] = &CodeInterpreter::op_return_void;
  213. gOpCodeArray[OP_RETURN_FLT] = &CodeInterpreter::op_return_flt;
  214. gOpCodeArray[OP_RETURN_UINT] = &CodeInterpreter::op_return_uint;
  215. gOpCodeArray[OP_CMPEQ] = &CodeInterpreter::op_cmpeq;
  216. gOpCodeArray[OP_CMPGR] = &CodeInterpreter::op_cmpgr;
  217. gOpCodeArray[OP_CMPGE] = &CodeInterpreter::op_cmpge;
  218. gOpCodeArray[OP_CMPLT] = &CodeInterpreter::op_cmplt;
  219. gOpCodeArray[OP_CMPLE] = &CodeInterpreter::op_cmple;
  220. gOpCodeArray[OP_CMPNE] = &CodeInterpreter::op_cmpne;
  221. gOpCodeArray[OP_XOR] = &CodeInterpreter::op_xor;
  222. gOpCodeArray[OP_MOD] = &CodeInterpreter::op_mod;
  223. gOpCodeArray[OP_BITAND] = &CodeInterpreter::op_bitand;
  224. gOpCodeArray[OP_BITOR] = &CodeInterpreter::op_bitor;
  225. gOpCodeArray[OP_NOT] = &CodeInterpreter::op_not;
  226. gOpCodeArray[OP_NOTF] = &CodeInterpreter::op_notf;
  227. gOpCodeArray[OP_ONESCOMPLEMENT] = &CodeInterpreter::op_onescomplement;
  228. gOpCodeArray[OP_SHR] = &CodeInterpreter::op_shr;
  229. gOpCodeArray[OP_SHL] = &CodeInterpreter::op_shl;
  230. gOpCodeArray[OP_AND] = &CodeInterpreter::op_and;
  231. gOpCodeArray[OP_OR] = &CodeInterpreter::op_or;
  232. gOpCodeArray[OP_ADD] = &CodeInterpreter::op_add;
  233. gOpCodeArray[OP_SUB] = &CodeInterpreter::op_sub;
  234. gOpCodeArray[OP_MUL] = &CodeInterpreter::op_mul;
  235. gOpCodeArray[OP_DIV] = &CodeInterpreter::op_div;
  236. gOpCodeArray[OP_NEG] = &CodeInterpreter::op_neg;
  237. gOpCodeArray[OP_INC] = &CodeInterpreter::op_inc;
  238. gOpCodeArray[OP_DEC] = &CodeInterpreter::op_dec;
  239. gOpCodeArray[OP_SETCURVAR] = &CodeInterpreter::op_setcurvar;
  240. gOpCodeArray[OP_SETCURVAR_CREATE] = &CodeInterpreter::op_setcurvar_create;
  241. gOpCodeArray[OP_SETCURVAR_ARRAY] = &CodeInterpreter::op_setcurvar_array;
  242. gOpCodeArray[OP_SETCURVAR_ARRAY_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_varlookup;
  243. gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE] = &CodeInterpreter::op_setcurvar_array_create;
  244. gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_create_varlookup;
  245. gOpCodeArray[OP_LOADVAR_UINT] = &CodeInterpreter::op_loadvar_uint;
  246. gOpCodeArray[OP_LOADVAR_FLT] = &CodeInterpreter::op_loadvar_flt;
  247. gOpCodeArray[OP_LOADVAR_STR] = &CodeInterpreter::op_loadvar_str;
  248. gOpCodeArray[OP_LOADVAR_VAR] = &CodeInterpreter::op_loadvar_var;
  249. gOpCodeArray[OP_SAVEVAR_UINT] = &CodeInterpreter::op_savevar_uint;
  250. gOpCodeArray[OP_SAVEVAR_FLT] = &CodeInterpreter::op_savevar_flt;
  251. gOpCodeArray[OP_SAVEVAR_STR] = &CodeInterpreter::op_savevar_str;
  252. gOpCodeArray[OP_SAVEVAR_VAR] = &CodeInterpreter::op_savevar_var;
  253. gOpCodeArray[OP_SETCUROBJECT] = &CodeInterpreter::op_setcurobject;
  254. gOpCodeArray[OP_SETCUROBJECT_INTERNAL] = &CodeInterpreter::op_setcurobject_internal;
  255. gOpCodeArray[OP_SETCUROBJECT_NEW] = &CodeInterpreter::op_setcurobject_new;
  256. gOpCodeArray[OP_SETCURFIELD] = &CodeInterpreter::op_setcurfield;
  257. gOpCodeArray[OP_SETCURFIELD_ARRAY] = &CodeInterpreter::op_setcurfield_array;
  258. gOpCodeArray[OP_SETCURFIELD_TYPE] = &CodeInterpreter::op_setcurfield_type;
  259. gOpCodeArray[OP_SETCURFIELD_ARRAY_VAR] = &CodeInterpreter::op_setcurfield_array_var;
  260. gOpCodeArray[OP_SETCURFIELD_THIS] = &CodeInterpreter::op_setcurfield_this;
  261. gOpCodeArray[OP_LOADFIELD_UINT] = &CodeInterpreter::op_loadfield_uint;
  262. gOpCodeArray[OP_LOADFIELD_FLT] = &CodeInterpreter::op_loadfield_flt;
  263. gOpCodeArray[OP_LOADFIELD_STR] = &CodeInterpreter::op_loadfield_str;
  264. gOpCodeArray[OP_SAVEFIELD_UINT] = &CodeInterpreter::op_savefield_uint;
  265. gOpCodeArray[OP_SAVEFIELD_FLT] = &CodeInterpreter::op_savefield_flt;
  266. gOpCodeArray[OP_SAVEFIELD_STR] = &CodeInterpreter::op_savefield_str;
  267. gOpCodeArray[OP_STR_TO_UINT] = &CodeInterpreter::op_str_to_uint;
  268. gOpCodeArray[OP_STR_TO_FLT] = &CodeInterpreter::op_str_to_flt;
  269. gOpCodeArray[OP_STR_TO_NONE] = &CodeInterpreter::op_str_to_none;
  270. gOpCodeArray[OP_FLT_TO_UINT] = &CodeInterpreter::op_flt_to_uint;
  271. gOpCodeArray[OP_FLT_TO_STR] = &CodeInterpreter::op_flt_to_str;
  272. gOpCodeArray[OP_FLT_TO_NONE] = &CodeInterpreter::op_flt_to_none;
  273. gOpCodeArray[OP_UINT_TO_FLT] = &CodeInterpreter::op_uint_to_flt;
  274. gOpCodeArray[OP_UINT_TO_STR] = &CodeInterpreter::op_uint_to_str;
  275. gOpCodeArray[OP_UINT_TO_NONE] = &CodeInterpreter::op_uint_to_none;
  276. gOpCodeArray[OP_COPYVAR_TO_NONE] = &CodeInterpreter::op_copyvar_to_none;
  277. gOpCodeArray[OP_LOADIMMED_UINT] = &CodeInterpreter::op_loadimmed_uint;
  278. gOpCodeArray[OP_LOADIMMED_FLT] = &CodeInterpreter::op_loadimmed_flt;
  279. gOpCodeArray[OP_TAG_TO_STR] = &CodeInterpreter::op_tag_to_str;
  280. gOpCodeArray[OP_LOADIMMED_STR] = &CodeInterpreter::op_loadimmed_str;
  281. gOpCodeArray[OP_DOCBLOCK_STR] = &CodeInterpreter::op_docblock_str;
  282. gOpCodeArray[OP_LOADIMMED_IDENT] = &CodeInterpreter::op_loadimmed_ident;
  283. gOpCodeArray[OP_CALLFUNC_RESOLVE] = &CodeInterpreter::op_callfunc_resolve;
  284. gOpCodeArray[OP_CALLFUNC] = &CodeInterpreter::op_callfunc;
  285. gOpCodeArray[OP_CALLFUNC_POINTER] = &CodeInterpreter::op_callfunc_pointer;
  286. gOpCodeArray[OP_CALLFUNC_THIS] = &CodeInterpreter::op_callfunc_this;
  287. gOpCodeArray[OP_ADVANCE_STR] = &CodeInterpreter::op_advance_str;
  288. gOpCodeArray[OP_ADVANCE_STR_APPENDCHAR] = &CodeInterpreter::op_advance_str_appendchar;
  289. gOpCodeArray[OP_ADVANCE_STR_COMMA] = &CodeInterpreter::op_advance_str_comma;
  290. gOpCodeArray[OP_ADVANCE_STR_NUL] = &CodeInterpreter::op_advance_str_nul;
  291. gOpCodeArray[OP_REWIND_STR] = &CodeInterpreter::op_rewind_str;
  292. gOpCodeArray[OP_TERMINATE_REWIND_STR] = &CodeInterpreter::op_terminate_rewind_str;
  293. gOpCodeArray[OP_COMPARE_STR] = &CodeInterpreter::op_compare_str;
  294. gOpCodeArray[OP_PUSH] = &CodeInterpreter::op_push;
  295. gOpCodeArray[OP_PUSH_UINT] = &CodeInterpreter::op_push_uint;
  296. gOpCodeArray[OP_PUSH_FLT] = &CodeInterpreter::op_push_flt;
  297. gOpCodeArray[OP_PUSH_VAR] = &CodeInterpreter::op_push_var;
  298. gOpCodeArray[OP_PUSH_THIS] = &CodeInterpreter::op_push_this;
  299. gOpCodeArray[OP_PUSH_FRAME] = &CodeInterpreter::op_push_frame;
  300. gOpCodeArray[OP_ASSERT] = &CodeInterpreter::op_assert;
  301. gOpCodeArray[OP_BREAK] = &CodeInterpreter::op_break;
  302. gOpCodeArray[OP_ITER_BEGIN_STR] = &CodeInterpreter::op_iter_begin_str;
  303. gOpCodeArray[OP_ITER_BEGIN] = &CodeInterpreter::op_iter_begin;
  304. gOpCodeArray[OP_ITER] = &CodeInterpreter::op_iter;
  305. gOpCodeArray[OP_ITER_END] = &CodeInterpreter::op_iter_end;
  306. gOpCodeArray[OP_INVALID] = &CodeInterpreter::op_invalid;
  307. }
  308. ConsoleValueRef CodeInterpreter::exec(U32 ip,
  309. StringTableEntry functionName,
  310. Namespace *thisNamespace,
  311. U32 argc,
  312. ConsoleValueRef *argv,
  313. bool noCalls,
  314. StringTableEntry packageName,
  315. S32 setFrame)
  316. {
  317. mExec.functionName = functionName;
  318. mExec.thisNamespace = thisNamespace;
  319. mExec.argc = argc;
  320. mExec.argv = argv;
  321. mExec.noCalls = noCalls;
  322. mExec.packageName = packageName;
  323. mExec.setFrame = setFrame;
  324. mCodeBlock->incRefCount();
  325. mPopFrame = false;
  326. #ifdef TORQUE_VALIDATE_STACK
  327. U32 stackStart = STR.mStartStackSize;
  328. #endif
  329. STR.clearFunctionOffset(); // ensures arg buffer offset is back to 0
  330. // Lets load up our function arguments.
  331. parseArgs(ip);
  332. // Grab the state of the telenet debugger here once
  333. // so that the push and pop frames are always balanced.
  334. const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
  335. if (telDebuggerOn && setFrame < 0)
  336. TelDebugger->pushStackFrame();
  337. mSaveCodeBlock = CodeBlock::smCurrentCodeBlock;
  338. CodeBlock::smCurrentCodeBlock = mCodeBlock;
  339. if (mCodeBlock->name)
  340. {
  341. Con::gCurrentFile = mCodeBlock->name;
  342. Con::gCurrentRoot = mCodeBlock->modPath;
  343. }
  344. U32 *code = mCodeBlock->code;
  345. while (true)
  346. {
  347. mCurrentInstruction = code[ip++];
  348. mNSEntry = nullptr;
  349. #ifdef TORQUE_VALIDATE_STACK
  350. // OP Code check.
  351. AssertFatal(mCurrentInstruction < MAX_OP_CODELEN, "Invalid OP code in script interpreter");
  352. #endif
  353. breakContinueLabel:
  354. OPCodeReturn ret = (this->*gOpCodeArray[mCurrentInstruction])(ip);
  355. if (ret == OPCodeReturn::exitCode)
  356. goto exitLabel;
  357. else if (ret == OPCodeReturn::breakContinue)
  358. goto breakContinueLabel;
  359. }
  360. exitLabel:
  361. if (telDebuggerOn && setFrame < 0)
  362. TelDebugger->popStackFrame();
  363. if (mPopFrame)
  364. gEvalState.popFrame();
  365. if (argv)
  366. {
  367. if (gEvalState.traceOn)
  368. {
  369. sTraceBuffer[0] = 0;
  370. dStrcat(sTraceBuffer, "Leaving ", 1024);
  371. if (packageName)
  372. {
  373. dStrcat(sTraceBuffer, "[", 1024);
  374. dStrcat(sTraceBuffer, packageName, 1024);
  375. dStrcat(sTraceBuffer, "]", 1024);
  376. }
  377. if (thisNamespace && thisNamespace->mName)
  378. {
  379. dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), sizeof(sTraceBuffer) - dStrlen(sTraceBuffer),
  380. "%s::%s() - return %s", thisNamespace->mName, mThisFunctionName, STR.getStringValue());
  381. }
  382. else
  383. {
  384. dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), sizeof(sTraceBuffer) - dStrlen(sTraceBuffer),
  385. "%s() - return %s", mThisFunctionName, STR.getStringValue());
  386. }
  387. Con::printf("%s", sTraceBuffer);
  388. }
  389. }
  390. CodeBlock::smCurrentCodeBlock = mSaveCodeBlock;
  391. if (mSaveCodeBlock && mSaveCodeBlock->name)
  392. {
  393. Con::gCurrentFile = mSaveCodeBlock->name;
  394. Con::gCurrentRoot = mSaveCodeBlock->modPath;
  395. }
  396. mCodeBlock->decRefCount();
  397. #ifdef TORQUE_VALIDATE_STACK
  398. AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
  399. AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
  400. #endif
  401. return mReturnValue;
  402. }
  403. void CodeInterpreter::parseArgs(U32 &ip)
  404. {
  405. U32 *code = mCodeBlock->code;
  406. if (mExec.argv)
  407. {
  408. U32 fnArgc = code[ip + 2 + 6];
  409. mThisFunctionName = Compiler::CodeToSTE(code, ip);
  410. S32 wantedArgc = getMin(mExec.argc - 1, fnArgc); // argv[0] is func name
  411. if (gEvalState.traceOn)
  412. {
  413. sTraceBuffer[0] = 0;
  414. dStrcat(sTraceBuffer, "Entering ", 1024);
  415. if (mExec.packageName)
  416. {
  417. dStrcat(sTraceBuffer, "[", 1024);
  418. dStrcat(sTraceBuffer, mExec.packageName, 1024);
  419. dStrcat(sTraceBuffer, "]", 1024);
  420. }
  421. if (mExec.thisNamespace && mExec.thisNamespace->mName)
  422. {
  423. dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), sizeof(sTraceBuffer) - dStrlen(sTraceBuffer),
  424. "%s::%s(", mExec.thisNamespace->mName, mThisFunctionName);
  425. }
  426. else
  427. {
  428. dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), sizeof(sTraceBuffer) - dStrlen(sTraceBuffer),
  429. "%s(", mThisFunctionName);
  430. }
  431. for (S32 i = 0; i < wantedArgc; i++)
  432. {
  433. dStrcat(sTraceBuffer, mExec.argv[i + 1], 1024);
  434. if (i != wantedArgc - 1)
  435. dStrcat(sTraceBuffer, ", ", 1024);
  436. }
  437. dStrcat(sTraceBuffer, ")", 1024);
  438. Con::printf("%s", sTraceBuffer);
  439. }
  440. gEvalState.pushFrame(mThisFunctionName, mExec.thisNamespace);
  441. mPopFrame = true;
  442. StringTableEntry thisPointer = StringTable->insert("%this");
  443. for (S32 i = 0; i < wantedArgc; i++)
  444. {
  445. StringTableEntry var = Compiler::CodeToSTE(code, ip + (2 + 6 + 1) + (i * 2));
  446. gEvalState.setCurVarNameCreate(var);
  447. ConsoleValueRef ref = mExec.argv[i + 1];
  448. switch (ref.getType())
  449. {
  450. case ConsoleValue::TypeInternalInt:
  451. gEvalState.setIntVariable(ref);
  452. break;
  453. case ConsoleValue::TypeInternalFloat:
  454. gEvalState.setFloatVariable(ref);
  455. break;
  456. case ConsoleValue::TypeInternalStringStackPtr:
  457. gEvalState.setStringStackPtrVariable(ref.getStringStackPtrValue());
  458. break;
  459. case ConsoleValue::TypeInternalStackString:
  460. case ConsoleValue::TypeInternalString:
  461. default:
  462. gEvalState.setStringVariable(ref);
  463. break;
  464. }
  465. if (var == thisPointer)
  466. {
  467. // %this gets optimized as it is flagged as a constant.
  468. // Since it is guarenteed to be constant, we can then perform optimizations.
  469. gEvalState.currentVariable->mIsConstant = true;
  470. // Store a reference to the this pointer object.
  471. mThisObject = Sim::findObject(gEvalState.getStringVariable());
  472. }
  473. }
  474. ip = ip + (fnArgc * 2) + (2 + 6 + 1);
  475. mCurFloatTable = mCodeBlock->functionFloats;
  476. mCurStringTable = mCodeBlock->functionStrings;
  477. }
  478. else
  479. {
  480. mCurFloatTable = mCodeBlock->globalFloats;
  481. mCurStringTable = mCodeBlock->globalStrings;
  482. // If requested stack frame isn't available, request a new one
  483. // (this prevents assert failures when creating local
  484. // variables without a stack frame)
  485. if (gEvalState.getStackDepth() <= mExec.setFrame)
  486. mExec.setFrame = -1;
  487. // Do we want this code to execute using a new stack frame?
  488. if (mExec.setFrame < 0)
  489. {
  490. gEvalState.pushFrame(NULL, NULL);
  491. mPopFrame = true;
  492. }
  493. else
  494. {
  495. // We want to copy a reference to an existing stack frame
  496. // on to the top of the stack. Any change that occurs to
  497. // the locals during this new frame will also occur in the
  498. // original frame.
  499. S32 stackIndex = gEvalState.getStackDepth() - mExec.setFrame - 1;
  500. gEvalState.pushFrameRef(stackIndex);
  501. mPopFrame = true;
  502. }
  503. }
  504. }
  505. OPCodeReturn CodeInterpreter::op_func_decl(U32 &ip)
  506. {
  507. U32 *code = mCodeBlock->code;
  508. if (!mExec.noCalls)
  509. {
  510. StringTableEntry fnName = CodeToSTE(code, ip);
  511. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  512. StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
  513. bool hasBody = (code[ip + 6] & 0x01) != 0;
  514. U32 lineNumber = code[ip + 6] >> 1;
  515. Namespace::unlinkPackages();
  516. Namespace *ns = Namespace::find(fnNamespace, fnPackage);
  517. ns->addFunction(fnName, mCodeBlock, hasBody ? ip : 0, mCurFNDocBlock ? dStrdup(mCurFNDocBlock) : NULL, lineNumber);// if no body, set the IP to 0
  518. if (mCurNSDocBlock)
  519. {
  520. // If we have a docblock before we declare the function in the script file,
  521. // this will attempt to set the doc block to the function.
  522. // See OP_DOCBLOCK_STR
  523. if (fnNamespace == StringTable->lookup(mNSDocBlockClass))
  524. {
  525. char *usageStr = dStrdup(mCurNSDocBlock);
  526. usageStr[dStrlen(usageStr)] = '\0';
  527. ns->mUsage = usageStr;
  528. ns->mCleanUpUsage = true;
  529. mCurNSDocBlock = NULL;
  530. }
  531. }
  532. Namespace::relinkPackages();
  533. // If we had a docblock, it's definitely not valid anymore, so clear it out.
  534. mCurFNDocBlock = NULL;
  535. //Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
  536. }
  537. ip = code[ip + 7];
  538. return OPCodeReturn::success;
  539. }
  540. OPCodeReturn CodeInterpreter::op_create_object(U32 &ip)
  541. {
  542. U32 *code = mCodeBlock->code;
  543. // Read some useful info.
  544. StringTableEntry objParent = CodeToSTE(code, ip);
  545. bool isDataBlock = code[ip + 2];
  546. bool isInternal = code[ip + 3];
  547. bool isSingleton = code[ip + 4];
  548. U32 lineNumber = code[ip + 5];
  549. mFailJump = code[ip + 6];
  550. // If we don't allow calls, we certainly don't allow creating objects!
  551. // Moved this to after failJump is set. Engine was crashing when
  552. // noCalls = true and an object was being created at the beginning of
  553. // a file. ADL.
  554. if (mExec.noCalls)
  555. {
  556. ip = mFailJump;
  557. return OPCodeReturn::success;
  558. }
  559. // Push the old info to the stack
  560. //Assert( objectCreationStackIndex < objectCreationStackSize );
  561. mObjectCreationStack[mObjectCreationStackIndex].newObject = mCurrentNewObject;
  562. mObjectCreationStack[mObjectCreationStackIndex++].failJump = mFailJump;
  563. // Get the constructor information off the stack.
  564. CSTK.getArgcArgv(NULL, &mCallArgc, &mCallArgv);
  565. const char *objectName = mCallArgv[2];
  566. // Con::printf("Creating object...");
  567. // objectName = argv[1]...
  568. mCurrentNewObject = NULL;
  569. // Are we creating a datablock? If so, deal with case where we override
  570. // an old one.
  571. if (isDataBlock)
  572. {
  573. // Con::printf(" - is a datablock");
  574. // Find the old one if any.
  575. SimObject *db = Sim::getDataBlockGroup()->findObject(objectName);
  576. // Make sure we're not changing types on ourselves...
  577. if (db && dStricmp(db->getClassName(), mCallArgv[1]))
  578. {
  579. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare data block %s with a different class.", mCodeBlock->getFileLine(ip), objectName);
  580. ip = mFailJump;
  581. STR.popFrame();
  582. CSTK.popFrame();
  583. return OPCodeReturn::success;
  584. }
  585. // If there was one, set the currentNewObject and move on.
  586. if (db)
  587. mCurrentNewObject = db;
  588. }
  589. else if (!isInternal)
  590. {
  591. // IF we aren't looking at a local/internal object, then check if
  592. // this object already exists in the global space
  593. AbstractClassRep* rep = AbstractClassRep::findClassRep(objectName);
  594. if (rep != NULL) {
  595. Con::errorf(ConsoleLogEntry::General, "%s: Cannot name object [%s] the same name as a script class.",
  596. mCodeBlock->getFileLine(ip), objectName);
  597. ip = mFailJump;
  598. STR.popFrame();
  599. CSTK.popFrame();
  600. return OPCodeReturn::success;
  601. }
  602. SimObject *obj = Sim::findObject((const char*)objectName);
  603. if (obj /*&& !obj->isLocalName()*/)
  604. {
  605. if (isSingleton)
  606. {
  607. // Make sure we're not trying to change types
  608. if (dStricmp(obj->getClassName(), (const char*)mCallArgv[1]) != 0)
  609. {
  610. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s] with a different class [%s] - was [%s].",
  611. mCodeBlock->getFileLine(ip), objectName, (const char*)mCallArgv[1], obj->getClassName());
  612. ip = mFailJump;
  613. STR.popFrame();
  614. CSTK.popFrame();
  615. return OPCodeReturn::success;
  616. }
  617. // We're creating a singleton, so use the found object
  618. // instead of creating a new object.
  619. mCurrentNewObject = obj;
  620. }
  621. else
  622. {
  623. const char* redefineBehavior = Con::getVariable("$Con::redefineBehavior");
  624. if (dStricmp(redefineBehavior, "replaceExisting") == 0)
  625. {
  626. // Save our constructor args as the argv vector is stored on the
  627. // string stack and may get stomped if deleteObject triggers
  628. // script execution.
  629. ConsoleValueRef savedArgv[StringStack::MaxArgs];
  630. for (int i = 0; i< mCallArgc; i++) {
  631. savedArgv[i] = mCallArgv[i];
  632. }
  633. //dMemcpy( savedArgv, callArgv, sizeof( savedArgv[ 0 ] ) * callArgc );
  634. // Prevent stack value corruption
  635. CSTK.pushFrame();
  636. STR.pushFrame();
  637. // --
  638. obj->deleteObject();
  639. obj = NULL;
  640. // Prevent stack value corruption
  641. CSTK.popFrame();
  642. STR.popFrame();
  643. // --
  644. //dMemcpy( callArgv, savedArgv, sizeof( callArgv[ 0 ] ) * callArgc );
  645. for (int i = 0; i<mCallArgc; i++) {
  646. mCallArgv[i] = savedArgv[i];
  647. }
  648. }
  649. else if (dStricmp(redefineBehavior, "renameNew") == 0)
  650. {
  651. for (U32 i = 1;; ++i)
  652. {
  653. String newName = String::ToString("%s%i", objectName, i);
  654. if (!Sim::findObject(newName))
  655. {
  656. objectName = StringTable->insert(newName);
  657. break;
  658. }
  659. }
  660. }
  661. else if (dStricmp(redefineBehavior, "unnameNew") == 0)
  662. {
  663. objectName = StringTable->insert("");
  664. }
  665. else if (dStricmp(redefineBehavior, "postfixNew") == 0)
  666. {
  667. const char* postfix = Con::getVariable("$Con::redefineBehaviorPostfix");
  668. String newName = String::ToString("%s%s", objectName, postfix);
  669. if (Sim::findObject(newName))
  670. {
  671. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object with postfix [%s].",
  672. mCodeBlock->getFileLine(ip), newName.c_str());
  673. ip = mFailJump;
  674. STR.popFrame();
  675. CSTK.popFrame();
  676. return OPCodeReturn::success;
  677. }
  678. else
  679. objectName = StringTable->insert(newName);
  680. }
  681. else
  682. {
  683. Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s].",
  684. mCodeBlock->getFileLine(ip), objectName);
  685. ip = mFailJump;
  686. STR.popFrame();
  687. CSTK.popFrame();
  688. return OPCodeReturn::success;
  689. }
  690. }
  691. }
  692. }
  693. STR.popFrame();
  694. CSTK.popFrame();
  695. if (!mCurrentNewObject)
  696. {
  697. // Well, looks like we have to create a new object.
  698. ConsoleObject *object = ConsoleObject::create((const char*)mCallArgv[1]);
  699. // Deal with failure!
  700. if (!object)
  701. {
  702. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
  703. ip = mFailJump;
  704. return OPCodeReturn::success;
  705. }
  706. // Do special datablock init if appropros
  707. if (isDataBlock)
  708. {
  709. SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(object);
  710. if (dataBlock)
  711. {
  712. dataBlock->assignId();
  713. }
  714. else
  715. {
  716. // They tried to make a non-datablock with a datablock keyword!
  717. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
  718. // Clean up...
  719. delete object;
  720. mCurrentNewObject = NULL;
  721. ip = mFailJump;
  722. return OPCodeReturn::success;
  723. }
  724. }
  725. // Finally, set currentNewObject to point to the new one.
  726. mCurrentNewObject = dynamic_cast<SimObject *>(object);
  727. // Deal with the case of a non-SimObject.
  728. if (!mCurrentNewObject)
  729. {
  730. Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
  731. delete object;
  732. mCurrentNewObject = NULL;
  733. ip = mFailJump;
  734. return OPCodeReturn::success;
  735. }
  736. // Set the declaration line
  737. mCurrentNewObject->setDeclarationLine(lineNumber);
  738. // Set the file that this object was created in
  739. mCurrentNewObject->setFilename(mCodeBlock->name);
  740. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  741. if (*objParent)
  742. {
  743. // Find it!
  744. SimObject *parent;
  745. if (Sim::findObject(objParent, parent))
  746. {
  747. // Con::printf(" - Parent object found: %s", parent->getClassName());
  748. mCurrentNewObject->setCopySource(parent);
  749. mCurrentNewObject->assignFieldsFrom(parent);
  750. // copy any substitution statements
  751. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  752. if (parent_db)
  753. {
  754. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
  755. if (currentNewObject_db)
  756. currentNewObject_db->copySubstitutionsFrom(parent_db);
  757. }
  758. }
  759. else
  760. {
  761. if (Con::gObjectCopyFailures == -1)
  762. Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", mCodeBlock->getFileLine(ip), objParent, (const char*)mCallArgv[1]);
  763. else
  764. ++Con::gObjectCopyFailures;
  765. // Fail to create the object.
  766. delete object;
  767. mCurrentNewObject = NULL;
  768. ip = mFailJump;
  769. return OPCodeReturn::success;
  770. }
  771. }
  772. // If a name was passed, assign it.
  773. if (objectName[0])
  774. {
  775. if (!isInternal)
  776. mCurrentNewObject->assignName(objectName);
  777. else
  778. mCurrentNewObject->setInternalName(objectName);
  779. // Set the original name
  780. mCurrentNewObject->setOriginalName(objectName);
  781. }
  782. // Prevent stack value corruption
  783. CSTK.pushFrame();
  784. STR.pushFrame();
  785. // --
  786. // Do the constructor parameters.
  787. if (!mCurrentNewObject->processArguments(mCallArgc - 3, mCallArgv + 3))
  788. {
  789. delete mCurrentNewObject;
  790. mCurrentNewObject = NULL;
  791. ip = mFailJump;
  792. // Prevent stack value corruption
  793. CSTK.popFrame();
  794. STR.popFrame();
  795. // --
  796. return OPCodeReturn::success;
  797. }
  798. // Prevent stack value corruption
  799. CSTK.popFrame();
  800. STR.popFrame();
  801. // --
  802. // If it's not a datablock, allow people to modify bits of it.
  803. if (!isDataBlock)
  804. {
  805. mCurrentNewObject->setModStaticFields(true);
  806. mCurrentNewObject->setModDynamicFields(true);
  807. }
  808. }
  809. else
  810. {
  811. mCurrentNewObject->reloadReset(); // AFX (reload-reset)
  812. // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
  813. if (*objParent)
  814. {
  815. // Find it!
  816. SimObject *parent;
  817. if (Sim::findObject(objParent, parent))
  818. {
  819. // Con::printf(" - Parent object found: %s", parent->getClassName());
  820. // temporarily block name change
  821. SimObject::preventNameChanging = true;
  822. mCurrentNewObject->setCopySource(parent);
  823. mCurrentNewObject->assignFieldsFrom(parent);
  824. // restore name changing
  825. SimObject::preventNameChanging = false;
  826. // copy any substitution statements
  827. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  828. if (parent_db)
  829. {
  830. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
  831. if (currentNewObject_db)
  832. currentNewObject_db->copySubstitutionsFrom(parent_db);
  833. }
  834. }
  835. else
  836. Con::errorf(ConsoleLogEntry::General, "%d: Unable to find parent object %s for %s.", lineNumber, objParent, (const char*)mCallArgv[1]);
  837. }
  838. }
  839. // Advance the IP past the create info...
  840. ip += 7;
  841. return OPCodeReturn::success;
  842. }
  843. OPCodeReturn CodeInterpreter::op_add_object(U32 &ip)
  844. {
  845. // See OP_SETCURVAR for why we do this.
  846. mCurFNDocBlock = NULL;
  847. mCurNSDocBlock = NULL;
  848. // Do we place this object at the root?
  849. bool placeAtRoot = mCodeBlock->code[ip++];
  850. // Con::printf("Adding object %s", currentNewObject->getName());
  851. // Prevent stack value corruption
  852. CSTK.pushFrame();
  853. STR.pushFrame();
  854. // --
  855. // Make sure it wasn't already added, then add it.
  856. if (mCurrentNewObject->isProperlyAdded() == false)
  857. {
  858. bool ret = false;
  859. Message *msg = dynamic_cast<Message *>(mCurrentNewObject);
  860. if (msg)
  861. {
  862. SimObjectId id = Message::getNextMessageID();
  863. if (id != 0xffffffff)
  864. ret = mCurrentNewObject->registerObject(id);
  865. else
  866. Con::errorf("%s: No more object IDs available for messages", mCodeBlock->getFileLine(ip));
  867. }
  868. else
  869. ret = mCurrentNewObject->registerObject();
  870. if (!ret)
  871. {
  872. // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
  873. Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", mCodeBlock->getFileLine(ip), mCurrentNewObject->getName(), mCurrentNewObject->getClassName());
  874. delete mCurrentNewObject;
  875. mCurrentNewObject = NULL;
  876. ip = mFailJump;
  877. // Prevent stack value corruption
  878. CSTK.popFrame();
  879. STR.popFrame();
  880. // --
  881. return OPCodeReturn::success;
  882. }
  883. }
  884. // Are we dealing with a datablock?
  885. SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(mCurrentNewObject);
  886. static String errorStr;
  887. // If so, preload it.
  888. if (dataBlock && !dataBlock->preload(true, errorStr))
  889. {
  890. Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", mCodeBlock->getFileLine(ip),
  891. mCurrentNewObject->getName(), errorStr.c_str());
  892. dataBlock->deleteObject();
  893. mCurrentNewObject = NULL;
  894. ip = mFailJump;
  895. // Prevent stack value corruption
  896. CSTK.popFrame();
  897. STR.popFrame();
  898. // --
  899. return OPCodeReturn::success;
  900. }
  901. // What group will we be added to, if any?
  902. U32 groupAddId = intStack[_UINT];
  903. SimGroup *grp = NULL;
  904. SimSet *set = NULL;
  905. bool isMessage = dynamic_cast<Message *>(mCurrentNewObject) != NULL;
  906. if (!placeAtRoot || !mCurrentNewObject->getGroup())
  907. {
  908. if (!isMessage)
  909. {
  910. if (!placeAtRoot)
  911. {
  912. // Otherwise just add to the requested group or set.
  913. if (!Sim::findObject(groupAddId, grp))
  914. Sim::findObject(groupAddId, set);
  915. }
  916. if (placeAtRoot)
  917. {
  918. // Deal with the instantGroup if we're being put at the root or we're adding to a component.
  919. if (Con::gInstantGroup.isEmpty()
  920. || !Sim::findObject(Con::gInstantGroup, grp))
  921. grp = Sim::getRootGroup();
  922. }
  923. }
  924. // If we didn't get a group, then make sure we have a pointer to
  925. // the rootgroup.
  926. if (!grp)
  927. grp = Sim::getRootGroup();
  928. // add to the parent group
  929. grp->addObject(mCurrentNewObject);
  930. // If for some reason the add failed, add the object to the
  931. // root group so it won't leak.
  932. if (!mCurrentNewObject->getGroup())
  933. Sim::getRootGroup()->addObject(mCurrentNewObject);
  934. // add to any set we might be in
  935. if (set)
  936. set->addObject(mCurrentNewObject);
  937. }
  938. // store the new object's ID on the stack (overwriting the group/set
  939. // id, if one was given, otherwise getting pushed)
  940. if (placeAtRoot)
  941. intStack[_UINT] = mCurrentNewObject->getId();
  942. else
  943. intStack[++_UINT] = mCurrentNewObject->getId();
  944. // Prevent stack value corruption
  945. CSTK.popFrame();
  946. STR.popFrame();
  947. // --
  948. return OPCodeReturn::success;
  949. }
  950. OPCodeReturn CodeInterpreter::op_end_object(U32 &ip)
  951. {
  952. // If we're not to be placed at the root, make sure we clean up
  953. // our group reference.
  954. bool placeAtRoot = mCodeBlock->code[ip++];
  955. if (!placeAtRoot)
  956. _UINT--;
  957. return OPCodeReturn::success;
  958. }
  959. OPCodeReturn CodeInterpreter::op_finish_object(U32 &ip)
  960. {
  961. if (mCurrentNewObject)
  962. mCurrentNewObject->onPostAdd();
  963. //Assert( objectCreationStackIndex >= 0 );
  964. // Restore the object info from the stack [7/9/2007 Black]
  965. mCurrentNewObject = mObjectCreationStack[--mObjectCreationStackIndex].newObject;
  966. mFailJump = mObjectCreationStack[mObjectCreationStackIndex].failJump;
  967. return OPCodeReturn::success;
  968. }
  969. OPCodeReturn CodeInterpreter::op_jmpiffnot(U32 &ip)
  970. {
  971. if (floatStack[_FLT--])
  972. {
  973. ip++;
  974. return OPCodeReturn::success;
  975. }
  976. ip = mCodeBlock->code[ip];
  977. return OPCodeReturn::success;
  978. }
  979. OPCodeReturn CodeInterpreter::op_jmpifnot(U32 &ip)
  980. {
  981. if (intStack[_UINT--])
  982. {
  983. ip++;
  984. return OPCodeReturn::success;
  985. }
  986. ip = mCodeBlock->code[ip];
  987. return OPCodeReturn::success;
  988. }
  989. OPCodeReturn CodeInterpreter::op_jmpiff(U32 &ip)
  990. {
  991. if (!floatStack[_FLT--])
  992. {
  993. ip++;
  994. return OPCodeReturn::success;
  995. }
  996. ip = mCodeBlock->code[ip];
  997. return OPCodeReturn::success;
  998. }
  999. OPCodeReturn CodeInterpreter::op_jmpif(U32 &ip)
  1000. {
  1001. if (!intStack[_UINT--])
  1002. {
  1003. ip++;
  1004. return OPCodeReturn::success;
  1005. }
  1006. ip = mCodeBlock->code[ip];
  1007. return OPCodeReturn::success;
  1008. }
  1009. OPCodeReturn CodeInterpreter::op_jmpifnot_np(U32 &ip)
  1010. {
  1011. if (intStack[_UINT])
  1012. {
  1013. _UINT--;
  1014. ip++;
  1015. return OPCodeReturn::success;
  1016. }
  1017. ip = mCodeBlock->code[ip];
  1018. return OPCodeReturn::success;
  1019. }
  1020. OPCodeReturn CodeInterpreter::op_jmpif_np(U32 &ip)
  1021. {
  1022. if (!intStack[_UINT])
  1023. {
  1024. _UINT--;
  1025. ip++;
  1026. return OPCodeReturn::success;
  1027. }
  1028. ip = mCodeBlock->code[ip];
  1029. return OPCodeReturn::success;
  1030. }
  1031. OPCodeReturn CodeInterpreter::op_jmp(U32 &ip)
  1032. {
  1033. ip = mCodeBlock->code[ip];
  1034. return OPCodeReturn::success;
  1035. }
  1036. OPCodeReturn CodeInterpreter::op_return_void(U32 &ip)
  1037. {
  1038. STR.setStringValue("");
  1039. // We're falling thru here on purpose.
  1040. OPCodeReturn ret = op_return(ip);
  1041. return ret;
  1042. }
  1043. OPCodeReturn CodeInterpreter::op_return(U32 &ip)
  1044. {
  1045. StringStackPtr retValue = STR.getStringValuePtr();
  1046. if (mIterDepth > 0)
  1047. {
  1048. // Clear iterator state.
  1049. while (mIterDepth > 0)
  1050. {
  1051. iterStack[--_ITER].mIsStringIter = false;
  1052. --mIterDepth;
  1053. }
  1054. STR.rewind();
  1055. STR.setStringValue(StringStackPtrRef(retValue).getPtr(&STR)); // Not nice but works.
  1056. retValue = STR.getStringValuePtr();
  1057. }
  1058. // Previously the return value was on the stack and would be returned using STR.getStringValue().
  1059. // Now though we need to wrap it in a ConsoleValueRef
  1060. mReturnValue.value = CSTK.pushStringStackPtr(retValue);
  1061. return OPCodeReturn::exitCode;
  1062. }
  1063. OPCodeReturn CodeInterpreter::op_return_flt(U32 &ip)
  1064. {
  1065. if (mIterDepth > 0)
  1066. {
  1067. // Clear iterator state.
  1068. while (mIterDepth > 0)
  1069. {
  1070. iterStack[--_ITER].mIsStringIter = false;
  1071. --mIterDepth;
  1072. }
  1073. }
  1074. mReturnValue.value = CSTK.pushFLT(floatStack[_FLT]);
  1075. _FLT--;
  1076. return OPCodeReturn::exitCode;
  1077. }
  1078. OPCodeReturn CodeInterpreter::op_return_uint(U32 &ip)
  1079. {
  1080. if (mIterDepth > 0)
  1081. {
  1082. // Clear iterator state.
  1083. while (mIterDepth > 0)
  1084. {
  1085. iterStack[--_ITER].mIsStringIter = false;
  1086. --mIterDepth;
  1087. }
  1088. }
  1089. mReturnValue.value = CSTK.pushUINT(intStack[_UINT]);
  1090. _UINT--;
  1091. return OPCodeReturn::exitCode;
  1092. }
  1093. OPCodeReturn CodeInterpreter::op_cmpeq(U32 &ip)
  1094. {
  1095. intStack[_UINT + 1] = bool(floatStack[_FLT] == floatStack[_FLT - 1]);
  1096. _UINT++;
  1097. _FLT -= 2;
  1098. return OPCodeReturn::success;
  1099. }
  1100. OPCodeReturn CodeInterpreter::op_cmpgr(U32 &ip)
  1101. {
  1102. intStack[_UINT + 1] = bool(floatStack[_FLT] > floatStack[_FLT - 1]);
  1103. _UINT++;
  1104. _FLT -= 2;
  1105. return OPCodeReturn::success;
  1106. }
  1107. OPCodeReturn CodeInterpreter::op_cmpge(U32 &ip)
  1108. {
  1109. intStack[_UINT + 1] = bool(floatStack[_FLT] >= floatStack[_FLT - 1]);
  1110. _UINT++;
  1111. _FLT -= 2;
  1112. return OPCodeReturn::success;
  1113. }
  1114. OPCodeReturn CodeInterpreter::op_cmplt(U32 &ip)
  1115. {
  1116. intStack[_UINT + 1] = bool(floatStack[_FLT] < floatStack[_FLT - 1]);
  1117. _UINT++;
  1118. _FLT -= 2;
  1119. return OPCodeReturn::success;
  1120. }
  1121. OPCodeReturn CodeInterpreter::op_cmple(U32 &ip)
  1122. {
  1123. intStack[_UINT + 1] = bool(floatStack[_FLT] <= floatStack[_FLT - 1]);
  1124. _UINT++;
  1125. _FLT -= 2;
  1126. return OPCodeReturn::success;
  1127. }
  1128. OPCodeReturn CodeInterpreter::op_cmpne(U32 &ip)
  1129. {
  1130. intStack[_UINT + 1] = bool(floatStack[_FLT] != floatStack[_FLT - 1]);
  1131. _UINT++;
  1132. _FLT -= 2;
  1133. return OPCodeReturn::success;
  1134. }
  1135. OPCodeReturn CodeInterpreter::op_xor(U32 &ip)
  1136. {
  1137. intStack[_UINT - 1] = intStack[_UINT] ^ intStack[_UINT - 1];
  1138. _UINT--;
  1139. return OPCodeReturn::success;
  1140. }
  1141. OPCodeReturn CodeInterpreter::op_mod(U32 &ip)
  1142. {
  1143. if (intStack[_UINT - 1] != 0)
  1144. intStack[_UINT - 1] = intStack[_UINT] % intStack[_UINT - 1];
  1145. else
  1146. intStack[_UINT - 1] = 0;
  1147. _UINT--;
  1148. return OPCodeReturn::success;
  1149. }
  1150. OPCodeReturn CodeInterpreter::op_bitand(U32 &ip)
  1151. {
  1152. intStack[_UINT - 1] = intStack[_UINT] & intStack[_UINT - 1];
  1153. _UINT--;
  1154. return OPCodeReturn::success;
  1155. }
  1156. OPCodeReturn CodeInterpreter::op_bitor(U32 &ip)
  1157. {
  1158. intStack[_UINT - 1] = intStack[_UINT] | intStack[_UINT - 1];
  1159. _UINT--;
  1160. return OPCodeReturn::success;
  1161. }
  1162. OPCodeReturn CodeInterpreter::op_not(U32 &ip)
  1163. {
  1164. intStack[_UINT] = !intStack[_UINT];
  1165. return OPCodeReturn::success;
  1166. }
  1167. OPCodeReturn CodeInterpreter::op_notf(U32 &ip)
  1168. {
  1169. intStack[_UINT + 1] = !floatStack[_FLT];
  1170. _FLT--;
  1171. _UINT++;
  1172. return OPCodeReturn::success;
  1173. }
  1174. OPCodeReturn CodeInterpreter::op_onescomplement(U32 &ip)
  1175. {
  1176. intStack[_UINT] = ~intStack[_UINT];
  1177. return OPCodeReturn::success;
  1178. }
  1179. OPCodeReturn CodeInterpreter::op_shr(U32 &ip)
  1180. {
  1181. intStack[_UINT - 1] = intStack[_UINT] >> intStack[_UINT - 1];
  1182. _UINT--;
  1183. return OPCodeReturn::success;
  1184. }
  1185. OPCodeReturn CodeInterpreter::op_shl(U32 &ip)
  1186. {
  1187. intStack[_UINT - 1] = intStack[_UINT] << intStack[_UINT - 1];
  1188. _UINT--;
  1189. return OPCodeReturn::success;
  1190. }
  1191. OPCodeReturn CodeInterpreter::op_and(U32 &ip)
  1192. {
  1193. intStack[_UINT - 1] = intStack[_UINT] && intStack[_UINT - 1];
  1194. _UINT--;
  1195. return OPCodeReturn::success;
  1196. }
  1197. OPCodeReturn CodeInterpreter::op_or(U32 &ip)
  1198. {
  1199. intStack[_UINT - 1] = intStack[_UINT] || intStack[_UINT - 1];
  1200. _UINT--;
  1201. return OPCodeReturn::success;
  1202. }
  1203. OPCodeReturn CodeInterpreter::op_add(U32 &ip)
  1204. {
  1205. floatStack[_FLT - 1] = floatStack[_FLT] + floatStack[_FLT - 1];
  1206. _FLT--;
  1207. return OPCodeReturn::success;
  1208. }
  1209. OPCodeReturn CodeInterpreter::op_sub(U32 &ip)
  1210. {
  1211. floatStack[_FLT - 1] = floatStack[_FLT] - floatStack[_FLT - 1];
  1212. _FLT--;
  1213. return OPCodeReturn::success;
  1214. }
  1215. OPCodeReturn CodeInterpreter::op_mul(U32 &ip)
  1216. {
  1217. floatStack[_FLT - 1] = floatStack[_FLT] * floatStack[_FLT - 1];
  1218. _FLT--;
  1219. return OPCodeReturn::success;
  1220. }
  1221. OPCodeReturn CodeInterpreter::op_div(U32 &ip)
  1222. {
  1223. floatStack[_FLT - 1] = floatStack[_FLT] / floatStack[_FLT - 1];
  1224. _FLT--;
  1225. return OPCodeReturn::success;
  1226. }
  1227. OPCodeReturn CodeInterpreter::op_neg(U32 &ip)
  1228. {
  1229. floatStack[_FLT] = -floatStack[_FLT];
  1230. return OPCodeReturn::success;
  1231. }
  1232. OPCodeReturn CodeInterpreter::op_inc(U32 &ip)
  1233. {
  1234. StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
  1235. ip += 2;
  1236. // If a variable is set, then these must be NULL. It is necessary
  1237. // to set this here so that the vector parser can appropriately
  1238. // identify whether it's dealing with a vector.
  1239. mPrevField = NULL;
  1240. mPrevObject = NULL;
  1241. mCurObject = NULL;
  1242. gEvalState.setCurVarNameCreate(var);
  1243. // In order to let docblocks work properly with variables, we have
  1244. // clear the current docblock when we do an assign. This way it
  1245. // won't inappropriately carry forward to following function decls.
  1246. mCurFNDocBlock = NULL;
  1247. mCurNSDocBlock = NULL;
  1248. F64 val = gEvalState.getFloatVariable() + 1.0;
  1249. gEvalState.setFloatVariable(val);
  1250. // We gotta push val onto the stack. What if we have
  1251. // more expressions that have to use this.
  1252. // If we don't, we send out an op code to pop it.
  1253. floatStack[_FLT + 1] = val;
  1254. _FLT++;
  1255. return OPCodeReturn::success;
  1256. }
  1257. OPCodeReturn CodeInterpreter::op_dec(U32 &ip)
  1258. {
  1259. StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
  1260. ip += 2;
  1261. // If a variable is set, then these must be NULL. It is necessary
  1262. // to set this here so that the vector parser can appropriately
  1263. // identify whether it's dealing with a vector.
  1264. mPrevField = NULL;
  1265. mPrevObject = NULL;
  1266. mCurObject = NULL;
  1267. gEvalState.setCurVarNameCreate(var);
  1268. // In order to let docblocks work properly with variables, we have
  1269. // clear the current docblock when we do an assign. This way it
  1270. // won't inappropriately carry forward to following function decls.
  1271. mCurFNDocBlock = NULL;
  1272. mCurNSDocBlock = NULL;
  1273. F64 val = gEvalState.getFloatVariable() - 1.0;
  1274. gEvalState.setFloatVariable(val);
  1275. // We gotta push val onto the stack. What if we have
  1276. // more expressions that have to use this.
  1277. // If we don't, we send out an op code to pop it.
  1278. floatStack[_FLT + 1] = val;
  1279. _FLT++;
  1280. return OPCodeReturn::success;
  1281. }
  1282. OPCodeReturn CodeInterpreter::op_setcurvar(U32 &ip)
  1283. {
  1284. StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
  1285. ip += 2;
  1286. // If a variable is set, then these must be NULL. It is necessary
  1287. // to set this here so that the vector parser can appropriately
  1288. // identify whether it's dealing with a vector.
  1289. mPrevField = NULL;
  1290. mPrevObject = NULL;
  1291. mCurObject = NULL;
  1292. gEvalState.setCurVarName(var);
  1293. // In order to let docblocks work properly with variables, we have
  1294. // clear the current docblock when we do an assign. This way it
  1295. // won't inappropriately carry forward to following function decls.
  1296. mCurFNDocBlock = NULL;
  1297. mCurNSDocBlock = NULL;
  1298. return OPCodeReturn::success;
  1299. }
  1300. OPCodeReturn CodeInterpreter::op_setcurvar_create(U32 &ip)
  1301. {
  1302. StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
  1303. ip += 2;
  1304. // See OP_SETCURVAR
  1305. mPrevField = NULL;
  1306. mPrevObject = NULL;
  1307. mCurObject = NULL;
  1308. gEvalState.setCurVarNameCreate(var);
  1309. // See OP_SETCURVAR for why we do this.
  1310. mCurFNDocBlock = NULL;
  1311. mCurNSDocBlock = NULL;
  1312. return OPCodeReturn::success;
  1313. }
  1314. OPCodeReturn CodeInterpreter::op_setcurvar_array(U32 &ip)
  1315. {
  1316. StringTableEntry var = STR.getSTValue();
  1317. // See OP_SETCURVAR
  1318. mPrevField = NULL;
  1319. mPrevObject = NULL;
  1320. mCurObject = NULL;
  1321. gEvalState.setCurVarName(var);
  1322. // See OP_SETCURVAR for why we do this.
  1323. mCurFNDocBlock = NULL;
  1324. mCurNSDocBlock = NULL;
  1325. return OPCodeReturn::success;
  1326. }
  1327. OPCodeReturn CodeInterpreter::op_setcurvar_array_varlookup(U32 &ip)
  1328. {
  1329. StringTableEntry arrayName = CodeToSTE(mCodeBlock->code, ip);
  1330. StringTableEntry arrayLookup = CodeToSTE(mCodeBlock->code, ip + 2);
  1331. ip += 4;
  1332. STR.setStringValue(arrayName);
  1333. STR.advance();
  1334. // See OP_SETCURVAR
  1335. mPrevField = NULL;
  1336. mPrevObject = NULL;
  1337. mCurObject = NULL;
  1338. // resolve arrayLookup to get the 'value'
  1339. // Note: we have to setCurVarNameCreate in case the var doesn't exist.
  1340. // this won't cause much of a performance hit since vars are hashed.
  1341. gEvalState.setCurVarNameCreate(arrayLookup);
  1342. StringTableEntry hash = gEvalState.getStringVariable();
  1343. STR.setStringValue(hash);
  1344. STR.rewind();
  1345. // Generate new array name.
  1346. StringTableEntry var = STR.getSTValue();
  1347. gEvalState.setCurVarName(var);
  1348. // See OP_SETCURVAR for why we do this.
  1349. mCurFNDocBlock = NULL;
  1350. mCurNSDocBlock = NULL;
  1351. return OPCodeReturn::success;
  1352. }
  1353. OPCodeReturn CodeInterpreter::op_setcurvar_array_create(U32 &ip)
  1354. {
  1355. StringTableEntry var = STR.getSTValue();
  1356. // See OP_SETCURVAR
  1357. mPrevField = NULL;
  1358. mPrevObject = NULL;
  1359. mCurObject = NULL;
  1360. gEvalState.setCurVarNameCreate(var);
  1361. // See OP_SETCURVAR for why we do this.
  1362. mCurFNDocBlock = NULL;
  1363. mCurNSDocBlock = NULL;
  1364. return OPCodeReturn::success;
  1365. }
  1366. OPCodeReturn CodeInterpreter::op_setcurvar_array_create_varlookup(U32 &ip)
  1367. {
  1368. StringTableEntry arrayName = CodeToSTE(mCodeBlock->code, ip);
  1369. StringTableEntry arrayLookup = CodeToSTE(mCodeBlock->code, ip + 2);
  1370. ip += 4;
  1371. // See OP_SETCURVAR
  1372. mPrevField = NULL;
  1373. mPrevObject = NULL;
  1374. mCurObject = NULL;
  1375. STR.setStringValue(arrayName);
  1376. STR.advance();
  1377. // resolve arrayLookup to get the 'value'
  1378. // Note: we have to setCurVarNameCreate in case the var doesn't exist.
  1379. // this won't cause much of a performance hit since vars are hashed.
  1380. gEvalState.setCurVarNameCreate(arrayLookup);
  1381. StringTableEntry hash = gEvalState.getStringVariable();
  1382. STR.setStringValue(hash);
  1383. STR.rewind();
  1384. // Generate new array name.
  1385. StringTableEntry var = STR.getSTValue();
  1386. gEvalState.setCurVarNameCreate(var);
  1387. // See OP_SETCURVAR for why we do this.
  1388. mCurFNDocBlock = NULL;
  1389. mCurNSDocBlock = NULL;
  1390. return OPCodeReturn::success;
  1391. }
  1392. OPCodeReturn CodeInterpreter::op_loadvar_uint(U32 &ip)
  1393. {
  1394. intStack[_UINT + 1] = gEvalState.getIntVariable();
  1395. _UINT++;
  1396. return OPCodeReturn::success;
  1397. }
  1398. OPCodeReturn CodeInterpreter::op_loadvar_flt(U32 &ip)
  1399. {
  1400. floatStack[_FLT + 1] = gEvalState.getFloatVariable();
  1401. _FLT++;
  1402. return OPCodeReturn::success;
  1403. }
  1404. OPCodeReturn CodeInterpreter::op_loadvar_str(U32 &ip)
  1405. {
  1406. StringTableEntry val = gEvalState.getStringVariable();
  1407. STR.setStringValue(val);
  1408. return OPCodeReturn::success;
  1409. }
  1410. OPCodeReturn CodeInterpreter::op_loadvar_var(U32 &ip)
  1411. {
  1412. // Sets current source of OP_SAVEVAR_VAR
  1413. gEvalState.copyVariable = gEvalState.currentVariable;
  1414. return OPCodeReturn::success;
  1415. }
  1416. OPCodeReturn CodeInterpreter::op_savevar_uint(U32 &ip)
  1417. {
  1418. gEvalState.setIntVariable(intStack[_UINT]);
  1419. return OPCodeReturn::success;
  1420. }
  1421. OPCodeReturn CodeInterpreter::op_savevar_flt(U32 &ip)
  1422. {
  1423. gEvalState.setFloatVariable(floatStack[_FLT]);
  1424. return OPCodeReturn::success;
  1425. }
  1426. OPCodeReturn CodeInterpreter::op_savevar_str(U32 &ip)
  1427. {
  1428. gEvalState.setStringVariable(STR.getStringValue());
  1429. return OPCodeReturn::success;
  1430. }
  1431. OPCodeReturn CodeInterpreter::op_savevar_var(U32 &ip)
  1432. {
  1433. // this basically handles %var1 = %var2
  1434. gEvalState.setCopyVariable();
  1435. return OPCodeReturn::success;
  1436. }
  1437. OPCodeReturn CodeInterpreter::op_setcurobject(U32 &ip)
  1438. {
  1439. // Save the previous object for parsing vector fields.
  1440. mPrevObject = mCurObject;
  1441. StringTableEntry val = STR.getStringValue();
  1442. // Sim::findObject will sometimes find valid objects from
  1443. // multi-component strings. This makes sure that doesn't
  1444. // happen.
  1445. for (const char* check = val; *check; check++)
  1446. {
  1447. if (*check == ' ')
  1448. {
  1449. val = "";
  1450. break;
  1451. }
  1452. }
  1453. mCurObject = Sim::findObject(val);
  1454. return OPCodeReturn::success;
  1455. }
  1456. OPCodeReturn CodeInterpreter::op_setcurobject_internal(U32 &ip)
  1457. {
  1458. ++ip; // To skip the recurse flag if the object wasn't found
  1459. if (mCurObject)
  1460. {
  1461. SimSet *set = dynamic_cast<SimSet *>(mCurObject);
  1462. if (set)
  1463. {
  1464. StringTableEntry intName = StringTable->insert(STR.getStringValue());
  1465. bool recurse = mCodeBlock->code[ip - 1];
  1466. SimObject *obj = set->findObjectByInternalName(intName, recurse);
  1467. intStack[_UINT + 1] = obj ? obj->getId() : 0;
  1468. _UINT++;
  1469. }
  1470. else
  1471. {
  1472. Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-set %s of class %s.", mCodeBlock->getFileLine(ip - 2), mCurObject->getName(), mCurObject->getClassName());
  1473. intStack[_UINT] = 0;
  1474. }
  1475. }
  1476. return OPCodeReturn::success;
  1477. }
  1478. OPCodeReturn CodeInterpreter::op_setcurobject_new(U32 &ip)
  1479. {
  1480. mCurObject = mCurrentNewObject;
  1481. return OPCodeReturn::success;
  1482. }
  1483. OPCodeReturn CodeInterpreter::op_setcurfield(U32 &ip)
  1484. {
  1485. // Save the previous field for parsing vector fields.
  1486. mPrevField = mCurField;
  1487. dStrcpy(prevFieldArray, curFieldArray);
  1488. mCurField = CodeToSTE(mCodeBlock->code, ip);
  1489. curFieldArray[0] = 0;
  1490. ip += 2;
  1491. return OPCodeReturn::success;
  1492. }
  1493. OPCodeReturn CodeInterpreter::op_setcurfield_array(U32 &ip)
  1494. {
  1495. dStrcpy(curFieldArray, STR.getStringValue());
  1496. return OPCodeReturn::success;
  1497. }
  1498. OPCodeReturn CodeInterpreter::op_setcurfield_type(U32 &ip)
  1499. {
  1500. if (mCurObject)
  1501. mCurObject->setDataFieldType(mCodeBlock->code[ip], mCurField, curFieldArray);
  1502. ip++;
  1503. return OPCodeReturn::success;
  1504. }
  1505. OPCodeReturn CodeInterpreter::op_setcurfield_array_var(U32 &ip)
  1506. {
  1507. StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
  1508. ip += 2;
  1509. // We set the current var name (create it as well in case if it doesn't exist,
  1510. // otherwise we will crash).
  1511. gEvalState.setCurVarNameCreate(var);
  1512. // Then load the var and copy the contents to the current field array
  1513. dStrncpy(curFieldArray, gEvalState.currentVariable->getStringValue(), sizeof(curFieldArray));
  1514. return OPCodeReturn::success;
  1515. }
  1516. OPCodeReturn CodeInterpreter::op_setcurfield_this(U32 &ip)
  1517. {
  1518. // set the 'this pointer' as the current object.
  1519. mCurObject = mThisObject;
  1520. mPrevField = mCurField;
  1521. dStrcpy(prevFieldArray, curFieldArray);
  1522. mCurField = CodeToSTE(mCodeBlock->code, ip);
  1523. curFieldArray[0] = 0;
  1524. ip += 2;
  1525. return OPCodeReturn::success;
  1526. }
  1527. OPCodeReturn CodeInterpreter::op_loadfield_uint(U32 &ip)
  1528. {
  1529. if (mCurObject)
  1530. intStack[_UINT + 1] = U32(dAtoi(mCurObject->getDataField(mCurField, curFieldArray)));
  1531. else
  1532. {
  1533. // The field is not being retrieved from an object. Maybe it's
  1534. // a special accessor?
  1535. char buff[FieldBufferSizeNumeric];
  1536. memset(buff, 0, sizeof(buff));
  1537. getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
  1538. intStack[_UINT + 1] = dAtoi(buff);
  1539. }
  1540. _UINT++;
  1541. return OPCodeReturn::success;
  1542. }
  1543. OPCodeReturn CodeInterpreter::op_loadfield_flt(U32 &ip)
  1544. {
  1545. if (mCurObject)
  1546. floatStack[_FLT + 1] = dAtof(mCurObject->getDataField(mCurField, curFieldArray));
  1547. else
  1548. {
  1549. // The field is not being retrieved from an object. Maybe it's
  1550. // a special accessor?
  1551. char buff[FieldBufferSizeNumeric];
  1552. memset(buff, 0, sizeof(buff));
  1553. getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
  1554. floatStack[_FLT + 1] = dAtof(buff);
  1555. }
  1556. _FLT++;
  1557. return OPCodeReturn::success;
  1558. }
  1559. OPCodeReturn CodeInterpreter::op_loadfield_str(U32 &ip)
  1560. {
  1561. if (mCurObject)
  1562. {
  1563. StringTableEntry val = mCurObject->getDataField(mCurField, curFieldArray);
  1564. STR.setStringValue(val);
  1565. }
  1566. else
  1567. {
  1568. // The field is not being retrieved from an object. Maybe it's
  1569. // a special accessor?
  1570. char buff[FieldBufferSizeString];
  1571. memset(buff, 0, sizeof(buff));
  1572. getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
  1573. STR.setStringValue(buff);
  1574. }
  1575. return OPCodeReturn::success;
  1576. }
  1577. OPCodeReturn CodeInterpreter::op_savefield_uint(U32 &ip)
  1578. {
  1579. STR.setIntValue(intStack[_UINT]);
  1580. if (mCurObject)
  1581. mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
  1582. else
  1583. {
  1584. // The field is not being set on an object. Maybe it's
  1585. // a special accessor?
  1586. setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
  1587. mPrevObject = NULL;
  1588. }
  1589. return OPCodeReturn::success;
  1590. }
  1591. OPCodeReturn CodeInterpreter::op_savefield_flt(U32 &ip)
  1592. {
  1593. STR.setFloatValue(floatStack[_FLT]);
  1594. if (mCurObject)
  1595. mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
  1596. else
  1597. {
  1598. // The field is not being set on an object. Maybe it's
  1599. // a special accessor?
  1600. setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
  1601. mPrevObject = NULL;
  1602. }
  1603. return OPCodeReturn::success;
  1604. }
  1605. OPCodeReturn CodeInterpreter::op_savefield_str(U32 &ip)
  1606. {
  1607. if (mCurObject)
  1608. mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
  1609. else
  1610. {
  1611. // The field is not being set on an object. Maybe it's
  1612. // a special accessor?
  1613. setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
  1614. mPrevObject = NULL;
  1615. }
  1616. return OPCodeReturn::success;
  1617. }
  1618. OPCodeReturn CodeInterpreter::op_str_to_uint(U32 &ip)
  1619. {
  1620. intStack[_UINT + 1] = STR.getIntValue();
  1621. _UINT++;
  1622. return OPCodeReturn::success;
  1623. }
  1624. OPCodeReturn CodeInterpreter::op_str_to_flt(U32 &ip)
  1625. {
  1626. floatStack[_FLT + 1] = STR.getFloatValue();
  1627. _FLT++;
  1628. return OPCodeReturn::success;
  1629. }
  1630. OPCodeReturn CodeInterpreter::op_str_to_none(U32 &ip)
  1631. {
  1632. // This exists simply to deal with certain typecast situations.
  1633. return OPCodeReturn::success;
  1634. }
  1635. OPCodeReturn CodeInterpreter::op_flt_to_uint(U32 &ip)
  1636. {
  1637. intStack[_UINT + 1] = (S64)floatStack[_FLT];
  1638. _FLT--;
  1639. _UINT++;
  1640. return OPCodeReturn::success;
  1641. }
  1642. OPCodeReturn CodeInterpreter::op_flt_to_str(U32 &ip)
  1643. {
  1644. STR.setFloatValue(floatStack[_FLT]);
  1645. _FLT--;
  1646. return OPCodeReturn::success;
  1647. }
  1648. OPCodeReturn CodeInterpreter::op_flt_to_none(U32 &ip)
  1649. {
  1650. _FLT--;
  1651. return OPCodeReturn::success;
  1652. }
  1653. OPCodeReturn CodeInterpreter::op_uint_to_flt(U32 &ip)
  1654. {
  1655. floatStack[_FLT + 1] = (F32)intStack[_UINT];
  1656. _UINT--;
  1657. _FLT++;
  1658. return OPCodeReturn::success;
  1659. }
  1660. OPCodeReturn CodeInterpreter::op_uint_to_str(U32 &ip)
  1661. {
  1662. STR.setIntValue(intStack[_UINT]);
  1663. _UINT--;
  1664. return OPCodeReturn::success;
  1665. }
  1666. OPCodeReturn CodeInterpreter::op_uint_to_none(U32 &ip)
  1667. {
  1668. _UINT--;
  1669. return OPCodeReturn::success;
  1670. }
  1671. OPCodeReturn CodeInterpreter::op_copyvar_to_none(U32 &ip)
  1672. {
  1673. gEvalState.copyVariable = NULL;
  1674. return OPCodeReturn::success;
  1675. }
  1676. OPCodeReturn CodeInterpreter::op_loadimmed_uint(U32 &ip)
  1677. {
  1678. intStack[_UINT + 1] = mCodeBlock->code[ip++];
  1679. _UINT++;
  1680. return OPCodeReturn::success;
  1681. }
  1682. OPCodeReturn CodeInterpreter::op_loadimmed_flt(U32 &ip)
  1683. {
  1684. floatStack[_FLT + 1] = mCurFloatTable[mCodeBlock->code[ip]];
  1685. ip++;
  1686. _FLT++;
  1687. return OPCodeReturn::success;
  1688. }
  1689. OPCodeReturn CodeInterpreter::op_tag_to_str(U32 &ip)
  1690. {
  1691. mCodeBlock->code[ip - 1] = OP_LOADIMMED_STR;
  1692. // it's possible the string has already been converted
  1693. if (U8(mCurStringTable[mCodeBlock->code[ip]]) != StringTagPrefixByte)
  1694. {
  1695. U32 id = GameAddTaggedString(mCurStringTable + mCodeBlock->code[ip]);
  1696. dSprintf(mCurStringTable + mCodeBlock->code[ip] + 1, 7, "%d", id);
  1697. *(mCurStringTable + mCodeBlock->code[ip]) = StringTagPrefixByte;
  1698. }
  1699. // Fallthrough
  1700. OPCodeReturn ret = op_loadimmed_str(ip);
  1701. return ret;
  1702. }
  1703. OPCodeReturn CodeInterpreter::op_loadimmed_str(U32 &ip)
  1704. {
  1705. STR.setStringValue(mCurStringTable + mCodeBlock->code[ip++]);
  1706. return OPCodeReturn::success;
  1707. }
  1708. OPCodeReturn CodeInterpreter::op_docblock_str(U32 &ip)
  1709. {
  1710. // If the first word of the doc is '\class' or '@class', then this
  1711. // is a namespace doc block, otherwise it is a function doc block.
  1712. const char* docblock = mCurStringTable + mCodeBlock->code[ip++];
  1713. const char* sansClass = dStrstr(docblock, "@class");
  1714. if (!sansClass)
  1715. sansClass = dStrstr(docblock, "\\class");
  1716. if (sansClass)
  1717. {
  1718. // Don't save the class declaration. Scan past the 'class'
  1719. // keyword and up to the first whitespace.
  1720. sansClass += 7;
  1721. S32 index = 0;
  1722. while ((*sansClass != ' ') && (*sansClass != '\n') && *sansClass && (index < (nsDocLength - 1)))
  1723. {
  1724. mNSDocBlockClass[index++] = *sansClass;
  1725. sansClass++;
  1726. }
  1727. mNSDocBlockClass[index] = '\0';
  1728. mCurNSDocBlock = sansClass + 1;
  1729. }
  1730. else
  1731. mCurFNDocBlock = docblock;
  1732. return OPCodeReturn::success;
  1733. }
  1734. OPCodeReturn CodeInterpreter::op_loadimmed_ident(U32 &ip)
  1735. {
  1736. STR.setStringValue(CodeToSTE(mCodeBlock->code, ip));
  1737. ip += 2;
  1738. return OPCodeReturn::success;
  1739. }
  1740. OPCodeReturn CodeInterpreter::op_callfunc_resolve(U32 &ip)
  1741. {
  1742. // This deals with a function that is potentially living in a namespace.
  1743. StringTableEntry fnNamespace = CodeToSTE(mCodeBlock->code, ip + 2);
  1744. StringTableEntry fnName = CodeToSTE(mCodeBlock->code, ip);
  1745. // Try to look it up.
  1746. mNSEntry = Namespace::find(fnNamespace)->lookup(fnName);
  1747. if (!mNSEntry)
  1748. {
  1749. ip += 5;
  1750. Con::warnf(ConsoleLogEntry::General,
  1751. "%s: Unable to find function %s%s%s",
  1752. mCodeBlock->getFileLine(ip - 7), fnNamespace ? fnNamespace : "",
  1753. fnNamespace ? "::" : "", fnName);
  1754. STR.popFrame();
  1755. CSTK.popFrame();
  1756. return OPCodeReturn::success;
  1757. }
  1758. // Fallthrough to op_callfunc_resolve
  1759. OPCodeReturn ret = op_callfunc(ip);
  1760. return ret;
  1761. }
  1762. OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip)
  1763. {
  1764. // This routingId is set when we query the object as to whether
  1765. // it handles this method. It is set to an enum from the table
  1766. // above indicating whether it handles it on a component it owns
  1767. // or just on the object.
  1768. S32 routingId = 0;
  1769. U32 *code = mCodeBlock->code;
  1770. StringTableEntry fnName = CodeToSTE(code, ip);
  1771. //if this is called from inside a function, append the ip and codeptr
  1772. if (gEvalState.getStackDepth() > 0)
  1773. {
  1774. gEvalState.getCurrentFrame().code = mCodeBlock;
  1775. gEvalState.getCurrentFrame().ip = ip - 1;
  1776. }
  1777. U32 callType = code[ip + 4];
  1778. ip += 5;
  1779. CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
  1780. const char *componentReturnValue = "";
  1781. Namespace *ns = NULL;
  1782. if (callType == FuncCallExprNode::FunctionCall)
  1783. {
  1784. if (!mNSEntry)
  1785. mNSEntry = Namespace::global()->lookup(fnName);
  1786. }
  1787. else if (callType == FuncCallExprNode::MethodCall)
  1788. {
  1789. mSaveObject = gEvalState.thisObject;
  1790. gEvalState.thisObject = Sim::findObject((const char*)mCallArgv[1]);
  1791. if (!gEvalState.thisObject)
  1792. {
  1793. // Go back to the previous saved object.
  1794. gEvalState.thisObject = mSaveObject;
  1795. Con::warnf(ConsoleLogEntry::General, "%s: Unable to find object: '%s' attempting to call function '%s'", mCodeBlock->getFileLine(ip - 4), (const char*)mCallArgv[1], fnName);
  1796. STR.popFrame();
  1797. CSTK.popFrame();
  1798. STR.setStringValue("");
  1799. return OPCodeReturn::success;
  1800. }
  1801. bool handlesMethod = gEvalState.thisObject->handlesConsoleMethod(fnName, &routingId);
  1802. if (handlesMethod && routingId == MethodOnComponent)
  1803. {
  1804. ICallMethod *pComponent = dynamic_cast<ICallMethod *>(gEvalState.thisObject);
  1805. if (pComponent)
  1806. componentReturnValue = pComponent->callMethodArgList(mCallArgc, mCallArgv, false);
  1807. }
  1808. ns = gEvalState.thisObject->getNamespace();
  1809. if (ns)
  1810. mNSEntry = ns->lookup(fnName);
  1811. else
  1812. mNSEntry = NULL;
  1813. }
  1814. else // it's a ParentCall
  1815. {
  1816. if (mExec.thisNamespace)
  1817. {
  1818. ns = mExec.thisNamespace->mParent;
  1819. if (ns)
  1820. mNSEntry = ns->lookup(fnName);
  1821. else
  1822. mNSEntry = NULL;
  1823. }
  1824. else
  1825. {
  1826. ns = NULL;
  1827. mNSEntry = NULL;
  1828. }
  1829. }
  1830. Namespace::Entry::CallbackUnion * nsCb = NULL;
  1831. const char * nsUsage = NULL;
  1832. if (mNSEntry)
  1833. {
  1834. nsCb = &mNSEntry->cb;
  1835. nsUsage = mNSEntry->mUsage;
  1836. routingId = 0;
  1837. }
  1838. if (!mNSEntry || mExec.noCalls)
  1839. {
  1840. if (!mExec.noCalls && !(routingId == MethodOnComponent))
  1841. {
  1842. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", mCodeBlock->getFileLine(ip - 6), fnName);
  1843. if (callType == FuncCallExprNode::MethodCall)
  1844. {
  1845. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  1846. gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : "",
  1847. gEvalState.thisObject->getId(), Con::getNamespaceList(ns));
  1848. }
  1849. }
  1850. STR.popFrame();
  1851. CSTK.popFrame();
  1852. if (routingId == MethodOnComponent)
  1853. STR.setStringValue(componentReturnValue);
  1854. else
  1855. STR.setStringValue("");
  1856. return OPCodeReturn::success;
  1857. }
  1858. // ConsoleFunctionType is for any function defined by script.
  1859. // Any 'callback' type is an engine function that is exposed to script.
  1860. if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
  1861. {
  1862. ConsoleValueRef ret;
  1863. if (mNSEntry->mFunctionOffset)
  1864. ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
  1865. STR.popFrame();
  1866. // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
  1867. if (code[ip] == OP_STR_TO_UINT)
  1868. {
  1869. ip++;
  1870. intStack[++_UINT] = (U32)((S32)ret);
  1871. }
  1872. else if (code[ip] == OP_STR_TO_FLT)
  1873. {
  1874. ip++;
  1875. floatStack[++_FLT] = (F32)ret;
  1876. }
  1877. else if (code[ip] == OP_STR_TO_NONE)
  1878. {
  1879. STR.setStringValue(ret.getStringValue());
  1880. ip++;
  1881. }
  1882. else
  1883. STR.setStringValue((const char*)ret);
  1884. // This will clear everything including returnValue
  1885. CSTK.popFrame();
  1886. //STR.clearFunctionOffset();
  1887. }
  1888. else
  1889. {
  1890. const char* nsName = ns ? ns->mName : "";
  1891. #ifndef TORQUE_DEBUG
  1892. // [tom, 12/13/2006] This stops tools functions from working in the console,
  1893. // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
  1894. if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
  1895. {
  1896. Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
  1897. }
  1898. else
  1899. #endif
  1900. if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
  1901. {
  1902. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
  1903. mCodeBlock->getFileLine(ip - 6), nsName, fnName,
  1904. mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
  1905. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
  1906. STR.popFrame();
  1907. CSTK.popFrame();
  1908. }
  1909. else
  1910. {
  1911. switch (mNSEntry->mType)
  1912. {
  1913. case Namespace::Entry::StringCallbackType:
  1914. {
  1915. const char *ret = mNSEntry->cb.mStringCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  1916. STR.popFrame();
  1917. CSTK.popFrame();
  1918. if (ret != STR.getStringValue())
  1919. STR.setStringValue(ret);
  1920. //else
  1921. // sSTR.setLen(dStrlen(ret));
  1922. break;
  1923. }
  1924. case Namespace::Entry::IntCallbackType:
  1925. {
  1926. S32 result = mNSEntry->cb.mIntCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  1927. STR.popFrame();
  1928. CSTK.popFrame();
  1929. if (code[ip] == OP_STR_TO_UINT)
  1930. {
  1931. ip++;
  1932. intStack[++_UINT] = result;
  1933. break;
  1934. }
  1935. else if (code[ip] == OP_STR_TO_FLT)
  1936. {
  1937. ip++;
  1938. floatStack[++_FLT] = result;
  1939. break;
  1940. }
  1941. else if (code[ip] == OP_STR_TO_NONE)
  1942. ip++;
  1943. else
  1944. STR.setIntValue(result);
  1945. break;
  1946. }
  1947. case Namespace::Entry::FloatCallbackType:
  1948. {
  1949. F64 result = mNSEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  1950. STR.popFrame();
  1951. CSTK.popFrame();
  1952. if (code[ip] == OP_STR_TO_UINT)
  1953. {
  1954. ip++;
  1955. intStack[++_UINT] = (S64)result;
  1956. break;
  1957. }
  1958. else if (code[ip] == OP_STR_TO_FLT)
  1959. {
  1960. ip++;
  1961. floatStack[++_FLT] = result;
  1962. break;
  1963. }
  1964. else if (code[ip] == OP_STR_TO_NONE)
  1965. ip++;
  1966. else
  1967. STR.setFloatValue(result);
  1968. break;
  1969. }
  1970. case Namespace::Entry::VoidCallbackType:
  1971. mNSEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  1972. if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
  1973. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
  1974. STR.popFrame();
  1975. CSTK.popFrame();
  1976. STR.setStringValue("");
  1977. break;
  1978. case Namespace::Entry::BoolCallbackType:
  1979. {
  1980. bool result = mNSEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  1981. STR.popFrame();
  1982. CSTK.popFrame();
  1983. if (code[ip] == OP_STR_TO_UINT)
  1984. {
  1985. ip++;
  1986. intStack[++_UINT] = result;
  1987. break;
  1988. }
  1989. else if (code[ip] == OP_STR_TO_FLT)
  1990. {
  1991. ip++;
  1992. floatStack[++_FLT] = result;
  1993. break;
  1994. }
  1995. else if (code[ip] == OP_STR_TO_NONE)
  1996. ip++;
  1997. else
  1998. STR.setIntValue(result);
  1999. break;
  2000. }
  2001. }
  2002. }
  2003. }
  2004. if (callType == FuncCallExprNode::MethodCall)
  2005. gEvalState.thisObject = mSaveObject;
  2006. return OPCodeReturn::success;
  2007. }
  2008. OPCodeReturn CodeInterpreter::op_callfunc_pointer(U32 &ip)
  2009. {
  2010. // get function name. This is the 'function pointer'.
  2011. StringTableEntry fnName = StringTable->insert(STR.getStringValue());
  2012. U32 *code = mCodeBlock->code;
  2013. mNSEntry = Namespace::global()->lookup(fnName);
  2014. //if this is called from inside a function, append the ip and codeptr
  2015. if (gEvalState.getStackDepth() > 0)
  2016. {
  2017. gEvalState.getCurrentFrame().code = mCodeBlock;
  2018. gEvalState.getCurrentFrame().ip = ip - 1;
  2019. }
  2020. CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
  2021. if (!mNSEntry || mExec.noCalls)
  2022. {
  2023. if (!mExec.noCalls)
  2024. {
  2025. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", mCodeBlock->getFileLine(ip - 6), fnName);
  2026. }
  2027. STR.popFrame();
  2028. CSTK.popFrame();
  2029. STR.setStringValue("");
  2030. return OPCodeReturn::success;
  2031. }
  2032. // ConsoleFunctionType is for any function defined by script.
  2033. // Any 'callback' type is an engine function that is exposed to script.
  2034. if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
  2035. {
  2036. ConsoleValueRef ret;
  2037. if (mNSEntry->mFunctionOffset)
  2038. ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
  2039. STR.popFrame();
  2040. // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
  2041. if (code[ip] == OP_STR_TO_UINT)
  2042. {
  2043. ip++;
  2044. intStack[++_UINT] = (U32)((S32)ret);
  2045. }
  2046. else if (code[ip] == OP_STR_TO_FLT)
  2047. {
  2048. ip++;
  2049. floatStack[++_FLT] = (F32)ret;
  2050. }
  2051. else if (code[ip] == OP_STR_TO_NONE)
  2052. {
  2053. STR.setStringValue(ret.getStringValue());
  2054. ip++;
  2055. }
  2056. else
  2057. STR.setStringValue((const char*)ret);
  2058. // This will clear everything including returnValue
  2059. CSTK.popFrame();
  2060. //STR.clearFunctionOffset();
  2061. }
  2062. else
  2063. {
  2064. const char* nsName = "";
  2065. Namespace::Entry::CallbackUnion * nsCb = &mNSEntry->cb;
  2066. const char * nsUsage = mNSEntry->mUsage;
  2067. #ifndef TORQUE_DEBUG
  2068. // [tom, 12/13/2006] This stops tools functions from working in the console,
  2069. // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
  2070. if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
  2071. {
  2072. Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
  2073. }
  2074. else
  2075. #endif
  2076. if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
  2077. {
  2078. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
  2079. mCodeBlock->getFileLine(ip - 6), nsName, fnName,
  2080. mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
  2081. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
  2082. STR.popFrame();
  2083. CSTK.popFrame();
  2084. }
  2085. else
  2086. {
  2087. switch (mNSEntry->mType)
  2088. {
  2089. case Namespace::Entry::StringCallbackType:
  2090. {
  2091. const char *ret = mNSEntry->cb.mStringCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  2092. STR.popFrame();
  2093. CSTK.popFrame();
  2094. if (ret != STR.getStringValue())
  2095. STR.setStringValue(ret);
  2096. //else
  2097. // sSTR.setLen(dStrlen(ret));
  2098. break;
  2099. }
  2100. case Namespace::Entry::IntCallbackType:
  2101. {
  2102. S32 result = mNSEntry->cb.mIntCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  2103. STR.popFrame();
  2104. CSTK.popFrame();
  2105. if (code[ip] == OP_STR_TO_UINT)
  2106. {
  2107. ip++;
  2108. intStack[++_UINT] = result;
  2109. break;
  2110. }
  2111. else if (code[ip] == OP_STR_TO_FLT)
  2112. {
  2113. ip++;
  2114. floatStack[++_FLT] = result;
  2115. break;
  2116. }
  2117. else if (code[ip] == OP_STR_TO_NONE)
  2118. ip++;
  2119. else
  2120. STR.setIntValue(result);
  2121. break;
  2122. }
  2123. case Namespace::Entry::FloatCallbackType:
  2124. {
  2125. F64 result = mNSEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  2126. STR.popFrame();
  2127. CSTK.popFrame();
  2128. if (code[ip] == OP_STR_TO_UINT)
  2129. {
  2130. ip++;
  2131. intStack[++_UINT] = (S64)result;
  2132. break;
  2133. }
  2134. else if (code[ip] == OP_STR_TO_FLT)
  2135. {
  2136. ip++;
  2137. floatStack[++_FLT] = result;
  2138. break;
  2139. }
  2140. else if (code[ip] == OP_STR_TO_NONE)
  2141. ip++;
  2142. else
  2143. STR.setFloatValue(result);
  2144. break;
  2145. }
  2146. case Namespace::Entry::VoidCallbackType:
  2147. mNSEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  2148. if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
  2149. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
  2150. STR.popFrame();
  2151. CSTK.popFrame();
  2152. STR.setStringValue("");
  2153. break;
  2154. case Namespace::Entry::BoolCallbackType:
  2155. {
  2156. bool result = mNSEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
  2157. STR.popFrame();
  2158. CSTK.popFrame();
  2159. if (code[ip] == OP_STR_TO_UINT)
  2160. {
  2161. ip++;
  2162. intStack[++_UINT] = result;
  2163. break;
  2164. }
  2165. else if (code[ip] == OP_STR_TO_FLT)
  2166. {
  2167. ip++;
  2168. floatStack[++_FLT] = result;
  2169. break;
  2170. }
  2171. else if (code[ip] == OP_STR_TO_NONE)
  2172. ip++;
  2173. else
  2174. STR.setIntValue(result);
  2175. break;
  2176. }
  2177. }
  2178. }
  2179. }
  2180. return OPCodeReturn::success;
  2181. }
  2182. OPCodeReturn CodeInterpreter::op_callfunc_this(U32 &ip)
  2183. {
  2184. U32 *code = mCodeBlock->code;
  2185. StringTableEntry fnName = CodeToSTE(code, ip);
  2186. //if this is called from inside a function, append the ip and codeptr
  2187. if (gEvalState.getStackDepth() > 0)
  2188. {
  2189. gEvalState.getCurrentFrame().code = mCodeBlock;
  2190. gEvalState.getCurrentFrame().ip = ip - 1;
  2191. }
  2192. ip += 2;
  2193. CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
  2194. Namespace *ns = mThisObject->getNamespace();
  2195. if (ns)
  2196. mNSEntry = ns->lookup(fnName);
  2197. else
  2198. mNSEntry = NULL;
  2199. if (!mNSEntry || mExec.noCalls)
  2200. {
  2201. if (!mExec.noCalls)
  2202. {
  2203. Con::warnf(ConsoleLogEntry::General, "%s: Unknown command %s.", mCodeBlock->getFileLine(ip - 6), fnName);
  2204. Con::warnf(ConsoleLogEntry::General, " Object %s(%d) %s",
  2205. mThisObject->getName() ? mThisObject->getName() : "",
  2206. mThisObject->getId(), Con::getNamespaceList(ns));
  2207. }
  2208. STR.popFrame();
  2209. CSTK.popFrame();
  2210. STR.setStringValue("");
  2211. return OPCodeReturn::success;
  2212. }
  2213. // ConsoleFunctionType is for any function defined by script.
  2214. // Any 'callback' type is an engine function that is exposed to script.
  2215. if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
  2216. {
  2217. ConsoleValueRef ret;
  2218. if (mNSEntry->mFunctionOffset)
  2219. ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
  2220. STR.popFrame();
  2221. // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
  2222. if (code[ip] == OP_STR_TO_UINT)
  2223. {
  2224. ip++;
  2225. intStack[++_UINT] = (U32)((S32)ret);
  2226. }
  2227. else if (code[ip] == OP_STR_TO_FLT)
  2228. {
  2229. ip++;
  2230. floatStack[++_FLT] = (F32)ret;
  2231. }
  2232. else if (code[ip] == OP_STR_TO_NONE)
  2233. {
  2234. STR.setStringValue(ret.getStringValue());
  2235. ip++;
  2236. }
  2237. else
  2238. STR.setStringValue((const char*)ret);
  2239. // This will clear everything including returnValue
  2240. CSTK.popFrame();
  2241. //STR.clearFunctionOffset();
  2242. }
  2243. else
  2244. {
  2245. Namespace::Entry::CallbackUnion * nsCb = &mNSEntry->cb;
  2246. const char * nsUsage = mNSEntry->mUsage;
  2247. const char* nsName = ns ? ns->mName : "";
  2248. #ifndef TORQUE_DEBUG
  2249. // [tom, 12/13/2006] This stops tools functions from working in the console,
  2250. // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
  2251. if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
  2252. {
  2253. Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
  2254. }
  2255. else
  2256. #endif
  2257. if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
  2258. {
  2259. Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
  2260. mCodeBlock->getFileLine(ip - 6), nsName, fnName,
  2261. mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
  2262. Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
  2263. STR.popFrame();
  2264. CSTK.popFrame();
  2265. }
  2266. else
  2267. {
  2268. switch (mNSEntry->mType)
  2269. {
  2270. case Namespace::Entry::StringCallbackType:
  2271. {
  2272. const char *ret = mNSEntry->cb.mStringCallbackFunc(mThisObject, mCallArgc, mCallArgv);
  2273. STR.popFrame();
  2274. CSTK.popFrame();
  2275. if (ret != STR.getStringValue())
  2276. STR.setStringValue(ret);
  2277. //else
  2278. // sSTR.setLen(dStrlen(ret));
  2279. break;
  2280. }
  2281. case Namespace::Entry::IntCallbackType:
  2282. {
  2283. S32 result = mNSEntry->cb.mIntCallbackFunc(mThisObject, mCallArgc, mCallArgv);
  2284. STR.popFrame();
  2285. CSTK.popFrame();
  2286. if (code[ip] == OP_STR_TO_UINT)
  2287. {
  2288. ip++;
  2289. intStack[++_UINT] = result;
  2290. break;
  2291. }
  2292. else if (code[ip] == OP_STR_TO_FLT)
  2293. {
  2294. ip++;
  2295. floatStack[++_FLT] = result;
  2296. break;
  2297. }
  2298. else if (code[ip] == OP_STR_TO_NONE)
  2299. ip++;
  2300. else
  2301. STR.setIntValue(result);
  2302. break;
  2303. }
  2304. case Namespace::Entry::FloatCallbackType:
  2305. {
  2306. F64 result = mNSEntry->cb.mFloatCallbackFunc(mThisObject, mCallArgc, mCallArgv);
  2307. STR.popFrame();
  2308. CSTK.popFrame();
  2309. if (code[ip] == OP_STR_TO_UINT)
  2310. {
  2311. ip++;
  2312. intStack[++_UINT] = (S64)result;
  2313. break;
  2314. }
  2315. else if (code[ip] == OP_STR_TO_FLT)
  2316. {
  2317. ip++;
  2318. floatStack[++_FLT] = result;
  2319. break;
  2320. }
  2321. else if (code[ip] == OP_STR_TO_NONE)
  2322. ip++;
  2323. else
  2324. STR.setFloatValue(result);
  2325. break;
  2326. }
  2327. case Namespace::Entry::VoidCallbackType:
  2328. mNSEntry->cb.mVoidCallbackFunc(mThisObject, mCallArgc, mCallArgv);
  2329. if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
  2330. Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
  2331. STR.popFrame();
  2332. CSTK.popFrame();
  2333. STR.setStringValue("");
  2334. break;
  2335. case Namespace::Entry::BoolCallbackType:
  2336. {
  2337. bool result = mNSEntry->cb.mBoolCallbackFunc(mThisObject, mCallArgc, mCallArgv);
  2338. STR.popFrame();
  2339. CSTK.popFrame();
  2340. if (code[ip] == OP_STR_TO_UINT)
  2341. {
  2342. ip++;
  2343. intStack[++_UINT] = result;
  2344. break;
  2345. }
  2346. else if (code[ip] == OP_STR_TO_FLT)
  2347. {
  2348. ip++;
  2349. floatStack[++_FLT] = result;
  2350. break;
  2351. }
  2352. else if (code[ip] == OP_STR_TO_NONE)
  2353. ip++;
  2354. else
  2355. STR.setIntValue(result);
  2356. break;
  2357. }
  2358. }
  2359. }
  2360. }
  2361. return OPCodeReturn::success;
  2362. }
  2363. OPCodeReturn CodeInterpreter::op_advance_str(U32 &ip)
  2364. {
  2365. STR.advance();
  2366. return OPCodeReturn::success;
  2367. }
  2368. OPCodeReturn CodeInterpreter::op_advance_str_appendchar(U32 &ip)
  2369. {
  2370. STR.advanceChar(mCodeBlock->code[ip++]);
  2371. return OPCodeReturn::success;
  2372. }
  2373. OPCodeReturn CodeInterpreter::op_advance_str_comma(U32 &ip)
  2374. {
  2375. STR.advanceChar('_');
  2376. return OPCodeReturn::success;
  2377. }
  2378. OPCodeReturn CodeInterpreter::op_advance_str_nul(U32 &ip)
  2379. {
  2380. STR.advanceChar(0);
  2381. return OPCodeReturn::success;
  2382. }
  2383. OPCodeReturn CodeInterpreter::op_rewind_str(U32 &ip)
  2384. {
  2385. STR.rewind();
  2386. return OPCodeReturn::success;
  2387. }
  2388. OPCodeReturn CodeInterpreter::op_terminate_rewind_str(U32 &ip)
  2389. {
  2390. STR.rewindTerminate();
  2391. return OPCodeReturn::success;
  2392. }
  2393. OPCodeReturn CodeInterpreter::op_compare_str(U32 &ip)
  2394. {
  2395. intStack[++_UINT] = STR.compare();
  2396. return OPCodeReturn::success;
  2397. }
  2398. OPCodeReturn CodeInterpreter::op_push(U32 &ip)
  2399. {
  2400. STR.push();
  2401. CSTK.pushStringStackPtr(STR.getPreviousStringValuePtr());
  2402. return OPCodeReturn::success;
  2403. }
  2404. OPCodeReturn CodeInterpreter::op_push_uint(U32 &ip)
  2405. {
  2406. CSTK.pushUINT(intStack[_UINT]);
  2407. _UINT--;
  2408. return OPCodeReturn::success;
  2409. }
  2410. OPCodeReturn CodeInterpreter::op_push_flt(U32 &ip)
  2411. {
  2412. CSTK.pushFLT(floatStack[_FLT]);
  2413. _FLT--;
  2414. return OPCodeReturn::success;
  2415. }
  2416. OPCodeReturn CodeInterpreter::op_push_var(U32 &ip)
  2417. {
  2418. if (gEvalState.currentVariable)
  2419. CSTK.pushValue(gEvalState.currentVariable->value);
  2420. else
  2421. CSTK.pushString("");
  2422. return OPCodeReturn::success;
  2423. }
  2424. OPCodeReturn CodeInterpreter::op_push_this(U32 &ip)
  2425. {
  2426. StringTableEntry varName = CodeToSTE(mCodeBlock->code, ip);
  2427. ip += 2;
  2428. // shorthand OP_SETCURVAR
  2429. // If a variable is set, then these must be NULL. It is necessary
  2430. // to set this here so that the vector parser can appropriately
  2431. // identify whether it's dealing with a vector.
  2432. mPrevField = NULL;
  2433. mPrevObject = NULL;
  2434. mCurObject = NULL;
  2435. gEvalState.setCurVarName(varName);
  2436. // In order to let docblocks work properly with variables, we have
  2437. // clear the current docblock when we do an assign. This way it
  2438. // won't inappropriately carry forward to following function decls.
  2439. mCurFNDocBlock = NULL;
  2440. mCurNSDocBlock = NULL;
  2441. // shorthand OP_LOADVAR_STR (since objs can be by name we can't assume uint)
  2442. STR.setStringValue(gEvalState.getStringVariable());
  2443. // shorthand OP_PUSH
  2444. STR.push();
  2445. CSTK.pushStringStackPtr(STR.getPreviousStringValuePtr());
  2446. return OPCodeReturn::success;
  2447. }
  2448. OPCodeReturn CodeInterpreter::op_push_frame(U32 &ip)
  2449. {
  2450. STR.pushFrame();
  2451. CSTK.pushFrame();
  2452. return OPCodeReturn::success;
  2453. }
  2454. OPCodeReturn CodeInterpreter::op_assert(U32 &ip)
  2455. {
  2456. if (!intStack[_UINT--])
  2457. {
  2458. const char *message = mCurStringTable + mCodeBlock->code[ip];
  2459. U32 breakLine, inst;
  2460. mCodeBlock->findBreakLine(ip - 1, breakLine, inst);
  2461. if (PlatformAssert::processAssert(PlatformAssert::Fatal,
  2462. mCodeBlock->name ? mCodeBlock->name : "eval",
  2463. breakLine,
  2464. message))
  2465. {
  2466. if (TelDebugger && TelDebugger->isConnected() && breakLine > 0)
  2467. {
  2468. TelDebugger->breakProcess();
  2469. }
  2470. else
  2471. Platform::debugBreak();
  2472. }
  2473. }
  2474. ip++;
  2475. return OPCodeReturn::success;
  2476. }
  2477. OPCodeReturn CodeInterpreter::op_break(U32 &ip)
  2478. {
  2479. //append the ip and codeptr before managing the breakpoint!
  2480. AssertFatal(gEvalState.getStackDepth() > 0, "Empty eval stack on break!");
  2481. gEvalState.getCurrentFrame().code = mCodeBlock;
  2482. gEvalState.getCurrentFrame().ip = ip - 1;
  2483. U32 breakLine;
  2484. mCodeBlock->findBreakLine(ip - 1, breakLine, mCurrentInstruction);
  2485. if (!breakLine)
  2486. return OPCodeReturn::breakContinue;
  2487. TelDebugger->executionStopped(mCodeBlock, breakLine);
  2488. return OPCodeReturn::breakContinue;
  2489. }
  2490. OPCodeReturn CodeInterpreter::op_iter_begin_str(U32 &ip)
  2491. {
  2492. iterStack[_ITER].mIsStringIter = true;
  2493. // Emulate fallthrough:
  2494. OPCodeReturn fallthrough = op_iter_begin(ip);
  2495. return fallthrough;
  2496. }
  2497. OPCodeReturn CodeInterpreter::op_iter_begin(U32 &ip)
  2498. {
  2499. StringTableEntry varName = CodeToSTE(mCodeBlock->code, ip);
  2500. U32 failIp = mCodeBlock->code[ip + 2];
  2501. IterStackRecord& iter = iterStack[_ITER];
  2502. iter.mVariable = gEvalState.getCurrentFrame().add(varName);
  2503. if (iter.mIsStringIter)
  2504. {
  2505. iter.mData.mStr.mString = STR.getStringValuePtr();
  2506. iter.mData.mStr.mIndex = 0;
  2507. }
  2508. else
  2509. {
  2510. // Look up the object.
  2511. SimSet* set;
  2512. if (!Sim::findObject(STR.getStringValue(), set))
  2513. {
  2514. Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", STR.getStringValue());
  2515. Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
  2516. ip = failIp;
  2517. return OPCodeReturn::success;
  2518. }
  2519. // Set up.
  2520. iter.mData.mObj.mSet = set;
  2521. iter.mData.mObj.mIndex = 0;
  2522. }
  2523. _ITER++;
  2524. mIterDepth++;
  2525. STR.push();
  2526. ip += 3;
  2527. return OPCodeReturn::success;
  2528. }
  2529. OPCodeReturn CodeInterpreter::op_iter(U32 &ip)
  2530. {
  2531. U32 breakIp = mCodeBlock->code[ip];
  2532. IterStackRecord& iter = iterStack[_ITER - 1];
  2533. if (iter.mIsStringIter)
  2534. {
  2535. const char* str = StringStackPtrRef(iter.mData.mStr.mString).getPtr(&STR);
  2536. U32 startIndex = iter.mData.mStr.mIndex;
  2537. U32 endIndex = startIndex;
  2538. // Break if at end.
  2539. if (!str[startIndex])
  2540. {
  2541. ip = breakIp;
  2542. return OPCodeReturn::success; // continue in old interpreter
  2543. }
  2544. // Find right end of current component.
  2545. if (!dIsspace(str[endIndex]))
  2546. do ++endIndex;
  2547. while (str[endIndex] && !dIsspace(str[endIndex]));
  2548. // Extract component.
  2549. if (endIndex != startIndex)
  2550. {
  2551. char savedChar = str[endIndex];
  2552. const_cast< char* >(str)[endIndex] = '\0'; // We are on the string stack so this is okay.
  2553. iter.mVariable->setStringValue(&str[startIndex]);
  2554. const_cast< char* >(str)[endIndex] = savedChar;
  2555. }
  2556. else
  2557. iter.mVariable->setStringValue("");
  2558. // Skip separator.
  2559. if (str[endIndex] != '\0')
  2560. ++endIndex;
  2561. iter.mData.mStr.mIndex = endIndex;
  2562. }
  2563. else
  2564. {
  2565. U32 index = iter.mData.mObj.mIndex;
  2566. SimSet* set = iter.mData.mObj.mSet;
  2567. if (index >= set->size())
  2568. {
  2569. ip = breakIp;
  2570. return OPCodeReturn::success; // continue in old interpreter
  2571. }
  2572. iter.mVariable->setIntValue(set->at(index)->getId());
  2573. iter.mData.mObj.mIndex = index + 1;
  2574. }
  2575. ++ip;
  2576. return OPCodeReturn::success;
  2577. }
  2578. OPCodeReturn CodeInterpreter::op_iter_end(U32 &ip)
  2579. {
  2580. --_ITER;
  2581. --mIterDepth;
  2582. STR.rewind();
  2583. iterStack[_ITER].mIsStringIter = false;
  2584. return OPCodeReturn::success;
  2585. }
  2586. OPCodeReturn CodeInterpreter::op_invalid(U32 &ip)
  2587. {
  2588. // Invalid does nothing.
  2589. return OPCodeReturn::exitCode;
  2590. }