gdscript_function.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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 && 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. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  927. if (argc >= 1) {
  928. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  929. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  930. err.argument += 1;
  931. }
  932. }
  933. }
  934. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  935. OPCODE_BREAK;
  936. }
  937. #endif
  938. //_call_func(nullptr,base,*methodname,ip,argc,p_instance,stack);
  939. ip += argc + 1;
  940. }
  941. DISPATCH_OPCODE;
  942. OPCODE(OPCODE_CALL_BUILT_IN) {
  943. CHECK_SPACE(4);
  944. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  945. int argc = _code_ptr[ip + 2];
  946. GD_ERR_BREAK(argc < 0);
  947. ip += 3;
  948. CHECK_SPACE(argc + 1);
  949. Variant **argptrs = call_args;
  950. for (int i = 0; i < argc; i++) {
  951. GET_VARIANT_PTR(v, i);
  952. argptrs[i] = v;
  953. }
  954. GET_VARIANT_PTR(dst, argc);
  955. Callable::CallError err;
  956. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  957. #ifdef DEBUG_ENABLED
  958. if (err.error != Callable::CallError::CALL_OK) {
  959. String methodstr = GDScriptFunctions::get_func_name(func);
  960. if (dst->get_type() == Variant::STRING) {
  961. //call provided error string
  962. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  963. } else {
  964. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  965. }
  966. OPCODE_BREAK;
  967. }
  968. #endif
  969. ip += argc + 1;
  970. }
  971. DISPATCH_OPCODE;
  972. OPCODE(OPCODE_CALL_SELF_BASE) {
  973. CHECK_SPACE(2);
  974. int self_fun = _code_ptr[ip + 1];
  975. #ifdef DEBUG_ENABLED
  976. if (self_fun < 0 || self_fun >= _global_names_count) {
  977. err_text = "compiler bug, function name not found";
  978. OPCODE_BREAK;
  979. }
  980. #endif
  981. const StringName *methodname = &_global_names_ptr[self_fun];
  982. int argc = _code_ptr[ip + 2];
  983. CHECK_SPACE(2 + argc + 1);
  984. Variant **argptrs = call_args;
  985. for (int i = 0; i < argc; i++) {
  986. GET_VARIANT_PTR(v, i + 3);
  987. argptrs[i] = v;
  988. }
  989. GET_VARIANT_PTR(dst, argc + 3);
  990. const GDScript *gds = _script;
  991. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  992. while (gds->base.ptr()) {
  993. gds = gds->base.ptr();
  994. E = gds->member_functions.find(*methodname);
  995. if (E) {
  996. break;
  997. }
  998. }
  999. Callable::CallError err;
  1000. if (E) {
  1001. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1002. } else if (gds->native.ptr()) {
  1003. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1004. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1005. if (!mb) {
  1006. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1007. } else {
  1008. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1009. }
  1010. } else {
  1011. err.error = Callable::CallError::CALL_OK;
  1012. }
  1013. } else {
  1014. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1015. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1016. } else {
  1017. err.error = Callable::CallError::CALL_OK;
  1018. }
  1019. }
  1020. if (err.error != Callable::CallError::CALL_OK) {
  1021. String methodstr = *methodname;
  1022. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1023. OPCODE_BREAK;
  1024. }
  1025. ip += 4 + argc;
  1026. }
  1027. DISPATCH_OPCODE;
  1028. OPCODE(OPCODE_AWAIT) {
  1029. CHECK_SPACE(2);
  1030. //do the oneshot connect
  1031. GET_VARIANT_PTR(argobj, 1);
  1032. Signal sig;
  1033. bool is_signal = true;
  1034. {
  1035. Variant result = *argobj;
  1036. if (argobj->get_type() == Variant::OBJECT) {
  1037. bool was_freed = false;
  1038. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1039. if (was_freed) {
  1040. err_text = "Trying to await on a freed object.";
  1041. OPCODE_BREAK;
  1042. }
  1043. // Is this even possible to be null at this point?
  1044. if (obj) {
  1045. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1046. static StringName completed = _scs_create("completed");
  1047. result = Signal(obj, completed);
  1048. }
  1049. }
  1050. }
  1051. if (result.get_type() != Variant::SIGNAL) {
  1052. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1053. // The stack pointer should be the same, so we don't need to set a return value.
  1054. is_signal = false;
  1055. } else {
  1056. sig = result;
  1057. }
  1058. }
  1059. if (is_signal) {
  1060. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1061. gdfs->function = this;
  1062. gdfs->state.stack.resize(alloca_size);
  1063. //copy variant stack
  1064. for (int i = 0; i < _stack_size; i++) {
  1065. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1066. }
  1067. gdfs->state.stack_size = _stack_size;
  1068. gdfs->state.self = self;
  1069. gdfs->state.alloca_size = alloca_size;
  1070. gdfs->state.ip = ip + 2;
  1071. gdfs->state.line = line;
  1072. gdfs->state.script = _script;
  1073. {
  1074. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1075. _script->pending_func_states.add(&gdfs->scripts_list);
  1076. if (p_instance) {
  1077. gdfs->state.instance = p_instance;
  1078. p_instance->pending_func_states.add(&gdfs->instances_list);
  1079. } else {
  1080. gdfs->state.instance = nullptr;
  1081. }
  1082. }
  1083. #ifdef DEBUG_ENABLED
  1084. gdfs->state.function_name = name;
  1085. gdfs->state.script_path = _script->get_path();
  1086. #endif
  1087. gdfs->state.defarg = defarg;
  1088. gdfs->function = this;
  1089. retvalue = gdfs;
  1090. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1091. if (err != OK) {
  1092. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1093. OPCODE_BREAK;
  1094. }
  1095. #ifdef DEBUG_ENABLED
  1096. exit_ok = true;
  1097. awaited = true;
  1098. #endif
  1099. OPCODE_BREAK;
  1100. }
  1101. }
  1102. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1103. OPCODE(OPCODE_AWAIT_RESUME) {
  1104. CHECK_SPACE(2);
  1105. #ifdef DEBUG_ENABLED
  1106. if (!p_state) {
  1107. err_text = ("Invalid Resume (bug?)");
  1108. OPCODE_BREAK;
  1109. }
  1110. #endif
  1111. GET_VARIANT_PTR(result, 1);
  1112. *result = p_state->result;
  1113. ip += 2;
  1114. }
  1115. DISPATCH_OPCODE;
  1116. OPCODE(OPCODE_JUMP) {
  1117. CHECK_SPACE(2);
  1118. int to = _code_ptr[ip + 1];
  1119. GD_ERR_BREAK(to < 0 || to > _code_size);
  1120. ip = to;
  1121. }
  1122. DISPATCH_OPCODE;
  1123. OPCODE(OPCODE_JUMP_IF) {
  1124. CHECK_SPACE(3);
  1125. GET_VARIANT_PTR(test, 1);
  1126. bool result = test->booleanize();
  1127. if (result) {
  1128. int to = _code_ptr[ip + 2];
  1129. GD_ERR_BREAK(to < 0 || to > _code_size);
  1130. ip = to;
  1131. } else {
  1132. ip += 3;
  1133. }
  1134. }
  1135. DISPATCH_OPCODE;
  1136. OPCODE(OPCODE_JUMP_IF_NOT) {
  1137. CHECK_SPACE(3);
  1138. GET_VARIANT_PTR(test, 1);
  1139. bool result = test->booleanize();
  1140. if (!result) {
  1141. int to = _code_ptr[ip + 2];
  1142. GD_ERR_BREAK(to < 0 || to > _code_size);
  1143. ip = to;
  1144. } else {
  1145. ip += 3;
  1146. }
  1147. }
  1148. DISPATCH_OPCODE;
  1149. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1150. CHECK_SPACE(2);
  1151. ip = _default_arg_ptr[defarg];
  1152. }
  1153. DISPATCH_OPCODE;
  1154. OPCODE(OPCODE_RETURN) {
  1155. CHECK_SPACE(2);
  1156. GET_VARIANT_PTR(r, 1);
  1157. retvalue = *r;
  1158. #ifdef DEBUG_ENABLED
  1159. exit_ok = true;
  1160. #endif
  1161. OPCODE_BREAK;
  1162. }
  1163. OPCODE(OPCODE_ITERATE_BEGIN) {
  1164. CHECK_SPACE(8); //space for this a regular iterate
  1165. GET_VARIANT_PTR(counter, 1);
  1166. GET_VARIANT_PTR(container, 2);
  1167. bool valid;
  1168. if (!container->iter_init(*counter, valid)) {
  1169. #ifdef DEBUG_ENABLED
  1170. if (!valid) {
  1171. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1172. OPCODE_BREAK;
  1173. }
  1174. #endif
  1175. int jumpto = _code_ptr[ip + 3];
  1176. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1177. ip = jumpto;
  1178. } else {
  1179. GET_VARIANT_PTR(iterator, 4);
  1180. *iterator = container->iter_get(*counter, valid);
  1181. #ifdef DEBUG_ENABLED
  1182. if (!valid) {
  1183. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1184. OPCODE_BREAK;
  1185. }
  1186. #endif
  1187. ip += 5; //skip regular iterate which is always next
  1188. }
  1189. }
  1190. DISPATCH_OPCODE;
  1191. OPCODE(OPCODE_ITERATE) {
  1192. CHECK_SPACE(4);
  1193. GET_VARIANT_PTR(counter, 1);
  1194. GET_VARIANT_PTR(container, 2);
  1195. bool valid;
  1196. if (!container->iter_next(*counter, valid)) {
  1197. #ifdef DEBUG_ENABLED
  1198. if (!valid) {
  1199. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1200. OPCODE_BREAK;
  1201. }
  1202. #endif
  1203. int jumpto = _code_ptr[ip + 3];
  1204. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1205. ip = jumpto;
  1206. } else {
  1207. GET_VARIANT_PTR(iterator, 4);
  1208. *iterator = container->iter_get(*counter, valid);
  1209. #ifdef DEBUG_ENABLED
  1210. if (!valid) {
  1211. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1212. OPCODE_BREAK;
  1213. }
  1214. #endif
  1215. ip += 5; //loop again
  1216. }
  1217. }
  1218. DISPATCH_OPCODE;
  1219. OPCODE(OPCODE_ASSERT) {
  1220. CHECK_SPACE(3);
  1221. #ifdef DEBUG_ENABLED
  1222. GET_VARIANT_PTR(test, 1);
  1223. bool result = test->booleanize();
  1224. if (!result) {
  1225. String message_str;
  1226. if (_code_ptr[ip + 2] != 0) {
  1227. GET_VARIANT_PTR(message, 2);
  1228. message_str = *message;
  1229. }
  1230. if (message_str.empty()) {
  1231. err_text = "Assertion failed.";
  1232. } else {
  1233. err_text = "Assertion failed: " + message_str;
  1234. }
  1235. OPCODE_BREAK;
  1236. }
  1237. #endif
  1238. ip += 3;
  1239. }
  1240. DISPATCH_OPCODE;
  1241. OPCODE(OPCODE_BREAKPOINT) {
  1242. #ifdef DEBUG_ENABLED
  1243. if (EngineDebugger::is_active()) {
  1244. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1245. }
  1246. #endif
  1247. ip += 1;
  1248. }
  1249. DISPATCH_OPCODE;
  1250. OPCODE(OPCODE_LINE) {
  1251. CHECK_SPACE(2);
  1252. line = _code_ptr[ip + 1];
  1253. ip += 2;
  1254. if (EngineDebugger::is_active()) {
  1255. // line
  1256. bool do_break = false;
  1257. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1258. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  1259. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1260. }
  1261. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  1262. do_break = true;
  1263. }
  1264. }
  1265. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  1266. do_break = true;
  1267. }
  1268. if (do_break) {
  1269. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1270. }
  1271. EngineDebugger::get_singleton()->line_poll();
  1272. }
  1273. }
  1274. DISPATCH_OPCODE;
  1275. OPCODE(OPCODE_END) {
  1276. #ifdef DEBUG_ENABLED
  1277. exit_ok = true;
  1278. #endif
  1279. OPCODE_BREAK;
  1280. }
  1281. #if 0 // Enable for debugging.
  1282. default: {
  1283. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1284. OPCODE_BREAK;
  1285. }
  1286. #endif
  1287. }
  1288. OPCODES_END
  1289. #ifdef DEBUG_ENABLED
  1290. if (exit_ok) {
  1291. OPCODE_OUT;
  1292. }
  1293. //error
  1294. // function, file, line, error, explanation
  1295. String err_file;
  1296. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  1297. err_file = p_instance->script->path;
  1298. } else if (script) {
  1299. err_file = script->path;
  1300. }
  1301. if (err_file == "") {
  1302. err_file = "<built-in>";
  1303. }
  1304. String err_func = name;
  1305. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  1306. err_func = p_instance->script->name + "." + err_func;
  1307. }
  1308. int err_line = line;
  1309. if (err_text == "") {
  1310. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1311. }
  1312. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1313. // debugger break did not happen
  1314. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1315. }
  1316. #endif
  1317. OPCODE_OUT;
  1318. }
  1319. OPCODES_OUT
  1320. #ifdef DEBUG_ENABLED
  1321. if (GDScriptLanguage::get_singleton()->profiling) {
  1322. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1323. profile.total_time += time_taken;
  1324. profile.self_time += time_taken - function_call_time;
  1325. profile.frame_total_time += time_taken;
  1326. profile.frame_self_time += time_taken - function_call_time;
  1327. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1328. }
  1329. // Check if this is the last time the function is resuming from await
  1330. // Will be true if never awaited as well
  1331. // When it's the last resume it will postpone the exit from stack,
  1332. // so the debugger knows which function triggered the resume of the next function (if any)
  1333. if (!p_state || awaited) {
  1334. if (EngineDebugger::is_active()) {
  1335. GDScriptLanguage::get_singleton()->exit_function();
  1336. }
  1337. #endif
  1338. if (_stack_size) {
  1339. //free stack
  1340. for (int i = 0; i < _stack_size; i++) {
  1341. stack[i].~Variant();
  1342. }
  1343. }
  1344. #ifdef DEBUG_ENABLED
  1345. }
  1346. #endif
  1347. return retvalue;
  1348. }
  1349. const int *GDScriptFunction::get_code() const {
  1350. return _code_ptr;
  1351. }
  1352. int GDScriptFunction::get_code_size() const {
  1353. return _code_size;
  1354. }
  1355. Variant GDScriptFunction::get_constant(int p_idx) const {
  1356. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1357. return constants[p_idx];
  1358. }
  1359. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1360. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1361. return global_names[p_idx];
  1362. }
  1363. int GDScriptFunction::get_default_argument_count() const {
  1364. return _default_arg_count;
  1365. }
  1366. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1367. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1368. return default_arguments[p_idx];
  1369. }
  1370. GDScriptDataType GDScriptFunction::get_return_type() const {
  1371. return return_type;
  1372. }
  1373. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1374. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1375. return argument_types[p_idx];
  1376. }
  1377. StringName GDScriptFunction::get_name() const {
  1378. return name;
  1379. }
  1380. int GDScriptFunction::get_max_stack_size() const {
  1381. return _stack_size;
  1382. }
  1383. struct _GDFKC {
  1384. int order;
  1385. List<int> pos;
  1386. };
  1387. struct _GDFKCS {
  1388. int order;
  1389. StringName id;
  1390. int pos;
  1391. bool operator<(const _GDFKCS &p_r) const {
  1392. return order < p_r.order;
  1393. }
  1394. };
  1395. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const {
  1396. int oc = 0;
  1397. Map<StringName, _GDFKC> sdmap;
  1398. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1399. const StackDebug &sd = E->get();
  1400. if (sd.line > p_line) {
  1401. break;
  1402. }
  1403. if (sd.added) {
  1404. if (!sdmap.has(sd.identifier)) {
  1405. _GDFKC d;
  1406. d.order = oc++;
  1407. d.pos.push_back(sd.pos);
  1408. sdmap[sd.identifier] = d;
  1409. } else {
  1410. sdmap[sd.identifier].pos.push_back(sd.pos);
  1411. }
  1412. } else {
  1413. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1414. sdmap[sd.identifier].pos.pop_back();
  1415. if (sdmap[sd.identifier].pos.empty()) {
  1416. sdmap.erase(sd.identifier);
  1417. }
  1418. }
  1419. }
  1420. List<_GDFKCS> stackpositions;
  1421. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1422. _GDFKCS spp;
  1423. spp.id = E->key();
  1424. spp.order = E->get().order;
  1425. spp.pos = E->get().pos.back()->get();
  1426. stackpositions.push_back(spp);
  1427. }
  1428. stackpositions.sort();
  1429. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1430. Pair<StringName, int> p;
  1431. p.first = E->get().id;
  1432. p.second = E->get().pos;
  1433. r_stackvars->push_back(p);
  1434. }
  1435. }
  1436. GDScriptFunction::GDScriptFunction() :
  1437. function_list(this) {
  1438. _stack_size = 0;
  1439. _call_size = 0;
  1440. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1441. name = "<anonymous>";
  1442. #ifdef DEBUG_ENABLED
  1443. _func_cname = nullptr;
  1444. {
  1445. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1446. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1447. }
  1448. profile.call_count = 0;
  1449. profile.self_time = 0;
  1450. profile.total_time = 0;
  1451. profile.frame_call_count = 0;
  1452. profile.frame_self_time = 0;
  1453. profile.frame_total_time = 0;
  1454. profile.last_frame_call_count = 0;
  1455. profile.last_frame_self_time = 0;
  1456. profile.last_frame_total_time = 0;
  1457. #endif
  1458. }
  1459. GDScriptFunction::~GDScriptFunction() {
  1460. #ifdef DEBUG_ENABLED
  1461. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1462. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1463. #endif
  1464. }
  1465. /////////////////////
  1466. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1467. Variant arg;
  1468. r_error.error = Callable::CallError::CALL_OK;
  1469. if (p_argcount == 0) {
  1470. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1471. r_error.argument = 1;
  1472. return Variant();
  1473. } else if (p_argcount == 1) {
  1474. //noooneee
  1475. } else if (p_argcount == 2) {
  1476. arg = *p_args[0];
  1477. } else {
  1478. Array extra_args;
  1479. for (int i = 0; i < p_argcount - 1; i++) {
  1480. extra_args.push_back(*p_args[i]);
  1481. }
  1482. arg = extra_args;
  1483. }
  1484. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1485. if (self.is_null()) {
  1486. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1487. r_error.argument = p_argcount - 1;
  1488. r_error.expected = Variant::OBJECT;
  1489. return Variant();
  1490. }
  1491. return resume(arg);
  1492. }
  1493. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1494. if (function == nullptr) {
  1495. return false;
  1496. }
  1497. if (p_extended_check) {
  1498. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1499. // Script gone?
  1500. if (!scripts_list.in_list()) {
  1501. return false;
  1502. }
  1503. // Class instance gone? (if not static function)
  1504. if (state.instance && !instances_list.in_list()) {
  1505. return false;
  1506. }
  1507. }
  1508. return true;
  1509. }
  1510. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1511. ERR_FAIL_COND_V(!function, Variant());
  1512. {
  1513. MutexLock lock(GDScriptLanguage::singleton->lock);
  1514. if (!scripts_list.in_list()) {
  1515. #ifdef DEBUG_ENABLED
  1516. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
  1517. #else
  1518. return Variant();
  1519. #endif
  1520. }
  1521. if (state.instance && !instances_list.in_list()) {
  1522. #ifdef DEBUG_ENABLED
  1523. 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));
  1524. #else
  1525. return Variant();
  1526. #endif
  1527. }
  1528. // Do these now to avoid locking again after the call
  1529. scripts_list.remove_from_list();
  1530. instances_list.remove_from_list();
  1531. }
  1532. state.result = p_arg;
  1533. Callable::CallError err;
  1534. Variant ret = function->call(nullptr, nullptr, 0, err, &state);
  1535. bool completed = true;
  1536. // If the return value is a GDScriptFunctionState reference,
  1537. // then the function did awaited again after resuming.
  1538. if (ret.is_ref()) {
  1539. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1540. if (gdfs && gdfs->function == function) {
  1541. completed = false;
  1542. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1543. }
  1544. }
  1545. function = nullptr; //cleaned up;
  1546. state.result = Variant();
  1547. if (completed) {
  1548. if (first_state.is_valid()) {
  1549. first_state->emit_signal("completed", ret);
  1550. } else {
  1551. emit_signal("completed", ret);
  1552. }
  1553. #ifdef DEBUG_ENABLED
  1554. if (EngineDebugger::is_active()) {
  1555. GDScriptLanguage::get_singleton()->exit_function();
  1556. }
  1557. #endif
  1558. }
  1559. return ret;
  1560. }
  1561. void GDScriptFunctionState::_clear_stack() {
  1562. if (state.stack_size) {
  1563. Variant *stack = (Variant *)state.stack.ptr();
  1564. for (int i = 0; i < state.stack_size; i++) {
  1565. stack[i].~Variant();
  1566. }
  1567. state.stack_size = 0;
  1568. }
  1569. }
  1570. void GDScriptFunctionState::_bind_methods() {
  1571. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1572. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1573. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1574. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1575. }
  1576. GDScriptFunctionState::GDScriptFunctionState() :
  1577. scripts_list(this),
  1578. instances_list(this) {
  1579. function = nullptr;
  1580. }
  1581. GDScriptFunctionState::~GDScriptFunctionState() {
  1582. _clear_stack();
  1583. {
  1584. MutexLock lock(GDScriptLanguage::singleton->lock);
  1585. scripts_list.remove_from_list();
  1586. instances_list.remove_from_list();
  1587. }
  1588. }
  1589. #ifdef DEBUG_ENABLED
  1590. static String _get_variant_string(const Variant &p_variant) {
  1591. String txt;
  1592. if (p_variant.get_type() == Variant::STRING) {
  1593. txt = "\"" + String(p_variant) + "\"";
  1594. } else if (p_variant.get_type() == Variant::STRING_NAME) {
  1595. txt = "&\"" + String(p_variant) + "\"";
  1596. } else if (p_variant.get_type() == Variant::NODE_PATH) {
  1597. txt = "^\"" + String(p_variant) + "\"";
  1598. } else if (p_variant.get_type() == Variant::OBJECT) {
  1599. Object *obj = p_variant;
  1600. if (!obj) {
  1601. txt = "null";
  1602. } else {
  1603. GDScriptNativeClass *cls = Object::cast_to<GDScriptNativeClass>(obj);
  1604. if (cls) {
  1605. txt += cls->get_name();
  1606. txt += " (class)";
  1607. } else {
  1608. txt = obj->get_class();
  1609. if (obj->get_script_instance()) {
  1610. txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
  1611. }
  1612. }
  1613. }
  1614. } else {
  1615. txt = p_variant;
  1616. }
  1617. return txt;
  1618. }
  1619. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  1620. int addr = p_address & GDScriptFunction::ADDR_MASK;
  1621. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  1622. case GDScriptFunction::ADDR_TYPE_SELF: {
  1623. return "self";
  1624. } break;
  1625. case GDScriptFunction::ADDR_TYPE_CLASS: {
  1626. return "class";
  1627. } break;
  1628. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  1629. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  1630. } break;
  1631. case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
  1632. return "class_const(" + p_function.get_global_name(addr) + ")";
  1633. } break;
  1634. case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  1635. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  1636. } break;
  1637. case GDScriptFunction::ADDR_TYPE_STACK: {
  1638. return "stack(" + itos(addr) + ")";
  1639. } break;
  1640. case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
  1641. return "var_stack(" + itos(addr) + ")";
  1642. } break;
  1643. case GDScriptFunction::ADDR_TYPE_GLOBAL: {
  1644. return "global(" + _get_variant_string(GDScriptLanguage::get_singleton()->get_global_array()[addr]) + ")";
  1645. } break;
  1646. case GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL: {
  1647. return "named_global(" + p_function.get_global_name(addr) + ")";
  1648. } break;
  1649. case GDScriptFunction::ADDR_TYPE_NIL: {
  1650. return "nil";
  1651. } break;
  1652. }
  1653. return "<err>";
  1654. }
  1655. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  1656. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  1657. for (int ip = 0; ip < _code_size;) {
  1658. StringBuilder text;
  1659. int incr = 0;
  1660. text += " ";
  1661. text += itos(ip);
  1662. text += ": ";
  1663. // This makes the compiler complain if some opcode is unchecked in the switch.
  1664. Opcode code = Opcode(_code_ptr[ip]);
  1665. switch (code) {
  1666. case OPCODE_OPERATOR: {
  1667. int operation = _code_ptr[ip + 1];
  1668. text += "operator ";
  1669. text += DADDR(4);
  1670. text += " = ";
  1671. text += DADDR(2);
  1672. text += " ";
  1673. text += Variant::get_operator_name(Variant::Operator(operation));
  1674. text += " ";
  1675. text += DADDR(3);
  1676. incr += 5;
  1677. } break;
  1678. case OPCODE_EXTENDS_TEST: {
  1679. text += "is object ";
  1680. text += DADDR(3);
  1681. text += " = ";
  1682. text += DADDR(1);
  1683. text += " is ";
  1684. text += DADDR(2);
  1685. incr += 4;
  1686. } break;
  1687. case OPCODE_IS_BUILTIN: {
  1688. text += "is builtin ";
  1689. text += DADDR(3);
  1690. text += " = ";
  1691. text += DADDR(1);
  1692. text += " is ";
  1693. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 2]));
  1694. incr += 4;
  1695. } break;
  1696. case OPCODE_SET: {
  1697. text += "set ";
  1698. text += DADDR(1);
  1699. text += "[";
  1700. text += DADDR(2);
  1701. text += "] = ";
  1702. text += DADDR(3);
  1703. incr += 4;
  1704. } break;
  1705. case OPCODE_GET: {
  1706. text += "get ";
  1707. text += DADDR(3);
  1708. text += " = ";
  1709. text += DADDR(1);
  1710. text += "[";
  1711. text += DADDR(2);
  1712. text += "]";
  1713. incr += 4;
  1714. } break;
  1715. case OPCODE_SET_NAMED: {
  1716. text += "set_named ";
  1717. text += DADDR(1);
  1718. text += "[\"";
  1719. text += _global_names_ptr[_code_ptr[ip + 2]];
  1720. text += "\"] = ";
  1721. text += DADDR(3);
  1722. incr += 4;
  1723. } break;
  1724. case OPCODE_GET_NAMED: {
  1725. text += "get_named ";
  1726. text += DADDR(3);
  1727. text += " = ";
  1728. text += DADDR(1);
  1729. text += "[\"";
  1730. text += _global_names_ptr[_code_ptr[ip + 2]];
  1731. text += "\"]";
  1732. incr += 4;
  1733. } break;
  1734. case OPCODE_SET_MEMBER: {
  1735. text += "set_member ";
  1736. text += "[\"";
  1737. text += _global_names_ptr[_code_ptr[ip + 1]];
  1738. text += "\"] = ";
  1739. text += DADDR(2);
  1740. incr += 3;
  1741. } break;
  1742. case OPCODE_GET_MEMBER: {
  1743. text += "get_member ";
  1744. text += DADDR(2);
  1745. text += " = ";
  1746. text += "[\"";
  1747. text += _global_names_ptr[_code_ptr[ip + 1]];
  1748. text += "\"]";
  1749. incr += 3;
  1750. } break;
  1751. case OPCODE_ASSIGN: {
  1752. text += "assign ";
  1753. text += DADDR(1);
  1754. text += " = ";
  1755. text += DADDR(2);
  1756. incr += 3;
  1757. } break;
  1758. case OPCODE_ASSIGN_TRUE: {
  1759. text += "assign ";
  1760. text += DADDR(1);
  1761. text += " = true";
  1762. incr += 2;
  1763. } break;
  1764. case OPCODE_ASSIGN_FALSE: {
  1765. text += "assign ";
  1766. text += DADDR(1);
  1767. text += " = false";
  1768. incr += 2;
  1769. } break;
  1770. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  1771. text += "assign typed builtin (";
  1772. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 1]);
  1773. text += ") ";
  1774. text += DADDR(2);
  1775. text += " = ";
  1776. text += DADDR(3);
  1777. incr += 4;
  1778. } break;
  1779. case OPCODE_ASSIGN_TYPED_NATIVE: {
  1780. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  1781. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  1782. text += "assign typed native (";
  1783. text += nc->get_name().operator String();
  1784. text += ") ";
  1785. text += DADDR(2);
  1786. text += " = ";
  1787. text += DADDR(3);
  1788. incr += 4;
  1789. } break;
  1790. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  1791. Variant script = _constants_ptr[_code_ptr[ip + 1]];
  1792. Script *sc = Object::cast_to<Script>(script.operator Object *());
  1793. text += "assign typed script (";
  1794. text += sc->get_path();
  1795. text += ") ";
  1796. text += DADDR(2);
  1797. text += " = ";
  1798. text += DADDR(3);
  1799. incr += 4;
  1800. } break;
  1801. case OPCODE_CAST_TO_BUILTIN: {
  1802. text += "cast builtin ";
  1803. text += DADDR(3);
  1804. text += " = ";
  1805. text += DADDR(2);
  1806. text += " as ";
  1807. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  1808. incr += 4;
  1809. } break;
  1810. case OPCODE_CAST_TO_NATIVE: {
  1811. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  1812. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  1813. text += "cast native ";
  1814. text += DADDR(3);
  1815. text += " = ";
  1816. text += DADDR(2);
  1817. text += " as ";
  1818. text += nc->get_name();
  1819. incr += 4;
  1820. } break;
  1821. case OPCODE_CAST_TO_SCRIPT: {
  1822. text += "cast ";
  1823. text += DADDR(3);
  1824. text += " = ";
  1825. text += DADDR(2);
  1826. text += " as ";
  1827. text += DADDR(1);
  1828. incr += 4;
  1829. } break;
  1830. case OPCODE_CONSTRUCT: {
  1831. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  1832. int argc = _code_ptr[ip + 2];
  1833. text += "construct ";
  1834. text += DADDR(3 + argc);
  1835. text += " = ";
  1836. text += Variant::get_type_name(t) + "(";
  1837. for (int i = 0; i < argc; i++) {
  1838. if (i > 0)
  1839. text += ", ";
  1840. text += DADDR(i + 3);
  1841. }
  1842. text += ")";
  1843. incr = 4 + argc;
  1844. } break;
  1845. case OPCODE_CONSTRUCT_ARRAY: {
  1846. int argc = _code_ptr[ip + 1];
  1847. text += " make_array ";
  1848. text += DADDR(2 + argc);
  1849. text += " = [";
  1850. for (int i = 0; i < argc; i++) {
  1851. if (i > 0)
  1852. text += ", ";
  1853. text += DADDR(2 + i);
  1854. }
  1855. text += "]";
  1856. incr += 3 + argc;
  1857. } break;
  1858. case OPCODE_CONSTRUCT_DICTIONARY: {
  1859. int argc = _code_ptr[ip + 1];
  1860. text += "make_dict ";
  1861. text += DADDR(2 + argc * 2);
  1862. text += " = {";
  1863. for (int i = 0; i < argc; i++) {
  1864. if (i > 0)
  1865. text += ", ";
  1866. text += DADDR(2 + i * 2 + 0);
  1867. text += ": ";
  1868. text += DADDR(2 + i * 2 + 1);
  1869. }
  1870. text += "}";
  1871. incr += 3 + argc * 2;
  1872. } break;
  1873. case OPCODE_CALL:
  1874. case OPCODE_CALL_RETURN:
  1875. case OPCODE_CALL_ASYNC: {
  1876. bool ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  1877. bool async = _code_ptr[ip] == OPCODE_CALL_ASYNC;
  1878. if (ret) {
  1879. text += "call-ret ";
  1880. } else if (async) {
  1881. text += "call-async ";
  1882. } else {
  1883. text += "call ";
  1884. }
  1885. int argc = _code_ptr[ip + 1];
  1886. if (ret || async) {
  1887. text += DADDR(4 + argc) + " = ";
  1888. }
  1889. text += DADDR(2) + ".";
  1890. text += String(_global_names_ptr[_code_ptr[ip + 3]]);
  1891. text += "(";
  1892. for (int i = 0; i < argc; i++) {
  1893. if (i > 0)
  1894. text += ", ";
  1895. text += DADDR(4 + i);
  1896. }
  1897. text += ")";
  1898. incr = 5 + argc;
  1899. } break;
  1900. case OPCODE_CALL_BUILT_IN: {
  1901. text += "call-built-in ";
  1902. int argc = _code_ptr[ip + 2];
  1903. text += DADDR(3 + argc) + " = ";
  1904. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 1]));
  1905. text += "(";
  1906. for (int i = 0; i < argc; i++) {
  1907. if (i > 0)
  1908. text += ", ";
  1909. text += DADDR(3 + i);
  1910. }
  1911. text += ")";
  1912. incr = 4 + argc;
  1913. } break;
  1914. case OPCODE_CALL_SELF_BASE: {
  1915. text += "call-self-base ";
  1916. int argc = _code_ptr[ip + 2];
  1917. text += DADDR(3 + argc) + " = ";
  1918. text += _global_names_ptr[_code_ptr[ip + 1]];
  1919. text += "(";
  1920. for (int i = 0; i < argc; i++) {
  1921. if (i > 0)
  1922. text += ", ";
  1923. text += DADDR(3 + i);
  1924. }
  1925. text += ")";
  1926. incr = 4 + argc;
  1927. } break;
  1928. case OPCODE_AWAIT: {
  1929. text += "await ";
  1930. text += DADDR(1);
  1931. incr += 2;
  1932. } break;
  1933. case OPCODE_AWAIT_RESUME: {
  1934. text += "await resume ";
  1935. text += DADDR(1);
  1936. incr = 2;
  1937. } break;
  1938. case OPCODE_JUMP: {
  1939. text += "jump ";
  1940. text += itos(_code_ptr[ip + 1]);
  1941. incr = 2;
  1942. } break;
  1943. case OPCODE_JUMP_IF: {
  1944. text += "jump-if ";
  1945. text += DADDR(1);
  1946. text += " to ";
  1947. text += itos(_code_ptr[ip + 2]);
  1948. incr = 3;
  1949. } break;
  1950. case OPCODE_JUMP_IF_NOT: {
  1951. text += "jump-if-not ";
  1952. text += DADDR(1);
  1953. text += " to ";
  1954. text += itos(_code_ptr[ip + 2]);
  1955. incr = 3;
  1956. } break;
  1957. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  1958. text += "jump-to-default-argument ";
  1959. incr = 1;
  1960. } break;
  1961. case OPCODE_RETURN: {
  1962. text += "return ";
  1963. text += DADDR(1);
  1964. incr = 2;
  1965. } break;
  1966. case OPCODE_ITERATE_BEGIN: {
  1967. text += "for-init ";
  1968. text += DADDR(4);
  1969. text += " in ";
  1970. text += DADDR(2);
  1971. text += " counter ";
  1972. text += DADDR(1);
  1973. text += " end ";
  1974. text += itos(_code_ptr[ip + 3]);
  1975. incr += 5;
  1976. } break;
  1977. case OPCODE_ITERATE: {
  1978. text += "for-loop ";
  1979. text += DADDR(4);
  1980. text += " in ";
  1981. text += DADDR(2);
  1982. text += " counter ";
  1983. text += DADDR(1);
  1984. text += " end ";
  1985. text += itos(_code_ptr[ip + 3]);
  1986. incr += 5;
  1987. } break;
  1988. case OPCODE_LINE: {
  1989. int line = _code_ptr[ip + 1] - 1;
  1990. if (line >= 0 && line < p_code_lines.size()) {
  1991. text += "line ";
  1992. text += itos(line + 1);
  1993. text += ": ";
  1994. text += p_code_lines[line];
  1995. } else {
  1996. text += "";
  1997. }
  1998. incr += 2;
  1999. } break;
  2000. case OPCODE_ASSERT: {
  2001. text += "assert (";
  2002. text += DADDR(1);
  2003. text += ", ";
  2004. text += DADDR(2);
  2005. text += ")";
  2006. incr += 3;
  2007. } break;
  2008. case OPCODE_BREAKPOINT: {
  2009. text += "breakpoint";
  2010. incr += 1;
  2011. } break;
  2012. case OPCODE_END: {
  2013. text += "== END ==";
  2014. incr += 1;
  2015. } break;
  2016. }
  2017. ip += incr;
  2018. if (text.get_string_length() > 0) {
  2019. print_line(text.as_string());
  2020. }
  2021. }
  2022. }
  2023. #endif