gdscript_vm.cpp 89 KB

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