codeInterpreter.cpp 92 KB

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