gdscript_vm.cpp 111 KB

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