gdscript_function.cpp 50 KB

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