gdscript_function.cpp 52 KB

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