codeInterpreter.cpp 92 KB

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