gdscript_vm.cpp 88 KB

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