gdscript_vm.cpp 106 KB

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