gdscript_function.cpp 50 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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 *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 &p_script->_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 *o = 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 (o) {
  68. GDScript *s = o;
  69. while (s) {
  70. Map<StringName, Variant>::Element *E = s->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. s = s->_base;
  75. }
  76. o = o->_owner;
  77. }
  78. ERR_EXPLAIN("GDScriptCompiler bug...");
  79. ERR_FAIL_V(NULL);
  80. } break;
  81. case ADDR_TYPE_LOCAL_CONSTANT: {
  82. #ifdef DEBUG_ENABLED
  83. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  84. #endif
  85. return &_constants_ptr[address];
  86. } break;
  87. case ADDR_TYPE_STACK:
  88. case ADDR_TYPE_STACK_VARIABLE: {
  89. #ifdef DEBUG_ENABLED
  90. ERR_FAIL_INDEX_V(address, _stack_size, NULL);
  91. #endif
  92. return &p_stack[address];
  93. } break;
  94. case ADDR_TYPE_GLOBAL: {
  95. #ifdef DEBUG_ENABLED
  96. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL);
  97. #endif
  98. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  99. } break;
  100. #ifdef TOOLS_ENABLED
  101. case ADDR_TYPE_NAMED_GLOBAL: {
  102. #ifdef DEBUG_ENABLED
  103. ERR_FAIL_INDEX_V(address, _named_globals_count, NULL);
  104. #endif
  105. StringName id = _named_globals_ptr[address];
  106. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  107. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  108. } else {
  109. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  110. return NULL;
  111. }
  112. } break;
  113. #endif
  114. case ADDR_TYPE_NIL: {
  115. return &nil;
  116. } break;
  117. }
  118. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  119. ERR_FAIL_V(NULL);
  120. return NULL;
  121. }
  122. #ifdef DEBUG_ENABLED
  123. static String _get_var_type(const Variant *p_var) {
  124. String basestr;
  125. if (p_var->get_type() == Variant::OBJECT) {
  126. Object *bobj = *p_var;
  127. if (!bobj) {
  128. basestr = "null instance";
  129. } else {
  130. if (ObjectDB::instance_validate(bobj)) {
  131. if (bobj->get_script_instance())
  132. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  133. else
  134. basestr = bobj->get_class();
  135. } else {
  136. basestr = "previously freed instance";
  137. }
  138. }
  139. } else {
  140. basestr = Variant::get_type_name(p_var->get_type());
  141. }
  142. return basestr;
  143. }
  144. #endif // DEBUG_ENABLED
  145. String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  146. String err_text;
  147. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  148. int errorarg = p_err.argument;
  149. // Handle the Object to Object case separately as we don't have further class details.
  150. #ifdef DEBUG_ENABLED
  151. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  152. err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class.";
  153. } else
  154. #endif // DEBUG_ENABLED
  155. {
  156. err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(p_err.expected) + ".";
  157. }
  158. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  159. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  160. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  161. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  162. } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  163. err_text = "Invalid call. Nonexistent " + p_where + ".";
  164. } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  165. err_text = "Attempt to call " + p_where + " on a null instance.";
  166. } else {
  167. err_text = "Bug, call error: #" + itos(p_err.error);
  168. }
  169. return err_text;
  170. }
  171. #if defined(__GNUC__)
  172. #define OPCODES_TABLE \
  173. static const void *switch_table_ops[] = { \
  174. &&OPCODE_OPERATOR, \
  175. &&OPCODE_EXTENDS_TEST, \
  176. &&OPCODE_IS_BUILTIN, \
  177. &&OPCODE_SET, \
  178. &&OPCODE_GET, \
  179. &&OPCODE_SET_NAMED, \
  180. &&OPCODE_GET_NAMED, \
  181. &&OPCODE_SET_MEMBER, \
  182. &&OPCODE_GET_MEMBER, \
  183. &&OPCODE_ASSIGN, \
  184. &&OPCODE_ASSIGN_TRUE, \
  185. &&OPCODE_ASSIGN_FALSE, \
  186. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  187. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  188. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  189. &&OPCODE_CAST_TO_BUILTIN, \
  190. &&OPCODE_CAST_TO_NATIVE, \
  191. &&OPCODE_CAST_TO_SCRIPT, \
  192. &&OPCODE_CONSTRUCT, \
  193. &&OPCODE_CONSTRUCT_ARRAY, \
  194. &&OPCODE_CONSTRUCT_DICTIONARY, \
  195. &&OPCODE_CALL, \
  196. &&OPCODE_CALL_RETURN, \
  197. &&OPCODE_CALL_BUILT_IN, \
  198. &&OPCODE_CALL_SELF, \
  199. &&OPCODE_CALL_SELF_BASE, \
  200. &&OPCODE_YIELD, \
  201. &&OPCODE_YIELD_SIGNAL, \
  202. &&OPCODE_YIELD_RESUME, \
  203. &&OPCODE_JUMP, \
  204. &&OPCODE_JUMP_IF, \
  205. &&OPCODE_JUMP_IF_NOT, \
  206. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  207. &&OPCODE_RETURN, \
  208. &&OPCODE_ITERATE_BEGIN, \
  209. &&OPCODE_ITERATE, \
  210. &&OPCODE_ASSERT, \
  211. &&OPCODE_BREAKPOINT, \
  212. &&OPCODE_LINE, \
  213. &&OPCODE_END \
  214. };
  215. #define OPCODE(m_op) \
  216. m_op:
  217. #define OPCODE_WHILE(m_test)
  218. #define OPCODES_END \
  219. OPSEXIT:
  220. #define OPCODES_OUT \
  221. OPSOUT:
  222. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  223. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  224. #define OPCODE_BREAK goto OPSEXIT
  225. #define OPCODE_OUT goto OPSOUT
  226. #else
  227. #define OPCODES_TABLE
  228. #define OPCODE(m_op) case m_op:
  229. #define OPCODE_WHILE(m_test) while (m_test)
  230. #define OPCODES_END
  231. #define OPCODES_OUT
  232. #define DISPATCH_OPCODE continue
  233. #define OPCODE_SWITCH(m_test) switch (m_test)
  234. #define OPCODE_BREAK break
  235. #define OPCODE_OUT break
  236. #endif
  237. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  238. OPCODES_TABLE;
  239. if (!_code_ptr) {
  240. return Variant();
  241. }
  242. r_err.error = Variant::CallError::CALL_OK;
  243. Variant self;
  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 = 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 = 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 (ScriptDebugger::get_singleton())
  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. } else \
  345. _err_error_exists = false; \
  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, 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, 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. #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. Object *obj_A = *a;
  423. Object *obj_B = *b;
  424. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  425. if (scr_B) {
  426. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  427. //in other situation, this shoul return false.
  428. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  429. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  430. //bool found=false;
  431. while (cmp) {
  432. if (cmp == scr_B) {
  433. //inherits from script, all ok
  434. extends_ok = true;
  435. break;
  436. }
  437. cmp = cmp->_base;
  438. }
  439. }
  440. } else {
  441. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  442. #ifdef DEBUG_ENABLED
  443. if (!nc) {
  444. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  445. OPCODE_BREAK;
  446. }
  447. #endif
  448. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  449. }
  450. }
  451. *dst = extends_ok;
  452. ip += 4;
  453. }
  454. DISPATCH_OPCODE;
  455. OPCODE(OPCODE_IS_BUILTIN) {
  456. CHECK_SPACE(4);
  457. GET_VARIANT_PTR(value, 1);
  458. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  459. GET_VARIANT_PTR(dst, 3);
  460. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  461. *dst = value->get_type() == var_type;
  462. ip += 4;
  463. }
  464. DISPATCH_OPCODE;
  465. OPCODE(OPCODE_SET) {
  466. CHECK_SPACE(3);
  467. GET_VARIANT_PTR(dst, 1);
  468. GET_VARIANT_PTR(index, 2);
  469. GET_VARIANT_PTR(value, 3);
  470. bool valid;
  471. dst->set(*index, *value, &valid);
  472. #ifdef DEBUG_ENABLED
  473. if (!valid) {
  474. String v = index->operator String();
  475. if (v != "") {
  476. v = "'" + v + "'";
  477. } else {
  478. v = "of type '" + _get_var_type(index) + "'";
  479. }
  480. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  481. OPCODE_BREAK;
  482. }
  483. #endif
  484. ip += 4;
  485. }
  486. DISPATCH_OPCODE;
  487. OPCODE(OPCODE_GET) {
  488. CHECK_SPACE(3);
  489. GET_VARIANT_PTR(src, 1);
  490. GET_VARIANT_PTR(index, 2);
  491. GET_VARIANT_PTR(dst, 3);
  492. bool valid;
  493. #ifdef DEBUG_ENABLED
  494. //allow better error message in cases where src and dst are the same stack position
  495. Variant ret = src->get(*index, &valid);
  496. #else
  497. *dst = src->get(*index, &valid);
  498. #endif
  499. #ifdef DEBUG_ENABLED
  500. if (!valid) {
  501. String v = index->operator String();
  502. if (v != "") {
  503. v = "'" + v + "'";
  504. } else {
  505. v = "of type '" + _get_var_type(index) + "'";
  506. }
  507. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  508. OPCODE_BREAK;
  509. }
  510. *dst = ret;
  511. #endif
  512. ip += 4;
  513. }
  514. DISPATCH_OPCODE;
  515. OPCODE(OPCODE_SET_NAMED) {
  516. CHECK_SPACE(3);
  517. GET_VARIANT_PTR(dst, 1);
  518. GET_VARIANT_PTR(value, 3);
  519. int indexname = _code_ptr[ip + 2];
  520. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  521. const StringName *index = &_global_names_ptr[indexname];
  522. bool valid;
  523. dst->set_named(*index, *value, &valid);
  524. #ifdef DEBUG_ENABLED
  525. if (!valid) {
  526. String err_type;
  527. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  528. OPCODE_BREAK;
  529. }
  530. #endif
  531. ip += 4;
  532. }
  533. DISPATCH_OPCODE;
  534. OPCODE(OPCODE_GET_NAMED) {
  535. CHECK_SPACE(4);
  536. GET_VARIANT_PTR(src, 1);
  537. GET_VARIANT_PTR(dst, 3);
  538. int indexname = _code_ptr[ip + 2];
  539. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  540. const StringName *index = &_global_names_ptr[indexname];
  541. bool valid;
  542. #ifdef DEBUG_ENABLED
  543. //allow better error message in cases where src and dst are the same stack position
  544. Variant ret = src->get_named(*index, &valid);
  545. #else
  546. *dst = src->get_named(*index, &valid);
  547. #endif
  548. #ifdef DEBUG_ENABLED
  549. if (!valid) {
  550. if (src->has_method(*index)) {
  551. 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() + "\") ?";
  552. } else {
  553. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  554. }
  555. OPCODE_BREAK;
  556. }
  557. *dst = ret;
  558. #endif
  559. ip += 4;
  560. }
  561. DISPATCH_OPCODE;
  562. OPCODE(OPCODE_SET_MEMBER) {
  563. CHECK_SPACE(3);
  564. int indexname = _code_ptr[ip + 1];
  565. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  566. const StringName *index = &_global_names_ptr[indexname];
  567. GET_VARIANT_PTR(src, 2);
  568. bool valid;
  569. #ifndef DEBUG_ENABLED
  570. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  571. #else
  572. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  573. if (!ok) {
  574. err_text = "Internal error setting property: " + String(*index);
  575. OPCODE_BREAK;
  576. } else if (!valid) {
  577. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  578. OPCODE_BREAK;
  579. }
  580. #endif
  581. ip += 3;
  582. }
  583. DISPATCH_OPCODE;
  584. OPCODE(OPCODE_GET_MEMBER) {
  585. CHECK_SPACE(3);
  586. int indexname = _code_ptr[ip + 1];
  587. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  588. const StringName *index = &_global_names_ptr[indexname];
  589. GET_VARIANT_PTR(dst, 2);
  590. #ifndef DEBUG_ENABLED
  591. ClassDB::get_property(p_instance->owner, *index, *dst);
  592. #else
  593. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  594. if (!ok) {
  595. err_text = "Internal error getting property: " + String(*index);
  596. OPCODE_BREAK;
  597. }
  598. #endif
  599. ip += 3;
  600. }
  601. DISPATCH_OPCODE;
  602. OPCODE(OPCODE_ASSIGN) {
  603. CHECK_SPACE(3);
  604. GET_VARIANT_PTR(dst, 1);
  605. GET_VARIANT_PTR(src, 2);
  606. *dst = *src;
  607. ip += 3;
  608. }
  609. DISPATCH_OPCODE;
  610. OPCODE(OPCODE_ASSIGN_TRUE) {
  611. CHECK_SPACE(2);
  612. GET_VARIANT_PTR(dst, 1);
  613. *dst = true;
  614. ip += 2;
  615. }
  616. DISPATCH_OPCODE;
  617. OPCODE(OPCODE_ASSIGN_FALSE) {
  618. CHECK_SPACE(2);
  619. GET_VARIANT_PTR(dst, 1);
  620. *dst = false;
  621. ip += 2;
  622. }
  623. DISPATCH_OPCODE;
  624. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  625. CHECK_SPACE(4);
  626. GET_VARIANT_PTR(dst, 2);
  627. GET_VARIANT_PTR(src, 3);
  628. #ifdef DEBUG_ENABLED
  629. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  630. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  631. if (src->get_type() != var_type) {
  632. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  633. Variant::CallError ce;
  634. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  635. } else {
  636. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  637. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  638. OPCODE_BREAK;
  639. }
  640. } else {
  641. #endif // DEBUG_ENABLED
  642. *dst = *src;
  643. #ifdef DEBUG_ENABLED
  644. }
  645. #endif // DEBUG_ENABLED
  646. ip += 4;
  647. }
  648. DISPATCH_OPCODE;
  649. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  650. CHECK_SPACE(4);
  651. GET_VARIANT_PTR(dst, 2);
  652. GET_VARIANT_PTR(src, 3);
  653. #ifdef DEBUG_ENABLED
  654. GET_VARIANT_PTR(type, 1);
  655. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  656. GD_ERR_BREAK(!nc);
  657. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  658. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  659. "' to a variable of type '" + nc->get_name() + "'.";
  660. OPCODE_BREAK;
  661. }
  662. Object *src_obj = src->operator Object *();
  663. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  664. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  665. "' to a variable of type '" + nc->get_name() + "'.";
  666. OPCODE_BREAK;
  667. }
  668. #endif // DEBUG_ENABLED
  669. *dst = *src;
  670. ip += 4;
  671. }
  672. DISPATCH_OPCODE;
  673. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  674. CHECK_SPACE(4);
  675. GET_VARIANT_PTR(dst, 2);
  676. GET_VARIANT_PTR(src, 3);
  677. #ifdef DEBUG_ENABLED
  678. GET_VARIANT_PTR(type, 1);
  679. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  680. GD_ERR_BREAK(!base_type);
  681. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  682. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  683. OPCODE_BREAK;
  684. }
  685. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  686. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  687. if (!scr_inst) {
  688. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  689. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  690. OPCODE_BREAK;
  691. }
  692. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  693. bool valid = false;
  694. while (src_type) {
  695. if (src_type == base_type) {
  696. valid = true;
  697. break;
  698. }
  699. src_type = src_type->get_base_script().ptr();
  700. }
  701. if (!valid) {
  702. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  703. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  704. OPCODE_BREAK;
  705. }
  706. }
  707. #endif // DEBUG_ENABLED
  708. *dst = *src;
  709. ip += 4;
  710. }
  711. DISPATCH_OPCODE;
  712. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  713. CHECK_SPACE(4);
  714. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  715. GET_VARIANT_PTR(src, 2);
  716. GET_VARIANT_PTR(dst, 3);
  717. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  718. Variant::CallError err;
  719. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  720. #ifdef DEBUG_ENABLED
  721. if (err.error != Variant::CallError::CALL_OK) {
  722. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  723. OPCODE_BREAK;
  724. }
  725. #endif
  726. ip += 4;
  727. }
  728. DISPATCH_OPCODE;
  729. OPCODE(OPCODE_CAST_TO_NATIVE) {
  730. CHECK_SPACE(4);
  731. GET_VARIANT_PTR(to_type, 1);
  732. GET_VARIANT_PTR(src, 2);
  733. GET_VARIANT_PTR(dst, 3);
  734. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  735. GD_ERR_BREAK(!nc);
  736. #ifdef DEBUG_ENABLED
  737. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  738. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  739. OPCODE_BREAK;
  740. }
  741. #endif
  742. Object *src_obj = src->operator Object *();
  743. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  744. *dst = Variant(); // invalid cast, assign NULL
  745. } else {
  746. *dst = *src;
  747. }
  748. ip += 4;
  749. }
  750. DISPATCH_OPCODE;
  751. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  752. CHECK_SPACE(4);
  753. GET_VARIANT_PTR(to_type, 1);
  754. GET_VARIANT_PTR(src, 2);
  755. GET_VARIANT_PTR(dst, 3);
  756. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  757. GD_ERR_BREAK(!base_type);
  758. #ifdef DEBUG_ENABLED
  759. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  760. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  761. OPCODE_BREAK;
  762. }
  763. #endif
  764. bool valid = false;
  765. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  766. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  767. if (scr_inst) {
  768. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  769. while (src_type) {
  770. if (src_type == base_type) {
  771. valid = true;
  772. break;
  773. }
  774. src_type = src_type->get_base_script().ptr();
  775. }
  776. }
  777. }
  778. if (valid) {
  779. *dst = *src; // Valid cast, copy the source object
  780. } else {
  781. *dst = Variant(); // invalid cast, assign NULL
  782. }
  783. ip += 4;
  784. }
  785. DISPATCH_OPCODE;
  786. OPCODE(OPCODE_CONSTRUCT) {
  787. CHECK_SPACE(2);
  788. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  789. int argc = _code_ptr[ip + 2];
  790. CHECK_SPACE(argc + 2);
  791. Variant **argptrs = call_args;
  792. for (int i = 0; i < argc; i++) {
  793. GET_VARIANT_PTR(v, 3 + i);
  794. argptrs[i] = v;
  795. }
  796. GET_VARIANT_PTR(dst, 3 + argc);
  797. Variant::CallError err;
  798. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  799. #ifdef DEBUG_ENABLED
  800. if (err.error != Variant::CallError::CALL_OK) {
  801. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  802. OPCODE_BREAK;
  803. }
  804. #endif
  805. ip += 4 + argc;
  806. //construct a basic type
  807. }
  808. DISPATCH_OPCODE;
  809. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  810. CHECK_SPACE(1);
  811. int argc = _code_ptr[ip + 1];
  812. Array array; //arrays are always shared
  813. array.resize(argc);
  814. CHECK_SPACE(argc + 2);
  815. for (int i = 0; i < argc; i++) {
  816. GET_VARIANT_PTR(v, 2 + i);
  817. array[i] = *v;
  818. }
  819. GET_VARIANT_PTR(dst, 2 + argc);
  820. *dst = array;
  821. ip += 3 + argc;
  822. }
  823. DISPATCH_OPCODE;
  824. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  825. CHECK_SPACE(1);
  826. int argc = _code_ptr[ip + 1];
  827. Dictionary dict; //arrays are always shared
  828. CHECK_SPACE(argc * 2 + 2);
  829. for (int i = 0; i < argc; i++) {
  830. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  831. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  832. dict[*k] = *v;
  833. }
  834. GET_VARIANT_PTR(dst, 2 + argc * 2);
  835. *dst = dict;
  836. ip += 3 + argc * 2;
  837. }
  838. DISPATCH_OPCODE;
  839. OPCODE(OPCODE_CALL_RETURN)
  840. OPCODE(OPCODE_CALL) {
  841. CHECK_SPACE(4);
  842. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  843. int argc = _code_ptr[ip + 1];
  844. GET_VARIANT_PTR(base, 2);
  845. int nameg = _code_ptr[ip + 3];
  846. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  847. const StringName *methodname = &_global_names_ptr[nameg];
  848. GD_ERR_BREAK(argc < 0);
  849. ip += 4;
  850. CHECK_SPACE(argc + 1);
  851. Variant **argptrs = call_args;
  852. for (int i = 0; i < argc; i++) {
  853. GET_VARIANT_PTR(v, i);
  854. argptrs[i] = v;
  855. }
  856. #ifdef DEBUG_ENABLED
  857. uint64_t call_time = 0;
  858. if (GDScriptLanguage::get_singleton()->profiling) {
  859. call_time = OS::get_singleton()->get_ticks_usec();
  860. }
  861. #endif
  862. Variant::CallError err;
  863. if (call_ret) {
  864. GET_VARIANT_PTR(ret, argc);
  865. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  866. } else {
  867. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  868. }
  869. #ifdef DEBUG_ENABLED
  870. if (GDScriptLanguage::get_singleton()->profiling) {
  871. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  872. }
  873. if (err.error != Variant::CallError::CALL_OK) {
  874. String methodstr = *methodname;
  875. String basestr = _get_var_type(base);
  876. if (methodstr == "call") {
  877. if (argc >= 1) {
  878. methodstr = String(*argptrs[0]) + " (via call)";
  879. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  880. err.argument += 1;
  881. }
  882. }
  883. } else if (methodstr == "free") {
  884. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  885. if (base->is_ref()) {
  886. err_text = "Attempted to free a reference.";
  887. OPCODE_BREAK;
  888. } else if (base->get_type() == Variant::OBJECT) {
  889. err_text = "Attempted to free a locked object (calling or emitting).";
  890. OPCODE_BREAK;
  891. }
  892. }
  893. }
  894. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  895. OPCODE_BREAK;
  896. }
  897. #endif
  898. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  899. ip += argc + 1;
  900. }
  901. DISPATCH_OPCODE;
  902. OPCODE(OPCODE_CALL_BUILT_IN) {
  903. CHECK_SPACE(4);
  904. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  905. int argc = _code_ptr[ip + 2];
  906. GD_ERR_BREAK(argc < 0);
  907. ip += 3;
  908. CHECK_SPACE(argc + 1);
  909. Variant **argptrs = call_args;
  910. for (int i = 0; i < argc; i++) {
  911. GET_VARIANT_PTR(v, i);
  912. argptrs[i] = v;
  913. }
  914. GET_VARIANT_PTR(dst, argc);
  915. Variant::CallError err;
  916. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  917. #ifdef DEBUG_ENABLED
  918. if (err.error != Variant::CallError::CALL_OK) {
  919. String methodstr = GDScriptFunctions::get_func_name(func);
  920. if (dst->get_type() == Variant::STRING) {
  921. //call provided error string
  922. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  923. } else {
  924. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  925. }
  926. OPCODE_BREAK;
  927. }
  928. #endif
  929. ip += argc + 1;
  930. }
  931. DISPATCH_OPCODE;
  932. OPCODE(OPCODE_CALL_SELF) {
  933. OPCODE_BREAK;
  934. }
  935. OPCODE(OPCODE_CALL_SELF_BASE) {
  936. CHECK_SPACE(2);
  937. int self_fun = _code_ptr[ip + 1];
  938. #ifdef DEBUG_ENABLED
  939. if (self_fun < 0 || self_fun >= _global_names_count) {
  940. err_text = "compiler bug, function name not found";
  941. OPCODE_BREAK;
  942. }
  943. #endif
  944. const StringName *methodname = &_global_names_ptr[self_fun];
  945. int argc = _code_ptr[ip + 2];
  946. CHECK_SPACE(2 + argc + 1);
  947. Variant **argptrs = call_args;
  948. for (int i = 0; i < argc; i++) {
  949. GET_VARIANT_PTR(v, i + 3);
  950. argptrs[i] = v;
  951. }
  952. GET_VARIANT_PTR(dst, argc + 3);
  953. const GDScript *gds = _script;
  954. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  955. while (gds->base.ptr()) {
  956. gds = gds->base.ptr();
  957. E = gds->member_functions.find(*methodname);
  958. if (E)
  959. break;
  960. }
  961. Variant::CallError err;
  962. if (E) {
  963. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  964. } else if (gds->native.ptr()) {
  965. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  966. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  967. if (!mb) {
  968. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  969. } else {
  970. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  971. }
  972. } else {
  973. err.error = Variant::CallError::CALL_OK;
  974. }
  975. } else {
  976. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  977. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  978. } else {
  979. err.error = Variant::CallError::CALL_OK;
  980. }
  981. }
  982. if (err.error != Variant::CallError::CALL_OK) {
  983. String methodstr = *methodname;
  984. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  985. OPCODE_BREAK;
  986. }
  987. ip += 4 + argc;
  988. }
  989. DISPATCH_OPCODE;
  990. OPCODE(OPCODE_YIELD)
  991. OPCODE(OPCODE_YIELD_SIGNAL) {
  992. int ipofs = 1;
  993. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  994. CHECK_SPACE(4);
  995. ipofs += 2;
  996. } else {
  997. CHECK_SPACE(2);
  998. }
  999. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1000. gdfs->function = this;
  1001. gdfs->state.stack.resize(alloca_size);
  1002. //copy variant stack
  1003. for (int i = 0; i < _stack_size; i++) {
  1004. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1005. }
  1006. gdfs->state.stack_size = _stack_size;
  1007. gdfs->state.self = self;
  1008. gdfs->state.alloca_size = alloca_size;
  1009. gdfs->state.script = Ref<GDScript>(_script);
  1010. gdfs->state.ip = ip + ipofs;
  1011. gdfs->state.line = line;
  1012. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
  1013. //gdfs->state.result_pos=ip+ipofs-1;
  1014. gdfs->state.defarg = defarg;
  1015. gdfs->state.instance = p_instance;
  1016. gdfs->function = this;
  1017. retvalue = gdfs;
  1018. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1019. //do the oneshot connect
  1020. GET_VARIANT_PTR(argobj, 1);
  1021. GET_VARIANT_PTR(argname, 2);
  1022. #ifdef DEBUG_ENABLED
  1023. if (argobj->get_type() != Variant::OBJECT) {
  1024. err_text = "First argument of yield() not of type object.";
  1025. OPCODE_BREAK;
  1026. }
  1027. if (argname->get_type() != Variant::STRING) {
  1028. err_text = "Second argument of yield() not a string (for signal name).";
  1029. OPCODE_BREAK;
  1030. }
  1031. #endif
  1032. Object *obj = argobj->operator Object *();
  1033. String signal = argname->operator String();
  1034. #ifdef DEBUG_ENABLED
  1035. if (!obj) {
  1036. err_text = "First argument of yield() is null.";
  1037. OPCODE_BREAK;
  1038. }
  1039. if (ScriptDebugger::get_singleton()) {
  1040. if (!ObjectDB::instance_validate(obj)) {
  1041. err_text = "First argument of yield() is a previously freed instance.";
  1042. OPCODE_BREAK;
  1043. }
  1044. }
  1045. if (signal.length() == 0) {
  1046. err_text = "Second argument of yield() is an empty string (for signal name).";
  1047. OPCODE_BREAK;
  1048. }
  1049. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1050. if (err != OK) {
  1051. err_text = "Error connecting to signal: " + signal + " during yield().";
  1052. OPCODE_BREAK;
  1053. }
  1054. #else
  1055. obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1056. #endif
  1057. }
  1058. #ifdef DEBUG_ENABLED
  1059. exit_ok = true;
  1060. #endif
  1061. OPCODE_BREAK;
  1062. }
  1063. OPCODE(OPCODE_YIELD_RESUME) {
  1064. CHECK_SPACE(2);
  1065. #ifdef DEBUG_ENABLED
  1066. if (!p_state) {
  1067. err_text = ("Invalid Resume (bug?)");
  1068. OPCODE_BREAK;
  1069. }
  1070. #endif
  1071. GET_VARIANT_PTR(result, 1);
  1072. *result = p_state->result;
  1073. ip += 2;
  1074. }
  1075. DISPATCH_OPCODE;
  1076. OPCODE(OPCODE_JUMP) {
  1077. CHECK_SPACE(2);
  1078. int to = _code_ptr[ip + 1];
  1079. GD_ERR_BREAK(to < 0 || to > _code_size);
  1080. ip = to;
  1081. }
  1082. DISPATCH_OPCODE;
  1083. OPCODE(OPCODE_JUMP_IF) {
  1084. CHECK_SPACE(3);
  1085. GET_VARIANT_PTR(test, 1);
  1086. bool result = test->booleanize();
  1087. if (result) {
  1088. int to = _code_ptr[ip + 2];
  1089. GD_ERR_BREAK(to < 0 || to > _code_size);
  1090. ip = to;
  1091. } else {
  1092. ip += 3;
  1093. }
  1094. }
  1095. DISPATCH_OPCODE;
  1096. OPCODE(OPCODE_JUMP_IF_NOT) {
  1097. CHECK_SPACE(3);
  1098. GET_VARIANT_PTR(test, 1);
  1099. bool result = test->booleanize();
  1100. if (!result) {
  1101. int to = _code_ptr[ip + 2];
  1102. GD_ERR_BREAK(to < 0 || to > _code_size);
  1103. ip = to;
  1104. } else {
  1105. ip += 3;
  1106. }
  1107. }
  1108. DISPATCH_OPCODE;
  1109. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1110. CHECK_SPACE(2);
  1111. ip = _default_arg_ptr[defarg];
  1112. }
  1113. DISPATCH_OPCODE;
  1114. OPCODE(OPCODE_RETURN) {
  1115. CHECK_SPACE(2);
  1116. GET_VARIANT_PTR(r, 1);
  1117. retvalue = *r;
  1118. #ifdef DEBUG_ENABLED
  1119. exit_ok = true;
  1120. #endif
  1121. OPCODE_BREAK;
  1122. }
  1123. OPCODE(OPCODE_ITERATE_BEGIN) {
  1124. CHECK_SPACE(8); //space for this a regular iterate
  1125. GET_VARIANT_PTR(counter, 1);
  1126. GET_VARIANT_PTR(container, 2);
  1127. bool valid;
  1128. if (!container->iter_init(*counter, valid)) {
  1129. #ifdef DEBUG_ENABLED
  1130. if (!valid) {
  1131. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1132. OPCODE_BREAK;
  1133. }
  1134. #endif
  1135. int jumpto = _code_ptr[ip + 3];
  1136. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1137. ip = jumpto;
  1138. } else {
  1139. GET_VARIANT_PTR(iterator, 4);
  1140. *iterator = container->iter_get(*counter, valid);
  1141. #ifdef DEBUG_ENABLED
  1142. if (!valid) {
  1143. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1144. OPCODE_BREAK;
  1145. }
  1146. #endif
  1147. ip += 5; //skip regular iterate which is always next
  1148. }
  1149. }
  1150. DISPATCH_OPCODE;
  1151. OPCODE(OPCODE_ITERATE) {
  1152. CHECK_SPACE(4);
  1153. GET_VARIANT_PTR(counter, 1);
  1154. GET_VARIANT_PTR(container, 2);
  1155. bool valid;
  1156. if (!container->iter_next(*counter, valid)) {
  1157. #ifdef DEBUG_ENABLED
  1158. if (!valid) {
  1159. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1160. OPCODE_BREAK;
  1161. }
  1162. #endif
  1163. int jumpto = _code_ptr[ip + 3];
  1164. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1165. ip = jumpto;
  1166. } else {
  1167. GET_VARIANT_PTR(iterator, 4);
  1168. *iterator = container->iter_get(*counter, valid);
  1169. #ifdef DEBUG_ENABLED
  1170. if (!valid) {
  1171. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1172. OPCODE_BREAK;
  1173. }
  1174. #endif
  1175. ip += 5; //loop again
  1176. }
  1177. }
  1178. DISPATCH_OPCODE;
  1179. OPCODE(OPCODE_ASSERT) {
  1180. CHECK_SPACE(2);
  1181. #ifdef DEBUG_ENABLED
  1182. GET_VARIANT_PTR(test, 1);
  1183. bool result = test->booleanize();
  1184. if (!result) {
  1185. err_text = "Assertion failed.";
  1186. OPCODE_BREAK;
  1187. }
  1188. #endif
  1189. ip += 2;
  1190. }
  1191. DISPATCH_OPCODE;
  1192. OPCODE(OPCODE_BREAKPOINT) {
  1193. #ifdef DEBUG_ENABLED
  1194. if (ScriptDebugger::get_singleton()) {
  1195. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1196. }
  1197. #endif
  1198. ip += 1;
  1199. }
  1200. DISPATCH_OPCODE;
  1201. OPCODE(OPCODE_LINE) {
  1202. CHECK_SPACE(2);
  1203. line = _code_ptr[ip + 1];
  1204. ip += 2;
  1205. if (ScriptDebugger::get_singleton()) {
  1206. // line
  1207. bool do_break = false;
  1208. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1209. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1210. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1211. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1212. do_break = true;
  1213. }
  1214. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  1215. do_break = true;
  1216. if (do_break) {
  1217. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1218. }
  1219. ScriptDebugger::get_singleton()->line_poll();
  1220. }
  1221. }
  1222. DISPATCH_OPCODE;
  1223. OPCODE(OPCODE_END) {
  1224. #ifdef DEBUG_ENABLED
  1225. exit_ok = true;
  1226. #endif
  1227. OPCODE_BREAK;
  1228. }
  1229. // Enable for debugging
  1230. #if 0
  1231. default: {
  1232. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1233. OPCODE_BREAK;
  1234. }
  1235. #endif
  1236. }
  1237. OPCODES_END
  1238. #ifdef DEBUG_ENABLED
  1239. if (exit_ok)
  1240. OPCODE_OUT;
  1241. //error
  1242. // function, file, line, error, explanation
  1243. String err_file;
  1244. if (p_instance)
  1245. err_file = p_instance->script->path;
  1246. else if (script)
  1247. err_file = script->path;
  1248. if (err_file == "")
  1249. err_file = "<built-in>";
  1250. String err_func = name;
  1251. if (p_instance && p_instance->script->name != "")
  1252. err_func = p_instance->script->name + "." + err_func;
  1253. int err_line = line;
  1254. if (err_text == "") {
  1255. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1256. }
  1257. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1258. // debugger break did not happen
  1259. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1260. }
  1261. #endif
  1262. OPCODE_OUT;
  1263. }
  1264. OPCODES_OUT
  1265. #ifdef DEBUG_ENABLED
  1266. if (GDScriptLanguage::get_singleton()->profiling) {
  1267. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1268. profile.total_time += time_taken;
  1269. profile.self_time += time_taken - function_call_time;
  1270. profile.frame_total_time += time_taken;
  1271. profile.frame_self_time += time_taken - function_call_time;
  1272. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1273. }
  1274. if (ScriptDebugger::get_singleton())
  1275. GDScriptLanguage::get_singleton()->exit_function();
  1276. #endif
  1277. if (_stack_size) {
  1278. //free stack
  1279. for (int i = 0; i < _stack_size; i++)
  1280. stack[i].~Variant();
  1281. }
  1282. return retvalue;
  1283. }
  1284. const int *GDScriptFunction::get_code() const {
  1285. return _code_ptr;
  1286. }
  1287. int GDScriptFunction::get_code_size() const {
  1288. return _code_size;
  1289. }
  1290. Variant GDScriptFunction::get_constant(int p_idx) const {
  1291. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1292. return constants[p_idx];
  1293. }
  1294. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1295. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1296. return global_names[p_idx];
  1297. }
  1298. int GDScriptFunction::get_default_argument_count() const {
  1299. return _default_arg_count;
  1300. }
  1301. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1302. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1303. return default_arguments[p_idx];
  1304. }
  1305. GDScriptDataType GDScriptFunction::get_return_type() const {
  1306. return return_type;
  1307. }
  1308. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1309. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1310. return argument_types[p_idx];
  1311. }
  1312. StringName GDScriptFunction::get_name() const {
  1313. return name;
  1314. }
  1315. int GDScriptFunction::get_max_stack_size() const {
  1316. return _stack_size;
  1317. }
  1318. struct _GDFKC {
  1319. int order;
  1320. List<int> pos;
  1321. };
  1322. struct _GDFKCS {
  1323. int order;
  1324. StringName id;
  1325. int pos;
  1326. bool operator<(const _GDFKCS &p_r) const {
  1327. return order < p_r.order;
  1328. }
  1329. };
  1330. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1331. int oc = 0;
  1332. Map<StringName, _GDFKC> sdmap;
  1333. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1334. const StackDebug &sd = E->get();
  1335. if (sd.line > p_line)
  1336. break;
  1337. if (sd.added) {
  1338. if (!sdmap.has(sd.identifier)) {
  1339. _GDFKC d;
  1340. d.order = oc++;
  1341. d.pos.push_back(sd.pos);
  1342. sdmap[sd.identifier] = d;
  1343. } else {
  1344. sdmap[sd.identifier].pos.push_back(sd.pos);
  1345. }
  1346. } else {
  1347. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1348. sdmap[sd.identifier].pos.pop_back();
  1349. if (sdmap[sd.identifier].pos.empty())
  1350. sdmap.erase(sd.identifier);
  1351. }
  1352. }
  1353. List<_GDFKCS> stackpositions;
  1354. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1355. _GDFKCS spp;
  1356. spp.id = E->key();
  1357. spp.order = E->get().order;
  1358. spp.pos = E->get().pos.back()->get();
  1359. stackpositions.push_back(spp);
  1360. }
  1361. stackpositions.sort();
  1362. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1363. Pair<StringName, int> p;
  1364. p.first = E->get().id;
  1365. p.second = E->get().pos;
  1366. r_stackvars->push_back(p);
  1367. }
  1368. }
  1369. GDScriptFunction::GDScriptFunction() :
  1370. function_list(this) {
  1371. _stack_size = 0;
  1372. _call_size = 0;
  1373. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1374. name = "<anonymous>";
  1375. #ifdef DEBUG_ENABLED
  1376. _func_cname = NULL;
  1377. if (GDScriptLanguage::get_singleton()->lock) {
  1378. GDScriptLanguage::get_singleton()->lock->lock();
  1379. }
  1380. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1381. if (GDScriptLanguage::get_singleton()->lock) {
  1382. GDScriptLanguage::get_singleton()->lock->unlock();
  1383. }
  1384. profile.call_count = 0;
  1385. profile.self_time = 0;
  1386. profile.total_time = 0;
  1387. profile.frame_call_count = 0;
  1388. profile.frame_self_time = 0;
  1389. profile.frame_total_time = 0;
  1390. profile.last_frame_call_count = 0;
  1391. profile.last_frame_self_time = 0;
  1392. profile.last_frame_total_time = 0;
  1393. #endif
  1394. }
  1395. GDScriptFunction::~GDScriptFunction() {
  1396. #ifdef DEBUG_ENABLED
  1397. if (GDScriptLanguage::get_singleton()->lock) {
  1398. GDScriptLanguage::get_singleton()->lock->lock();
  1399. }
  1400. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1401. if (GDScriptLanguage::get_singleton()->lock) {
  1402. GDScriptLanguage::get_singleton()->lock->unlock();
  1403. }
  1404. #endif
  1405. }
  1406. /////////////////////
  1407. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1408. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1409. #ifdef DEBUG_ENABLED
  1410. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1411. ERR_FAIL_V(Variant());
  1412. #else
  1413. return Variant();
  1414. #endif
  1415. }
  1416. Variant arg;
  1417. r_error.error = Variant::CallError::CALL_OK;
  1418. ERR_FAIL_COND_V(!function, Variant());
  1419. if (p_argcount == 0) {
  1420. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1421. r_error.argument = 1;
  1422. return Variant();
  1423. } else if (p_argcount == 1) {
  1424. //noooneee
  1425. } else if (p_argcount == 2) {
  1426. arg = *p_args[0];
  1427. } else {
  1428. Array extra_args;
  1429. for (int i = 0; i < p_argcount - 1; i++) {
  1430. extra_args.push_back(*p_args[i]);
  1431. }
  1432. arg = extra_args;
  1433. }
  1434. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1435. if (self.is_null()) {
  1436. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1437. r_error.argument = p_argcount - 1;
  1438. r_error.expected = Variant::OBJECT;
  1439. return Variant();
  1440. }
  1441. state.result = arg;
  1442. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1443. bool completed = true;
  1444. // If the return value is a GDScriptFunctionState reference,
  1445. // then the function did yield again after resuming.
  1446. if (ret.is_ref()) {
  1447. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1448. if (gdfs && gdfs->function == function) {
  1449. completed = false;
  1450. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1451. }
  1452. }
  1453. function = NULL; //cleaned up;
  1454. state.result = Variant();
  1455. if (completed) {
  1456. if (first_state.is_valid()) {
  1457. first_state->emit_signal("completed", ret);
  1458. } else {
  1459. emit_signal("completed", ret);
  1460. }
  1461. }
  1462. return ret;
  1463. }
  1464. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1465. if (function == NULL)
  1466. return false;
  1467. if (p_extended_check) {
  1468. //class instance gone?
  1469. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1470. return false;
  1471. }
  1472. return true;
  1473. }
  1474. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1475. ERR_FAIL_COND_V(!function, Variant());
  1476. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1477. #ifdef DEBUG_ENABLED
  1478. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1479. ERR_FAIL_V(Variant());
  1480. #else
  1481. return Variant();
  1482. #endif
  1483. }
  1484. state.result = p_arg;
  1485. Variant::CallError err;
  1486. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1487. bool completed = true;
  1488. // If the return value is a GDScriptFunctionState reference,
  1489. // then the function did yield again after resuming.
  1490. if (ret.is_ref()) {
  1491. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1492. if (gdfs && gdfs->function == function) {
  1493. completed = false;
  1494. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1495. }
  1496. }
  1497. function = NULL; //cleaned up;
  1498. state.result = Variant();
  1499. if (completed) {
  1500. if (first_state.is_valid()) {
  1501. first_state->emit_signal("completed", ret);
  1502. } else {
  1503. emit_signal("completed", ret);
  1504. }
  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. }