gdscript_vm.cpp 89 KB

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