codeInterpreter.cpp 92 KB

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