gdscript_function.cpp 50 KB

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