123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
- // Copyright (C) 2015 Faust Logic, Inc.
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- #include "console/codeInterpreter.h"
- #include "console/compiler.h"
- #include "console/simBase.h"
- #include "console/telnetDebugger.h"
- #include "sim/netStringTable.h"
- #include "console/ICallMethod.h"
- #include "console/stringStack.h"
- #include "util/messaging/message.h"
- #include "core/strings/findMatch.h"
- #include "core/strings/stringUnit.h"
- #include "console/console.h"
- #include "console/consoleInternal.h"
- #include "cinterface/cinterface.h"
- //#define TORQUE_VALIDATE_STACK
- using namespace Compiler;
- enum EvalConstants
- {
- MaxStackSize = 1024,
- FieldBufferSizeString = 2048,
- FieldBufferSizeNumeric = 128,
- MethodOnComponent = -2
- };
- namespace Con
- {
- // Current script file name and root, these are registered as
- // console variables.
- extern StringTableEntry gCurrentFile;
- extern StringTableEntry gCurrentRoot;
- extern S32 gObjectCopyFailures;
- }
- // Gets a component of an object's field value or a variable and returns it
- // in val.
- static void getFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField, char val[])
- {
- const char* prevVal = NULL;
- // Grab value from object.
- if (object && field)
- prevVal = object->getDataField(field, array);
- // Otherwise, grab from the string stack. The value coming in will always
- // be a string because that is how multicomponent variables are handled.
- else
- prevVal = STR.getStringValue();
- // Make sure we got a value.
- if (prevVal && *prevVal)
- {
- static const StringTableEntry xyzw[] =
- {
- StringTable->insert("x"),
- StringTable->insert("y"),
- StringTable->insert("z"),
- StringTable->insert("w")
- };
- static const StringTableEntry rgba[] =
- {
- StringTable->insert("r"),
- StringTable->insert("g"),
- StringTable->insert("b"),
- StringTable->insert("a")
- };
- // Translate xyzw and rgba into the indexed component
- // of the variable or field.
- if (subField == xyzw[0] || subField == rgba[0])
- dStrcpy(val, StringUnit::getUnit(prevVal, 0, " \t\n"), 128);
- else if (subField == xyzw[1] || subField == rgba[1])
- dStrcpy(val, StringUnit::getUnit(prevVal, 1, " \t\n"), 128);
- else if (subField == xyzw[2] || subField == rgba[2])
- dStrcpy(val, StringUnit::getUnit(prevVal, 2, " \t\n"), 128);
- else if (subField == xyzw[3] || subField == rgba[3])
- dStrcpy(val, StringUnit::getUnit(prevVal, 3, " \t\n"), 128);
- else
- val[0] = 0;
- }
- else
- val[0] = 0;
- }
- // Sets a component of an object's field value based on the sub field. 'x' will
- // set the first field, 'y' the second, and 'z' the third.
- static void setFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField)
- {
- // Copy the current string value
- char strValue[1024];
- dStrncpy(strValue, STR.getStringValue(), 1024);
- char val[1024] = "";
- const char* prevVal = NULL;
- // Set the value on an object field.
- if (object && field)
- prevVal = object->getDataField(field, array);
- // Set the value on a variable.
- else if (gEvalState.currentVariable)
- prevVal = gEvalState.getStringVariable();
- // Ensure that the variable has a value
- if (!prevVal)
- return;
- static const StringTableEntry xyzw[] =
- {
- StringTable->insert("x"),
- StringTable->insert("y"),
- StringTable->insert("z"),
- StringTable->insert("w")
- };
- static const StringTableEntry rgba[] =
- {
- StringTable->insert("r"),
- StringTable->insert("g"),
- StringTable->insert("b"),
- StringTable->insert("a")
- };
- // Insert the value into the specified
- // component of the string.
- if (subField == xyzw[0] || subField == rgba[0])
- dStrcpy(val, StringUnit::setUnit(prevVal, 0, strValue, " \t\n"), 128);
- else if (subField == xyzw[1] || subField == rgba[1])
- dStrcpy(val, StringUnit::setUnit(prevVal, 1, strValue, " \t\n"), 128);
- else if (subField == xyzw[2] || subField == rgba[2])
- dStrcpy(val, StringUnit::setUnit(prevVal, 2, strValue, " \t\n"), 128);
- else if (subField == xyzw[3] || subField == rgba[3])
- dStrcpy(val, StringUnit::setUnit(prevVal, 3, strValue, " \t\n"), 128);
- if (val[0] != 0)
- {
- // Update the field or variable.
- if (object && field)
- object->setDataField(field, 0, val);
- else if (gEvalState.currentVariable)
- gEvalState.setStringVariable(val);
- }
- }
- extern ExprEvalState gEvalState;
- char sTraceBuffer[1024];
- StringStack STR;
- ConsoleValueStack CSTK;
- U32 _FLT = 0; ///< Stack pointer for floatStack.
- U32 _UINT = 0; ///< Stack pointer for intStack.
- U32 _ITER = 0; ///< Stack pointer for iterStack.
- IterStackRecord iterStack[MaxStackSize];
- F64 floatStack[MaxStackSize];
- S64 intStack[MaxStackSize];
- char curFieldArray[256];
- char prevFieldArray[256];
- typedef OPCodeReturn(CodeInterpreter::*OpFn)(U32&);
- static OpFn gOpCodeArray[MAX_OP_CODELEN];
- CodeInterpreter::CodeInterpreter(CodeBlock *cb) :
- mCodeBlock(cb),
- mIterDepth(0),
- mCurFloatTable(nullptr),
- mCurStringTable(nullptr),
- mThisFunctionName(nullptr),
- mPopFrame(false),
- mObjectCreationStackIndex(0),
- mCurrentNewObject(nullptr),
- mFailJump(0),
- mPrevField(nullptr),
- mCurField(nullptr),
- mPrevObject(nullptr),
- mCurObject(nullptr),
- mSaveObject(nullptr),
- mThisObject(nullptr),
- mNSEntry(nullptr),
- mCurFNDocBlock(nullptr),
- mCurNSDocBlock(nullptr),
- mCallArgc(0),
- mCallArgv(nullptr),
- mSaveCodeBlock(nullptr),
- mCurrentInstruction(0)
- {
- dMemset(&mExec, 0, sizeof(mExec));
- dMemset(&mObjectCreationStack, 0, sizeof(mObjectCreationStack));
- dMemset(&mNSDocBlockClass, 0, sizeof(mNSDocBlockClass));
- }
- CodeInterpreter::~CodeInterpreter()
- {
- }
- void CodeInterpreter::init()
- {
- gOpCodeArray[OP_FUNC_DECL] = &CodeInterpreter::op_func_decl;
- gOpCodeArray[OP_CREATE_OBJECT] = &CodeInterpreter::op_create_object;
- gOpCodeArray[OP_ADD_OBJECT] = &CodeInterpreter::op_add_object;
- gOpCodeArray[OP_END_OBJECT] = &CodeInterpreter::op_end_object;
- gOpCodeArray[OP_FINISH_OBJECT] = &CodeInterpreter::op_finish_object;
- gOpCodeArray[OP_JMPIFFNOT] = &CodeInterpreter::op_jmpiffnot;
- gOpCodeArray[OP_JMPIFNOT] = &CodeInterpreter::op_jmpifnot;
- gOpCodeArray[OP_JMPIFF] = &CodeInterpreter::op_jmpiff;
- gOpCodeArray[OP_JMPIF] = &CodeInterpreter::op_jmpif;
- gOpCodeArray[OP_JMPIFNOT_NP] = &CodeInterpreter::op_jmpifnot_np;
- gOpCodeArray[OP_JMPIF_NP] = &CodeInterpreter::op_jmpif_np;
- gOpCodeArray[OP_JMP] = &CodeInterpreter::op_jmp;
- gOpCodeArray[OP_RETURN] = &CodeInterpreter::op_return;
- gOpCodeArray[OP_RETURN_VOID] = &CodeInterpreter::op_return_void;
- gOpCodeArray[OP_RETURN_FLT] = &CodeInterpreter::op_return_flt;
- gOpCodeArray[OP_RETURN_UINT] = &CodeInterpreter::op_return_uint;
- gOpCodeArray[OP_CMPEQ] = &CodeInterpreter::op_cmpeq;
- gOpCodeArray[OP_CMPGR] = &CodeInterpreter::op_cmpgr;
- gOpCodeArray[OP_CMPGE] = &CodeInterpreter::op_cmpge;
- gOpCodeArray[OP_CMPLT] = &CodeInterpreter::op_cmplt;
- gOpCodeArray[OP_CMPLE] = &CodeInterpreter::op_cmple;
- gOpCodeArray[OP_CMPNE] = &CodeInterpreter::op_cmpne;
- gOpCodeArray[OP_XOR] = &CodeInterpreter::op_xor;
- gOpCodeArray[OP_MOD] = &CodeInterpreter::op_mod;
- gOpCodeArray[OP_BITAND] = &CodeInterpreter::op_bitand;
- gOpCodeArray[OP_BITOR] = &CodeInterpreter::op_bitor;
- gOpCodeArray[OP_NOT] = &CodeInterpreter::op_not;
- gOpCodeArray[OP_NOTF] = &CodeInterpreter::op_notf;
- gOpCodeArray[OP_ONESCOMPLEMENT] = &CodeInterpreter::op_onescomplement;
- gOpCodeArray[OP_SHR] = &CodeInterpreter::op_shr;
- gOpCodeArray[OP_SHL] = &CodeInterpreter::op_shl;
- gOpCodeArray[OP_AND] = &CodeInterpreter::op_and;
- gOpCodeArray[OP_OR] = &CodeInterpreter::op_or;
- gOpCodeArray[OP_ADD] = &CodeInterpreter::op_add;
- gOpCodeArray[OP_SUB] = &CodeInterpreter::op_sub;
- gOpCodeArray[OP_MUL] = &CodeInterpreter::op_mul;
- gOpCodeArray[OP_DIV] = &CodeInterpreter::op_div;
- gOpCodeArray[OP_NEG] = &CodeInterpreter::op_neg;
- gOpCodeArray[OP_INC] = &CodeInterpreter::op_inc;
- gOpCodeArray[OP_DEC] = &CodeInterpreter::op_dec;
- gOpCodeArray[OP_SETCURVAR] = &CodeInterpreter::op_setcurvar;
- gOpCodeArray[OP_SETCURVAR_CREATE] = &CodeInterpreter::op_setcurvar_create;
- gOpCodeArray[OP_SETCURVAR_ARRAY] = &CodeInterpreter::op_setcurvar_array;
- gOpCodeArray[OP_SETCURVAR_ARRAY_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_varlookup;
- gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE] = &CodeInterpreter::op_setcurvar_array_create;
- gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_create_varlookup;
- gOpCodeArray[OP_LOADVAR_UINT] = &CodeInterpreter::op_loadvar_uint;
- gOpCodeArray[OP_LOADVAR_FLT] = &CodeInterpreter::op_loadvar_flt;
- gOpCodeArray[OP_LOADVAR_STR] = &CodeInterpreter::op_loadvar_str;
- gOpCodeArray[OP_LOADVAR_VAR] = &CodeInterpreter::op_loadvar_var;
- gOpCodeArray[OP_SAVEVAR_UINT] = &CodeInterpreter::op_savevar_uint;
- gOpCodeArray[OP_SAVEVAR_FLT] = &CodeInterpreter::op_savevar_flt;
- gOpCodeArray[OP_SAVEVAR_STR] = &CodeInterpreter::op_savevar_str;
- gOpCodeArray[OP_SAVEVAR_VAR] = &CodeInterpreter::op_savevar_var;
- gOpCodeArray[OP_SETCUROBJECT] = &CodeInterpreter::op_setcurobject;
- gOpCodeArray[OP_SETCUROBJECT_INTERNAL] = &CodeInterpreter::op_setcurobject_internal;
- gOpCodeArray[OP_SETCUROBJECT_NEW] = &CodeInterpreter::op_setcurobject_new;
- gOpCodeArray[OP_SETCURFIELD] = &CodeInterpreter::op_setcurfield;
- gOpCodeArray[OP_SETCURFIELD_ARRAY] = &CodeInterpreter::op_setcurfield_array;
- gOpCodeArray[OP_SETCURFIELD_TYPE] = &CodeInterpreter::op_setcurfield_type;
- gOpCodeArray[OP_SETCURFIELD_ARRAY_VAR] = &CodeInterpreter::op_setcurfield_array_var;
- gOpCodeArray[OP_SETCURFIELD_THIS] = &CodeInterpreter::op_setcurfield_this;
- gOpCodeArray[OP_LOADFIELD_UINT] = &CodeInterpreter::op_loadfield_uint;
- gOpCodeArray[OP_LOADFIELD_FLT] = &CodeInterpreter::op_loadfield_flt;
- gOpCodeArray[OP_LOADFIELD_STR] = &CodeInterpreter::op_loadfield_str;
- gOpCodeArray[OP_SAVEFIELD_UINT] = &CodeInterpreter::op_savefield_uint;
- gOpCodeArray[OP_SAVEFIELD_FLT] = &CodeInterpreter::op_savefield_flt;
- gOpCodeArray[OP_SAVEFIELD_STR] = &CodeInterpreter::op_savefield_str;
- gOpCodeArray[OP_STR_TO_UINT] = &CodeInterpreter::op_str_to_uint;
- gOpCodeArray[OP_STR_TO_FLT] = &CodeInterpreter::op_str_to_flt;
- gOpCodeArray[OP_STR_TO_NONE] = &CodeInterpreter::op_str_to_none;
- gOpCodeArray[OP_FLT_TO_UINT] = &CodeInterpreter::op_flt_to_uint;
- gOpCodeArray[OP_FLT_TO_STR] = &CodeInterpreter::op_flt_to_str;
- gOpCodeArray[OP_FLT_TO_NONE] = &CodeInterpreter::op_flt_to_none;
- gOpCodeArray[OP_UINT_TO_FLT] = &CodeInterpreter::op_uint_to_flt;
- gOpCodeArray[OP_UINT_TO_STR] = &CodeInterpreter::op_uint_to_str;
- gOpCodeArray[OP_UINT_TO_NONE] = &CodeInterpreter::op_uint_to_none;
- gOpCodeArray[OP_COPYVAR_TO_NONE] = &CodeInterpreter::op_copyvar_to_none;
- gOpCodeArray[OP_LOADIMMED_UINT] = &CodeInterpreter::op_loadimmed_uint;
- gOpCodeArray[OP_LOADIMMED_FLT] = &CodeInterpreter::op_loadimmed_flt;
- gOpCodeArray[OP_TAG_TO_STR] = &CodeInterpreter::op_tag_to_str;
- gOpCodeArray[OP_LOADIMMED_STR] = &CodeInterpreter::op_loadimmed_str;
- gOpCodeArray[OP_DOCBLOCK_STR] = &CodeInterpreter::op_docblock_str;
- gOpCodeArray[OP_LOADIMMED_IDENT] = &CodeInterpreter::op_loadimmed_ident;
- gOpCodeArray[OP_CALLFUNC_RESOLVE] = &CodeInterpreter::op_callfunc_resolve;
- gOpCodeArray[OP_CALLFUNC] = &CodeInterpreter::op_callfunc;
- gOpCodeArray[OP_CALLFUNC_POINTER] = &CodeInterpreter::op_callfunc_pointer;
- gOpCodeArray[OP_CALLFUNC_THIS] = &CodeInterpreter::op_callfunc_this;
- gOpCodeArray[OP_ADVANCE_STR] = &CodeInterpreter::op_advance_str;
- gOpCodeArray[OP_ADVANCE_STR_APPENDCHAR] = &CodeInterpreter::op_advance_str_appendchar;
- gOpCodeArray[OP_ADVANCE_STR_COMMA] = &CodeInterpreter::op_advance_str_comma;
- gOpCodeArray[OP_ADVANCE_STR_NUL] = &CodeInterpreter::op_advance_str_nul;
- gOpCodeArray[OP_REWIND_STR] = &CodeInterpreter::op_rewind_str;
- gOpCodeArray[OP_TERMINATE_REWIND_STR] = &CodeInterpreter::op_terminate_rewind_str;
- gOpCodeArray[OP_COMPARE_STR] = &CodeInterpreter::op_compare_str;
- gOpCodeArray[OP_PUSH] = &CodeInterpreter::op_push;
- gOpCodeArray[OP_PUSH_UINT] = &CodeInterpreter::op_push_uint;
- gOpCodeArray[OP_PUSH_FLT] = &CodeInterpreter::op_push_flt;
- gOpCodeArray[OP_PUSH_VAR] = &CodeInterpreter::op_push_var;
- gOpCodeArray[OP_PUSH_THIS] = &CodeInterpreter::op_push_this;
- gOpCodeArray[OP_PUSH_FRAME] = &CodeInterpreter::op_push_frame;
- gOpCodeArray[OP_ASSERT] = &CodeInterpreter::op_assert;
- gOpCodeArray[OP_BREAK] = &CodeInterpreter::op_break;
- gOpCodeArray[OP_ITER_BEGIN_STR] = &CodeInterpreter::op_iter_begin_str;
- gOpCodeArray[OP_ITER_BEGIN] = &CodeInterpreter::op_iter_begin;
- gOpCodeArray[OP_ITER] = &CodeInterpreter::op_iter;
- gOpCodeArray[OP_ITER_END] = &CodeInterpreter::op_iter_end;
- gOpCodeArray[OP_INVALID] = &CodeInterpreter::op_invalid;
- }
- ConsoleValueRef CodeInterpreter::exec(U32 ip,
- StringTableEntry functionName,
- Namespace *thisNamespace,
- U32 argc,
- ConsoleValueRef *argv,
- bool noCalls,
- StringTableEntry packageName,
- S32 setFrame)
- {
- mExec.functionName = functionName;
- mExec.thisNamespace = thisNamespace;
- mExec.argc = argc;
- mExec.argv = argv;
- mExec.noCalls = noCalls;
- mExec.packageName = packageName;
- mExec.setFrame = setFrame;
- mCodeBlock->incRefCount();
- mPopFrame = false;
- #ifdef TORQUE_VALIDATE_STACK
- U32 stackStart = STR.mStartStackSize;
- #endif
- STR.clearFunctionOffset(); // ensures arg buffer offset is back to 0
- // Lets load up our function arguments.
- parseArgs(ip);
- // Grab the state of the telenet debugger here once
- // so that the push and pop frames are always balanced.
- const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
- if (telDebuggerOn && setFrame < 0)
- TelDebugger->pushStackFrame();
- mSaveCodeBlock = CodeBlock::smCurrentCodeBlock;
- CodeBlock::smCurrentCodeBlock = mCodeBlock;
- if (mCodeBlock->name)
- {
- Con::gCurrentFile = mCodeBlock->name;
- Con::gCurrentRoot = mCodeBlock->modPath;
- }
- U32 *code = mCodeBlock->code;
- while (true)
- {
- mCurrentInstruction = code[ip++];
- mNSEntry = nullptr;
- #ifdef TORQUE_VALIDATE_STACK
- // OP Code check.
- AssertFatal(mCurrentInstruction < MAX_OP_CODELEN, "Invalid OP code in script interpreter");
- #endif
- breakContinueLabel:
- OPCodeReturn ret = (this->*gOpCodeArray[mCurrentInstruction])(ip);
- if (ret == OPCodeReturn::exitCode)
- break;
- else if (ret == OPCodeReturn::breakContinue)
- goto breakContinueLabel;
- }
- if (telDebuggerOn && setFrame < 0)
- TelDebugger->popStackFrame();
- if (mPopFrame)
- gEvalState.popFrame();
- if (argv)
- {
- if (gEvalState.traceOn)
- {
- sTraceBuffer[0] = 0;
- dStrcat(sTraceBuffer, "Leaving ", 1024);
- if (packageName)
- {
- dStrcat(sTraceBuffer, "[", 1024);
- dStrcat(sTraceBuffer, packageName, 1024);
- dStrcat(sTraceBuffer, "]", 1024);
- }
- if (thisNamespace && thisNamespace->mName)
- {
- dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
- "%s::%s() - return %s", thisNamespace->mName, mThisFunctionName, STR.getStringValue());
- }
- else
- {
- dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
- "%s() - return %s", mThisFunctionName, STR.getStringValue());
- }
- Con::printf("%s", sTraceBuffer);
- }
- }
- CodeBlock::smCurrentCodeBlock = mSaveCodeBlock;
- if (mSaveCodeBlock && mSaveCodeBlock->name)
- {
- Con::gCurrentFile = mSaveCodeBlock->name;
- Con::gCurrentRoot = mSaveCodeBlock->modPath;
- }
- mCodeBlock->decRefCount();
- #ifdef TORQUE_VALIDATE_STACK
- AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
- AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
- #endif
- return mReturnValue;
- }
- void CodeInterpreter::parseArgs(U32 &ip)
- {
- U32 *code = mCodeBlock->code;
- if (mExec.argv)
- {
- U32 fnArgc = code[ip + 2 + 6];
- mThisFunctionName = Compiler::CodeToSTE(code, ip);
- S32 wantedArgc = getMin(mExec.argc - 1, fnArgc); // argv[0] is func name
- if (gEvalState.traceOn)
- {
- sTraceBuffer[0] = 0;
- dStrcat(sTraceBuffer, "Entering ", 1024);
- if (mExec.packageName)
- {
- dStrcat(sTraceBuffer, "[", 1024);
- dStrcat(sTraceBuffer, mExec.packageName, 1024);
- dStrcat(sTraceBuffer, "]", 1024);
- }
- if (mExec.thisNamespace && mExec.thisNamespace->mName)
- {
- dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
- "%s::%s(", mExec.thisNamespace->mName, mThisFunctionName);
- }
- else
- {
- dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
- "%s(", mThisFunctionName);
- }
- for (S32 i = 0; i < wantedArgc; i++)
- {
- dStrcat(sTraceBuffer, mExec.argv[i + 1], 1024);
- if (i != wantedArgc - 1)
- dStrcat(sTraceBuffer, ", ", 1024);
- }
- dStrcat(sTraceBuffer, ")", 1024);
- Con::printf("%s", sTraceBuffer);
- }
- gEvalState.pushFrame(mThisFunctionName, mExec.thisNamespace);
- mPopFrame = true;
- StringTableEntry thisPointer = StringTable->insert("%this");
- for (S32 i = 0; i < wantedArgc; i++)
- {
- StringTableEntry var = Compiler::CodeToSTE(code, ip + (2 + 6 + 1) + (i * 2));
- gEvalState.setCurVarNameCreate(var);
- ConsoleValueRef ref = mExec.argv[i + 1];
- switch (ref.getType())
- {
- case ConsoleValue::TypeInternalInt:
- gEvalState.setIntVariable(ref);
- break;
- case ConsoleValue::TypeInternalFloat:
- gEvalState.setFloatVariable(ref);
- break;
- case ConsoleValue::TypeInternalStringStackPtr:
- gEvalState.setStringStackPtrVariable(ref.getStringStackPtrValue());
- break;
- case ConsoleValue::TypeInternalStackString:
- case ConsoleValue::TypeInternalString:
- default:
- gEvalState.setStringVariable(ref);
- break;
- }
- if (var == thisPointer)
- {
- // %this gets optimized as it is flagged as a constant.
- // Since it is guarenteed to be constant, we can then perform optimizations.
- gEvalState.currentVariable->mIsConstant = true;
- // Store a reference to the this pointer object.
- mThisObject = Sim::findObject(gEvalState.getStringVariable());
- }
- }
- ip = ip + (fnArgc * 2) + (2 + 6 + 1);
- mCurFloatTable = mCodeBlock->functionFloats;
- mCurStringTable = mCodeBlock->functionStrings;
- }
- else
- {
- mCurFloatTable = mCodeBlock->globalFloats;
- mCurStringTable = mCodeBlock->globalStrings;
- // If requested stack frame isn't available, request a new one
- // (this prevents assert failures when creating local
- // variables without a stack frame)
- if (gEvalState.getStackDepth() <= mExec.setFrame)
- mExec.setFrame = -1;
- // Do we want this code to execute using a new stack frame?
- if (mExec.setFrame < 0)
- {
- gEvalState.pushFrame(NULL, NULL);
- mPopFrame = true;
- }
- else
- {
- // We want to copy a reference to an existing stack frame
- // on to the top of the stack. Any change that occurs to
- // the locals during this new frame will also occur in the
- // original frame.
- S32 stackIndex = gEvalState.getStackDepth() - mExec.setFrame - 1;
- gEvalState.pushFrameRef(stackIndex);
- mPopFrame = true;
- }
- }
- }
- OPCodeReturn CodeInterpreter::op_func_decl(U32 &ip)
- {
- U32 *code = mCodeBlock->code;
- if (!mExec.noCalls)
- {
- StringTableEntry fnName = CodeToSTE(code, ip);
- StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
- StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
- bool hasBody = (code[ip + 6] & 0x01) != 0;
- U32 lineNumber = code[ip + 6] >> 1;
- Namespace::unlinkPackages();
- Namespace *ns = Namespace::find(fnNamespace, fnPackage);
- ns->addFunction(fnName, mCodeBlock, hasBody ? ip : 0, mCurFNDocBlock ? dStrdup(mCurFNDocBlock) : NULL, lineNumber);// if no body, set the IP to 0
- if (mCurNSDocBlock)
- {
- // If we have a docblock before we declare the function in the script file,
- // this will attempt to set the doc block to the function.
- // See OP_DOCBLOCK_STR
- if (fnNamespace == StringTable->lookup(mNSDocBlockClass))
- {
- char *usageStr = dStrdup(mCurNSDocBlock);
- usageStr[dStrlen(usageStr)] = '\0';
- ns->mUsage = usageStr;
- ns->mCleanUpUsage = true;
- mCurNSDocBlock = NULL;
- }
- }
- Namespace::relinkPackages();
- // If we had a docblock, it's definitely not valid anymore, so clear it out.
- mCurFNDocBlock = NULL;
- //Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
- }
- ip = code[ip + 7];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_create_object(U32 &ip)
- {
- U32 *code = mCodeBlock->code;
- // Read some useful info.
- StringTableEntry objParent = CodeToSTE(code, ip);
- bool isDataBlock = code[ip + 2];
- bool isInternal = code[ip + 3];
- bool isSingleton = code[ip + 4];
- U32 lineNumber = code[ip + 5];
- mFailJump = code[ip + 6];
- // If we don't allow calls, we certainly don't allow creating objects!
- // Moved this to after failJump is set. Engine was crashing when
- // noCalls = true and an object was being created at the beginning of
- // a file. ADL.
- if (mExec.noCalls)
- {
- ip = mFailJump;
- return OPCodeReturn::success;
- }
- // Push the old info to the stack
- //Assert( objectCreationStackIndex < objectCreationStackSize );
- mObjectCreationStack[mObjectCreationStackIndex].newObject = mCurrentNewObject;
- mObjectCreationStack[mObjectCreationStackIndex++].failJump = mFailJump;
- // Get the constructor information off the stack.
- CSTK.getArgcArgv(NULL, &mCallArgc, &mCallArgv);
- const char *objectName = mCallArgv[2];
- // Con::printf("Creating object...");
- // objectName = argv[1]...
- mCurrentNewObject = NULL;
- // Are we creating a datablock? If so, deal with case where we override
- // an old one.
- if (isDataBlock)
- {
- // Con::printf(" - is a datablock");
- // Find the old one if any.
- SimObject *db = Sim::getDataBlockGroup()->findObject(objectName);
- // Make sure we're not changing types on ourselves...
- if (db && dStricmp(db->getClassName(), mCallArgv[1]))
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare data block %s with a different class.", mCodeBlock->getFileLine(ip), objectName);
- ip = mFailJump;
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- // If there was one, set the currentNewObject and move on.
- if (db)
- mCurrentNewObject = db;
- }
- else if (!isInternal)
- {
- // IF we aren't looking at a local/internal object, then check if
- // this object already exists in the global space
- AbstractClassRep* rep = AbstractClassRep::findClassRep(objectName);
- if (rep != NULL) {
- Con::errorf(ConsoleLogEntry::General, "%s: Cannot name object [%s] the same name as a script class.",
- mCodeBlock->getFileLine(ip), objectName);
- ip = mFailJump;
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- SimObject *obj = Sim::findObject((const char*)objectName);
- if (obj /*&& !obj->isLocalName()*/)
- {
- if (isSingleton)
- {
- // Make sure we're not trying to change types
- if (dStricmp(obj->getClassName(), (const char*)mCallArgv[1]) != 0)
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s] with a different class [%s] - was [%s].",
- mCodeBlock->getFileLine(ip), objectName, (const char*)mCallArgv[1], obj->getClassName());
- ip = mFailJump;
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- // We're creating a singleton, so use the found object
- // instead of creating a new object.
- mCurrentNewObject = obj;
- }
- else
- {
- const char* redefineBehavior = Con::getVariable("$Con::redefineBehavior");
- if (dStricmp(redefineBehavior, "replaceExisting") == 0)
- {
- // Save our constructor args as the argv vector is stored on the
- // string stack and may get stomped if deleteObject triggers
- // script execution.
- ConsoleValueRef savedArgv[StringStack::MaxArgs];
- for (int i = 0; i< mCallArgc; i++) {
- savedArgv[i] = mCallArgv[i];
- }
- //dMemcpy( savedArgv, callArgv, sizeof( savedArgv[ 0 ] ) * callArgc );
- // Prevent stack value corruption
- CSTK.pushFrame();
- STR.pushFrame();
- // --
- obj->deleteObject();
- obj = NULL;
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- //dMemcpy( callArgv, savedArgv, sizeof( callArgv[ 0 ] ) * callArgc );
- for (int i = 0; i<mCallArgc; i++) {
- mCallArgv[i] = savedArgv[i];
- }
- }
- else if (dStricmp(redefineBehavior, "renameNew") == 0)
- {
- for (U32 i = 1;; ++i)
- {
- String newName = String::ToString("%s%i", objectName, i);
- if (!Sim::findObject(newName))
- {
- objectName = StringTable->insert(newName);
- break;
- }
- }
- }
- else if (dStricmp(redefineBehavior, "unnameNew") == 0)
- {
- objectName = StringTable->insert("");
- }
- else if (dStricmp(redefineBehavior, "postfixNew") == 0)
- {
- const char* postfix = Con::getVariable("$Con::redefineBehaviorPostfix");
- String newName = String::ToString("%s%s", objectName, postfix);
- if (Sim::findObject(newName))
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object with postfix [%s].",
- mCodeBlock->getFileLine(ip), newName.c_str());
- ip = mFailJump;
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- else
- objectName = StringTable->insert(newName);
- }
- else
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s].",
- mCodeBlock->getFileLine(ip), objectName);
- ip = mFailJump;
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- }
- }
- }
- STR.popFrame();
- CSTK.popFrame();
- if (!mCurrentNewObject)
- {
- // Well, looks like we have to create a new object.
- ConsoleObject *object = ConsoleObject::create((const char*)mCallArgv[1]);
- // Deal with failure!
- if (!object)
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
- ip = mFailJump;
- return OPCodeReturn::success;
- }
- // Do special datablock init if appropros
- if (isDataBlock)
- {
- SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(object);
- if (dataBlock)
- {
- dataBlock->assignId();
- }
- else
- {
- // They tried to make a non-datablock with a datablock keyword!
- Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
- // Clean up...
- delete object;
- mCurrentNewObject = NULL;
- ip = mFailJump;
- return OPCodeReturn::success;
- }
- }
- // Finally, set currentNewObject to point to the new one.
- mCurrentNewObject = dynamic_cast<SimObject *>(object);
- // Deal with the case of a non-SimObject.
- if (!mCurrentNewObject)
- {
- Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
- delete object;
- mCurrentNewObject = NULL;
- ip = mFailJump;
- return OPCodeReturn::success;
- }
- // Set the declaration line
- mCurrentNewObject->setDeclarationLine(lineNumber);
- // Set the file that this object was created in
- mCurrentNewObject->setFilename(mCodeBlock->name);
- // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
- if (*objParent)
- {
- // Find it!
- SimObject *parent;
- if (Sim::findObject(objParent, parent))
- {
- // Con::printf(" - Parent object found: %s", parent->getClassName());
- mCurrentNewObject->setCopySource(parent);
- mCurrentNewObject->assignFieldsFrom(parent);
- // copy any substitution statements
- SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
- if (parent_db)
- {
- SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
- if (currentNewObject_db)
- currentNewObject_db->copySubstitutionsFrom(parent_db);
- }
- }
- else
- {
- if (Con::gObjectCopyFailures == -1)
- Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", mCodeBlock->getFileLine(ip), objParent, (const char*)mCallArgv[1]);
- else
- ++Con::gObjectCopyFailures;
- // Fail to create the object.
- delete object;
- mCurrentNewObject = NULL;
- ip = mFailJump;
- return OPCodeReturn::success;
- }
- }
- // If a name was passed, assign it.
- if (objectName[0])
- {
- if (!isInternal)
- mCurrentNewObject->assignName(objectName);
- else
- mCurrentNewObject->setInternalName(objectName);
- // Set the original name
- mCurrentNewObject->setOriginalName(objectName);
- }
- // Prevent stack value corruption
- CSTK.pushFrame();
- STR.pushFrame();
- // --
- // Do the constructor parameters.
- if (!mCurrentNewObject->processArguments(mCallArgc - 3, mCallArgv + 3))
- {
- delete mCurrentNewObject;
- mCurrentNewObject = NULL;
- ip = mFailJump;
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- return OPCodeReturn::success;
- }
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- // If it's not a datablock, allow people to modify bits of it.
- if (!isDataBlock)
- {
- mCurrentNewObject->setModStaticFields(true);
- mCurrentNewObject->setModDynamicFields(true);
- }
- }
- else
- {
- mCurrentNewObject->reloadReset(); // AFX (reload-reset)
- // Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
- if (*objParent)
- {
- // Find it!
- SimObject *parent;
- if (Sim::findObject(objParent, parent))
- {
- // Con::printf(" - Parent object found: %s", parent->getClassName());
- // temporarily block name change
- SimObject::preventNameChanging = true;
- mCurrentNewObject->setCopySource(parent);
- mCurrentNewObject->assignFieldsFrom(parent);
- // restore name changing
- SimObject::preventNameChanging = false;
- // copy any substitution statements
- SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
- if (parent_db)
- {
- SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
- if (currentNewObject_db)
- currentNewObject_db->copySubstitutionsFrom(parent_db);
- }
- }
- else
- Con::errorf(ConsoleLogEntry::General, "%d: Unable to find parent object %s for %s.", lineNumber, objParent, (const char*)mCallArgv[1]);
- }
- }
- // Advance the IP past the create info...
- ip += 7;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_add_object(U32 &ip)
- {
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- // Do we place this object at the root?
- bool placeAtRoot = mCodeBlock->code[ip++];
- // Con::printf("Adding object %s", currentNewObject->getName());
- // Prevent stack value corruption
- CSTK.pushFrame();
- STR.pushFrame();
- // --
- // Make sure it wasn't already added, then add it.
- if (mCurrentNewObject->isProperlyAdded() == false)
- {
- bool ret = false;
- Message *msg = dynamic_cast<Message *>(mCurrentNewObject);
- if (msg)
- {
- SimObjectId id = Message::getNextMessageID();
- if (id != 0xffffffff)
- ret = mCurrentNewObject->registerObject(id);
- else
- Con::errorf("%s: No more object IDs available for messages", mCodeBlock->getFileLine(ip));
- }
- else
- ret = mCurrentNewObject->registerObject();
- if (!ret)
- {
- // This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
- Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", mCodeBlock->getFileLine(ip), mCurrentNewObject->getName(), mCurrentNewObject->getClassName());
- delete mCurrentNewObject;
- mCurrentNewObject = NULL;
- ip = mFailJump;
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- return OPCodeReturn::success;
- }
- }
- // Are we dealing with a datablock?
- SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(mCurrentNewObject);
- static String errorStr;
- // If so, preload it.
- if (dataBlock && !dataBlock->preload(true, errorStr))
- {
- Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s: %s.", mCodeBlock->getFileLine(ip),
- mCurrentNewObject->getName(), errorStr.c_str());
- dataBlock->deleteObject();
- mCurrentNewObject = NULL;
- ip = mFailJump;
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- return OPCodeReturn::success;
- }
- // What group will we be added to, if any?
- U32 groupAddId = intStack[_UINT];
- SimGroup *grp = NULL;
- SimSet *set = NULL;
- bool isMessage = dynamic_cast<Message *>(mCurrentNewObject) != NULL;
- if (!placeAtRoot || !mCurrentNewObject->getGroup())
- {
- if (!isMessage)
- {
- if (!placeAtRoot)
- {
- // Otherwise just add to the requested group or set.
- if (!Sim::findObject(groupAddId, grp))
- Sim::findObject(groupAddId, set);
- }
- if (placeAtRoot)
- {
- // Deal with the instantGroup if we're being put at the root or we're adding to a component.
- if (Con::gInstantGroup.isEmpty()
- || !Sim::findObject(Con::gInstantGroup, grp))
- grp = Sim::getRootGroup();
- }
- }
- // If we didn't get a group, then make sure we have a pointer to
- // the rootgroup.
- if (!grp)
- grp = Sim::getRootGroup();
- // add to the parent group
- grp->addObject(mCurrentNewObject);
- // If for some reason the add failed, add the object to the
- // root group so it won't leak.
- if (!mCurrentNewObject->getGroup())
- Sim::getRootGroup()->addObject(mCurrentNewObject);
- // add to any set we might be in
- if (set)
- set->addObject(mCurrentNewObject);
- }
- // store the new object's ID on the stack (overwriting the group/set
- // id, if one was given, otherwise getting pushed)
- if (placeAtRoot)
- intStack[_UINT] = mCurrentNewObject->getId();
- else
- intStack[++_UINT] = mCurrentNewObject->getId();
- // Prevent stack value corruption
- CSTK.popFrame();
- STR.popFrame();
- // --
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_end_object(U32 &ip)
- {
- // If we're not to be placed at the root, make sure we clean up
- // our group reference.
- bool placeAtRoot = mCodeBlock->code[ip++];
- if (!placeAtRoot)
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_finish_object(U32 &ip)
- {
- if (mCurrentNewObject)
- mCurrentNewObject->onPostAdd();
- //Assert( objectCreationStackIndex >= 0 );
- // Restore the object info from the stack [7/9/2007 Black]
- mCurrentNewObject = mObjectCreationStack[--mObjectCreationStackIndex].newObject;
- mFailJump = mObjectCreationStack[mObjectCreationStackIndex].failJump;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpiffnot(U32 &ip)
- {
- if (floatStack[_FLT--])
- {
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpifnot(U32 &ip)
- {
- if (intStack[_UINT--])
- {
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpiff(U32 &ip)
- {
- if (!floatStack[_FLT--])
- {
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpif(U32 &ip)
- {
- if (!intStack[_UINT--])
- {
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpifnot_np(U32 &ip)
- {
- if (intStack[_UINT])
- {
- _UINT--;
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmpif_np(U32 &ip)
- {
- if (!intStack[_UINT])
- {
- _UINT--;
- ip++;
- return OPCodeReturn::success;
- }
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_jmp(U32 &ip)
- {
- ip = mCodeBlock->code[ip];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_return_void(U32 &ip)
- {
- STR.setStringValue("");
- // We're falling thru here on purpose.
- OPCodeReturn ret = op_return(ip);
- return ret;
- }
- OPCodeReturn CodeInterpreter::op_return(U32 &ip)
- {
- StringStackPtr retValue = STR.getStringValuePtr();
- if (mIterDepth > 0)
- {
- // Clear iterator state.
- while (mIterDepth > 0)
- {
- iterStack[--_ITER].mIsStringIter = false;
- --mIterDepth;
- }
- STR.rewind();
- STR.setStringValue(StringStackPtrRef(retValue).getPtr(&STR)); // Not nice but works.
- retValue = STR.getStringValuePtr();
- }
- // Previously the return value was on the stack and would be returned using STR.getStringValue().
- // Now though we need to wrap it in a ConsoleValueRef
- mReturnValue.value = CSTK.pushStringStackPtr(retValue);
- return OPCodeReturn::exitCode;
- }
- OPCodeReturn CodeInterpreter::op_return_flt(U32 &ip)
- {
- if (mIterDepth > 0)
- {
- // Clear iterator state.
- while (mIterDepth > 0)
- {
- iterStack[--_ITER].mIsStringIter = false;
- --mIterDepth;
- }
- }
- mReturnValue.value = CSTK.pushFLT(floatStack[_FLT]);
- _FLT--;
- return OPCodeReturn::exitCode;
- }
- OPCodeReturn CodeInterpreter::op_return_uint(U32 &ip)
- {
- if (mIterDepth > 0)
- {
- // Clear iterator state.
- while (mIterDepth > 0)
- {
- iterStack[--_ITER].mIsStringIter = false;
- --mIterDepth;
- }
- }
- mReturnValue.value = CSTK.pushUINT(intStack[_UINT]);
- _UINT--;
- return OPCodeReturn::exitCode;
- }
- OPCodeReturn CodeInterpreter::op_cmpeq(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] == floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_cmpgr(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] > floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_cmpge(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] >= floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_cmplt(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] < floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_cmple(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] <= floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_cmpne(U32 &ip)
- {
- intStack[_UINT + 1] = bool(floatStack[_FLT] != floatStack[_FLT - 1]);
- _UINT++;
- _FLT -= 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_xor(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] ^ intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_mod(U32 &ip)
- {
- if (intStack[_UINT - 1] != 0)
- intStack[_UINT - 1] = intStack[_UINT] % intStack[_UINT - 1];
- else
- intStack[_UINT - 1] = 0;
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_bitand(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] & intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_bitor(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] | intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_not(U32 &ip)
- {
- intStack[_UINT] = !intStack[_UINT];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_notf(U32 &ip)
- {
- intStack[_UINT + 1] = !floatStack[_FLT];
- _FLT--;
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_onescomplement(U32 &ip)
- {
- intStack[_UINT] = ~intStack[_UINT];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_shr(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] >> intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_shl(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] << intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_and(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] && intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_or(U32 &ip)
- {
- intStack[_UINT - 1] = intStack[_UINT] || intStack[_UINT - 1];
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_add(U32 &ip)
- {
- floatStack[_FLT - 1] = floatStack[_FLT] + floatStack[_FLT - 1];
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_sub(U32 &ip)
- {
- floatStack[_FLT - 1] = floatStack[_FLT] - floatStack[_FLT - 1];
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_mul(U32 &ip)
- {
- floatStack[_FLT - 1] = floatStack[_FLT] * floatStack[_FLT - 1];
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_div(U32 &ip)
- {
- floatStack[_FLT - 1] = floatStack[_FLT] / floatStack[_FLT - 1];
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_neg(U32 &ip)
- {
- floatStack[_FLT] = -floatStack[_FLT];
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_inc(U32 &ip)
- {
- StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // If a variable is set, then these must be NULL. It is necessary
- // to set this here so that the vector parser can appropriately
- // identify whether it's dealing with a vector.
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarNameCreate(var);
- // In order to let docblocks work properly with variables, we have
- // clear the current docblock when we do an assign. This way it
- // won't inappropriately carry forward to following function decls.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- F64 val = gEvalState.getFloatVariable() + 1.0;
- gEvalState.setFloatVariable(val);
- // We gotta push val onto the stack. What if we have
- // more expressions that have to use this.
- // If we don't, we send out an op code to pop it.
- floatStack[_FLT + 1] = val;
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_dec(U32 &ip)
- {
- StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // If a variable is set, then these must be NULL. It is necessary
- // to set this here so that the vector parser can appropriately
- // identify whether it's dealing with a vector.
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarNameCreate(var);
- // In order to let docblocks work properly with variables, we have
- // clear the current docblock when we do an assign. This way it
- // won't inappropriately carry forward to following function decls.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- F64 val = gEvalState.getFloatVariable() - 1.0;
- gEvalState.setFloatVariable(val);
- // We gotta push val onto the stack. What if we have
- // more expressions that have to use this.
- // If we don't, we send out an op code to pop it.
- floatStack[_FLT + 1] = val;
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar(U32 &ip)
- {
- StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // If a variable is set, then these must be NULL. It is necessary
- // to set this here so that the vector parser can appropriately
- // identify whether it's dealing with a vector.
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarName(var);
- // In order to let docblocks work properly with variables, we have
- // clear the current docblock when we do an assign. This way it
- // won't inappropriately carry forward to following function decls.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar_create(U32 &ip)
- {
- StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // See OP_SETCURVAR
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarNameCreate(var);
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar_array(U32 &ip)
- {
- StringTableEntry var = STR.getSTValue();
- // See OP_SETCURVAR
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarName(var);
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar_array_varlookup(U32 &ip)
- {
- StringTableEntry arrayName = CodeToSTE(mCodeBlock->code, ip);
- StringTableEntry arrayLookup = CodeToSTE(mCodeBlock->code, ip + 2);
- ip += 4;
- STR.setStringValue(arrayName);
- STR.advance();
- // See OP_SETCURVAR
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- // resolve arrayLookup to get the 'value'
- // Note: we have to setCurVarNameCreate in case the var doesn't exist.
- // this won't cause much of a performance hit since vars are hashed.
- gEvalState.setCurVarNameCreate(arrayLookup);
- StringTableEntry hash = gEvalState.getStringVariable();
- STR.setStringValue(hash);
- STR.rewind();
- // Generate new array name.
- StringTableEntry var = STR.getSTValue();
- gEvalState.setCurVarName(var);
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar_array_create(U32 &ip)
- {
- StringTableEntry var = STR.getSTValue();
- // See OP_SETCURVAR
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarNameCreate(var);
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurvar_array_create_varlookup(U32 &ip)
- {
- StringTableEntry arrayName = CodeToSTE(mCodeBlock->code, ip);
- StringTableEntry arrayLookup = CodeToSTE(mCodeBlock->code, ip + 2);
- ip += 4;
- // See OP_SETCURVAR
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- STR.setStringValue(arrayName);
- STR.advance();
- // resolve arrayLookup to get the 'value'
- // Note: we have to setCurVarNameCreate in case the var doesn't exist.
- // this won't cause much of a performance hit since vars are hashed.
- gEvalState.setCurVarNameCreate(arrayLookup);
- StringTableEntry hash = gEvalState.getStringVariable();
- STR.setStringValue(hash);
- STR.rewind();
- // Generate new array name.
- StringTableEntry var = STR.getSTValue();
- gEvalState.setCurVarNameCreate(var);
- // See OP_SETCURVAR for why we do this.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadvar_uint(U32 &ip)
- {
- intStack[_UINT + 1] = gEvalState.getIntVariable();
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadvar_flt(U32 &ip)
- {
- floatStack[_FLT + 1] = gEvalState.getFloatVariable();
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadvar_str(U32 &ip)
- {
- StringTableEntry val = gEvalState.getStringVariable();
- STR.setStringValue(val);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadvar_var(U32 &ip)
- {
- // Sets current source of OP_SAVEVAR_VAR
- gEvalState.copyVariable = gEvalState.currentVariable;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savevar_uint(U32 &ip)
- {
- gEvalState.setIntVariable(intStack[_UINT]);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savevar_flt(U32 &ip)
- {
- gEvalState.setFloatVariable(floatStack[_FLT]);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savevar_str(U32 &ip)
- {
- gEvalState.setStringVariable(STR.getStringValue());
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savevar_var(U32 &ip)
- {
- // this basically handles %var1 = %var2
- gEvalState.setCopyVariable();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurobject(U32 &ip)
- {
- // Save the previous object for parsing vector fields.
- mPrevObject = mCurObject;
- StringTableEntry val = STR.getStringValue();
- // Sim::findObject will sometimes find valid objects from
- // multi-component strings. This makes sure that doesn't
- // happen.
- for (const char* check = val; *check; check++)
- {
- if (*check == ' ')
- {
- val = "";
- break;
- }
- }
- mCurObject = Sim::findObject(val);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurobject_internal(U32 &ip)
- {
- ++ip; // To skip the recurse flag if the object wasn't found
- if (mCurObject)
- {
- SimSet *set = dynamic_cast<SimSet *>(mCurObject);
- if (set)
- {
- StringTableEntry intName = StringTable->insert(STR.getStringValue());
- bool recurse = mCodeBlock->code[ip - 1];
- SimObject *obj = set->findObjectByInternalName(intName, recurse);
- intStack[_UINT + 1] = obj ? obj->getId() : 0;
- _UINT++;
- }
- else
- {
- Con::errorf(ConsoleLogEntry::Script, "%s: Attempt to use -> on non-set %s of class %s.", mCodeBlock->getFileLine(ip - 2), mCurObject->getName(), mCurObject->getClassName());
- intStack[_UINT] = 0;
- }
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurobject_new(U32 &ip)
- {
- mCurObject = mCurrentNewObject;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurfield(U32 &ip)
- {
- // Save the previous field for parsing vector fields.
- mPrevField = mCurField;
- dStrcpy(prevFieldArray, curFieldArray, 256);
- mCurField = CodeToSTE(mCodeBlock->code, ip);
- curFieldArray[0] = 0;
- ip += 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurfield_array(U32 &ip)
- {
- dStrcpy(curFieldArray, STR.getStringValue(), 256);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurfield_type(U32 &ip)
- {
- if (mCurObject)
- mCurObject->setDataFieldType(mCodeBlock->code[ip], mCurField, curFieldArray);
- ip++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurfield_array_var(U32 &ip)
- {
- StringTableEntry var = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // We set the current var name (create it as well in case if it doesn't exist,
- // otherwise we will crash).
- gEvalState.setCurVarNameCreate(var);
- // Then load the var and copy the contents to the current field array
- dStrncpy(curFieldArray, gEvalState.currentVariable->getStringValue(), sizeof(curFieldArray));
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_setcurfield_this(U32 &ip)
- {
- // set the 'this pointer' as the current object.
- mCurObject = mThisObject;
- mPrevField = mCurField;
- dStrcpy(prevFieldArray, curFieldArray, 256);
- mCurField = CodeToSTE(mCodeBlock->code, ip);
- curFieldArray[0] = 0;
- ip += 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadfield_uint(U32 &ip)
- {
- if (mCurObject)
- intStack[_UINT + 1] = U32(dAtoi(mCurObject->getDataField(mCurField, curFieldArray)));
- else
- {
- // The field is not being retrieved from an object. Maybe it's
- // a special accessor?
- char buff[FieldBufferSizeNumeric];
- memset(buff, 0, sizeof(buff));
- getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
- intStack[_UINT + 1] = dAtoi(buff);
- }
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadfield_flt(U32 &ip)
- {
- if (mCurObject)
- floatStack[_FLT + 1] = dAtof(mCurObject->getDataField(mCurField, curFieldArray));
- else
- {
- // The field is not being retrieved from an object. Maybe it's
- // a special accessor?
- char buff[FieldBufferSizeNumeric];
- memset(buff, 0, sizeof(buff));
- getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
- floatStack[_FLT + 1] = dAtof(buff);
- }
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadfield_str(U32 &ip)
- {
- if (mCurObject)
- {
- StringTableEntry val = mCurObject->getDataField(mCurField, curFieldArray);
- STR.setStringValue(val);
- }
- else
- {
- // The field is not being retrieved from an object. Maybe it's
- // a special accessor?
- char buff[FieldBufferSizeString];
- memset(buff, 0, sizeof(buff));
- getFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField, buff);
- STR.setStringValue(buff);
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savefield_uint(U32 &ip)
- {
- STR.setIntValue(intStack[_UINT]);
- if (mCurObject)
- mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
- else
- {
- // The field is not being set on an object. Maybe it's
- // a special accessor?
- setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
- mPrevObject = NULL;
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savefield_flt(U32 &ip)
- {
- STR.setFloatValue(floatStack[_FLT]);
- if (mCurObject)
- mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
- else
- {
- // The field is not being set on an object. Maybe it's
- // a special accessor?
- setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
- mPrevObject = NULL;
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_savefield_str(U32 &ip)
- {
- if (mCurObject)
- mCurObject->setDataField(mCurField, curFieldArray, STR.getStringValue());
- else
- {
- // The field is not being set on an object. Maybe it's
- // a special accessor?
- setFieldComponent(mPrevObject, mPrevField, prevFieldArray, mCurField);
- mPrevObject = NULL;
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_str_to_uint(U32 &ip)
- {
- intStack[_UINT + 1] = STR.getIntValue();
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_str_to_flt(U32 &ip)
- {
- floatStack[_FLT + 1] = STR.getFloatValue();
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_str_to_none(U32 &ip)
- {
- // This exists simply to deal with certain typecast situations.
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_flt_to_uint(U32 &ip)
- {
- intStack[_UINT + 1] = (S64)floatStack[_FLT];
- _FLT--;
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_flt_to_str(U32 &ip)
- {
- STR.setFloatValue(floatStack[_FLT]);
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_flt_to_none(U32 &ip)
- {
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_uint_to_flt(U32 &ip)
- {
- floatStack[_FLT + 1] = (F32)intStack[_UINT];
- _UINT--;
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_uint_to_str(U32 &ip)
- {
- STR.setIntValue(intStack[_UINT]);
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_uint_to_none(U32 &ip)
- {
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_copyvar_to_none(U32 &ip)
- {
- gEvalState.copyVariable = NULL;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadimmed_uint(U32 &ip)
- {
- intStack[_UINT + 1] = mCodeBlock->code[ip++];
- _UINT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadimmed_flt(U32 &ip)
- {
- floatStack[_FLT + 1] = mCurFloatTable[mCodeBlock->code[ip]];
- ip++;
- _FLT++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_tag_to_str(U32 &ip)
- {
- mCodeBlock->code[ip - 1] = OP_LOADIMMED_STR;
- // it's possible the string has already been converted
- if (U8(mCurStringTable[mCodeBlock->code[ip]]) != StringTagPrefixByte)
- {
- U32 id = GameAddTaggedString(mCurStringTable + mCodeBlock->code[ip]);
- dSprintf(mCurStringTable + mCodeBlock->code[ip] + 1, 7, "%d", id);
- *(mCurStringTable + mCodeBlock->code[ip]) = StringTagPrefixByte;
- }
- // Fallthrough
- OPCodeReturn ret = op_loadimmed_str(ip);
- return ret;
- }
- OPCodeReturn CodeInterpreter::op_loadimmed_str(U32 &ip)
- {
- STR.setStringValue(mCurStringTable + mCodeBlock->code[ip++]);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_docblock_str(U32 &ip)
- {
- // If the first word of the doc is '\class' or '@class', then this
- // is a namespace doc block, otherwise it is a function doc block.
- const char* docblock = mCurStringTable + mCodeBlock->code[ip++];
- const char* sansClass = dStrstr(docblock, "@class");
- if (!sansClass)
- sansClass = dStrstr(docblock, "\\class");
- if (sansClass)
- {
- // Don't save the class declaration. Scan past the 'class'
- // keyword and up to the first whitespace.
- sansClass += 7;
- S32 index = 0;
- while ((*sansClass != ' ') && (*sansClass != '\n') && *sansClass && (index < (nsDocLength - 1)))
- {
- mNSDocBlockClass[index++] = *sansClass;
- sansClass++;
- }
- mNSDocBlockClass[index] = '\0';
- mCurNSDocBlock = sansClass + 1;
- }
- else
- mCurFNDocBlock = docblock;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_loadimmed_ident(U32 &ip)
- {
- STR.setStringValue(CodeToSTE(mCodeBlock->code, ip));
- ip += 2;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_callfunc_resolve(U32 &ip)
- {
- // This deals with a function that is potentially living in a namespace.
- StringTableEntry fnNamespace = CodeToSTE(mCodeBlock->code, ip + 2);
- StringTableEntry fnName = CodeToSTE(mCodeBlock->code, ip);
- // Try to look it up.
- mNSEntry = Namespace::find(fnNamespace)->lookup(fnName);
- if (!CInterface::GetCInterface().isMethod(fnNamespace, fnName) && !mNSEntry)
- {
- ip += 5;
- Con::warnf(ConsoleLogEntry::General,
- "%s: Unable to find function %s%s%s",
- mCodeBlock->getFileLine(ip - 7), fnNamespace ? fnNamespace : "",
- fnNamespace ? "::" : "", fnName);
- STR.popFrame();
- CSTK.popFrame();
- return OPCodeReturn::success;
- }
- // Fallthrough to op_callfunc_resolve
- OPCodeReturn ret = op_callfunc(ip);
- return ret;
- }
- OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip)
- {
- // This routingId is set when we query the object as to whether
- // it handles this method. It is set to an enum from the table
- // above indicating whether it handles it on a component it owns
- // or just on the object.
- S32 routingId = 0;
- U32 *code = mCodeBlock->code;
- StringTableEntry fnNamespace = CodeToSTE(mCodeBlock->code, ip + 2);
- StringTableEntry fnName = CodeToSTE(code, ip);
- //if this is called from inside a function, append the ip and codeptr
- if (gEvalState.getStackDepth() > 0)
- {
- gEvalState.getCurrentFrame().code = mCodeBlock;
- gEvalState.getCurrentFrame().ip = ip - 1;
- }
- U32 callType = code[ip + 4];
- ip += 5;
- CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
- const char *componentReturnValue = "";
- Namespace *ns = NULL;
- bool cFunctionRes = false;
- const char* cRetRes = NULL;
- if (callType == FuncCallExprNode::FunctionCall)
- {
- if (!mNSEntry)
- mNSEntry = Namespace::global()->lookup(fnName);
- StringStackWrapper args(mCallArgc, mCallArgv);
- cRetRes = CInterface::CallFunction(fnNamespace, fnName, args.argv + 1, args.argc - 1, &cFunctionRes);
- }
- else if (callType == FuncCallExprNode::MethodCall)
- {
- mSaveObject = gEvalState.thisObject;
- gEvalState.thisObject = Sim::findObject((const char*)mCallArgv[1]);
- if (!gEvalState.thisObject)
- {
- // Go back to the previous saved object.
- gEvalState.thisObject = mSaveObject;
- Con::warnf(ConsoleLogEntry::General, "%s: Unable to find object: '%s' attempting to call function '%s'", mCodeBlock->getFileLine(ip - 4), (const char*)mCallArgv[1], fnName);
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- return OPCodeReturn::success;
- }
- bool handlesMethod = gEvalState.thisObject->handlesConsoleMethod(fnName, &routingId);
- if (handlesMethod && routingId == MethodOnComponent)
- {
- ICallMethod *pComponent = dynamic_cast<ICallMethod *>(gEvalState.thisObject);
- if (pComponent)
- componentReturnValue = pComponent->callMethodArgList(mCallArgc, mCallArgv, false);
- }
- ns = gEvalState.thisObject->getNamespace();
- if (ns)
- mNSEntry = ns->lookup(fnName);
- else
- mNSEntry = NULL;
- StringStackWrapper args(mCallArgc, mCallArgv);
- cRetRes = CInterface::CallMethod(gEvalState.thisObject, fnName, args.argv + 2, args.argc - 2, &cFunctionRes);
- }
- else // it's a ParentCall
- {
- if (mExec.thisNamespace)
- {
- ns = mExec.thisNamespace->mParent;
- if (ns)
- mNSEntry = ns->lookup(fnName);
- else
- mNSEntry = NULL;
- }
- else
- {
- ns = NULL;
- mNSEntry = NULL;
- }
- }
- Namespace::Entry::CallbackUnion * nsCb = NULL;
- const char * nsUsage = NULL;
- if (mNSEntry)
- {
- nsCb = &mNSEntry->cb;
- nsUsage = mNSEntry->mUsage;
- routingId = 0;
- }
- if (!cFunctionRes && (!mNSEntry || mExec.noCalls))
- {
- if (!mExec.noCalls && !(routingId == MethodOnComponent))
- {
- if (callType == FuncCallExprNode::MethodCall)
- {
- if (gEvalState.thisObject != NULL)
- {
- // Try to use the name instead of the id
- StringTableEntry name = gEvalState.thisObject->getName() ? gEvalState.thisObject->getName() : gEvalState.thisObject->getIdString();
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown method %s.%s Namespace List: %s", mCodeBlock->getFileLine(ip - 6), name, fnName, Con::getNamespaceList(ns));
- }
- else
- {
- // NULL.
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown method NULL.%s", mCodeBlock->getFileLine(ip - 6), fnName);
- }
- }
- else if (callType == FuncCallExprNode::ParentCall)
- {
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown parent call %s.", mCodeBlock->getFileLine(ip - 6), fnName);
- }
- else
- {
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown function %s.", mCodeBlock->getFileLine(ip - 6), fnName);
- }
- }
- STR.popFrame();
- CSTK.popFrame();
- if (routingId == MethodOnComponent)
- STR.setStringValue(componentReturnValue);
- else
- STR.setStringValue("");
- return OPCodeReturn::success;
- }
- // ConsoleFunctionType is for any function defined by script.
- // Any 'callback' type is an engine function that is exposed to script.
- if (cFunctionRes || mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
- {
- ConsoleValue retVal;
- ConsoleValueRef ret;
- if (cFunctionRes)
- {
- retVal.init();
- ret.value = &retVal;
- retVal.setStackStringValue(cRetRes);
- }
- else if (mNSEntry->mFunctionOffset)
- {
- ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
- }
- STR.popFrame();
- // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (U32)((S32)ret);
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = (F32)ret;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- {
- STR.setStringValue(ret.getStringValue());
- ip++;
- }
- else
- STR.setStringValue((const char*)ret);
- // This will clear everything including returnValue
- CSTK.popFrame();
- //STR.clearFunctionOffset();
- }
- else
- {
- const char* nsName = ns ? ns->mName : "";
- #ifndef TORQUE_DEBUG
- // [tom, 12/13/2006] This stops tools functions from working in the console,
- // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
- if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
- {
- Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
- }
- else
- #endif
- if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
- {
- Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
- mCodeBlock->getFileLine(ip - 6), nsName, fnName,
- mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
- Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
- STR.popFrame();
- CSTK.popFrame();
- }
- else
- {
- switch (mNSEntry->mType)
- {
- case Namespace::Entry::StringCallbackType:
- {
- const char *ret = mNSEntry->cb.mStringCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (ret != STR.getStringValue())
- STR.setStringValue(ret);
- //else
- // sSTR.setLen(dStrlen(ret));
- break;
- }
- case Namespace::Entry::IntCallbackType:
- {
- S32 result = mNSEntry->cb.mIntCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- case Namespace::Entry::FloatCallbackType:
- {
- F64 result = mNSEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (S64)result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setFloatValue(result);
- break;
- }
- case Namespace::Entry::VoidCallbackType:
- mNSEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
- Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- break;
- case Namespace::Entry::BoolCallbackType:
- {
- bool result = mNSEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- }
- }
- }
- if (callType == FuncCallExprNode::MethodCall)
- gEvalState.thisObject = mSaveObject;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_callfunc_pointer(U32 &ip)
- {
- // get function name. This is the 'function pointer'.
- StringTableEntry fnName = StringTable->insert(STR.getStringValue());
- U32 *code = mCodeBlock->code;
- mNSEntry = Namespace::global()->lookup(fnName);
- //if this is called from inside a function, append the ip and codeptr
- if (gEvalState.getStackDepth() > 0)
- {
- gEvalState.getCurrentFrame().code = mCodeBlock;
- gEvalState.getCurrentFrame().ip = ip - 1;
- }
- CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
- if (!mNSEntry || mExec.noCalls)
- {
- if (!mExec.noCalls)
- {
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown function %s.", mCodeBlock->getFileLine(ip - 6), fnName);
- }
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- return OPCodeReturn::success;
- }
- // ConsoleFunctionType is for any function defined by script.
- // Any 'callback' type is an engine function that is exposed to script.
- if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
- {
- ConsoleValueRef ret;
- if (mNSEntry->mFunctionOffset)
- ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
- STR.popFrame();
- // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (U32)((S32)ret);
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = (F32)ret;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- {
- STR.setStringValue(ret.getStringValue());
- ip++;
- }
- else
- STR.setStringValue((const char*)ret);
- // This will clear everything including returnValue
- CSTK.popFrame();
- //STR.clearFunctionOffset();
- }
- else
- {
- const char* nsName = "";
- #ifndef TORQUE_DEBUG
- // [tom, 12/13/2006] This stops tools functions from working in the console,
- // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
- if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
- {
- Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
- }
- else
- #endif
- if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
- {
- Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
- mCodeBlock->getFileLine(ip - 6), nsName, fnName,
- mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
- Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
- STR.popFrame();
- CSTK.popFrame();
- }
- else
- {
- switch (mNSEntry->mType)
- {
- case Namespace::Entry::StringCallbackType:
- {
- const char *ret = mNSEntry->cb.mStringCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (ret != STR.getStringValue())
- STR.setStringValue(ret);
- //else
- // sSTR.setLen(dStrlen(ret));
- break;
- }
- case Namespace::Entry::IntCallbackType:
- {
- S32 result = mNSEntry->cb.mIntCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- case Namespace::Entry::FloatCallbackType:
- {
- F64 result = mNSEntry->cb.mFloatCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (S64)result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setFloatValue(result);
- break;
- }
- case Namespace::Entry::VoidCallbackType:
- mNSEntry->cb.mVoidCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
- Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- break;
- case Namespace::Entry::BoolCallbackType:
- {
- bool result = mNSEntry->cb.mBoolCallbackFunc(gEvalState.thisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- }
- }
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_callfunc_this(U32 &ip)
- {
- U32 *code = mCodeBlock->code;
- StringTableEntry fnName = CodeToSTE(code, ip);
- //if this is called from inside a function, append the ip and codeptr
- if (gEvalState.getStackDepth() > 0)
- {
- gEvalState.getCurrentFrame().code = mCodeBlock;
- gEvalState.getCurrentFrame().ip = ip - 1;
- }
- ip += 2;
- CSTK.getArgcArgv(fnName, &mCallArgc, &mCallArgv);
- Namespace *ns = mThisObject ? mThisObject->getNamespace() : NULL;
- if (ns)
- mNSEntry = ns->lookup(fnName);
- else
- mNSEntry = NULL;
- if (!mNSEntry || mExec.noCalls)
- {
- if (!mExec.noCalls)
- {
- if (mThisObject)
- {
- // Try to use the name instead of the id
- StringTableEntry name = mThisObject->getName() ? mThisObject->getName() : mThisObject->getIdString();
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown method %s.%s Namespace List: %s", mCodeBlock->getFileLine(ip - 6), name, fnName, Con::getNamespaceList(ns));
- }
- else
- {
- // At least let the scripter know that they access the object.
- Con::warnf(ConsoleLogEntry::General, "%s: Unknown method NULL.%s", mCodeBlock->getFileLine(ip - 6), fnName);
- }
- }
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- return OPCodeReturn::success;
- }
- // ConsoleFunctionType is for any function defined by script.
- // Any 'callback' type is an engine function that is exposed to script.
- if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType)
- {
- ConsoleValueRef ret;
- if (mNSEntry->mFunctionOffset)
- ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage);
- STR.popFrame();
- // Functions are assumed to return strings, so look ahead to see if we can skip the conversion
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (U32)((S32)ret);
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = (F32)ret;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- {
- STR.setStringValue(ret.getStringValue());
- ip++;
- }
- else
- STR.setStringValue((const char*)ret);
- // This will clear everything including returnValue
- CSTK.popFrame();
- //STR.clearFunctionOffset();
- }
- else
- {
- const char* nsName = ns ? ns->mName : "";
- #ifndef TORQUE_DEBUG
- // [tom, 12/13/2006] This stops tools functions from working in the console,
- // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
- if (mNSEntry->mToolOnly && !Con::isCurrentScriptToolScript())
- {
- Con::errorf(ConsoleLogEntry::Script, "%s: %s::%s - attempting to call tools only function from outside of tools.", mCodeBlock->getFileLine(ip - 6), nsName, fnName);
- }
- else
- #endif
- if ((mNSEntry->mMinArgs && S32(mCallArgc) < mNSEntry->mMinArgs) || (mNSEntry->mMaxArgs && S32(mCallArgc) > mNSEntry->mMaxArgs))
- {
- Con::warnf(ConsoleLogEntry::Script, "%s: %s::%s - wrong number of arguments (got %i, expected min %i and max %i).",
- mCodeBlock->getFileLine(ip - 6), nsName, fnName,
- mCallArgc, mNSEntry->mMinArgs, mNSEntry->mMaxArgs);
- Con::warnf(ConsoleLogEntry::Script, "%s: usage: %s", mCodeBlock->getFileLine(ip - 6), mNSEntry->mUsage);
- STR.popFrame();
- CSTK.popFrame();
- }
- else
- {
- switch (mNSEntry->mType)
- {
- case Namespace::Entry::StringCallbackType:
- {
- const char *ret = mNSEntry->cb.mStringCallbackFunc(mThisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (ret != STR.getStringValue())
- STR.setStringValue(ret);
- //else
- // sSTR.setLen(dStrlen(ret));
- break;
- }
- case Namespace::Entry::IntCallbackType:
- {
- S32 result = mNSEntry->cb.mIntCallbackFunc(mThisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- case Namespace::Entry::FloatCallbackType:
- {
- F64 result = mNSEntry->cb.mFloatCallbackFunc(mThisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = (S64)result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setFloatValue(result);
- break;
- }
- case Namespace::Entry::VoidCallbackType:
- mNSEntry->cb.mVoidCallbackFunc(mThisObject, mCallArgc, mCallArgv);
- if (code[ip] != OP_STR_TO_NONE && Con::getBoolVariable("$Con::warnVoidAssignment", true))
- Con::warnf(ConsoleLogEntry::General, "%s: Call to %s in %s uses result of void function call.", mCodeBlock->getFileLine(ip - 6), fnName, mExec.functionName);
- STR.popFrame();
- CSTK.popFrame();
- STR.setStringValue("");
- break;
- case Namespace::Entry::BoolCallbackType:
- {
- bool result = mNSEntry->cb.mBoolCallbackFunc(mThisObject, mCallArgc, mCallArgv);
- STR.popFrame();
- CSTK.popFrame();
- if (code[ip] == OP_STR_TO_UINT)
- {
- ip++;
- intStack[++_UINT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_FLT)
- {
- ip++;
- floatStack[++_FLT] = result;
- break;
- }
- else if (code[ip] == OP_STR_TO_NONE)
- ip++;
- else
- STR.setIntValue(result);
- break;
- }
- }
- }
- }
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_advance_str(U32 &ip)
- {
- STR.advance();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_advance_str_appendchar(U32 &ip)
- {
- STR.advanceChar(mCodeBlock->code[ip++]);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_advance_str_comma(U32 &ip)
- {
- STR.advanceChar('_');
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_advance_str_nul(U32 &ip)
- {
- STR.advanceChar(0);
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_rewind_str(U32 &ip)
- {
- STR.rewind();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_terminate_rewind_str(U32 &ip)
- {
- STR.rewindTerminate();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_compare_str(U32 &ip)
- {
- intStack[++_UINT] = STR.compare();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push(U32 &ip)
- {
- STR.push();
- CSTK.pushStringStackPtr(STR.getPreviousStringValuePtr());
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push_uint(U32 &ip)
- {
- CSTK.pushUINT(intStack[_UINT]);
- _UINT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push_flt(U32 &ip)
- {
- CSTK.pushFLT(floatStack[_FLT]);
- _FLT--;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push_var(U32 &ip)
- {
- if (gEvalState.currentVariable)
- CSTK.pushValue(gEvalState.currentVariable->value);
- else
- CSTK.pushString("");
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push_this(U32 &ip)
- {
- StringTableEntry varName = CodeToSTE(mCodeBlock->code, ip);
- ip += 2;
- // shorthand OP_SETCURVAR
- // If a variable is set, then these must be NULL. It is necessary
- // to set this here so that the vector parser can appropriately
- // identify whether it's dealing with a vector.
- mPrevField = NULL;
- mPrevObject = NULL;
- mCurObject = NULL;
- gEvalState.setCurVarName(varName);
- // In order to let docblocks work properly with variables, we have
- // clear the current docblock when we do an assign. This way it
- // won't inappropriately carry forward to following function decls.
- mCurFNDocBlock = NULL;
- mCurNSDocBlock = NULL;
- // shorthand OP_LOADVAR_STR (since objs can be by name we can't assume uint)
- STR.setStringValue(gEvalState.getStringVariable());
- // shorthand OP_PUSH
- STR.push();
- CSTK.pushStringStackPtr(STR.getPreviousStringValuePtr());
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_push_frame(U32 &ip)
- {
- STR.pushFrame();
- CSTK.pushFrame();
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_assert(U32 &ip)
- {
- if (!intStack[_UINT--])
- {
- const char *message = mCurStringTable + mCodeBlock->code[ip];
- U32 breakLine, inst;
- mCodeBlock->findBreakLine(ip - 1, breakLine, inst);
- if (PlatformAssert::processAssert(PlatformAssert::Fatal,
- mCodeBlock->name ? mCodeBlock->name : "eval",
- breakLine,
- message))
- {
- if (TelDebugger && TelDebugger->isConnected() && breakLine > 0)
- {
- TelDebugger->breakProcess();
- }
- else
- Platform::debugBreak();
- }
- }
- ip++;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_break(U32 &ip)
- {
- //append the ip and codeptr before managing the breakpoint!
- AssertFatal(gEvalState.getStackDepth() > 0, "Empty eval stack on break!");
- gEvalState.getCurrentFrame().code = mCodeBlock;
- gEvalState.getCurrentFrame().ip = ip - 1;
- U32 breakLine;
- mCodeBlock->findBreakLine(ip - 1, breakLine, mCurrentInstruction);
- if (!breakLine)
- return OPCodeReturn::breakContinue;
- TelDebugger->executionStopped(mCodeBlock, breakLine);
- return OPCodeReturn::breakContinue;
- }
- OPCodeReturn CodeInterpreter::op_iter_begin_str(U32 &ip)
- {
- iterStack[_ITER].mIsStringIter = true;
- // Emulate fallthrough:
- OPCodeReturn fallthrough = op_iter_begin(ip);
- return fallthrough;
- }
- OPCodeReturn CodeInterpreter::op_iter_begin(U32 &ip)
- {
- StringTableEntry varName = CodeToSTE(mCodeBlock->code, ip);
- U32 failIp = mCodeBlock->code[ip + 2];
- IterStackRecord& iter = iterStack[_ITER];
- if (varName[0] == '$')
- iter.mVariable = gEvalState.globalVars.add(varName);
- else
- iter.mVariable = gEvalState.getCurrentFrame().add(varName);
- if (iter.mIsStringIter)
- {
- iter.mData.mStr.mString = STR.getStringValuePtr();
- iter.mData.mStr.mIndex = 0;
- }
- else
- {
- // Look up the object.
- SimSet* set;
- if (!Sim::findObject(STR.getStringValue(), set))
- {
- Con::errorf(ConsoleLogEntry::General, "No SimSet object '%s'", STR.getStringValue());
- Con::errorf(ConsoleLogEntry::General, "Did you mean to use 'foreach$' instead of 'foreach'?");
- ip = failIp;
- return OPCodeReturn::success;
- }
- // Set up.
- iter.mData.mObj.mSet = set;
- iter.mData.mObj.mIndex = 0;
- }
- _ITER++;
- mIterDepth++;
- STR.push();
- ip += 3;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_iter(U32 &ip)
- {
- U32 breakIp = mCodeBlock->code[ip];
- IterStackRecord& iter = iterStack[_ITER - 1];
- if (iter.mIsStringIter)
- {
- const char* str = StringStackPtrRef(iter.mData.mStr.mString).getPtr(&STR);
- U32 startIndex = iter.mData.mStr.mIndex;
- U32 endIndex = startIndex;
- // Break if at end.
- if (!str[startIndex])
- {
- ip = breakIp;
- return OPCodeReturn::success; // continue in old interpreter
- }
- // Find right end of current component.
- if (!dIsspace(str[endIndex]))
- do ++endIndex;
- while (str[endIndex] && !dIsspace(str[endIndex]));
- // Extract component.
- if (endIndex != startIndex)
- {
- char savedChar = str[endIndex];
- const_cast< char* >(str)[endIndex] = '\0'; // We are on the string stack so this is okay.
- iter.mVariable->setStringValue(&str[startIndex]);
- const_cast< char* >(str)[endIndex] = savedChar;
- }
- else
- iter.mVariable->setStringValue("");
- // Skip separator.
- if (str[endIndex] != '\0')
- ++endIndex;
- iter.mData.mStr.mIndex = endIndex;
- }
- else
- {
- U32 index = iter.mData.mObj.mIndex;
- SimSet* set = iter.mData.mObj.mSet;
- if (index >= set->size())
- {
- ip = breakIp;
- return OPCodeReturn::success; // continue in old interpreter
- }
- iter.mVariable->setIntValue(set->at(index)->getId());
- iter.mData.mObj.mIndex = index + 1;
- }
- ++ip;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_iter_end(U32 &ip)
- {
- --_ITER;
- --mIterDepth;
- STR.rewind();
- iterStack[_ITER].mIsStringIter = false;
- return OPCodeReturn::success;
- }
- OPCodeReturn CodeInterpreter::op_invalid(U32 &ip)
- {
- // Invalid does nothing.
- return OPCodeReturn::exitCode;
- }
|