gdscript_function.cpp 51 KB

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