gdscript_vm.cpp 130 KB

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