gdscript_vm.cpp 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989
  1. /**************************************************************************/
  2. /* gdscript_vm.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript.h"
  31. #include "gdscript_function.h"
  32. #include "gdscript_lambda_callable.h"
  33. #include "core/os/os.h"
  34. #ifdef DEBUG_ENABLED
  35. static bool _profile_count_as_native(const Object *p_base_obj, const StringName &p_methodname) {
  36. if (!p_base_obj) {
  37. return false;
  38. }
  39. StringName cname = p_base_obj->get_class_name();
  40. if ((p_methodname == "new" && cname == "GDScript") || p_methodname == "call") {
  41. return false;
  42. }
  43. return ClassDB::class_exists(cname) && ClassDB::has_method(cname, p_methodname, false);
  44. }
  45. static String _get_element_type(Variant::Type builtin_type, const StringName &native_type, const Ref<Script> &script_type) {
  46. if (script_type.is_valid() && script_type->is_valid()) {
  47. return GDScript::debug_get_script_name(script_type);
  48. } else if (native_type != StringName()) {
  49. return native_type.operator String();
  50. } else {
  51. return Variant::get_type_name(builtin_type);
  52. }
  53. }
  54. static String _get_var_type(const Variant *p_var) {
  55. String basestr;
  56. if (p_var->get_type() == Variant::OBJECT) {
  57. bool was_freed;
  58. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  59. if (!bobj) {
  60. if (was_freed) {
  61. basestr = "previously freed";
  62. } else {
  63. basestr = "null instance";
  64. }
  65. } else {
  66. if (bobj->is_class_ptr(GDScriptNativeClass::get_class_ptr_static())) {
  67. basestr = Object::cast_to<GDScriptNativeClass>(bobj)->get_name();
  68. } else {
  69. basestr = bobj->get_class();
  70. if (bobj->get_script_instance()) {
  71. basestr += " (" + GDScript::debug_get_script_name(bobj->get_script_instance()->get_script()) + ")";
  72. }
  73. }
  74. }
  75. } else {
  76. if (p_var->get_type() == Variant::ARRAY) {
  77. basestr = "Array";
  78. const Array *p_array = VariantInternal::get_array(p_var);
  79. if (p_array->is_typed()) {
  80. basestr += "[" + _get_element_type((Variant::Type)p_array->get_typed_builtin(), p_array->get_typed_class_name(), p_array->get_typed_script()) + "]";
  81. }
  82. } else if (p_var->get_type() == Variant::DICTIONARY) {
  83. basestr = "Dictionary";
  84. const Dictionary *p_dictionary = VariantInternal::get_dictionary(p_var);
  85. if (p_dictionary->is_typed()) {
  86. basestr += "[" + _get_element_type((Variant::Type)p_dictionary->get_typed_key_builtin(), p_dictionary->get_typed_key_class_name(), p_dictionary->get_typed_key_script()) +
  87. ", " + _get_element_type((Variant::Type)p_dictionary->get_typed_value_builtin(), p_dictionary->get_typed_value_class_name(), p_dictionary->get_typed_value_script()) + "]";
  88. }
  89. } else {
  90. basestr = Variant::get_type_name(p_var->get_type());
  91. }
  92. }
  93. return basestr;
  94. }
  95. void GDScriptFunction::_profile_native_call(uint64_t p_t_taken, const String &p_func_name, const String &p_instance_class_name) {
  96. HashMap<String, Profile::NativeProfile>::Iterator inner_prof = profile.native_calls.find(p_func_name);
  97. if (inner_prof) {
  98. inner_prof->value.call_count += 1;
  99. } else {
  100. String sig = vformat("%s::0::%s%s%s", get_script()->get_script_path(), p_instance_class_name, p_instance_class_name.is_empty() ? "" : ".", p_func_name);
  101. inner_prof = profile.native_calls.insert(p_func_name, Profile::NativeProfile{ 1, 0, sig });
  102. }
  103. inner_prof->value.total_time += p_t_taken;
  104. }
  105. #endif // DEBUG_ENABLED
  106. Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataType &p_data_type) {
  107. if (p_data_type.kind == GDScriptDataType::BUILTIN) {
  108. if (p_data_type.builtin_type == Variant::ARRAY) {
  109. Array array;
  110. // Typed array.
  111. if (p_data_type.has_container_element_type(0)) {
  112. const GDScriptDataType &element_type = p_data_type.get_container_element_type(0);
  113. array.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type);
  114. }
  115. return array;
  116. } else if (p_data_type.builtin_type == Variant::DICTIONARY) {
  117. Dictionary dict;
  118. // Typed dictionary.
  119. if (p_data_type.has_container_element_types()) {
  120. const GDScriptDataType &key_type = p_data_type.get_container_element_type_or_variant(0);
  121. const GDScriptDataType &value_type = p_data_type.get_container_element_type_or_variant(1);
  122. dict.set_typed(key_type.builtin_type, key_type.native_type, key_type.script_type, value_type.builtin_type, value_type.native_type, value_type.script_type);
  123. }
  124. return dict;
  125. } else {
  126. Callable::CallError ce;
  127. Variant variant;
  128. Variant::construct(p_data_type.builtin_type, variant, nullptr, 0, ce);
  129. ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, Variant());
  130. return variant;
  131. }
  132. }
  133. return Variant();
  134. }
  135. String GDScriptFunction::_get_call_error(const String &p_where, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const {
  136. switch (p_err.error) {
  137. case Callable::CallError::CALL_OK:
  138. return String();
  139. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  140. if (p_ret.get_type() == Variant::STRING && !p_ret.operator String().is_empty()) {
  141. return "Invalid call " + p_where + ": " + p_ret.operator String();
  142. }
  143. return "Invalid call. Nonexistent " + p_where + ".";
  144. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  145. ERR_FAIL_INDEX_V_MSG(p_err.argument, p_argcount, "Bug: Invalid call error argument index.", "Bug: Invalid call error argument index.");
  146. ERR_FAIL_NULL_V_MSG(p_argptrs[p_err.argument], "Bug: Argument is null pointer.", "Bug: Argument is null pointer.");
  147. // Handle the Object to Object case separately as we don't have further class details.
  148. #ifdef DEBUG_ENABLED
  149. if (p_err.expected == Variant::OBJECT && p_argptrs[p_err.argument]->get_type() == p_err.expected) {
  150. return "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(p_err.argument + 1) + " (" + _get_var_type(p_argptrs[p_err.argument]) + ") is not a subclass of the expected argument class.";
  151. }
  152. if (p_err.expected == Variant::ARRAY && p_argptrs[p_err.argument]->get_type() == p_err.expected) {
  153. return "Invalid type in " + p_where + ". The array of argument " + itos(p_err.argument + 1) + " (" + _get_var_type(p_argptrs[p_err.argument]) + ") does not have the same element type as the expected typed array argument.";
  154. }
  155. if (p_err.expected == Variant::DICTIONARY && p_argptrs[p_err.argument]->get_type() == p_err.expected) {
  156. return "Invalid type in " + p_where + ". The dictionary of argument " + itos(p_err.argument + 1) + " (" + _get_var_type(p_argptrs[p_err.argument]) + ") does not have the same element type as the expected typed dictionary argument.";
  157. }
  158. #endif // DEBUG_ENABLED
  159. return "Invalid type in " + p_where + ". Cannot convert argument " + itos(p_err.argument + 1) + " from " + Variant::get_type_name(p_argptrs[p_err.argument]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
  160. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  161. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  162. return "Invalid call to " + p_where + ". Expected " + itos(p_err.expected) + " arguments.";
  163. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  164. return "Attempt to call " + p_where + " on a null instance.";
  165. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  166. return "Attempt to call " + p_where + " on a const instance.";
  167. }
  168. return "Bug: Invalid call error code " + itos(p_err.error) + ".";
  169. }
  170. String GDScriptFunction::_get_callable_call_error(const String &p_where, const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const {
  171. Vector<Variant> binds;
  172. p_callable.get_bound_arguments_ref(binds);
  173. int args_unbound = p_callable.get_unbound_arguments_count();
  174. if (p_argcount - args_unbound < 0) {
  175. return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
  176. } else {
  177. Vector<const Variant *> argptrs;
  178. argptrs.resize(p_argcount - args_unbound + binds.size());
  179. for (int i = 0; i < p_argcount - args_unbound; i++) {
  180. argptrs.write[i] = p_argptrs[i];
  181. }
  182. for (int i = 0; i < binds.size(); i++) {
  183. argptrs.write[i + p_argcount - args_unbound] = &binds[i];
  184. }
  185. return _get_call_error(p_where, (const Variant **)argptrs.ptr(), argptrs.size(), p_ret, p_err);
  186. }
  187. }
  188. void (*type_init_function_table[])(Variant *) = {
  189. nullptr, // NIL (shouldn't be called).
  190. &VariantInitializer<bool>::init, // BOOL.
  191. &VariantInitializer<int64_t>::init, // INT.
  192. &VariantInitializer<double>::init, // FLOAT.
  193. &VariantInitializer<String>::init, // STRING.
  194. &VariantInitializer<Vector2>::init, // VECTOR2.
  195. &VariantInitializer<Vector2i>::init, // VECTOR2I.
  196. &VariantInitializer<Rect2>::init, // RECT2.
  197. &VariantInitializer<Rect2i>::init, // RECT2I.
  198. &VariantInitializer<Vector3>::init, // VECTOR3.
  199. &VariantInitializer<Vector3i>::init, // VECTOR3I.
  200. &VariantInitializer<Transform2D>::init, // TRANSFORM2D.
  201. &VariantInitializer<Vector4>::init, // VECTOR4.
  202. &VariantInitializer<Vector4i>::init, // VECTOR4I.
  203. &VariantInitializer<Plane>::init, // PLANE.
  204. &VariantInitializer<Quaternion>::init, // QUATERNION.
  205. &VariantInitializer<AABB>::init, // AABB.
  206. &VariantInitializer<Basis>::init, // BASIS.
  207. &VariantInitializer<Transform3D>::init, // TRANSFORM3D.
  208. &VariantInitializer<Projection>::init, // PROJECTION.
  209. &VariantInitializer<Color>::init, // COLOR.
  210. &VariantInitializer<StringName>::init, // STRING_NAME.
  211. &VariantInitializer<NodePath>::init, // NODE_PATH.
  212. &VariantInitializer<RID>::init, // RID.
  213. &VariantInitializer<Object *>::init, // OBJECT.
  214. &VariantInitializer<Callable>::init, // CALLABLE.
  215. &VariantInitializer<Signal>::init, // SIGNAL.
  216. &VariantInitializer<Dictionary>::init, // DICTIONARY.
  217. &VariantInitializer<Array>::init, // ARRAY.
  218. &VariantInitializer<PackedByteArray>::init, // PACKED_BYTE_ARRAY.
  219. &VariantInitializer<PackedInt32Array>::init, // PACKED_INT32_ARRAY.
  220. &VariantInitializer<PackedInt64Array>::init, // PACKED_INT64_ARRAY.
  221. &VariantInitializer<PackedFloat32Array>::init, // PACKED_FLOAT32_ARRAY.
  222. &VariantInitializer<PackedFloat64Array>::init, // PACKED_FLOAT64_ARRAY.
  223. &VariantInitializer<PackedStringArray>::init, // PACKED_STRING_ARRAY.
  224. &VariantInitializer<PackedVector2Array>::init, // PACKED_VECTOR2_ARRAY.
  225. &VariantInitializer<PackedVector3Array>::init, // PACKED_VECTOR3_ARRAY.
  226. &VariantInitializer<PackedColorArray>::init, // PACKED_COLOR_ARRAY.
  227. &VariantInitializer<PackedVector4Array>::init, // PACKED_VECTOR4_ARRAY.
  228. };
  229. #if defined(__GNUC__) || defined(__clang__)
  230. #define OPCODES_TABLE \
  231. static const void *switch_table_ops[] = { \
  232. &&OPCODE_OPERATOR, \
  233. &&OPCODE_OPERATOR_VALIDATED, \
  234. &&OPCODE_TYPE_TEST_BUILTIN, \
  235. &&OPCODE_TYPE_TEST_ARRAY, \
  236. &&OPCODE_TYPE_TEST_DICTIONARY, \
  237. &&OPCODE_TYPE_TEST_NATIVE, \
  238. &&OPCODE_TYPE_TEST_SCRIPT, \
  239. &&OPCODE_SET_KEYED, \
  240. &&OPCODE_SET_KEYED_VALIDATED, \
  241. &&OPCODE_SET_INDEXED_VALIDATED, \
  242. &&OPCODE_GET_KEYED, \
  243. &&OPCODE_GET_KEYED_VALIDATED, \
  244. &&OPCODE_GET_INDEXED_VALIDATED, \
  245. &&OPCODE_SET_NAMED, \
  246. &&OPCODE_SET_NAMED_VALIDATED, \
  247. &&OPCODE_GET_NAMED, \
  248. &&OPCODE_GET_NAMED_VALIDATED, \
  249. &&OPCODE_SET_MEMBER, \
  250. &&OPCODE_GET_MEMBER, \
  251. &&OPCODE_SET_STATIC_VARIABLE, \
  252. &&OPCODE_GET_STATIC_VARIABLE, \
  253. &&OPCODE_ASSIGN, \
  254. &&OPCODE_ASSIGN_NULL, \
  255. &&OPCODE_ASSIGN_TRUE, \
  256. &&OPCODE_ASSIGN_FALSE, \
  257. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  258. &&OPCODE_ASSIGN_TYPED_ARRAY, \
  259. &&OPCODE_ASSIGN_TYPED_DICTIONARY, \
  260. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  261. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  262. &&OPCODE_CAST_TO_BUILTIN, \
  263. &&OPCODE_CAST_TO_NATIVE, \
  264. &&OPCODE_CAST_TO_SCRIPT, \
  265. &&OPCODE_CONSTRUCT, \
  266. &&OPCODE_CONSTRUCT_VALIDATED, \
  267. &&OPCODE_CONSTRUCT_ARRAY, \
  268. &&OPCODE_CONSTRUCT_TYPED_ARRAY, \
  269. &&OPCODE_CONSTRUCT_DICTIONARY, \
  270. &&OPCODE_CONSTRUCT_TYPED_DICTIONARY, \
  271. &&OPCODE_CALL, \
  272. &&OPCODE_CALL_RETURN, \
  273. &&OPCODE_CALL_ASYNC, \
  274. &&OPCODE_CALL_UTILITY, \
  275. &&OPCODE_CALL_UTILITY_VALIDATED, \
  276. &&OPCODE_CALL_GDSCRIPT_UTILITY, \
  277. &&OPCODE_CALL_BUILTIN_TYPE_VALIDATED, \
  278. &&OPCODE_CALL_SELF_BASE, \
  279. &&OPCODE_CALL_METHOD_BIND, \
  280. &&OPCODE_CALL_METHOD_BIND_RET, \
  281. &&OPCODE_CALL_BUILTIN_STATIC, \
  282. &&OPCODE_CALL_NATIVE_STATIC, \
  283. &&OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN, \
  284. &&OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN, \
  285. &&OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN, \
  286. &&OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN, \
  287. &&OPCODE_AWAIT, \
  288. &&OPCODE_AWAIT_RESUME, \
  289. &&OPCODE_CREATE_LAMBDA, \
  290. &&OPCODE_CREATE_SELF_LAMBDA, \
  291. &&OPCODE_JUMP, \
  292. &&OPCODE_JUMP_IF, \
  293. &&OPCODE_JUMP_IF_NOT, \
  294. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  295. &&OPCODE_JUMP_IF_SHARED, \
  296. &&OPCODE_RETURN, \
  297. &&OPCODE_RETURN_TYPED_BUILTIN, \
  298. &&OPCODE_RETURN_TYPED_ARRAY, \
  299. &&OPCODE_RETURN_TYPED_DICTIONARY, \
  300. &&OPCODE_RETURN_TYPED_NATIVE, \
  301. &&OPCODE_RETURN_TYPED_SCRIPT, \
  302. &&OPCODE_ITERATE_BEGIN, \
  303. &&OPCODE_ITERATE_BEGIN_INT, \
  304. &&OPCODE_ITERATE_BEGIN_FLOAT, \
  305. &&OPCODE_ITERATE_BEGIN_VECTOR2, \
  306. &&OPCODE_ITERATE_BEGIN_VECTOR2I, \
  307. &&OPCODE_ITERATE_BEGIN_VECTOR3, \
  308. &&OPCODE_ITERATE_BEGIN_VECTOR3I, \
  309. &&OPCODE_ITERATE_BEGIN_STRING, \
  310. &&OPCODE_ITERATE_BEGIN_DICTIONARY, \
  311. &&OPCODE_ITERATE_BEGIN_ARRAY, \
  312. &&OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY, \
  313. &&OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY, \
  314. &&OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY, \
  315. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY, \
  316. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY, \
  317. &&OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY, \
  318. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY, \
  319. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY, \
  320. &&OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY, \
  321. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY, \
  322. &&OPCODE_ITERATE_BEGIN_OBJECT, \
  323. &&OPCODE_ITERATE_BEGIN_RANGE, \
  324. &&OPCODE_ITERATE, \
  325. &&OPCODE_ITERATE_INT, \
  326. &&OPCODE_ITERATE_FLOAT, \
  327. &&OPCODE_ITERATE_VECTOR2, \
  328. &&OPCODE_ITERATE_VECTOR2I, \
  329. &&OPCODE_ITERATE_VECTOR3, \
  330. &&OPCODE_ITERATE_VECTOR3I, \
  331. &&OPCODE_ITERATE_STRING, \
  332. &&OPCODE_ITERATE_DICTIONARY, \
  333. &&OPCODE_ITERATE_ARRAY, \
  334. &&OPCODE_ITERATE_PACKED_BYTE_ARRAY, \
  335. &&OPCODE_ITERATE_PACKED_INT32_ARRAY, \
  336. &&OPCODE_ITERATE_PACKED_INT64_ARRAY, \
  337. &&OPCODE_ITERATE_PACKED_FLOAT32_ARRAY, \
  338. &&OPCODE_ITERATE_PACKED_FLOAT64_ARRAY, \
  339. &&OPCODE_ITERATE_PACKED_STRING_ARRAY, \
  340. &&OPCODE_ITERATE_PACKED_VECTOR2_ARRAY, \
  341. &&OPCODE_ITERATE_PACKED_VECTOR3_ARRAY, \
  342. &&OPCODE_ITERATE_PACKED_COLOR_ARRAY, \
  343. &&OPCODE_ITERATE_PACKED_VECTOR4_ARRAY, \
  344. &&OPCODE_ITERATE_OBJECT, \
  345. &&OPCODE_ITERATE_RANGE, \
  346. &&OPCODE_STORE_GLOBAL, \
  347. &&OPCODE_STORE_NAMED_GLOBAL, \
  348. &&OPCODE_TYPE_ADJUST_BOOL, \
  349. &&OPCODE_TYPE_ADJUST_INT, \
  350. &&OPCODE_TYPE_ADJUST_FLOAT, \
  351. &&OPCODE_TYPE_ADJUST_STRING, \
  352. &&OPCODE_TYPE_ADJUST_VECTOR2, \
  353. &&OPCODE_TYPE_ADJUST_VECTOR2I, \
  354. &&OPCODE_TYPE_ADJUST_RECT2, \
  355. &&OPCODE_TYPE_ADJUST_RECT2I, \
  356. &&OPCODE_TYPE_ADJUST_VECTOR3, \
  357. &&OPCODE_TYPE_ADJUST_VECTOR3I, \
  358. &&OPCODE_TYPE_ADJUST_TRANSFORM2D, \
  359. &&OPCODE_TYPE_ADJUST_VECTOR4, \
  360. &&OPCODE_TYPE_ADJUST_VECTOR4I, \
  361. &&OPCODE_TYPE_ADJUST_PLANE, \
  362. &&OPCODE_TYPE_ADJUST_QUATERNION, \
  363. &&OPCODE_TYPE_ADJUST_AABB, \
  364. &&OPCODE_TYPE_ADJUST_BASIS, \
  365. &&OPCODE_TYPE_ADJUST_TRANSFORM3D, \
  366. &&OPCODE_TYPE_ADJUST_PROJECTION, \
  367. &&OPCODE_TYPE_ADJUST_COLOR, \
  368. &&OPCODE_TYPE_ADJUST_STRING_NAME, \
  369. &&OPCODE_TYPE_ADJUST_NODE_PATH, \
  370. &&OPCODE_TYPE_ADJUST_RID, \
  371. &&OPCODE_TYPE_ADJUST_OBJECT, \
  372. &&OPCODE_TYPE_ADJUST_CALLABLE, \
  373. &&OPCODE_TYPE_ADJUST_SIGNAL, \
  374. &&OPCODE_TYPE_ADJUST_DICTIONARY, \
  375. &&OPCODE_TYPE_ADJUST_ARRAY, \
  376. &&OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, \
  377. &&OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, \
  378. &&OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, \
  379. &&OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, \
  380. &&OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, \
  381. &&OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, \
  382. &&OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, \
  383. &&OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, \
  384. &&OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, \
  385. &&OPCODE_TYPE_ADJUST_PACKED_VECTOR4_ARRAY, \
  386. &&OPCODE_ASSERT, \
  387. &&OPCODE_BREAKPOINT, \
  388. &&OPCODE_LINE, \
  389. &&OPCODE_END \
  390. }; \
  391. static_assert(std::size(switch_table_ops) == (OPCODE_END + 1), "Opcodes in jump table aren't the same as opcodes in enum.");
  392. #define OPCODE(m_op) \
  393. m_op:
  394. #define OPCODE_WHILE(m_test)
  395. #define OPCODES_END \
  396. OPSEXIT:
  397. #define OPCODES_OUT \
  398. OPSOUT:
  399. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  400. #ifdef DEBUG_ENABLED
  401. #define DISPATCH_OPCODE \
  402. last_opcode = _code_ptr[ip]; \
  403. goto *switch_table_ops[last_opcode]
  404. #else // !DEBUG_ENABLED
  405. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  406. #endif // DEBUG_ENABLED
  407. #define OPCODE_BREAK goto OPSEXIT
  408. #define OPCODE_OUT goto OPSOUT
  409. #else // !(defined(__GNUC__) || defined(__clang__))
  410. #define OPCODES_TABLE
  411. #define OPCODE(m_op) case m_op:
  412. #define OPCODE_WHILE(m_test) while (m_test)
  413. #define OPCODES_END
  414. #define OPCODES_OUT
  415. #define DISPATCH_OPCODE continue
  416. #ifdef _MSC_VER
  417. #define OPCODE_SWITCH(m_test) \
  418. __assume(m_test <= OPCODE_END); \
  419. switch (m_test)
  420. #else // !_MSC_VER
  421. #define OPCODE_SWITCH(m_test) switch (m_test)
  422. #endif // _MSC_VER
  423. #define OPCODE_BREAK break
  424. #define OPCODE_OUT break
  425. #endif // defined(__GNUC__) || defined(__clang__)
  426. // Helpers for VariantInternal methods in macros.
  427. #define OP_GET_BOOL get_bool
  428. #define OP_GET_INT get_int
  429. #define OP_GET_FLOAT get_float
  430. #define OP_GET_VECTOR2 get_vector2
  431. #define OP_GET_VECTOR2I get_vector2i
  432. #define OP_GET_VECTOR3 get_vector3
  433. #define OP_GET_VECTOR3I get_vector3i
  434. #define OP_GET_RECT2 get_rect2
  435. #define OP_GET_VECTOR4 get_vector4
  436. #define OP_GET_VECTOR4I get_vector4i
  437. #define OP_GET_RECT2I get_rect2i
  438. #define OP_GET_QUATERNION get_quaternion
  439. #define OP_GET_COLOR get_color
  440. #define OP_GET_STRING get_string
  441. #define OP_GET_STRING_NAME get_string_name
  442. #define OP_GET_NODE_PATH get_node_path
  443. #define OP_GET_CALLABLE get_callable
  444. #define OP_GET_SIGNAL get_signal
  445. #define OP_GET_ARRAY get_array
  446. #define OP_GET_DICTIONARY get_dictionary
  447. #define OP_GET_PACKED_BYTE_ARRAY get_byte_array
  448. #define OP_GET_PACKED_INT32_ARRAY get_int32_array
  449. #define OP_GET_PACKED_INT64_ARRAY get_int64_array
  450. #define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
  451. #define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
  452. #define OP_GET_PACKED_STRING_ARRAY get_string_array
  453. #define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
  454. #define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
  455. #define OP_GET_PACKED_COLOR_ARRAY get_color_array
  456. #define OP_GET_PACKED_VECTOR4_ARRAY get_vector4_array
  457. #define OP_GET_TRANSFORM3D get_transform
  458. #define OP_GET_TRANSFORM2D get_transform2d
  459. #define OP_GET_PROJECTION get_projection
  460. #define OP_GET_PLANE get_plane
  461. #define OP_GET_AABB get_aabb
  462. #define OP_GET_BASIS get_basis
  463. #define OP_GET_RID get_rid
  464. #define METHOD_CALL_ON_NULL_VALUE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a null value."
  465. #define METHOD_CALL_ON_FREED_INSTANCE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a previously freed instance."
  466. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  467. OPCODES_TABLE;
  468. if (!_code_ptr) {
  469. return _get_default_variant_for_data_type(return_type);
  470. }
  471. r_err.error = Callable::CallError::CALL_OK;
  472. static thread_local int call_depth = 0;
  473. if (unlikely(++call_depth > MAX_CALL_DEPTH)) {
  474. call_depth--;
  475. #ifdef DEBUG_ENABLED
  476. String err_file;
  477. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->path.is_empty()) {
  478. err_file = p_instance->script->path;
  479. } else if (_script) {
  480. err_file = _script->path;
  481. }
  482. if (err_file.is_empty()) {
  483. err_file = "<built-in>";
  484. }
  485. String err_func = name;
  486. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->local_name != StringName()) {
  487. err_func = p_instance->script->local_name.operator String() + "." + err_func;
  488. }
  489. int err_line = _initial_line;
  490. const char *err_text = "Stack overflow. Check for infinite recursion in your script.";
  491. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  492. // Debugger break did not happen.
  493. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text, false, ERR_HANDLER_SCRIPT);
  494. }
  495. #endif
  496. return _get_default_variant_for_data_type(return_type);
  497. }
  498. Variant retvalue;
  499. Variant *stack = nullptr;
  500. Variant **instruction_args = nullptr;
  501. int defarg = 0;
  502. uint32_t alloca_size = 0;
  503. GDScript *script;
  504. int ip = 0;
  505. int line = _initial_line;
  506. if (p_state) {
  507. //use existing (supplied) state (awaited)
  508. stack = (Variant *)p_state->stack.ptr();
  509. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  510. line = p_state->line;
  511. ip = p_state->ip;
  512. alloca_size = p_state->stack.size();
  513. script = p_state->script;
  514. p_instance = p_state->instance;
  515. defarg = p_state->defarg;
  516. } else {
  517. if (p_argcount != _argument_count) {
  518. if (p_argcount > _argument_count) {
  519. if (!is_vararg()) {
  520. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  521. r_err.expected = _argument_count;
  522. call_depth--;
  523. return _get_default_variant_for_data_type(return_type);
  524. }
  525. } else if (p_argcount < _argument_count - _default_arg_count) {
  526. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  527. r_err.expected = _argument_count - _default_arg_count;
  528. call_depth--;
  529. return _get_default_variant_for_data_type(return_type);
  530. } else {
  531. defarg = _argument_count - p_argcount;
  532. }
  533. }
  534. alloca_size = sizeof(Variant *) * FIXED_ADDRESSES_MAX + sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  535. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  536. stack = (Variant *)aptr;
  537. const int non_vararg_arg_count = MIN(p_argcount, _argument_count);
  538. for (int i = 0; i < non_vararg_arg_count; i++) {
  539. if (!argument_types[i].has_type) {
  540. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(*p_args[i]));
  541. continue;
  542. }
  543. // If types already match, don't call Variant::construct(). Constructors of some types
  544. // (e.g. packed arrays) do copies, whereas they pass by reference when inside a Variant.
  545. if (argument_types[i].is_type(*p_args[i], false)) {
  546. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(*p_args[i]));
  547. continue;
  548. }
  549. if (!argument_types[i].is_type(*p_args[i], true)) {
  550. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  551. r_err.argument = i;
  552. r_err.expected = argument_types[i].builtin_type;
  553. call_depth--;
  554. return _get_default_variant_for_data_type(return_type);
  555. }
  556. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  557. if (argument_types[i].builtin_type == Variant::DICTIONARY && argument_types[i].has_container_element_types()) {
  558. const GDScriptDataType &arg_key_type = argument_types[i].get_container_element_type_or_variant(0);
  559. const GDScriptDataType &arg_value_type = argument_types[i].get_container_element_type_or_variant(1);
  560. Dictionary dict(p_args[i]->operator Dictionary(), arg_key_type.builtin_type, arg_key_type.native_type, arg_key_type.script_type, arg_value_type.builtin_type, arg_value_type.native_type, arg_value_type.script_type);
  561. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(dict));
  562. } else if (argument_types[i].builtin_type == Variant::ARRAY && argument_types[i].has_container_element_type(0)) {
  563. const GDScriptDataType &arg_type = argument_types[i].container_element_types[0];
  564. Array array(p_args[i]->operator Array(), arg_type.builtin_type, arg_type.native_type, arg_type.script_type);
  565. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(array));
  566. } else {
  567. Variant variant;
  568. Variant::construct(argument_types[i].builtin_type, variant, &p_args[i], 1, r_err);
  569. if (unlikely(r_err.error)) {
  570. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  571. r_err.argument = i;
  572. r_err.expected = argument_types[i].builtin_type;
  573. call_depth--;
  574. return _get_default_variant_for_data_type(return_type);
  575. }
  576. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(variant));
  577. }
  578. } else {
  579. memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(*p_args[i]));
  580. }
  581. }
  582. for (int i = non_vararg_arg_count + FIXED_ADDRESSES_MAX; i < _stack_size; i++) {
  583. memnew_placement(&stack[i], Variant);
  584. }
  585. if (is_vararg()) {
  586. Array vararg;
  587. stack[_vararg_index] = vararg;
  588. if (p_argcount > _argument_count) {
  589. vararg.resize(p_argcount - _argument_count);
  590. for (int i = 0; i < p_argcount - _argument_count; i++) {
  591. vararg[i] = *p_args[i + _argument_count];
  592. }
  593. }
  594. }
  595. if (_instruction_args_size) {
  596. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  597. } else {
  598. instruction_args = nullptr;
  599. }
  600. for (const KeyValue<int, Variant::Type> &E : temporary_slots) {
  601. type_init_function_table[E.value](&stack[E.key]);
  602. }
  603. }
  604. if (p_instance) {
  605. memnew_placement(&stack[ADDR_STACK_SELF], Variant(p_instance->owner));
  606. script = p_instance->script.ptr();
  607. } else {
  608. memnew_placement(&stack[ADDR_STACK_SELF], Variant);
  609. script = _script;
  610. }
  611. memnew_placement(&stack[ADDR_STACK_CLASS], Variant(script));
  612. memnew_placement(&stack[ADDR_STACK_NIL], Variant);
  613. String err_text;
  614. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  615. #ifdef DEBUG_ENABLED
  616. #define GD_ERR_BREAK(m_cond) \
  617. { \
  618. if (unlikely(m_cond)) { \
  619. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  620. OPCODE_BREAK; \
  621. } \
  622. }
  623. #define CHECK_SPACE(m_space) \
  624. GD_ERR_BREAK((ip + m_space) > _code_size)
  625. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  626. Variant *m_v; \
  627. { \
  628. int address = _code_ptr[ip + 1 + (m_code_ofs)]; \
  629. int address_type = (address & ADDR_TYPE_MASK) >> ADDR_BITS; \
  630. if (unlikely(address_type < 0 || address_type >= ADDR_TYPE_MAX)) { \
  631. err_text = "Bad address type."; \
  632. OPCODE_BREAK; \
  633. } \
  634. int address_index = address & ADDR_MASK; \
  635. if (unlikely(address_index < 0 || address_index >= variant_address_limits[address_type])) { \
  636. if (address_type == ADDR_TYPE_MEMBER && !p_instance) { \
  637. err_text = "Cannot access member without instance."; \
  638. } else { \
  639. err_text = "Bad address index."; \
  640. } \
  641. OPCODE_BREAK; \
  642. } \
  643. m_v = &variant_addresses[address_type][address_index]; \
  644. if (unlikely(!m_v)) \
  645. OPCODE_BREAK; \
  646. }
  647. #else // !DEBUG_ENABLED
  648. #define GD_ERR_BREAK(m_cond)
  649. #define CHECK_SPACE(m_space)
  650. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  651. Variant *m_v; \
  652. { \
  653. int address = _code_ptr[ip + 1 + (m_code_ofs)]; \
  654. m_v = &variant_addresses[(address & ADDR_TYPE_MASK) >> ADDR_BITS][address & ADDR_MASK]; \
  655. if (unlikely(!m_v)) \
  656. OPCODE_BREAK; \
  657. }
  658. #endif // DEBUG_ENABLED
  659. #define LOAD_INSTRUCTION_ARGS \
  660. int instr_arg_count = _code_ptr[ip + 1]; \
  661. for (int i = 0; i < instr_arg_count; i++) { \
  662. GET_VARIANT_PTR(v, i + 1); \
  663. instruction_args[i] = v; \
  664. } \
  665. ip += 1; // Offset to skip instruction argcount.
  666. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  667. Variant *m_v = instruction_args[m_idx]
  668. #ifdef DEBUG_ENABLED
  669. uint64_t function_start_time = 0;
  670. uint64_t function_call_time = 0;
  671. if (GDScriptLanguage::get_singleton()->profiling) {
  672. function_start_time = OS::get_singleton()->get_ticks_usec();
  673. function_call_time = 0;
  674. profile.call_count.increment();
  675. profile.frame_call_count.increment();
  676. }
  677. bool exit_ok = false;
  678. int variant_address_limits[ADDR_TYPE_MAX] = { _stack_size, _constant_count, p_instance ? (int)p_instance->members.size() : 0 };
  679. #endif
  680. bool awaited = false;
  681. Variant *variant_addresses[ADDR_TYPE_MAX] = { stack, _constants_ptr, p_instance ? p_instance->members.ptrw() : nullptr };
  682. #ifdef DEBUG_ENABLED
  683. OPCODE_WHILE(ip < _code_size) {
  684. int last_opcode = _code_ptr[ip];
  685. #else
  686. OPCODE_WHILE(true) {
  687. #endif
  688. OPCODE_SWITCH(_code_ptr[ip]) {
  689. OPCODE(OPCODE_OPERATOR) {
  690. constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*_code_ptr);
  691. CHECK_SPACE(7 + _pointer_size);
  692. bool valid;
  693. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  694. GD_ERR_BREAK(op >= Variant::OP_MAX);
  695. GET_VARIANT_PTR(a, 0);
  696. GET_VARIANT_PTR(b, 1);
  697. GET_VARIANT_PTR(dst, 2);
  698. // Compute signatures (types of operands) so it can be optimized when matching.
  699. uint32_t op_signature = _code_ptr[ip + 5];
  700. uint32_t actual_signature = (a->get_type() << 8) | (b->get_type());
  701. #ifdef DEBUG_ENABLED
  702. if (op == Variant::OP_DIVIDE || op == Variant::OP_MODULE) {
  703. // Don't optimize division and modulo since there's not check for division by zero with validated calls.
  704. op_signature = 0xFFFF;
  705. _code_ptr[ip + 5] = op_signature;
  706. }
  707. #endif
  708. // Check if this is the first run. If so, store the current signature for the optimized path.
  709. if (unlikely(op_signature == 0)) {
  710. static Mutex initializer_mutex;
  711. initializer_mutex.lock();
  712. Variant::Type a_type = (Variant::Type)((actual_signature >> 8) & 0xFF);
  713. Variant::Type b_type = (Variant::Type)(actual_signature & 0xFF);
  714. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(op, a_type, b_type);
  715. if (unlikely(!op_func)) {
  716. #ifdef DEBUG_ENABLED
  717. err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
  718. #endif
  719. initializer_mutex.unlock();
  720. OPCODE_BREAK;
  721. } else {
  722. Variant::Type ret_type = Variant::get_operator_return_type(op, a_type, b_type);
  723. VariantInternal::initialize(dst, ret_type);
  724. op_func(a, b, dst);
  725. // Check again in case another thread already set it.
  726. if (_code_ptr[ip + 5] == 0) {
  727. _code_ptr[ip + 5] = actual_signature;
  728. _code_ptr[ip + 6] = static_cast<int>(ret_type);
  729. Variant::ValidatedOperatorEvaluator *tmp = reinterpret_cast<Variant::ValidatedOperatorEvaluator *>(&_code_ptr[ip + 7]);
  730. *tmp = op_func;
  731. }
  732. }
  733. initializer_mutex.unlock();
  734. } else if (likely(op_signature == actual_signature)) {
  735. // If the signature matches, we can use the optimized path.
  736. Variant::Type ret_type = static_cast<Variant::Type>(_code_ptr[ip + 6]);
  737. Variant::ValidatedOperatorEvaluator op_func = *reinterpret_cast<Variant::ValidatedOperatorEvaluator *>(&_code_ptr[ip + 7]);
  738. // Make sure the return value has the correct type.
  739. VariantInternal::initialize(dst, ret_type);
  740. op_func(a, b, dst);
  741. } else {
  742. // If the signature doesn't match, we have to use the slow path.
  743. #ifdef DEBUG_ENABLED
  744. Variant ret;
  745. Variant::evaluate(op, *a, *b, ret, valid);
  746. #else
  747. Variant::evaluate(op, *a, *b, *dst, valid);
  748. #endif
  749. #ifdef DEBUG_ENABLED
  750. if (!valid) {
  751. if (ret.get_type() == Variant::STRING) {
  752. //return a string when invalid with the error
  753. err_text = ret;
  754. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  755. } else {
  756. err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
  757. }
  758. OPCODE_BREAK;
  759. }
  760. *dst = ret;
  761. #endif
  762. }
  763. ip += 7 + _pointer_size;
  764. }
  765. DISPATCH_OPCODE;
  766. OPCODE(OPCODE_OPERATOR_VALIDATED) {
  767. CHECK_SPACE(5);
  768. int operator_idx = _code_ptr[ip + 4];
  769. GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
  770. Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
  771. GET_VARIANT_PTR(a, 0);
  772. GET_VARIANT_PTR(b, 1);
  773. GET_VARIANT_PTR(dst, 2);
  774. operator_func(a, b, dst);
  775. ip += 5;
  776. }
  777. DISPATCH_OPCODE;
  778. OPCODE(OPCODE_TYPE_TEST_BUILTIN) {
  779. CHECK_SPACE(4);
  780. GET_VARIANT_PTR(dst, 0);
  781. GET_VARIANT_PTR(value, 1);
  782. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
  783. GD_ERR_BREAK(builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX);
  784. *dst = value->get_type() == builtin_type;
  785. ip += 4;
  786. }
  787. DISPATCH_OPCODE;
  788. OPCODE(OPCODE_TYPE_TEST_ARRAY) {
  789. CHECK_SPACE(6);
  790. GET_VARIANT_PTR(dst, 0);
  791. GET_VARIANT_PTR(value, 1);
  792. GET_VARIANT_PTR(script_type, 2);
  793. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4];
  794. int native_type_idx = _code_ptr[ip + 5];
  795. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  796. const StringName native_type = _global_names_ptr[native_type_idx];
  797. bool result = false;
  798. if (value->get_type() == Variant::ARRAY) {
  799. Array *array = VariantInternal::get_array(value);
  800. result = array->get_typed_builtin() == ((uint32_t)builtin_type) && array->get_typed_class_name() == native_type && array->get_typed_script() == *script_type;
  801. }
  802. *dst = result;
  803. ip += 6;
  804. }
  805. DISPATCH_OPCODE;
  806. OPCODE(OPCODE_TYPE_TEST_DICTIONARY) {
  807. CHECK_SPACE(9);
  808. GET_VARIANT_PTR(dst, 0);
  809. GET_VARIANT_PTR(value, 1);
  810. GET_VARIANT_PTR(key_script_type, 2);
  811. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 5];
  812. int key_native_type_idx = _code_ptr[ip + 6];
  813. GD_ERR_BREAK(key_native_type_idx < 0 || key_native_type_idx >= _global_names_count);
  814. const StringName key_native_type = _global_names_ptr[key_native_type_idx];
  815. GET_VARIANT_PTR(value_script_type, 3);
  816. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 7];
  817. int value_native_type_idx = _code_ptr[ip + 8];
  818. GD_ERR_BREAK(value_native_type_idx < 0 || value_native_type_idx >= _global_names_count);
  819. const StringName value_native_type = _global_names_ptr[value_native_type_idx];
  820. bool result = false;
  821. if (value->get_type() == Variant::DICTIONARY) {
  822. Dictionary *dictionary = VariantInternal::get_dictionary(value);
  823. result = dictionary->get_typed_key_builtin() == ((uint32_t)key_builtin_type) && dictionary->get_typed_key_class_name() == key_native_type && dictionary->get_typed_key_script() == *key_script_type &&
  824. dictionary->get_typed_value_builtin() == ((uint32_t)value_builtin_type) && dictionary->get_typed_value_class_name() == value_native_type && dictionary->get_typed_value_script() == *value_script_type;
  825. }
  826. *dst = result;
  827. ip += 9;
  828. }
  829. DISPATCH_OPCODE;
  830. OPCODE(OPCODE_TYPE_TEST_NATIVE) {
  831. CHECK_SPACE(4);
  832. GET_VARIANT_PTR(dst, 0);
  833. GET_VARIANT_PTR(value, 1);
  834. int native_type_idx = _code_ptr[ip + 3];
  835. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  836. const StringName native_type = _global_names_ptr[native_type_idx];
  837. bool was_freed = false;
  838. Object *object = value->get_validated_object_with_check(was_freed);
  839. if (was_freed) {
  840. err_text = "Left operand of 'is' is a previously freed instance.";
  841. OPCODE_BREAK;
  842. }
  843. *dst = object && ClassDB::is_parent_class(object->get_class_name(), native_type);
  844. ip += 4;
  845. }
  846. DISPATCH_OPCODE;
  847. OPCODE(OPCODE_TYPE_TEST_SCRIPT) {
  848. CHECK_SPACE(4);
  849. GET_VARIANT_PTR(dst, 0);
  850. GET_VARIANT_PTR(value, 1);
  851. GET_VARIANT_PTR(type, 2);
  852. Script *script_type = Object::cast_to<Script>(type->operator Object *());
  853. GD_ERR_BREAK(!script_type);
  854. bool was_freed = false;
  855. Object *object = value->get_validated_object_with_check(was_freed);
  856. if (was_freed) {
  857. err_text = "Left operand of 'is' is a previously freed instance.";
  858. OPCODE_BREAK;
  859. }
  860. bool result = false;
  861. if (object && object->get_script_instance()) {
  862. Script *script_ptr = object->get_script_instance()->get_script().ptr();
  863. while (script_ptr) {
  864. if (script_ptr == script_type) {
  865. result = true;
  866. break;
  867. }
  868. script_ptr = script_ptr->get_base_script().ptr();
  869. }
  870. }
  871. *dst = result;
  872. ip += 4;
  873. }
  874. DISPATCH_OPCODE;
  875. OPCODE(OPCODE_SET_KEYED) {
  876. CHECK_SPACE(3);
  877. GET_VARIANT_PTR(dst, 0);
  878. GET_VARIANT_PTR(index, 1);
  879. GET_VARIANT_PTR(value, 2);
  880. bool valid;
  881. #ifdef DEBUG_ENABLED
  882. Variant::VariantSetError err_code;
  883. dst->set(*index, *value, &valid, &err_code);
  884. #else
  885. dst->set(*index, *value, &valid);
  886. #endif
  887. #ifdef DEBUG_ENABLED
  888. if (!valid) {
  889. if (dst->is_read_only()) {
  890. err_text = "Invalid assignment on read-only value (on base: '" + _get_var_type(dst) + "').";
  891. } else {
  892. Object *obj = dst->get_validated_object();
  893. String v = index->operator String();
  894. bool read_only_property = false;
  895. if (obj) {
  896. read_only_property = ClassDB::has_property(obj->get_class_name(), v) && (ClassDB::get_property_setter(obj->get_class_name(), v) == StringName());
  897. }
  898. if (read_only_property) {
  899. err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", v, _get_var_type(dst));
  900. } else {
  901. if (!v.is_empty()) {
  902. v = "'" + v + "'";
  903. } else {
  904. v = "of type '" + _get_var_type(index) + "'";
  905. }
  906. err_text = "Invalid assignment of property or key " + v + " with value of type '" + _get_var_type(value) + "' on a base object of type '" + _get_var_type(dst) + "'.";
  907. if (err_code == Variant::VariantSetError::SET_INDEXED_ERR) {
  908. err_text = "Invalid assignment of index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  909. }
  910. }
  911. }
  912. OPCODE_BREAK;
  913. }
  914. #endif
  915. ip += 4;
  916. }
  917. DISPATCH_OPCODE;
  918. OPCODE(OPCODE_SET_KEYED_VALIDATED) {
  919. CHECK_SPACE(4);
  920. GET_VARIANT_PTR(dst, 0);
  921. GET_VARIANT_PTR(index, 1);
  922. GET_VARIANT_PTR(value, 2);
  923. int index_setter = _code_ptr[ip + 4];
  924. GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
  925. const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
  926. bool valid;
  927. setter(dst, index, value, &valid);
  928. #ifdef DEBUG_ENABLED
  929. if (!valid) {
  930. if (dst->is_read_only()) {
  931. err_text = "Invalid assignment on read-only value (on base: '" + _get_var_type(dst) + "').";
  932. } else {
  933. String v = index->operator String();
  934. if (!v.is_empty()) {
  935. v = "'" + v + "'";
  936. } else {
  937. v = "of type '" + _get_var_type(index) + "'";
  938. }
  939. err_text = "Invalid assignment of property or key " + v + " with value of type '" + _get_var_type(value) + "' on a base object of type '" + _get_var_type(dst) + "'.";
  940. }
  941. OPCODE_BREAK;
  942. }
  943. #endif
  944. ip += 5;
  945. }
  946. DISPATCH_OPCODE;
  947. OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
  948. CHECK_SPACE(4);
  949. GET_VARIANT_PTR(dst, 0);
  950. GET_VARIANT_PTR(index, 1);
  951. GET_VARIANT_PTR(value, 2);
  952. int index_setter = _code_ptr[ip + 4];
  953. GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
  954. const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
  955. int64_t int_index = *VariantInternal::get_int(index);
  956. bool oob;
  957. setter(dst, int_index, value, &oob);
  958. #ifdef DEBUG_ENABLED
  959. if (oob) {
  960. if (dst->is_read_only()) {
  961. err_text = "Invalid assignment on read-only value (on base: '" + _get_var_type(dst) + "').";
  962. } else {
  963. String v = index->operator String();
  964. if (!v.is_empty()) {
  965. v = "'" + v + "'";
  966. } else {
  967. v = "of type '" + _get_var_type(index) + "'";
  968. }
  969. err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
  970. }
  971. OPCODE_BREAK;
  972. }
  973. #endif
  974. ip += 5;
  975. }
  976. DISPATCH_OPCODE;
  977. OPCODE(OPCODE_GET_KEYED) {
  978. CHECK_SPACE(3);
  979. GET_VARIANT_PTR(src, 0);
  980. GET_VARIANT_PTR(index, 1);
  981. GET_VARIANT_PTR(dst, 2);
  982. bool valid;
  983. #ifdef DEBUG_ENABLED
  984. // Allow better error message in cases where src and dst are the same stack position.
  985. Variant::VariantGetError err_code;
  986. Variant ret = src->get(*index, &valid, &err_code);
  987. #else
  988. *dst = src->get(*index, &valid);
  989. #endif
  990. #ifdef DEBUG_ENABLED
  991. if (!valid) {
  992. String v = index->operator String();
  993. if (!v.is_empty()) {
  994. v = "'" + v + "'";
  995. } else {
  996. v = "of type '" + _get_var_type(index) + "'";
  997. }
  998. err_text = "Invalid access to property or key " + v + " on a base object of type '" + _get_var_type(src) + "'.";
  999. if (err_code == Variant::VariantGetError::GET_INDEXED_ERR) {
  1000. err_text = "Invalid access of index " + v + " on a base object of type: '" + _get_var_type(src) + "'.";
  1001. }
  1002. OPCODE_BREAK;
  1003. }
  1004. *dst = ret;
  1005. #endif
  1006. ip += 4;
  1007. }
  1008. DISPATCH_OPCODE;
  1009. OPCODE(OPCODE_GET_KEYED_VALIDATED) {
  1010. CHECK_SPACE(4);
  1011. GET_VARIANT_PTR(src, 0);
  1012. GET_VARIANT_PTR(key, 1);
  1013. GET_VARIANT_PTR(dst, 2);
  1014. int index_getter = _code_ptr[ip + 4];
  1015. GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
  1016. const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
  1017. bool valid;
  1018. #ifdef DEBUG_ENABLED
  1019. // Allow better error message in cases where src and dst are the same stack position.
  1020. Variant ret;
  1021. getter(src, key, &ret, &valid);
  1022. #else
  1023. getter(src, key, dst, &valid);
  1024. #endif
  1025. #ifdef DEBUG_ENABLED
  1026. if (!valid) {
  1027. String v = key->operator String();
  1028. if (!v.is_empty()) {
  1029. v = "'" + v + "'";
  1030. } else {
  1031. v = "of type '" + _get_var_type(key) + "'";
  1032. }
  1033. err_text = "Invalid access to property or key " + v + " on a base object of type '" + _get_var_type(src) + "'.";
  1034. OPCODE_BREAK;
  1035. }
  1036. *dst = ret;
  1037. #endif
  1038. ip += 5;
  1039. }
  1040. DISPATCH_OPCODE;
  1041. OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
  1042. CHECK_SPACE(4);
  1043. GET_VARIANT_PTR(src, 0);
  1044. GET_VARIANT_PTR(index, 1);
  1045. GET_VARIANT_PTR(dst, 2);
  1046. int index_getter = _code_ptr[ip + 4];
  1047. GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
  1048. const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
  1049. int64_t int_index = *VariantInternal::get_int(index);
  1050. bool oob;
  1051. getter(src, int_index, dst, &oob);
  1052. #ifdef DEBUG_ENABLED
  1053. if (oob) {
  1054. String v = index->operator String();
  1055. if (!v.is_empty()) {
  1056. v = "'" + v + "'";
  1057. } else {
  1058. v = "of type '" + _get_var_type(index) + "'";
  1059. }
  1060. err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
  1061. OPCODE_BREAK;
  1062. }
  1063. #endif
  1064. ip += 5;
  1065. }
  1066. DISPATCH_OPCODE;
  1067. OPCODE(OPCODE_SET_NAMED) {
  1068. CHECK_SPACE(3);
  1069. GET_VARIANT_PTR(dst, 0);
  1070. GET_VARIANT_PTR(value, 1);
  1071. int indexname = _code_ptr[ip + 3];
  1072. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  1073. const StringName *index = &_global_names_ptr[indexname];
  1074. bool valid;
  1075. dst->set_named(*index, *value, valid);
  1076. #ifdef DEBUG_ENABLED
  1077. if (!valid) {
  1078. if (dst->is_read_only()) {
  1079. err_text = "Invalid assignment on read-only value (on base: '" + _get_var_type(dst) + "').";
  1080. } else {
  1081. Object *obj = dst->get_validated_object();
  1082. bool read_only_property = false;
  1083. if (obj) {
  1084. read_only_property = ClassDB::has_property(obj->get_class_name(), *index) && (ClassDB::get_property_setter(obj->get_class_name(), *index) == StringName());
  1085. }
  1086. if (read_only_property) {
  1087. err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", String(*index), _get_var_type(dst));
  1088. } else {
  1089. err_text = "Invalid assignment of property or key '" + String(*index) + "' with value of type '" + _get_var_type(value) + "' on a base object of type '" + _get_var_type(dst) + "'.";
  1090. }
  1091. }
  1092. OPCODE_BREAK;
  1093. }
  1094. #endif
  1095. ip += 4;
  1096. }
  1097. DISPATCH_OPCODE;
  1098. OPCODE(OPCODE_SET_NAMED_VALIDATED) {
  1099. CHECK_SPACE(3);
  1100. GET_VARIANT_PTR(dst, 0);
  1101. GET_VARIANT_PTR(value, 1);
  1102. int index_setter = _code_ptr[ip + 3];
  1103. GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
  1104. const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
  1105. setter(dst, value);
  1106. ip += 4;
  1107. }
  1108. DISPATCH_OPCODE;
  1109. OPCODE(OPCODE_GET_NAMED) {
  1110. CHECK_SPACE(4);
  1111. GET_VARIANT_PTR(src, 0);
  1112. GET_VARIANT_PTR(dst, 1);
  1113. int indexname = _code_ptr[ip + 3];
  1114. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  1115. const StringName *index = &_global_names_ptr[indexname];
  1116. bool valid;
  1117. #ifdef DEBUG_ENABLED
  1118. //allow better error message in cases where src and dst are the same stack position
  1119. Variant ret = src->get_named(*index, valid);
  1120. #else
  1121. *dst = src->get_named(*index, valid);
  1122. #endif
  1123. #ifdef DEBUG_ENABLED
  1124. if (!valid) {
  1125. err_text = "Invalid access to property or key '" + index->operator String() + "' on a base object of type '" + _get_var_type(src) + "'.";
  1126. OPCODE_BREAK;
  1127. }
  1128. *dst = ret;
  1129. #endif
  1130. ip += 4;
  1131. }
  1132. DISPATCH_OPCODE;
  1133. OPCODE(OPCODE_GET_NAMED_VALIDATED) {
  1134. CHECK_SPACE(3);
  1135. GET_VARIANT_PTR(src, 0);
  1136. GET_VARIANT_PTR(dst, 1);
  1137. int index_getter = _code_ptr[ip + 3];
  1138. GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
  1139. const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
  1140. getter(src, dst);
  1141. ip += 4;
  1142. }
  1143. DISPATCH_OPCODE;
  1144. OPCODE(OPCODE_SET_MEMBER) {
  1145. CHECK_SPACE(3);
  1146. GET_VARIANT_PTR(src, 0);
  1147. int indexname = _code_ptr[ip + 2];
  1148. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  1149. const StringName *index = &_global_names_ptr[indexname];
  1150. bool valid;
  1151. #ifndef DEBUG_ENABLED
  1152. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  1153. #else
  1154. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  1155. if (!ok) {
  1156. err_text = "Internal error setting property: " + String(*index);
  1157. OPCODE_BREAK;
  1158. } else if (!valid) {
  1159. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  1160. OPCODE_BREAK;
  1161. }
  1162. #endif
  1163. ip += 3;
  1164. }
  1165. DISPATCH_OPCODE;
  1166. OPCODE(OPCODE_GET_MEMBER) {
  1167. CHECK_SPACE(3);
  1168. GET_VARIANT_PTR(dst, 0);
  1169. int indexname = _code_ptr[ip + 2];
  1170. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  1171. const StringName *index = &_global_names_ptr[indexname];
  1172. #ifndef DEBUG_ENABLED
  1173. ClassDB::get_property(p_instance->owner, *index, *dst);
  1174. #else
  1175. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  1176. if (!ok) {
  1177. err_text = "Internal error getting property: " + String(*index);
  1178. OPCODE_BREAK;
  1179. }
  1180. #endif
  1181. ip += 3;
  1182. }
  1183. DISPATCH_OPCODE;
  1184. OPCODE(OPCODE_SET_STATIC_VARIABLE) {
  1185. CHECK_SPACE(4);
  1186. GET_VARIANT_PTR(value, 0);
  1187. GET_VARIANT_PTR(_class, 1);
  1188. GDScript *gdscript = Object::cast_to<GDScript>(_class->operator Object *());
  1189. GD_ERR_BREAK(!gdscript);
  1190. int index = _code_ptr[ip + 3];
  1191. GD_ERR_BREAK(index < 0 || index >= gdscript->static_variables.size());
  1192. gdscript->static_variables.write[index] = *value;
  1193. ip += 4;
  1194. }
  1195. DISPATCH_OPCODE;
  1196. OPCODE(OPCODE_GET_STATIC_VARIABLE) {
  1197. CHECK_SPACE(4);
  1198. GET_VARIANT_PTR(target, 0);
  1199. GET_VARIANT_PTR(_class, 1);
  1200. GDScript *gdscript = Object::cast_to<GDScript>(_class->operator Object *());
  1201. GD_ERR_BREAK(!gdscript);
  1202. int index = _code_ptr[ip + 3];
  1203. GD_ERR_BREAK(index < 0 || index >= gdscript->static_variables.size());
  1204. *target = gdscript->static_variables[index];
  1205. ip += 4;
  1206. }
  1207. DISPATCH_OPCODE;
  1208. OPCODE(OPCODE_ASSIGN) {
  1209. CHECK_SPACE(3);
  1210. GET_VARIANT_PTR(dst, 0);
  1211. GET_VARIANT_PTR(src, 1);
  1212. *dst = *src;
  1213. ip += 3;
  1214. }
  1215. DISPATCH_OPCODE;
  1216. OPCODE(OPCODE_ASSIGN_NULL) {
  1217. CHECK_SPACE(2);
  1218. GET_VARIANT_PTR(dst, 0);
  1219. *dst = Variant();
  1220. ip += 2;
  1221. }
  1222. DISPATCH_OPCODE;
  1223. OPCODE(OPCODE_ASSIGN_TRUE) {
  1224. CHECK_SPACE(2);
  1225. GET_VARIANT_PTR(dst, 0);
  1226. *dst = true;
  1227. ip += 2;
  1228. }
  1229. DISPATCH_OPCODE;
  1230. OPCODE(OPCODE_ASSIGN_FALSE) {
  1231. CHECK_SPACE(2);
  1232. GET_VARIANT_PTR(dst, 0);
  1233. *dst = false;
  1234. ip += 2;
  1235. }
  1236. DISPATCH_OPCODE;
  1237. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  1238. CHECK_SPACE(4);
  1239. GET_VARIANT_PTR(dst, 0);
  1240. GET_VARIANT_PTR(src, 1);
  1241. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  1242. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  1243. if (src->get_type() != var_type) {
  1244. #ifdef DEBUG_ENABLED
  1245. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  1246. #endif // DEBUG_ENABLED
  1247. Callable::CallError ce;
  1248. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  1249. } else {
  1250. #ifdef DEBUG_ENABLED
  1251. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  1252. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  1253. OPCODE_BREAK;
  1254. }
  1255. } else {
  1256. #endif // DEBUG_ENABLED
  1257. *dst = *src;
  1258. }
  1259. ip += 4;
  1260. }
  1261. DISPATCH_OPCODE;
  1262. OPCODE(OPCODE_ASSIGN_TYPED_ARRAY) {
  1263. CHECK_SPACE(6);
  1264. GET_VARIANT_PTR(dst, 0);
  1265. GET_VARIANT_PTR(src, 1);
  1266. GET_VARIANT_PTR(script_type, 2);
  1267. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4];
  1268. int native_type_idx = _code_ptr[ip + 5];
  1269. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  1270. const StringName native_type = _global_names_ptr[native_type_idx];
  1271. if (src->get_type() != Variant::ARRAY) {
  1272. #ifdef DEBUG_ENABLED
  1273. err_text = vformat(R"(Trying to assign a value of type "%s" to a variable of type "Array[%s]".)",
  1274. _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type));
  1275. #endif // DEBUG_ENABLED
  1276. OPCODE_BREAK;
  1277. }
  1278. Array *array = VariantInternal::get_array(src);
  1279. if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
  1280. #ifdef DEBUG_ENABLED
  1281. err_text = vformat(R"(Trying to assign an array of type "%s" to a variable of type "Array[%s]".)",
  1282. _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type));
  1283. #endif // DEBUG_ENABLED
  1284. OPCODE_BREAK;
  1285. }
  1286. *dst = *src;
  1287. ip += 6;
  1288. }
  1289. DISPATCH_OPCODE;
  1290. OPCODE(OPCODE_ASSIGN_TYPED_DICTIONARY) {
  1291. CHECK_SPACE(9);
  1292. GET_VARIANT_PTR(dst, 0);
  1293. GET_VARIANT_PTR(src, 1);
  1294. GET_VARIANT_PTR(key_script_type, 2);
  1295. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 5];
  1296. int key_native_type_idx = _code_ptr[ip + 6];
  1297. GD_ERR_BREAK(key_native_type_idx < 0 || key_native_type_idx >= _global_names_count);
  1298. const StringName key_native_type = _global_names_ptr[key_native_type_idx];
  1299. GET_VARIANT_PTR(value_script_type, 3);
  1300. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 7];
  1301. int value_native_type_idx = _code_ptr[ip + 8];
  1302. GD_ERR_BREAK(value_native_type_idx < 0 || value_native_type_idx >= _global_names_count);
  1303. const StringName value_native_type = _global_names_ptr[value_native_type_idx];
  1304. if (src->get_type() != Variant::DICTIONARY) {
  1305. #ifdef DEBUG_ENABLED
  1306. err_text = vformat(R"(Trying to assign a value of type "%s" to a variable of type "Dictionary[%s, %s]".)",
  1307. _get_var_type(src), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
  1308. _get_element_type(value_builtin_type, value_native_type, *value_script_type));
  1309. #endif // DEBUG_ENABLED
  1310. OPCODE_BREAK;
  1311. }
  1312. Dictionary *dictionary = VariantInternal::get_dictionary(src);
  1313. if (dictionary->get_typed_key_builtin() != ((uint32_t)key_builtin_type) || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
  1314. dictionary->get_typed_value_builtin() != ((uint32_t)value_builtin_type) || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
  1315. #ifdef DEBUG_ENABLED
  1316. err_text = vformat(R"(Trying to assign a dictionary of type "%s" to a variable of type "Dictionary[%s, %s]".)",
  1317. _get_var_type(src), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
  1318. _get_element_type(value_builtin_type, value_native_type, *value_script_type));
  1319. #endif // DEBUG_ENABLED
  1320. OPCODE_BREAK;
  1321. }
  1322. *dst = *src;
  1323. ip += 9;
  1324. }
  1325. DISPATCH_OPCODE;
  1326. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  1327. CHECK_SPACE(4);
  1328. GET_VARIANT_PTR(dst, 0);
  1329. GET_VARIANT_PTR(src, 1);
  1330. #ifdef DEBUG_ENABLED
  1331. GET_VARIANT_PTR(type, 2);
  1332. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  1333. GD_ERR_BREAK(!nc);
  1334. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1335. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  1336. "' to a variable of type '" + nc->get_name() + "'.";
  1337. OPCODE_BREAK;
  1338. }
  1339. if (src->get_type() == Variant::OBJECT) {
  1340. bool was_freed = false;
  1341. Object *src_obj = src->get_validated_object_with_check(was_freed);
  1342. if (!src_obj && was_freed) {
  1343. err_text = "Trying to assign invalid previously freed instance.";
  1344. OPCODE_BREAK;
  1345. }
  1346. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1347. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  1348. "' to a variable of type '" + nc->get_name() + "'.";
  1349. OPCODE_BREAK;
  1350. }
  1351. }
  1352. #endif // DEBUG_ENABLED
  1353. *dst = *src;
  1354. ip += 4;
  1355. }
  1356. DISPATCH_OPCODE;
  1357. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  1358. CHECK_SPACE(4);
  1359. GET_VARIANT_PTR(dst, 0);
  1360. GET_VARIANT_PTR(src, 1);
  1361. #ifdef DEBUG_ENABLED
  1362. GET_VARIANT_PTR(type, 2);
  1363. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  1364. GD_ERR_BREAK(!base_type);
  1365. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1366. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1367. OPCODE_BREAK;
  1368. }
  1369. if (src->get_type() == Variant::OBJECT) {
  1370. bool was_freed = false;
  1371. Object *val_obj = src->get_validated_object_with_check(was_freed);
  1372. if (!val_obj && was_freed) {
  1373. err_text = "Trying to assign invalid previously freed instance.";
  1374. OPCODE_BREAK;
  1375. }
  1376. if (val_obj) { // src is not null
  1377. ScriptInstance *scr_inst = val_obj->get_script_instance();
  1378. if (!scr_inst) {
  1379. err_text = "Trying to assign value of type '" + val_obj->get_class_name() +
  1380. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  1381. OPCODE_BREAK;
  1382. }
  1383. Script *src_type = scr_inst->get_script().ptr();
  1384. bool valid = false;
  1385. while (src_type) {
  1386. if (src_type == base_type) {
  1387. valid = true;
  1388. break;
  1389. }
  1390. src_type = src_type->get_base_script().ptr();
  1391. }
  1392. if (!valid) {
  1393. err_text = "Trying to assign value of type '" + val_obj->get_script_instance()->get_script()->get_path().get_file() +
  1394. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  1395. OPCODE_BREAK;
  1396. }
  1397. }
  1398. }
  1399. #endif // DEBUG_ENABLED
  1400. *dst = *src;
  1401. ip += 4;
  1402. }
  1403. DISPATCH_OPCODE;
  1404. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  1405. CHECK_SPACE(4);
  1406. GET_VARIANT_PTR(src, 0);
  1407. GET_VARIANT_PTR(dst, 1);
  1408. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  1409. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  1410. #ifdef DEBUG_ENABLED
  1411. if (src->operator Object *() && !src->get_validated_object()) {
  1412. err_text = "Trying to cast a freed object.";
  1413. OPCODE_BREAK;
  1414. }
  1415. #endif
  1416. Callable::CallError err;
  1417. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  1418. #ifdef DEBUG_ENABLED
  1419. if (err.error != Callable::CallError::CALL_OK) {
  1420. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  1421. OPCODE_BREAK;
  1422. }
  1423. #endif
  1424. ip += 4;
  1425. }
  1426. DISPATCH_OPCODE;
  1427. OPCODE(OPCODE_CAST_TO_NATIVE) {
  1428. CHECK_SPACE(4);
  1429. GET_VARIANT_PTR(src, 0);
  1430. GET_VARIANT_PTR(dst, 1);
  1431. GET_VARIANT_PTR(to_type, 2);
  1432. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  1433. GD_ERR_BREAK(!nc);
  1434. #ifdef DEBUG_ENABLED
  1435. if (src->operator Object *() && !src->get_validated_object()) {
  1436. err_text = "Trying to cast a freed object.";
  1437. OPCODE_BREAK;
  1438. }
  1439. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1440. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  1441. OPCODE_BREAK;
  1442. }
  1443. #endif
  1444. Object *src_obj = src->operator Object *();
  1445. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1446. *dst = Variant(); // invalid cast, assign NULL
  1447. } else {
  1448. *dst = *src;
  1449. }
  1450. ip += 4;
  1451. }
  1452. DISPATCH_OPCODE;
  1453. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1454. CHECK_SPACE(4);
  1455. GET_VARIANT_PTR(src, 0);
  1456. GET_VARIANT_PTR(dst, 1);
  1457. GET_VARIANT_PTR(to_type, 2);
  1458. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1459. GD_ERR_BREAK(!base_type);
  1460. #ifdef DEBUG_ENABLED
  1461. if (src->operator Object *() && !src->get_validated_object()) {
  1462. err_text = "Trying to cast a freed object.";
  1463. OPCODE_BREAK;
  1464. }
  1465. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1466. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1467. OPCODE_BREAK;
  1468. }
  1469. #endif
  1470. bool valid = false;
  1471. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1472. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1473. if (scr_inst) {
  1474. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1475. while (src_type) {
  1476. if (src_type == base_type) {
  1477. valid = true;
  1478. break;
  1479. }
  1480. src_type = src_type->get_base_script().ptr();
  1481. }
  1482. }
  1483. }
  1484. if (valid) {
  1485. *dst = *src; // Valid cast, copy the source object
  1486. } else {
  1487. *dst = Variant(); // invalid cast, assign NULL
  1488. }
  1489. ip += 4;
  1490. }
  1491. DISPATCH_OPCODE;
  1492. OPCODE(OPCODE_CONSTRUCT) {
  1493. LOAD_INSTRUCTION_ARGS
  1494. CHECK_SPACE(2 + instr_arg_count);
  1495. ip += instr_arg_count;
  1496. int argc = _code_ptr[ip + 1];
  1497. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1498. Variant **argptrs = instruction_args;
  1499. GET_INSTRUCTION_ARG(dst, argc);
  1500. Callable::CallError err;
  1501. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1502. #ifdef DEBUG_ENABLED
  1503. if (err.error != Callable::CallError::CALL_OK) {
  1504. err_text = _get_call_error("'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs, argc, *dst, err);
  1505. OPCODE_BREAK;
  1506. }
  1507. #endif
  1508. ip += 3;
  1509. }
  1510. DISPATCH_OPCODE;
  1511. OPCODE(OPCODE_CONSTRUCT_VALIDATED) {
  1512. LOAD_INSTRUCTION_ARGS
  1513. CHECK_SPACE(2 + instr_arg_count);
  1514. ip += instr_arg_count;
  1515. int argc = _code_ptr[ip + 1];
  1516. int constructor_idx = _code_ptr[ip + 2];
  1517. GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count);
  1518. Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx];
  1519. Variant **argptrs = instruction_args;
  1520. GET_INSTRUCTION_ARG(dst, argc);
  1521. constructor(dst, (const Variant **)argptrs);
  1522. ip += 3;
  1523. }
  1524. DISPATCH_OPCODE;
  1525. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1526. LOAD_INSTRUCTION_ARGS
  1527. CHECK_SPACE(1 + instr_arg_count);
  1528. ip += instr_arg_count;
  1529. int argc = _code_ptr[ip + 1];
  1530. Array array;
  1531. array.resize(argc);
  1532. for (int i = 0; i < argc; i++) {
  1533. array[i] = *(instruction_args[i]);
  1534. }
  1535. GET_INSTRUCTION_ARG(dst, argc);
  1536. *dst = Variant(); // Clear potential previous typed array.
  1537. *dst = array;
  1538. ip += 2;
  1539. }
  1540. DISPATCH_OPCODE;
  1541. OPCODE(OPCODE_CONSTRUCT_TYPED_ARRAY) {
  1542. LOAD_INSTRUCTION_ARGS
  1543. CHECK_SPACE(3 + instr_arg_count);
  1544. ip += instr_arg_count;
  1545. int argc = _code_ptr[ip + 1];
  1546. GET_INSTRUCTION_ARG(script_type, argc + 1);
  1547. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 2];
  1548. int native_type_idx = _code_ptr[ip + 3];
  1549. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  1550. const StringName native_type = _global_names_ptr[native_type_idx];
  1551. Array array;
  1552. array.resize(argc);
  1553. for (int i = 0; i < argc; i++) {
  1554. array[i] = *(instruction_args[i]);
  1555. }
  1556. GET_INSTRUCTION_ARG(dst, argc);
  1557. *dst = Variant(); // Clear potential previous typed array.
  1558. *dst = Array(array, builtin_type, native_type, *script_type);
  1559. ip += 4;
  1560. }
  1561. DISPATCH_OPCODE;
  1562. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1563. LOAD_INSTRUCTION_ARGS
  1564. CHECK_SPACE(2 + instr_arg_count);
  1565. ip += instr_arg_count;
  1566. int argc = _code_ptr[ip + 1];
  1567. Dictionary dict;
  1568. for (int i = 0; i < argc; i++) {
  1569. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1570. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1571. dict[*k] = *v;
  1572. }
  1573. GET_INSTRUCTION_ARG(dst, argc * 2);
  1574. *dst = Variant(); // Clear potential previous typed dictionary.
  1575. *dst = dict;
  1576. ip += 2;
  1577. }
  1578. DISPATCH_OPCODE;
  1579. OPCODE(OPCODE_CONSTRUCT_TYPED_DICTIONARY) {
  1580. LOAD_INSTRUCTION_ARGS
  1581. CHECK_SPACE(6 + instr_arg_count);
  1582. ip += instr_arg_count;
  1583. int argc = _code_ptr[ip + 1];
  1584. GET_INSTRUCTION_ARG(key_script_type, argc * 2 + 1);
  1585. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 2];
  1586. int key_native_type_idx = _code_ptr[ip + 3];
  1587. GD_ERR_BREAK(key_native_type_idx < 0 || key_native_type_idx >= _global_names_count);
  1588. const StringName key_native_type = _global_names_ptr[key_native_type_idx];
  1589. GET_INSTRUCTION_ARG(value_script_type, argc * 2 + 2);
  1590. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 4];
  1591. int value_native_type_idx = _code_ptr[ip + 5];
  1592. GD_ERR_BREAK(value_native_type_idx < 0 || value_native_type_idx >= _global_names_count);
  1593. const StringName value_native_type = _global_names_ptr[value_native_type_idx];
  1594. Dictionary dict;
  1595. for (int i = 0; i < argc; i++) {
  1596. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1597. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1598. dict[*k] = *v;
  1599. }
  1600. GET_INSTRUCTION_ARG(dst, argc * 2);
  1601. *dst = Variant(); // Clear potential previous typed dictionary.
  1602. *dst = Dictionary(dict, key_builtin_type, key_native_type, *key_script_type, value_builtin_type, value_native_type, *value_script_type);
  1603. ip += 6;
  1604. }
  1605. DISPATCH_OPCODE;
  1606. OPCODE(OPCODE_CALL_ASYNC)
  1607. OPCODE(OPCODE_CALL_RETURN)
  1608. OPCODE(OPCODE_CALL) {
  1609. bool call_ret = (_code_ptr[ip]) != OPCODE_CALL;
  1610. #ifdef DEBUG_ENABLED
  1611. bool call_async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  1612. #endif
  1613. LOAD_INSTRUCTION_ARGS
  1614. CHECK_SPACE(3 + instr_arg_count);
  1615. ip += instr_arg_count;
  1616. int argc = _code_ptr[ip + 1];
  1617. GD_ERR_BREAK(argc < 0);
  1618. int methodname_idx = _code_ptr[ip + 2];
  1619. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1620. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1621. GET_INSTRUCTION_ARG(base, argc);
  1622. Variant **argptrs = instruction_args;
  1623. #ifdef DEBUG_ENABLED
  1624. uint64_t call_time = 0;
  1625. if (GDScriptLanguage::get_singleton()->profiling) {
  1626. call_time = OS::get_singleton()->get_ticks_usec();
  1627. }
  1628. Variant::Type base_type = base->get_type();
  1629. Object *base_obj = base->get_validated_object();
  1630. StringName base_class = base_obj ? base_obj->get_class_name() : StringName();
  1631. #endif
  1632. Variant temp_ret;
  1633. Callable::CallError err;
  1634. if (call_ret) {
  1635. GET_INSTRUCTION_ARG(ret, argc + 1);
  1636. base->callp(*methodname, (const Variant **)argptrs, argc, temp_ret, err);
  1637. *ret = temp_ret;
  1638. #ifdef DEBUG_ENABLED
  1639. if (ret->get_type() == Variant::NIL) {
  1640. if (base_type == Variant::OBJECT) {
  1641. if (base_obj) {
  1642. MethodBind *method = ClassDB::get_method(base_class, *methodname);
  1643. if (*methodname == CoreStringName(free_) || (method && !method->has_return())) {
  1644. err_text = R"(Trying to get a return value of a method that returns "void")";
  1645. OPCODE_BREAK;
  1646. }
  1647. }
  1648. } else if (Variant::has_builtin_method(base_type, *methodname) && !Variant::has_builtin_method_return_value(base_type, *methodname)) {
  1649. err_text = R"(Trying to get a return value of a method that returns "void")";
  1650. OPCODE_BREAK;
  1651. }
  1652. }
  1653. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1654. // Check if getting a function state without await.
  1655. bool was_freed = false;
  1656. Object *obj = ret->get_validated_object_with_check(was_freed);
  1657. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1658. err_text = R"(Trying to call an async function without "await".)";
  1659. OPCODE_BREAK;
  1660. }
  1661. }
  1662. #endif
  1663. } else {
  1664. base->callp(*methodname, (const Variant **)argptrs, argc, temp_ret, err);
  1665. }
  1666. #ifdef DEBUG_ENABLED
  1667. if (GDScriptLanguage::get_singleton()->profiling) {
  1668. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1669. if (GDScriptLanguage::get_singleton()->profile_native_calls && _profile_count_as_native(base_obj, *methodname)) {
  1670. _profile_native_call(t_taken, *methodname, base_class);
  1671. }
  1672. function_call_time += t_taken;
  1673. }
  1674. if (err.error != Callable::CallError::CALL_OK) {
  1675. String methodstr = *methodname;
  1676. String basestr = _get_var_type(base);
  1677. bool is_callable = false;
  1678. if (methodstr == "call") {
  1679. if (argc >= 1 && base->get_type() != Variant::CALLABLE) {
  1680. methodstr = String(*argptrs[0]) + " (via call)";
  1681. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1682. err.argument += 1;
  1683. }
  1684. } else {
  1685. methodstr = base->operator String() + " (Callable)";
  1686. is_callable = true;
  1687. }
  1688. } else if (methodstr == "free") {
  1689. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1690. if (base->is_ref_counted()) {
  1691. err_text = "Attempted to free a RefCounted object.";
  1692. OPCODE_BREAK;
  1693. } else if (base->get_type() == Variant::OBJECT) {
  1694. err_text = "Attempted to free a locked object (calling or emitting).";
  1695. OPCODE_BREAK;
  1696. }
  1697. }
  1698. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1699. if (argc >= 1) {
  1700. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1701. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1702. err.argument += 1;
  1703. }
  1704. }
  1705. }
  1706. if (is_callable) {
  1707. err_text = _get_callable_call_error(vformat("function '%s'", methodstr), *base, (const Variant **)argptrs, argc, temp_ret, err);
  1708. } else {
  1709. err_text = _get_call_error(vformat("function '%s' in base '%s'", methodstr, basestr), (const Variant **)argptrs, argc, temp_ret, err);
  1710. }
  1711. OPCODE_BREAK;
  1712. }
  1713. #endif // DEBUG_ENABLED
  1714. ip += 3;
  1715. }
  1716. DISPATCH_OPCODE;
  1717. OPCODE(OPCODE_CALL_METHOD_BIND)
  1718. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1719. bool call_ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  1720. LOAD_INSTRUCTION_ARGS
  1721. CHECK_SPACE(3 + instr_arg_count);
  1722. ip += instr_arg_count;
  1723. int argc = _code_ptr[ip + 1];
  1724. GD_ERR_BREAK(argc < 0);
  1725. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1726. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1727. GET_INSTRUCTION_ARG(base, argc);
  1728. #ifdef DEBUG_ENABLED
  1729. bool freed = false;
  1730. Object *base_obj = base->get_validated_object_with_check(freed);
  1731. if (freed) {
  1732. err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
  1733. OPCODE_BREAK;
  1734. } else if (!base_obj) {
  1735. err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
  1736. OPCODE_BREAK;
  1737. }
  1738. #else
  1739. Object *base_obj = base->operator Object *();
  1740. #endif
  1741. Variant **argptrs = instruction_args;
  1742. #ifdef DEBUG_ENABLED
  1743. uint64_t call_time = 0;
  1744. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1745. call_time = OS::get_singleton()->get_ticks_usec();
  1746. }
  1747. #endif
  1748. Variant temp_ret;
  1749. Callable::CallError err;
  1750. if (call_ret) {
  1751. GET_INSTRUCTION_ARG(ret, argc + 1);
  1752. temp_ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1753. *ret = temp_ret;
  1754. } else {
  1755. temp_ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1756. }
  1757. #ifdef DEBUG_ENABLED
  1758. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1759. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1760. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1761. function_call_time += t_taken;
  1762. }
  1763. if (err.error != Callable::CallError::CALL_OK) {
  1764. String methodstr = method->get_name();
  1765. String basestr = _get_var_type(base);
  1766. if (methodstr == "call") {
  1767. if (argc >= 1) {
  1768. methodstr = String(*argptrs[0]) + " (via call)";
  1769. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1770. err.argument += 1;
  1771. }
  1772. }
  1773. } else if (methodstr == "free") {
  1774. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1775. if (base->is_ref_counted()) {
  1776. err_text = "Attempted to free a RefCounted object.";
  1777. OPCODE_BREAK;
  1778. } else if (base->get_type() == Variant::OBJECT) {
  1779. err_text = "Attempted to free a locked object (calling or emitting).";
  1780. OPCODE_BREAK;
  1781. }
  1782. }
  1783. }
  1784. err_text = _get_call_error("function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs, argc, temp_ret, err);
  1785. OPCODE_BREAK;
  1786. }
  1787. #endif
  1788. ip += 3;
  1789. }
  1790. DISPATCH_OPCODE;
  1791. OPCODE(OPCODE_CALL_BUILTIN_STATIC) {
  1792. LOAD_INSTRUCTION_ARGS
  1793. CHECK_SPACE(4 + instr_arg_count);
  1794. ip += instr_arg_count;
  1795. GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= Variant::VARIANT_MAX);
  1796. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 1];
  1797. int methodname_idx = _code_ptr[ip + 2];
  1798. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1799. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1800. int argc = _code_ptr[ip + 3];
  1801. GD_ERR_BREAK(argc < 0);
  1802. GET_INSTRUCTION_ARG(ret, argc);
  1803. const Variant **argptrs = const_cast<const Variant **>(instruction_args);
  1804. Callable::CallError err;
  1805. Variant::call_static(builtin_type, *methodname, argptrs, argc, *ret, err);
  1806. #ifdef DEBUG_ENABLED
  1807. if (err.error != Callable::CallError::CALL_OK) {
  1808. err_text = _get_call_error("static function '" + methodname->operator String() + "' in type '" + Variant::get_type_name(builtin_type) + "'", argptrs, argc, *ret, err);
  1809. OPCODE_BREAK;
  1810. }
  1811. #endif
  1812. ip += 4;
  1813. }
  1814. DISPATCH_OPCODE;
  1815. OPCODE(OPCODE_CALL_NATIVE_STATIC) {
  1816. LOAD_INSTRUCTION_ARGS
  1817. CHECK_SPACE(3 + instr_arg_count);
  1818. ip += instr_arg_count;
  1819. GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= _methods_count);
  1820. MethodBind *method = _methods_ptr[_code_ptr[ip + 1]];
  1821. int argc = _code_ptr[ip + 2];
  1822. GD_ERR_BREAK(argc < 0);
  1823. GET_INSTRUCTION_ARG(ret, argc);
  1824. const Variant **argptrs = const_cast<const Variant **>(instruction_args);
  1825. #ifdef DEBUG_ENABLED
  1826. uint64_t call_time = 0;
  1827. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1828. call_time = OS::get_singleton()->get_ticks_usec();
  1829. }
  1830. #endif
  1831. Callable::CallError err;
  1832. *ret = method->call(nullptr, argptrs, argc, err);
  1833. #ifdef DEBUG_ENABLED
  1834. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1835. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1836. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1837. function_call_time += t_taken;
  1838. }
  1839. #endif
  1840. if (err.error != Callable::CallError::CALL_OK) {
  1841. err_text = _get_call_error("static function '" + method->get_name().operator String() + "' in type '" + method->get_instance_class().operator String() + "'", argptrs, argc, *ret, err);
  1842. OPCODE_BREAK;
  1843. }
  1844. ip += 3;
  1845. }
  1846. DISPATCH_OPCODE;
  1847. OPCODE(OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN) {
  1848. LOAD_INSTRUCTION_ARGS
  1849. CHECK_SPACE(3 + instr_arg_count);
  1850. ip += instr_arg_count;
  1851. int argc = _code_ptr[ip + 1];
  1852. GD_ERR_BREAK(argc < 0);
  1853. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1854. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1855. Variant **argptrs = instruction_args;
  1856. #ifdef DEBUG_ENABLED
  1857. uint64_t call_time = 0;
  1858. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1859. call_time = OS::get_singleton()->get_ticks_usec();
  1860. }
  1861. #endif
  1862. GET_INSTRUCTION_ARG(ret, argc);
  1863. method->validated_call(nullptr, (const Variant **)argptrs, ret);
  1864. #ifdef DEBUG_ENABLED
  1865. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1866. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1867. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1868. function_call_time += t_taken;
  1869. }
  1870. #endif
  1871. ip += 3;
  1872. }
  1873. DISPATCH_OPCODE;
  1874. OPCODE(OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN) {
  1875. LOAD_INSTRUCTION_ARGS
  1876. CHECK_SPACE(3 + instr_arg_count);
  1877. ip += instr_arg_count;
  1878. int argc = _code_ptr[ip + 1];
  1879. GD_ERR_BREAK(argc < 0);
  1880. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1881. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1882. Variant **argptrs = instruction_args;
  1883. #ifdef DEBUG_ENABLED
  1884. uint64_t call_time = 0;
  1885. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1886. call_time = OS::get_singleton()->get_ticks_usec();
  1887. }
  1888. #endif
  1889. GET_INSTRUCTION_ARG(ret, argc);
  1890. VariantInternal::initialize(ret, Variant::NIL);
  1891. method->validated_call(nullptr, (const Variant **)argptrs, nullptr);
  1892. #ifdef DEBUG_ENABLED
  1893. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1894. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1895. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1896. function_call_time += t_taken;
  1897. }
  1898. #endif
  1899. ip += 3;
  1900. }
  1901. DISPATCH_OPCODE;
  1902. OPCODE(OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN) {
  1903. LOAD_INSTRUCTION_ARGS
  1904. CHECK_SPACE(3 + instr_arg_count);
  1905. ip += instr_arg_count;
  1906. int argc = _code_ptr[ip + 1];
  1907. GD_ERR_BREAK(argc < 0);
  1908. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1909. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1910. GET_INSTRUCTION_ARG(base, argc);
  1911. #ifdef DEBUG_ENABLED
  1912. bool freed = false;
  1913. Object *base_obj = base->get_validated_object_with_check(freed);
  1914. if (freed) {
  1915. err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
  1916. OPCODE_BREAK;
  1917. } else if (!base_obj) {
  1918. err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
  1919. OPCODE_BREAK;
  1920. }
  1921. #else
  1922. Object *base_obj = *VariantInternal::get_object(base);
  1923. #endif
  1924. Variant **argptrs = instruction_args;
  1925. #ifdef DEBUG_ENABLED
  1926. uint64_t call_time = 0;
  1927. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1928. call_time = OS::get_singleton()->get_ticks_usec();
  1929. }
  1930. #endif
  1931. GET_INSTRUCTION_ARG(ret, argc + 1);
  1932. method->validated_call(base_obj, (const Variant **)argptrs, ret);
  1933. #ifdef DEBUG_ENABLED
  1934. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1935. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1936. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1937. function_call_time += t_taken;
  1938. }
  1939. #endif
  1940. ip += 3;
  1941. }
  1942. DISPATCH_OPCODE;
  1943. OPCODE(OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN) {
  1944. LOAD_INSTRUCTION_ARGS
  1945. CHECK_SPACE(3 + instr_arg_count);
  1946. ip += instr_arg_count;
  1947. int argc = _code_ptr[ip + 1];
  1948. GD_ERR_BREAK(argc < 0);
  1949. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1950. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1951. GET_INSTRUCTION_ARG(base, argc);
  1952. #ifdef DEBUG_ENABLED
  1953. bool freed = false;
  1954. Object *base_obj = base->get_validated_object_with_check(freed);
  1955. if (freed) {
  1956. err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
  1957. OPCODE_BREAK;
  1958. } else if (!base_obj) {
  1959. err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
  1960. OPCODE_BREAK;
  1961. }
  1962. #else
  1963. Object *base_obj = *VariantInternal::get_object(base);
  1964. #endif
  1965. Variant **argptrs = instruction_args;
  1966. #ifdef DEBUG_ENABLED
  1967. uint64_t call_time = 0;
  1968. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1969. call_time = OS::get_singleton()->get_ticks_usec();
  1970. }
  1971. #endif
  1972. GET_INSTRUCTION_ARG(ret, argc + 1);
  1973. VariantInternal::initialize(ret, Variant::NIL);
  1974. method->validated_call(base_obj, (const Variant **)argptrs, nullptr);
  1975. #ifdef DEBUG_ENABLED
  1976. if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) {
  1977. uint64_t t_taken = OS::get_singleton()->get_ticks_usec() - call_time;
  1978. _profile_native_call(t_taken, method->get_name(), method->get_instance_class());
  1979. function_call_time += t_taken;
  1980. }
  1981. #endif
  1982. ip += 3;
  1983. }
  1984. DISPATCH_OPCODE;
  1985. OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
  1986. LOAD_INSTRUCTION_ARGS
  1987. CHECK_SPACE(3 + instr_arg_count);
  1988. ip += instr_arg_count;
  1989. int argc = _code_ptr[ip + 1];
  1990. GD_ERR_BREAK(argc < 0);
  1991. GET_INSTRUCTION_ARG(base, argc);
  1992. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
  1993. Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
  1994. Variant **argptrs = instruction_args;
  1995. GET_INSTRUCTION_ARG(ret, argc + 1);
  1996. method(base, (const Variant **)argptrs, argc, ret);
  1997. ip += 3;
  1998. }
  1999. DISPATCH_OPCODE;
  2000. OPCODE(OPCODE_CALL_UTILITY) {
  2001. LOAD_INSTRUCTION_ARGS
  2002. CHECK_SPACE(3 + instr_arg_count);
  2003. ip += instr_arg_count;
  2004. int argc = _code_ptr[ip + 1];
  2005. GD_ERR_BREAK(argc < 0);
  2006. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count);
  2007. StringName function = _global_names_ptr[_code_ptr[ip + 2]];
  2008. Variant **argptrs = instruction_args;
  2009. GET_INSTRUCTION_ARG(dst, argc);
  2010. Callable::CallError err;
  2011. Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err);
  2012. #ifdef DEBUG_ENABLED
  2013. if (err.error != Callable::CallError::CALL_OK) {
  2014. String methodstr = function;
  2015. if (dst->get_type() == Variant::STRING && !dst->operator String().is_empty()) {
  2016. // Call provided error string.
  2017. err_text = vformat(R"*(Error calling utility function "%s()": %s)*", methodstr, *dst);
  2018. } else {
  2019. err_text = _get_call_error(vformat(R"*(utility function "%s()")*", methodstr), (const Variant **)argptrs, argc, *dst, err);
  2020. }
  2021. OPCODE_BREAK;
  2022. }
  2023. #endif
  2024. ip += 3;
  2025. }
  2026. DISPATCH_OPCODE;
  2027. OPCODE(OPCODE_CALL_UTILITY_VALIDATED) {
  2028. LOAD_INSTRUCTION_ARGS
  2029. CHECK_SPACE(3 + instr_arg_count);
  2030. ip += instr_arg_count;
  2031. int argc = _code_ptr[ip + 1];
  2032. GD_ERR_BREAK(argc < 0);
  2033. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count);
  2034. Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]];
  2035. Variant **argptrs = instruction_args;
  2036. GET_INSTRUCTION_ARG(dst, argc);
  2037. function(dst, (const Variant **)argptrs, argc);
  2038. ip += 3;
  2039. }
  2040. DISPATCH_OPCODE;
  2041. OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) {
  2042. LOAD_INSTRUCTION_ARGS
  2043. CHECK_SPACE(3 + instr_arg_count);
  2044. ip += instr_arg_count;
  2045. int argc = _code_ptr[ip + 1];
  2046. GD_ERR_BREAK(argc < 0);
  2047. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count);
  2048. GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]];
  2049. Variant **argptrs = instruction_args;
  2050. GET_INSTRUCTION_ARG(dst, argc);
  2051. Callable::CallError err;
  2052. function(dst, (const Variant **)argptrs, argc, err);
  2053. #ifdef DEBUG_ENABLED
  2054. if (err.error != Callable::CallError::CALL_OK) {
  2055. String methodstr = gds_utilities_names[_code_ptr[ip + 2]];
  2056. if (dst->get_type() == Variant::STRING && !dst->operator String().is_empty()) {
  2057. // Call provided error string.
  2058. err_text = vformat(R"*(Error calling GDScript utility function "%s()": %s)*", methodstr, *dst);
  2059. } else {
  2060. err_text = _get_call_error(vformat(R"*(GDScript utility function "%s()")*", methodstr), (const Variant **)argptrs, argc, *dst, err);
  2061. }
  2062. OPCODE_BREAK;
  2063. }
  2064. #endif
  2065. ip += 3;
  2066. }
  2067. DISPATCH_OPCODE;
  2068. OPCODE(OPCODE_CALL_SELF_BASE) {
  2069. LOAD_INSTRUCTION_ARGS
  2070. CHECK_SPACE(3 + instr_arg_count);
  2071. ip += instr_arg_count;
  2072. int argc = _code_ptr[ip + 1];
  2073. GD_ERR_BREAK(argc < 0);
  2074. int self_fun = _code_ptr[ip + 2];
  2075. #ifdef DEBUG_ENABLED
  2076. if (self_fun < 0 || self_fun >= _global_names_count) {
  2077. err_text = "compiler bug, function name not found";
  2078. OPCODE_BREAK;
  2079. }
  2080. #endif
  2081. const StringName *methodname = &_global_names_ptr[self_fun];
  2082. Variant **argptrs = instruction_args;
  2083. GET_INSTRUCTION_ARG(dst, argc);
  2084. const GDScript *gds = _script;
  2085. HashMap<StringName, GDScriptFunction *>::ConstIterator E;
  2086. while (gds->base.ptr()) {
  2087. gds = gds->base.ptr();
  2088. E = gds->member_functions.find(*methodname);
  2089. if (E) {
  2090. break;
  2091. }
  2092. }
  2093. Callable::CallError err;
  2094. if (E) {
  2095. *dst = E->value->call(p_instance, (const Variant **)argptrs, argc, err);
  2096. } else if (gds->native.ptr()) {
  2097. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  2098. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  2099. if (!mb) {
  2100. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  2101. } else {
  2102. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  2103. }
  2104. } else {
  2105. err.error = Callable::CallError::CALL_OK;
  2106. }
  2107. } else {
  2108. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  2109. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  2110. } else {
  2111. err.error = Callable::CallError::CALL_OK;
  2112. }
  2113. }
  2114. if (err.error != Callable::CallError::CALL_OK) {
  2115. String methodstr = *methodname;
  2116. err_text = _get_call_error("function '" + methodstr + "'", (const Variant **)argptrs, argc, *dst, err);
  2117. OPCODE_BREAK;
  2118. }
  2119. ip += 3;
  2120. }
  2121. DISPATCH_OPCODE;
  2122. OPCODE(OPCODE_AWAIT) {
  2123. CHECK_SPACE(2);
  2124. // Do the one-shot connect.
  2125. GET_VARIANT_PTR(argobj, 0);
  2126. Signal sig;
  2127. bool is_signal = true;
  2128. {
  2129. Variant result = *argobj;
  2130. if (argobj->get_type() == Variant::OBJECT) {
  2131. bool was_freed = false;
  2132. Object *obj = argobj->get_validated_object_with_check(was_freed);
  2133. if (was_freed) {
  2134. err_text = "Trying to await on a freed object.";
  2135. OPCODE_BREAK;
  2136. }
  2137. // Is this even possible to be null at this point?
  2138. if (obj) {
  2139. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  2140. result = Signal(obj, "completed");
  2141. }
  2142. }
  2143. }
  2144. if (result.get_type() != Variant::SIGNAL) {
  2145. // Not async, return immediately using the target from OPCODE_AWAIT_RESUME.
  2146. GET_VARIANT_PTR(target, 2);
  2147. *target = result;
  2148. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  2149. is_signal = false;
  2150. } else {
  2151. sig = result;
  2152. }
  2153. }
  2154. if (is_signal) {
  2155. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  2156. gdfs->function = this;
  2157. gdfs->state.stack.resize(alloca_size);
  2158. // First `FIXED_ADDRESSES_MAX` stack addresses are special, so we just skip them here.
  2159. for (int i = FIXED_ADDRESSES_MAX; i < _stack_size; i++) {
  2160. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  2161. }
  2162. gdfs->state.stack_size = _stack_size;
  2163. gdfs->state.ip = ip + 2;
  2164. gdfs->state.line = line;
  2165. gdfs->state.script = _script;
  2166. {
  2167. MutexLock lock(GDScriptLanguage::get_singleton()->mutex);
  2168. _script->pending_func_states.add(&gdfs->scripts_list);
  2169. if (p_instance) {
  2170. gdfs->state.instance = p_instance;
  2171. p_instance->pending_func_states.add(&gdfs->instances_list);
  2172. } else {
  2173. gdfs->state.instance = nullptr;
  2174. }
  2175. }
  2176. #ifdef DEBUG_ENABLED
  2177. gdfs->state.function_name = name;
  2178. gdfs->state.script_path = _script->get_script_path();
  2179. #endif
  2180. gdfs->state.defarg = defarg;
  2181. gdfs->function = this;
  2182. retvalue = gdfs;
  2183. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback").bind(retvalue), Object::CONNECT_ONE_SHOT);
  2184. if (err != OK) {
  2185. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  2186. OPCODE_BREAK;
  2187. }
  2188. awaited = true;
  2189. #ifdef DEBUG_ENABLED
  2190. exit_ok = true;
  2191. #endif
  2192. OPCODE_BREAK;
  2193. }
  2194. }
  2195. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  2196. OPCODE(OPCODE_AWAIT_RESUME) {
  2197. CHECK_SPACE(2);
  2198. #ifdef DEBUG_ENABLED
  2199. if (!p_state) {
  2200. err_text = ("Invalid Resume (bug?)");
  2201. OPCODE_BREAK;
  2202. }
  2203. #endif
  2204. GET_VARIANT_PTR(result, 0);
  2205. *result = p_state->result;
  2206. ip += 2;
  2207. }
  2208. DISPATCH_OPCODE;
  2209. OPCODE(OPCODE_CREATE_LAMBDA) {
  2210. LOAD_INSTRUCTION_ARGS
  2211. CHECK_SPACE(2 + instr_arg_count);
  2212. ip += instr_arg_count;
  2213. int captures_count = _code_ptr[ip + 1];
  2214. GD_ERR_BREAK(captures_count < 0);
  2215. int lambda_index = _code_ptr[ip + 2];
  2216. GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
  2217. GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
  2218. Vector<Variant> captures;
  2219. captures.resize(captures_count);
  2220. for (int i = 0; i < captures_count; i++) {
  2221. GET_INSTRUCTION_ARG(arg, i);
  2222. captures.write[i] = *arg;
  2223. }
  2224. GDScriptLambdaCallable *callable = memnew(GDScriptLambdaCallable(Ref<GDScript>(script), lambda, captures));
  2225. GET_INSTRUCTION_ARG(result, captures_count);
  2226. *result = Callable(callable);
  2227. ip += 3;
  2228. }
  2229. DISPATCH_OPCODE;
  2230. OPCODE(OPCODE_CREATE_SELF_LAMBDA) {
  2231. LOAD_INSTRUCTION_ARGS
  2232. CHECK_SPACE(2 + instr_arg_count);
  2233. GD_ERR_BREAK(p_instance == nullptr);
  2234. ip += instr_arg_count;
  2235. int captures_count = _code_ptr[ip + 1];
  2236. GD_ERR_BREAK(captures_count < 0);
  2237. int lambda_index = _code_ptr[ip + 2];
  2238. GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
  2239. GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
  2240. Vector<Variant> captures;
  2241. captures.resize(captures_count);
  2242. for (int i = 0; i < captures_count; i++) {
  2243. GET_INSTRUCTION_ARG(arg, i);
  2244. captures.write[i] = *arg;
  2245. }
  2246. GDScriptLambdaSelfCallable *callable;
  2247. if (Object::cast_to<RefCounted>(p_instance->owner)) {
  2248. callable = memnew(GDScriptLambdaSelfCallable(Ref<RefCounted>(Object::cast_to<RefCounted>(p_instance->owner)), lambda, captures));
  2249. } else {
  2250. callable = memnew(GDScriptLambdaSelfCallable(p_instance->owner, lambda, captures));
  2251. }
  2252. GET_INSTRUCTION_ARG(result, captures_count);
  2253. *result = Callable(callable);
  2254. ip += 3;
  2255. }
  2256. DISPATCH_OPCODE;
  2257. OPCODE(OPCODE_JUMP) {
  2258. CHECK_SPACE(2);
  2259. int to = _code_ptr[ip + 1];
  2260. GD_ERR_BREAK(to < 0 || to > _code_size);
  2261. ip = to;
  2262. }
  2263. DISPATCH_OPCODE;
  2264. OPCODE(OPCODE_JUMP_IF) {
  2265. CHECK_SPACE(3);
  2266. GET_VARIANT_PTR(test, 0);
  2267. bool result = test->booleanize();
  2268. if (result) {
  2269. int to = _code_ptr[ip + 2];
  2270. GD_ERR_BREAK(to < 0 || to > _code_size);
  2271. ip = to;
  2272. } else {
  2273. ip += 3;
  2274. }
  2275. }
  2276. DISPATCH_OPCODE;
  2277. OPCODE(OPCODE_JUMP_IF_NOT) {
  2278. CHECK_SPACE(3);
  2279. GET_VARIANT_PTR(test, 0);
  2280. bool result = test->booleanize();
  2281. if (!result) {
  2282. int to = _code_ptr[ip + 2];
  2283. GD_ERR_BREAK(to < 0 || to > _code_size);
  2284. ip = to;
  2285. } else {
  2286. ip += 3;
  2287. }
  2288. }
  2289. DISPATCH_OPCODE;
  2290. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  2291. CHECK_SPACE(2);
  2292. ip = _default_arg_ptr[defarg];
  2293. }
  2294. DISPATCH_OPCODE;
  2295. OPCODE(OPCODE_JUMP_IF_SHARED) {
  2296. CHECK_SPACE(3);
  2297. GET_VARIANT_PTR(val, 0);
  2298. if (val->is_shared()) {
  2299. int to = _code_ptr[ip + 2];
  2300. GD_ERR_BREAK(to < 0 || to > _code_size);
  2301. ip = to;
  2302. } else {
  2303. ip += 3;
  2304. }
  2305. }
  2306. DISPATCH_OPCODE;
  2307. OPCODE(OPCODE_RETURN) {
  2308. CHECK_SPACE(2);
  2309. GET_VARIANT_PTR(r, 0);
  2310. retvalue = *r;
  2311. #ifdef DEBUG_ENABLED
  2312. exit_ok = true;
  2313. #endif
  2314. OPCODE_BREAK;
  2315. }
  2316. OPCODE(OPCODE_RETURN_TYPED_BUILTIN) {
  2317. CHECK_SPACE(3);
  2318. GET_VARIANT_PTR(r, 0);
  2319. Variant::Type ret_type = (Variant::Type)_code_ptr[ip + 2];
  2320. GD_ERR_BREAK(ret_type < 0 || ret_type >= Variant::VARIANT_MAX);
  2321. if (r->get_type() != ret_type) {
  2322. if (Variant::can_convert_strict(r->get_type(), ret_type)) {
  2323. Callable::CallError ce;
  2324. Variant::construct(ret_type, retvalue, const_cast<const Variant **>(&r), 1, ce);
  2325. } else {
  2326. #ifdef DEBUG_ENABLED
  2327. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2328. Variant::get_type_name(r->get_type()), Variant::get_type_name(ret_type));
  2329. #endif // DEBUG_ENABLED
  2330. // Construct a base type anyway so type constraints are met.
  2331. Callable::CallError ce;
  2332. Variant::construct(ret_type, retvalue, nullptr, 0, ce);
  2333. OPCODE_BREAK;
  2334. }
  2335. } else {
  2336. retvalue = *r;
  2337. }
  2338. #ifdef DEBUG_ENABLED
  2339. exit_ok = true;
  2340. #endif // DEBUG_ENABLED
  2341. OPCODE_BREAK;
  2342. }
  2343. OPCODE(OPCODE_RETURN_TYPED_ARRAY) {
  2344. CHECK_SPACE(5);
  2345. GET_VARIANT_PTR(r, 0);
  2346. GET_VARIANT_PTR(script_type, 1);
  2347. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
  2348. int native_type_idx = _code_ptr[ip + 4];
  2349. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  2350. const StringName native_type = _global_names_ptr[native_type_idx];
  2351. if (r->get_type() != Variant::ARRAY) {
  2352. #ifdef DEBUG_ENABLED
  2353. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "Array[%s]".)",
  2354. Variant::get_type_name(r->get_type()), Variant::get_type_name(builtin_type));
  2355. #endif
  2356. OPCODE_BREAK;
  2357. }
  2358. Array *array = VariantInternal::get_array(r);
  2359. if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
  2360. #ifdef DEBUG_ENABLED
  2361. err_text = vformat(R"(Trying to return an array of type "%s" where expected return type is "Array[%s]".)",
  2362. _get_var_type(r), _get_element_type(builtin_type, native_type, *script_type));
  2363. #endif // DEBUG_ENABLED
  2364. OPCODE_BREAK;
  2365. }
  2366. retvalue = *array;
  2367. #ifdef DEBUG_ENABLED
  2368. exit_ok = true;
  2369. #endif // DEBUG_ENABLED
  2370. OPCODE_BREAK;
  2371. }
  2372. OPCODE(OPCODE_RETURN_TYPED_DICTIONARY) {
  2373. CHECK_SPACE(8);
  2374. GET_VARIANT_PTR(r, 0);
  2375. GET_VARIANT_PTR(key_script_type, 1);
  2376. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 4];
  2377. int key_native_type_idx = _code_ptr[ip + 5];
  2378. GD_ERR_BREAK(key_native_type_idx < 0 || key_native_type_idx >= _global_names_count);
  2379. const StringName key_native_type = _global_names_ptr[key_native_type_idx];
  2380. GET_VARIANT_PTR(value_script_type, 2);
  2381. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 6];
  2382. int value_native_type_idx = _code_ptr[ip + 7];
  2383. GD_ERR_BREAK(value_native_type_idx < 0 || value_native_type_idx >= _global_names_count);
  2384. const StringName value_native_type = _global_names_ptr[value_native_type_idx];
  2385. if (r->get_type() != Variant::DICTIONARY) {
  2386. #ifdef DEBUG_ENABLED
  2387. err_text = vformat(R"(Trying to return a value of type "%s" where expected return type is "Dictionary[%s, %s]".)",
  2388. _get_var_type(r), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
  2389. _get_element_type(value_builtin_type, value_native_type, *value_script_type));
  2390. #endif // DEBUG_ENABLED
  2391. OPCODE_BREAK;
  2392. }
  2393. Dictionary *dictionary = VariantInternal::get_dictionary(r);
  2394. if (dictionary->get_typed_key_builtin() != ((uint32_t)key_builtin_type) || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
  2395. dictionary->get_typed_value_builtin() != ((uint32_t)value_builtin_type) || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
  2396. #ifdef DEBUG_ENABLED
  2397. err_text = vformat(R"(Trying to return a dictionary of type "%s" where expected return type is "Dictionary[%s, %s]".)",
  2398. _get_var_type(r), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
  2399. _get_element_type(value_builtin_type, value_native_type, *value_script_type));
  2400. #endif // DEBUG_ENABLED
  2401. OPCODE_BREAK;
  2402. }
  2403. retvalue = *dictionary;
  2404. #ifdef DEBUG_ENABLED
  2405. exit_ok = true;
  2406. #endif // DEBUG_ENABLED
  2407. OPCODE_BREAK;
  2408. }
  2409. OPCODE(OPCODE_RETURN_TYPED_NATIVE) {
  2410. CHECK_SPACE(3);
  2411. GET_VARIANT_PTR(r, 0);
  2412. GET_VARIANT_PTR(type, 1);
  2413. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  2414. GD_ERR_BREAK(!nc);
  2415. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  2416. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2417. Variant::get_type_name(r->get_type()), nc->get_name());
  2418. OPCODE_BREAK;
  2419. }
  2420. #ifdef DEBUG_ENABLED
  2421. bool freed = false;
  2422. Object *ret_obj = r->get_validated_object_with_check(freed);
  2423. if (freed) {
  2424. err_text = "Trying to return a previously freed instance.";
  2425. OPCODE_BREAK;
  2426. }
  2427. #else
  2428. Object *ret_obj = r->operator Object *();
  2429. #endif // DEBUG_ENABLED
  2430. if (ret_obj && !ClassDB::is_parent_class(ret_obj->get_class_name(), nc->get_name())) {
  2431. #ifdef DEBUG_ENABLED
  2432. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2433. ret_obj->get_class_name(), nc->get_name());
  2434. #endif // DEBUG_ENABLED
  2435. OPCODE_BREAK;
  2436. }
  2437. retvalue = *r;
  2438. #ifdef DEBUG_ENABLED
  2439. exit_ok = true;
  2440. #endif // DEBUG_ENABLED
  2441. OPCODE_BREAK;
  2442. }
  2443. OPCODE(OPCODE_RETURN_TYPED_SCRIPT) {
  2444. CHECK_SPACE(3);
  2445. GET_VARIANT_PTR(r, 0);
  2446. GET_VARIANT_PTR(type, 1);
  2447. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  2448. GD_ERR_BREAK(!base_type);
  2449. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  2450. #ifdef DEBUG_ENABLED
  2451. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2452. Variant::get_type_name(r->get_type()), GDScript::debug_get_script_name(Ref<Script>(base_type)));
  2453. #endif // DEBUG_ENABLED
  2454. OPCODE_BREAK;
  2455. }
  2456. #ifdef DEBUG_ENABLED
  2457. bool freed = false;
  2458. Object *ret_obj = r->get_validated_object_with_check(freed);
  2459. if (freed) {
  2460. err_text = "Trying to return a previously freed instance.";
  2461. OPCODE_BREAK;
  2462. }
  2463. #else
  2464. Object *ret_obj = r->operator Object *();
  2465. #endif // DEBUG_ENABLED
  2466. if (ret_obj) {
  2467. ScriptInstance *ret_inst = ret_obj->get_script_instance();
  2468. if (!ret_inst) {
  2469. #ifdef DEBUG_ENABLED
  2470. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2471. ret_obj->get_class_name(), GDScript::debug_get_script_name(Ref<GDScript>(base_type)));
  2472. #endif // DEBUG_ENABLED
  2473. OPCODE_BREAK;
  2474. }
  2475. Script *ret_type = ret_obj->get_script_instance()->get_script().ptr();
  2476. bool valid = false;
  2477. while (ret_type) {
  2478. if (ret_type == base_type) {
  2479. valid = true;
  2480. break;
  2481. }
  2482. ret_type = ret_type->get_base_script().ptr();
  2483. }
  2484. if (!valid) {
  2485. #ifdef DEBUG_ENABLED
  2486. err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",
  2487. GDScript::debug_get_script_name(ret_obj->get_script_instance()->get_script()), GDScript::debug_get_script_name(Ref<GDScript>(base_type)));
  2488. #endif // DEBUG_ENABLED
  2489. OPCODE_BREAK;
  2490. }
  2491. }
  2492. retvalue = *r;
  2493. #ifdef DEBUG_ENABLED
  2494. exit_ok = true;
  2495. #endif // DEBUG_ENABLED
  2496. OPCODE_BREAK;
  2497. }
  2498. OPCODE(OPCODE_ITERATE_BEGIN) {
  2499. CHECK_SPACE(8); // Space for this and a regular iterate.
  2500. GET_VARIANT_PTR(counter, 0);
  2501. GET_VARIANT_PTR(container, 1);
  2502. *counter = Variant();
  2503. bool valid;
  2504. if (!container->iter_init(*counter, valid)) {
  2505. #ifdef DEBUG_ENABLED
  2506. if (!valid) {
  2507. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2508. OPCODE_BREAK;
  2509. }
  2510. #endif
  2511. int jumpto = _code_ptr[ip + 4];
  2512. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2513. ip = jumpto;
  2514. } else {
  2515. GET_VARIANT_PTR(iterator, 2);
  2516. *iterator = container->iter_get(*counter, valid);
  2517. #ifdef DEBUG_ENABLED
  2518. if (!valid) {
  2519. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2520. OPCODE_BREAK;
  2521. }
  2522. #endif
  2523. ip += 5; // Skip regular iterate which is always next.
  2524. }
  2525. }
  2526. DISPATCH_OPCODE;
  2527. OPCODE(OPCODE_ITERATE_BEGIN_INT) {
  2528. CHECK_SPACE(8); // Check space for iterate instruction too.
  2529. GET_VARIANT_PTR(counter, 0);
  2530. GET_VARIANT_PTR(container, 1);
  2531. int64_t size = *VariantInternal::get_int(container);
  2532. VariantInternal::initialize(counter, Variant::INT);
  2533. *VariantInternal::get_int(counter) = 0;
  2534. if (size > 0) {
  2535. GET_VARIANT_PTR(iterator, 2);
  2536. VariantInternal::initialize(iterator, Variant::INT);
  2537. *VariantInternal::get_int(iterator) = 0;
  2538. // Skip regular iterate.
  2539. ip += 5;
  2540. } else {
  2541. // Jump to end of loop.
  2542. int jumpto = _code_ptr[ip + 4];
  2543. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2544. ip = jumpto;
  2545. }
  2546. }
  2547. DISPATCH_OPCODE;
  2548. OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) {
  2549. CHECK_SPACE(8); // Check space for iterate instruction too.
  2550. GET_VARIANT_PTR(counter, 0);
  2551. GET_VARIANT_PTR(container, 1);
  2552. double size = *VariantInternal::get_float(container);
  2553. VariantInternal::initialize(counter, Variant::FLOAT);
  2554. *VariantInternal::get_float(counter) = 0.0;
  2555. if (size > 0) {
  2556. GET_VARIANT_PTR(iterator, 2);
  2557. VariantInternal::initialize(iterator, Variant::FLOAT);
  2558. *VariantInternal::get_float(iterator) = 0;
  2559. // Skip regular iterate.
  2560. ip += 5;
  2561. } else {
  2562. // Jump to end of loop.
  2563. int jumpto = _code_ptr[ip + 4];
  2564. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2565. ip = jumpto;
  2566. }
  2567. }
  2568. DISPATCH_OPCODE;
  2569. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) {
  2570. CHECK_SPACE(8); // Check space for iterate instruction too.
  2571. GET_VARIANT_PTR(counter, 0);
  2572. GET_VARIANT_PTR(container, 1);
  2573. Vector2 *bounds = VariantInternal::get_vector2(container);
  2574. VariantInternal::initialize(counter, Variant::FLOAT);
  2575. *VariantInternal::get_float(counter) = bounds->x;
  2576. if (bounds->x < bounds->y) {
  2577. GET_VARIANT_PTR(iterator, 2);
  2578. VariantInternal::initialize(iterator, Variant::FLOAT);
  2579. *VariantInternal::get_float(iterator) = bounds->x;
  2580. // Skip regular iterate.
  2581. ip += 5;
  2582. } else {
  2583. // Jump to end of loop.
  2584. int jumpto = _code_ptr[ip + 4];
  2585. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2586. ip = jumpto;
  2587. }
  2588. }
  2589. DISPATCH_OPCODE;
  2590. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) {
  2591. CHECK_SPACE(8); // Check space for iterate instruction too.
  2592. GET_VARIANT_PTR(counter, 0);
  2593. GET_VARIANT_PTR(container, 1);
  2594. Vector2i *bounds = VariantInternal::get_vector2i(container);
  2595. VariantInternal::initialize(counter, Variant::FLOAT);
  2596. *VariantInternal::get_int(counter) = bounds->x;
  2597. if (bounds->x < bounds->y) {
  2598. GET_VARIANT_PTR(iterator, 2);
  2599. VariantInternal::initialize(iterator, Variant::INT);
  2600. *VariantInternal::get_int(iterator) = bounds->x;
  2601. // Skip regular iterate.
  2602. ip += 5;
  2603. } else {
  2604. // Jump to end of loop.
  2605. int jumpto = _code_ptr[ip + 4];
  2606. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2607. ip = jumpto;
  2608. }
  2609. }
  2610. DISPATCH_OPCODE;
  2611. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) {
  2612. CHECK_SPACE(8); // Check space for iterate instruction too.
  2613. GET_VARIANT_PTR(counter, 0);
  2614. GET_VARIANT_PTR(container, 1);
  2615. Vector3 *bounds = VariantInternal::get_vector3(container);
  2616. double from = bounds->x;
  2617. double to = bounds->y;
  2618. double step = bounds->z;
  2619. VariantInternal::initialize(counter, Variant::FLOAT);
  2620. *VariantInternal::get_float(counter) = from;
  2621. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2622. if (do_continue) {
  2623. GET_VARIANT_PTR(iterator, 2);
  2624. VariantInternal::initialize(iterator, Variant::FLOAT);
  2625. *VariantInternal::get_float(iterator) = from;
  2626. // Skip regular iterate.
  2627. ip += 5;
  2628. } else {
  2629. // Jump to end of loop.
  2630. int jumpto = _code_ptr[ip + 4];
  2631. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2632. ip = jumpto;
  2633. }
  2634. }
  2635. DISPATCH_OPCODE;
  2636. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) {
  2637. CHECK_SPACE(8); // Check space for iterate instruction too.
  2638. GET_VARIANT_PTR(counter, 0);
  2639. GET_VARIANT_PTR(container, 1);
  2640. Vector3i *bounds = VariantInternal::get_vector3i(container);
  2641. int64_t from = bounds->x;
  2642. int64_t to = bounds->y;
  2643. int64_t step = bounds->z;
  2644. VariantInternal::initialize(counter, Variant::INT);
  2645. *VariantInternal::get_int(counter) = from;
  2646. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2647. if (do_continue) {
  2648. GET_VARIANT_PTR(iterator, 2);
  2649. VariantInternal::initialize(iterator, Variant::INT);
  2650. *VariantInternal::get_int(iterator) = from;
  2651. // Skip regular iterate.
  2652. ip += 5;
  2653. } else {
  2654. // Jump to end of loop.
  2655. int jumpto = _code_ptr[ip + 4];
  2656. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2657. ip = jumpto;
  2658. }
  2659. }
  2660. DISPATCH_OPCODE;
  2661. OPCODE(OPCODE_ITERATE_BEGIN_STRING) {
  2662. CHECK_SPACE(8); // Check space for iterate instruction too.
  2663. GET_VARIANT_PTR(counter, 0);
  2664. GET_VARIANT_PTR(container, 1);
  2665. String *str = VariantInternal::get_string(container);
  2666. VariantInternal::initialize(counter, Variant::INT);
  2667. *VariantInternal::get_int(counter) = 0;
  2668. if (!str->is_empty()) {
  2669. GET_VARIANT_PTR(iterator, 2);
  2670. VariantInternal::initialize(iterator, Variant::STRING);
  2671. *VariantInternal::get_string(iterator) = str->substr(0, 1);
  2672. // Skip regular iterate.
  2673. ip += 5;
  2674. } else {
  2675. // Jump to end of loop.
  2676. int jumpto = _code_ptr[ip + 4];
  2677. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2678. ip = jumpto;
  2679. }
  2680. }
  2681. DISPATCH_OPCODE;
  2682. OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) {
  2683. CHECK_SPACE(8); // Check space for iterate instruction too.
  2684. GET_VARIANT_PTR(counter, 0);
  2685. GET_VARIANT_PTR(container, 1);
  2686. Dictionary *dict = VariantInternal::get_dictionary(container);
  2687. const Variant *next = dict->next(nullptr);
  2688. if (!dict->is_empty()) {
  2689. GET_VARIANT_PTR(iterator, 2);
  2690. *counter = *next;
  2691. *iterator = *next;
  2692. // Skip regular iterate.
  2693. ip += 5;
  2694. } else {
  2695. // Jump to end of loop.
  2696. int jumpto = _code_ptr[ip + 4];
  2697. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2698. ip = jumpto;
  2699. }
  2700. }
  2701. DISPATCH_OPCODE;
  2702. OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) {
  2703. CHECK_SPACE(8); // Check space for iterate instruction too.
  2704. GET_VARIANT_PTR(counter, 0);
  2705. GET_VARIANT_PTR(container, 1);
  2706. Array *array = VariantInternal::get_array(container);
  2707. VariantInternal::initialize(counter, Variant::INT);
  2708. *VariantInternal::get_int(counter) = 0;
  2709. if (!array->is_empty()) {
  2710. GET_VARIANT_PTR(iterator, 2);
  2711. *iterator = array->get(0);
  2712. // Skip regular iterate.
  2713. ip += 5;
  2714. } else {
  2715. // Jump to end of loop.
  2716. int jumpto = _code_ptr[ip + 4];
  2717. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2718. ip = jumpto;
  2719. }
  2720. }
  2721. DISPATCH_OPCODE;
  2722. #define OPCODE_ITERATE_BEGIN_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_var_ret_type, m_ret_type, m_ret_get_func) \
  2723. OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \
  2724. CHECK_SPACE(8); \
  2725. GET_VARIANT_PTR(counter, 0); \
  2726. GET_VARIANT_PTR(container, 1); \
  2727. Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \
  2728. VariantInternal::initialize(counter, Variant::INT); \
  2729. *VariantInternal::get_int(counter) = 0; \
  2730. if (!array->is_empty()) { \
  2731. GET_VARIANT_PTR(iterator, 2); \
  2732. VariantInternal::initialize(iterator, Variant::m_var_ret_type); \
  2733. m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \
  2734. *it = array->get(0); \
  2735. ip += 5; \
  2736. } else { \
  2737. int jumpto = _code_ptr[ip + 4]; \
  2738. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); \
  2739. ip = jumpto; \
  2740. } \
  2741. } \
  2742. DISPATCH_OPCODE
  2743. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, INT, int64_t, get_int);
  2744. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT32, int32_t, get_int32_array, INT, int64_t, get_int);
  2745. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT64, int64_t, get_int64_array, INT, int64_t, get_int);
  2746. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT32, float, get_float32_array, FLOAT, double, get_float);
  2747. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT64, double, get_float64_array, FLOAT, double, get_float);
  2748. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(STRING, String, get_string_array, STRING, String, get_string);
  2749. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, VECTOR2, Vector2, get_vector2);
  2750. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, VECTOR3, Vector3, get_vector3);
  2751. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(COLOR, Color, get_color_array, COLOR, Color, get_color);
  2752. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR4, Vector4, get_vector4_array, VECTOR4, Vector4, get_vector4);
  2753. OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) {
  2754. CHECK_SPACE(4);
  2755. GET_VARIANT_PTR(counter, 0);
  2756. GET_VARIANT_PTR(container, 1);
  2757. #ifdef DEBUG_ENABLED
  2758. bool freed = false;
  2759. Object *obj = container->get_validated_object_with_check(freed);
  2760. if (freed) {
  2761. err_text = "Trying to iterate on a previously freed object.";
  2762. OPCODE_BREAK;
  2763. } else if (!obj) {
  2764. err_text = "Trying to iterate on a null value.";
  2765. OPCODE_BREAK;
  2766. }
  2767. #else
  2768. Object *obj = *VariantInternal::get_object(container);
  2769. #endif
  2770. *counter = Variant();
  2771. Array ref = { *counter };
  2772. Variant vref;
  2773. VariantInternal::initialize(&vref, Variant::ARRAY);
  2774. *VariantInternal::get_array(&vref) = ref;
  2775. const Variant *args[] = { &vref };
  2776. Callable::CallError ce;
  2777. Variant has_next = obj->callp(CoreStringName(_iter_init), args, 1, ce);
  2778. #ifdef DEBUG_ENABLED
  2779. if (ref.size() != 1 || ce.error != Callable::CallError::CALL_OK) {
  2780. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2781. OPCODE_BREAK;
  2782. }
  2783. #endif
  2784. if (!has_next.booleanize()) {
  2785. int jumpto = _code_ptr[ip + 4];
  2786. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2787. ip = jumpto;
  2788. } else {
  2789. *counter = ref[0];
  2790. GET_VARIANT_PTR(iterator, 2);
  2791. *iterator = obj->callp(CoreStringName(_iter_get), (const Variant **)&counter, 1, ce);
  2792. #ifdef DEBUG_ENABLED
  2793. if (ce.error != Callable::CallError::CALL_OK) {
  2794. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2795. OPCODE_BREAK;
  2796. }
  2797. #endif
  2798. ip += 5; // Loop again.
  2799. }
  2800. }
  2801. DISPATCH_OPCODE;
  2802. OPCODE(OPCODE_ITERATE_BEGIN_RANGE) {
  2803. CHECK_SPACE(6);
  2804. GET_VARIANT_PTR(counter, 0);
  2805. GET_VARIANT_PTR(from_ptr, 1);
  2806. GET_VARIANT_PTR(to_ptr, 2);
  2807. GET_VARIANT_PTR(step_ptr, 3);
  2808. int64_t from = *VariantInternal::get_int(from_ptr);
  2809. int64_t to = *VariantInternal::get_int(to_ptr);
  2810. int64_t step = *VariantInternal::get_int(step_ptr);
  2811. VariantInternal::initialize(counter, Variant::INT);
  2812. *VariantInternal::get_int(counter) = from;
  2813. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2814. if (do_continue) {
  2815. GET_VARIANT_PTR(iterator, 4);
  2816. VariantInternal::initialize(iterator, Variant::INT);
  2817. *VariantInternal::get_int(iterator) = from;
  2818. // Skip regular iterate.
  2819. ip += 7;
  2820. } else {
  2821. // Jump to end of loop.
  2822. int jumpto = _code_ptr[ip + 6];
  2823. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2824. ip = jumpto;
  2825. }
  2826. }
  2827. DISPATCH_OPCODE;
  2828. OPCODE(OPCODE_ITERATE) {
  2829. CHECK_SPACE(4);
  2830. GET_VARIANT_PTR(counter, 0);
  2831. GET_VARIANT_PTR(container, 1);
  2832. bool valid;
  2833. if (!container->iter_next(*counter, valid)) {
  2834. #ifdef DEBUG_ENABLED
  2835. if (!valid) {
  2836. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  2837. OPCODE_BREAK;
  2838. }
  2839. #endif
  2840. int jumpto = _code_ptr[ip + 4];
  2841. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2842. ip = jumpto;
  2843. } else {
  2844. GET_VARIANT_PTR(iterator, 2);
  2845. *iterator = container->iter_get(*counter, valid);
  2846. #ifdef DEBUG_ENABLED
  2847. if (!valid) {
  2848. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  2849. OPCODE_BREAK;
  2850. }
  2851. #endif
  2852. ip += 5; //loop again
  2853. }
  2854. }
  2855. DISPATCH_OPCODE;
  2856. OPCODE(OPCODE_ITERATE_INT) {
  2857. CHECK_SPACE(4);
  2858. GET_VARIANT_PTR(counter, 0);
  2859. GET_VARIANT_PTR(container, 1);
  2860. int64_t size = *VariantInternal::get_int(container);
  2861. int64_t *count = VariantInternal::get_int(counter);
  2862. (*count)++;
  2863. if (*count >= size) {
  2864. int jumpto = _code_ptr[ip + 4];
  2865. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2866. ip = jumpto;
  2867. } else {
  2868. GET_VARIANT_PTR(iterator, 2);
  2869. *VariantInternal::get_int(iterator) = *count;
  2870. ip += 5; // Loop again.
  2871. }
  2872. }
  2873. DISPATCH_OPCODE;
  2874. OPCODE(OPCODE_ITERATE_FLOAT) {
  2875. CHECK_SPACE(4);
  2876. GET_VARIANT_PTR(counter, 0);
  2877. GET_VARIANT_PTR(container, 1);
  2878. double size = *VariantInternal::get_float(container);
  2879. double *count = VariantInternal::get_float(counter);
  2880. (*count)++;
  2881. if (*count >= size) {
  2882. int jumpto = _code_ptr[ip + 4];
  2883. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2884. ip = jumpto;
  2885. } else {
  2886. GET_VARIANT_PTR(iterator, 2);
  2887. *VariantInternal::get_float(iterator) = *count;
  2888. ip += 5; // Loop again.
  2889. }
  2890. }
  2891. DISPATCH_OPCODE;
  2892. OPCODE(OPCODE_ITERATE_VECTOR2) {
  2893. CHECK_SPACE(4);
  2894. GET_VARIANT_PTR(counter, 0);
  2895. GET_VARIANT_PTR(container, 1);
  2896. const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container);
  2897. double *count = VariantInternal::get_float(counter);
  2898. (*count)++;
  2899. if (*count >= bounds->y) {
  2900. int jumpto = _code_ptr[ip + 4];
  2901. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2902. ip = jumpto;
  2903. } else {
  2904. GET_VARIANT_PTR(iterator, 2);
  2905. *VariantInternal::get_float(iterator) = *count;
  2906. ip += 5; // Loop again.
  2907. }
  2908. }
  2909. DISPATCH_OPCODE;
  2910. OPCODE(OPCODE_ITERATE_VECTOR2I) {
  2911. CHECK_SPACE(4);
  2912. GET_VARIANT_PTR(counter, 0);
  2913. GET_VARIANT_PTR(container, 1);
  2914. const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container);
  2915. int64_t *count = VariantInternal::get_int(counter);
  2916. (*count)++;
  2917. if (*count >= bounds->y) {
  2918. int jumpto = _code_ptr[ip + 4];
  2919. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2920. ip = jumpto;
  2921. } else {
  2922. GET_VARIANT_PTR(iterator, 2);
  2923. *VariantInternal::get_int(iterator) = *count;
  2924. ip += 5; // Loop again.
  2925. }
  2926. }
  2927. DISPATCH_OPCODE;
  2928. OPCODE(OPCODE_ITERATE_VECTOR3) {
  2929. CHECK_SPACE(4);
  2930. GET_VARIANT_PTR(counter, 0);
  2931. GET_VARIANT_PTR(container, 1);
  2932. const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container);
  2933. double *count = VariantInternal::get_float(counter);
  2934. *count += bounds->z;
  2935. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2936. int jumpto = _code_ptr[ip + 4];
  2937. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2938. ip = jumpto;
  2939. } else {
  2940. GET_VARIANT_PTR(iterator, 2);
  2941. *VariantInternal::get_float(iterator) = *count;
  2942. ip += 5; // Loop again.
  2943. }
  2944. }
  2945. DISPATCH_OPCODE;
  2946. OPCODE(OPCODE_ITERATE_VECTOR3I) {
  2947. CHECK_SPACE(4);
  2948. GET_VARIANT_PTR(counter, 0);
  2949. GET_VARIANT_PTR(container, 1);
  2950. const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container);
  2951. int64_t *count = VariantInternal::get_int(counter);
  2952. *count += bounds->z;
  2953. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2954. int jumpto = _code_ptr[ip + 4];
  2955. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2956. ip = jumpto;
  2957. } else {
  2958. GET_VARIANT_PTR(iterator, 2);
  2959. *VariantInternal::get_int(iterator) = *count;
  2960. ip += 5; // Loop again.
  2961. }
  2962. }
  2963. DISPATCH_OPCODE;
  2964. OPCODE(OPCODE_ITERATE_STRING) {
  2965. CHECK_SPACE(4);
  2966. GET_VARIANT_PTR(counter, 0);
  2967. GET_VARIANT_PTR(container, 1);
  2968. const String *str = VariantInternal::get_string((const Variant *)container);
  2969. int64_t *idx = VariantInternal::get_int(counter);
  2970. (*idx)++;
  2971. if (*idx >= str->length()) {
  2972. int jumpto = _code_ptr[ip + 4];
  2973. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2974. ip = jumpto;
  2975. } else {
  2976. GET_VARIANT_PTR(iterator, 2);
  2977. *VariantInternal::get_string(iterator) = str->substr(*idx, 1);
  2978. ip += 5; // Loop again.
  2979. }
  2980. }
  2981. DISPATCH_OPCODE;
  2982. OPCODE(OPCODE_ITERATE_DICTIONARY) {
  2983. CHECK_SPACE(4);
  2984. GET_VARIANT_PTR(counter, 0);
  2985. GET_VARIANT_PTR(container, 1);
  2986. const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container);
  2987. const Variant *next = dict->next(counter);
  2988. if (!next) {
  2989. int jumpto = _code_ptr[ip + 4];
  2990. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2991. ip = jumpto;
  2992. } else {
  2993. GET_VARIANT_PTR(iterator, 2);
  2994. *counter = *next;
  2995. *iterator = *next;
  2996. ip += 5; // Loop again.
  2997. }
  2998. }
  2999. DISPATCH_OPCODE;
  3000. OPCODE(OPCODE_ITERATE_ARRAY) {
  3001. CHECK_SPACE(4);
  3002. GET_VARIANT_PTR(counter, 0);
  3003. GET_VARIANT_PTR(container, 1);
  3004. const Array *array = VariantInternal::get_array((const Variant *)container);
  3005. int64_t *idx = VariantInternal::get_int(counter);
  3006. (*idx)++;
  3007. if (*idx >= array->size()) {
  3008. int jumpto = _code_ptr[ip + 4];
  3009. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  3010. ip = jumpto;
  3011. } else {
  3012. GET_VARIANT_PTR(iterator, 2);
  3013. *iterator = array->get(*idx);
  3014. ip += 5; // Loop again.
  3015. }
  3016. }
  3017. DISPATCH_OPCODE;
  3018. #define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \
  3019. OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \
  3020. CHECK_SPACE(4); \
  3021. GET_VARIANT_PTR(counter, 0); \
  3022. GET_VARIANT_PTR(container, 1); \
  3023. const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \
  3024. int64_t *idx = VariantInternal::get_int(counter); \
  3025. (*idx)++; \
  3026. if (*idx >= array->size()) { \
  3027. int jumpto = _code_ptr[ip + 4]; \
  3028. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); \
  3029. ip = jumpto; \
  3030. } else { \
  3031. GET_VARIANT_PTR(iterator, 2); \
  3032. *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \
  3033. ip += 5; \
  3034. } \
  3035. } \
  3036. DISPATCH_OPCODE
  3037. OPCODE_ITERATE_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, get_int);
  3038. OPCODE_ITERATE_PACKED_ARRAY(INT32, int32_t, get_int32_array, get_int);
  3039. OPCODE_ITERATE_PACKED_ARRAY(INT64, int64_t, get_int64_array, get_int);
  3040. OPCODE_ITERATE_PACKED_ARRAY(FLOAT32, float, get_float32_array, get_float);
  3041. OPCODE_ITERATE_PACKED_ARRAY(FLOAT64, double, get_float64_array, get_float);
  3042. OPCODE_ITERATE_PACKED_ARRAY(STRING, String, get_string_array, get_string);
  3043. OPCODE_ITERATE_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, get_vector2);
  3044. OPCODE_ITERATE_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, get_vector3);
  3045. OPCODE_ITERATE_PACKED_ARRAY(COLOR, Color, get_color_array, get_color);
  3046. OPCODE_ITERATE_PACKED_ARRAY(VECTOR4, Vector4, get_vector4_array, get_vector4);
  3047. OPCODE(OPCODE_ITERATE_OBJECT) {
  3048. CHECK_SPACE(4);
  3049. GET_VARIANT_PTR(counter, 0);
  3050. GET_VARIANT_PTR(container, 1);
  3051. #ifdef DEBUG_ENABLED
  3052. bool freed = false;
  3053. Object *obj = container->get_validated_object_with_check(freed);
  3054. if (freed) {
  3055. err_text = "Trying to iterate on a previously freed object.";
  3056. OPCODE_BREAK;
  3057. } else if (!obj) {
  3058. err_text = "Trying to iterate on a null value.";
  3059. OPCODE_BREAK;
  3060. }
  3061. #else
  3062. Object *obj = *VariantInternal::get_object(container);
  3063. #endif
  3064. Array ref = { *counter };
  3065. Variant vref;
  3066. VariantInternal::initialize(&vref, Variant::ARRAY);
  3067. *VariantInternal::get_array(&vref) = ref;
  3068. const Variant *args[] = { &vref };
  3069. Callable::CallError ce;
  3070. Variant has_next = obj->callp(CoreStringName(_iter_next), args, 1, ce);
  3071. #ifdef DEBUG_ENABLED
  3072. if (ref.size() != 1 || ce.error != Callable::CallError::CALL_OK) {
  3073. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  3074. OPCODE_BREAK;
  3075. }
  3076. #endif
  3077. if (!has_next.booleanize()) {
  3078. int jumpto = _code_ptr[ip + 4];
  3079. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  3080. ip = jumpto;
  3081. } else {
  3082. *counter = ref[0];
  3083. GET_VARIANT_PTR(iterator, 2);
  3084. *iterator = obj->callp(CoreStringName(_iter_get), (const Variant **)&counter, 1, ce);
  3085. #ifdef DEBUG_ENABLED
  3086. if (ce.error != Callable::CallError::CALL_OK) {
  3087. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  3088. OPCODE_BREAK;
  3089. }
  3090. #endif
  3091. ip += 5; // Loop again.
  3092. }
  3093. }
  3094. DISPATCH_OPCODE;
  3095. OPCODE(OPCODE_ITERATE_RANGE) {
  3096. CHECK_SPACE(5);
  3097. GET_VARIANT_PTR(counter, 0);
  3098. GET_VARIANT_PTR(to_ptr, 1);
  3099. GET_VARIANT_PTR(step_ptr, 2);
  3100. int64_t to = *VariantInternal::get_int(to_ptr);
  3101. int64_t step = *VariantInternal::get_int(step_ptr);
  3102. int64_t *count = VariantInternal::get_int(counter);
  3103. *count += step;
  3104. if ((step < 0 && *count <= to) || (step > 0 && *count >= to)) {
  3105. int jumpto = _code_ptr[ip + 5];
  3106. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  3107. ip = jumpto;
  3108. } else {
  3109. GET_VARIANT_PTR(iterator, 3);
  3110. *VariantInternal::get_int(iterator) = *count;
  3111. ip += 6; // Loop again.
  3112. }
  3113. }
  3114. DISPATCH_OPCODE;
  3115. OPCODE(OPCODE_STORE_GLOBAL) {
  3116. CHECK_SPACE(3);
  3117. int global_idx = _code_ptr[ip + 2];
  3118. GD_ERR_BREAK(global_idx < 0 || global_idx >= GDScriptLanguage::get_singleton()->get_global_array_size());
  3119. GET_VARIANT_PTR(dst, 0);
  3120. *dst = GDScriptLanguage::get_singleton()->get_global_array()[global_idx];
  3121. ip += 3;
  3122. }
  3123. DISPATCH_OPCODE;
  3124. OPCODE(OPCODE_STORE_NAMED_GLOBAL) {
  3125. CHECK_SPACE(3);
  3126. int globalname_idx = _code_ptr[ip + 2];
  3127. GD_ERR_BREAK(globalname_idx < 0 || globalname_idx >= _global_names_count);
  3128. const StringName *globalname = &_global_names_ptr[globalname_idx];
  3129. GD_ERR_BREAK(!GDScriptLanguage::get_singleton()->get_named_globals_map().has(*globalname));
  3130. GET_VARIANT_PTR(dst, 0);
  3131. *dst = GDScriptLanguage::get_singleton()->get_named_globals_map()[*globalname];
  3132. ip += 3;
  3133. }
  3134. DISPATCH_OPCODE;
  3135. #define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \
  3136. OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \
  3137. CHECK_SPACE(2); \
  3138. GET_VARIANT_PTR(arg, 0); \
  3139. VariantTypeAdjust<m_c_type>::adjust(arg); \
  3140. ip += 2; \
  3141. } \
  3142. DISPATCH_OPCODE
  3143. OPCODE_TYPE_ADJUST(BOOL, bool);
  3144. OPCODE_TYPE_ADJUST(INT, int64_t);
  3145. OPCODE_TYPE_ADJUST(FLOAT, double);
  3146. OPCODE_TYPE_ADJUST(STRING, String);
  3147. OPCODE_TYPE_ADJUST(VECTOR2, Vector2);
  3148. OPCODE_TYPE_ADJUST(VECTOR2I, Vector2i);
  3149. OPCODE_TYPE_ADJUST(RECT2, Rect2);
  3150. OPCODE_TYPE_ADJUST(RECT2I, Rect2i);
  3151. OPCODE_TYPE_ADJUST(VECTOR3, Vector3);
  3152. OPCODE_TYPE_ADJUST(VECTOR3I, Vector3i);
  3153. OPCODE_TYPE_ADJUST(TRANSFORM2D, Transform2D);
  3154. OPCODE_TYPE_ADJUST(VECTOR4, Vector4);
  3155. OPCODE_TYPE_ADJUST(VECTOR4I, Vector4i);
  3156. OPCODE_TYPE_ADJUST(PLANE, Plane);
  3157. OPCODE_TYPE_ADJUST(QUATERNION, Quaternion);
  3158. OPCODE_TYPE_ADJUST(AABB, AABB);
  3159. OPCODE_TYPE_ADJUST(BASIS, Basis);
  3160. OPCODE_TYPE_ADJUST(TRANSFORM3D, Transform3D);
  3161. OPCODE_TYPE_ADJUST(PROJECTION, Projection);
  3162. OPCODE_TYPE_ADJUST(COLOR, Color);
  3163. OPCODE_TYPE_ADJUST(STRING_NAME, StringName);
  3164. OPCODE_TYPE_ADJUST(NODE_PATH, NodePath);
  3165. OPCODE_TYPE_ADJUST(RID, RID);
  3166. OPCODE_TYPE_ADJUST(OBJECT, Object *);
  3167. OPCODE_TYPE_ADJUST(CALLABLE, Callable);
  3168. OPCODE_TYPE_ADJUST(SIGNAL, Signal);
  3169. OPCODE_TYPE_ADJUST(DICTIONARY, Dictionary);
  3170. OPCODE_TYPE_ADJUST(ARRAY, Array);
  3171. OPCODE_TYPE_ADJUST(PACKED_BYTE_ARRAY, PackedByteArray);
  3172. OPCODE_TYPE_ADJUST(PACKED_INT32_ARRAY, PackedInt32Array);
  3173. OPCODE_TYPE_ADJUST(PACKED_INT64_ARRAY, PackedInt64Array);
  3174. OPCODE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY, PackedFloat32Array);
  3175. OPCODE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY, PackedFloat64Array);
  3176. OPCODE_TYPE_ADJUST(PACKED_STRING_ARRAY, PackedStringArray);
  3177. OPCODE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY, PackedVector2Array);
  3178. OPCODE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY, PackedVector3Array);
  3179. OPCODE_TYPE_ADJUST(PACKED_COLOR_ARRAY, PackedColorArray);
  3180. OPCODE_TYPE_ADJUST(PACKED_VECTOR4_ARRAY, PackedVector4Array);
  3181. OPCODE(OPCODE_ASSERT) {
  3182. CHECK_SPACE(3);
  3183. #ifdef DEBUG_ENABLED
  3184. GET_VARIANT_PTR(test, 0);
  3185. bool result = test->booleanize();
  3186. if (!result) {
  3187. String message_str;
  3188. if (_code_ptr[ip + 2] != 0) {
  3189. GET_VARIANT_PTR(message, 1);
  3190. Variant message_var = *message;
  3191. if (message->get_type() != Variant::NIL) {
  3192. message_str = message_var;
  3193. }
  3194. }
  3195. if (message_str.is_empty()) {
  3196. err_text = "Assertion failed.";
  3197. } else {
  3198. err_text = "Assertion failed: " + message_str;
  3199. }
  3200. OPCODE_BREAK;
  3201. }
  3202. #endif
  3203. ip += 3;
  3204. }
  3205. DISPATCH_OPCODE;
  3206. OPCODE(OPCODE_BREAKPOINT) {
  3207. #ifdef DEBUG_ENABLED
  3208. if (EngineDebugger::is_active()) {
  3209. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  3210. }
  3211. #endif
  3212. ip += 1;
  3213. }
  3214. DISPATCH_OPCODE;
  3215. OPCODE(OPCODE_LINE) {
  3216. CHECK_SPACE(2);
  3217. line = _code_ptr[ip + 1];
  3218. ip += 2;
  3219. if (EngineDebugger::is_active()) {
  3220. // line
  3221. bool do_break = false;
  3222. if (unlikely(EngineDebugger::get_script_debugger()->get_lines_left() > 0)) {
  3223. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  3224. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  3225. }
  3226. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  3227. do_break = true;
  3228. }
  3229. }
  3230. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  3231. do_break = true;
  3232. }
  3233. if (unlikely(do_break)) {
  3234. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  3235. }
  3236. EngineDebugger::get_singleton()->line_poll();
  3237. }
  3238. }
  3239. DISPATCH_OPCODE;
  3240. OPCODE(OPCODE_END) {
  3241. #ifdef DEBUG_ENABLED
  3242. exit_ok = true;
  3243. #endif
  3244. OPCODE_BREAK;
  3245. }
  3246. #if 0 // Enable for debugging.
  3247. default: {
  3248. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  3249. OPCODE_BREAK;
  3250. }
  3251. #endif
  3252. }
  3253. OPCODES_END
  3254. #ifdef DEBUG_ENABLED
  3255. if (exit_ok) {
  3256. OPCODE_OUT;
  3257. }
  3258. //error
  3259. // function, file, line, error, explanation
  3260. String err_file;
  3261. bool instance_valid_with_script = p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid();
  3262. if (instance_valid_with_script && !get_script()->path.is_empty()) {
  3263. err_file = get_script()->path;
  3264. } else if (script) {
  3265. err_file = script->path;
  3266. }
  3267. if (err_file.is_empty()) {
  3268. err_file = "<built-in>";
  3269. }
  3270. String err_func = name;
  3271. if (instance_valid_with_script && p_instance->script->local_name != StringName()) {
  3272. err_func = p_instance->script->local_name.operator String() + "." + err_func;
  3273. }
  3274. int err_line = line;
  3275. if (err_text.is_empty()) {
  3276. err_text = "Internal script error! Opcode: " + itos(last_opcode) + " (please report).";
  3277. }
  3278. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  3279. // debugger break did not happen
  3280. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), false, ERR_HANDLER_SCRIPT);
  3281. }
  3282. // Get a default return type in case of failure
  3283. retvalue = _get_default_variant_for_data_type(return_type);
  3284. #endif
  3285. OPCODE_OUT;
  3286. }
  3287. OPCODES_OUT
  3288. #ifdef DEBUG_ENABLED
  3289. if (GDScriptLanguage::get_singleton()->profiling) {
  3290. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  3291. profile.total_time.add(time_taken);
  3292. profile.self_time.add(time_taken - function_call_time);
  3293. profile.frame_total_time.add(time_taken);
  3294. profile.frame_self_time.add(time_taken - function_call_time);
  3295. if (Thread::get_caller_id() == Thread::get_main_id()) {
  3296. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  3297. }
  3298. }
  3299. #endif
  3300. // Check if this is not the last time it was interrupted by `await` or if it's the first time executing.
  3301. // If that is the case then we exit the function as normal. Otherwise we postpone it until the last `await` is completed.
  3302. // This ensures the call stack can be properly shown when using `await`, showing what resumed the function.
  3303. if (!p_state || awaited) {
  3304. GDScriptLanguage::get_singleton()->exit_function();
  3305. // Free stack, except reserved addresses.
  3306. for (int i = FIXED_ADDRESSES_MAX; i < _stack_size; i++) {
  3307. stack[i].~Variant();
  3308. }
  3309. }
  3310. // Always free reserved addresses, since they are never copied.
  3311. for (int i = 0; i < FIXED_ADDRESSES_MAX; i++) {
  3312. stack[i].~Variant();
  3313. }
  3314. call_depth--;
  3315. return retvalue;
  3316. }