gdscript_function.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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, _named_globals_count, nullptr);
  103. #endif
  104. StringName id = _named_globals_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. Object *bobj = *p_var;
  125. if (!bobj) {
  126. basestr = "null instance";
  127. } else {
  128. if (ObjectDB::instance_validate(bobj)) {
  129. if (bobj->get_script_instance()) {
  130. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  131. } else {
  132. basestr = bobj->get_class();
  133. }
  134. } else {
  135. basestr = "previously freed instance";
  136. }
  137. }
  138. } else {
  139. basestr = Variant::get_type_name(p_var->get_type());
  140. }
  141. return basestr;
  142. }
  143. #endif // DEBUG_ENABLED
  144. String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  145. String err_text;
  146. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  147. int errorarg = p_err.argument;
  148. // Handle the Object to Object case separately as we don't have further class details.
  149. #ifdef DEBUG_ENABLED
  150. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  151. 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.";
  152. } else
  153. #endif // DEBUG_ENABLED
  154. {
  155. 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(p_err.expected) + ".";
  156. }
  157. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  158. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  159. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  160. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  161. } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  162. err_text = "Invalid call. Nonexistent " + p_where + ".";
  163. } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  164. err_text = "Attempt to call " + p_where + " on a null instance.";
  165. } else {
  166. err_text = "Bug, call error: #" + itos(p_err.error);
  167. }
  168. return err_text;
  169. }
  170. #if defined(__GNUC__)
  171. #define OPCODES_TABLE \
  172. static const void *switch_table_ops[] = { \
  173. &&OPCODE_OPERATOR, \
  174. &&OPCODE_EXTENDS_TEST, \
  175. &&OPCODE_IS_BUILTIN, \
  176. &&OPCODE_SET, \
  177. &&OPCODE_GET, \
  178. &&OPCODE_SET_NAMED, \
  179. &&OPCODE_GET_NAMED, \
  180. &&OPCODE_SET_MEMBER, \
  181. &&OPCODE_GET_MEMBER, \
  182. &&OPCODE_ASSIGN, \
  183. &&OPCODE_ASSIGN_TRUE, \
  184. &&OPCODE_ASSIGN_FALSE, \
  185. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  186. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  187. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  188. &&OPCODE_CAST_TO_BUILTIN, \
  189. &&OPCODE_CAST_TO_NATIVE, \
  190. &&OPCODE_CAST_TO_SCRIPT, \
  191. &&OPCODE_CONSTRUCT, \
  192. &&OPCODE_CONSTRUCT_ARRAY, \
  193. &&OPCODE_CONSTRUCT_DICTIONARY, \
  194. &&OPCODE_CALL, \
  195. &&OPCODE_CALL_RETURN, \
  196. &&OPCODE_CALL_BUILT_IN, \
  197. &&OPCODE_CALL_SELF, \
  198. &&OPCODE_CALL_SELF_BASE, \
  199. &&OPCODE_YIELD, \
  200. &&OPCODE_YIELD_SIGNAL, \
  201. &&OPCODE_YIELD_RESUME, \
  202. &&OPCODE_JUMP, \
  203. &&OPCODE_JUMP_IF, \
  204. &&OPCODE_JUMP_IF_NOT, \
  205. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  206. &&OPCODE_RETURN, \
  207. &&OPCODE_ITERATE_BEGIN, \
  208. &&OPCODE_ITERATE, \
  209. &&OPCODE_ASSERT, \
  210. &&OPCODE_BREAKPOINT, \
  211. &&OPCODE_LINE, \
  212. &&OPCODE_END \
  213. };
  214. #define OPCODE(m_op) \
  215. m_op:
  216. #define OPCODE_WHILE(m_test)
  217. #define OPCODES_END \
  218. OPSEXIT:
  219. #define OPCODES_OUT \
  220. OPSOUT:
  221. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  222. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  223. #define OPCODE_BREAK goto OPSEXIT
  224. #define OPCODE_OUT goto OPSOUT
  225. #else
  226. #define OPCODES_TABLE
  227. #define OPCODE(m_op) case m_op:
  228. #define OPCODE_WHILE(m_test) while (m_test)
  229. #define OPCODES_END
  230. #define OPCODES_OUT
  231. #define DISPATCH_OPCODE continue
  232. #define OPCODE_SWITCH(m_test) switch (m_test)
  233. #define OPCODE_BREAK break
  234. #define OPCODE_OUT break
  235. #endif
  236. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  237. OPCODES_TABLE;
  238. if (!_code_ptr) {
  239. return Variant();
  240. }
  241. r_err.error = Variant::CallError::CALL_OK;
  242. Variant self;
  243. Variant static_ref;
  244. Variant retvalue;
  245. Variant *stack = nullptr;
  246. Variant **call_args;
  247. int defarg = 0;
  248. #ifdef DEBUG_ENABLED
  249. //GDScriptLanguage::get_singleton()->calls++;
  250. #endif
  251. uint32_t alloca_size = 0;
  252. GDScript *script;
  253. int ip = 0;
  254. int line = _initial_line;
  255. if (p_state) {
  256. //use existing (supplied) state (yielded)
  257. stack = (Variant *)p_state->stack.ptr();
  258. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  259. line = p_state->line;
  260. ip = p_state->ip;
  261. alloca_size = p_state->stack.size();
  262. script = p_state->script;
  263. p_instance = p_state->instance;
  264. defarg = p_state->defarg;
  265. self = p_state->self;
  266. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  267. } else {
  268. if (p_argcount != _argument_count) {
  269. if (p_argcount > _argument_count) {
  270. r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  271. r_err.argument = _argument_count;
  272. return Variant();
  273. } else if (p_argcount < _argument_count - _default_arg_count) {
  274. r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  275. r_err.argument = _argument_count - _default_arg_count;
  276. return Variant();
  277. } else {
  278. defarg = _argument_count - p_argcount;
  279. }
  280. }
  281. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  282. if (alloca_size) {
  283. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  284. if (_stack_size) {
  285. stack = (Variant *)aptr;
  286. for (int i = 0; i < p_argcount; i++) {
  287. if (!argument_types[i].has_type) {
  288. memnew_placement(&stack[i], Variant(*p_args[i]));
  289. continue;
  290. }
  291. if (!argument_types[i].is_type(*p_args[i], true)) {
  292. if (argument_types[i].is_type(Variant(), true)) {
  293. memnew_placement(&stack[i], Variant);
  294. continue;
  295. } else {
  296. r_err.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  297. r_err.argument = i;
  298. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  299. return Variant();
  300. }
  301. }
  302. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  303. Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err);
  304. memnew_placement(&stack[i], Variant(arg));
  305. } else {
  306. memnew_placement(&stack[i], Variant(*p_args[i]));
  307. }
  308. }
  309. for (int i = p_argcount; i < _stack_size; i++) {
  310. memnew_placement(&stack[i], Variant);
  311. }
  312. } else {
  313. stack = nullptr;
  314. }
  315. if (_call_size) {
  316. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  317. } else {
  318. call_args = nullptr;
  319. }
  320. } else {
  321. stack = nullptr;
  322. call_args = nullptr;
  323. }
  324. if (p_instance) {
  325. self = p_instance->owner;
  326. script = p_instance->script.ptr();
  327. } else {
  328. script = _script;
  329. }
  330. }
  331. static_ref = script;
  332. String err_text;
  333. #ifdef DEBUG_ENABLED
  334. if (ScriptDebugger::get_singleton()) {
  335. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  336. }
  337. #define GD_ERR_BREAK(m_cond) \
  338. { \
  339. if (unlikely(m_cond)) { \
  340. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  341. OPCODE_BREAK; \
  342. } \
  343. }
  344. #define CHECK_SPACE(m_space) \
  345. GD_ERR_BREAK((ip + m_space) > _code_size)
  346. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  347. Variant *m_v; \
  348. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  349. if (unlikely(!m_v)) \
  350. OPCODE_BREAK;
  351. #else
  352. #define GD_ERR_BREAK(m_cond)
  353. #define CHECK_SPACE(m_space)
  354. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  355. Variant *m_v; \
  356. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  357. #endif
  358. #ifdef DEBUG_ENABLED
  359. uint64_t function_start_time = 0;
  360. uint64_t function_call_time = 0;
  361. if (GDScriptLanguage::get_singleton()->profiling) {
  362. function_start_time = OS::get_singleton()->get_ticks_usec();
  363. function_call_time = 0;
  364. profile.call_count++;
  365. profile.frame_call_count++;
  366. }
  367. bool exit_ok = false;
  368. bool yielded = false;
  369. #endif
  370. #ifdef DEBUG_ENABLED
  371. OPCODE_WHILE(ip < _code_size) {
  372. int last_opcode = _code_ptr[ip];
  373. #else
  374. OPCODE_WHILE(true) {
  375. #endif
  376. OPCODE_SWITCH(_code_ptr[ip]) {
  377. OPCODE(OPCODE_OPERATOR) {
  378. CHECK_SPACE(5);
  379. bool valid;
  380. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  381. GD_ERR_BREAK(op >= Variant::OP_MAX);
  382. GET_VARIANT_PTR(a, 2);
  383. GET_VARIANT_PTR(b, 3);
  384. GET_VARIANT_PTR(dst, 4);
  385. #ifdef DEBUG_ENABLED
  386. Variant ret;
  387. Variant::evaluate(op, *a, *b, ret, valid);
  388. #else
  389. Variant::evaluate(op, *a, *b, *dst, valid);
  390. #endif
  391. #ifdef DEBUG_ENABLED
  392. if (!valid) {
  393. if (ret.get_type() == Variant::STRING) {
  394. //return a string when invalid with the error
  395. err_text = ret;
  396. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  397. } else {
  398. 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) + "'.";
  399. }
  400. OPCODE_BREAK;
  401. }
  402. *dst = ret;
  403. #endif
  404. ip += 5;
  405. }
  406. DISPATCH_OPCODE;
  407. OPCODE(OPCODE_EXTENDS_TEST) {
  408. CHECK_SPACE(4);
  409. GET_VARIANT_PTR(a, 1);
  410. GET_VARIANT_PTR(b, 2);
  411. GET_VARIANT_PTR(dst, 3);
  412. #ifdef DEBUG_ENABLED
  413. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  414. err_text = "Right operand of 'is' is not a class.";
  415. OPCODE_BREAK;
  416. }
  417. #endif
  418. bool extends_ok = false;
  419. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  420. Object *obj_A = *a;
  421. Object *obj_B = *b;
  422. #ifdef DEBUG_ENABLED
  423. if (!ObjectDB::instance_validate(obj_A)) {
  424. err_text = "Left operand of 'is' was already freed.";
  425. OPCODE_BREAK;
  426. }
  427. #endif // DEBUG_ENABLED
  428. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  429. if (scr_B) {
  430. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  431. //in other situation, this shoul return false.
  432. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  433. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  434. //bool found=false;
  435. while (cmp) {
  436. if (cmp == scr_B) {
  437. //inherits from script, all ok
  438. extends_ok = true;
  439. break;
  440. }
  441. cmp = cmp->_base;
  442. }
  443. }
  444. } else {
  445. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  446. #ifdef DEBUG_ENABLED
  447. if (!nc) {
  448. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  449. OPCODE_BREAK;
  450. }
  451. #endif
  452. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  453. }
  454. }
  455. *dst = extends_ok;
  456. ip += 4;
  457. }
  458. DISPATCH_OPCODE;
  459. OPCODE(OPCODE_IS_BUILTIN) {
  460. CHECK_SPACE(4);
  461. GET_VARIANT_PTR(value, 1);
  462. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  463. GET_VARIANT_PTR(dst, 3);
  464. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  465. *dst = value->get_type() == var_type;
  466. ip += 4;
  467. }
  468. DISPATCH_OPCODE;
  469. OPCODE(OPCODE_SET) {
  470. CHECK_SPACE(3);
  471. GET_VARIANT_PTR(dst, 1);
  472. GET_VARIANT_PTR(index, 2);
  473. GET_VARIANT_PTR(value, 3);
  474. bool valid;
  475. dst->set(*index, *value, &valid);
  476. #ifdef DEBUG_ENABLED
  477. if (!valid) {
  478. String v = index->operator String();
  479. if (v != "") {
  480. v = "'" + v + "'";
  481. } else {
  482. v = "of type '" + _get_var_type(index) + "'";
  483. }
  484. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  485. OPCODE_BREAK;
  486. }
  487. #endif
  488. ip += 4;
  489. }
  490. DISPATCH_OPCODE;
  491. OPCODE(OPCODE_GET) {
  492. CHECK_SPACE(3);
  493. GET_VARIANT_PTR(src, 1);
  494. GET_VARIANT_PTR(index, 2);
  495. GET_VARIANT_PTR(dst, 3);
  496. bool valid;
  497. #ifdef DEBUG_ENABLED
  498. //allow better error message in cases where src and dst are the same stack position
  499. Variant ret = src->get(*index, &valid);
  500. #else
  501. *dst = src->get(*index, &valid);
  502. #endif
  503. #ifdef DEBUG_ENABLED
  504. if (!valid) {
  505. String v = index->operator String();
  506. if (v != "") {
  507. v = "'" + v + "'";
  508. } else {
  509. v = "of type '" + _get_var_type(index) + "'";
  510. }
  511. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  512. OPCODE_BREAK;
  513. }
  514. *dst = ret;
  515. #endif
  516. ip += 4;
  517. }
  518. DISPATCH_OPCODE;
  519. OPCODE(OPCODE_SET_NAMED) {
  520. CHECK_SPACE(3);
  521. GET_VARIANT_PTR(dst, 1);
  522. GET_VARIANT_PTR(value, 3);
  523. int indexname = _code_ptr[ip + 2];
  524. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  525. const StringName *index = &_global_names_ptr[indexname];
  526. bool valid;
  527. dst->set_named(*index, *value, &valid);
  528. #ifdef DEBUG_ENABLED
  529. if (!valid) {
  530. String err_type;
  531. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  532. OPCODE_BREAK;
  533. }
  534. #endif
  535. ip += 4;
  536. }
  537. DISPATCH_OPCODE;
  538. OPCODE(OPCODE_GET_NAMED) {
  539. CHECK_SPACE(4);
  540. GET_VARIANT_PTR(src, 1);
  541. GET_VARIANT_PTR(dst, 3);
  542. int indexname = _code_ptr[ip + 2];
  543. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  544. const StringName *index = &_global_names_ptr[indexname];
  545. bool valid;
  546. #ifdef DEBUG_ENABLED
  547. //allow better error message in cases where src and dst are the same stack position
  548. Variant ret = src->get_named(*index, &valid);
  549. #else
  550. *dst = src->get_named(*index, &valid);
  551. #endif
  552. #ifdef DEBUG_ENABLED
  553. if (!valid) {
  554. if (src->has_method(*index)) {
  555. 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() + "\") ?";
  556. } else {
  557. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  558. }
  559. OPCODE_BREAK;
  560. }
  561. *dst = ret;
  562. #endif
  563. ip += 4;
  564. }
  565. DISPATCH_OPCODE;
  566. OPCODE(OPCODE_SET_MEMBER) {
  567. CHECK_SPACE(3);
  568. int indexname = _code_ptr[ip + 1];
  569. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  570. const StringName *index = &_global_names_ptr[indexname];
  571. GET_VARIANT_PTR(src, 2);
  572. bool valid;
  573. #ifndef DEBUG_ENABLED
  574. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  575. #else
  576. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  577. if (!ok) {
  578. err_text = "Internal error setting property: " + String(*index);
  579. OPCODE_BREAK;
  580. } else if (!valid) {
  581. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  582. OPCODE_BREAK;
  583. }
  584. #endif
  585. ip += 3;
  586. }
  587. DISPATCH_OPCODE;
  588. OPCODE(OPCODE_GET_MEMBER) {
  589. CHECK_SPACE(3);
  590. int indexname = _code_ptr[ip + 1];
  591. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  592. const StringName *index = &_global_names_ptr[indexname];
  593. GET_VARIANT_PTR(dst, 2);
  594. #ifndef DEBUG_ENABLED
  595. ClassDB::get_property(p_instance->owner, *index, *dst);
  596. #else
  597. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  598. if (!ok) {
  599. err_text = "Internal error getting property: " + String(*index);
  600. OPCODE_BREAK;
  601. }
  602. #endif
  603. ip += 3;
  604. }
  605. DISPATCH_OPCODE;
  606. OPCODE(OPCODE_ASSIGN) {
  607. CHECK_SPACE(3);
  608. GET_VARIANT_PTR(dst, 1);
  609. GET_VARIANT_PTR(src, 2);
  610. *dst = *src;
  611. ip += 3;
  612. }
  613. DISPATCH_OPCODE;
  614. OPCODE(OPCODE_ASSIGN_TRUE) {
  615. CHECK_SPACE(2);
  616. GET_VARIANT_PTR(dst, 1);
  617. *dst = true;
  618. ip += 2;
  619. }
  620. DISPATCH_OPCODE;
  621. OPCODE(OPCODE_ASSIGN_FALSE) {
  622. CHECK_SPACE(2);
  623. GET_VARIANT_PTR(dst, 1);
  624. *dst = false;
  625. ip += 2;
  626. }
  627. DISPATCH_OPCODE;
  628. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  629. CHECK_SPACE(4);
  630. GET_VARIANT_PTR(dst, 2);
  631. GET_VARIANT_PTR(src, 3);
  632. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  633. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  634. if (src->get_type() != var_type) {
  635. #ifdef DEBUG_ENABLED
  636. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  637. #endif // DEBUG_ENABLED
  638. Variant::CallError ce;
  639. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  640. } else {
  641. #ifdef DEBUG_ENABLED
  642. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  643. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  644. OPCODE_BREAK;
  645. }
  646. } else {
  647. #endif // DEBUG_ENABLED
  648. *dst = *src;
  649. }
  650. ip += 4;
  651. }
  652. DISPATCH_OPCODE;
  653. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  654. CHECK_SPACE(4);
  655. GET_VARIANT_PTR(dst, 2);
  656. GET_VARIANT_PTR(src, 3);
  657. #ifdef DEBUG_ENABLED
  658. GET_VARIANT_PTR(type, 1);
  659. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  660. GD_ERR_BREAK(!nc);
  661. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  662. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  663. "' to a variable of type '" + nc->get_name() + "'.";
  664. OPCODE_BREAK;
  665. }
  666. Object *src_obj = src->operator Object *();
  667. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  668. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  669. "' to a variable of type '" + nc->get_name() + "'.";
  670. OPCODE_BREAK;
  671. }
  672. #endif // DEBUG_ENABLED
  673. *dst = *src;
  674. ip += 4;
  675. }
  676. DISPATCH_OPCODE;
  677. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  678. CHECK_SPACE(4);
  679. GET_VARIANT_PTR(dst, 2);
  680. GET_VARIANT_PTR(src, 3);
  681. #ifdef DEBUG_ENABLED
  682. GET_VARIANT_PTR(type, 1);
  683. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  684. GD_ERR_BREAK(!base_type);
  685. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  686. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  687. OPCODE_BREAK;
  688. }
  689. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  690. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  691. if (!scr_inst) {
  692. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  693. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  694. OPCODE_BREAK;
  695. }
  696. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  697. bool valid = false;
  698. while (src_type) {
  699. if (src_type == base_type) {
  700. valid = true;
  701. break;
  702. }
  703. src_type = src_type->get_base_script().ptr();
  704. }
  705. if (!valid) {
  706. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  707. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  708. OPCODE_BREAK;
  709. }
  710. }
  711. #endif // DEBUG_ENABLED
  712. *dst = *src;
  713. ip += 4;
  714. }
  715. DISPATCH_OPCODE;
  716. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  717. CHECK_SPACE(4);
  718. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  719. GET_VARIANT_PTR(src, 2);
  720. GET_VARIANT_PTR(dst, 3);
  721. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  722. Variant::CallError err;
  723. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  724. #ifdef DEBUG_ENABLED
  725. if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
  726. err_text = "Trying to cast a deleted object.";
  727. OPCODE_BREAK;
  728. }
  729. if (err.error != Variant::CallError::CALL_OK) {
  730. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  731. OPCODE_BREAK;
  732. }
  733. #endif
  734. ip += 4;
  735. }
  736. DISPATCH_OPCODE;
  737. OPCODE(OPCODE_CAST_TO_NATIVE) {
  738. CHECK_SPACE(4);
  739. GET_VARIANT_PTR(to_type, 1);
  740. GET_VARIANT_PTR(src, 2);
  741. GET_VARIANT_PTR(dst, 3);
  742. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  743. GD_ERR_BREAK(!nc);
  744. #ifdef DEBUG_ENABLED
  745. if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
  746. err_text = "Trying to cast a deleted object.";
  747. OPCODE_BREAK;
  748. }
  749. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  750. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  751. OPCODE_BREAK;
  752. }
  753. #endif
  754. Object *src_obj = src->operator Object *();
  755. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  756. *dst = Variant(); // invalid cast, assign NULL
  757. } else {
  758. *dst = *src;
  759. }
  760. ip += 4;
  761. }
  762. DISPATCH_OPCODE;
  763. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  764. CHECK_SPACE(4);
  765. GET_VARIANT_PTR(to_type, 1);
  766. GET_VARIANT_PTR(src, 2);
  767. GET_VARIANT_PTR(dst, 3);
  768. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  769. GD_ERR_BREAK(!base_type);
  770. #ifdef DEBUG_ENABLED
  771. if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
  772. err_text = "Trying to cast a deleted object.";
  773. OPCODE_BREAK;
  774. }
  775. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  776. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  777. OPCODE_BREAK;
  778. }
  779. #endif
  780. bool valid = false;
  781. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  782. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  783. if (scr_inst) {
  784. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  785. while (src_type) {
  786. if (src_type == base_type) {
  787. valid = true;
  788. break;
  789. }
  790. src_type = src_type->get_base_script().ptr();
  791. }
  792. }
  793. }
  794. if (valid) {
  795. *dst = *src; // Valid cast, copy the source object
  796. } else {
  797. *dst = Variant(); // invalid cast, assign NULL
  798. }
  799. ip += 4;
  800. }
  801. DISPATCH_OPCODE;
  802. OPCODE(OPCODE_CONSTRUCT) {
  803. CHECK_SPACE(2);
  804. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  805. int argc = _code_ptr[ip + 2];
  806. CHECK_SPACE(argc + 2);
  807. Variant **argptrs = call_args;
  808. for (int i = 0; i < argc; i++) {
  809. GET_VARIANT_PTR(v, 3 + i);
  810. argptrs[i] = v;
  811. }
  812. GET_VARIANT_PTR(dst, 3 + argc);
  813. Variant::CallError err;
  814. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  815. #ifdef DEBUG_ENABLED
  816. if (err.error != Variant::CallError::CALL_OK) {
  817. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  818. OPCODE_BREAK;
  819. }
  820. #endif
  821. ip += 4 + argc;
  822. //construct a basic type
  823. }
  824. DISPATCH_OPCODE;
  825. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  826. CHECK_SPACE(1);
  827. int argc = _code_ptr[ip + 1];
  828. Array array; //arrays are always shared
  829. array.resize(argc);
  830. CHECK_SPACE(argc + 2);
  831. for (int i = 0; i < argc; i++) {
  832. GET_VARIANT_PTR(v, 2 + i);
  833. array[i] = *v;
  834. }
  835. GET_VARIANT_PTR(dst, 2 + argc);
  836. *dst = array;
  837. ip += 3 + argc;
  838. }
  839. DISPATCH_OPCODE;
  840. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  841. CHECK_SPACE(1);
  842. int argc = _code_ptr[ip + 1];
  843. Dictionary dict; //arrays are always shared
  844. CHECK_SPACE(argc * 2 + 2);
  845. for (int i = 0; i < argc; i++) {
  846. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  847. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  848. dict[*k] = *v;
  849. }
  850. GET_VARIANT_PTR(dst, 2 + argc * 2);
  851. *dst = dict;
  852. ip += 3 + argc * 2;
  853. }
  854. DISPATCH_OPCODE;
  855. OPCODE(OPCODE_CALL_RETURN)
  856. OPCODE(OPCODE_CALL) {
  857. CHECK_SPACE(4);
  858. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  859. int argc = _code_ptr[ip + 1];
  860. GET_VARIANT_PTR(base, 2);
  861. int nameg = _code_ptr[ip + 3];
  862. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  863. const StringName *methodname = &_global_names_ptr[nameg];
  864. GD_ERR_BREAK(argc < 0);
  865. ip += 4;
  866. CHECK_SPACE(argc + 1);
  867. Variant **argptrs = call_args;
  868. for (int i = 0; i < argc; i++) {
  869. GET_VARIANT_PTR(v, i);
  870. argptrs[i] = v;
  871. }
  872. #ifdef DEBUG_ENABLED
  873. uint64_t call_time = 0;
  874. if (GDScriptLanguage::get_singleton()->profiling) {
  875. call_time = OS::get_singleton()->get_ticks_usec();
  876. }
  877. #endif
  878. Variant::CallError err;
  879. if (call_ret) {
  880. GET_VARIANT_PTR(ret, argc);
  881. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  882. } else {
  883. base->call_ptr(*methodname, (const Variant **)argptrs, argc, nullptr, err);
  884. }
  885. #ifdef DEBUG_ENABLED
  886. if (GDScriptLanguage::get_singleton()->profiling) {
  887. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  888. }
  889. if (err.error != Variant::CallError::CALL_OK) {
  890. String methodstr = *methodname;
  891. String basestr = _get_var_type(base);
  892. if (methodstr == "call") {
  893. if (argc >= 1) {
  894. methodstr = String(*argptrs[0]) + " (via call)";
  895. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  896. err.argument += 1;
  897. }
  898. }
  899. } else if (methodstr == "free") {
  900. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  901. if (base->is_ref()) {
  902. err_text = "Attempted to free a reference.";
  903. OPCODE_BREAK;
  904. } else if (base->get_type() == Variant::OBJECT) {
  905. err_text = "Attempted to free a locked object (calling or emitting).";
  906. OPCODE_BREAK;
  907. }
  908. }
  909. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  910. if (argc >= 1) {
  911. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  912. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  913. err.argument += 1;
  914. }
  915. }
  916. }
  917. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  918. OPCODE_BREAK;
  919. }
  920. #endif
  921. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  922. ip += argc + 1;
  923. }
  924. DISPATCH_OPCODE;
  925. OPCODE(OPCODE_CALL_BUILT_IN) {
  926. CHECK_SPACE(4);
  927. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  928. int argc = _code_ptr[ip + 2];
  929. GD_ERR_BREAK(argc < 0);
  930. ip += 3;
  931. CHECK_SPACE(argc + 1);
  932. Variant **argptrs = call_args;
  933. for (int i = 0; i < argc; i++) {
  934. GET_VARIANT_PTR(v, i);
  935. argptrs[i] = v;
  936. }
  937. GET_VARIANT_PTR(dst, argc);
  938. Variant::CallError err;
  939. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  940. #ifdef DEBUG_ENABLED
  941. if (err.error != Variant::CallError::CALL_OK) {
  942. String methodstr = GDScriptFunctions::get_func_name(func);
  943. if (dst->get_type() == Variant::STRING) {
  944. //call provided error string
  945. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  946. } else {
  947. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  948. }
  949. OPCODE_BREAK;
  950. }
  951. #endif
  952. ip += argc + 1;
  953. }
  954. DISPATCH_OPCODE;
  955. OPCODE(OPCODE_CALL_SELF) {
  956. OPCODE_BREAK;
  957. }
  958. OPCODE(OPCODE_CALL_SELF_BASE) {
  959. CHECK_SPACE(2);
  960. int self_fun = _code_ptr[ip + 1];
  961. #ifdef DEBUG_ENABLED
  962. if (self_fun < 0 || self_fun >= _global_names_count) {
  963. err_text = "compiler bug, function name not found";
  964. OPCODE_BREAK;
  965. }
  966. #endif
  967. const StringName *methodname = &_global_names_ptr[self_fun];
  968. int argc = _code_ptr[ip + 2];
  969. CHECK_SPACE(2 + argc + 1);
  970. Variant **argptrs = call_args;
  971. for (int i = 0; i < argc; i++) {
  972. GET_VARIANT_PTR(v, i + 3);
  973. argptrs[i] = v;
  974. }
  975. GET_VARIANT_PTR(dst, argc + 3);
  976. const GDScript *gds = _script;
  977. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  978. while (gds->base.ptr()) {
  979. gds = gds->base.ptr();
  980. E = gds->member_functions.find(*methodname);
  981. if (E) {
  982. break;
  983. }
  984. }
  985. Variant::CallError err;
  986. if (E) {
  987. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  988. } else if (gds->native.ptr()) {
  989. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  990. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  991. if (!mb) {
  992. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  993. } else {
  994. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  995. }
  996. } else {
  997. err.error = Variant::CallError::CALL_OK;
  998. }
  999. } else {
  1000. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1001. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1002. } else {
  1003. err.error = Variant::CallError::CALL_OK;
  1004. }
  1005. }
  1006. if (err.error != Variant::CallError::CALL_OK) {
  1007. String methodstr = *methodname;
  1008. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1009. OPCODE_BREAK;
  1010. }
  1011. ip += 4 + argc;
  1012. }
  1013. DISPATCH_OPCODE;
  1014. OPCODE(OPCODE_YIELD)
  1015. OPCODE(OPCODE_YIELD_SIGNAL) {
  1016. int ipofs = 1;
  1017. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1018. CHECK_SPACE(4);
  1019. ipofs += 2;
  1020. } else {
  1021. CHECK_SPACE(2);
  1022. }
  1023. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1024. gdfs->function = this;
  1025. gdfs->state.stack.resize(alloca_size);
  1026. //copy variant stack
  1027. for (int i = 0; i < _stack_size; i++) {
  1028. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1029. }
  1030. gdfs->state.stack_size = _stack_size;
  1031. gdfs->state.self = self;
  1032. gdfs->state.alloca_size = alloca_size;
  1033. gdfs->state.ip = ip + ipofs;
  1034. gdfs->state.line = line;
  1035. gdfs->state.script = _script;
  1036. GDScriptLanguage::singleton->lock.lock();
  1037. _script->pending_func_states.add(&gdfs->scripts_list);
  1038. if (p_instance) {
  1039. gdfs->state.instance = p_instance;
  1040. p_instance->pending_func_states.add(&gdfs->instances_list);
  1041. } else {
  1042. gdfs->state.instance = nullptr;
  1043. }
  1044. GDScriptLanguage::singleton->lock.unlock();
  1045. #ifdef DEBUG_ENABLED
  1046. gdfs->state.function_name = name;
  1047. gdfs->state.script_path = _script->get_path();
  1048. #endif
  1049. //gdfs->state.result_pos=ip+ipofs-1;
  1050. gdfs->state.defarg = defarg;
  1051. gdfs->function = this;
  1052. retvalue = gdfs;
  1053. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1054. //do the oneshot connect
  1055. GET_VARIANT_PTR(argobj, 1);
  1056. GET_VARIANT_PTR(argname, 2);
  1057. #ifdef DEBUG_ENABLED
  1058. if (argobj->get_type() != Variant::OBJECT) {
  1059. err_text = "First argument of yield() not of type object.";
  1060. OPCODE_BREAK;
  1061. }
  1062. if (argname->get_type() != Variant::STRING) {
  1063. err_text = "Second argument of yield() not a string (for signal name).";
  1064. OPCODE_BREAK;
  1065. }
  1066. #endif
  1067. Object *obj = argobj->operator Object *();
  1068. String signal = argname->operator String();
  1069. #ifdef DEBUG_ENABLED
  1070. if (!obj) {
  1071. err_text = "First argument of yield() is null.";
  1072. OPCODE_BREAK;
  1073. }
  1074. if (ScriptDebugger::get_singleton()) {
  1075. if (!ObjectDB::instance_validate(obj)) {
  1076. err_text = "First argument of yield() is a previously freed instance.";
  1077. OPCODE_BREAK;
  1078. }
  1079. }
  1080. if (signal.length() == 0) {
  1081. err_text = "Second argument of yield() is an empty string (for signal name).";
  1082. OPCODE_BREAK;
  1083. }
  1084. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1085. if (err != OK) {
  1086. err_text = "Error connecting to signal: " + signal + " during yield().";
  1087. OPCODE_BREAK;
  1088. }
  1089. #else
  1090. obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1091. #endif
  1092. }
  1093. #ifdef DEBUG_ENABLED
  1094. exit_ok = true;
  1095. yielded = true;
  1096. #endif
  1097. OPCODE_BREAK;
  1098. }
  1099. OPCODE(OPCODE_YIELD_RESUME) {
  1100. CHECK_SPACE(2);
  1101. #ifdef DEBUG_ENABLED
  1102. if (!p_state) {
  1103. err_text = ("Invalid Resume (bug?)");
  1104. OPCODE_BREAK;
  1105. }
  1106. #endif
  1107. GET_VARIANT_PTR(result, 1);
  1108. *result = p_state->result;
  1109. ip += 2;
  1110. }
  1111. DISPATCH_OPCODE;
  1112. OPCODE(OPCODE_JUMP) {
  1113. CHECK_SPACE(2);
  1114. int to = _code_ptr[ip + 1];
  1115. GD_ERR_BREAK(to < 0 || to > _code_size);
  1116. ip = to;
  1117. }
  1118. DISPATCH_OPCODE;
  1119. OPCODE(OPCODE_JUMP_IF) {
  1120. CHECK_SPACE(3);
  1121. GET_VARIANT_PTR(test, 1);
  1122. bool result = test->booleanize();
  1123. if (result) {
  1124. int to = _code_ptr[ip + 2];
  1125. GD_ERR_BREAK(to < 0 || to > _code_size);
  1126. ip = to;
  1127. } else {
  1128. ip += 3;
  1129. }
  1130. }
  1131. DISPATCH_OPCODE;
  1132. OPCODE(OPCODE_JUMP_IF_NOT) {
  1133. CHECK_SPACE(3);
  1134. GET_VARIANT_PTR(test, 1);
  1135. bool result = test->booleanize();
  1136. if (!result) {
  1137. int to = _code_ptr[ip + 2];
  1138. GD_ERR_BREAK(to < 0 || to > _code_size);
  1139. ip = to;
  1140. } else {
  1141. ip += 3;
  1142. }
  1143. }
  1144. DISPATCH_OPCODE;
  1145. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1146. CHECK_SPACE(2);
  1147. ip = _default_arg_ptr[defarg];
  1148. }
  1149. DISPATCH_OPCODE;
  1150. OPCODE(OPCODE_RETURN) {
  1151. CHECK_SPACE(2);
  1152. GET_VARIANT_PTR(r, 1);
  1153. retvalue = *r;
  1154. #ifdef DEBUG_ENABLED
  1155. exit_ok = true;
  1156. #endif
  1157. OPCODE_BREAK;
  1158. }
  1159. OPCODE(OPCODE_ITERATE_BEGIN) {
  1160. CHECK_SPACE(8); //space for this a regular iterate
  1161. GET_VARIANT_PTR(counter, 1);
  1162. GET_VARIANT_PTR(container, 2);
  1163. bool valid;
  1164. if (!container->iter_init(*counter, valid)) {
  1165. #ifdef DEBUG_ENABLED
  1166. if (!valid) {
  1167. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1168. OPCODE_BREAK;
  1169. }
  1170. #endif
  1171. int jumpto = _code_ptr[ip + 3];
  1172. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1173. ip = jumpto;
  1174. } else {
  1175. GET_VARIANT_PTR(iterator, 4);
  1176. *iterator = container->iter_get(*counter, valid);
  1177. #ifdef DEBUG_ENABLED
  1178. if (!valid) {
  1179. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1180. OPCODE_BREAK;
  1181. }
  1182. #endif
  1183. ip += 5; //skip regular iterate which is always next
  1184. }
  1185. }
  1186. DISPATCH_OPCODE;
  1187. OPCODE(OPCODE_ITERATE) {
  1188. CHECK_SPACE(4);
  1189. GET_VARIANT_PTR(counter, 1);
  1190. GET_VARIANT_PTR(container, 2);
  1191. bool valid;
  1192. if (!container->iter_next(*counter, valid)) {
  1193. #ifdef DEBUG_ENABLED
  1194. if (!valid) {
  1195. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1196. OPCODE_BREAK;
  1197. }
  1198. #endif
  1199. int jumpto = _code_ptr[ip + 3];
  1200. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1201. ip = jumpto;
  1202. } else {
  1203. GET_VARIANT_PTR(iterator, 4);
  1204. *iterator = container->iter_get(*counter, valid);
  1205. #ifdef DEBUG_ENABLED
  1206. if (!valid) {
  1207. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1208. OPCODE_BREAK;
  1209. }
  1210. #endif
  1211. ip += 5; //loop again
  1212. }
  1213. }
  1214. DISPATCH_OPCODE;
  1215. OPCODE(OPCODE_ASSERT) {
  1216. CHECK_SPACE(3);
  1217. #ifdef DEBUG_ENABLED
  1218. GET_VARIANT_PTR(test, 1);
  1219. bool result = test->booleanize();
  1220. if (!result) {
  1221. String message_str;
  1222. if (_code_ptr[ip + 2] != 0) {
  1223. GET_VARIANT_PTR(message, 2);
  1224. message_str = *message;
  1225. }
  1226. if (message_str.empty()) {
  1227. err_text = "Assertion failed.";
  1228. } else {
  1229. err_text = "Assertion failed: " + message_str;
  1230. }
  1231. OPCODE_BREAK;
  1232. }
  1233. #endif
  1234. ip += 3;
  1235. }
  1236. DISPATCH_OPCODE;
  1237. OPCODE(OPCODE_BREAKPOINT) {
  1238. #ifdef DEBUG_ENABLED
  1239. if (ScriptDebugger::get_singleton()) {
  1240. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1241. }
  1242. #endif
  1243. ip += 1;
  1244. }
  1245. DISPATCH_OPCODE;
  1246. OPCODE(OPCODE_LINE) {
  1247. CHECK_SPACE(2);
  1248. line = _code_ptr[ip + 1];
  1249. ip += 2;
  1250. if (ScriptDebugger::get_singleton()) {
  1251. // line
  1252. bool do_break = false;
  1253. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1254. if (ScriptDebugger::get_singleton()->get_depth() <= 0) {
  1255. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1256. }
  1257. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0) {
  1258. do_break = true;
  1259. }
  1260. }
  1261. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source)) {
  1262. do_break = true;
  1263. }
  1264. if (do_break) {
  1265. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1266. }
  1267. ScriptDebugger::get_singleton()->line_poll();
  1268. }
  1269. }
  1270. DISPATCH_OPCODE;
  1271. OPCODE(OPCODE_END) {
  1272. #ifdef DEBUG_ENABLED
  1273. exit_ok = true;
  1274. #endif
  1275. OPCODE_BREAK;
  1276. }
  1277. #if 0 // Enable for debugging.
  1278. default: {
  1279. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1280. OPCODE_BREAK;
  1281. }
  1282. #endif
  1283. }
  1284. OPCODES_END
  1285. #ifdef DEBUG_ENABLED
  1286. if (exit_ok) {
  1287. OPCODE_OUT;
  1288. }
  1289. //error
  1290. // function, file, line, error, explanation
  1291. String err_file;
  1292. if (p_instance && ObjectDB::instance_validate(p_instance->owner) && p_instance->script->is_valid() && p_instance->script->path != "") {
  1293. err_file = p_instance->script->path;
  1294. } else if (script) {
  1295. err_file = script->path;
  1296. }
  1297. if (err_file == "") {
  1298. err_file = "<built-in>";
  1299. }
  1300. String err_func = name;
  1301. if (p_instance && ObjectDB::instance_validate(p_instance->owner) && p_instance->script->is_valid() && p_instance->script->name != "") {
  1302. err_func = p_instance->script->name + "." + err_func;
  1303. }
  1304. int err_line = line;
  1305. if (err_text == "") {
  1306. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1307. }
  1308. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1309. // debugger break did not happen
  1310. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1311. }
  1312. #endif
  1313. OPCODE_OUT;
  1314. }
  1315. OPCODES_OUT
  1316. #ifdef DEBUG_ENABLED
  1317. if (GDScriptLanguage::get_singleton()->profiling) {
  1318. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1319. profile.total_time += time_taken;
  1320. profile.self_time += time_taken - function_call_time;
  1321. profile.frame_total_time += time_taken;
  1322. profile.frame_self_time += time_taken - function_call_time;
  1323. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1324. }
  1325. // Check if this is the last time the function is resuming from yield
  1326. // Will be true if never yielded as well
  1327. // When it's the last resume it will postpone the exit from stack,
  1328. // so the debugger knows which function triggered the resume of the next function (if any)
  1329. if (!p_state || yielded) {
  1330. if (ScriptDebugger::get_singleton()) {
  1331. GDScriptLanguage::get_singleton()->exit_function();
  1332. }
  1333. #endif
  1334. if (_stack_size) {
  1335. //free stack
  1336. for (int i = 0; i < _stack_size; i++) {
  1337. stack[i].~Variant();
  1338. }
  1339. }
  1340. #ifdef DEBUG_ENABLED
  1341. }
  1342. #endif
  1343. return retvalue;
  1344. }
  1345. const int *GDScriptFunction::get_code() const {
  1346. return _code_ptr;
  1347. }
  1348. int GDScriptFunction::get_code_size() const {
  1349. return _code_size;
  1350. }
  1351. Variant GDScriptFunction::get_constant(int p_idx) const {
  1352. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1353. return constants[p_idx];
  1354. }
  1355. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1356. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1357. return global_names[p_idx];
  1358. }
  1359. int GDScriptFunction::get_default_argument_count() const {
  1360. return _default_arg_count;
  1361. }
  1362. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1363. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1364. return default_arguments[p_idx];
  1365. }
  1366. GDScriptDataType GDScriptFunction::get_return_type() const {
  1367. return return_type;
  1368. }
  1369. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1370. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1371. return argument_types[p_idx];
  1372. }
  1373. StringName GDScriptFunction::get_name() const {
  1374. return name;
  1375. }
  1376. int GDScriptFunction::get_max_stack_size() const {
  1377. return _stack_size;
  1378. }
  1379. struct _GDFKC {
  1380. int order;
  1381. List<int> pos;
  1382. };
  1383. struct _GDFKCS {
  1384. int order;
  1385. StringName id;
  1386. int pos;
  1387. bool operator<(const _GDFKCS &p_r) const {
  1388. return order < p_r.order;
  1389. }
  1390. };
  1391. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const {
  1392. int oc = 0;
  1393. Map<StringName, _GDFKC> sdmap;
  1394. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1395. const StackDebug &sd = E->get();
  1396. if (sd.line > p_line) {
  1397. break;
  1398. }
  1399. if (sd.added) {
  1400. if (!sdmap.has(sd.identifier)) {
  1401. _GDFKC d;
  1402. d.order = oc++;
  1403. d.pos.push_back(sd.pos);
  1404. sdmap[sd.identifier] = d;
  1405. } else {
  1406. sdmap[sd.identifier].pos.push_back(sd.pos);
  1407. }
  1408. } else {
  1409. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1410. sdmap[sd.identifier].pos.pop_back();
  1411. if (sdmap[sd.identifier].pos.empty()) {
  1412. sdmap.erase(sd.identifier);
  1413. }
  1414. }
  1415. }
  1416. List<_GDFKCS> stackpositions;
  1417. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1418. _GDFKCS spp;
  1419. spp.id = E->key();
  1420. spp.order = E->get().order;
  1421. spp.pos = E->get().pos.back()->get();
  1422. stackpositions.push_back(spp);
  1423. }
  1424. stackpositions.sort();
  1425. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1426. Pair<StringName, int> p;
  1427. p.first = E->get().id;
  1428. p.second = E->get().pos;
  1429. r_stackvars->push_back(p);
  1430. }
  1431. }
  1432. GDScriptFunction::GDScriptFunction() :
  1433. function_list(this) {
  1434. _stack_size = 0;
  1435. _call_size = 0;
  1436. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1437. name = "<anonymous>";
  1438. #ifdef DEBUG_ENABLED
  1439. _func_cname = nullptr;
  1440. GDScriptLanguage::get_singleton()->lock.lock();
  1441. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1442. GDScriptLanguage::get_singleton()->lock.unlock();
  1443. profile.call_count = 0;
  1444. profile.self_time = 0;
  1445. profile.total_time = 0;
  1446. profile.frame_call_count = 0;
  1447. profile.frame_self_time = 0;
  1448. profile.frame_total_time = 0;
  1449. profile.last_frame_call_count = 0;
  1450. profile.last_frame_self_time = 0;
  1451. profile.last_frame_total_time = 0;
  1452. #endif
  1453. }
  1454. GDScriptFunction::~GDScriptFunction() {
  1455. #ifdef DEBUG_ENABLED
  1456. GDScriptLanguage::get_singleton()->lock.lock();
  1457. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1458. GDScriptLanguage::get_singleton()->lock.unlock();
  1459. #endif
  1460. }
  1461. /////////////////////
  1462. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1463. Variant arg;
  1464. r_error.error = Variant::CallError::CALL_OK;
  1465. if (p_argcount == 0) {
  1466. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1467. r_error.argument = 1;
  1468. return Variant();
  1469. } else if (p_argcount == 1) {
  1470. //noooneee
  1471. } else if (p_argcount == 2) {
  1472. arg = *p_args[0];
  1473. } else {
  1474. Array extra_args;
  1475. for (int i = 0; i < p_argcount - 1; i++) {
  1476. extra_args.push_back(*p_args[i]);
  1477. }
  1478. arg = extra_args;
  1479. }
  1480. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1481. if (self.is_null()) {
  1482. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1483. r_error.argument = p_argcount - 1;
  1484. r_error.expected = Variant::OBJECT;
  1485. return Variant();
  1486. }
  1487. return resume(arg);
  1488. }
  1489. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1490. if (function == nullptr) {
  1491. return false;
  1492. }
  1493. if (p_extended_check) {
  1494. #ifndef NO_THREADS
  1495. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1496. #endif
  1497. // Script gone?
  1498. if (!scripts_list.in_list()) {
  1499. return false;
  1500. }
  1501. // Class instance gone? (if not static function)
  1502. if (state.instance && !instances_list.in_list()) {
  1503. return false;
  1504. }
  1505. }
  1506. return true;
  1507. }
  1508. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1509. ERR_FAIL_COND_V(!function, Variant());
  1510. {
  1511. #ifndef NO_THREADS
  1512. MutexLock lock(GDScriptLanguage::singleton->lock);
  1513. #endif
  1514. if (!scripts_list.in_list()) {
  1515. #ifdef DEBUG_ENABLED
  1516. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after yield, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
  1517. #else
  1518. return Variant();
  1519. #endif
  1520. }
  1521. if (state.instance && !instances_list.in_list()) {
  1522. #ifdef DEBUG_ENABLED
  1523. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after yield, but class instance is gone. At script: " + state.script_path + ":" + itos(state.line));
  1524. #else
  1525. return Variant();
  1526. #endif
  1527. }
  1528. // Do these now to avoid locking again after the call
  1529. scripts_list.remove_from_list();
  1530. instances_list.remove_from_list();
  1531. }
  1532. state.result = p_arg;
  1533. Variant::CallError err;
  1534. Variant ret = function->call(nullptr, nullptr, 0, err, &state);
  1535. bool completed = true;
  1536. // If the return value is a GDScriptFunctionState reference,
  1537. // then the function did yield again after resuming.
  1538. if (ret.is_ref()) {
  1539. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1540. if (gdfs && gdfs->function == function) {
  1541. completed = false;
  1542. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1543. }
  1544. }
  1545. function = nullptr; //cleaned up;
  1546. state.result = Variant();
  1547. if (completed) {
  1548. if (first_state.is_valid()) {
  1549. first_state->emit_signal("completed", ret);
  1550. } else {
  1551. emit_signal("completed", ret);
  1552. }
  1553. #ifdef DEBUG_ENABLED
  1554. if (ScriptDebugger::get_singleton()) {
  1555. GDScriptLanguage::get_singleton()->exit_function();
  1556. }
  1557. #endif
  1558. }
  1559. return ret;
  1560. }
  1561. void GDScriptFunctionState::_clear_stack() {
  1562. if (state.stack_size) {
  1563. Variant *stack = (Variant *)state.stack.ptr();
  1564. for (int i = 0; i < state.stack_size; i++) {
  1565. stack[i].~Variant();
  1566. }
  1567. state.stack_size = 0;
  1568. }
  1569. }
  1570. void GDScriptFunctionState::_bind_methods() {
  1571. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1572. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1573. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1574. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1575. }
  1576. GDScriptFunctionState::GDScriptFunctionState() :
  1577. scripts_list(this),
  1578. instances_list(this) {
  1579. function = nullptr;
  1580. }
  1581. GDScriptFunctionState::~GDScriptFunctionState() {
  1582. _clear_stack();
  1583. GDScriptLanguage::singleton->lock.lock();
  1584. scripts_list.remove_from_list();
  1585. instances_list.remove_from_list();
  1586. GDScriptLanguage::singleton->lock.unlock();
  1587. }