gdscript_function.cpp 63 KB

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