gdscript_function.cpp 51 KB

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