gdscript_function.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_function.h"
  31. #include "core/os/os.h"
  32. #include "gdscript.h"
  33. #include "gdscript_functions.h"
  34. 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 NULL;
  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 NULL;
  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, NULL);
  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(NULL, "GDScriptCompiler bug.");
  79. } break;
  80. case ADDR_TYPE_LOCAL_CONSTANT: {
  81. #ifdef DEBUG_ENABLED
  82. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  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, NULL);
  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(), NULL);
  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, NULL);
  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 NULL;
  110. }
  111. } break;
  112. #endif
  113. case ADDR_TYPE_NIL: {
  114. return &nil;
  115. } break;
  116. }
  117. ERR_FAIL_V_MSG(NULL, "Bad code! (unknown addressing mode).");
  118. return NULL;
  119. }
  120. #ifdef DEBUG_ENABLED
  121. static String _get_var_type(const Variant *p_var) {
  122. String basestr;
  123. if (p_var->get_type() == Variant::OBJECT) {
  124. bool was_freed;
  125. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  126. if (!bobj) {
  127. if (was_freed) {
  128. basestr = "null instance";
  129. } else {
  130. basestr = "previously freed";
  131. }
  132. } else {
  133. if (bobj->get_script_instance())
  134. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  135. else
  136. basestr = bobj->get_class();
  137. }
  138. } 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 Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  145. String err_text;
  146. if (p_err.error == Callable::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(Variant::Type(p_err.expected)) + ".";
  156. }
  157. } else if (p_err.error == Callable::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 == Callable::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 == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  162. err_text = "Invalid call. Nonexistent " + p_where + ".";
  163. } else if (p_err.error == Callable::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, Callable::CallError &r_err, CallState *p_state) {
  237. OPCODES_TABLE;
  238. if (!_code_ptr) {
  239. return Variant();
  240. }
  241. r_err.error = Callable::CallError::CALL_OK;
  242. Variant self;
  243. Variant static_ref;
  244. Variant retvalue;
  245. Variant *stack = NULL;
  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.ptr();
  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 = Callable::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 = Callable::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 = Callable::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 = NULL;
  314. }
  315. if (_call_size) {
  316. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  317. } else {
  318. call_args = NULL;
  319. }
  320. } else {
  321. stack = NULL;
  322. call_args = NULL;
  323. }
  324. if (p_instance) {
  325. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  326. self = REF(static_cast<Reference *>(p_instance->owner));
  327. } else {
  328. self = p_instance->owner;
  329. }
  330. script = p_instance->script.ptr();
  331. } else {
  332. script = _script;
  333. }
  334. }
  335. String err_text;
  336. #ifdef DEBUG_ENABLED
  337. if (EngineDebugger::is_active())
  338. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  339. #define GD_ERR_BREAK(m_cond) \
  340. { \
  341. if (unlikely(m_cond)) { \
  342. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  343. OPCODE_BREAK; \
  344. } \
  345. }
  346. #define CHECK_SPACE(m_space) \
  347. GD_ERR_BREAK((ip + m_space) > _code_size)
  348. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  349. Variant *m_v; \
  350. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  351. if (unlikely(!m_v)) \
  352. OPCODE_BREAK;
  353. #else
  354. #define GD_ERR_BREAK(m_cond)
  355. #define CHECK_SPACE(m_space)
  356. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  357. Variant *m_v; \
  358. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  359. #endif
  360. #ifdef DEBUG_ENABLED
  361. uint64_t function_start_time = 0;
  362. uint64_t function_call_time = 0;
  363. if (GDScriptLanguage::get_singleton()->profiling) {
  364. function_start_time = OS::get_singleton()->get_ticks_usec();
  365. function_call_time = 0;
  366. profile.call_count++;
  367. profile.frame_call_count++;
  368. }
  369. bool exit_ok = false;
  370. bool yielded = false;
  371. #endif
  372. #ifdef DEBUG_ENABLED
  373. OPCODE_WHILE(ip < _code_size) {
  374. int last_opcode = _code_ptr[ip];
  375. #else
  376. OPCODE_WHILE(true) {
  377. #endif
  378. OPCODE_SWITCH(_code_ptr[ip]) {
  379. OPCODE(OPCODE_OPERATOR) {
  380. CHECK_SPACE(5);
  381. bool valid;
  382. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  383. GD_ERR_BREAK(op >= Variant::OP_MAX);
  384. GET_VARIANT_PTR(a, 2);
  385. GET_VARIANT_PTR(b, 3);
  386. GET_VARIANT_PTR(dst, 4);
  387. #ifdef DEBUG_ENABLED
  388. Variant ret;
  389. Variant::evaluate(op, *a, *b, ret, valid);
  390. #else
  391. Variant::evaluate(op, *a, *b, *dst, valid);
  392. #endif
  393. #ifdef DEBUG_ENABLED
  394. if (!valid) {
  395. if (ret.get_type() == Variant::STRING) {
  396. //return a string when invalid with the error
  397. err_text = ret;
  398. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  399. } else {
  400. 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) + "'.";
  401. }
  402. OPCODE_BREAK;
  403. }
  404. *dst = ret;
  405. #endif
  406. ip += 5;
  407. }
  408. DISPATCH_OPCODE;
  409. OPCODE(OPCODE_EXTENDS_TEST) {
  410. CHECK_SPACE(4);
  411. GET_VARIANT_PTR(a, 1);
  412. GET_VARIANT_PTR(b, 2);
  413. GET_VARIANT_PTR(dst, 3);
  414. #ifdef DEBUG_ENABLED
  415. if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) {
  416. err_text = "Right operand of 'is' is not a class.";
  417. OPCODE_BREAK;
  418. }
  419. #endif
  420. bool extends_ok = false;
  421. if (a->get_type() == Variant::OBJECT && a->operator Object *() != NULL) {
  422. #ifdef DEBUG_ENABLED
  423. bool was_freed;
  424. Object *obj_A = a->get_validated_object_with_check(was_freed);
  425. if (was_freed) {
  426. err_text = "Left operand of 'is' is a previously freed instance.";
  427. OPCODE_BREAK;
  428. }
  429. Object *obj_B = b->get_validated_object_with_check(was_freed);
  430. if (was_freed) {
  431. err_text = "Right operand of 'is' is a previously freed instance.";
  432. OPCODE_BREAK;
  433. }
  434. #else
  435. Object *obj_A = *a;
  436. Object *obj_B = *b;
  437. #endif // DEBUG_ENABLED
  438. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  439. if (scr_B) {
  440. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  441. //in other situation, this shoul return false.
  442. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  443. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  444. //bool found=false;
  445. while (cmp) {
  446. if (cmp == scr_B) {
  447. //inherits from script, all ok
  448. extends_ok = true;
  449. break;
  450. }
  451. cmp = cmp->_base;
  452. }
  453. }
  454. } else {
  455. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  456. #ifdef DEBUG_ENABLED
  457. if (!nc) {
  458. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  459. OPCODE_BREAK;
  460. }
  461. #endif
  462. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  463. }
  464. }
  465. *dst = extends_ok;
  466. ip += 4;
  467. }
  468. DISPATCH_OPCODE;
  469. OPCODE(OPCODE_IS_BUILTIN) {
  470. CHECK_SPACE(4);
  471. GET_VARIANT_PTR(value, 1);
  472. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  473. GET_VARIANT_PTR(dst, 3);
  474. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  475. *dst = value->get_type() == var_type;
  476. ip += 4;
  477. }
  478. DISPATCH_OPCODE;
  479. OPCODE(OPCODE_SET) {
  480. CHECK_SPACE(3);
  481. GET_VARIANT_PTR(dst, 1);
  482. GET_VARIANT_PTR(index, 2);
  483. GET_VARIANT_PTR(value, 3);
  484. bool valid;
  485. dst->set(*index, *value, &valid);
  486. #ifdef DEBUG_ENABLED
  487. if (!valid) {
  488. String v = index->operator String();
  489. if (v != "") {
  490. v = "'" + v + "'";
  491. } else {
  492. v = "of type '" + _get_var_type(index) + "'";
  493. }
  494. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  495. OPCODE_BREAK;
  496. }
  497. #endif
  498. ip += 4;
  499. }
  500. DISPATCH_OPCODE;
  501. OPCODE(OPCODE_GET) {
  502. CHECK_SPACE(3);
  503. GET_VARIANT_PTR(src, 1);
  504. GET_VARIANT_PTR(index, 2);
  505. GET_VARIANT_PTR(dst, 3);
  506. bool valid;
  507. #ifdef DEBUG_ENABLED
  508. //allow better error message in cases where src and dst are the same stack position
  509. Variant ret = src->get(*index, &valid);
  510. #else
  511. *dst = src->get(*index, &valid);
  512. #endif
  513. #ifdef DEBUG_ENABLED
  514. if (!valid) {
  515. String v = index->operator String();
  516. if (v != "") {
  517. v = "'" + v + "'";
  518. } else {
  519. v = "of type '" + _get_var_type(index) + "'";
  520. }
  521. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  522. OPCODE_BREAK;
  523. }
  524. *dst = ret;
  525. #endif
  526. ip += 4;
  527. }
  528. DISPATCH_OPCODE;
  529. OPCODE(OPCODE_SET_NAMED) {
  530. CHECK_SPACE(3);
  531. GET_VARIANT_PTR(dst, 1);
  532. GET_VARIANT_PTR(value, 3);
  533. int indexname = _code_ptr[ip + 2];
  534. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  535. const StringName *index = &_global_names_ptr[indexname];
  536. bool valid;
  537. dst->set_named(*index, *value, &valid);
  538. #ifdef DEBUG_ENABLED
  539. if (!valid) {
  540. String err_type;
  541. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  542. OPCODE_BREAK;
  543. }
  544. #endif
  545. ip += 4;
  546. }
  547. DISPATCH_OPCODE;
  548. OPCODE(OPCODE_GET_NAMED) {
  549. CHECK_SPACE(4);
  550. GET_VARIANT_PTR(src, 1);
  551. GET_VARIANT_PTR(dst, 3);
  552. int indexname = _code_ptr[ip + 2];
  553. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  554. const StringName *index = &_global_names_ptr[indexname];
  555. bool valid;
  556. #ifdef DEBUG_ENABLED
  557. //allow better error message in cases where src and dst are the same stack position
  558. Variant ret = src->get_named(*index, &valid);
  559. #else
  560. *dst = src->get_named(*index, &valid);
  561. #endif
  562. #ifdef DEBUG_ENABLED
  563. if (!valid) {
  564. if (src->has_method(*index)) {
  565. 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() + "\") ?";
  566. } else {
  567. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  568. }
  569. OPCODE_BREAK;
  570. }
  571. *dst = ret;
  572. #endif
  573. ip += 4;
  574. }
  575. DISPATCH_OPCODE;
  576. OPCODE(OPCODE_SET_MEMBER) {
  577. CHECK_SPACE(3);
  578. int indexname = _code_ptr[ip + 1];
  579. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  580. const StringName *index = &_global_names_ptr[indexname];
  581. GET_VARIANT_PTR(src, 2);
  582. bool valid;
  583. #ifndef DEBUG_ENABLED
  584. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  585. #else
  586. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  587. if (!ok) {
  588. err_text = "Internal error setting property: " + String(*index);
  589. OPCODE_BREAK;
  590. } else if (!valid) {
  591. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  592. OPCODE_BREAK;
  593. }
  594. #endif
  595. ip += 3;
  596. }
  597. DISPATCH_OPCODE;
  598. OPCODE(OPCODE_GET_MEMBER) {
  599. CHECK_SPACE(3);
  600. int indexname = _code_ptr[ip + 1];
  601. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  602. const StringName *index = &_global_names_ptr[indexname];
  603. GET_VARIANT_PTR(dst, 2);
  604. #ifndef DEBUG_ENABLED
  605. ClassDB::get_property(p_instance->owner, *index, *dst);
  606. #else
  607. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  608. if (!ok) {
  609. err_text = "Internal error getting property: " + String(*index);
  610. OPCODE_BREAK;
  611. }
  612. #endif
  613. ip += 3;
  614. }
  615. DISPATCH_OPCODE;
  616. OPCODE(OPCODE_ASSIGN) {
  617. CHECK_SPACE(3);
  618. GET_VARIANT_PTR(dst, 1);
  619. GET_VARIANT_PTR(src, 2);
  620. *dst = *src;
  621. ip += 3;
  622. }
  623. DISPATCH_OPCODE;
  624. OPCODE(OPCODE_ASSIGN_TRUE) {
  625. CHECK_SPACE(2);
  626. GET_VARIANT_PTR(dst, 1);
  627. *dst = true;
  628. ip += 2;
  629. }
  630. DISPATCH_OPCODE;
  631. OPCODE(OPCODE_ASSIGN_FALSE) {
  632. CHECK_SPACE(2);
  633. GET_VARIANT_PTR(dst, 1);
  634. *dst = false;
  635. ip += 2;
  636. }
  637. DISPATCH_OPCODE;
  638. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  639. CHECK_SPACE(4);
  640. GET_VARIANT_PTR(dst, 2);
  641. GET_VARIANT_PTR(src, 3);
  642. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  643. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  644. if (src->get_type() != var_type) {
  645. #ifdef DEBUG_ENABLED
  646. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  647. #endif // DEBUG_ENABLED
  648. Callable::CallError ce;
  649. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  650. } else {
  651. #ifdef DEBUG_ENABLED
  652. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  653. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  654. OPCODE_BREAK;
  655. }
  656. } else {
  657. #endif // DEBUG_ENABLED
  658. *dst = *src;
  659. }
  660. ip += 4;
  661. }
  662. DISPATCH_OPCODE;
  663. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  664. CHECK_SPACE(4);
  665. GET_VARIANT_PTR(dst, 2);
  666. GET_VARIANT_PTR(src, 3);
  667. #ifdef DEBUG_ENABLED
  668. GET_VARIANT_PTR(type, 1);
  669. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  670. GD_ERR_BREAK(!nc);
  671. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  672. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  673. "' to a variable of type '" + nc->get_name() + "'.";
  674. OPCODE_BREAK;
  675. }
  676. Object *src_obj = src->operator Object *();
  677. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  678. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  679. "' to a variable of type '" + nc->get_name() + "'.";
  680. OPCODE_BREAK;
  681. }
  682. #endif // DEBUG_ENABLED
  683. *dst = *src;
  684. ip += 4;
  685. }
  686. DISPATCH_OPCODE;
  687. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  688. CHECK_SPACE(4);
  689. GET_VARIANT_PTR(dst, 2);
  690. GET_VARIANT_PTR(src, 3);
  691. #ifdef DEBUG_ENABLED
  692. GET_VARIANT_PTR(type, 1);
  693. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  694. GD_ERR_BREAK(!base_type);
  695. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  696. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  697. OPCODE_BREAK;
  698. }
  699. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  700. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  701. if (!scr_inst) {
  702. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  703. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  704. OPCODE_BREAK;
  705. }
  706. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  707. bool valid = false;
  708. while (src_type) {
  709. if (src_type == base_type) {
  710. valid = true;
  711. break;
  712. }
  713. src_type = src_type->get_base_script().ptr();
  714. }
  715. if (!valid) {
  716. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  717. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  718. OPCODE_BREAK;
  719. }
  720. }
  721. #endif // DEBUG_ENABLED
  722. *dst = *src;
  723. ip += 4;
  724. }
  725. DISPATCH_OPCODE;
  726. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  727. CHECK_SPACE(4);
  728. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  729. GET_VARIANT_PTR(src, 2);
  730. GET_VARIANT_PTR(dst, 3);
  731. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  732. Callable::CallError err;
  733. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  734. #ifdef DEBUG_ENABLED
  735. if (err.error != Callable::CallError::CALL_OK) {
  736. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  737. OPCODE_BREAK;
  738. }
  739. #endif
  740. ip += 4;
  741. }
  742. DISPATCH_OPCODE;
  743. OPCODE(OPCODE_CAST_TO_NATIVE) {
  744. CHECK_SPACE(4);
  745. GET_VARIANT_PTR(to_type, 1);
  746. GET_VARIANT_PTR(src, 2);
  747. GET_VARIANT_PTR(dst, 3);
  748. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  749. GD_ERR_BREAK(!nc);
  750. #ifdef DEBUG_ENABLED
  751. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  752. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  753. OPCODE_BREAK;
  754. }
  755. #endif
  756. Object *src_obj = src->operator Object *();
  757. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  758. *dst = Variant(); // invalid cast, assign NULL
  759. } else {
  760. *dst = *src;
  761. }
  762. ip += 4;
  763. }
  764. DISPATCH_OPCODE;
  765. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  766. CHECK_SPACE(4);
  767. GET_VARIANT_PTR(to_type, 1);
  768. GET_VARIANT_PTR(src, 2);
  769. GET_VARIANT_PTR(dst, 3);
  770. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  771. GD_ERR_BREAK(!base_type);
  772. #ifdef DEBUG_ENABLED
  773. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  774. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  775. OPCODE_BREAK;
  776. }
  777. #endif
  778. bool valid = false;
  779. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  780. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  781. if (scr_inst) {
  782. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  783. while (src_type) {
  784. if (src_type == base_type) {
  785. valid = true;
  786. break;
  787. }
  788. src_type = src_type->get_base_script().ptr();
  789. }
  790. }
  791. }
  792. if (valid) {
  793. *dst = *src; // Valid cast, copy the source object
  794. } else {
  795. *dst = Variant(); // invalid cast, assign NULL
  796. }
  797. ip += 4;
  798. }
  799. DISPATCH_OPCODE;
  800. OPCODE(OPCODE_CONSTRUCT) {
  801. CHECK_SPACE(2);
  802. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  803. int argc = _code_ptr[ip + 2];
  804. CHECK_SPACE(argc + 2);
  805. Variant **argptrs = call_args;
  806. for (int i = 0; i < argc; i++) {
  807. GET_VARIANT_PTR(v, 3 + i);
  808. argptrs[i] = v;
  809. }
  810. GET_VARIANT_PTR(dst, 3 + argc);
  811. Callable::CallError err;
  812. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  813. #ifdef DEBUG_ENABLED
  814. if (err.error != Callable::CallError::CALL_OK) {
  815. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  816. OPCODE_BREAK;
  817. }
  818. #endif
  819. ip += 4 + argc;
  820. //construct a basic type
  821. }
  822. DISPATCH_OPCODE;
  823. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  824. CHECK_SPACE(1);
  825. int argc = _code_ptr[ip + 1];
  826. Array array; //arrays are always shared
  827. array.resize(argc);
  828. CHECK_SPACE(argc + 2);
  829. for (int i = 0; i < argc; i++) {
  830. GET_VARIANT_PTR(v, 2 + i);
  831. array[i] = *v;
  832. }
  833. GET_VARIANT_PTR(dst, 2 + argc);
  834. *dst = array;
  835. ip += 3 + argc;
  836. }
  837. DISPATCH_OPCODE;
  838. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  839. CHECK_SPACE(1);
  840. int argc = _code_ptr[ip + 1];
  841. Dictionary dict; //arrays are always shared
  842. CHECK_SPACE(argc * 2 + 2);
  843. for (int i = 0; i < argc; i++) {
  844. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  845. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  846. dict[*k] = *v;
  847. }
  848. GET_VARIANT_PTR(dst, 2 + argc * 2);
  849. *dst = dict;
  850. ip += 3 + argc * 2;
  851. }
  852. DISPATCH_OPCODE;
  853. OPCODE(OPCODE_CALL_RETURN)
  854. OPCODE(OPCODE_CALL) {
  855. CHECK_SPACE(4);
  856. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  857. int argc = _code_ptr[ip + 1];
  858. GET_VARIANT_PTR(base, 2);
  859. int nameg = _code_ptr[ip + 3];
  860. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  861. const StringName *methodname = &_global_names_ptr[nameg];
  862. GD_ERR_BREAK(argc < 0);
  863. ip += 4;
  864. CHECK_SPACE(argc + 1);
  865. Variant **argptrs = call_args;
  866. for (int i = 0; i < argc; i++) {
  867. GET_VARIANT_PTR(v, i);
  868. argptrs[i] = v;
  869. }
  870. #ifdef DEBUG_ENABLED
  871. uint64_t call_time = 0;
  872. if (GDScriptLanguage::get_singleton()->profiling) {
  873. call_time = OS::get_singleton()->get_ticks_usec();
  874. }
  875. #endif
  876. Callable::CallError err;
  877. if (call_ret) {
  878. GET_VARIANT_PTR(ret, argc);
  879. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  880. } else {
  881. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  882. }
  883. #ifdef DEBUG_ENABLED
  884. if (GDScriptLanguage::get_singleton()->profiling) {
  885. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  886. }
  887. if (err.error != Callable::CallError::CALL_OK) {
  888. String methodstr = *methodname;
  889. String basestr = _get_var_type(base);
  890. if (methodstr == "call") {
  891. if (argc >= 1) {
  892. methodstr = String(*argptrs[0]) + " (via call)";
  893. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  894. err.argument += 1;
  895. }
  896. }
  897. } else if (methodstr == "free") {
  898. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  899. if (base->is_ref()) {
  900. err_text = "Attempted to free a reference.";
  901. OPCODE_BREAK;
  902. } else if (base->get_type() == Variant::OBJECT) {
  903. err_text = "Attempted to free a locked object (calling or emitting).";
  904. OPCODE_BREAK;
  905. }
  906. }
  907. }
  908. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  909. OPCODE_BREAK;
  910. }
  911. #endif
  912. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  913. ip += argc + 1;
  914. }
  915. DISPATCH_OPCODE;
  916. OPCODE(OPCODE_CALL_BUILT_IN) {
  917. CHECK_SPACE(4);
  918. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  919. int argc = _code_ptr[ip + 2];
  920. GD_ERR_BREAK(argc < 0);
  921. ip += 3;
  922. CHECK_SPACE(argc + 1);
  923. Variant **argptrs = call_args;
  924. for (int i = 0; i < argc; i++) {
  925. GET_VARIANT_PTR(v, i);
  926. argptrs[i] = v;
  927. }
  928. GET_VARIANT_PTR(dst, argc);
  929. Callable::CallError err;
  930. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  931. #ifdef DEBUG_ENABLED
  932. if (err.error != Callable::CallError::CALL_OK) {
  933. String methodstr = GDScriptFunctions::get_func_name(func);
  934. if (dst->get_type() == Variant::STRING) {
  935. //call provided error string
  936. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  937. } else {
  938. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  939. }
  940. OPCODE_BREAK;
  941. }
  942. #endif
  943. ip += argc + 1;
  944. }
  945. DISPATCH_OPCODE;
  946. OPCODE(OPCODE_CALL_SELF) {
  947. OPCODE_BREAK;
  948. }
  949. OPCODE(OPCODE_CALL_SELF_BASE) {
  950. CHECK_SPACE(2);
  951. int self_fun = _code_ptr[ip + 1];
  952. #ifdef DEBUG_ENABLED
  953. if (self_fun < 0 || self_fun >= _global_names_count) {
  954. err_text = "compiler bug, function name not found";
  955. OPCODE_BREAK;
  956. }
  957. #endif
  958. const StringName *methodname = &_global_names_ptr[self_fun];
  959. int argc = _code_ptr[ip + 2];
  960. CHECK_SPACE(2 + argc + 1);
  961. Variant **argptrs = call_args;
  962. for (int i = 0; i < argc; i++) {
  963. GET_VARIANT_PTR(v, i + 3);
  964. argptrs[i] = v;
  965. }
  966. GET_VARIANT_PTR(dst, argc + 3);
  967. const GDScript *gds = _script;
  968. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  969. while (gds->base.ptr()) {
  970. gds = gds->base.ptr();
  971. E = gds->member_functions.find(*methodname);
  972. if (E)
  973. break;
  974. }
  975. Callable::CallError err;
  976. if (E) {
  977. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  978. } else if (gds->native.ptr()) {
  979. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  980. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  981. if (!mb) {
  982. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  983. } else {
  984. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  985. }
  986. } else {
  987. err.error = Callable::CallError::CALL_OK;
  988. }
  989. } else {
  990. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  991. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  992. } else {
  993. err.error = Callable::CallError::CALL_OK;
  994. }
  995. }
  996. if (err.error != Callable::CallError::CALL_OK) {
  997. String methodstr = *methodname;
  998. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  999. OPCODE_BREAK;
  1000. }
  1001. ip += 4 + argc;
  1002. }
  1003. DISPATCH_OPCODE;
  1004. OPCODE(OPCODE_YIELD)
  1005. OPCODE(OPCODE_YIELD_SIGNAL) {
  1006. int ipofs = 1;
  1007. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1008. CHECK_SPACE(4);
  1009. ipofs += 2;
  1010. } else {
  1011. CHECK_SPACE(2);
  1012. }
  1013. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1014. gdfs->function = this;
  1015. gdfs->state.stack.resize(alloca_size);
  1016. //copy variant stack
  1017. for (int i = 0; i < _stack_size; i++) {
  1018. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1019. }
  1020. gdfs->state.stack_size = _stack_size;
  1021. gdfs->state.self = self;
  1022. gdfs->state.alloca_size = alloca_size;
  1023. gdfs->state.script = Ref<GDScript>(_script);
  1024. gdfs->state.ip = ip + ipofs;
  1025. gdfs->state.line = line;
  1026. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : ObjectID();
  1027. //gdfs->state.result_pos=ip+ipofs-1;
  1028. gdfs->state.defarg = defarg;
  1029. gdfs->state.instance = p_instance;
  1030. gdfs->function = this;
  1031. retvalue = gdfs;
  1032. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1033. //do the oneshot connect
  1034. GET_VARIANT_PTR(argobj, 1);
  1035. GET_VARIANT_PTR(argname, 2);
  1036. #ifdef DEBUG_ENABLED
  1037. if (argobj->get_type() != Variant::OBJECT) {
  1038. err_text = "First argument of yield() not of type object.";
  1039. OPCODE_BREAK;
  1040. }
  1041. if (argname->get_type() != Variant::STRING) {
  1042. err_text = "Second argument of yield() not a string (for signal name).";
  1043. OPCODE_BREAK;
  1044. }
  1045. #endif
  1046. #ifdef DEBUG_ENABLED
  1047. bool was_freed;
  1048. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1049. String signal = argname->operator String();
  1050. if (was_freed) {
  1051. err_text = "First argument of yield() is a previously freed instance.";
  1052. OPCODE_BREAK;
  1053. }
  1054. if (!obj) {
  1055. err_text = "First argument of yield() is null.";
  1056. OPCODE_BREAK;
  1057. }
  1058. if (signal.length() == 0) {
  1059. err_text = "Second argument of yield() is an empty string (for signal name).";
  1060. OPCODE_BREAK;
  1061. }
  1062. Error err = obj->connect_compat(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1063. if (err != OK) {
  1064. err_text = "Error connecting to signal: " + signal + " during yield().";
  1065. OPCODE_BREAK;
  1066. }
  1067. #else
  1068. Object *obj = argobj->operator Object *();
  1069. String signal = argname->operator String();
  1070. obj->connect_compat(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1071. #endif
  1072. }
  1073. #ifdef DEBUG_ENABLED
  1074. exit_ok = true;
  1075. yielded = true;
  1076. #endif
  1077. OPCODE_BREAK;
  1078. }
  1079. OPCODE(OPCODE_YIELD_RESUME) {
  1080. CHECK_SPACE(2);
  1081. #ifdef DEBUG_ENABLED
  1082. if (!p_state) {
  1083. err_text = ("Invalid Resume (bug?)");
  1084. OPCODE_BREAK;
  1085. }
  1086. #endif
  1087. GET_VARIANT_PTR(result, 1);
  1088. *result = p_state->result;
  1089. ip += 2;
  1090. }
  1091. DISPATCH_OPCODE;
  1092. OPCODE(OPCODE_JUMP) {
  1093. CHECK_SPACE(2);
  1094. int to = _code_ptr[ip + 1];
  1095. GD_ERR_BREAK(to < 0 || to > _code_size);
  1096. ip = to;
  1097. }
  1098. DISPATCH_OPCODE;
  1099. OPCODE(OPCODE_JUMP_IF) {
  1100. CHECK_SPACE(3);
  1101. GET_VARIANT_PTR(test, 1);
  1102. bool result = test->booleanize();
  1103. if (result) {
  1104. int to = _code_ptr[ip + 2];
  1105. GD_ERR_BREAK(to < 0 || to > _code_size);
  1106. ip = to;
  1107. } else {
  1108. ip += 3;
  1109. }
  1110. }
  1111. DISPATCH_OPCODE;
  1112. OPCODE(OPCODE_JUMP_IF_NOT) {
  1113. CHECK_SPACE(3);
  1114. GET_VARIANT_PTR(test, 1);
  1115. bool result = test->booleanize();
  1116. if (!result) {
  1117. int to = _code_ptr[ip + 2];
  1118. GD_ERR_BREAK(to < 0 || to > _code_size);
  1119. ip = to;
  1120. } else {
  1121. ip += 3;
  1122. }
  1123. }
  1124. DISPATCH_OPCODE;
  1125. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1126. CHECK_SPACE(2);
  1127. ip = _default_arg_ptr[defarg];
  1128. }
  1129. DISPATCH_OPCODE;
  1130. OPCODE(OPCODE_RETURN) {
  1131. CHECK_SPACE(2);
  1132. GET_VARIANT_PTR(r, 1);
  1133. retvalue = *r;
  1134. #ifdef DEBUG_ENABLED
  1135. exit_ok = true;
  1136. #endif
  1137. OPCODE_BREAK;
  1138. }
  1139. OPCODE(OPCODE_ITERATE_BEGIN) {
  1140. CHECK_SPACE(8); //space for this a regular iterate
  1141. GET_VARIANT_PTR(counter, 1);
  1142. GET_VARIANT_PTR(container, 2);
  1143. bool valid;
  1144. if (!container->iter_init(*counter, valid)) {
  1145. #ifdef DEBUG_ENABLED
  1146. if (!valid) {
  1147. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1148. OPCODE_BREAK;
  1149. }
  1150. #endif
  1151. int jumpto = _code_ptr[ip + 3];
  1152. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1153. ip = jumpto;
  1154. } else {
  1155. GET_VARIANT_PTR(iterator, 4);
  1156. *iterator = container->iter_get(*counter, valid);
  1157. #ifdef DEBUG_ENABLED
  1158. if (!valid) {
  1159. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1160. OPCODE_BREAK;
  1161. }
  1162. #endif
  1163. ip += 5; //skip regular iterate which is always next
  1164. }
  1165. }
  1166. DISPATCH_OPCODE;
  1167. OPCODE(OPCODE_ITERATE) {
  1168. CHECK_SPACE(4);
  1169. GET_VARIANT_PTR(counter, 1);
  1170. GET_VARIANT_PTR(container, 2);
  1171. bool valid;
  1172. if (!container->iter_next(*counter, valid)) {
  1173. #ifdef DEBUG_ENABLED
  1174. if (!valid) {
  1175. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1176. OPCODE_BREAK;
  1177. }
  1178. #endif
  1179. int jumpto = _code_ptr[ip + 3];
  1180. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1181. ip = jumpto;
  1182. } else {
  1183. GET_VARIANT_PTR(iterator, 4);
  1184. *iterator = container->iter_get(*counter, valid);
  1185. #ifdef DEBUG_ENABLED
  1186. if (!valid) {
  1187. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1188. OPCODE_BREAK;
  1189. }
  1190. #endif
  1191. ip += 5; //loop again
  1192. }
  1193. }
  1194. DISPATCH_OPCODE;
  1195. OPCODE(OPCODE_ASSERT) {
  1196. CHECK_SPACE(3);
  1197. #ifdef DEBUG_ENABLED
  1198. GET_VARIANT_PTR(test, 1);
  1199. GET_VARIANT_PTR(message, 2);
  1200. bool result = test->booleanize();
  1201. if (!result) {
  1202. const String &message_str = *message;
  1203. if (message_str.empty()) {
  1204. err_text = "Assertion failed.";
  1205. } else {
  1206. err_text = "Assertion failed: " + message_str;
  1207. }
  1208. OPCODE_BREAK;
  1209. }
  1210. #endif
  1211. ip += 3;
  1212. }
  1213. DISPATCH_OPCODE;
  1214. OPCODE(OPCODE_BREAKPOINT) {
  1215. #ifdef DEBUG_ENABLED
  1216. if (EngineDebugger::is_active()) {
  1217. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1218. }
  1219. #endif
  1220. ip += 1;
  1221. }
  1222. DISPATCH_OPCODE;
  1223. OPCODE(OPCODE_LINE) {
  1224. CHECK_SPACE(2);
  1225. line = _code_ptr[ip + 1];
  1226. ip += 2;
  1227. if (EngineDebugger::is_active()) {
  1228. // line
  1229. bool do_break = false;
  1230. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1231. if (EngineDebugger::get_script_debugger()->get_depth() <= 0)
  1232. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1233. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0)
  1234. do_break = true;
  1235. }
  1236. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source))
  1237. do_break = true;
  1238. if (do_break) {
  1239. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1240. }
  1241. EngineDebugger::get_singleton()->line_poll();
  1242. }
  1243. }
  1244. DISPATCH_OPCODE;
  1245. OPCODE(OPCODE_END) {
  1246. #ifdef DEBUG_ENABLED
  1247. exit_ok = true;
  1248. #endif
  1249. OPCODE_BREAK;
  1250. }
  1251. #if 0 // Enable for debugging.
  1252. default: {
  1253. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1254. OPCODE_BREAK;
  1255. }
  1256. #endif
  1257. }
  1258. OPCODES_END
  1259. #ifdef DEBUG_ENABLED
  1260. if (exit_ok)
  1261. OPCODE_OUT;
  1262. //error
  1263. // function, file, line, error, explanation
  1264. String err_file;
  1265. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "")
  1266. err_file = p_instance->script->path;
  1267. else if (script)
  1268. err_file = script->path;
  1269. if (err_file == "")
  1270. err_file = "<built-in>";
  1271. String err_func = name;
  1272. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "")
  1273. err_func = p_instance->script->name + "." + err_func;
  1274. int err_line = line;
  1275. if (err_text == "") {
  1276. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1277. }
  1278. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1279. // debugger break did not happen
  1280. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1281. }
  1282. #endif
  1283. OPCODE_OUT;
  1284. }
  1285. OPCODES_OUT
  1286. #ifdef DEBUG_ENABLED
  1287. if (GDScriptLanguage::get_singleton()->profiling) {
  1288. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1289. profile.total_time += time_taken;
  1290. profile.self_time += time_taken - function_call_time;
  1291. profile.frame_total_time += time_taken;
  1292. profile.frame_self_time += time_taken - function_call_time;
  1293. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1294. }
  1295. // Check if this is the last time the function is resuming from yield
  1296. // Will be true if never yielded as well
  1297. // When it's the last resume it will postpone the exit from stack,
  1298. // so the debugger knows which function triggered the resume of the next function (if any)
  1299. if (!p_state || yielded) {
  1300. if (EngineDebugger::is_active())
  1301. GDScriptLanguage::get_singleton()->exit_function();
  1302. #endif
  1303. if (_stack_size) {
  1304. //free stack
  1305. for (int i = 0; i < _stack_size; i++)
  1306. stack[i].~Variant();
  1307. }
  1308. #ifdef DEBUG_ENABLED
  1309. }
  1310. #endif
  1311. return retvalue;
  1312. }
  1313. const int *GDScriptFunction::get_code() const {
  1314. return _code_ptr;
  1315. }
  1316. int GDScriptFunction::get_code_size() const {
  1317. return _code_size;
  1318. }
  1319. Variant GDScriptFunction::get_constant(int p_idx) const {
  1320. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1321. return constants[p_idx];
  1322. }
  1323. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1324. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1325. return global_names[p_idx];
  1326. }
  1327. int GDScriptFunction::get_default_argument_count() const {
  1328. return _default_arg_count;
  1329. }
  1330. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1331. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1332. return default_arguments[p_idx];
  1333. }
  1334. GDScriptDataType GDScriptFunction::get_return_type() const {
  1335. return return_type;
  1336. }
  1337. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1338. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1339. return argument_types[p_idx];
  1340. }
  1341. StringName GDScriptFunction::get_name() const {
  1342. return name;
  1343. }
  1344. int GDScriptFunction::get_max_stack_size() const {
  1345. return _stack_size;
  1346. }
  1347. struct _GDFKC {
  1348. int order;
  1349. List<int> pos;
  1350. };
  1351. struct _GDFKCS {
  1352. int order;
  1353. StringName id;
  1354. int pos;
  1355. bool operator<(const _GDFKCS &p_r) const {
  1356. return order < p_r.order;
  1357. }
  1358. };
  1359. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1360. int oc = 0;
  1361. Map<StringName, _GDFKC> sdmap;
  1362. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1363. const StackDebug &sd = E->get();
  1364. if (sd.line > p_line)
  1365. break;
  1366. if (sd.added) {
  1367. if (!sdmap.has(sd.identifier)) {
  1368. _GDFKC d;
  1369. d.order = oc++;
  1370. d.pos.push_back(sd.pos);
  1371. sdmap[sd.identifier] = d;
  1372. } else {
  1373. sdmap[sd.identifier].pos.push_back(sd.pos);
  1374. }
  1375. } else {
  1376. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1377. sdmap[sd.identifier].pos.pop_back();
  1378. if (sdmap[sd.identifier].pos.empty())
  1379. sdmap.erase(sd.identifier);
  1380. }
  1381. }
  1382. List<_GDFKCS> stackpositions;
  1383. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1384. _GDFKCS spp;
  1385. spp.id = E->key();
  1386. spp.order = E->get().order;
  1387. spp.pos = E->get().pos.back()->get();
  1388. stackpositions.push_back(spp);
  1389. }
  1390. stackpositions.sort();
  1391. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1392. Pair<StringName, int> p;
  1393. p.first = E->get().id;
  1394. p.second = E->get().pos;
  1395. r_stackvars->push_back(p);
  1396. }
  1397. }
  1398. GDScriptFunction::GDScriptFunction() :
  1399. function_list(this) {
  1400. _stack_size = 0;
  1401. _call_size = 0;
  1402. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1403. name = "<anonymous>";
  1404. #ifdef DEBUG_ENABLED
  1405. _func_cname = NULL;
  1406. {
  1407. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1408. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1409. }
  1410. profile.call_count = 0;
  1411. profile.self_time = 0;
  1412. profile.total_time = 0;
  1413. profile.frame_call_count = 0;
  1414. profile.frame_self_time = 0;
  1415. profile.frame_total_time = 0;
  1416. profile.last_frame_call_count = 0;
  1417. profile.last_frame_self_time = 0;
  1418. profile.last_frame_total_time = 0;
  1419. #endif
  1420. }
  1421. GDScriptFunction::~GDScriptFunction() {
  1422. #ifdef DEBUG_ENABLED
  1423. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1424. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1425. #endif
  1426. }
  1427. /////////////////////
  1428. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1429. Variant arg;
  1430. r_error.error = Callable::CallError::CALL_OK;
  1431. if (p_argcount == 0) {
  1432. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1433. r_error.argument = 1;
  1434. return Variant();
  1435. } else if (p_argcount == 1) {
  1436. //noooneee
  1437. } else if (p_argcount == 2) {
  1438. arg = *p_args[0];
  1439. } else {
  1440. Array extra_args;
  1441. for (int i = 0; i < p_argcount - 1; i++) {
  1442. extra_args.push_back(*p_args[i]);
  1443. }
  1444. arg = extra_args;
  1445. }
  1446. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1447. if (self.is_null()) {
  1448. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1449. r_error.argument = p_argcount - 1;
  1450. r_error.expected = Variant::OBJECT;
  1451. return Variant();
  1452. }
  1453. return resume(arg);
  1454. }
  1455. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1456. if (function == NULL)
  1457. return false;
  1458. if (p_extended_check) {
  1459. //class instance gone?
  1460. if (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id))
  1461. return false;
  1462. }
  1463. return true;
  1464. }
  1465. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1466. ERR_FAIL_COND_V(!function, Variant());
  1467. if (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id)) {
  1468. #ifdef DEBUG_ENABLED
  1469. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + String(function->get_name()) + "()' after yield, but class instance is gone. At script: " + state.script->get_path() + ":" + itos(state.line));
  1470. #else
  1471. return Variant();
  1472. #endif
  1473. }
  1474. state.result = p_arg;
  1475. Callable::CallError err;
  1476. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1477. bool completed = true;
  1478. // If the return value is a GDScriptFunctionState reference,
  1479. // then the function did yield again after resuming.
  1480. if (ret.is_ref()) {
  1481. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1482. if (gdfs && gdfs->function == function) {
  1483. completed = false;
  1484. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1485. }
  1486. }
  1487. function = NULL; //cleaned up;
  1488. state.result = Variant();
  1489. if (completed) {
  1490. if (first_state.is_valid()) {
  1491. first_state->emit_signal("completed", ret);
  1492. } else {
  1493. emit_signal("completed", ret);
  1494. }
  1495. #ifdef DEBUG_ENABLED
  1496. if (EngineDebugger::is_active())
  1497. GDScriptLanguage::get_singleton()->exit_function();
  1498. if (state.stack_size) {
  1499. //free stack
  1500. Variant *stack = (Variant *)state.stack.ptr();
  1501. for (int i = 0; i < state.stack_size; i++)
  1502. stack[i].~Variant();
  1503. }
  1504. #endif
  1505. }
  1506. return ret;
  1507. }
  1508. void GDScriptFunctionState::_bind_methods() {
  1509. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1510. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1511. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1512. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1513. }
  1514. GDScriptFunctionState::GDScriptFunctionState() {
  1515. function = NULL;
  1516. }
  1517. GDScriptFunctionState::~GDScriptFunctionState() {
  1518. if (function != NULL) {
  1519. //never called, deinitialize stack
  1520. for (int i = 0; i < state.stack_size; i++) {
  1521. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1522. v->~Variant();
  1523. }
  1524. }
  1525. }