codeInterpreter.cpp 92 KB

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