gd_function.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*************************************************************************/
  2. /* gd_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  125. else
  126. basestr = bobj->get_class();
  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. continue;
  276. }
  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 'is' 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 'is' 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 'is' is not a class (type: '" + obj_B->get_class() + "').";
  315. break;
  316. }
  317. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  318. }
  319. *dst = extends_ok;
  320. ip += 4;
  321. continue;
  322. }
  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. continue;
  342. }
  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. continue;
  370. }
  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. continue;
  387. }
  388. case OPCODE_GET_NAMED: {
  389. CHECK_SPACE(4);
  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. continue;
  415. }
  416. case OPCODE_SET_MEMBER: {
  417. CHECK_SPACE(3);
  418. int indexname = _code_ptr[ip + 1];
  419. ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  420. const StringName *index = &_global_names_ptr[indexname];
  421. GET_VARIANT_PTR(src, 2);
  422. bool valid;
  423. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  424. #ifdef DEBUG_ENABLED
  425. if (!ok) {
  426. err_text = "Internal error setting property: " + String(*index);
  427. break;
  428. } else if (!valid) {
  429. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  430. break;
  431. }
  432. #endif
  433. ip += 3;
  434. continue;
  435. }
  436. case OPCODE_GET_MEMBER: {
  437. CHECK_SPACE(3);
  438. int indexname = _code_ptr[ip + 1];
  439. ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  440. const StringName *index = &_global_names_ptr[indexname];
  441. GET_VARIANT_PTR(dst, 2);
  442. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  443. #ifdef DEBUG_ENABLED
  444. if (!ok) {
  445. err_text = "Internal error getting property: " + String(*index);
  446. break;
  447. }
  448. #endif
  449. ip += 3;
  450. continue;
  451. }
  452. case OPCODE_ASSIGN: {
  453. CHECK_SPACE(3);
  454. GET_VARIANT_PTR(dst, 1);
  455. GET_VARIANT_PTR(src, 2);
  456. *dst = *src;
  457. ip += 3;
  458. continue;
  459. }
  460. case OPCODE_ASSIGN_TRUE: {
  461. CHECK_SPACE(2);
  462. GET_VARIANT_PTR(dst, 1);
  463. *dst = true;
  464. ip += 2;
  465. continue;
  466. }
  467. case OPCODE_ASSIGN_FALSE: {
  468. CHECK_SPACE(2);
  469. GET_VARIANT_PTR(dst, 1);
  470. *dst = false;
  471. ip += 2;
  472. continue;
  473. }
  474. case OPCODE_CONSTRUCT: {
  475. CHECK_SPACE(2);
  476. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  477. int argc = _code_ptr[ip + 2];
  478. CHECK_SPACE(argc + 2);
  479. Variant **argptrs = call_args;
  480. for (int i = 0; i < argc; i++) {
  481. GET_VARIANT_PTR(v, 3 + i);
  482. argptrs[i] = v;
  483. }
  484. GET_VARIANT_PTR(dst, 3 + argc);
  485. Variant::CallError err;
  486. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  487. if (err.error != Variant::CallError::CALL_OK) {
  488. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  489. break;
  490. }
  491. ip += 4 + argc;
  492. //construct a basic type
  493. continue;
  494. }
  495. case OPCODE_CONSTRUCT_ARRAY: {
  496. CHECK_SPACE(1);
  497. int argc = _code_ptr[ip + 1];
  498. Array array; //arrays are always shared
  499. array.resize(argc);
  500. CHECK_SPACE(argc + 2);
  501. for (int i = 0; i < argc; i++) {
  502. GET_VARIANT_PTR(v, 2 + i);
  503. array[i] = *v;
  504. }
  505. GET_VARIANT_PTR(dst, 2 + argc);
  506. *dst = array;
  507. ip += 3 + argc;
  508. continue;
  509. }
  510. case OPCODE_CONSTRUCT_DICTIONARY: {
  511. CHECK_SPACE(1);
  512. int argc = _code_ptr[ip + 1];
  513. Dictionary dict; //arrays are always shared
  514. CHECK_SPACE(argc * 2 + 2);
  515. for (int i = 0; i < argc; i++) {
  516. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  517. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  518. dict[*k] = *v;
  519. }
  520. GET_VARIANT_PTR(dst, 2 + argc * 2);
  521. *dst = dict;
  522. ip += 3 + argc * 2;
  523. continue;
  524. }
  525. case OPCODE_CALL_RETURN:
  526. case OPCODE_CALL: {
  527. CHECK_SPACE(4);
  528. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  529. int argc = _code_ptr[ip + 1];
  530. GET_VARIANT_PTR(base, 2);
  531. int nameg = _code_ptr[ip + 3];
  532. ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  533. const StringName *methodname = &_global_names_ptr[nameg];
  534. ERR_BREAK(argc < 0);
  535. ip += 4;
  536. CHECK_SPACE(argc + 1);
  537. Variant **argptrs = call_args;
  538. for (int i = 0; i < argc; i++) {
  539. GET_VARIANT_PTR(v, i);
  540. argptrs[i] = v;
  541. }
  542. #ifdef DEBUG_ENABLED
  543. uint64_t call_time;
  544. if (GDScriptLanguage::get_singleton()->profiling) {
  545. call_time = OS::get_singleton()->get_ticks_usec();
  546. }
  547. #endif
  548. Variant::CallError err;
  549. if (call_ret) {
  550. GET_VARIANT_PTR(ret, argc);
  551. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  552. } else {
  553. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  554. }
  555. #ifdef DEBUG_ENABLED
  556. if (GDScriptLanguage::get_singleton()->profiling) {
  557. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  558. }
  559. #endif
  560. if (err.error != Variant::CallError::CALL_OK) {
  561. String methodstr = *methodname;
  562. String basestr = _get_var_type(base);
  563. if (methodstr == "call") {
  564. if (argc >= 1) {
  565. methodstr = String(*argptrs[0]) + " (via call)";
  566. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  567. err.argument -= 1;
  568. }
  569. }
  570. } else if (methodstr == "free") {
  571. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  572. if (base->is_ref()) {
  573. err_text = "Attempted to free a reference.";
  574. break;
  575. } else if (base->get_type() == Variant::OBJECT) {
  576. err_text = "Attempted to free a locked object (calling or emitting).";
  577. break;
  578. }
  579. }
  580. }
  581. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  582. break;
  583. }
  584. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  585. ip += argc + 1;
  586. continue;
  587. }
  588. case OPCODE_CALL_BUILT_IN: {
  589. CHECK_SPACE(4);
  590. GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip + 1]);
  591. int argc = _code_ptr[ip + 2];
  592. ERR_BREAK(argc < 0);
  593. ip += 3;
  594. CHECK_SPACE(argc + 1);
  595. Variant **argptrs = call_args;
  596. for (int i = 0; i < argc; i++) {
  597. GET_VARIANT_PTR(v, i);
  598. argptrs[i] = v;
  599. }
  600. GET_VARIANT_PTR(dst, argc);
  601. Variant::CallError err;
  602. GDFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  603. if (err.error != Variant::CallError::CALL_OK) {
  604. String methodstr = GDFunctions::get_func_name(func);
  605. if (dst->get_type() == Variant::STRING) {
  606. //call provided error string
  607. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  608. } else {
  609. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  610. }
  611. break;
  612. }
  613. ip += argc + 1;
  614. continue;
  615. }
  616. case OPCODE_CALL_SELF: {
  617. break;
  618. }
  619. case OPCODE_CALL_SELF_BASE: {
  620. CHECK_SPACE(2);
  621. int self_fun = _code_ptr[ip + 1];
  622. #ifdef DEBUG_ENABLED
  623. if (self_fun < 0 || self_fun >= _global_names_count) {
  624. err_text = "compiler bug, function name not found";
  625. break;
  626. }
  627. #endif
  628. const StringName *methodname = &_global_names_ptr[self_fun];
  629. int argc = _code_ptr[ip + 2];
  630. CHECK_SPACE(2 + argc + 1);
  631. Variant **argptrs = call_args;
  632. for (int i = 0; i < argc; i++) {
  633. GET_VARIANT_PTR(v, i + 3);
  634. argptrs[i] = v;
  635. }
  636. GET_VARIANT_PTR(dst, argc + 3);
  637. const GDScript *gds = _script;
  638. const Map<StringName, GDFunction *>::Element *E = NULL;
  639. while (gds->base.ptr()) {
  640. gds = gds->base.ptr();
  641. E = gds->member_functions.find(*methodname);
  642. if (E)
  643. break;
  644. }
  645. Variant::CallError err;
  646. if (E) {
  647. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  648. } else if (gds->native.ptr()) {
  649. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  650. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  651. if (!mb) {
  652. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  653. } else {
  654. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  655. }
  656. } else {
  657. err.error = Variant::CallError::CALL_OK;
  658. }
  659. } else {
  660. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  661. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  662. } else {
  663. err.error = Variant::CallError::CALL_OK;
  664. }
  665. }
  666. if (err.error != Variant::CallError::CALL_OK) {
  667. String methodstr = *methodname;
  668. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  669. break;
  670. }
  671. ip += 4 + argc;
  672. continue;
  673. }
  674. case OPCODE_YIELD:
  675. case OPCODE_YIELD_SIGNAL: {
  676. int ipofs = 1;
  677. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  678. CHECK_SPACE(4);
  679. ipofs += 2;
  680. } else {
  681. CHECK_SPACE(2);
  682. }
  683. Ref<GDFunctionState> gdfs = memnew(GDFunctionState);
  684. gdfs->function = this;
  685. gdfs->state.stack.resize(alloca_size);
  686. //copy variant stack
  687. for (int i = 0; i < _stack_size; i++) {
  688. memnew_placement(&gdfs->state.stack[sizeof(Variant) * i], Variant(stack[i]));
  689. }
  690. gdfs->state.stack_size = _stack_size;
  691. gdfs->state.self = self;
  692. gdfs->state.alloca_size = alloca_size;
  693. gdfs->state._class = _class;
  694. gdfs->state.ip = ip + ipofs;
  695. gdfs->state.line = line;
  696. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_ID() : 0;
  697. gdfs->state.script_id = _class->get_instance_ID();
  698. //gdfs->state.result_pos=ip+ipofs-1;
  699. gdfs->state.defarg = defarg;
  700. gdfs->state.instance = p_instance;
  701. gdfs->function = this;
  702. retvalue = gdfs;
  703. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  704. GET_VARIANT_PTR(argobj, 1);
  705. GET_VARIANT_PTR(argname, 2);
  706. //do the oneshot connect
  707. if (argobj->get_type() != Variant::OBJECT) {
  708. err_text = "First argument of yield() not of type object.";
  709. break;
  710. }
  711. if (argname->get_type() != Variant::STRING) {
  712. err_text = "Second argument of yield() not a string (for signal name).";
  713. break;
  714. }
  715. Object *obj = argobj->operator Object *();
  716. String signal = argname->operator String();
  717. #ifdef DEBUG_ENABLED
  718. if (!obj) {
  719. err_text = "First argument of yield() is null.";
  720. break;
  721. }
  722. if (ScriptDebugger::get_singleton()) {
  723. if (!ObjectDB::instance_validate(obj)) {
  724. err_text = "First argument of yield() is a previously freed instance.";
  725. break;
  726. }
  727. }
  728. if (signal.length() == 0) {
  729. err_text = "Second argument of yield() is an empty string (for signal name).";
  730. break;
  731. }
  732. #endif
  733. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  734. if (err != OK) {
  735. err_text = "Error connecting to signal: " + signal + " during yield().";
  736. break;
  737. }
  738. }
  739. exit_ok = true;
  740. break;
  741. }
  742. case OPCODE_YIELD_RESUME: {
  743. CHECK_SPACE(2);
  744. if (!p_state) {
  745. err_text = ("Invalid Resume (bug?)");
  746. break;
  747. }
  748. GET_VARIANT_PTR(result, 1);
  749. *result = p_state->result;
  750. ip += 2;
  751. continue;
  752. }
  753. case OPCODE_JUMP: {
  754. CHECK_SPACE(2);
  755. int to = _code_ptr[ip + 1];
  756. ERR_BREAK(to < 0 || to > _code_size);
  757. ip = to;
  758. continue;
  759. }
  760. case OPCODE_JUMP_IF: {
  761. CHECK_SPACE(3);
  762. GET_VARIANT_PTR(test, 1);
  763. bool valid;
  764. bool result = test->booleanize(valid);
  765. #ifdef DEBUG_ENABLED
  766. if (!valid) {
  767. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  768. break;
  769. }
  770. #endif
  771. if (result) {
  772. int to = _code_ptr[ip + 2];
  773. ERR_BREAK(to < 0 || to > _code_size);
  774. ip = to;
  775. continue;
  776. }
  777. ip += 3;
  778. continue;
  779. }
  780. case OPCODE_JUMP_IF_NOT: {
  781. CHECK_SPACE(3);
  782. GET_VARIANT_PTR(test, 1);
  783. bool valid;
  784. bool result = test->booleanize(valid);
  785. #ifdef DEBUG_ENABLED
  786. if (!valid) {
  787. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  788. break;
  789. }
  790. #endif
  791. if (!result) {
  792. int to = _code_ptr[ip + 2];
  793. ERR_BREAK(to < 0 || to > _code_size);
  794. ip = to;
  795. continue;
  796. }
  797. ip += 3;
  798. continue;
  799. }
  800. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  801. CHECK_SPACE(2);
  802. ip = _default_arg_ptr[defarg];
  803. continue;
  804. }
  805. case OPCODE_RETURN: {
  806. CHECK_SPACE(2);
  807. GET_VARIANT_PTR(r, 1);
  808. retvalue = *r;
  809. exit_ok = true;
  810. break;
  811. }
  812. case OPCODE_ITERATE_BEGIN: {
  813. CHECK_SPACE(8); //space for this an regular iterate
  814. GET_VARIANT_PTR(counter, 1);
  815. GET_VARIANT_PTR(container, 2);
  816. bool valid;
  817. if (!container->iter_init(*counter, valid)) {
  818. if (!valid) {
  819. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  820. break;
  821. }
  822. int jumpto = _code_ptr[ip + 3];
  823. ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  824. ip = jumpto;
  825. continue;
  826. }
  827. GET_VARIANT_PTR(iterator, 4);
  828. *iterator = container->iter_get(*counter, valid);
  829. if (!valid) {
  830. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  831. break;
  832. }
  833. ip += 5; //skip regular iterate which is always next
  834. continue;
  835. }
  836. case OPCODE_ITERATE: {
  837. CHECK_SPACE(4);
  838. GET_VARIANT_PTR(counter, 1);
  839. GET_VARIANT_PTR(container, 2);
  840. bool valid;
  841. if (!container->iter_next(*counter, valid)) {
  842. if (!valid) {
  843. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  844. break;
  845. }
  846. int jumpto = _code_ptr[ip + 3];
  847. ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  848. ip = jumpto;
  849. continue;
  850. }
  851. GET_VARIANT_PTR(iterator, 4);
  852. *iterator = container->iter_get(*counter, valid);
  853. if (!valid) {
  854. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  855. break;
  856. }
  857. ip += 5; //loop again
  858. continue;
  859. }
  860. case OPCODE_ASSERT: {
  861. CHECK_SPACE(2);
  862. GET_VARIANT_PTR(test, 1);
  863. #ifdef DEBUG_ENABLED
  864. bool valid;
  865. bool result = test->booleanize(valid);
  866. if (!valid) {
  867. err_text = "cannot evaluate conditional expression of type: " + Variant::get_type_name(test->get_type());
  868. break;
  869. }
  870. if (!result) {
  871. err_text = "Assertion failed.";
  872. break;
  873. }
  874. #endif
  875. ip += 2;
  876. continue;
  877. }
  878. case OPCODE_BREAKPOINT: {
  879. #ifdef DEBUG_ENABLED
  880. if (ScriptDebugger::get_singleton()) {
  881. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  882. }
  883. #endif
  884. ip += 1;
  885. continue;
  886. }
  887. case OPCODE_LINE: {
  888. CHECK_SPACE(2);
  889. line = _code_ptr[ip + 1];
  890. ip += 2;
  891. if (ScriptDebugger::get_singleton()) {
  892. // line
  893. bool do_break = false;
  894. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  895. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  896. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  897. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  898. do_break = true;
  899. }
  900. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  901. do_break = true;
  902. if (do_break) {
  903. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  904. }
  905. ScriptDebugger::get_singleton()->line_poll();
  906. }
  907. continue;
  908. }
  909. case OPCODE_END: {
  910. exit_ok = true;
  911. break;
  912. }
  913. default: {
  914. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  915. break;
  916. }
  917. }
  918. if (exit_ok)
  919. break;
  920. //error
  921. // function, file, line, error, explanation
  922. String err_file;
  923. if (p_instance)
  924. err_file = p_instance->script->path;
  925. else if (_class)
  926. err_file = _class->path;
  927. if (err_file == "")
  928. err_file = "<built-in>";
  929. String err_func = name;
  930. if (p_instance && p_instance->script->name != "")
  931. err_func = p_instance->script->name + "." + err_func;
  932. int err_line = line;
  933. if (err_text == "") {
  934. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  935. }
  936. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  937. // debugger break did not happen
  938. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  939. }
  940. break;
  941. }
  942. #ifdef DEBUG_ENABLED
  943. if (GDScriptLanguage::get_singleton()->profiling) {
  944. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  945. profile.total_time += time_taken;
  946. profile.self_time += time_taken - function_call_time;
  947. profile.frame_total_time += time_taken;
  948. profile.frame_self_time += time_taken - function_call_time;
  949. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  950. }
  951. #endif
  952. if (ScriptDebugger::get_singleton())
  953. GDScriptLanguage::get_singleton()->exit_function();
  954. if (_stack_size) {
  955. //free stack
  956. for (int i = 0; i < _stack_size; i++)
  957. stack[i].~Variant();
  958. }
  959. return retvalue;
  960. }
  961. const int *GDFunction::get_code() const {
  962. return _code_ptr;
  963. }
  964. int GDFunction::get_code_size() const {
  965. return _code_size;
  966. }
  967. Variant GDFunction::get_constant(int p_idx) const {
  968. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  969. return constants[p_idx];
  970. }
  971. StringName GDFunction::get_global_name(int p_idx) const {
  972. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  973. return global_names[p_idx];
  974. }
  975. int GDFunction::get_default_argument_count() const {
  976. return default_arguments.size();
  977. }
  978. int GDFunction::get_default_argument_addr(int p_arg) const {
  979. ERR_FAIL_INDEX_V(p_arg, default_arguments.size(), -1);
  980. return default_arguments[p_arg];
  981. }
  982. StringName GDFunction::get_name() const {
  983. return name;
  984. }
  985. int GDFunction::get_max_stack_size() const {
  986. return _stack_size;
  987. }
  988. struct _GDFKC {
  989. int order;
  990. List<int> pos;
  991. };
  992. struct _GDFKCS {
  993. int order;
  994. StringName id;
  995. int pos;
  996. bool operator<(const _GDFKCS &p_r) const {
  997. return order < p_r.order;
  998. }
  999. };
  1000. void GDFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1001. int oc = 0;
  1002. Map<StringName, _GDFKC> sdmap;
  1003. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1004. const StackDebug &sd = E->get();
  1005. if (sd.line > p_line)
  1006. break;
  1007. if (sd.added) {
  1008. if (!sdmap.has(sd.identifier)) {
  1009. _GDFKC d;
  1010. d.order = oc++;
  1011. d.pos.push_back(sd.pos);
  1012. sdmap[sd.identifier] = d;
  1013. } else {
  1014. sdmap[sd.identifier].pos.push_back(sd.pos);
  1015. }
  1016. } else {
  1017. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1018. sdmap[sd.identifier].pos.pop_back();
  1019. if (sdmap[sd.identifier].pos.empty())
  1020. sdmap.erase(sd.identifier);
  1021. }
  1022. }
  1023. List<_GDFKCS> stackpositions;
  1024. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1025. _GDFKCS spp;
  1026. spp.id = E->key();
  1027. spp.order = E->get().order;
  1028. spp.pos = E->get().pos.back()->get();
  1029. stackpositions.push_back(spp);
  1030. }
  1031. stackpositions.sort();
  1032. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1033. Pair<StringName, int> p;
  1034. p.first = E->get().id;
  1035. p.second = E->get().pos;
  1036. r_stackvars->push_back(p);
  1037. }
  1038. }
  1039. #if 0
  1040. void GDFunction::clear() {
  1041. name=StringName();
  1042. constants.clear();
  1043. _stack_size=0;
  1044. code.clear();
  1045. _constants_ptr=NULL;
  1046. _constant_count=0;
  1047. _global_names_ptr=NULL;
  1048. _global_names_count=0;
  1049. _code_ptr=NULL;
  1050. _code_size=0;
  1051. }
  1052. #endif
  1053. GDFunction::GDFunction()
  1054. : function_list(this) {
  1055. _stack_size = 0;
  1056. _call_size = 0;
  1057. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  1058. name = "<anonymous>";
  1059. #ifdef DEBUG_ENABLED
  1060. _func_cname = NULL;
  1061. if (GDScriptLanguage::get_singleton()->lock) {
  1062. GDScriptLanguage::get_singleton()->lock->lock();
  1063. }
  1064. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1065. if (GDScriptLanguage::get_singleton()->lock) {
  1066. GDScriptLanguage::get_singleton()->lock->unlock();
  1067. }
  1068. profile.call_count = 0;
  1069. profile.self_time = 0;
  1070. profile.total_time = 0;
  1071. profile.frame_call_count = 0;
  1072. profile.frame_self_time = 0;
  1073. profile.frame_total_time = 0;
  1074. profile.last_frame_call_count = 0;
  1075. profile.last_frame_self_time = 0;
  1076. profile.last_frame_total_time = 0;
  1077. #endif
  1078. }
  1079. GDFunction::~GDFunction() {
  1080. #ifdef DEBUG_ENABLED
  1081. if (GDScriptLanguage::get_singleton()->lock) {
  1082. GDScriptLanguage::get_singleton()->lock->lock();
  1083. }
  1084. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1085. if (GDScriptLanguage::get_singleton()->lock) {
  1086. GDScriptLanguage::get_singleton()->lock->unlock();
  1087. }
  1088. #endif
  1089. }
  1090. /////////////////////
  1091. Variant GDFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1092. #ifdef DEBUG_ENABLED
  1093. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1094. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1095. ERR_FAIL_V(Variant());
  1096. }
  1097. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1098. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1099. ERR_FAIL_V(Variant());
  1100. }
  1101. #endif
  1102. Variant arg;
  1103. r_error.error = Variant::CallError::CALL_OK;
  1104. ERR_FAIL_COND_V(!function, Variant());
  1105. if (p_argcount == 0) {
  1106. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1107. r_error.argument = 1;
  1108. return Variant();
  1109. } else if (p_argcount == 1) {
  1110. //noooneee
  1111. } else if (p_argcount == 2) {
  1112. arg = *p_args[0];
  1113. } else {
  1114. Array extra_args;
  1115. for (int i = 0; i < p_argcount - 1; i++) {
  1116. extra_args.push_back(*p_args[i]);
  1117. }
  1118. arg = extra_args;
  1119. }
  1120. Ref<GDFunctionState> self = *p_args[p_argcount - 1];
  1121. if (self.is_null()) {
  1122. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1123. r_error.argument = p_argcount - 1;
  1124. r_error.expected = Variant::OBJECT;
  1125. return Variant();
  1126. }
  1127. state.result = arg;
  1128. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1129. bool completed = true;
  1130. // If the return value is a GDFunctionState reference,
  1131. // then the function did yield again after resuming.
  1132. if (ret.is_ref()) {
  1133. GDFunctionState *gdfs = ret.operator Object *()->cast_to<GDFunctionState>();
  1134. if (gdfs && gdfs->function == function)
  1135. completed = false;
  1136. }
  1137. function = NULL; //cleaned up;
  1138. state.result = Variant();
  1139. if (completed) {
  1140. emit_signal("completed", ret);
  1141. }
  1142. return ret;
  1143. }
  1144. bool GDFunctionState::is_valid(bool p_extended_check) const {
  1145. if (function == NULL)
  1146. return false;
  1147. if (p_extended_check) {
  1148. //class instance gone?
  1149. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1150. return false;
  1151. //script gone?
  1152. if (state.script_id && !ObjectDB::get_instance(state.script_id))
  1153. return false;
  1154. }
  1155. return true;
  1156. }
  1157. Variant GDFunctionState::resume(const Variant &p_arg) {
  1158. ERR_FAIL_COND_V(!function, Variant());
  1159. #ifdef DEBUG_ENABLED
  1160. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1161. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1162. ERR_FAIL_V(Variant());
  1163. }
  1164. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1165. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1166. ERR_FAIL_V(Variant());
  1167. }
  1168. #endif
  1169. state.result = p_arg;
  1170. Variant::CallError err;
  1171. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1172. bool completed = true;
  1173. // If the return value is a GDFunctionState reference,
  1174. // then the function did yield again after resuming.
  1175. if (ret.is_ref()) {
  1176. GDFunctionState *gdfs = ret.operator Object *()->cast_to<GDFunctionState>();
  1177. if (gdfs && gdfs->function == function)
  1178. completed = false;
  1179. }
  1180. function = NULL; //cleaned up;
  1181. state.result = Variant();
  1182. if (completed) {
  1183. emit_signal("completed", ret);
  1184. }
  1185. return ret;
  1186. }
  1187. void GDFunctionState::_bind_methods() {
  1188. ClassDB::bind_method(D_METHOD("resume:Variant", "arg"), &GDFunctionState::resume, DEFVAL(Variant()));
  1189. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDFunctionState::is_valid, DEFVAL(false));
  1190. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1191. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result")));
  1192. }
  1193. GDFunctionState::GDFunctionState() {
  1194. function = NULL;
  1195. }
  1196. GDFunctionState::~GDFunctionState() {
  1197. if (function != NULL) {
  1198. //never called, deinitialize stack
  1199. for (int i = 0; i < state.stack_size; i++) {
  1200. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1201. v->~Variant();
  1202. }
  1203. }
  1204. }