gdscript_vm.cpp 91 KB

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