gdscript_function.cpp 52 KB

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