gd_function.cpp 38 KB

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