gdscript_function.cpp 50 KB

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