codeInterpreter.cpp 90 KB

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