2
0

gdscript_vm.cpp 110 KB

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