gdscript_function.cpp 52 KB

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