gdscript_function.cpp 63 KB

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