2
0

gdscript_vm.cpp 97 KB

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