gdscript_vm.cpp 132 KB

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