codeInterpreter.cpp 91 KB

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