gdscript_vm.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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/os/os.h"
  32. #include "gdscript.h"
  33. #include "gdscript_functions.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant &static_ref, Variant *p_stack, String &r_error) const {
  35. int address = p_address & ADDR_MASK;
  36. //sequential table (jump table generated by compiler)
  37. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  38. case ADDR_TYPE_SELF: {
  39. #ifdef DEBUG_ENABLED
  40. if (unlikely(!p_instance)) {
  41. r_error = "Cannot access self without instance.";
  42. return nullptr;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(!p_instance)) {
  53. r_error = "Cannot access member without instance.";
  54. return nullptr;
  55. }
  56. #endif
  57. //member indexing is O(1)
  58. return &p_instance->members.write[address];
  59. } break;
  60. case ADDR_TYPE_CLASS_CONSTANT: {
  61. //todo change to index!
  62. GDScript *s = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (s) {
  68. GDScript *o = s;
  69. while (o) {
  70. Map<StringName, Variant>::Element *E = o->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. o = o->_owner;
  75. }
  76. s = s->_base;
  77. }
  78. ERR_FAIL_V_MSG(nullptr, "GDScriptCompiler bug.");
  79. } break;
  80. case ADDR_TYPE_LOCAL_CONSTANT: {
  81. #ifdef DEBUG_ENABLED
  82. ERR_FAIL_INDEX_V(address, _constant_count, nullptr);
  83. #endif
  84. return &_constants_ptr[address];
  85. } break;
  86. case ADDR_TYPE_STACK:
  87. case ADDR_TYPE_STACK_VARIABLE: {
  88. #ifdef DEBUG_ENABLED
  89. ERR_FAIL_INDEX_V(address, _stack_size, nullptr);
  90. #endif
  91. return &p_stack[address];
  92. } break;
  93. case ADDR_TYPE_GLOBAL: {
  94. #ifdef DEBUG_ENABLED
  95. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), nullptr);
  96. #endif
  97. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  98. } break;
  99. #ifdef TOOLS_ENABLED
  100. case ADDR_TYPE_NAMED_GLOBAL: {
  101. #ifdef DEBUG_ENABLED
  102. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  103. #endif
  104. StringName id = _global_names_ptr[address];
  105. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  106. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  107. } else {
  108. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  109. return nullptr;
  110. }
  111. } break;
  112. #endif
  113. case ADDR_TYPE_NIL: {
  114. return &nil;
  115. } break;
  116. }
  117. ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode).");
  118. return nullptr;
  119. }
  120. #ifdef DEBUG_ENABLED
  121. static String _get_var_type(const Variant *p_var) {
  122. String basestr;
  123. if (p_var->get_type() == Variant::OBJECT) {
  124. bool was_freed;
  125. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  126. if (!bobj) {
  127. if (was_freed) {
  128. basestr = "null instance";
  129. } else {
  130. basestr = "previously freed";
  131. }
  132. } else {
  133. if (bobj->get_script_instance()) {
  134. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  135. } else {
  136. basestr = bobj->get_class();
  137. }
  138. }
  139. } else {
  140. basestr = Variant::get_type_name(p_var->get_type());
  141. }
  142. return basestr;
  143. }
  144. #endif // DEBUG_ENABLED
  145. String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  146. String err_text;
  147. if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  148. int errorarg = p_err.argument;
  149. // Handle the Object to Object case separately as we don't have further class details.
  150. #ifdef DEBUG_ENABLED
  151. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  152. err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class.";
  153. } else
  154. #endif // DEBUG_ENABLED
  155. {
  156. err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
  157. }
  158. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  159. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  160. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  161. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  162. } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  163. err_text = "Invalid call. Nonexistent " + p_where + ".";
  164. } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  165. err_text = "Attempt to call " + p_where + " on a null instance.";
  166. } else {
  167. err_text = "Bug, call error: #" + itos(p_err.error);
  168. }
  169. return err_text;
  170. }
  171. #if defined(__GNUC__)
  172. #define OPCODES_TABLE \
  173. static const void *switch_table_ops[] = { \
  174. &&OPCODE_OPERATOR, \
  175. &&OPCODE_OPERATOR_VALIDATED, \
  176. &&OPCODE_EXTENDS_TEST, \
  177. &&OPCODE_IS_BUILTIN, \
  178. &&OPCODE_SET_KEYED, \
  179. &&OPCODE_SET_KEYED_VALIDATED, \
  180. &&OPCODE_SET_INDEXED_VALIDATED, \
  181. &&OPCODE_GET_KEYED, \
  182. &&OPCODE_GET_KEYED_VALIDATED, \
  183. &&OPCODE_GET_INDEXED_VALIDATED, \
  184. &&OPCODE_SET_NAMED, \
  185. &&OPCODE_SET_NAMED_VALIDATED, \
  186. &&OPCODE_GET_NAMED, \
  187. &&OPCODE_GET_NAMED_VALIDATED, \
  188. &&OPCODE_SET_MEMBER, \
  189. &&OPCODE_GET_MEMBER, \
  190. &&OPCODE_ASSIGN, \
  191. &&OPCODE_ASSIGN_TRUE, \
  192. &&OPCODE_ASSIGN_FALSE, \
  193. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  194. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  195. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  196. &&OPCODE_CAST_TO_BUILTIN, \
  197. &&OPCODE_CAST_TO_NATIVE, \
  198. &&OPCODE_CAST_TO_SCRIPT, \
  199. &&OPCODE_CONSTRUCT, \
  200. &&OPCODE_CONSTRUCT_ARRAY, \
  201. &&OPCODE_CONSTRUCT_DICTIONARY, \
  202. &&OPCODE_CALL, \
  203. &&OPCODE_CALL_RETURN, \
  204. &&OPCODE_CALL_ASYNC, \
  205. &&OPCODE_CALL_BUILT_IN, \
  206. &&OPCODE_CALL_SELF_BASE, \
  207. &&OPCODE_CALL_METHOD_BIND, \
  208. &&OPCODE_CALL_METHOD_BIND_RET, \
  209. &&OPCODE_CALL_PTRCALL_NO_RETURN, \
  210. &&OPCODE_CALL_PTRCALL_BOOL, \
  211. &&OPCODE_CALL_PTRCALL_INT, \
  212. &&OPCODE_CALL_PTRCALL_FLOAT, \
  213. &&OPCODE_CALL_PTRCALL_STRING, \
  214. &&OPCODE_CALL_PTRCALL_VECTOR2, \
  215. &&OPCODE_CALL_PTRCALL_VECTOR2I, \
  216. &&OPCODE_CALL_PTRCALL_RECT2, \
  217. &&OPCODE_CALL_PTRCALL_RECT2I, \
  218. &&OPCODE_CALL_PTRCALL_VECTOR3, \
  219. &&OPCODE_CALL_PTRCALL_VECTOR3I, \
  220. &&OPCODE_CALL_PTRCALL_TRANSFORM2D, \
  221. &&OPCODE_CALL_PTRCALL_PLANE, \
  222. &&OPCODE_CALL_PTRCALL_QUAT, \
  223. &&OPCODE_CALL_PTRCALL_AABB, \
  224. &&OPCODE_CALL_PTRCALL_BASIS, \
  225. &&OPCODE_CALL_PTRCALL_TRANSFORM, \
  226. &&OPCODE_CALL_PTRCALL_COLOR, \
  227. &&OPCODE_CALL_PTRCALL_STRING_NAME, \
  228. &&OPCODE_CALL_PTRCALL_NODE_PATH, \
  229. &&OPCODE_CALL_PTRCALL_RID, \
  230. &&OPCODE_CALL_PTRCALL_OBJECT, \
  231. &&OPCODE_CALL_PTRCALL_CALLABLE, \
  232. &&OPCODE_CALL_PTRCALL_SIGNAL, \
  233. &&OPCODE_CALL_PTRCALL_DICTIONARY, \
  234. &&OPCODE_CALL_PTRCALL_ARRAY, \
  235. &&OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY, \
  236. &&OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY, \
  237. &&OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY, \
  238. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY, \
  239. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY, \
  240. &&OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY, \
  241. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY, \
  242. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY, \
  243. &&OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY, \
  244. &&OPCODE_AWAIT, \
  245. &&OPCODE_AWAIT_RESUME, \
  246. &&OPCODE_JUMP, \
  247. &&OPCODE_JUMP_IF, \
  248. &&OPCODE_JUMP_IF_NOT, \
  249. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  250. &&OPCODE_RETURN, \
  251. &&OPCODE_ITERATE_BEGIN, \
  252. &&OPCODE_ITERATE, \
  253. &&OPCODE_ASSERT, \
  254. &&OPCODE_BREAKPOINT, \
  255. &&OPCODE_LINE, \
  256. &&OPCODE_END \
  257. }; \
  258. 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.");
  259. #define OPCODE(m_op) \
  260. m_op:
  261. #define OPCODE_WHILE(m_test) \
  262. OPSWHILE:
  263. #define OPCODES_END \
  264. OPSEXIT:
  265. #define OPCODES_OUT \
  266. OPSOUT:
  267. #define DISPATCH_OPCODE goto OPSWHILE
  268. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  269. #define OPCODE_BREAK goto OPSEXIT
  270. #define OPCODE_OUT goto OPSOUT
  271. #else
  272. #define OPCODES_TABLE
  273. #define OPCODE(m_op) case m_op:
  274. #define OPCODE_WHILE(m_test) while (m_test)
  275. #define OPCODES_END
  276. #define OPCODES_OUT
  277. #define DISPATCH_OPCODE continue
  278. #define OPCODE_SWITCH(m_test) switch (m_test)
  279. #define OPCODE_BREAK break
  280. #define OPCODE_OUT break
  281. #endif
  282. // Helpers for VariantInternal methods in macros.
  283. #define OP_GET_BOOL get_bool
  284. #define OP_GET_INT get_int
  285. #define OP_GET_FLOAT get_float
  286. #define OP_GET_VECTOR2 get_vector2
  287. #define OP_GET_VECTOR2I get_vector2i
  288. #define OP_GET_VECTOR3 get_vector3
  289. #define OP_GET_VECTOR3I get_vector3i
  290. #define OP_GET_RECT2 get_rect2
  291. #define OP_GET_RECT2I get_rect2i
  292. #define OP_GET_QUAT get_quat
  293. #define OP_GET_COLOR get_color
  294. #define OP_GET_STRING get_string
  295. #define OP_GET_STRING_NAME get_string_name
  296. #define OP_GET_NODE_PATH get_node_path
  297. #define OP_GET_CALLABLE get_callable
  298. #define OP_GET_SIGNAL get_signal
  299. #define OP_GET_ARRAY get_array
  300. #define OP_GET_DICTIONARY get_dictionary
  301. #define OP_GET_PACKED_BYTE_ARRAY get_byte_array
  302. #define OP_GET_PACKED_INT32_ARRAY get_int32_array
  303. #define OP_GET_PACKED_INT64_ARRAY get_int64_array
  304. #define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
  305. #define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
  306. #define OP_GET_PACKED_STRING_ARRAY get_string_array
  307. #define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
  308. #define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
  309. #define OP_GET_PACKED_COLOR_ARRAY get_color_array
  310. #define OP_GET_TRANSFORM get_transform
  311. #define OP_GET_TRANSFORM2D get_transform2d
  312. #define OP_GET_PLANE get_plane
  313. #define OP_GET_AABB get_aabb
  314. #define OP_GET_BASIS get_basis
  315. #define OP_GET_RID get_rid
  316. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  317. OPCODES_TABLE;
  318. if (!_code_ptr) {
  319. return Variant();
  320. }
  321. r_err.error = Callable::CallError::CALL_OK;
  322. Variant self;
  323. Variant static_ref;
  324. Variant retvalue;
  325. Variant *stack = nullptr;
  326. Variant **instruction_args;
  327. const void **call_args_ptr = nullptr;
  328. int defarg = 0;
  329. #ifdef DEBUG_ENABLED
  330. //GDScriptLanguage::get_singleton()->calls++;
  331. #endif
  332. uint32_t alloca_size = 0;
  333. GDScript *script;
  334. int ip = 0;
  335. int line = _initial_line;
  336. if (p_state) {
  337. //use existing (supplied) state (awaited)
  338. stack = (Variant *)p_state->stack.ptr();
  339. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  340. line = p_state->line;
  341. ip = p_state->ip;
  342. alloca_size = p_state->stack.size();
  343. script = p_state->script;
  344. p_instance = p_state->instance;
  345. defarg = p_state->defarg;
  346. self = p_state->self;
  347. } else {
  348. if (p_argcount != _argument_count) {
  349. if (p_argcount > _argument_count) {
  350. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  351. r_err.argument = _argument_count;
  352. return Variant();
  353. } else if (p_argcount < _argument_count - _default_arg_count) {
  354. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  355. r_err.argument = _argument_count - _default_arg_count;
  356. return Variant();
  357. } else {
  358. defarg = _argument_count - p_argcount;
  359. }
  360. }
  361. alloca_size = sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  362. if (alloca_size) {
  363. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  364. if (_stack_size) {
  365. stack = (Variant *)aptr;
  366. for (int i = 0; i < p_argcount; i++) {
  367. if (!argument_types[i].has_type) {
  368. memnew_placement(&stack[i], Variant(*p_args[i]));
  369. continue;
  370. }
  371. if (!argument_types[i].is_type(*p_args[i], true)) {
  372. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  373. r_err.argument = i;
  374. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  375. return Variant();
  376. }
  377. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  378. Variant arg;
  379. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  380. memnew_placement(&stack[i], Variant(arg));
  381. } else {
  382. memnew_placement(&stack[i], Variant(*p_args[i]));
  383. }
  384. }
  385. for (int i = p_argcount; i < _stack_size; i++) {
  386. memnew_placement(&stack[i], Variant);
  387. }
  388. } else {
  389. stack = nullptr;
  390. }
  391. if (_instruction_args_size) {
  392. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  393. } else {
  394. instruction_args = nullptr;
  395. }
  396. } else {
  397. stack = nullptr;
  398. instruction_args = nullptr;
  399. }
  400. if (p_instance) {
  401. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  402. self = REF(static_cast<Reference *>(p_instance->owner));
  403. } else {
  404. self = p_instance->owner;
  405. }
  406. script = p_instance->script.ptr();
  407. } else {
  408. script = _script;
  409. }
  410. }
  411. if (_ptrcall_args_size) {
  412. call_args_ptr = (const void **)alloca(_ptrcall_args_size * sizeof(void *));
  413. } else {
  414. call_args_ptr = nullptr;
  415. }
  416. static_ref = script;
  417. String err_text;
  418. #ifdef DEBUG_ENABLED
  419. if (EngineDebugger::is_active()) {
  420. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  421. }
  422. #define GD_ERR_BREAK(m_cond) \
  423. { \
  424. if (unlikely(m_cond)) { \
  425. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  426. OPCODE_BREAK; \
  427. } \
  428. }
  429. #define CHECK_SPACE(m_space) \
  430. GD_ERR_BREAK((ip + m_space) > _code_size)
  431. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  432. Variant *m_v; \
  433. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  434. if (unlikely(!m_v)) \
  435. OPCODE_BREAK;
  436. #else
  437. #define GD_ERR_BREAK(m_cond)
  438. #define CHECK_SPACE(m_space)
  439. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  440. Variant *m_v; \
  441. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  442. #endif
  443. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  444. Variant *m_v = instruction_args[m_idx]
  445. #ifdef DEBUG_ENABLED
  446. uint64_t function_start_time = 0;
  447. uint64_t function_call_time = 0;
  448. if (GDScriptLanguage::get_singleton()->profiling) {
  449. function_start_time = OS::get_singleton()->get_ticks_usec();
  450. function_call_time = 0;
  451. profile.call_count++;
  452. profile.frame_call_count++;
  453. }
  454. bool exit_ok = false;
  455. bool awaited = false;
  456. #endif
  457. #ifdef DEBUG_ENABLED
  458. OPCODE_WHILE(ip < _code_size) {
  459. int last_opcode = _code_ptr[ip];
  460. #else
  461. OPCODE_WHILE(true) {
  462. #endif
  463. // Load arguments for the instruction before each instruction.
  464. int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS;
  465. for (int i = 0; i < instr_arg_count; i++) {
  466. GET_VARIANT_PTR(v, i + 1);
  467. instruction_args[i] = v;
  468. }
  469. OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) {
  470. OPCODE(OPCODE_OPERATOR) {
  471. CHECK_SPACE(5);
  472. bool valid;
  473. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  474. GD_ERR_BREAK(op >= Variant::OP_MAX);
  475. GET_INSTRUCTION_ARG(a, 0);
  476. GET_INSTRUCTION_ARG(b, 1);
  477. GET_INSTRUCTION_ARG(dst, 2);
  478. #ifdef DEBUG_ENABLED
  479. Variant ret;
  480. Variant::evaluate(op, *a, *b, ret, valid);
  481. #else
  482. Variant::evaluate(op, *a, *b, *dst, valid);
  483. #endif
  484. #ifdef DEBUG_ENABLED
  485. if (!valid) {
  486. if (ret.get_type() == Variant::STRING) {
  487. //return a string when invalid with the error
  488. err_text = ret;
  489. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  490. } else {
  491. 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) + "'.";
  492. }
  493. OPCODE_BREAK;
  494. }
  495. *dst = ret;
  496. #endif
  497. ip += 5;
  498. }
  499. DISPATCH_OPCODE;
  500. OPCODE(OPCODE_OPERATOR_VALIDATED) {
  501. CHECK_SPACE(5);
  502. int operator_idx = _code_ptr[ip + 4];
  503. GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
  504. Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
  505. GET_INSTRUCTION_ARG(a, 0);
  506. GET_INSTRUCTION_ARG(b, 1);
  507. GET_INSTRUCTION_ARG(dst, 2);
  508. operator_func(a, b, dst);
  509. ip += 5;
  510. }
  511. DISPATCH_OPCODE;
  512. OPCODE(OPCODE_EXTENDS_TEST) {
  513. CHECK_SPACE(4);
  514. GET_INSTRUCTION_ARG(a, 0);
  515. GET_INSTRUCTION_ARG(b, 1);
  516. GET_INSTRUCTION_ARG(dst, 2);
  517. #ifdef DEBUG_ENABLED
  518. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  519. err_text = "Right operand of 'is' is not a class.";
  520. OPCODE_BREAK;
  521. }
  522. #endif
  523. bool extends_ok = false;
  524. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  525. #ifdef DEBUG_ENABLED
  526. bool was_freed;
  527. Object *obj_A = a->get_validated_object_with_check(was_freed);
  528. if (was_freed) {
  529. err_text = "Left operand of 'is' is a previously freed instance.";
  530. OPCODE_BREAK;
  531. }
  532. Object *obj_B = b->get_validated_object_with_check(was_freed);
  533. if (was_freed) {
  534. err_text = "Right operand of 'is' is a previously freed instance.";
  535. OPCODE_BREAK;
  536. }
  537. #else
  538. Object *obj_A = *a;
  539. Object *obj_B = *b;
  540. #endif // DEBUG_ENABLED
  541. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  542. if (scr_B) {
  543. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  544. //in other situation, this shoul return false.
  545. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  546. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  547. //bool found=false;
  548. while (cmp) {
  549. if (cmp == scr_B) {
  550. //inherits from script, all ok
  551. extends_ok = true;
  552. break;
  553. }
  554. cmp = cmp->_base;
  555. }
  556. }
  557. } else {
  558. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  559. #ifdef DEBUG_ENABLED
  560. if (!nc) {
  561. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  562. OPCODE_BREAK;
  563. }
  564. #endif
  565. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  566. }
  567. }
  568. *dst = extends_ok;
  569. ip += 4;
  570. }
  571. DISPATCH_OPCODE;
  572. OPCODE(OPCODE_IS_BUILTIN) {
  573. CHECK_SPACE(4);
  574. GET_INSTRUCTION_ARG(value, 0);
  575. GET_INSTRUCTION_ARG(dst, 1);
  576. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  577. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  578. *dst = value->get_type() == var_type;
  579. ip += 4;
  580. }
  581. DISPATCH_OPCODE;
  582. OPCODE(OPCODE_SET_KEYED) {
  583. CHECK_SPACE(3);
  584. GET_INSTRUCTION_ARG(dst, 0);
  585. GET_INSTRUCTION_ARG(index, 1);
  586. GET_INSTRUCTION_ARG(value, 2);
  587. bool valid;
  588. dst->set(*index, *value, &valid);
  589. #ifdef DEBUG_ENABLED
  590. if (!valid) {
  591. String v = index->operator String();
  592. if (v != "") {
  593. v = "'" + v + "'";
  594. } else {
  595. v = "of type '" + _get_var_type(index) + "'";
  596. }
  597. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  598. OPCODE_BREAK;
  599. }
  600. #endif
  601. ip += 4;
  602. }
  603. DISPATCH_OPCODE;
  604. OPCODE(OPCODE_SET_KEYED_VALIDATED) {
  605. CHECK_SPACE(4);
  606. GET_INSTRUCTION_ARG(dst, 0);
  607. GET_INSTRUCTION_ARG(index, 1);
  608. GET_INSTRUCTION_ARG(value, 2);
  609. int index_setter = _code_ptr[ip + 4];
  610. GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
  611. const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
  612. bool valid;
  613. setter(dst, index, value, valid);
  614. #ifdef DEBUG_ENABLED
  615. if (!valid) {
  616. String v = index->operator String();
  617. if (v != "") {
  618. v = "'" + v + "'";
  619. } else {
  620. v = "of type '" + _get_var_type(index) + "'";
  621. }
  622. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  623. OPCODE_BREAK;
  624. }
  625. #endif
  626. ip += 5;
  627. }
  628. DISPATCH_OPCODE;
  629. OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
  630. CHECK_SPACE(4);
  631. GET_INSTRUCTION_ARG(dst, 0);
  632. GET_INSTRUCTION_ARG(index, 1);
  633. GET_INSTRUCTION_ARG(value, 2);
  634. int index_setter = _code_ptr[ip + 4];
  635. GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
  636. const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
  637. int64_t int_index = *VariantInternal::get_int(index);
  638. bool oob;
  639. setter(dst, int_index, value, oob);
  640. #ifdef DEBUG_ENABLED
  641. if (oob) {
  642. String v = index->operator String();
  643. if (v != "") {
  644. v = "'" + v + "'";
  645. } else {
  646. v = "of type '" + _get_var_type(index) + "'";
  647. }
  648. err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
  649. OPCODE_BREAK;
  650. }
  651. #endif
  652. ip += 5;
  653. }
  654. DISPATCH_OPCODE;
  655. OPCODE(OPCODE_GET_KEYED) {
  656. CHECK_SPACE(3);
  657. GET_INSTRUCTION_ARG(src, 0);
  658. GET_INSTRUCTION_ARG(index, 1);
  659. GET_INSTRUCTION_ARG(dst, 2);
  660. bool valid;
  661. #ifdef DEBUG_ENABLED
  662. // Allow better error message in cases where src and dst are the same stack position.
  663. Variant ret = src->get(*index, &valid);
  664. #else
  665. *dst = src->get(*index, &valid);
  666. #endif
  667. #ifdef DEBUG_ENABLED
  668. if (!valid) {
  669. String v = index->operator String();
  670. if (v != "") {
  671. v = "'" + v + "'";
  672. } else {
  673. v = "of type '" + _get_var_type(index) + "'";
  674. }
  675. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  676. OPCODE_BREAK;
  677. }
  678. *dst = ret;
  679. #endif
  680. ip += 4;
  681. }
  682. DISPATCH_OPCODE;
  683. OPCODE(OPCODE_GET_KEYED_VALIDATED) {
  684. CHECK_SPACE(4);
  685. GET_INSTRUCTION_ARG(src, 0);
  686. GET_INSTRUCTION_ARG(key, 1);
  687. GET_INSTRUCTION_ARG(dst, 2);
  688. int index_getter = _code_ptr[ip + 4];
  689. GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
  690. const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
  691. bool valid;
  692. #ifdef DEBUG_ENABLED
  693. // Allow better error message in cases where src and dst are the same stack position.
  694. Variant ret;
  695. getter(src, key, &ret, valid);
  696. #else
  697. getter(src, key, dst, valid);
  698. #endif
  699. #ifdef DEBUG_ENABLED
  700. if (!valid) {
  701. String v = key->operator String();
  702. if (v != "") {
  703. v = "'" + v + "'";
  704. } else {
  705. v = "of type '" + _get_var_type(key) + "'";
  706. }
  707. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  708. OPCODE_BREAK;
  709. }
  710. *dst = ret;
  711. #endif
  712. ip += 5;
  713. }
  714. DISPATCH_OPCODE;
  715. OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
  716. CHECK_SPACE(4);
  717. GET_INSTRUCTION_ARG(src, 0);
  718. GET_INSTRUCTION_ARG(index, 1);
  719. GET_INSTRUCTION_ARG(dst, 2);
  720. int index_getter = _code_ptr[ip + 4];
  721. GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
  722. const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
  723. int64_t int_index = *VariantInternal::get_int(index);
  724. bool oob;
  725. getter(src, int_index, dst, oob);
  726. #ifdef DEBUG_ENABLED
  727. if (oob) {
  728. String v = index->operator String();
  729. if (v != "") {
  730. v = "'" + v + "'";
  731. } else {
  732. v = "of type '" + _get_var_type(index) + "'";
  733. }
  734. err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
  735. OPCODE_BREAK;
  736. }
  737. #endif
  738. ip += 5;
  739. }
  740. DISPATCH_OPCODE;
  741. OPCODE(OPCODE_SET_NAMED) {
  742. CHECK_SPACE(3);
  743. GET_INSTRUCTION_ARG(dst, 0);
  744. GET_INSTRUCTION_ARG(value, 1);
  745. int indexname = _code_ptr[ip + 3];
  746. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  747. const StringName *index = &_global_names_ptr[indexname];
  748. bool valid;
  749. dst->set_named(*index, *value, valid);
  750. #ifdef DEBUG_ENABLED
  751. if (!valid) {
  752. String err_type;
  753. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  754. OPCODE_BREAK;
  755. }
  756. #endif
  757. ip += 4;
  758. }
  759. DISPATCH_OPCODE;
  760. OPCODE(OPCODE_SET_NAMED_VALIDATED) {
  761. CHECK_SPACE(3);
  762. GET_INSTRUCTION_ARG(dst, 0);
  763. GET_INSTRUCTION_ARG(value, 1);
  764. int index_setter = _code_ptr[ip + 3];
  765. GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
  766. const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
  767. setter(dst, value);
  768. ip += 4;
  769. }
  770. DISPATCH_OPCODE;
  771. OPCODE(OPCODE_GET_NAMED) {
  772. CHECK_SPACE(4);
  773. GET_INSTRUCTION_ARG(src, 0);
  774. GET_INSTRUCTION_ARG(dst, 1);
  775. int indexname = _code_ptr[ip + 3];
  776. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  777. const StringName *index = &_global_names_ptr[indexname];
  778. bool valid;
  779. #ifdef DEBUG_ENABLED
  780. //allow better error message in cases where src and dst are the same stack position
  781. Variant ret = src->get_named(*index, valid);
  782. #else
  783. *dst = src->get_named(*index, valid);
  784. #endif
  785. #ifdef DEBUG_ENABLED
  786. if (!valid) {
  787. if (src->has_method(*index)) {
  788. 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() + "\") ?";
  789. } else {
  790. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  791. }
  792. OPCODE_BREAK;
  793. }
  794. *dst = ret;
  795. #endif
  796. ip += 4;
  797. }
  798. DISPATCH_OPCODE;
  799. OPCODE(OPCODE_GET_NAMED_VALIDATED) {
  800. CHECK_SPACE(3);
  801. GET_INSTRUCTION_ARG(src, 0);
  802. GET_INSTRUCTION_ARG(dst, 1);
  803. int index_getter = _code_ptr[ip + 3];
  804. GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
  805. const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
  806. getter(src, dst);
  807. ip += 4;
  808. }
  809. DISPATCH_OPCODE;
  810. OPCODE(OPCODE_SET_MEMBER) {
  811. CHECK_SPACE(3);
  812. GET_INSTRUCTION_ARG(src, 0);
  813. int indexname = _code_ptr[ip + 2];
  814. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  815. const StringName *index = &_global_names_ptr[indexname];
  816. bool valid;
  817. #ifndef DEBUG_ENABLED
  818. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  819. #else
  820. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  821. if (!ok) {
  822. err_text = "Internal error setting property: " + String(*index);
  823. OPCODE_BREAK;
  824. } else if (!valid) {
  825. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  826. OPCODE_BREAK;
  827. }
  828. #endif
  829. ip += 3;
  830. }
  831. DISPATCH_OPCODE;
  832. OPCODE(OPCODE_GET_MEMBER) {
  833. CHECK_SPACE(3);
  834. GET_INSTRUCTION_ARG(dst, 0);
  835. int indexname = _code_ptr[ip + 2];
  836. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  837. const StringName *index = &_global_names_ptr[indexname];
  838. #ifndef DEBUG_ENABLED
  839. ClassDB::get_property(p_instance->owner, *index, *dst);
  840. #else
  841. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  842. if (!ok) {
  843. err_text = "Internal error getting property: " + String(*index);
  844. OPCODE_BREAK;
  845. }
  846. #endif
  847. ip += 3;
  848. }
  849. DISPATCH_OPCODE;
  850. OPCODE(OPCODE_ASSIGN) {
  851. CHECK_SPACE(3);
  852. GET_INSTRUCTION_ARG(dst, 0);
  853. GET_INSTRUCTION_ARG(src, 1);
  854. *dst = *src;
  855. ip += 3;
  856. }
  857. DISPATCH_OPCODE;
  858. OPCODE(OPCODE_ASSIGN_TRUE) {
  859. CHECK_SPACE(2);
  860. GET_INSTRUCTION_ARG(dst, 0);
  861. *dst = true;
  862. ip += 2;
  863. }
  864. DISPATCH_OPCODE;
  865. OPCODE(OPCODE_ASSIGN_FALSE) {
  866. CHECK_SPACE(2);
  867. GET_INSTRUCTION_ARG(dst, 0);
  868. *dst = false;
  869. ip += 2;
  870. }
  871. DISPATCH_OPCODE;
  872. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  873. CHECK_SPACE(4);
  874. GET_INSTRUCTION_ARG(dst, 0);
  875. GET_INSTRUCTION_ARG(src, 1);
  876. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  877. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  878. if (src->get_type() != var_type) {
  879. #ifdef DEBUG_ENABLED
  880. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  881. #endif // DEBUG_ENABLED
  882. Callable::CallError ce;
  883. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  884. } else {
  885. #ifdef DEBUG_ENABLED
  886. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  887. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  888. OPCODE_BREAK;
  889. }
  890. } else {
  891. #endif // DEBUG_ENABLED
  892. *dst = *src;
  893. }
  894. ip += 4;
  895. }
  896. DISPATCH_OPCODE;
  897. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  898. CHECK_SPACE(4);
  899. GET_INSTRUCTION_ARG(dst, 0);
  900. GET_INSTRUCTION_ARG(src, 1);
  901. #ifdef DEBUG_ENABLED
  902. GET_INSTRUCTION_ARG(type, 2);
  903. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  904. GD_ERR_BREAK(!nc);
  905. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  906. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  907. "' to a variable of type '" + nc->get_name() + "'.";
  908. OPCODE_BREAK;
  909. }
  910. Object *src_obj = src->operator Object *();
  911. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  912. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  913. "' to a variable of type '" + nc->get_name() + "'.";
  914. OPCODE_BREAK;
  915. }
  916. #endif // DEBUG_ENABLED
  917. *dst = *src;
  918. ip += 4;
  919. }
  920. DISPATCH_OPCODE;
  921. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  922. CHECK_SPACE(4);
  923. GET_INSTRUCTION_ARG(dst, 0);
  924. GET_INSTRUCTION_ARG(src, 1);
  925. #ifdef DEBUG_ENABLED
  926. GET_INSTRUCTION_ARG(type, 2);
  927. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  928. GD_ERR_BREAK(!base_type);
  929. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  930. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  931. OPCODE_BREAK;
  932. }
  933. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  934. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  935. if (!scr_inst) {
  936. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  937. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  938. OPCODE_BREAK;
  939. }
  940. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  941. bool valid = false;
  942. while (src_type) {
  943. if (src_type == base_type) {
  944. valid = true;
  945. break;
  946. }
  947. src_type = src_type->get_base_script().ptr();
  948. }
  949. if (!valid) {
  950. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  951. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  952. OPCODE_BREAK;
  953. }
  954. }
  955. #endif // DEBUG_ENABLED
  956. *dst = *src;
  957. ip += 4;
  958. }
  959. DISPATCH_OPCODE;
  960. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  961. CHECK_SPACE(4);
  962. GET_INSTRUCTION_ARG(src, 0);
  963. GET_INSTRUCTION_ARG(dst, 1);
  964. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  965. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  966. Callable::CallError err;
  967. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  968. #ifdef DEBUG_ENABLED
  969. if (err.error != Callable::CallError::CALL_OK) {
  970. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  971. OPCODE_BREAK;
  972. }
  973. #endif
  974. ip += 4;
  975. }
  976. DISPATCH_OPCODE;
  977. OPCODE(OPCODE_CAST_TO_NATIVE) {
  978. CHECK_SPACE(4);
  979. GET_INSTRUCTION_ARG(src, 0);
  980. GET_INSTRUCTION_ARG(dst, 1);
  981. GET_INSTRUCTION_ARG(to_type, 2);
  982. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  983. GD_ERR_BREAK(!nc);
  984. #ifdef DEBUG_ENABLED
  985. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  986. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  987. OPCODE_BREAK;
  988. }
  989. #endif
  990. Object *src_obj = src->operator Object *();
  991. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  992. *dst = Variant(); // invalid cast, assign NULL
  993. } else {
  994. *dst = *src;
  995. }
  996. ip += 4;
  997. }
  998. DISPATCH_OPCODE;
  999. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1000. CHECK_SPACE(4);
  1001. GET_INSTRUCTION_ARG(src, 0);
  1002. GET_INSTRUCTION_ARG(dst, 1);
  1003. GET_INSTRUCTION_ARG(to_type, 2);
  1004. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1005. GD_ERR_BREAK(!base_type);
  1006. #ifdef DEBUG_ENABLED
  1007. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1008. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1009. OPCODE_BREAK;
  1010. }
  1011. #endif
  1012. bool valid = false;
  1013. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1014. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1015. if (scr_inst) {
  1016. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1017. while (src_type) {
  1018. if (src_type == base_type) {
  1019. valid = true;
  1020. break;
  1021. }
  1022. src_type = src_type->get_base_script().ptr();
  1023. }
  1024. }
  1025. }
  1026. if (valid) {
  1027. *dst = *src; // Valid cast, copy the source object
  1028. } else {
  1029. *dst = Variant(); // invalid cast, assign NULL
  1030. }
  1031. ip += 4;
  1032. }
  1033. DISPATCH_OPCODE;
  1034. OPCODE(OPCODE_CONSTRUCT) {
  1035. CHECK_SPACE(2 + instr_arg_count);
  1036. ip += instr_arg_count;
  1037. int argc = _code_ptr[ip + 1];
  1038. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1039. Variant **argptrs = instruction_args;
  1040. GET_INSTRUCTION_ARG(dst, argc);
  1041. Callable::CallError err;
  1042. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1043. #ifdef DEBUG_ENABLED
  1044. if (err.error != Callable::CallError::CALL_OK) {
  1045. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  1046. OPCODE_BREAK;
  1047. }
  1048. #endif
  1049. ip += 3;
  1050. }
  1051. DISPATCH_OPCODE;
  1052. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1053. CHECK_SPACE(1 + instr_arg_count);
  1054. ip += instr_arg_count;
  1055. int argc = _code_ptr[ip + 1];
  1056. Array array;
  1057. array.resize(argc);
  1058. for (int i = 0; i < argc; i++) {
  1059. array[i] = *(instruction_args[i]);
  1060. }
  1061. GET_INSTRUCTION_ARG(dst, argc);
  1062. *dst = array;
  1063. ip += 2;
  1064. }
  1065. DISPATCH_OPCODE;
  1066. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1067. CHECK_SPACE(2 + instr_arg_count);
  1068. ip += instr_arg_count;
  1069. int argc = _code_ptr[ip + 1];
  1070. Dictionary dict;
  1071. for (int i = 0; i < argc; i++) {
  1072. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1073. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1074. dict[*k] = *v;
  1075. }
  1076. GET_INSTRUCTION_ARG(dst, argc * 2);
  1077. *dst = dict;
  1078. ip += 2;
  1079. }
  1080. DISPATCH_OPCODE;
  1081. OPCODE(OPCODE_CALL_ASYNC)
  1082. OPCODE(OPCODE_CALL_RETURN)
  1083. OPCODE(OPCODE_CALL) {
  1084. CHECK_SPACE(3 + instr_arg_count);
  1085. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  1086. #ifdef DEBUG_ENABLED
  1087. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  1088. #endif
  1089. ip += instr_arg_count;
  1090. int argc = _code_ptr[ip + 1];
  1091. GD_ERR_BREAK(argc < 0);
  1092. int methodname_idx = _code_ptr[ip + 2];
  1093. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1094. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1095. GET_INSTRUCTION_ARG(base, argc);
  1096. Variant **argptrs = instruction_args;
  1097. #ifdef DEBUG_ENABLED
  1098. uint64_t call_time = 0;
  1099. if (GDScriptLanguage::get_singleton()->profiling) {
  1100. call_time = OS::get_singleton()->get_ticks_usec();
  1101. }
  1102. #endif
  1103. Callable::CallError err;
  1104. if (call_ret) {
  1105. GET_INSTRUCTION_ARG(ret, argc + 1);
  1106. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  1107. #ifdef DEBUG_ENABLED
  1108. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1109. // Check if getting a function state without await.
  1110. bool was_freed = false;
  1111. Object *obj = ret->get_validated_object_with_check(was_freed);
  1112. if (was_freed) {
  1113. err_text = "Got a freed object as a result of the call.";
  1114. OPCODE_BREAK;
  1115. }
  1116. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1117. err_text = R"(Trying to call an async function without "await".)";
  1118. OPCODE_BREAK;
  1119. }
  1120. }
  1121. #endif
  1122. } else {
  1123. Variant ret;
  1124. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  1125. }
  1126. #ifdef DEBUG_ENABLED
  1127. if (GDScriptLanguage::get_singleton()->profiling) {
  1128. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1129. }
  1130. if (err.error != Callable::CallError::CALL_OK) {
  1131. String methodstr = *methodname;
  1132. String basestr = _get_var_type(base);
  1133. if (methodstr == "call") {
  1134. if (argc >= 1) {
  1135. methodstr = String(*argptrs[0]) + " (via call)";
  1136. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1137. err.argument += 1;
  1138. }
  1139. }
  1140. } else if (methodstr == "free") {
  1141. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1142. if (base->is_ref()) {
  1143. err_text = "Attempted to free a reference.";
  1144. OPCODE_BREAK;
  1145. } else if (base->get_type() == Variant::OBJECT) {
  1146. err_text = "Attempted to free a locked object (calling or emitting).";
  1147. OPCODE_BREAK;
  1148. }
  1149. }
  1150. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1151. if (argc >= 1) {
  1152. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1153. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1154. err.argument += 1;
  1155. }
  1156. }
  1157. }
  1158. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1159. OPCODE_BREAK;
  1160. }
  1161. #endif
  1162. ip += 3;
  1163. }
  1164. DISPATCH_OPCODE;
  1165. OPCODE(OPCODE_CALL_METHOD_BIND)
  1166. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1167. CHECK_SPACE(3 + instr_arg_count);
  1168. bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  1169. ip += instr_arg_count;
  1170. int argc = _code_ptr[ip + 1];
  1171. GD_ERR_BREAK(argc < 0);
  1172. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1173. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1174. GET_INSTRUCTION_ARG(base, argc);
  1175. #ifdef DEBUG_ENABLED
  1176. bool freed = false;
  1177. Object *base_obj = base->get_validated_object_with_check(freed);
  1178. if (freed) {
  1179. err_text = "Trying to call a function on a previously freed instance.";
  1180. OPCODE_BREAK;
  1181. } else if (!base_obj) {
  1182. err_text = "Trying to call a function on a null value.";
  1183. OPCODE_BREAK;
  1184. }
  1185. #else
  1186. Object *base_obj = base->operator Object *();
  1187. #endif
  1188. Variant **argptrs = instruction_args;
  1189. #ifdef DEBUG_ENABLED
  1190. uint64_t call_time = 0;
  1191. if (GDScriptLanguage::get_singleton()->profiling) {
  1192. call_time = OS::get_singleton()->get_ticks_usec();
  1193. }
  1194. #endif
  1195. Callable::CallError err;
  1196. if (call_ret) {
  1197. GET_INSTRUCTION_ARG(ret, argc + 1);
  1198. *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1199. } else {
  1200. method->call(base_obj, (const Variant **)argptrs, argc, err);
  1201. }
  1202. #ifdef DEBUG_ENABLED
  1203. if (GDScriptLanguage::get_singleton()->profiling) {
  1204. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1205. }
  1206. if (err.error != Callable::CallError::CALL_OK) {
  1207. String methodstr = method->get_name();
  1208. String basestr = _get_var_type(base);
  1209. if (methodstr == "call") {
  1210. if (argc >= 1) {
  1211. methodstr = String(*argptrs[0]) + " (via call)";
  1212. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1213. err.argument += 1;
  1214. }
  1215. }
  1216. } else if (methodstr == "free") {
  1217. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1218. if (base->is_ref()) {
  1219. err_text = "Attempted to free a reference.";
  1220. OPCODE_BREAK;
  1221. } else if (base->get_type() == Variant::OBJECT) {
  1222. err_text = "Attempted to free a locked object (calling or emitting).";
  1223. OPCODE_BREAK;
  1224. }
  1225. }
  1226. }
  1227. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1228. OPCODE_BREAK;
  1229. }
  1230. #endif
  1231. ip += 3;
  1232. }
  1233. DISPATCH_OPCODE;
  1234. #ifdef DEBUG_ENABLED
  1235. #define OPCODE_CALL_PTR(m_type) \
  1236. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1237. CHECK_SPACE(3 + instr_arg_count); \
  1238. ip += instr_arg_count; \
  1239. int argc = _code_ptr[ip + 1]; \
  1240. GD_ERR_BREAK(argc < 0); \
  1241. GET_INSTRUCTION_ARG(base, argc); \
  1242. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
  1243. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1244. bool freed = false; \
  1245. Object *base_obj = base->get_validated_object_with_check(freed); \
  1246. if (freed) { \
  1247. err_text = "Trying to call a function on a previously freed instance."; \
  1248. OPCODE_BREAK; \
  1249. } else if (!base_obj) { \
  1250. err_text = "Trying to call a function on a null value."; \
  1251. OPCODE_BREAK; \
  1252. } \
  1253. const void **argptrs = call_args_ptr; \
  1254. for (int i = 0; i < argc; i++) { \
  1255. GET_INSTRUCTION_ARG(v, i); \
  1256. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1257. } \
  1258. uint64_t call_time = 0; \
  1259. if (GDScriptLanguage::get_singleton()->profiling) { \
  1260. call_time = OS::get_singleton()->get_ticks_usec(); \
  1261. } \
  1262. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1263. VariantInternal::initialize(ret, Variant::m_type); \
  1264. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1265. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1266. if (GDScriptLanguage::get_singleton()->profiling) { \
  1267. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
  1268. } \
  1269. ip += 3; \
  1270. } \
  1271. DISPATCH_OPCODE
  1272. #else
  1273. #define OPCODE_CALL_PTR(m_type) \
  1274. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1275. CHECK_SPACE(3 + instr_arg_count); \
  1276. int argc = _code_ptr[ip + 1]; \
  1277. GET_INSTRUCTION_ARG(base, argc); \
  1278. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1279. Object *base_obj = *VariantInternal::get_object(base); \
  1280. const void **argptrs = call_args_ptr; \
  1281. for (int i = 0; i < argc; i++) { \
  1282. GET_INSTRUCTION_ARG(v, i); \
  1283. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1284. } \
  1285. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1286. VariantInternal::initialize(ret, Variant::m_type); \
  1287. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1288. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1289. ip += 3; \
  1290. } \
  1291. DISPATCH_OPCODE
  1292. #endif
  1293. OPCODE_CALL_PTR(BOOL);
  1294. OPCODE_CALL_PTR(INT);
  1295. OPCODE_CALL_PTR(FLOAT);
  1296. OPCODE_CALL_PTR(STRING);
  1297. OPCODE_CALL_PTR(VECTOR2);
  1298. OPCODE_CALL_PTR(VECTOR2I);
  1299. OPCODE_CALL_PTR(RECT2);
  1300. OPCODE_CALL_PTR(RECT2I);
  1301. OPCODE_CALL_PTR(VECTOR3);
  1302. OPCODE_CALL_PTR(VECTOR3I);
  1303. OPCODE_CALL_PTR(TRANSFORM2D);
  1304. OPCODE_CALL_PTR(PLANE);
  1305. OPCODE_CALL_PTR(QUAT);
  1306. OPCODE_CALL_PTR(AABB);
  1307. OPCODE_CALL_PTR(BASIS);
  1308. OPCODE_CALL_PTR(TRANSFORM);
  1309. OPCODE_CALL_PTR(COLOR);
  1310. OPCODE_CALL_PTR(STRING_NAME);
  1311. OPCODE_CALL_PTR(NODE_PATH);
  1312. OPCODE_CALL_PTR(RID);
  1313. OPCODE_CALL_PTR(CALLABLE);
  1314. OPCODE_CALL_PTR(SIGNAL);
  1315. OPCODE_CALL_PTR(DICTIONARY);
  1316. OPCODE_CALL_PTR(ARRAY);
  1317. OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
  1318. OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
  1319. OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
  1320. OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
  1321. OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
  1322. OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
  1323. OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
  1324. OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
  1325. OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
  1326. OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
  1327. CHECK_SPACE(3 + instr_arg_count);
  1328. ip += instr_arg_count;
  1329. int argc = _code_ptr[ip + 1];
  1330. GD_ERR_BREAK(argc < 0);
  1331. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1332. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1333. GET_INSTRUCTION_ARG(base, argc);
  1334. #ifdef DEBUG_ENABLED
  1335. bool freed = false;
  1336. Object *base_obj = base->get_validated_object_with_check(freed);
  1337. if (freed) {
  1338. err_text = "Trying to call a function on a previously freed instance.";
  1339. OPCODE_BREAK;
  1340. } else if (!base_obj) {
  1341. err_text = "Trying to call a function on a null value.";
  1342. OPCODE_BREAK;
  1343. }
  1344. #else
  1345. Object *base_obj = *VariantInternal::get_object(base);
  1346. #endif
  1347. const void **argptrs = call_args_ptr;
  1348. for (int i = 0; i < argc; i++) {
  1349. GET_INSTRUCTION_ARG(v, i);
  1350. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1351. }
  1352. #ifdef DEBUG_ENABLED
  1353. uint64_t call_time = 0;
  1354. if (GDScriptLanguage::get_singleton()->profiling) {
  1355. call_time = OS::get_singleton()->get_ticks_usec();
  1356. }
  1357. #endif
  1358. GET_INSTRUCTION_ARG(ret, argc + 1);
  1359. VariantInternal::initialize(ret, Variant::OBJECT);
  1360. Object **ret_opaque = VariantInternal::get_object(ret);
  1361. method->ptrcall(base_obj, argptrs, ret_opaque);
  1362. VariantInternal::set_object(ret, *ret_opaque);
  1363. #ifdef DEBUG_ENABLED
  1364. if (GDScriptLanguage::get_singleton()->profiling) {
  1365. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1366. }
  1367. #endif
  1368. ip += 3;
  1369. }
  1370. DISPATCH_OPCODE;
  1371. OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
  1372. CHECK_SPACE(3 + instr_arg_count);
  1373. ip += instr_arg_count;
  1374. int argc = _code_ptr[ip + 1];
  1375. GD_ERR_BREAK(argc < 0);
  1376. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1377. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1378. GET_INSTRUCTION_ARG(base, argc);
  1379. #ifdef DEBUG_ENABLED
  1380. bool freed = false;
  1381. Object *base_obj = base->get_validated_object_with_check(freed);
  1382. if (freed) {
  1383. err_text = "Trying to call a function on a previously freed instance.";
  1384. OPCODE_BREAK;
  1385. } else if (!base_obj) {
  1386. err_text = "Trying to call a function on a null value.";
  1387. OPCODE_BREAK;
  1388. }
  1389. #else
  1390. Object *base_obj = *VariantInternal::get_object(base);
  1391. #endif
  1392. const void **argptrs = call_args_ptr;
  1393. for (int i = 0; i < argc; i++) {
  1394. GET_INSTRUCTION_ARG(v, i);
  1395. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1396. }
  1397. #ifdef DEBUG_ENABLED
  1398. uint64_t call_time = 0;
  1399. if (GDScriptLanguage::get_singleton()->profiling) {
  1400. call_time = OS::get_singleton()->get_ticks_usec();
  1401. }
  1402. #endif
  1403. GET_INSTRUCTION_ARG(ret, argc + 1);
  1404. VariantInternal::initialize(ret, Variant::NIL);
  1405. method->ptrcall(base_obj, argptrs, nullptr);
  1406. #ifdef DEBUG_ENABLED
  1407. if (GDScriptLanguage::get_singleton()->profiling) {
  1408. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1409. }
  1410. #endif
  1411. ip += 3;
  1412. }
  1413. DISPATCH_OPCODE;
  1414. OPCODE(OPCODE_CALL_BUILT_IN) {
  1415. CHECK_SPACE(3 + instr_arg_count);
  1416. ip += instr_arg_count;
  1417. int argc = _code_ptr[ip + 1];
  1418. GD_ERR_BREAK(argc < 0);
  1419. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 2]);
  1420. Variant **argptrs = instruction_args;
  1421. GET_INSTRUCTION_ARG(dst, argc);
  1422. Callable::CallError err;
  1423. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  1424. #ifdef DEBUG_ENABLED
  1425. if (err.error != Callable::CallError::CALL_OK) {
  1426. String methodstr = GDScriptFunctions::get_func_name(func);
  1427. if (dst->get_type() == Variant::STRING) {
  1428. //call provided error string
  1429. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  1430. } else {
  1431. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  1432. }
  1433. OPCODE_BREAK;
  1434. }
  1435. #endif
  1436. ip += 3;
  1437. }
  1438. DISPATCH_OPCODE;
  1439. OPCODE(OPCODE_CALL_SELF_BASE) {
  1440. CHECK_SPACE(3 + instr_arg_count);
  1441. ip += instr_arg_count;
  1442. int self_fun = _code_ptr[ip + 1];
  1443. #ifdef DEBUG_ENABLED
  1444. if (self_fun < 0 || self_fun >= _global_names_count) {
  1445. err_text = "compiler bug, function name not found";
  1446. OPCODE_BREAK;
  1447. }
  1448. #endif
  1449. const StringName *methodname = &_global_names_ptr[self_fun];
  1450. int argc = _code_ptr[ip + 2];
  1451. GD_ERR_BREAK(argc < 0);
  1452. Variant **argptrs = instruction_args;
  1453. GET_INSTRUCTION_ARG(dst, argc);
  1454. const GDScript *gds = _script;
  1455. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  1456. while (gds->base.ptr()) {
  1457. gds = gds->base.ptr();
  1458. E = gds->member_functions.find(*methodname);
  1459. if (E) {
  1460. break;
  1461. }
  1462. }
  1463. Callable::CallError err;
  1464. if (E) {
  1465. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1466. } else if (gds->native.ptr()) {
  1467. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1468. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1469. if (!mb) {
  1470. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1471. } else {
  1472. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1473. }
  1474. } else {
  1475. err.error = Callable::CallError::CALL_OK;
  1476. }
  1477. } else {
  1478. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1479. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1480. } else {
  1481. err.error = Callable::CallError::CALL_OK;
  1482. }
  1483. }
  1484. if (err.error != Callable::CallError::CALL_OK) {
  1485. String methodstr = *methodname;
  1486. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1487. OPCODE_BREAK;
  1488. }
  1489. ip += 3;
  1490. }
  1491. DISPATCH_OPCODE;
  1492. OPCODE(OPCODE_AWAIT) {
  1493. CHECK_SPACE(2);
  1494. // Do the oneshot connect.
  1495. GET_INSTRUCTION_ARG(argobj, 0);
  1496. Signal sig;
  1497. bool is_signal = true;
  1498. {
  1499. Variant result = *argobj;
  1500. if (argobj->get_type() == Variant::OBJECT) {
  1501. bool was_freed = false;
  1502. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1503. if (was_freed) {
  1504. err_text = "Trying to await on a freed object.";
  1505. OPCODE_BREAK;
  1506. }
  1507. // Is this even possible to be null at this point?
  1508. if (obj) {
  1509. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1510. static StringName completed = _scs_create("completed");
  1511. result = Signal(obj, completed);
  1512. }
  1513. }
  1514. }
  1515. if (result.get_type() != Variant::SIGNAL) {
  1516. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1517. // The stack pointer should be the same, so we don't need to set a return value.
  1518. is_signal = false;
  1519. } else {
  1520. sig = result;
  1521. }
  1522. }
  1523. if (is_signal) {
  1524. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1525. gdfs->function = this;
  1526. gdfs->state.stack.resize(alloca_size);
  1527. //copy variant stack
  1528. for (int i = 0; i < _stack_size; i++) {
  1529. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1530. }
  1531. gdfs->state.stack_size = _stack_size;
  1532. gdfs->state.self = self;
  1533. gdfs->state.alloca_size = alloca_size;
  1534. gdfs->state.ip = ip + 2;
  1535. gdfs->state.line = line;
  1536. gdfs->state.script = _script;
  1537. {
  1538. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1539. _script->pending_func_states.add(&gdfs->scripts_list);
  1540. if (p_instance) {
  1541. gdfs->state.instance = p_instance;
  1542. p_instance->pending_func_states.add(&gdfs->instances_list);
  1543. } else {
  1544. gdfs->state.instance = nullptr;
  1545. }
  1546. }
  1547. #ifdef DEBUG_ENABLED
  1548. gdfs->state.function_name = name;
  1549. gdfs->state.script_path = _script->get_path();
  1550. #endif
  1551. gdfs->state.defarg = defarg;
  1552. gdfs->function = this;
  1553. retvalue = gdfs;
  1554. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1555. if (err != OK) {
  1556. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1557. OPCODE_BREAK;
  1558. }
  1559. #ifdef DEBUG_ENABLED
  1560. exit_ok = true;
  1561. awaited = true;
  1562. #endif
  1563. OPCODE_BREAK;
  1564. }
  1565. }
  1566. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1567. OPCODE(OPCODE_AWAIT_RESUME) {
  1568. CHECK_SPACE(2);
  1569. #ifdef DEBUG_ENABLED
  1570. if (!p_state) {
  1571. err_text = ("Invalid Resume (bug?)");
  1572. OPCODE_BREAK;
  1573. }
  1574. #endif
  1575. GET_INSTRUCTION_ARG(result, 0);
  1576. *result = p_state->result;
  1577. ip += 2;
  1578. }
  1579. DISPATCH_OPCODE;
  1580. OPCODE(OPCODE_JUMP) {
  1581. CHECK_SPACE(2);
  1582. int to = _code_ptr[ip + 1];
  1583. GD_ERR_BREAK(to < 0 || to > _code_size);
  1584. ip = to;
  1585. }
  1586. DISPATCH_OPCODE;
  1587. OPCODE(OPCODE_JUMP_IF) {
  1588. CHECK_SPACE(3);
  1589. GET_INSTRUCTION_ARG(test, 0);
  1590. bool result = test->booleanize();
  1591. if (result) {
  1592. int to = _code_ptr[ip + 2];
  1593. GD_ERR_BREAK(to < 0 || to > _code_size);
  1594. ip = to;
  1595. } else {
  1596. ip += 3;
  1597. }
  1598. }
  1599. DISPATCH_OPCODE;
  1600. OPCODE(OPCODE_JUMP_IF_NOT) {
  1601. CHECK_SPACE(3);
  1602. GET_INSTRUCTION_ARG(test, 0);
  1603. bool result = test->booleanize();
  1604. if (!result) {
  1605. int to = _code_ptr[ip + 2];
  1606. GD_ERR_BREAK(to < 0 || to > _code_size);
  1607. ip = to;
  1608. } else {
  1609. ip += 3;
  1610. }
  1611. }
  1612. DISPATCH_OPCODE;
  1613. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1614. CHECK_SPACE(2);
  1615. ip = _default_arg_ptr[defarg];
  1616. }
  1617. DISPATCH_OPCODE;
  1618. OPCODE(OPCODE_RETURN) {
  1619. CHECK_SPACE(2);
  1620. GET_INSTRUCTION_ARG(r, 0);
  1621. retvalue = *r;
  1622. #ifdef DEBUG_ENABLED
  1623. exit_ok = true;
  1624. #endif
  1625. OPCODE_BREAK;
  1626. }
  1627. OPCODE(OPCODE_ITERATE_BEGIN) {
  1628. CHECK_SPACE(8); // Space for this and a regular iterate.
  1629. GET_INSTRUCTION_ARG(counter, 0);
  1630. GET_INSTRUCTION_ARG(container, 1);
  1631. bool valid;
  1632. if (!container->iter_init(*counter, valid)) {
  1633. #ifdef DEBUG_ENABLED
  1634. if (!valid) {
  1635. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1636. OPCODE_BREAK;
  1637. }
  1638. #endif
  1639. int jumpto = _code_ptr[ip + 4];
  1640. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1641. ip = jumpto;
  1642. } else {
  1643. GET_INSTRUCTION_ARG(iterator, 2);
  1644. *iterator = container->iter_get(*counter, valid);
  1645. #ifdef DEBUG_ENABLED
  1646. if (!valid) {
  1647. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1648. OPCODE_BREAK;
  1649. }
  1650. #endif
  1651. ip += 5; // Skip regular iterate which is always next.
  1652. }
  1653. }
  1654. DISPATCH_OPCODE;
  1655. OPCODE(OPCODE_ITERATE) {
  1656. CHECK_SPACE(4);
  1657. GET_INSTRUCTION_ARG(counter, 0);
  1658. GET_INSTRUCTION_ARG(container, 1);
  1659. bool valid;
  1660. if (!container->iter_next(*counter, valid)) {
  1661. #ifdef DEBUG_ENABLED
  1662. if (!valid) {
  1663. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1664. OPCODE_BREAK;
  1665. }
  1666. #endif
  1667. int jumpto = _code_ptr[ip + 4];
  1668. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1669. ip = jumpto;
  1670. } else {
  1671. GET_INSTRUCTION_ARG(iterator, 2);
  1672. *iterator = container->iter_get(*counter, valid);
  1673. #ifdef DEBUG_ENABLED
  1674. if (!valid) {
  1675. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1676. OPCODE_BREAK;
  1677. }
  1678. #endif
  1679. ip += 5; //loop again
  1680. }
  1681. }
  1682. DISPATCH_OPCODE;
  1683. OPCODE(OPCODE_ASSERT) {
  1684. CHECK_SPACE(3);
  1685. #ifdef DEBUG_ENABLED
  1686. GET_INSTRUCTION_ARG(test, 0);
  1687. bool result = test->booleanize();
  1688. if (!result) {
  1689. String message_str;
  1690. if (_code_ptr[ip + 2] != 0) {
  1691. GET_INSTRUCTION_ARG(message, 1);
  1692. message_str = *message;
  1693. }
  1694. if (message_str.empty()) {
  1695. err_text = "Assertion failed.";
  1696. } else {
  1697. err_text = "Assertion failed: " + message_str;
  1698. }
  1699. OPCODE_BREAK;
  1700. }
  1701. #endif
  1702. ip += 3;
  1703. }
  1704. DISPATCH_OPCODE;
  1705. OPCODE(OPCODE_BREAKPOINT) {
  1706. #ifdef DEBUG_ENABLED
  1707. if (EngineDebugger::is_active()) {
  1708. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1709. }
  1710. #endif
  1711. ip += 1;
  1712. }
  1713. DISPATCH_OPCODE;
  1714. OPCODE(OPCODE_LINE) {
  1715. CHECK_SPACE(2);
  1716. line = _code_ptr[ip + 1];
  1717. ip += 2;
  1718. if (EngineDebugger::is_active()) {
  1719. // line
  1720. bool do_break = false;
  1721. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1722. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  1723. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1724. }
  1725. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  1726. do_break = true;
  1727. }
  1728. }
  1729. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  1730. do_break = true;
  1731. }
  1732. if (do_break) {
  1733. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1734. }
  1735. EngineDebugger::get_singleton()->line_poll();
  1736. }
  1737. }
  1738. DISPATCH_OPCODE;
  1739. OPCODE(OPCODE_END) {
  1740. #ifdef DEBUG_ENABLED
  1741. exit_ok = true;
  1742. #endif
  1743. OPCODE_BREAK;
  1744. }
  1745. #if 0 // Enable for debugging.
  1746. default: {
  1747. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1748. OPCODE_BREAK;
  1749. }
  1750. #endif
  1751. }
  1752. OPCODES_END
  1753. #ifdef DEBUG_ENABLED
  1754. if (exit_ok) {
  1755. OPCODE_OUT;
  1756. }
  1757. //error
  1758. // function, file, line, error, explanation
  1759. String err_file;
  1760. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  1761. err_file = p_instance->script->path;
  1762. } else if (script) {
  1763. err_file = script->path;
  1764. }
  1765. if (err_file == "") {
  1766. err_file = "<built-in>";
  1767. }
  1768. String err_func = name;
  1769. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  1770. err_func = p_instance->script->name + "." + err_func;
  1771. }
  1772. int err_line = line;
  1773. if (err_text == "") {
  1774. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1775. }
  1776. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1777. // debugger break did not happen
  1778. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1779. }
  1780. #endif
  1781. OPCODE_OUT;
  1782. }
  1783. OPCODES_OUT
  1784. #ifdef DEBUG_ENABLED
  1785. if (GDScriptLanguage::get_singleton()->profiling) {
  1786. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1787. profile.total_time += time_taken;
  1788. profile.self_time += time_taken - function_call_time;
  1789. profile.frame_total_time += time_taken;
  1790. profile.frame_self_time += time_taken - function_call_time;
  1791. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1792. }
  1793. // Check if this is the last time the function is resuming from await
  1794. // Will be true if never awaited as well
  1795. // When it's the last resume it will postpone the exit from stack,
  1796. // so the debugger knows which function triggered the resume of the next function (if any)
  1797. if (!p_state || awaited) {
  1798. if (EngineDebugger::is_active()) {
  1799. GDScriptLanguage::get_singleton()->exit_function();
  1800. }
  1801. #endif
  1802. if (_stack_size) {
  1803. //free stack
  1804. for (int i = 0; i < _stack_size; i++) {
  1805. stack[i].~Variant();
  1806. }
  1807. }
  1808. #ifdef DEBUG_ENABLED
  1809. }
  1810. #endif
  1811. return retvalue;
  1812. }