gdscript_function.cpp 52 KB

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