gdscript_function.cpp 51 KB

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