gdscript_vm.cpp 110 KB

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