gd_function.cpp 36 KB

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