gdscript_function.cpp 52 KB

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