gdscript_function.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  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. #ifdef DEBUG_ENABLED
  35. #include "core/string/string_builder.h"
  36. #endif
  37. 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 {
  38. int address = p_address & ADDR_MASK;
  39. //sequential table (jump table generated by compiler)
  40. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  41. case ADDR_TYPE_SELF: {
  42. #ifdef DEBUG_ENABLED
  43. if (unlikely(!p_instance)) {
  44. r_error = "Cannot access self without instance.";
  45. return nullptr;
  46. }
  47. #endif
  48. return &self;
  49. } break;
  50. case ADDR_TYPE_CLASS: {
  51. return &static_ref;
  52. } break;
  53. case ADDR_TYPE_MEMBER: {
  54. #ifdef DEBUG_ENABLED
  55. if (unlikely(!p_instance)) {
  56. r_error = "Cannot access member without instance.";
  57. return nullptr;
  58. }
  59. #endif
  60. //member indexing is O(1)
  61. return &p_instance->members.write[address];
  62. } break;
  63. case ADDR_TYPE_CLASS_CONSTANT: {
  64. //todo change to index!
  65. GDScript *s = p_script;
  66. #ifdef DEBUG_ENABLED
  67. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  68. #endif
  69. const StringName *sn = &_global_names_ptr[address];
  70. while (s) {
  71. GDScript *o = s;
  72. while (o) {
  73. Map<StringName, Variant>::Element *E = o->constants.find(*sn);
  74. if (E) {
  75. return &E->get();
  76. }
  77. o = o->_owner;
  78. }
  79. s = s->_base;
  80. }
  81. ERR_FAIL_V_MSG(nullptr, "GDScriptCompiler bug.");
  82. } break;
  83. case ADDR_TYPE_LOCAL_CONSTANT: {
  84. #ifdef DEBUG_ENABLED
  85. ERR_FAIL_INDEX_V(address, _constant_count, nullptr);
  86. #endif
  87. return &_constants_ptr[address];
  88. } break;
  89. case ADDR_TYPE_STACK:
  90. case ADDR_TYPE_STACK_VARIABLE: {
  91. #ifdef DEBUG_ENABLED
  92. ERR_FAIL_INDEX_V(address, _stack_size, nullptr);
  93. #endif
  94. return &p_stack[address];
  95. } break;
  96. case ADDR_TYPE_GLOBAL: {
  97. #ifdef DEBUG_ENABLED
  98. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), nullptr);
  99. #endif
  100. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  101. } break;
  102. #ifdef TOOLS_ENABLED
  103. case ADDR_TYPE_NAMED_GLOBAL: {
  104. #ifdef DEBUG_ENABLED
  105. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  106. #endif
  107. StringName id = _global_names_ptr[address];
  108. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  109. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  110. } else {
  111. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  112. return nullptr;
  113. }
  114. } break;
  115. #endif
  116. case ADDR_TYPE_NIL: {
  117. return &nil;
  118. } break;
  119. }
  120. ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode).");
  121. return nullptr;
  122. }
  123. #ifdef DEBUG_ENABLED
  124. static String _get_var_type(const Variant *p_var) {
  125. String basestr;
  126. if (p_var->get_type() == Variant::OBJECT) {
  127. bool was_freed;
  128. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  129. if (!bobj) {
  130. if (was_freed) {
  131. basestr = "null instance";
  132. } else {
  133. basestr = "previously freed";
  134. }
  135. } else {
  136. if (bobj->get_script_instance()) {
  137. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  138. } else {
  139. basestr = bobj->get_class();
  140. }
  141. }
  142. } else {
  143. basestr = Variant::get_type_name(p_var->get_type());
  144. }
  145. return basestr;
  146. }
  147. #endif // DEBUG_ENABLED
  148. String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  149. String err_text;
  150. if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  151. int errorarg = p_err.argument;
  152. // Handle the Object to Object case separately as we don't have further class details.
  153. #ifdef DEBUG_ENABLED
  154. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  155. 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.";
  156. } else
  157. #endif // DEBUG_ENABLED
  158. {
  159. err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
  160. }
  161. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  162. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  163. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  164. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  165. } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  166. err_text = "Invalid call. Nonexistent " + p_where + ".";
  167. } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  168. err_text = "Attempt to call " + p_where + " on a null instance.";
  169. } else {
  170. err_text = "Bug, call error: #" + itos(p_err.error);
  171. }
  172. return err_text;
  173. }
  174. #if defined(__GNUC__)
  175. #define OPCODES_TABLE \
  176. static const void *switch_table_ops[] = { \
  177. &&OPCODE_OPERATOR, \
  178. &&OPCODE_EXTENDS_TEST, \
  179. &&OPCODE_IS_BUILTIN, \
  180. &&OPCODE_SET, \
  181. &&OPCODE_GET, \
  182. &&OPCODE_SET_NAMED, \
  183. &&OPCODE_GET_NAMED, \
  184. &&OPCODE_SET_MEMBER, \
  185. &&OPCODE_GET_MEMBER, \
  186. &&OPCODE_ASSIGN, \
  187. &&OPCODE_ASSIGN_TRUE, \
  188. &&OPCODE_ASSIGN_FALSE, \
  189. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  190. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  191. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  192. &&OPCODE_CAST_TO_BUILTIN, \
  193. &&OPCODE_CAST_TO_NATIVE, \
  194. &&OPCODE_CAST_TO_SCRIPT, \
  195. &&OPCODE_CONSTRUCT, \
  196. &&OPCODE_CONSTRUCT_ARRAY, \
  197. &&OPCODE_CONSTRUCT_DICTIONARY, \
  198. &&OPCODE_CALL, \
  199. &&OPCODE_CALL_RETURN, \
  200. &&OPCODE_CALL_ASYNC, \
  201. &&OPCODE_CALL_BUILT_IN, \
  202. &&OPCODE_CALL_SELF_BASE, \
  203. &&OPCODE_AWAIT, \
  204. &&OPCODE_AWAIT_RESUME, \
  205. &&OPCODE_JUMP, \
  206. &&OPCODE_JUMP_IF, \
  207. &&OPCODE_JUMP_IF_NOT, \
  208. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  209. &&OPCODE_RETURN, \
  210. &&OPCODE_ITERATE_BEGIN, \
  211. &&OPCODE_ITERATE, \
  212. &&OPCODE_ASSERT, \
  213. &&OPCODE_BREAKPOINT, \
  214. &&OPCODE_LINE, \
  215. &&OPCODE_END \
  216. }; \
  217. static_assert((sizeof(switch_table_ops) / sizeof(switch_table_ops[0]) == (OPCODE_END + 1)), "Opcodes in jump table aren't the same as opcodes in enum.");
  218. #define OPCODE(m_op) \
  219. m_op:
  220. #define OPCODE_WHILE(m_test)
  221. #define OPCODES_END \
  222. OPSEXIT:
  223. #define OPCODES_OUT \
  224. OPSOUT:
  225. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  226. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  227. #define OPCODE_BREAK goto OPSEXIT
  228. #define OPCODE_OUT goto OPSOUT
  229. #else
  230. #define OPCODES_TABLE
  231. #define OPCODE(m_op) case m_op:
  232. #define OPCODE_WHILE(m_test) while (m_test)
  233. #define OPCODES_END
  234. #define OPCODES_OUT
  235. #define DISPATCH_OPCODE continue
  236. #define OPCODE_SWITCH(m_test) switch (m_test)
  237. #define OPCODE_BREAK break
  238. #define OPCODE_OUT break
  239. #endif
  240. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  241. OPCODES_TABLE;
  242. if (!_code_ptr) {
  243. return Variant();
  244. }
  245. r_err.error = Callable::CallError::CALL_OK;
  246. Variant self;
  247. Variant static_ref;
  248. Variant retvalue;
  249. Variant *stack = nullptr;
  250. Variant **call_args;
  251. int defarg = 0;
  252. #ifdef DEBUG_ENABLED
  253. //GDScriptLanguage::get_singleton()->calls++;
  254. #endif
  255. uint32_t alloca_size = 0;
  256. GDScript *script;
  257. int ip = 0;
  258. int line = _initial_line;
  259. if (p_state) {
  260. //use existing (supplied) state (awaited)
  261. stack = (Variant *)p_state->stack.ptr();
  262. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  263. line = p_state->line;
  264. ip = p_state->ip;
  265. alloca_size = p_state->stack.size();
  266. script = p_state->script;
  267. p_instance = p_state->instance;
  268. defarg = p_state->defarg;
  269. self = p_state->self;
  270. } else {
  271. if (p_argcount != _argument_count) {
  272. if (p_argcount > _argument_count) {
  273. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  274. r_err.argument = _argument_count;
  275. return Variant();
  276. } else if (p_argcount < _argument_count - _default_arg_count) {
  277. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  278. r_err.argument = _argument_count - _default_arg_count;
  279. return Variant();
  280. } else {
  281. defarg = _argument_count - p_argcount;
  282. }
  283. }
  284. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  285. if (alloca_size) {
  286. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  287. if (_stack_size) {
  288. stack = (Variant *)aptr;
  289. for (int i = 0; i < p_argcount; i++) {
  290. if (!argument_types[i].has_type) {
  291. memnew_placement(&stack[i], Variant(*p_args[i]));
  292. continue;
  293. }
  294. if (!argument_types[i].is_type(*p_args[i], true)) {
  295. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  296. r_err.argument = i;
  297. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  298. return Variant();
  299. }
  300. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  301. Variant arg;
  302. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  303. memnew_placement(&stack[i], Variant(arg));
  304. } else {
  305. memnew_placement(&stack[i], Variant(*p_args[i]));
  306. }
  307. }
  308. for (int i = p_argcount; i < _stack_size; i++) {
  309. memnew_placement(&stack[i], Variant);
  310. }
  311. } else {
  312. stack = nullptr;
  313. }
  314. if (_call_size) {
  315. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  316. } else {
  317. call_args = nullptr;
  318. }
  319. } else {
  320. stack = nullptr;
  321. call_args = nullptr;
  322. }
  323. if (p_instance) {
  324. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  325. self = REF(static_cast<Reference *>(p_instance->owner));
  326. } else {
  327. self = p_instance->owner;
  328. }
  329. script = p_instance->script.ptr();
  330. } else {
  331. script = _script;
  332. }
  333. }
  334. static_ref = script;
  335. String err_text;
  336. #ifdef DEBUG_ENABLED
  337. if (EngineDebugger::is_active()) {
  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 awaited = 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 (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  417. err_text = "Right operand of 'is' is not a class.";
  418. OPCODE_BREAK;
  419. }
  420. #endif
  421. bool extends_ok = false;
  422. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  423. #ifdef DEBUG_ENABLED
  424. bool was_freed;
  425. Object *obj_A = a->get_validated_object_with_check(was_freed);
  426. if (was_freed) {
  427. err_text = "Left operand of 'is' is a previously freed instance.";
  428. OPCODE_BREAK;
  429. }
  430. Object *obj_B = b->get_validated_object_with_check(was_freed);
  431. if (was_freed) {
  432. err_text = "Right operand of 'is' is a previously freed instance.";
  433. OPCODE_BREAK;
  434. }
  435. #else
  436. Object *obj_A = *a;
  437. Object *obj_B = *b;
  438. #endif // DEBUG_ENABLED
  439. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  440. if (scr_B) {
  441. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  442. //in other situation, this shoul return false.
  443. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  444. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  445. //bool found=false;
  446. while (cmp) {
  447. if (cmp == scr_B) {
  448. //inherits from script, all ok
  449. extends_ok = true;
  450. break;
  451. }
  452. cmp = cmp->_base;
  453. }
  454. }
  455. } else {
  456. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  457. #ifdef DEBUG_ENABLED
  458. if (!nc) {
  459. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  460. OPCODE_BREAK;
  461. }
  462. #endif
  463. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  464. }
  465. }
  466. *dst = extends_ok;
  467. ip += 4;
  468. }
  469. DISPATCH_OPCODE;
  470. OPCODE(OPCODE_IS_BUILTIN) {
  471. CHECK_SPACE(4);
  472. GET_VARIANT_PTR(value, 1);
  473. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  474. GET_VARIANT_PTR(dst, 3);
  475. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  476. *dst = value->get_type() == var_type;
  477. ip += 4;
  478. }
  479. DISPATCH_OPCODE;
  480. OPCODE(OPCODE_SET) {
  481. CHECK_SPACE(3);
  482. GET_VARIANT_PTR(dst, 1);
  483. GET_VARIANT_PTR(index, 2);
  484. GET_VARIANT_PTR(value, 3);
  485. bool valid;
  486. dst->set(*index, *value, &valid);
  487. #ifdef DEBUG_ENABLED
  488. if (!valid) {
  489. String v = index->operator String();
  490. if (v != "") {
  491. v = "'" + v + "'";
  492. } else {
  493. v = "of type '" + _get_var_type(index) + "'";
  494. }
  495. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  496. OPCODE_BREAK;
  497. }
  498. #endif
  499. ip += 4;
  500. }
  501. DISPATCH_OPCODE;
  502. OPCODE(OPCODE_GET) {
  503. CHECK_SPACE(3);
  504. GET_VARIANT_PTR(src, 1);
  505. GET_VARIANT_PTR(index, 2);
  506. GET_VARIANT_PTR(dst, 3);
  507. bool valid;
  508. #ifdef DEBUG_ENABLED
  509. //allow better error message in cases where src and dst are the same stack position
  510. Variant ret = src->get(*index, &valid);
  511. #else
  512. *dst = src->get(*index, &valid);
  513. #endif
  514. #ifdef DEBUG_ENABLED
  515. if (!valid) {
  516. String v = index->operator String();
  517. if (v != "") {
  518. v = "'" + v + "'";
  519. } else {
  520. v = "of type '" + _get_var_type(index) + "'";
  521. }
  522. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  523. OPCODE_BREAK;
  524. }
  525. *dst = ret;
  526. #endif
  527. ip += 4;
  528. }
  529. DISPATCH_OPCODE;
  530. OPCODE(OPCODE_SET_NAMED) {
  531. CHECK_SPACE(3);
  532. GET_VARIANT_PTR(dst, 1);
  533. GET_VARIANT_PTR(value, 3);
  534. int indexname = _code_ptr[ip + 2];
  535. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  536. const StringName *index = &_global_names_ptr[indexname];
  537. bool valid;
  538. dst->set_named(*index, *value, valid);
  539. #ifdef DEBUG_ENABLED
  540. if (!valid) {
  541. String err_type;
  542. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  543. OPCODE_BREAK;
  544. }
  545. #endif
  546. ip += 4;
  547. }
  548. DISPATCH_OPCODE;
  549. OPCODE(OPCODE_GET_NAMED) {
  550. CHECK_SPACE(4);
  551. GET_VARIANT_PTR(src, 1);
  552. GET_VARIANT_PTR(dst, 3);
  553. int indexname = _code_ptr[ip + 2];
  554. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  555. const StringName *index = &_global_names_ptr[indexname];
  556. bool valid;
  557. #ifdef DEBUG_ENABLED
  558. //allow better error message in cases where src and dst are the same stack position
  559. Variant ret = src->get_named(*index, valid);
  560. #else
  561. *dst = src->get_named(*index, valid);
  562. #endif
  563. #ifdef DEBUG_ENABLED
  564. if (!valid) {
  565. if (src->has_method(*index)) {
  566. 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() + "\") ?";
  567. } else {
  568. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  569. }
  570. OPCODE_BREAK;
  571. }
  572. *dst = ret;
  573. #endif
  574. ip += 4;
  575. }
  576. DISPATCH_OPCODE;
  577. OPCODE(OPCODE_SET_MEMBER) {
  578. CHECK_SPACE(3);
  579. int indexname = _code_ptr[ip + 1];
  580. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  581. const StringName *index = &_global_names_ptr[indexname];
  582. GET_VARIANT_PTR(src, 2);
  583. bool valid;
  584. #ifndef DEBUG_ENABLED
  585. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  586. #else
  587. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  588. if (!ok) {
  589. err_text = "Internal error setting property: " + String(*index);
  590. OPCODE_BREAK;
  591. } else if (!valid) {
  592. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  593. OPCODE_BREAK;
  594. }
  595. #endif
  596. ip += 3;
  597. }
  598. DISPATCH_OPCODE;
  599. OPCODE(OPCODE_GET_MEMBER) {
  600. CHECK_SPACE(3);
  601. int indexname = _code_ptr[ip + 1];
  602. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  603. const StringName *index = &_global_names_ptr[indexname];
  604. GET_VARIANT_PTR(dst, 2);
  605. #ifndef DEBUG_ENABLED
  606. ClassDB::get_property(p_instance->owner, *index, *dst);
  607. #else
  608. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  609. if (!ok) {
  610. err_text = "Internal error getting property: " + String(*index);
  611. OPCODE_BREAK;
  612. }
  613. #endif
  614. ip += 3;
  615. }
  616. DISPATCH_OPCODE;
  617. OPCODE(OPCODE_ASSIGN) {
  618. CHECK_SPACE(3);
  619. GET_VARIANT_PTR(dst, 1);
  620. GET_VARIANT_PTR(src, 2);
  621. *dst = *src;
  622. ip += 3;
  623. }
  624. DISPATCH_OPCODE;
  625. OPCODE(OPCODE_ASSIGN_TRUE) {
  626. CHECK_SPACE(2);
  627. GET_VARIANT_PTR(dst, 1);
  628. *dst = true;
  629. ip += 2;
  630. }
  631. DISPATCH_OPCODE;
  632. OPCODE(OPCODE_ASSIGN_FALSE) {
  633. CHECK_SPACE(2);
  634. GET_VARIANT_PTR(dst, 1);
  635. *dst = false;
  636. ip += 2;
  637. }
  638. DISPATCH_OPCODE;
  639. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  640. CHECK_SPACE(4);
  641. GET_VARIANT_PTR(dst, 2);
  642. GET_VARIANT_PTR(src, 3);
  643. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  644. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  645. if (src->get_type() != var_type) {
  646. #ifdef DEBUG_ENABLED
  647. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  648. #endif // DEBUG_ENABLED
  649. Callable::CallError ce;
  650. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  651. } else {
  652. #ifdef DEBUG_ENABLED
  653. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  654. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  655. OPCODE_BREAK;
  656. }
  657. } else {
  658. #endif // DEBUG_ENABLED
  659. *dst = *src;
  660. }
  661. ip += 4;
  662. }
  663. DISPATCH_OPCODE;
  664. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  665. CHECK_SPACE(4);
  666. GET_VARIANT_PTR(dst, 2);
  667. GET_VARIANT_PTR(src, 3);
  668. #ifdef DEBUG_ENABLED
  669. GET_VARIANT_PTR(type, 1);
  670. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  671. GD_ERR_BREAK(!nc);
  672. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  673. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  674. "' to a variable of type '" + nc->get_name() + "'.";
  675. OPCODE_BREAK;
  676. }
  677. Object *src_obj = src->operator Object *();
  678. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  679. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  680. "' to a variable of type '" + nc->get_name() + "'.";
  681. OPCODE_BREAK;
  682. }
  683. #endif // DEBUG_ENABLED
  684. *dst = *src;
  685. ip += 4;
  686. }
  687. DISPATCH_OPCODE;
  688. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  689. CHECK_SPACE(4);
  690. GET_VARIANT_PTR(dst, 2);
  691. GET_VARIANT_PTR(src, 3);
  692. #ifdef DEBUG_ENABLED
  693. GET_VARIANT_PTR(type, 1);
  694. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  695. GD_ERR_BREAK(!base_type);
  696. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  697. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  698. OPCODE_BREAK;
  699. }
  700. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  701. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  702. if (!scr_inst) {
  703. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  704. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  705. OPCODE_BREAK;
  706. }
  707. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  708. bool valid = false;
  709. while (src_type) {
  710. if (src_type == base_type) {
  711. valid = true;
  712. break;
  713. }
  714. src_type = src_type->get_base_script().ptr();
  715. }
  716. if (!valid) {
  717. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  718. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  719. OPCODE_BREAK;
  720. }
  721. }
  722. #endif // DEBUG_ENABLED
  723. *dst = *src;
  724. ip += 4;
  725. }
  726. DISPATCH_OPCODE;
  727. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  728. CHECK_SPACE(4);
  729. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  730. GET_VARIANT_PTR(src, 2);
  731. GET_VARIANT_PTR(dst, 3);
  732. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  733. Callable::CallError err;
  734. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  735. #ifdef DEBUG_ENABLED
  736. if (err.error != Callable::CallError::CALL_OK) {
  737. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  738. OPCODE_BREAK;
  739. }
  740. #endif
  741. ip += 4;
  742. }
  743. DISPATCH_OPCODE;
  744. OPCODE(OPCODE_CAST_TO_NATIVE) {
  745. CHECK_SPACE(4);
  746. GET_VARIANT_PTR(to_type, 1);
  747. GET_VARIANT_PTR(src, 2);
  748. GET_VARIANT_PTR(dst, 3);
  749. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  750. GD_ERR_BREAK(!nc);
  751. #ifdef DEBUG_ENABLED
  752. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  753. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  754. OPCODE_BREAK;
  755. }
  756. #endif
  757. Object *src_obj = src->operator Object *();
  758. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  759. *dst = Variant(); // invalid cast, assign NULL
  760. } else {
  761. *dst = *src;
  762. }
  763. ip += 4;
  764. }
  765. DISPATCH_OPCODE;
  766. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  767. CHECK_SPACE(4);
  768. GET_VARIANT_PTR(to_type, 1);
  769. GET_VARIANT_PTR(src, 2);
  770. GET_VARIANT_PTR(dst, 3);
  771. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  772. GD_ERR_BREAK(!base_type);
  773. #ifdef DEBUG_ENABLED
  774. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  775. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  776. OPCODE_BREAK;
  777. }
  778. #endif
  779. bool valid = false;
  780. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  781. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  782. if (scr_inst) {
  783. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  784. while (src_type) {
  785. if (src_type == base_type) {
  786. valid = true;
  787. break;
  788. }
  789. src_type = src_type->get_base_script().ptr();
  790. }
  791. }
  792. }
  793. if (valid) {
  794. *dst = *src; // Valid cast, copy the source object
  795. } else {
  796. *dst = Variant(); // invalid cast, assign NULL
  797. }
  798. ip += 4;
  799. }
  800. DISPATCH_OPCODE;
  801. OPCODE(OPCODE_CONSTRUCT) {
  802. CHECK_SPACE(2);
  803. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  804. int argc = _code_ptr[ip + 2];
  805. CHECK_SPACE(argc + 2);
  806. Variant **argptrs = call_args;
  807. for (int i = 0; i < argc; i++) {
  808. GET_VARIANT_PTR(v, 3 + i);
  809. argptrs[i] = v;
  810. }
  811. GET_VARIANT_PTR(dst, 3 + argc);
  812. Callable::CallError err;
  813. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  814. #ifdef DEBUG_ENABLED
  815. if (err.error != Callable::CallError::CALL_OK) {
  816. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  817. OPCODE_BREAK;
  818. }
  819. #endif
  820. ip += 4 + argc;
  821. //construct a basic type
  822. }
  823. DISPATCH_OPCODE;
  824. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  825. CHECK_SPACE(1);
  826. int argc = _code_ptr[ip + 1];
  827. Array array; //arrays are always shared
  828. array.resize(argc);
  829. CHECK_SPACE(argc + 2);
  830. for (int i = 0; i < argc; i++) {
  831. GET_VARIANT_PTR(v, 2 + i);
  832. array[i] = *v;
  833. }
  834. GET_VARIANT_PTR(dst, 2 + argc);
  835. *dst = array;
  836. ip += 3 + argc;
  837. }
  838. DISPATCH_OPCODE;
  839. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  840. CHECK_SPACE(1);
  841. int argc = _code_ptr[ip + 1];
  842. Dictionary dict; //arrays are always shared
  843. CHECK_SPACE(argc * 2 + 2);
  844. for (int i = 0; i < argc; i++) {
  845. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  846. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  847. dict[*k] = *v;
  848. }
  849. GET_VARIANT_PTR(dst, 2 + argc * 2);
  850. *dst = dict;
  851. ip += 3 + argc * 2;
  852. }
  853. DISPATCH_OPCODE;
  854. OPCODE(OPCODE_CALL_ASYNC)
  855. OPCODE(OPCODE_CALL_RETURN)
  856. OPCODE(OPCODE_CALL) {
  857. CHECK_SPACE(4);
  858. bool call_ret = _code_ptr[ip] != OPCODE_CALL;
  859. #ifdef DEBUG_ENABLED
  860. bool call_async = _code_ptr[ip] == OPCODE_CALL_ASYNC;
  861. #endif
  862. int argc = _code_ptr[ip + 1];
  863. GET_VARIANT_PTR(base, 2);
  864. int nameg = _code_ptr[ip + 3];
  865. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  866. const StringName *methodname = &_global_names_ptr[nameg];
  867. GD_ERR_BREAK(argc < 0);
  868. ip += 4;
  869. CHECK_SPACE(argc + 1);
  870. Variant **argptrs = call_args;
  871. for (int i = 0; i < argc; i++) {
  872. GET_VARIANT_PTR(v, i);
  873. argptrs[i] = v;
  874. }
  875. #ifdef DEBUG_ENABLED
  876. uint64_t call_time = 0;
  877. if (GDScriptLanguage::get_singleton()->profiling) {
  878. call_time = OS::get_singleton()->get_ticks_usec();
  879. }
  880. #endif
  881. Callable::CallError err;
  882. if (call_ret) {
  883. GET_VARIANT_PTR(ret, argc);
  884. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  885. #ifdef DEBUG_ENABLED
  886. if (!call_async && ret->get_type() == Variant::OBJECT) {
  887. // Check if getting a function state without await.
  888. bool was_freed = false;
  889. Object *obj = ret->get_validated_object_with_check(was_freed);
  890. if (was_freed) {
  891. err_text = "Got a freed object as a result of the call.";
  892. OPCODE_BREAK;
  893. }
  894. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  895. err_text = R"(Trying to call an async function without "await".)";
  896. OPCODE_BREAK;
  897. }
  898. }
  899. #endif
  900. } else {
  901. Variant ret;
  902. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  903. }
  904. #ifdef DEBUG_ENABLED
  905. if (GDScriptLanguage::get_singleton()->profiling) {
  906. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  907. }
  908. if (err.error != Callable::CallError::CALL_OK) {
  909. String methodstr = *methodname;
  910. String basestr = _get_var_type(base);
  911. if (methodstr == "call") {
  912. if (argc >= 1) {
  913. methodstr = String(*argptrs[0]) + " (via call)";
  914. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  915. err.argument += 1;
  916. }
  917. }
  918. } else if (methodstr == "free") {
  919. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  920. if (base->is_ref()) {
  921. err_text = "Attempted to free a reference.";
  922. OPCODE_BREAK;
  923. } else if (base->get_type() == Variant::OBJECT) {
  924. err_text = "Attempted to free a locked object (calling or emitting).";
  925. OPCODE_BREAK;
  926. }
  927. }
  928. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  929. if (argc >= 1) {
  930. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  931. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  932. err.argument += 1;
  933. }
  934. }
  935. }
  936. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  937. OPCODE_BREAK;
  938. }
  939. #endif
  940. //_call_func(nullptr,base,*methodname,ip,argc,p_instance,stack);
  941. ip += argc + 1;
  942. }
  943. DISPATCH_OPCODE;
  944. OPCODE(OPCODE_CALL_BUILT_IN) {
  945. CHECK_SPACE(4);
  946. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  947. int argc = _code_ptr[ip + 2];
  948. GD_ERR_BREAK(argc < 0);
  949. ip += 3;
  950. CHECK_SPACE(argc + 1);
  951. Variant **argptrs = call_args;
  952. for (int i = 0; i < argc; i++) {
  953. GET_VARIANT_PTR(v, i);
  954. argptrs[i] = v;
  955. }
  956. GET_VARIANT_PTR(dst, argc);
  957. Callable::CallError err;
  958. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  959. #ifdef DEBUG_ENABLED
  960. if (err.error != Callable::CallError::CALL_OK) {
  961. String methodstr = GDScriptFunctions::get_func_name(func);
  962. if (dst->get_type() == Variant::STRING) {
  963. //call provided error string
  964. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  965. } else {
  966. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  967. }
  968. OPCODE_BREAK;
  969. }
  970. #endif
  971. ip += argc + 1;
  972. }
  973. DISPATCH_OPCODE;
  974. OPCODE(OPCODE_CALL_SELF_BASE) {
  975. CHECK_SPACE(2);
  976. int self_fun = _code_ptr[ip + 1];
  977. #ifdef DEBUG_ENABLED
  978. if (self_fun < 0 || self_fun >= _global_names_count) {
  979. err_text = "compiler bug, function name not found";
  980. OPCODE_BREAK;
  981. }
  982. #endif
  983. const StringName *methodname = &_global_names_ptr[self_fun];
  984. int argc = _code_ptr[ip + 2];
  985. CHECK_SPACE(2 + argc + 1);
  986. Variant **argptrs = call_args;
  987. for (int i = 0; i < argc; i++) {
  988. GET_VARIANT_PTR(v, i + 3);
  989. argptrs[i] = v;
  990. }
  991. GET_VARIANT_PTR(dst, argc + 3);
  992. const GDScript *gds = _script;
  993. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  994. while (gds->base.ptr()) {
  995. gds = gds->base.ptr();
  996. E = gds->member_functions.find(*methodname);
  997. if (E) {
  998. break;
  999. }
  1000. }
  1001. Callable::CallError err;
  1002. if (E) {
  1003. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1004. } else if (gds->native.ptr()) {
  1005. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1006. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1007. if (!mb) {
  1008. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1009. } else {
  1010. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1011. }
  1012. } else {
  1013. err.error = Callable::CallError::CALL_OK;
  1014. }
  1015. } else {
  1016. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1017. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1018. } else {
  1019. err.error = Callable::CallError::CALL_OK;
  1020. }
  1021. }
  1022. if (err.error != Callable::CallError::CALL_OK) {
  1023. String methodstr = *methodname;
  1024. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1025. OPCODE_BREAK;
  1026. }
  1027. ip += 4 + argc;
  1028. }
  1029. DISPATCH_OPCODE;
  1030. OPCODE(OPCODE_AWAIT) {
  1031. CHECK_SPACE(2);
  1032. //do the oneshot connect
  1033. GET_VARIANT_PTR(argobj, 1);
  1034. Signal sig;
  1035. bool is_signal = true;
  1036. {
  1037. Variant result = *argobj;
  1038. if (argobj->get_type() == Variant::OBJECT) {
  1039. bool was_freed = false;
  1040. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1041. if (was_freed) {
  1042. err_text = "Trying to await on a freed object.";
  1043. OPCODE_BREAK;
  1044. }
  1045. // Is this even possible to be null at this point?
  1046. if (obj) {
  1047. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1048. static StringName completed = _scs_create("completed");
  1049. result = Signal(obj, completed);
  1050. }
  1051. }
  1052. }
  1053. if (result.get_type() != Variant::SIGNAL) {
  1054. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1055. // The stack pointer should be the same, so we don't need to set a return value.
  1056. is_signal = false;
  1057. } else {
  1058. sig = result;
  1059. }
  1060. }
  1061. if (is_signal) {
  1062. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1063. gdfs->function = this;
  1064. gdfs->state.stack.resize(alloca_size);
  1065. //copy variant stack
  1066. for (int i = 0; i < _stack_size; i++) {
  1067. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1068. }
  1069. gdfs->state.stack_size = _stack_size;
  1070. gdfs->state.self = self;
  1071. gdfs->state.alloca_size = alloca_size;
  1072. gdfs->state.ip = ip + 2;
  1073. gdfs->state.line = line;
  1074. gdfs->state.script = _script;
  1075. {
  1076. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1077. _script->pending_func_states.add(&gdfs->scripts_list);
  1078. if (p_instance) {
  1079. gdfs->state.instance = p_instance;
  1080. p_instance->pending_func_states.add(&gdfs->instances_list);
  1081. } else {
  1082. gdfs->state.instance = nullptr;
  1083. }
  1084. }
  1085. #ifdef DEBUG_ENABLED
  1086. gdfs->state.function_name = name;
  1087. gdfs->state.script_path = _script->get_path();
  1088. #endif
  1089. gdfs->state.defarg = defarg;
  1090. gdfs->function = this;
  1091. retvalue = gdfs;
  1092. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1093. if (err != OK) {
  1094. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1095. OPCODE_BREAK;
  1096. }
  1097. #ifdef DEBUG_ENABLED
  1098. exit_ok = true;
  1099. awaited = true;
  1100. #endif
  1101. OPCODE_BREAK;
  1102. }
  1103. }
  1104. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1105. OPCODE(OPCODE_AWAIT_RESUME) {
  1106. CHECK_SPACE(2);
  1107. #ifdef DEBUG_ENABLED
  1108. if (!p_state) {
  1109. err_text = ("Invalid Resume (bug?)");
  1110. OPCODE_BREAK;
  1111. }
  1112. #endif
  1113. GET_VARIANT_PTR(result, 1);
  1114. *result = p_state->result;
  1115. ip += 2;
  1116. }
  1117. DISPATCH_OPCODE;
  1118. OPCODE(OPCODE_JUMP) {
  1119. CHECK_SPACE(2);
  1120. int to = _code_ptr[ip + 1];
  1121. GD_ERR_BREAK(to < 0 || to > _code_size);
  1122. ip = to;
  1123. }
  1124. DISPATCH_OPCODE;
  1125. OPCODE(OPCODE_JUMP_IF) {
  1126. CHECK_SPACE(3);
  1127. GET_VARIANT_PTR(test, 1);
  1128. bool result = test->booleanize();
  1129. if (result) {
  1130. int to = _code_ptr[ip + 2];
  1131. GD_ERR_BREAK(to < 0 || to > _code_size);
  1132. ip = to;
  1133. } else {
  1134. ip += 3;
  1135. }
  1136. }
  1137. DISPATCH_OPCODE;
  1138. OPCODE(OPCODE_JUMP_IF_NOT) {
  1139. CHECK_SPACE(3);
  1140. GET_VARIANT_PTR(test, 1);
  1141. bool result = test->booleanize();
  1142. if (!result) {
  1143. int to = _code_ptr[ip + 2];
  1144. GD_ERR_BREAK(to < 0 || to > _code_size);
  1145. ip = to;
  1146. } else {
  1147. ip += 3;
  1148. }
  1149. }
  1150. DISPATCH_OPCODE;
  1151. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1152. CHECK_SPACE(2);
  1153. ip = _default_arg_ptr[defarg];
  1154. }
  1155. DISPATCH_OPCODE;
  1156. OPCODE(OPCODE_RETURN) {
  1157. CHECK_SPACE(2);
  1158. GET_VARIANT_PTR(r, 1);
  1159. retvalue = *r;
  1160. #ifdef DEBUG_ENABLED
  1161. exit_ok = true;
  1162. #endif
  1163. OPCODE_BREAK;
  1164. }
  1165. OPCODE(OPCODE_ITERATE_BEGIN) {
  1166. CHECK_SPACE(8); //space for this a regular iterate
  1167. GET_VARIANT_PTR(counter, 1);
  1168. GET_VARIANT_PTR(container, 2);
  1169. bool valid;
  1170. if (!container->iter_init(*counter, valid)) {
  1171. #ifdef DEBUG_ENABLED
  1172. if (!valid) {
  1173. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1174. OPCODE_BREAK;
  1175. }
  1176. #endif
  1177. int jumpto = _code_ptr[ip + 3];
  1178. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1179. ip = jumpto;
  1180. } else {
  1181. GET_VARIANT_PTR(iterator, 4);
  1182. *iterator = container->iter_get(*counter, valid);
  1183. #ifdef DEBUG_ENABLED
  1184. if (!valid) {
  1185. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1186. OPCODE_BREAK;
  1187. }
  1188. #endif
  1189. ip += 5; //skip regular iterate which is always next
  1190. }
  1191. }
  1192. DISPATCH_OPCODE;
  1193. OPCODE(OPCODE_ITERATE) {
  1194. CHECK_SPACE(4);
  1195. GET_VARIANT_PTR(counter, 1);
  1196. GET_VARIANT_PTR(container, 2);
  1197. bool valid;
  1198. if (!container->iter_next(*counter, valid)) {
  1199. #ifdef DEBUG_ENABLED
  1200. if (!valid) {
  1201. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1202. OPCODE_BREAK;
  1203. }
  1204. #endif
  1205. int jumpto = _code_ptr[ip + 3];
  1206. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1207. ip = jumpto;
  1208. } else {
  1209. GET_VARIANT_PTR(iterator, 4);
  1210. *iterator = container->iter_get(*counter, valid);
  1211. #ifdef DEBUG_ENABLED
  1212. if (!valid) {
  1213. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1214. OPCODE_BREAK;
  1215. }
  1216. #endif
  1217. ip += 5; //loop again
  1218. }
  1219. }
  1220. DISPATCH_OPCODE;
  1221. OPCODE(OPCODE_ASSERT) {
  1222. CHECK_SPACE(3);
  1223. #ifdef DEBUG_ENABLED
  1224. GET_VARIANT_PTR(test, 1);
  1225. bool result = test->booleanize();
  1226. if (!result) {
  1227. String message_str;
  1228. if (_code_ptr[ip + 2] != 0) {
  1229. GET_VARIANT_PTR(message, 2);
  1230. message_str = *message;
  1231. }
  1232. if (message_str.empty()) {
  1233. err_text = "Assertion failed.";
  1234. } else {
  1235. err_text = "Assertion failed: " + message_str;
  1236. }
  1237. OPCODE_BREAK;
  1238. }
  1239. #endif
  1240. ip += 3;
  1241. }
  1242. DISPATCH_OPCODE;
  1243. OPCODE(OPCODE_BREAKPOINT) {
  1244. #ifdef DEBUG_ENABLED
  1245. if (EngineDebugger::is_active()) {
  1246. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1247. }
  1248. #endif
  1249. ip += 1;
  1250. }
  1251. DISPATCH_OPCODE;
  1252. OPCODE(OPCODE_LINE) {
  1253. CHECK_SPACE(2);
  1254. line = _code_ptr[ip + 1];
  1255. ip += 2;
  1256. if (EngineDebugger::is_active()) {
  1257. // line
  1258. bool do_break = false;
  1259. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1260. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  1261. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1262. }
  1263. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  1264. do_break = true;
  1265. }
  1266. }
  1267. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  1268. do_break = true;
  1269. }
  1270. if (do_break) {
  1271. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1272. }
  1273. EngineDebugger::get_singleton()->line_poll();
  1274. }
  1275. }
  1276. DISPATCH_OPCODE;
  1277. OPCODE(OPCODE_END) {
  1278. #ifdef DEBUG_ENABLED
  1279. exit_ok = true;
  1280. #endif
  1281. OPCODE_BREAK;
  1282. }
  1283. #if 0 // Enable for debugging.
  1284. default: {
  1285. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1286. OPCODE_BREAK;
  1287. }
  1288. #endif
  1289. }
  1290. OPCODES_END
  1291. #ifdef DEBUG_ENABLED
  1292. if (exit_ok) {
  1293. OPCODE_OUT;
  1294. }
  1295. //error
  1296. // function, file, line, error, explanation
  1297. String err_file;
  1298. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  1299. err_file = p_instance->script->path;
  1300. } else if (script) {
  1301. err_file = script->path;
  1302. }
  1303. if (err_file == "") {
  1304. err_file = "<built-in>";
  1305. }
  1306. String err_func = name;
  1307. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  1308. err_func = p_instance->script->name + "." + err_func;
  1309. }
  1310. int err_line = line;
  1311. if (err_text == "") {
  1312. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1313. }
  1314. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1315. // debugger break did not happen
  1316. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1317. }
  1318. #endif
  1319. OPCODE_OUT;
  1320. }
  1321. OPCODES_OUT
  1322. #ifdef DEBUG_ENABLED
  1323. if (GDScriptLanguage::get_singleton()->profiling) {
  1324. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1325. profile.total_time += time_taken;
  1326. profile.self_time += time_taken - function_call_time;
  1327. profile.frame_total_time += time_taken;
  1328. profile.frame_self_time += time_taken - function_call_time;
  1329. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1330. }
  1331. // Check if this is the last time the function is resuming from await
  1332. // Will be true if never awaited as well
  1333. // When it's the last resume it will postpone the exit from stack,
  1334. // so the debugger knows which function triggered the resume of the next function (if any)
  1335. if (!p_state || awaited) {
  1336. if (EngineDebugger::is_active()) {
  1337. GDScriptLanguage::get_singleton()->exit_function();
  1338. }
  1339. #endif
  1340. if (_stack_size) {
  1341. //free stack
  1342. for (int i = 0; i < _stack_size; i++) {
  1343. stack[i].~Variant();
  1344. }
  1345. }
  1346. #ifdef DEBUG_ENABLED
  1347. }
  1348. #endif
  1349. return retvalue;
  1350. }
  1351. const int *GDScriptFunction::get_code() const {
  1352. return _code_ptr;
  1353. }
  1354. int GDScriptFunction::get_code_size() const {
  1355. return _code_size;
  1356. }
  1357. Variant GDScriptFunction::get_constant(int p_idx) const {
  1358. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1359. return constants[p_idx];
  1360. }
  1361. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1362. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1363. return global_names[p_idx];
  1364. }
  1365. int GDScriptFunction::get_default_argument_count() const {
  1366. return _default_arg_count;
  1367. }
  1368. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1369. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1370. return default_arguments[p_idx];
  1371. }
  1372. GDScriptDataType GDScriptFunction::get_return_type() const {
  1373. return return_type;
  1374. }
  1375. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1376. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1377. return argument_types[p_idx];
  1378. }
  1379. StringName GDScriptFunction::get_name() const {
  1380. return name;
  1381. }
  1382. int GDScriptFunction::get_max_stack_size() const {
  1383. return _stack_size;
  1384. }
  1385. struct _GDFKC {
  1386. int order;
  1387. List<int> pos;
  1388. };
  1389. struct _GDFKCS {
  1390. int order;
  1391. StringName id;
  1392. int pos;
  1393. bool operator<(const _GDFKCS &p_r) const {
  1394. return order < p_r.order;
  1395. }
  1396. };
  1397. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const {
  1398. int oc = 0;
  1399. Map<StringName, _GDFKC> sdmap;
  1400. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1401. const StackDebug &sd = E->get();
  1402. if (sd.line > p_line) {
  1403. break;
  1404. }
  1405. if (sd.added) {
  1406. if (!sdmap.has(sd.identifier)) {
  1407. _GDFKC d;
  1408. d.order = oc++;
  1409. d.pos.push_back(sd.pos);
  1410. sdmap[sd.identifier] = d;
  1411. } else {
  1412. sdmap[sd.identifier].pos.push_back(sd.pos);
  1413. }
  1414. } else {
  1415. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1416. sdmap[sd.identifier].pos.pop_back();
  1417. if (sdmap[sd.identifier].pos.empty()) {
  1418. sdmap.erase(sd.identifier);
  1419. }
  1420. }
  1421. }
  1422. List<_GDFKCS> stackpositions;
  1423. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1424. _GDFKCS spp;
  1425. spp.id = E->key();
  1426. spp.order = E->get().order;
  1427. spp.pos = E->get().pos.back()->get();
  1428. stackpositions.push_back(spp);
  1429. }
  1430. stackpositions.sort();
  1431. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1432. Pair<StringName, int> p;
  1433. p.first = E->get().id;
  1434. p.second = E->get().pos;
  1435. r_stackvars->push_back(p);
  1436. }
  1437. }
  1438. GDScriptFunction::GDScriptFunction() :
  1439. function_list(this) {
  1440. _stack_size = 0;
  1441. _call_size = 0;
  1442. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1443. name = "<anonymous>";
  1444. #ifdef DEBUG_ENABLED
  1445. _func_cname = nullptr;
  1446. {
  1447. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1448. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1449. }
  1450. profile.call_count = 0;
  1451. profile.self_time = 0;
  1452. profile.total_time = 0;
  1453. profile.frame_call_count = 0;
  1454. profile.frame_self_time = 0;
  1455. profile.frame_total_time = 0;
  1456. profile.last_frame_call_count = 0;
  1457. profile.last_frame_self_time = 0;
  1458. profile.last_frame_total_time = 0;
  1459. #endif
  1460. }
  1461. GDScriptFunction::~GDScriptFunction() {
  1462. #ifdef DEBUG_ENABLED
  1463. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1464. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1465. #endif
  1466. }
  1467. /////////////////////
  1468. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1469. Variant arg;
  1470. r_error.error = Callable::CallError::CALL_OK;
  1471. if (p_argcount == 0) {
  1472. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1473. r_error.argument = 1;
  1474. return Variant();
  1475. } else if (p_argcount == 1) {
  1476. //noooneee
  1477. } else if (p_argcount == 2) {
  1478. arg = *p_args[0];
  1479. } else {
  1480. Array extra_args;
  1481. for (int i = 0; i < p_argcount - 1; i++) {
  1482. extra_args.push_back(*p_args[i]);
  1483. }
  1484. arg = extra_args;
  1485. }
  1486. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1487. if (self.is_null()) {
  1488. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1489. r_error.argument = p_argcount - 1;
  1490. r_error.expected = Variant::OBJECT;
  1491. return Variant();
  1492. }
  1493. return resume(arg);
  1494. }
  1495. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1496. if (function == nullptr) {
  1497. return false;
  1498. }
  1499. if (p_extended_check) {
  1500. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1501. // Script gone?
  1502. if (!scripts_list.in_list()) {
  1503. return false;
  1504. }
  1505. // Class instance gone? (if not static function)
  1506. if (state.instance && !instances_list.in_list()) {
  1507. return false;
  1508. }
  1509. }
  1510. return true;
  1511. }
  1512. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1513. ERR_FAIL_COND_V(!function, Variant());
  1514. {
  1515. MutexLock lock(GDScriptLanguage::singleton->lock);
  1516. if (!scripts_list.in_list()) {
  1517. #ifdef DEBUG_ENABLED
  1518. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
  1519. #else
  1520. return Variant();
  1521. #endif
  1522. }
  1523. if (state.instance && !instances_list.in_list()) {
  1524. #ifdef DEBUG_ENABLED
  1525. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but class instance is gone. At script: " + state.script_path + ":" + itos(state.line));
  1526. #else
  1527. return Variant();
  1528. #endif
  1529. }
  1530. // Do these now to avoid locking again after the call
  1531. scripts_list.remove_from_list();
  1532. instances_list.remove_from_list();
  1533. }
  1534. state.result = p_arg;
  1535. Callable::CallError err;
  1536. Variant ret = function->call(nullptr, nullptr, 0, err, &state);
  1537. bool completed = true;
  1538. // If the return value is a GDScriptFunctionState reference,
  1539. // then the function did awaited again after resuming.
  1540. if (ret.is_ref()) {
  1541. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1542. if (gdfs && gdfs->function == function) {
  1543. completed = false;
  1544. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1545. }
  1546. }
  1547. function = nullptr; //cleaned up;
  1548. state.result = Variant();
  1549. if (completed) {
  1550. if (first_state.is_valid()) {
  1551. first_state->emit_signal("completed", ret);
  1552. } else {
  1553. emit_signal("completed", ret);
  1554. }
  1555. #ifdef DEBUG_ENABLED
  1556. if (EngineDebugger::is_active()) {
  1557. GDScriptLanguage::get_singleton()->exit_function();
  1558. }
  1559. #endif
  1560. }
  1561. return ret;
  1562. }
  1563. void GDScriptFunctionState::_clear_stack() {
  1564. if (state.stack_size) {
  1565. Variant *stack = (Variant *)state.stack.ptr();
  1566. for (int i = 0; i < state.stack_size; i++) {
  1567. stack[i].~Variant();
  1568. }
  1569. state.stack_size = 0;
  1570. }
  1571. }
  1572. void GDScriptFunctionState::_bind_methods() {
  1573. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1574. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1575. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1576. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1577. }
  1578. GDScriptFunctionState::GDScriptFunctionState() :
  1579. scripts_list(this),
  1580. instances_list(this) {
  1581. function = nullptr;
  1582. }
  1583. GDScriptFunctionState::~GDScriptFunctionState() {
  1584. _clear_stack();
  1585. {
  1586. MutexLock lock(GDScriptLanguage::singleton->lock);
  1587. scripts_list.remove_from_list();
  1588. instances_list.remove_from_list();
  1589. }
  1590. }
  1591. #ifdef DEBUG_ENABLED
  1592. static String _get_variant_string(const Variant &p_variant) {
  1593. String txt;
  1594. if (p_variant.get_type() == Variant::STRING) {
  1595. txt = "\"" + String(p_variant) + "\"";
  1596. } else if (p_variant.get_type() == Variant::STRING_NAME) {
  1597. txt = "&\"" + String(p_variant) + "\"";
  1598. } else if (p_variant.get_type() == Variant::NODE_PATH) {
  1599. txt = "^\"" + String(p_variant) + "\"";
  1600. } else if (p_variant.get_type() == Variant::OBJECT) {
  1601. Object *obj = p_variant;
  1602. if (!obj) {
  1603. txt = "null";
  1604. } else {
  1605. GDScriptNativeClass *cls = Object::cast_to<GDScriptNativeClass>(obj);
  1606. if (cls) {
  1607. txt += cls->get_name();
  1608. txt += " (class)";
  1609. } else {
  1610. txt = obj->get_class();
  1611. if (obj->get_script_instance()) {
  1612. txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
  1613. }
  1614. }
  1615. }
  1616. } else {
  1617. txt = p_variant;
  1618. }
  1619. return txt;
  1620. }
  1621. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  1622. int addr = p_address & GDScriptFunction::ADDR_MASK;
  1623. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  1624. case GDScriptFunction::ADDR_TYPE_SELF: {
  1625. return "self";
  1626. } break;
  1627. case GDScriptFunction::ADDR_TYPE_CLASS: {
  1628. return "class";
  1629. } break;
  1630. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  1631. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  1632. } break;
  1633. case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
  1634. return "class_const(" + p_function.get_global_name(addr) + ")";
  1635. } break;
  1636. case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  1637. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  1638. } break;
  1639. case GDScriptFunction::ADDR_TYPE_STACK: {
  1640. return "stack(" + itos(addr) + ")";
  1641. } break;
  1642. case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
  1643. return "var_stack(" + itos(addr) + ")";
  1644. } break;
  1645. case GDScriptFunction::ADDR_TYPE_GLOBAL: {
  1646. return "global(" + _get_variant_string(GDScriptLanguage::get_singleton()->get_global_array()[addr]) + ")";
  1647. } break;
  1648. case GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL: {
  1649. return "named_global(" + p_function.get_global_name(addr) + ")";
  1650. } break;
  1651. case GDScriptFunction::ADDR_TYPE_NIL: {
  1652. return "nil";
  1653. } break;
  1654. }
  1655. return "<err>";
  1656. }
  1657. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  1658. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  1659. for (int ip = 0; ip < _code_size;) {
  1660. StringBuilder text;
  1661. int incr = 0;
  1662. text += " ";
  1663. text += itos(ip);
  1664. text += ": ";
  1665. // This makes the compiler complain if some opcode is unchecked in the switch.
  1666. Opcode code = Opcode(_code_ptr[ip]);
  1667. switch (code) {
  1668. case OPCODE_OPERATOR: {
  1669. int operation = _code_ptr[ip + 1];
  1670. text += "operator ";
  1671. text += DADDR(4);
  1672. text += " = ";
  1673. text += DADDR(2);
  1674. text += " ";
  1675. text += Variant::get_operator_name(Variant::Operator(operation));
  1676. text += " ";
  1677. text += DADDR(3);
  1678. incr += 5;
  1679. } break;
  1680. case OPCODE_EXTENDS_TEST: {
  1681. text += "is object ";
  1682. text += DADDR(3);
  1683. text += " = ";
  1684. text += DADDR(1);
  1685. text += " is ";
  1686. text += DADDR(2);
  1687. incr += 4;
  1688. } break;
  1689. case OPCODE_IS_BUILTIN: {
  1690. text += "is builtin ";
  1691. text += DADDR(3);
  1692. text += " = ";
  1693. text += DADDR(1);
  1694. text += " is ";
  1695. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 2]));
  1696. incr += 4;
  1697. } break;
  1698. case OPCODE_SET: {
  1699. text += "set ";
  1700. text += DADDR(1);
  1701. text += "[";
  1702. text += DADDR(2);
  1703. text += "] = ";
  1704. text += DADDR(3);
  1705. incr += 4;
  1706. } break;
  1707. case OPCODE_GET: {
  1708. text += "get ";
  1709. text += DADDR(3);
  1710. text += " = ";
  1711. text += DADDR(1);
  1712. text += "[";
  1713. text += DADDR(2);
  1714. text += "]";
  1715. incr += 4;
  1716. } break;
  1717. case OPCODE_SET_NAMED: {
  1718. text += "set_named ";
  1719. text += DADDR(1);
  1720. text += "[\"";
  1721. text += _global_names_ptr[_code_ptr[ip + 2]];
  1722. text += "\"] = ";
  1723. text += DADDR(3);
  1724. incr += 4;
  1725. } break;
  1726. case OPCODE_GET_NAMED: {
  1727. text += "get_named ";
  1728. text += DADDR(3);
  1729. text += " = ";
  1730. text += DADDR(1);
  1731. text += "[\"";
  1732. text += _global_names_ptr[_code_ptr[ip + 2]];
  1733. text += "\"]";
  1734. incr += 4;
  1735. } break;
  1736. case OPCODE_SET_MEMBER: {
  1737. text += "set_member ";
  1738. text += "[\"";
  1739. text += _global_names_ptr[_code_ptr[ip + 1]];
  1740. text += "\"] = ";
  1741. text += DADDR(2);
  1742. incr += 3;
  1743. } break;
  1744. case OPCODE_GET_MEMBER: {
  1745. text += "get_member ";
  1746. text += DADDR(2);
  1747. text += " = ";
  1748. text += "[\"";
  1749. text += _global_names_ptr[_code_ptr[ip + 1]];
  1750. text += "\"]";
  1751. incr += 3;
  1752. } break;
  1753. case OPCODE_ASSIGN: {
  1754. text += "assign ";
  1755. text += DADDR(1);
  1756. text += " = ";
  1757. text += DADDR(2);
  1758. incr += 3;
  1759. } break;
  1760. case OPCODE_ASSIGN_TRUE: {
  1761. text += "assign ";
  1762. text += DADDR(1);
  1763. text += " = true";
  1764. incr += 2;
  1765. } break;
  1766. case OPCODE_ASSIGN_FALSE: {
  1767. text += "assign ";
  1768. text += DADDR(1);
  1769. text += " = false";
  1770. incr += 2;
  1771. } break;
  1772. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  1773. text += "assign typed builtin (";
  1774. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 1]);
  1775. text += ") ";
  1776. text += DADDR(2);
  1777. text += " = ";
  1778. text += DADDR(3);
  1779. incr += 4;
  1780. } break;
  1781. case OPCODE_ASSIGN_TYPED_NATIVE: {
  1782. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  1783. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  1784. text += "assign typed native (";
  1785. text += nc->get_name().operator String();
  1786. text += ") ";
  1787. text += DADDR(2);
  1788. text += " = ";
  1789. text += DADDR(3);
  1790. incr += 4;
  1791. } break;
  1792. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  1793. Variant script = _constants_ptr[_code_ptr[ip + 1]];
  1794. Script *sc = Object::cast_to<Script>(script.operator Object *());
  1795. text += "assign typed script (";
  1796. text += sc->get_path();
  1797. text += ") ";
  1798. text += DADDR(2);
  1799. text += " = ";
  1800. text += DADDR(3);
  1801. incr += 4;
  1802. } break;
  1803. case OPCODE_CAST_TO_BUILTIN: {
  1804. text += "cast builtin ";
  1805. text += DADDR(3);
  1806. text += " = ";
  1807. text += DADDR(2);
  1808. text += " as ";
  1809. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  1810. incr += 4;
  1811. } break;
  1812. case OPCODE_CAST_TO_NATIVE: {
  1813. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  1814. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  1815. text += "cast native ";
  1816. text += DADDR(3);
  1817. text += " = ";
  1818. text += DADDR(2);
  1819. text += " as ";
  1820. text += nc->get_name();
  1821. incr += 4;
  1822. } break;
  1823. case OPCODE_CAST_TO_SCRIPT: {
  1824. text += "cast ";
  1825. text += DADDR(3);
  1826. text += " = ";
  1827. text += DADDR(2);
  1828. text += " as ";
  1829. text += DADDR(1);
  1830. incr += 4;
  1831. } break;
  1832. case OPCODE_CONSTRUCT: {
  1833. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  1834. int argc = _code_ptr[ip + 2];
  1835. text += "construct ";
  1836. text += DADDR(3 + argc);
  1837. text += " = ";
  1838. text += Variant::get_type_name(t) + "(";
  1839. for (int i = 0; i < argc; i++) {
  1840. if (i > 0)
  1841. text += ", ";
  1842. text += DADDR(i + 3);
  1843. }
  1844. text += ")";
  1845. incr = 4 + argc;
  1846. } break;
  1847. case OPCODE_CONSTRUCT_ARRAY: {
  1848. int argc = _code_ptr[ip + 1];
  1849. text += " make_array ";
  1850. text += DADDR(2 + argc);
  1851. text += " = [";
  1852. for (int i = 0; i < argc; i++) {
  1853. if (i > 0)
  1854. text += ", ";
  1855. text += DADDR(2 + i);
  1856. }
  1857. text += "]";
  1858. incr += 3 + argc;
  1859. } break;
  1860. case OPCODE_CONSTRUCT_DICTIONARY: {
  1861. int argc = _code_ptr[ip + 1];
  1862. text += "make_dict ";
  1863. text += DADDR(2 + argc * 2);
  1864. text += " = {";
  1865. for (int i = 0; i < argc; i++) {
  1866. if (i > 0)
  1867. text += ", ";
  1868. text += DADDR(2 + i * 2 + 0);
  1869. text += ": ";
  1870. text += DADDR(2 + i * 2 + 1);
  1871. }
  1872. text += "}";
  1873. incr += 3 + argc * 2;
  1874. } break;
  1875. case OPCODE_CALL:
  1876. case OPCODE_CALL_RETURN:
  1877. case OPCODE_CALL_ASYNC: {
  1878. bool ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  1879. bool async = _code_ptr[ip] == OPCODE_CALL_ASYNC;
  1880. if (ret) {
  1881. text += "call-ret ";
  1882. } else if (async) {
  1883. text += "call-async ";
  1884. } else {
  1885. text += "call ";
  1886. }
  1887. int argc = _code_ptr[ip + 1];
  1888. if (ret || async) {
  1889. text += DADDR(4 + argc) + " = ";
  1890. }
  1891. text += DADDR(2) + ".";
  1892. text += String(_global_names_ptr[_code_ptr[ip + 3]]);
  1893. text += "(";
  1894. for (int i = 0; i < argc; i++) {
  1895. if (i > 0)
  1896. text += ", ";
  1897. text += DADDR(4 + i);
  1898. }
  1899. text += ")";
  1900. incr = 5 + argc;
  1901. } break;
  1902. case OPCODE_CALL_BUILT_IN: {
  1903. text += "call-built-in ";
  1904. int argc = _code_ptr[ip + 2];
  1905. text += DADDR(3 + argc) + " = ";
  1906. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 1]));
  1907. text += "(";
  1908. for (int i = 0; i < argc; i++) {
  1909. if (i > 0)
  1910. text += ", ";
  1911. text += DADDR(3 + i);
  1912. }
  1913. text += ")";
  1914. incr = 4 + argc;
  1915. } break;
  1916. case OPCODE_CALL_SELF_BASE: {
  1917. text += "call-self-base ";
  1918. int argc = _code_ptr[ip + 2];
  1919. text += DADDR(3 + argc) + " = ";
  1920. text += _global_names_ptr[_code_ptr[ip + 1]];
  1921. text += "(";
  1922. for (int i = 0; i < argc; i++) {
  1923. if (i > 0)
  1924. text += ", ";
  1925. text += DADDR(3 + i);
  1926. }
  1927. text += ")";
  1928. incr = 4 + argc;
  1929. } break;
  1930. case OPCODE_AWAIT: {
  1931. text += "await ";
  1932. text += DADDR(1);
  1933. incr += 2;
  1934. } break;
  1935. case OPCODE_AWAIT_RESUME: {
  1936. text += "await resume ";
  1937. text += DADDR(1);
  1938. incr = 2;
  1939. } break;
  1940. case OPCODE_JUMP: {
  1941. text += "jump ";
  1942. text += itos(_code_ptr[ip + 1]);
  1943. incr = 2;
  1944. } break;
  1945. case OPCODE_JUMP_IF: {
  1946. text += "jump-if ";
  1947. text += DADDR(1);
  1948. text += " to ";
  1949. text += itos(_code_ptr[ip + 2]);
  1950. incr = 3;
  1951. } break;
  1952. case OPCODE_JUMP_IF_NOT: {
  1953. text += "jump-if-not ";
  1954. text += DADDR(1);
  1955. text += " to ";
  1956. text += itos(_code_ptr[ip + 2]);
  1957. incr = 3;
  1958. } break;
  1959. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  1960. text += "jump-to-default-argument ";
  1961. incr = 1;
  1962. } break;
  1963. case OPCODE_RETURN: {
  1964. text += "return ";
  1965. text += DADDR(1);
  1966. incr = 2;
  1967. } break;
  1968. case OPCODE_ITERATE_BEGIN: {
  1969. text += "for-init ";
  1970. text += DADDR(4);
  1971. text += " in ";
  1972. text += DADDR(2);
  1973. text += " counter ";
  1974. text += DADDR(1);
  1975. text += " end ";
  1976. text += itos(_code_ptr[ip + 3]);
  1977. incr += 5;
  1978. } break;
  1979. case OPCODE_ITERATE: {
  1980. text += "for-loop ";
  1981. text += DADDR(4);
  1982. text += " in ";
  1983. text += DADDR(2);
  1984. text += " counter ";
  1985. text += DADDR(1);
  1986. text += " end ";
  1987. text += itos(_code_ptr[ip + 3]);
  1988. incr += 5;
  1989. } break;
  1990. case OPCODE_LINE: {
  1991. int line = _code_ptr[ip + 1] - 1;
  1992. if (line >= 0 && line < p_code_lines.size()) {
  1993. text += "line ";
  1994. text += itos(line + 1);
  1995. text += ": ";
  1996. text += p_code_lines[line];
  1997. } else {
  1998. text += "";
  1999. }
  2000. incr += 2;
  2001. } break;
  2002. case OPCODE_ASSERT: {
  2003. text += "assert (";
  2004. text += DADDR(1);
  2005. text += ", ";
  2006. text += DADDR(2);
  2007. text += ")";
  2008. incr += 3;
  2009. } break;
  2010. case OPCODE_BREAKPOINT: {
  2011. text += "breakpoint";
  2012. incr += 1;
  2013. } break;
  2014. case OPCODE_END: {
  2015. text += "== END ==";
  2016. incr += 1;
  2017. } break;
  2018. }
  2019. ip += incr;
  2020. if (text.get_string_length() > 0) {
  2021. print_line(text.as_string());
  2022. }
  2023. }
  2024. }
  2025. #endif