gd_function.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. #include "gd_function.h"
  2. #include "gd_functions.h"
  3. #include "gd_script.h"
  4. #include "os/os.h"
  5. Variant *GDFunction::_get_variant(int p_address, GDInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const {
  6. int address = p_address & ADDR_MASK;
  7. //sequential table (jump table generated by compiler)
  8. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  9. case ADDR_TYPE_SELF: {
  10. if (!p_instance) {
  11. r_error = "Cannot access self without instance.";
  12. return NULL;
  13. }
  14. return &self;
  15. } break;
  16. case ADDR_TYPE_CLASS: {
  17. return &p_script->_static_ref;
  18. } break;
  19. case ADDR_TYPE_MEMBER: {
  20. //member indexing is O(1)
  21. if (!p_instance) {
  22. r_error = "Cannot access member without instance.";
  23. return NULL;
  24. }
  25. return &p_instance->members[address];
  26. } break;
  27. case ADDR_TYPE_CLASS_CONSTANT: {
  28. //todo change to index!
  29. GDScript *o = p_script;
  30. ERR_FAIL_INDEX_V(address, _global_names_count, NULL);
  31. const StringName *sn = &_global_names_ptr[address];
  32. while (o) {
  33. GDScript *s = o;
  34. while (s) {
  35. Map<StringName, Variant>::Element *E = s->constants.find(*sn);
  36. if (E) {
  37. return &E->get();
  38. }
  39. s = s->_base;
  40. }
  41. o = o->_owner;
  42. }
  43. ERR_EXPLAIN("GDCompiler bug..");
  44. ERR_FAIL_V(NULL);
  45. } break;
  46. case ADDR_TYPE_LOCAL_CONSTANT: {
  47. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  48. return &_constants_ptr[address];
  49. } break;
  50. case ADDR_TYPE_STACK:
  51. case ADDR_TYPE_STACK_VARIABLE: {
  52. ERR_FAIL_INDEX_V(address, _stack_size, NULL);
  53. return &p_stack[address];
  54. } break;
  55. case ADDR_TYPE_GLOBAL: {
  56. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL);
  57. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  58. } break;
  59. case ADDR_TYPE_NIL: {
  60. return &nil;
  61. } break;
  62. }
  63. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  64. ERR_FAIL_V(NULL);
  65. return NULL;
  66. }
  67. String GDFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  68. String err_text;
  69. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  70. int errorarg = p_err.argument;
  71. 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) + ".";
  72. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  73. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  74. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  75. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  76. } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  77. err_text = "Invalid call. Nonexistent " + p_where + ".";
  78. } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  79. err_text = "Attempt to call " + p_where + " on a null instance.";
  80. } else {
  81. err_text = "Bug, call error: #" + itos(p_err.error);
  82. }
  83. return err_text;
  84. }
  85. static String _get_var_type(const Variant *p_type) {
  86. String basestr;
  87. if (p_type->get_type() == Variant::OBJECT) {
  88. Object *bobj = *p_type;
  89. if (!bobj) {
  90. basestr = "null instance";
  91. } else {
  92. #ifdef DEBUG_ENABLED
  93. if (ObjectDB::instance_validate(bobj)) {
  94. if (bobj->get_script_instance())
  95. basestr = bobj->get_type() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  96. else
  97. basestr = bobj->get_type();
  98. } else {
  99. basestr = "previously freed instance";
  100. }
  101. #else
  102. basestr = "Object";
  103. #endif
  104. }
  105. } else {
  106. basestr = Variant::get_type_name(p_type->get_type());
  107. }
  108. return basestr;
  109. }
  110. Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  111. if (!_code_ptr) {
  112. return Variant();
  113. }
  114. r_err.error = Variant::CallError::CALL_OK;
  115. Variant self;
  116. Variant retvalue;
  117. Variant *stack = NULL;
  118. Variant **call_args;
  119. int defarg = 0;
  120. #ifdef DEBUG_ENABLED
  121. //GDScriptLanguage::get_singleton()->calls++;
  122. #endif
  123. uint32_t alloca_size = 0;
  124. GDScript *_class;
  125. int ip = 0;
  126. int line = _initial_line;
  127. if (p_state) {
  128. //use existing (supplied) state (yielded)
  129. stack = (Variant *)p_state->stack.ptr();
  130. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  131. line = p_state->line;
  132. ip = p_state->ip;
  133. alloca_size = p_state->stack.size();
  134. _class = p_state->_class;
  135. p_instance = p_state->instance;
  136. defarg = p_state->defarg;
  137. self = p_state->self;
  138. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  139. } else {
  140. if (p_argcount != _argument_count) {
  141. if (p_argcount > _argument_count) {
  142. r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  143. r_err.argument = _argument_count;
  144. return Variant();
  145. } else if (p_argcount < _argument_count - _default_arg_count) {
  146. r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  147. r_err.argument = _argument_count - _default_arg_count;
  148. return Variant();
  149. } else {
  150. defarg = _argument_count - p_argcount;
  151. }
  152. }
  153. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  154. if (alloca_size) {
  155. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  156. if (_stack_size) {
  157. stack = (Variant *)aptr;
  158. for (int i = 0; i < p_argcount; i++)
  159. memnew_placement(&stack[i], Variant(*p_args[i]));
  160. for (int i = p_argcount; i < _stack_size; i++)
  161. memnew_placement(&stack[i], Variant);
  162. } else {
  163. stack = NULL;
  164. }
  165. if (_call_size) {
  166. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  167. } else {
  168. call_args = NULL;
  169. }
  170. } else {
  171. stack = NULL;
  172. call_args = NULL;
  173. }
  174. if (p_instance) {
  175. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  176. self = REF(static_cast<Reference *>(p_instance->owner));
  177. } else {
  178. self = p_instance->owner;
  179. }
  180. _class = p_instance->script.ptr();
  181. } else {
  182. _class = _script;
  183. }
  184. }
  185. String err_text;
  186. #ifdef DEBUG_ENABLED
  187. if (ScriptDebugger::get_singleton())
  188. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  189. #define CHECK_SPACE(m_space) \
  190. ERR_BREAK((ip + m_space) > _code_size)
  191. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  192. Variant *m_v; \
  193. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text); \
  194. if (!m_v) \
  195. break;
  196. #else
  197. #define CHECK_SPACE(m_space)
  198. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  199. Variant *m_v; \
  200. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text);
  201. #endif
  202. #ifdef DEBUG_ENABLED
  203. uint64_t function_start_time;
  204. uint64_t function_call_time;
  205. if (GDScriptLanguage::get_singleton()->profiling) {
  206. function_start_time = OS::get_singleton()->get_ticks_usec();
  207. function_call_time = 0;
  208. profile.call_count++;
  209. profile.frame_call_count++;
  210. }
  211. #endif
  212. bool exit_ok = false;
  213. while (ip < _code_size) {
  214. int last_opcode = _code_ptr[ip];
  215. switch (_code_ptr[ip]) {
  216. case OPCODE_OPERATOR: {
  217. CHECK_SPACE(5);
  218. bool valid;
  219. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  220. ERR_BREAK(op >= Variant::OP_MAX);
  221. GET_VARIANT_PTR(a, 2);
  222. GET_VARIANT_PTR(b, 3);
  223. GET_VARIANT_PTR(dst, 4);
  224. #ifdef DEBUG_ENABLED
  225. Variant ret;
  226. Variant::evaluate(op, *a, *b, ret, valid);
  227. #else
  228. Variant::evaluate(op, *a, *b, *dst, valid);
  229. #endif
  230. if (!valid) {
  231. #ifdef DEBUG_ENABLED
  232. if (ret.get_type() == Variant::STRING) {
  233. //return a string when invalid with the error
  234. err_text = ret;
  235. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  236. } else {
  237. 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) + "'.";
  238. }
  239. #endif
  240. break;
  241. }
  242. #ifdef DEBUG_ENABLED
  243. *dst = ret;
  244. #endif
  245. ip += 5;
  246. }
  247. continue;
  248. case OPCODE_EXTENDS_TEST: {
  249. CHECK_SPACE(4);
  250. GET_VARIANT_PTR(a, 1);
  251. GET_VARIANT_PTR(b, 2);
  252. GET_VARIANT_PTR(dst, 3);
  253. #ifdef DEBUG_ENABLED
  254. if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) {
  255. err_text = "Left operand of 'extends' is not an instance of anything.";
  256. break;
  257. }
  258. if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) {
  259. err_text = "Right operand of 'extends' is not a class.";
  260. break;
  261. }
  262. #endif
  263. Object *obj_A = *a;
  264. Object *obj_B = *b;
  265. GDScript *scr_B = obj_B->cast_to<GDScript>();
  266. bool extends_ok = false;
  267. if (scr_B) {
  268. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  269. //in other situation, this shoul return false.
  270. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  271. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  272. //bool found=false;
  273. while (cmp) {
  274. if (cmp == scr_B) {
  275. //inherits from script, all ok
  276. extends_ok = true;
  277. break;
  278. }
  279. cmp = cmp->_base;
  280. }
  281. }
  282. } else {
  283. GDNativeClass *nc = obj_B->cast_to<GDNativeClass>();
  284. if (!nc) {
  285. err_text = "Right operand of 'extends' is not a class (type: '" + obj_B->get_type() + "').";
  286. break;
  287. }
  288. extends_ok = ObjectTypeDB::is_type(obj_A->get_type_name(), nc->get_name());
  289. }
  290. *dst = extends_ok;
  291. ip += 4;
  292. }
  293. continue;
  294. case OPCODE_SET: {
  295. CHECK_SPACE(3);
  296. GET_VARIANT_PTR(dst, 1);
  297. GET_VARIANT_PTR(index, 2);
  298. GET_VARIANT_PTR(value, 3);
  299. bool valid;
  300. dst->set(*index, *value, &valid);
  301. if (!valid) {
  302. String v = index->operator String();
  303. if (v != "") {
  304. v = "'" + v + "'";
  305. } else {
  306. v = "of type '" + _get_var_type(index) + "'";
  307. }
  308. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "').";
  309. break;
  310. }
  311. ip += 4;
  312. }
  313. continue;
  314. case OPCODE_GET: {
  315. CHECK_SPACE(3);
  316. GET_VARIANT_PTR(src, 1);
  317. GET_VARIANT_PTR(index, 2);
  318. GET_VARIANT_PTR(dst, 3);
  319. bool valid;
  320. #ifdef DEBUG_ENABLED
  321. //allow better error message in cases where src and dst are the same stack position
  322. Variant ret = src->get(*index, &valid);
  323. #else
  324. *dst = src->get(*index, &valid);
  325. #endif
  326. if (!valid) {
  327. String v = index->operator String();
  328. if (v != "") {
  329. v = "'" + v + "'";
  330. } else {
  331. v = "of type '" + _get_var_type(index) + "'";
  332. }
  333. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  334. break;
  335. }
  336. #ifdef DEBUG_ENABLED
  337. *dst = ret;
  338. #endif
  339. ip += 4;
  340. }
  341. continue;
  342. case OPCODE_SET_NAMED: {
  343. CHECK_SPACE(3);
  344. GET_VARIANT_PTR(dst, 1);
  345. GET_VARIANT_PTR(value, 3);
  346. int indexname = _code_ptr[ip + 2];
  347. ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  348. const StringName *index = &_global_names_ptr[indexname];
  349. bool valid;
  350. dst->set_named(*index, *value, &valid);
  351. if (!valid) {
  352. String err_type;
  353. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "').";
  354. break;
  355. }
  356. ip += 4;
  357. }
  358. continue;
  359. case OPCODE_GET_NAMED: {
  360. CHECK_SPACE(3);
  361. GET_VARIANT_PTR(src, 1);
  362. GET_VARIANT_PTR(dst, 3);
  363. int indexname = _code_ptr[ip + 2];
  364. ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  365. const StringName *index = &_global_names_ptr[indexname];
  366. bool valid;
  367. #ifdef DEBUG_ENABLED
  368. //allow better error message in cases where src and dst are the same stack position
  369. Variant ret = src->get_named(*index, &valid);
  370. #else
  371. *dst = src->get_named(*index, &valid);
  372. #endif
  373. if (!valid) {
  374. if (src->has_method(*index)) {
  375. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "'). Did you mean '." + index->operator String() + "()' ?";
  376. } else {
  377. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  378. }
  379. break;
  380. }
  381. #ifdef DEBUG_ENABLED
  382. *dst = ret;
  383. #endif
  384. ip += 4;
  385. }
  386. continue;
  387. case OPCODE_ASSIGN: {
  388. CHECK_SPACE(3);
  389. GET_VARIANT_PTR(dst, 1);
  390. GET_VARIANT_PTR(src, 2);
  391. *dst = *src;
  392. ip += 3;
  393. }
  394. continue;
  395. case OPCODE_ASSIGN_TRUE: {
  396. CHECK_SPACE(2);
  397. GET_VARIANT_PTR(dst, 1);
  398. *dst = true;
  399. ip += 2;
  400. }
  401. continue;
  402. case OPCODE_ASSIGN_FALSE: {
  403. CHECK_SPACE(2);
  404. GET_VARIANT_PTR(dst, 1);
  405. *dst = false;
  406. ip += 2;
  407. }
  408. continue;
  409. case OPCODE_CONSTRUCT: {
  410. CHECK_SPACE(2);
  411. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  412. int argc = _code_ptr[ip + 2];
  413. CHECK_SPACE(argc + 2);
  414. Variant **argptrs = call_args;
  415. for (int i = 0; i < argc; i++) {
  416. GET_VARIANT_PTR(v, 3 + i);
  417. argptrs[i] = v;
  418. }
  419. GET_VARIANT_PTR(dst, 3 + argc);
  420. Variant::CallError err;
  421. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  422. if (err.error != Variant::CallError::CALL_OK) {
  423. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  424. break;
  425. }
  426. ip += 4 + argc;
  427. //construct a basic type
  428. }
  429. continue;
  430. case OPCODE_CONSTRUCT_ARRAY: {
  431. CHECK_SPACE(1);
  432. int argc = _code_ptr[ip + 1];
  433. Array array(true); //arrays are always shared
  434. array.resize(argc);
  435. CHECK_SPACE(argc + 2);
  436. for (int i = 0; i < argc; i++) {
  437. GET_VARIANT_PTR(v, 2 + i);
  438. array[i] = *v;
  439. }
  440. GET_VARIANT_PTR(dst, 2 + argc);
  441. *dst = array;
  442. ip += 3 + argc;
  443. }
  444. continue;
  445. case OPCODE_CONSTRUCT_DICTIONARY: {
  446. CHECK_SPACE(1);
  447. int argc = _code_ptr[ip + 1];
  448. Dictionary dict(true); //arrays are always shared
  449. CHECK_SPACE(argc * 2 + 2);
  450. for (int i = 0; i < argc; i++) {
  451. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  452. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  453. dict[*k] = *v;
  454. }
  455. GET_VARIANT_PTR(dst, 2 + argc * 2);
  456. *dst = dict;
  457. ip += 3 + argc * 2;
  458. }
  459. continue;
  460. case OPCODE_CALL_RETURN:
  461. case OPCODE_CALL: {
  462. CHECK_SPACE(4);
  463. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  464. int argc = _code_ptr[ip + 1];
  465. GET_VARIANT_PTR(base, 2);
  466. int nameg = _code_ptr[ip + 3];
  467. ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  468. const StringName *methodname = &_global_names_ptr[nameg];
  469. ERR_BREAK(argc < 0);
  470. ip += 4;
  471. CHECK_SPACE(argc + 1);
  472. Variant **argptrs = call_args;
  473. for (int i = 0; i < argc; i++) {
  474. GET_VARIANT_PTR(v, i);
  475. argptrs[i] = v;
  476. }
  477. #ifdef DEBUG_ENABLED
  478. uint64_t call_time;
  479. if (GDScriptLanguage::get_singleton()->profiling) {
  480. call_time = OS::get_singleton()->get_ticks_usec();
  481. }
  482. #endif
  483. Variant::CallError err;
  484. if (call_ret) {
  485. GET_VARIANT_PTR(ret, argc);
  486. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  487. } else {
  488. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  489. }
  490. #ifdef DEBUG_ENABLED
  491. if (GDScriptLanguage::get_singleton()->profiling) {
  492. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  493. }
  494. #endif
  495. if (err.error != Variant::CallError::CALL_OK) {
  496. String methodstr = *methodname;
  497. String basestr = _get_var_type(base);
  498. if (methodstr == "call") {
  499. if (argc >= 1) {
  500. methodstr = String(*argptrs[0]) + " (via call)";
  501. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  502. err.argument -= 1;
  503. }
  504. }
  505. }
  506. if (methodstr == "free") {
  507. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  508. if (base->is_ref()) {
  509. err_text = "Attempted to free a reference.";
  510. break;
  511. } else if (base->get_type() == Variant::OBJECT) {
  512. err_text = "Attempted to free a locked object (calling or emitting).";
  513. break;
  514. }
  515. }
  516. }
  517. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  518. break;
  519. }
  520. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  521. ip += argc + 1;
  522. }
  523. continue;
  524. case OPCODE_CALL_BUILT_IN: {
  525. CHECK_SPACE(4);
  526. GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip + 1]);
  527. int argc = _code_ptr[ip + 2];
  528. ERR_BREAK(argc < 0);
  529. ip += 3;
  530. CHECK_SPACE(argc + 1);
  531. Variant **argptrs = call_args;
  532. for (int i = 0; i < argc; i++) {
  533. GET_VARIANT_PTR(v, i);
  534. argptrs[i] = v;
  535. }
  536. GET_VARIANT_PTR(dst, argc);
  537. Variant::CallError err;
  538. GDFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  539. if (err.error != Variant::CallError::CALL_OK) {
  540. String methodstr = GDFunctions::get_func_name(func);
  541. if (dst->get_type() == Variant::STRING) {
  542. //call provided error string
  543. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  544. } else {
  545. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  546. }
  547. break;
  548. }
  549. ip += argc + 1;
  550. }
  551. continue;
  552. case OPCODE_CALL_SELF: {
  553. } break;
  554. case OPCODE_CALL_SELF_BASE: {
  555. CHECK_SPACE(2);
  556. int self_fun = _code_ptr[ip + 1];
  557. #ifdef DEBUG_ENABLED
  558. if (self_fun < 0 || self_fun >= _global_names_count) {
  559. err_text = "compiler bug, function name not found";
  560. break;
  561. }
  562. #endif
  563. const StringName *methodname = &_global_names_ptr[self_fun];
  564. int argc = _code_ptr[ip + 2];
  565. CHECK_SPACE(2 + argc + 1);
  566. Variant **argptrs = call_args;
  567. for (int i = 0; i < argc; i++) {
  568. GET_VARIANT_PTR(v, i + 3);
  569. argptrs[i] = v;
  570. }
  571. GET_VARIANT_PTR(dst, argc + 3);
  572. const GDScript *gds = _script;
  573. const Map<StringName, GDFunction *>::Element *E = NULL;
  574. while (gds->base.ptr()) {
  575. gds = gds->base.ptr();
  576. E = gds->member_functions.find(*methodname);
  577. if (E)
  578. break;
  579. }
  580. Variant::CallError err;
  581. if (E) {
  582. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  583. } else if (gds->native.ptr()) {
  584. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  585. MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(), *methodname);
  586. if (!mb) {
  587. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  588. } else {
  589. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  590. }
  591. } else {
  592. err.error = Variant::CallError::CALL_OK;
  593. }
  594. } else {
  595. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  596. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  597. } else {
  598. err.error = Variant::CallError::CALL_OK;
  599. }
  600. }
  601. if (err.error != Variant::CallError::CALL_OK) {
  602. String methodstr = *methodname;
  603. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  604. break;
  605. }
  606. ip += 4 + argc;
  607. }
  608. continue;
  609. case OPCODE_YIELD:
  610. case OPCODE_YIELD_SIGNAL: {
  611. int ipofs = 1;
  612. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  613. CHECK_SPACE(4);
  614. ipofs += 2;
  615. } else {
  616. CHECK_SPACE(2);
  617. }
  618. Ref<GDFunctionState> gdfs = memnew(GDFunctionState);
  619. gdfs->function = this;
  620. gdfs->state.stack.resize(alloca_size);
  621. //copy variant stack
  622. for (int i = 0; i < _stack_size; i++) {
  623. memnew_placement(&gdfs->state.stack[sizeof(Variant) * i], Variant(stack[i]));
  624. }
  625. gdfs->state.stack_size = _stack_size;
  626. gdfs->state.self = self;
  627. gdfs->state.alloca_size = alloca_size;
  628. gdfs->state._class = _class;
  629. gdfs->state.ip = ip + ipofs;
  630. gdfs->state.line = line;
  631. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_ID() : 0;
  632. gdfs->state.script_id = _class->get_instance_ID();
  633. //gdfs->state.result_pos=ip+ipofs-1;
  634. gdfs->state.defarg = defarg;
  635. gdfs->state.instance = p_instance;
  636. gdfs->function = this;
  637. retvalue = gdfs;
  638. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  639. GET_VARIANT_PTR(argobj, 1);
  640. GET_VARIANT_PTR(argname, 2);
  641. //do the oneshot connect
  642. if (argobj->get_type() != Variant::OBJECT) {
  643. err_text = "First argument of yield() not of type object.";
  644. break;
  645. }
  646. if (argname->get_type() != Variant::STRING) {
  647. err_text = "Second argument of yield() not a string (for signal name).";
  648. break;
  649. }
  650. Object *obj = argobj->operator Object *();
  651. String signal = argname->operator String();
  652. #ifdef DEBUG_ENABLED
  653. if (!obj) {
  654. err_text = "First argument of yield() is null.";
  655. break;
  656. }
  657. if (ScriptDebugger::get_singleton()) {
  658. if (!ObjectDB::instance_validate(obj)) {
  659. err_text = "First argument of yield() is a previously freed instance.";
  660. break;
  661. }
  662. }
  663. if (signal.length() == 0) {
  664. err_text = "Second argument of yield() is an empty string (for signal name).";
  665. break;
  666. }
  667. #endif
  668. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  669. if (err != OK) {
  670. err_text = "Error connecting to signal: " + signal + " during yield().";
  671. break;
  672. }
  673. }
  674. exit_ok = true;
  675. } break;
  676. case OPCODE_YIELD_RESUME: {
  677. CHECK_SPACE(2);
  678. if (!p_state) {
  679. err_text = ("Invalid Resume (bug?)");
  680. break;
  681. }
  682. GET_VARIANT_PTR(result, 1);
  683. *result = p_state->result;
  684. ip += 2;
  685. }
  686. continue;
  687. case OPCODE_JUMP: {
  688. CHECK_SPACE(2);
  689. int to = _code_ptr[ip + 1];
  690. ERR_BREAK(to < 0 || to > _code_size);
  691. ip = to;
  692. }
  693. continue;
  694. case OPCODE_JUMP_IF: {
  695. CHECK_SPACE(3);
  696. GET_VARIANT_PTR(test, 1);
  697. bool valid;
  698. bool result = test->booleanize(valid);
  699. #ifdef DEBUG_ENABLED
  700. if (!valid) {
  701. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  702. break;
  703. }
  704. #endif
  705. if (result) {
  706. int to = _code_ptr[ip + 2];
  707. ERR_BREAK(to < 0 || to > _code_size);
  708. ip = to;
  709. continue;
  710. }
  711. ip += 3;
  712. }
  713. continue;
  714. case OPCODE_JUMP_IF_NOT: {
  715. CHECK_SPACE(3);
  716. GET_VARIANT_PTR(test, 1);
  717. bool valid;
  718. bool result = test->booleanize(valid);
  719. #ifdef DEBUG_ENABLED
  720. if (!valid) {
  721. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  722. break;
  723. }
  724. #endif
  725. if (!result) {
  726. int to = _code_ptr[ip + 2];
  727. ERR_BREAK(to < 0 || to > _code_size);
  728. ip = to;
  729. continue;
  730. }
  731. ip += 3;
  732. }
  733. continue;
  734. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  735. CHECK_SPACE(2);
  736. ip = _default_arg_ptr[defarg];
  737. }
  738. continue;
  739. case OPCODE_RETURN: {
  740. CHECK_SPACE(2);
  741. GET_VARIANT_PTR(r, 1);
  742. retvalue = *r;
  743. exit_ok = true;
  744. } break;
  745. case OPCODE_ITERATE_BEGIN: {
  746. CHECK_SPACE(8); //space for this an regular iterate
  747. GET_VARIANT_PTR(counter, 1);
  748. GET_VARIANT_PTR(container, 2);
  749. bool valid;
  750. if (!container->iter_init(*counter, valid)) {
  751. if (!valid) {
  752. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  753. break;
  754. }
  755. int jumpto = _code_ptr[ip + 3];
  756. ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  757. ip = jumpto;
  758. continue;
  759. }
  760. GET_VARIANT_PTR(iterator, 4);
  761. *iterator = container->iter_get(*counter, valid);
  762. if (!valid) {
  763. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  764. break;
  765. }
  766. ip += 5; //skip regular iterate which is always next
  767. }
  768. continue;
  769. case OPCODE_ITERATE: {
  770. CHECK_SPACE(4);
  771. GET_VARIANT_PTR(counter, 1);
  772. GET_VARIANT_PTR(container, 2);
  773. bool valid;
  774. if (!container->iter_next(*counter, valid)) {
  775. if (!valid) {
  776. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  777. break;
  778. }
  779. int jumpto = _code_ptr[ip + 3];
  780. ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  781. ip = jumpto;
  782. continue;
  783. }
  784. GET_VARIANT_PTR(iterator, 4);
  785. *iterator = container->iter_get(*counter, valid);
  786. if (!valid) {
  787. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  788. break;
  789. }
  790. ip += 5; //loop again
  791. }
  792. continue;
  793. case OPCODE_ASSERT: {
  794. CHECK_SPACE(2);
  795. GET_VARIANT_PTR(test, 1);
  796. #ifdef DEBUG_ENABLED
  797. bool valid;
  798. bool result = test->booleanize(valid);
  799. if (!valid) {
  800. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  801. break;
  802. }
  803. if (!result) {
  804. err_text = "Assertion failed.";
  805. break;
  806. }
  807. #endif
  808. ip += 2;
  809. }
  810. continue;
  811. case OPCODE_BREAKPOINT: {
  812. #ifdef DEBUG_ENABLED
  813. if (ScriptDebugger::get_singleton()) {
  814. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  815. }
  816. #endif
  817. ip += 1;
  818. }
  819. continue;
  820. case OPCODE_LINE: {
  821. CHECK_SPACE(2);
  822. line = _code_ptr[ip + 1];
  823. ip += 2;
  824. if (ScriptDebugger::get_singleton()) {
  825. // line
  826. bool do_break = false;
  827. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  828. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  829. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  830. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  831. do_break = true;
  832. }
  833. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  834. do_break = true;
  835. if (do_break) {
  836. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  837. }
  838. ScriptDebugger::get_singleton()->line_poll();
  839. }
  840. }
  841. continue;
  842. case OPCODE_END: {
  843. exit_ok = true;
  844. break;
  845. } break;
  846. default: {
  847. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  848. } break;
  849. }
  850. if (exit_ok)
  851. break;
  852. //error
  853. // function, file, line, error, explanation
  854. String err_file;
  855. if (p_instance)
  856. err_file = p_instance->script->path;
  857. else if (_class)
  858. err_file = _class->path;
  859. if (err_file == "")
  860. err_file = "<built-in>";
  861. String err_func = name;
  862. if (p_instance && p_instance->script->name != "")
  863. err_func = p_instance->script->name + "." + err_func;
  864. int err_line = line;
  865. if (err_text == "") {
  866. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  867. }
  868. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  869. // debugger break did not happen
  870. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  871. }
  872. break;
  873. }
  874. #ifdef DEBUG_ENABLED
  875. if (GDScriptLanguage::get_singleton()->profiling) {
  876. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  877. profile.total_time += time_taken;
  878. profile.self_time += time_taken - function_call_time;
  879. profile.frame_total_time += time_taken;
  880. profile.frame_self_time += time_taken - function_call_time;
  881. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  882. }
  883. #endif
  884. if (ScriptDebugger::get_singleton())
  885. GDScriptLanguage::get_singleton()->exit_function();
  886. if (_stack_size) {
  887. //free stack
  888. for (int i = 0; i < _stack_size; i++)
  889. stack[i].~Variant();
  890. }
  891. return retvalue;
  892. }
  893. const int *GDFunction::get_code() const {
  894. return _code_ptr;
  895. }
  896. int GDFunction::get_code_size() const {
  897. return _code_size;
  898. }
  899. Variant GDFunction::get_constant(int p_idx) const {
  900. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  901. return constants[p_idx];
  902. }
  903. StringName GDFunction::get_global_name(int p_idx) const {
  904. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  905. return global_names[p_idx];
  906. }
  907. int GDFunction::get_default_argument_count() const {
  908. return default_arguments.size();
  909. }
  910. int GDFunction::get_default_argument_addr(int p_arg) const {
  911. ERR_FAIL_INDEX_V(p_arg, default_arguments.size(), -1);
  912. return default_arguments[p_arg];
  913. }
  914. StringName GDFunction::get_name() const {
  915. return name;
  916. }
  917. int GDFunction::get_max_stack_size() const {
  918. return _stack_size;
  919. }
  920. struct _GDFKC {
  921. int order;
  922. List<int> pos;
  923. };
  924. struct _GDFKCS {
  925. int order;
  926. StringName id;
  927. int pos;
  928. bool operator<(const _GDFKCS &p_r) const {
  929. return order < p_r.order;
  930. }
  931. };
  932. void GDFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  933. int oc = 0;
  934. Map<StringName, _GDFKC> sdmap;
  935. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  936. const StackDebug &sd = E->get();
  937. if (sd.line > p_line)
  938. break;
  939. if (sd.added) {
  940. if (!sdmap.has(sd.identifier)) {
  941. _GDFKC d;
  942. d.order = oc++;
  943. d.pos.push_back(sd.pos);
  944. sdmap[sd.identifier] = d;
  945. } else {
  946. sdmap[sd.identifier].pos.push_back(sd.pos);
  947. }
  948. } else {
  949. ERR_CONTINUE(!sdmap.has(sd.identifier));
  950. sdmap[sd.identifier].pos.pop_back();
  951. if (sdmap[sd.identifier].pos.empty())
  952. sdmap.erase(sd.identifier);
  953. }
  954. }
  955. List<_GDFKCS> stackpositions;
  956. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  957. _GDFKCS spp;
  958. spp.id = E->key();
  959. spp.order = E->get().order;
  960. spp.pos = E->get().pos.back()->get();
  961. stackpositions.push_back(spp);
  962. }
  963. stackpositions.sort();
  964. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  965. Pair<StringName, int> p;
  966. p.first = E->get().id;
  967. p.second = E->get().pos;
  968. r_stackvars->push_back(p);
  969. }
  970. }
  971. #if 0
  972. void GDFunction::clear() {
  973. name=StringName();
  974. constants.clear();
  975. _stack_size=0;
  976. code.clear();
  977. _constants_ptr=NULL;
  978. _constant_count=0;
  979. _global_names_ptr=NULL;
  980. _global_names_count=0;
  981. _code_ptr=NULL;
  982. _code_size=0;
  983. }
  984. #endif
  985. GDFunction::GDFunction() :
  986. function_list(this) {
  987. _stack_size = 0;
  988. _call_size = 0;
  989. name = "<anonymous>";
  990. #ifdef DEBUG_ENABLED
  991. _func_cname = NULL;
  992. if (GDScriptLanguage::get_singleton()->lock) {
  993. GDScriptLanguage::get_singleton()->lock->lock();
  994. }
  995. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  996. if (GDScriptLanguage::get_singleton()->lock) {
  997. GDScriptLanguage::get_singleton()->lock->unlock();
  998. }
  999. profile.call_count = 0;
  1000. profile.self_time = 0;
  1001. profile.total_time = 0;
  1002. profile.frame_call_count = 0;
  1003. profile.frame_self_time = 0;
  1004. profile.frame_total_time = 0;
  1005. profile.last_frame_call_count = 0;
  1006. profile.last_frame_self_time = 0;
  1007. profile.last_frame_total_time = 0;
  1008. #endif
  1009. }
  1010. GDFunction::~GDFunction() {
  1011. #ifdef DEBUG_ENABLED
  1012. if (GDScriptLanguage::get_singleton()->lock) {
  1013. GDScriptLanguage::get_singleton()->lock->lock();
  1014. }
  1015. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1016. if (GDScriptLanguage::get_singleton()->lock) {
  1017. GDScriptLanguage::get_singleton()->lock->unlock();
  1018. }
  1019. #endif
  1020. }
  1021. /////////////////////
  1022. Variant GDFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1023. #ifdef DEBUG_ENABLED
  1024. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1025. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1026. ERR_FAIL_V(Variant());
  1027. }
  1028. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1029. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1030. ERR_FAIL_V(Variant());
  1031. }
  1032. #endif
  1033. Variant arg;
  1034. r_error.error = Variant::CallError::CALL_OK;
  1035. ERR_FAIL_COND_V(!function, Variant());
  1036. if (p_argcount == 0) {
  1037. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1038. r_error.argument = 1;
  1039. return Variant();
  1040. } else if (p_argcount == 1) {
  1041. //noooneee
  1042. } else if (p_argcount == 2) {
  1043. arg = *p_args[0];
  1044. } else {
  1045. Array extra_args;
  1046. for (int i = 0; i < p_argcount - 1; i++) {
  1047. extra_args.push_back(*p_args[i]);
  1048. }
  1049. arg = extra_args;
  1050. }
  1051. Ref<GDFunctionState> self = *p_args[p_argcount - 1];
  1052. if (self.is_null()) {
  1053. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1054. r_error.argument = p_argcount - 1;
  1055. r_error.expected = Variant::OBJECT;
  1056. return Variant();
  1057. }
  1058. state.result = arg;
  1059. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1060. function = NULL; //cleaned up;
  1061. state.result = Variant();
  1062. return ret;
  1063. }
  1064. bool GDFunctionState::is_valid(bool p_extended_check) const {
  1065. if (function == NULL)
  1066. return false;
  1067. if (p_extended_check) {
  1068. //class instance gone?
  1069. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1070. return false;
  1071. //script gone?
  1072. if (state.script_id && !ObjectDB::get_instance(state.script_id))
  1073. return false;
  1074. }
  1075. return true;
  1076. }
  1077. Variant GDFunctionState::resume(const Variant &p_arg) {
  1078. ERR_FAIL_COND_V(!function, Variant());
  1079. #ifdef DEBUG_ENABLED
  1080. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1081. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1082. ERR_FAIL_V(Variant());
  1083. }
  1084. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1085. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1086. ERR_FAIL_V(Variant());
  1087. }
  1088. #endif
  1089. state.result = p_arg;
  1090. Variant::CallError err;
  1091. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1092. function = NULL; //cleaned up;
  1093. state.result = Variant();
  1094. return ret;
  1095. }
  1096. void GDFunctionState::_bind_methods() {
  1097. ObjectTypeDB::bind_method(_MD("resume:Variant", "arg"), &GDFunctionState::resume, DEFVAL(Variant()));
  1098. ObjectTypeDB::bind_method(_MD("is_valid", "extended_check"), &GDFunctionState::is_valid, DEFVAL(false));
  1099. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1100. }
  1101. GDFunctionState::GDFunctionState() {
  1102. function = NULL;
  1103. }
  1104. GDFunctionState::~GDFunctionState() {
  1105. if (function != NULL) {
  1106. //never called, deinitialize stack
  1107. for (int i = 0; i < state.stack_size; i++) {
  1108. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1109. v->~Variant();
  1110. }
  1111. }
  1112. }