gdscript_function.cpp 52 KB

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