gdscript_vm.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. /*************************************************************************/
  2. /* gdscript_vm.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_function.h"
  31. #include "core/os/os.h"
  32. #include "gdscript.h"
  33. #include "gdscript_functions.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant &static_ref, Variant *p_stack, String &r_error) const {
  35. int address = p_address & ADDR_MASK;
  36. //sequential table (jump table generated by compiler)
  37. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  38. case ADDR_TYPE_SELF: {
  39. #ifdef DEBUG_ENABLED
  40. if (unlikely(!p_instance)) {
  41. r_error = "Cannot access self without instance.";
  42. return nullptr;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(!p_instance)) {
  53. r_error = "Cannot access member without instance.";
  54. return nullptr;
  55. }
  56. #endif
  57. //member indexing is O(1)
  58. return &p_instance->members.write[address];
  59. } break;
  60. case ADDR_TYPE_CLASS_CONSTANT: {
  61. //todo change to index!
  62. GDScript *s = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (s) {
  68. GDScript *o = s;
  69. while (o) {
  70. Map<StringName, Variant>::Element *E = o->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. o = o->_owner;
  75. }
  76. s = s->_base;
  77. }
  78. ERR_FAIL_V_MSG(nullptr, "GDScriptCompiler bug.");
  79. } break;
  80. case ADDR_TYPE_LOCAL_CONSTANT: {
  81. #ifdef DEBUG_ENABLED
  82. ERR_FAIL_INDEX_V(address, _constant_count, nullptr);
  83. #endif
  84. return &_constants_ptr[address];
  85. } break;
  86. case ADDR_TYPE_STACK:
  87. case ADDR_TYPE_STACK_VARIABLE: {
  88. #ifdef DEBUG_ENABLED
  89. ERR_FAIL_INDEX_V(address, _stack_size, nullptr);
  90. #endif
  91. return &p_stack[address];
  92. } break;
  93. case ADDR_TYPE_GLOBAL: {
  94. #ifdef DEBUG_ENABLED
  95. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), nullptr);
  96. #endif
  97. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  98. } break;
  99. #ifdef TOOLS_ENABLED
  100. case ADDR_TYPE_NAMED_GLOBAL: {
  101. #ifdef DEBUG_ENABLED
  102. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  103. #endif
  104. StringName id = _global_names_ptr[address];
  105. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  106. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  107. } else {
  108. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  109. return nullptr;
  110. }
  111. } break;
  112. #endif
  113. case ADDR_TYPE_NIL: {
  114. return &nil;
  115. } break;
  116. }
  117. ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode).");
  118. return nullptr;
  119. }
  120. #ifdef DEBUG_ENABLED
  121. static String _get_var_type(const Variant *p_var) {
  122. String basestr;
  123. if (p_var->get_type() == Variant::OBJECT) {
  124. bool was_freed;
  125. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  126. if (!bobj) {
  127. if (was_freed) {
  128. basestr = "null instance";
  129. } else {
  130. basestr = "previously freed";
  131. }
  132. } else {
  133. if (bobj->get_script_instance()) {
  134. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  135. } else {
  136. basestr = bobj->get_class();
  137. }
  138. }
  139. } else {
  140. basestr = Variant::get_type_name(p_var->get_type());
  141. }
  142. return basestr;
  143. }
  144. #endif // DEBUG_ENABLED
  145. String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  146. String err_text;
  147. if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  148. int errorarg = p_err.argument;
  149. // Handle the Object to Object case separately as we don't have further class details.
  150. #ifdef DEBUG_ENABLED
  151. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  152. 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.";
  153. } else
  154. #endif // DEBUG_ENABLED
  155. {
  156. 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)) + ".";
  157. }
  158. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  159. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  160. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  161. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  162. } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  163. err_text = "Invalid call. Nonexistent " + p_where + ".";
  164. } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  165. err_text = "Attempt to call " + p_where + " on a null instance.";
  166. } else {
  167. err_text = "Bug, call error: #" + itos(p_err.error);
  168. }
  169. return err_text;
  170. }
  171. #if defined(__GNUC__)
  172. #define OPCODES_TABLE \
  173. static const void *switch_table_ops[] = { \
  174. &&OPCODE_OPERATOR, \
  175. &&OPCODE_OPERATOR_VALIDATED, \
  176. &&OPCODE_EXTENDS_TEST, \
  177. &&OPCODE_IS_BUILTIN, \
  178. &&OPCODE_SET_KEYED, \
  179. &&OPCODE_SET_KEYED_VALIDATED, \
  180. &&OPCODE_SET_INDEXED_VALIDATED, \
  181. &&OPCODE_GET_KEYED, \
  182. &&OPCODE_GET_KEYED_VALIDATED, \
  183. &&OPCODE_GET_INDEXED_VALIDATED, \
  184. &&OPCODE_SET_NAMED, \
  185. &&OPCODE_SET_NAMED_VALIDATED, \
  186. &&OPCODE_GET_NAMED, \
  187. &&OPCODE_GET_NAMED_VALIDATED, \
  188. &&OPCODE_SET_MEMBER, \
  189. &&OPCODE_GET_MEMBER, \
  190. &&OPCODE_ASSIGN, \
  191. &&OPCODE_ASSIGN_TRUE, \
  192. &&OPCODE_ASSIGN_FALSE, \
  193. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  194. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  195. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  196. &&OPCODE_CAST_TO_BUILTIN, \
  197. &&OPCODE_CAST_TO_NATIVE, \
  198. &&OPCODE_CAST_TO_SCRIPT, \
  199. &&OPCODE_CONSTRUCT, \
  200. &&OPCODE_CONSTRUCT_ARRAY, \
  201. &&OPCODE_CONSTRUCT_DICTIONARY, \
  202. &&OPCODE_CALL, \
  203. &&OPCODE_CALL_RETURN, \
  204. &&OPCODE_CALL_ASYNC, \
  205. &&OPCODE_CALL_BUILT_IN, \
  206. &&OPCODE_CALL_BUILTIN_TYPE_VALIDATED, \
  207. &&OPCODE_CALL_SELF_BASE, \
  208. &&OPCODE_CALL_METHOD_BIND, \
  209. &&OPCODE_CALL_METHOD_BIND_RET, \
  210. &&OPCODE_CALL_PTRCALL_NO_RETURN, \
  211. &&OPCODE_CALL_PTRCALL_BOOL, \
  212. &&OPCODE_CALL_PTRCALL_INT, \
  213. &&OPCODE_CALL_PTRCALL_FLOAT, \
  214. &&OPCODE_CALL_PTRCALL_STRING, \
  215. &&OPCODE_CALL_PTRCALL_VECTOR2, \
  216. &&OPCODE_CALL_PTRCALL_VECTOR2I, \
  217. &&OPCODE_CALL_PTRCALL_RECT2, \
  218. &&OPCODE_CALL_PTRCALL_RECT2I, \
  219. &&OPCODE_CALL_PTRCALL_VECTOR3, \
  220. &&OPCODE_CALL_PTRCALL_VECTOR3I, \
  221. &&OPCODE_CALL_PTRCALL_TRANSFORM2D, \
  222. &&OPCODE_CALL_PTRCALL_PLANE, \
  223. &&OPCODE_CALL_PTRCALL_QUAT, \
  224. &&OPCODE_CALL_PTRCALL_AABB, \
  225. &&OPCODE_CALL_PTRCALL_BASIS, \
  226. &&OPCODE_CALL_PTRCALL_TRANSFORM, \
  227. &&OPCODE_CALL_PTRCALL_COLOR, \
  228. &&OPCODE_CALL_PTRCALL_STRING_NAME, \
  229. &&OPCODE_CALL_PTRCALL_NODE_PATH, \
  230. &&OPCODE_CALL_PTRCALL_RID, \
  231. &&OPCODE_CALL_PTRCALL_OBJECT, \
  232. &&OPCODE_CALL_PTRCALL_CALLABLE, \
  233. &&OPCODE_CALL_PTRCALL_SIGNAL, \
  234. &&OPCODE_CALL_PTRCALL_DICTIONARY, \
  235. &&OPCODE_CALL_PTRCALL_ARRAY, \
  236. &&OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY, \
  237. &&OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY, \
  238. &&OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY, \
  239. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY, \
  240. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY, \
  241. &&OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY, \
  242. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY, \
  243. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY, \
  244. &&OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY, \
  245. &&OPCODE_AWAIT, \
  246. &&OPCODE_AWAIT_RESUME, \
  247. &&OPCODE_JUMP, \
  248. &&OPCODE_JUMP_IF, \
  249. &&OPCODE_JUMP_IF_NOT, \
  250. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  251. &&OPCODE_RETURN, \
  252. &&OPCODE_ITERATE_BEGIN, \
  253. &&OPCODE_ITERATE, \
  254. &&OPCODE_ASSERT, \
  255. &&OPCODE_BREAKPOINT, \
  256. &&OPCODE_LINE, \
  257. &&OPCODE_END \
  258. }; \
  259. 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.");
  260. #define OPCODE(m_op) \
  261. m_op:
  262. #define OPCODE_WHILE(m_test) \
  263. OPSWHILE:
  264. #define OPCODES_END \
  265. OPSEXIT:
  266. #define OPCODES_OUT \
  267. OPSOUT:
  268. #define DISPATCH_OPCODE goto OPSWHILE
  269. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  270. #define OPCODE_BREAK goto OPSEXIT
  271. #define OPCODE_OUT goto OPSOUT
  272. #else
  273. #define OPCODES_TABLE
  274. #define OPCODE(m_op) case m_op:
  275. #define OPCODE_WHILE(m_test) while (m_test)
  276. #define OPCODES_END
  277. #define OPCODES_OUT
  278. #define DISPATCH_OPCODE continue
  279. #define OPCODE_SWITCH(m_test) switch (m_test)
  280. #define OPCODE_BREAK break
  281. #define OPCODE_OUT break
  282. #endif
  283. // Helpers for VariantInternal methods in macros.
  284. #define OP_GET_BOOL get_bool
  285. #define OP_GET_INT get_int
  286. #define OP_GET_FLOAT get_float
  287. #define OP_GET_VECTOR2 get_vector2
  288. #define OP_GET_VECTOR2I get_vector2i
  289. #define OP_GET_VECTOR3 get_vector3
  290. #define OP_GET_VECTOR3I get_vector3i
  291. #define OP_GET_RECT2 get_rect2
  292. #define OP_GET_RECT2I get_rect2i
  293. #define OP_GET_QUAT get_quat
  294. #define OP_GET_COLOR get_color
  295. #define OP_GET_STRING get_string
  296. #define OP_GET_STRING_NAME get_string_name
  297. #define OP_GET_NODE_PATH get_node_path
  298. #define OP_GET_CALLABLE get_callable
  299. #define OP_GET_SIGNAL get_signal
  300. #define OP_GET_ARRAY get_array
  301. #define OP_GET_DICTIONARY get_dictionary
  302. #define OP_GET_PACKED_BYTE_ARRAY get_byte_array
  303. #define OP_GET_PACKED_INT32_ARRAY get_int32_array
  304. #define OP_GET_PACKED_INT64_ARRAY get_int64_array
  305. #define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
  306. #define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
  307. #define OP_GET_PACKED_STRING_ARRAY get_string_array
  308. #define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
  309. #define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
  310. #define OP_GET_PACKED_COLOR_ARRAY get_color_array
  311. #define OP_GET_TRANSFORM get_transform
  312. #define OP_GET_TRANSFORM2D get_transform2d
  313. #define OP_GET_PLANE get_plane
  314. #define OP_GET_AABB get_aabb
  315. #define OP_GET_BASIS get_basis
  316. #define OP_GET_RID get_rid
  317. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  318. OPCODES_TABLE;
  319. if (!_code_ptr) {
  320. return Variant();
  321. }
  322. r_err.error = Callable::CallError::CALL_OK;
  323. Variant self;
  324. Variant static_ref;
  325. Variant retvalue;
  326. Variant *stack = nullptr;
  327. Variant **instruction_args;
  328. const void **call_args_ptr = nullptr;
  329. int defarg = 0;
  330. #ifdef DEBUG_ENABLED
  331. //GDScriptLanguage::get_singleton()->calls++;
  332. #endif
  333. uint32_t alloca_size = 0;
  334. GDScript *script;
  335. int ip = 0;
  336. int line = _initial_line;
  337. if (p_state) {
  338. //use existing (supplied) state (awaited)
  339. stack = (Variant *)p_state->stack.ptr();
  340. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  341. line = p_state->line;
  342. ip = p_state->ip;
  343. alloca_size = p_state->stack.size();
  344. script = p_state->script;
  345. p_instance = p_state->instance;
  346. defarg = p_state->defarg;
  347. self = p_state->self;
  348. } else {
  349. if (p_argcount != _argument_count) {
  350. if (p_argcount > _argument_count) {
  351. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  352. r_err.argument = _argument_count;
  353. return Variant();
  354. } else if (p_argcount < _argument_count - _default_arg_count) {
  355. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  356. r_err.argument = _argument_count - _default_arg_count;
  357. return Variant();
  358. } else {
  359. defarg = _argument_count - p_argcount;
  360. }
  361. }
  362. alloca_size = sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  363. if (alloca_size) {
  364. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  365. if (_stack_size) {
  366. stack = (Variant *)aptr;
  367. for (int i = 0; i < p_argcount; i++) {
  368. if (!argument_types[i].has_type) {
  369. memnew_placement(&stack[i], Variant(*p_args[i]));
  370. continue;
  371. }
  372. if (!argument_types[i].is_type(*p_args[i], true)) {
  373. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  374. r_err.argument = i;
  375. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  376. return Variant();
  377. }
  378. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  379. Variant arg;
  380. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  381. memnew_placement(&stack[i], Variant(arg));
  382. } else {
  383. memnew_placement(&stack[i], Variant(*p_args[i]));
  384. }
  385. }
  386. for (int i = p_argcount; i < _stack_size; i++) {
  387. memnew_placement(&stack[i], Variant);
  388. }
  389. } else {
  390. stack = nullptr;
  391. }
  392. if (_instruction_args_size) {
  393. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  394. } else {
  395. instruction_args = nullptr;
  396. }
  397. } else {
  398. stack = nullptr;
  399. instruction_args = nullptr;
  400. }
  401. if (p_instance) {
  402. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  403. self = REF(static_cast<Reference *>(p_instance->owner));
  404. } else {
  405. self = p_instance->owner;
  406. }
  407. script = p_instance->script.ptr();
  408. } else {
  409. script = _script;
  410. }
  411. }
  412. if (_ptrcall_args_size) {
  413. call_args_ptr = (const void **)alloca(_ptrcall_args_size * sizeof(void *));
  414. } else {
  415. call_args_ptr = nullptr;
  416. }
  417. static_ref = script;
  418. String err_text;
  419. #ifdef DEBUG_ENABLED
  420. if (EngineDebugger::is_active()) {
  421. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  422. }
  423. #define GD_ERR_BREAK(m_cond) \
  424. { \
  425. if (unlikely(m_cond)) { \
  426. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  427. OPCODE_BREAK; \
  428. } \
  429. }
  430. #define CHECK_SPACE(m_space) \
  431. GD_ERR_BREAK((ip + m_space) > _code_size)
  432. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  433. Variant *m_v; \
  434. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  435. if (unlikely(!m_v)) \
  436. OPCODE_BREAK;
  437. #else
  438. #define GD_ERR_BREAK(m_cond)
  439. #define CHECK_SPACE(m_space)
  440. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  441. Variant *m_v; \
  442. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  443. #endif
  444. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  445. Variant *m_v = instruction_args[m_idx]
  446. #ifdef DEBUG_ENABLED
  447. uint64_t function_start_time = 0;
  448. uint64_t function_call_time = 0;
  449. if (GDScriptLanguage::get_singleton()->profiling) {
  450. function_start_time = OS::get_singleton()->get_ticks_usec();
  451. function_call_time = 0;
  452. profile.call_count++;
  453. profile.frame_call_count++;
  454. }
  455. bool exit_ok = false;
  456. bool awaited = false;
  457. #endif
  458. #ifdef DEBUG_ENABLED
  459. OPCODE_WHILE(ip < _code_size) {
  460. int last_opcode = _code_ptr[ip];
  461. #else
  462. OPCODE_WHILE(true) {
  463. #endif
  464. // Load arguments for the instruction before each instruction.
  465. int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS;
  466. for (int i = 0; i < instr_arg_count; i++) {
  467. GET_VARIANT_PTR(v, i + 1);
  468. instruction_args[i] = v;
  469. }
  470. OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) {
  471. OPCODE(OPCODE_OPERATOR) {
  472. CHECK_SPACE(5);
  473. bool valid;
  474. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  475. GD_ERR_BREAK(op >= Variant::OP_MAX);
  476. GET_INSTRUCTION_ARG(a, 0);
  477. GET_INSTRUCTION_ARG(b, 1);
  478. GET_INSTRUCTION_ARG(dst, 2);
  479. #ifdef DEBUG_ENABLED
  480. Variant ret;
  481. Variant::evaluate(op, *a, *b, ret, valid);
  482. #else
  483. Variant::evaluate(op, *a, *b, *dst, valid);
  484. #endif
  485. #ifdef DEBUG_ENABLED
  486. if (!valid) {
  487. if (ret.get_type() == Variant::STRING) {
  488. //return a string when invalid with the error
  489. err_text = ret;
  490. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  491. } else {
  492. 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) + "'.";
  493. }
  494. OPCODE_BREAK;
  495. }
  496. *dst = ret;
  497. #endif
  498. ip += 5;
  499. }
  500. DISPATCH_OPCODE;
  501. OPCODE(OPCODE_OPERATOR_VALIDATED) {
  502. CHECK_SPACE(5);
  503. int operator_idx = _code_ptr[ip + 4];
  504. GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
  505. Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
  506. GET_INSTRUCTION_ARG(a, 0);
  507. GET_INSTRUCTION_ARG(b, 1);
  508. GET_INSTRUCTION_ARG(dst, 2);
  509. operator_func(a, b, dst);
  510. ip += 5;
  511. }
  512. DISPATCH_OPCODE;
  513. OPCODE(OPCODE_EXTENDS_TEST) {
  514. CHECK_SPACE(4);
  515. GET_INSTRUCTION_ARG(a, 0);
  516. GET_INSTRUCTION_ARG(b, 1);
  517. GET_INSTRUCTION_ARG(dst, 2);
  518. #ifdef DEBUG_ENABLED
  519. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  520. err_text = "Right operand of 'is' is not a class.";
  521. OPCODE_BREAK;
  522. }
  523. #endif
  524. bool extends_ok = false;
  525. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  526. #ifdef DEBUG_ENABLED
  527. bool was_freed;
  528. Object *obj_A = a->get_validated_object_with_check(was_freed);
  529. if (was_freed) {
  530. err_text = "Left operand of 'is' is a previously freed instance.";
  531. OPCODE_BREAK;
  532. }
  533. Object *obj_B = b->get_validated_object_with_check(was_freed);
  534. if (was_freed) {
  535. err_text = "Right operand of 'is' is a previously freed instance.";
  536. OPCODE_BREAK;
  537. }
  538. #else
  539. Object *obj_A = *a;
  540. Object *obj_B = *b;
  541. #endif // DEBUG_ENABLED
  542. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  543. if (scr_B) {
  544. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  545. //in other situation, this shoul return false.
  546. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  547. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  548. //bool found=false;
  549. while (cmp) {
  550. if (cmp == scr_B) {
  551. //inherits from script, all ok
  552. extends_ok = true;
  553. break;
  554. }
  555. cmp = cmp->_base;
  556. }
  557. }
  558. } else {
  559. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  560. #ifdef DEBUG_ENABLED
  561. if (!nc) {
  562. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  563. OPCODE_BREAK;
  564. }
  565. #endif
  566. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  567. }
  568. }
  569. *dst = extends_ok;
  570. ip += 4;
  571. }
  572. DISPATCH_OPCODE;
  573. OPCODE(OPCODE_IS_BUILTIN) {
  574. CHECK_SPACE(4);
  575. GET_INSTRUCTION_ARG(value, 0);
  576. GET_INSTRUCTION_ARG(dst, 1);
  577. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  578. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  579. *dst = value->get_type() == var_type;
  580. ip += 4;
  581. }
  582. DISPATCH_OPCODE;
  583. OPCODE(OPCODE_SET_KEYED) {
  584. CHECK_SPACE(3);
  585. GET_INSTRUCTION_ARG(dst, 0);
  586. GET_INSTRUCTION_ARG(index, 1);
  587. GET_INSTRUCTION_ARG(value, 2);
  588. bool valid;
  589. dst->set(*index, *value, &valid);
  590. #ifdef DEBUG_ENABLED
  591. if (!valid) {
  592. String v = index->operator String();
  593. if (v != "") {
  594. v = "'" + v + "'";
  595. } else {
  596. v = "of type '" + _get_var_type(index) + "'";
  597. }
  598. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  599. OPCODE_BREAK;
  600. }
  601. #endif
  602. ip += 4;
  603. }
  604. DISPATCH_OPCODE;
  605. OPCODE(OPCODE_SET_KEYED_VALIDATED) {
  606. CHECK_SPACE(4);
  607. GET_INSTRUCTION_ARG(dst, 0);
  608. GET_INSTRUCTION_ARG(index, 1);
  609. GET_INSTRUCTION_ARG(value, 2);
  610. int index_setter = _code_ptr[ip + 4];
  611. GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
  612. const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
  613. bool valid;
  614. setter(dst, index, value, valid);
  615. #ifdef DEBUG_ENABLED
  616. if (!valid) {
  617. String v = index->operator String();
  618. if (v != "") {
  619. v = "'" + v + "'";
  620. } else {
  621. v = "of type '" + _get_var_type(index) + "'";
  622. }
  623. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  624. OPCODE_BREAK;
  625. }
  626. #endif
  627. ip += 5;
  628. }
  629. DISPATCH_OPCODE;
  630. OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
  631. CHECK_SPACE(4);
  632. GET_INSTRUCTION_ARG(dst, 0);
  633. GET_INSTRUCTION_ARG(index, 1);
  634. GET_INSTRUCTION_ARG(value, 2);
  635. int index_setter = _code_ptr[ip + 4];
  636. GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
  637. const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
  638. int64_t int_index = *VariantInternal::get_int(index);
  639. bool oob;
  640. setter(dst, int_index, value, oob);
  641. #ifdef DEBUG_ENABLED
  642. if (oob) {
  643. String v = index->operator String();
  644. if (v != "") {
  645. v = "'" + v + "'";
  646. } else {
  647. v = "of type '" + _get_var_type(index) + "'";
  648. }
  649. err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
  650. OPCODE_BREAK;
  651. }
  652. #endif
  653. ip += 5;
  654. }
  655. DISPATCH_OPCODE;
  656. OPCODE(OPCODE_GET_KEYED) {
  657. CHECK_SPACE(3);
  658. GET_INSTRUCTION_ARG(src, 0);
  659. GET_INSTRUCTION_ARG(index, 1);
  660. GET_INSTRUCTION_ARG(dst, 2);
  661. bool valid;
  662. #ifdef DEBUG_ENABLED
  663. // Allow better error message in cases where src and dst are the same stack position.
  664. Variant ret = src->get(*index, &valid);
  665. #else
  666. *dst = src->get(*index, &valid);
  667. #endif
  668. #ifdef DEBUG_ENABLED
  669. if (!valid) {
  670. String v = index->operator String();
  671. if (v != "") {
  672. v = "'" + v + "'";
  673. } else {
  674. v = "of type '" + _get_var_type(index) + "'";
  675. }
  676. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  677. OPCODE_BREAK;
  678. }
  679. *dst = ret;
  680. #endif
  681. ip += 4;
  682. }
  683. DISPATCH_OPCODE;
  684. OPCODE(OPCODE_GET_KEYED_VALIDATED) {
  685. CHECK_SPACE(4);
  686. GET_INSTRUCTION_ARG(src, 0);
  687. GET_INSTRUCTION_ARG(key, 1);
  688. GET_INSTRUCTION_ARG(dst, 2);
  689. int index_getter = _code_ptr[ip + 4];
  690. GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
  691. const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
  692. bool valid;
  693. #ifdef DEBUG_ENABLED
  694. // Allow better error message in cases where src and dst are the same stack position.
  695. Variant ret;
  696. getter(src, key, &ret, valid);
  697. #else
  698. getter(src, key, dst, valid);
  699. #endif
  700. #ifdef DEBUG_ENABLED
  701. if (!valid) {
  702. String v = key->operator String();
  703. if (v != "") {
  704. v = "'" + v + "'";
  705. } else {
  706. v = "of type '" + _get_var_type(key) + "'";
  707. }
  708. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  709. OPCODE_BREAK;
  710. }
  711. *dst = ret;
  712. #endif
  713. ip += 5;
  714. }
  715. DISPATCH_OPCODE;
  716. OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
  717. CHECK_SPACE(4);
  718. GET_INSTRUCTION_ARG(src, 0);
  719. GET_INSTRUCTION_ARG(index, 1);
  720. GET_INSTRUCTION_ARG(dst, 2);
  721. int index_getter = _code_ptr[ip + 4];
  722. GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
  723. const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
  724. int64_t int_index = *VariantInternal::get_int(index);
  725. bool oob;
  726. getter(src, int_index, dst, oob);
  727. #ifdef DEBUG_ENABLED
  728. if (oob) {
  729. String v = index->operator String();
  730. if (v != "") {
  731. v = "'" + v + "'";
  732. } else {
  733. v = "of type '" + _get_var_type(index) + "'";
  734. }
  735. err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
  736. OPCODE_BREAK;
  737. }
  738. #endif
  739. ip += 5;
  740. }
  741. DISPATCH_OPCODE;
  742. OPCODE(OPCODE_SET_NAMED) {
  743. CHECK_SPACE(3);
  744. GET_INSTRUCTION_ARG(dst, 0);
  745. GET_INSTRUCTION_ARG(value, 1);
  746. int indexname = _code_ptr[ip + 3];
  747. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  748. const StringName *index = &_global_names_ptr[indexname];
  749. bool valid;
  750. dst->set_named(*index, *value, valid);
  751. #ifdef DEBUG_ENABLED
  752. if (!valid) {
  753. String err_type;
  754. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  755. OPCODE_BREAK;
  756. }
  757. #endif
  758. ip += 4;
  759. }
  760. DISPATCH_OPCODE;
  761. OPCODE(OPCODE_SET_NAMED_VALIDATED) {
  762. CHECK_SPACE(3);
  763. GET_INSTRUCTION_ARG(dst, 0);
  764. GET_INSTRUCTION_ARG(value, 1);
  765. int index_setter = _code_ptr[ip + 3];
  766. GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
  767. const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
  768. setter(dst, value);
  769. ip += 4;
  770. }
  771. DISPATCH_OPCODE;
  772. OPCODE(OPCODE_GET_NAMED) {
  773. CHECK_SPACE(4);
  774. GET_INSTRUCTION_ARG(src, 0);
  775. GET_INSTRUCTION_ARG(dst, 1);
  776. int indexname = _code_ptr[ip + 3];
  777. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  778. const StringName *index = &_global_names_ptr[indexname];
  779. bool valid;
  780. #ifdef DEBUG_ENABLED
  781. //allow better error message in cases where src and dst are the same stack position
  782. Variant ret = src->get_named(*index, valid);
  783. #else
  784. *dst = src->get_named(*index, valid);
  785. #endif
  786. #ifdef DEBUG_ENABLED
  787. if (!valid) {
  788. if (src->has_method(*index)) {
  789. 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() + "\") ?";
  790. } else {
  791. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  792. }
  793. OPCODE_BREAK;
  794. }
  795. *dst = ret;
  796. #endif
  797. ip += 4;
  798. }
  799. DISPATCH_OPCODE;
  800. OPCODE(OPCODE_GET_NAMED_VALIDATED) {
  801. CHECK_SPACE(3);
  802. GET_INSTRUCTION_ARG(src, 0);
  803. GET_INSTRUCTION_ARG(dst, 1);
  804. int index_getter = _code_ptr[ip + 3];
  805. GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
  806. const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
  807. getter(src, dst);
  808. ip += 4;
  809. }
  810. DISPATCH_OPCODE;
  811. OPCODE(OPCODE_SET_MEMBER) {
  812. CHECK_SPACE(3);
  813. GET_INSTRUCTION_ARG(src, 0);
  814. int indexname = _code_ptr[ip + 2];
  815. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  816. const StringName *index = &_global_names_ptr[indexname];
  817. bool valid;
  818. #ifndef DEBUG_ENABLED
  819. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  820. #else
  821. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  822. if (!ok) {
  823. err_text = "Internal error setting property: " + String(*index);
  824. OPCODE_BREAK;
  825. } else if (!valid) {
  826. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  827. OPCODE_BREAK;
  828. }
  829. #endif
  830. ip += 3;
  831. }
  832. DISPATCH_OPCODE;
  833. OPCODE(OPCODE_GET_MEMBER) {
  834. CHECK_SPACE(3);
  835. GET_INSTRUCTION_ARG(dst, 0);
  836. int indexname = _code_ptr[ip + 2];
  837. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  838. const StringName *index = &_global_names_ptr[indexname];
  839. #ifndef DEBUG_ENABLED
  840. ClassDB::get_property(p_instance->owner, *index, *dst);
  841. #else
  842. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  843. if (!ok) {
  844. err_text = "Internal error getting property: " + String(*index);
  845. OPCODE_BREAK;
  846. }
  847. #endif
  848. ip += 3;
  849. }
  850. DISPATCH_OPCODE;
  851. OPCODE(OPCODE_ASSIGN) {
  852. CHECK_SPACE(3);
  853. GET_INSTRUCTION_ARG(dst, 0);
  854. GET_INSTRUCTION_ARG(src, 1);
  855. *dst = *src;
  856. ip += 3;
  857. }
  858. DISPATCH_OPCODE;
  859. OPCODE(OPCODE_ASSIGN_TRUE) {
  860. CHECK_SPACE(2);
  861. GET_INSTRUCTION_ARG(dst, 0);
  862. *dst = true;
  863. ip += 2;
  864. }
  865. DISPATCH_OPCODE;
  866. OPCODE(OPCODE_ASSIGN_FALSE) {
  867. CHECK_SPACE(2);
  868. GET_INSTRUCTION_ARG(dst, 0);
  869. *dst = false;
  870. ip += 2;
  871. }
  872. DISPATCH_OPCODE;
  873. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  874. CHECK_SPACE(4);
  875. GET_INSTRUCTION_ARG(dst, 0);
  876. GET_INSTRUCTION_ARG(src, 1);
  877. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  878. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  879. if (src->get_type() != var_type) {
  880. #ifdef DEBUG_ENABLED
  881. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  882. #endif // DEBUG_ENABLED
  883. Callable::CallError ce;
  884. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  885. } else {
  886. #ifdef DEBUG_ENABLED
  887. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  888. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  889. OPCODE_BREAK;
  890. }
  891. } else {
  892. #endif // DEBUG_ENABLED
  893. *dst = *src;
  894. }
  895. ip += 4;
  896. }
  897. DISPATCH_OPCODE;
  898. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  899. CHECK_SPACE(4);
  900. GET_INSTRUCTION_ARG(dst, 0);
  901. GET_INSTRUCTION_ARG(src, 1);
  902. #ifdef DEBUG_ENABLED
  903. GET_INSTRUCTION_ARG(type, 2);
  904. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  905. GD_ERR_BREAK(!nc);
  906. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  907. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  908. "' to a variable of type '" + nc->get_name() + "'.";
  909. OPCODE_BREAK;
  910. }
  911. Object *src_obj = src->operator Object *();
  912. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  913. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  914. "' to a variable of type '" + nc->get_name() + "'.";
  915. OPCODE_BREAK;
  916. }
  917. #endif // DEBUG_ENABLED
  918. *dst = *src;
  919. ip += 4;
  920. }
  921. DISPATCH_OPCODE;
  922. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  923. CHECK_SPACE(4);
  924. GET_INSTRUCTION_ARG(dst, 0);
  925. GET_INSTRUCTION_ARG(src, 1);
  926. #ifdef DEBUG_ENABLED
  927. GET_INSTRUCTION_ARG(type, 2);
  928. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  929. GD_ERR_BREAK(!base_type);
  930. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  931. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  932. OPCODE_BREAK;
  933. }
  934. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  935. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  936. if (!scr_inst) {
  937. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  938. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  939. OPCODE_BREAK;
  940. }
  941. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  942. bool valid = false;
  943. while (src_type) {
  944. if (src_type == base_type) {
  945. valid = true;
  946. break;
  947. }
  948. src_type = src_type->get_base_script().ptr();
  949. }
  950. if (!valid) {
  951. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  952. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  953. OPCODE_BREAK;
  954. }
  955. }
  956. #endif // DEBUG_ENABLED
  957. *dst = *src;
  958. ip += 4;
  959. }
  960. DISPATCH_OPCODE;
  961. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  962. CHECK_SPACE(4);
  963. GET_INSTRUCTION_ARG(src, 0);
  964. GET_INSTRUCTION_ARG(dst, 1);
  965. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  966. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  967. Callable::CallError err;
  968. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  969. #ifdef DEBUG_ENABLED
  970. if (err.error != Callable::CallError::CALL_OK) {
  971. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  972. OPCODE_BREAK;
  973. }
  974. #endif
  975. ip += 4;
  976. }
  977. DISPATCH_OPCODE;
  978. OPCODE(OPCODE_CAST_TO_NATIVE) {
  979. CHECK_SPACE(4);
  980. GET_INSTRUCTION_ARG(src, 0);
  981. GET_INSTRUCTION_ARG(dst, 1);
  982. GET_INSTRUCTION_ARG(to_type, 2);
  983. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  984. GD_ERR_BREAK(!nc);
  985. #ifdef DEBUG_ENABLED
  986. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  987. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  988. OPCODE_BREAK;
  989. }
  990. #endif
  991. Object *src_obj = src->operator Object *();
  992. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  993. *dst = Variant(); // invalid cast, assign NULL
  994. } else {
  995. *dst = *src;
  996. }
  997. ip += 4;
  998. }
  999. DISPATCH_OPCODE;
  1000. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1001. CHECK_SPACE(4);
  1002. GET_INSTRUCTION_ARG(src, 0);
  1003. GET_INSTRUCTION_ARG(dst, 1);
  1004. GET_INSTRUCTION_ARG(to_type, 2);
  1005. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1006. GD_ERR_BREAK(!base_type);
  1007. #ifdef DEBUG_ENABLED
  1008. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1009. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1010. OPCODE_BREAK;
  1011. }
  1012. #endif
  1013. bool valid = false;
  1014. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1015. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1016. if (scr_inst) {
  1017. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1018. while (src_type) {
  1019. if (src_type == base_type) {
  1020. valid = true;
  1021. break;
  1022. }
  1023. src_type = src_type->get_base_script().ptr();
  1024. }
  1025. }
  1026. }
  1027. if (valid) {
  1028. *dst = *src; // Valid cast, copy the source object
  1029. } else {
  1030. *dst = Variant(); // invalid cast, assign NULL
  1031. }
  1032. ip += 4;
  1033. }
  1034. DISPATCH_OPCODE;
  1035. OPCODE(OPCODE_CONSTRUCT) {
  1036. CHECK_SPACE(2 + instr_arg_count);
  1037. ip += instr_arg_count;
  1038. int argc = _code_ptr[ip + 1];
  1039. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1040. Variant **argptrs = instruction_args;
  1041. GET_INSTRUCTION_ARG(dst, argc);
  1042. Callable::CallError err;
  1043. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1044. #ifdef DEBUG_ENABLED
  1045. if (err.error != Callable::CallError::CALL_OK) {
  1046. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  1047. OPCODE_BREAK;
  1048. }
  1049. #endif
  1050. ip += 3;
  1051. }
  1052. DISPATCH_OPCODE;
  1053. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1054. CHECK_SPACE(1 + instr_arg_count);
  1055. ip += instr_arg_count;
  1056. int argc = _code_ptr[ip + 1];
  1057. Array array;
  1058. array.resize(argc);
  1059. for (int i = 0; i < argc; i++) {
  1060. array[i] = *(instruction_args[i]);
  1061. }
  1062. GET_INSTRUCTION_ARG(dst, argc);
  1063. *dst = array;
  1064. ip += 2;
  1065. }
  1066. DISPATCH_OPCODE;
  1067. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1068. CHECK_SPACE(2 + instr_arg_count);
  1069. ip += instr_arg_count;
  1070. int argc = _code_ptr[ip + 1];
  1071. Dictionary dict;
  1072. for (int i = 0; i < argc; i++) {
  1073. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1074. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1075. dict[*k] = *v;
  1076. }
  1077. GET_INSTRUCTION_ARG(dst, argc * 2);
  1078. *dst = dict;
  1079. ip += 2;
  1080. }
  1081. DISPATCH_OPCODE;
  1082. OPCODE(OPCODE_CALL_ASYNC)
  1083. OPCODE(OPCODE_CALL_RETURN)
  1084. OPCODE(OPCODE_CALL) {
  1085. CHECK_SPACE(3 + instr_arg_count);
  1086. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  1087. #ifdef DEBUG_ENABLED
  1088. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  1089. #endif
  1090. ip += instr_arg_count;
  1091. int argc = _code_ptr[ip + 1];
  1092. GD_ERR_BREAK(argc < 0);
  1093. int methodname_idx = _code_ptr[ip + 2];
  1094. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1095. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1096. GET_INSTRUCTION_ARG(base, argc);
  1097. Variant **argptrs = instruction_args;
  1098. #ifdef DEBUG_ENABLED
  1099. uint64_t call_time = 0;
  1100. if (GDScriptLanguage::get_singleton()->profiling) {
  1101. call_time = OS::get_singleton()->get_ticks_usec();
  1102. }
  1103. #endif
  1104. Callable::CallError err;
  1105. if (call_ret) {
  1106. GET_INSTRUCTION_ARG(ret, argc + 1);
  1107. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  1108. #ifdef DEBUG_ENABLED
  1109. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1110. // Check if getting a function state without await.
  1111. bool was_freed = false;
  1112. Object *obj = ret->get_validated_object_with_check(was_freed);
  1113. if (was_freed) {
  1114. err_text = "Got a freed object as a result of the call.";
  1115. OPCODE_BREAK;
  1116. }
  1117. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1118. err_text = R"(Trying to call an async function without "await".)";
  1119. OPCODE_BREAK;
  1120. }
  1121. }
  1122. #endif
  1123. } else {
  1124. Variant ret;
  1125. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  1126. }
  1127. #ifdef DEBUG_ENABLED
  1128. if (GDScriptLanguage::get_singleton()->profiling) {
  1129. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1130. }
  1131. if (err.error != Callable::CallError::CALL_OK) {
  1132. String methodstr = *methodname;
  1133. String basestr = _get_var_type(base);
  1134. if (methodstr == "call") {
  1135. if (argc >= 1) {
  1136. methodstr = String(*argptrs[0]) + " (via call)";
  1137. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1138. err.argument += 1;
  1139. }
  1140. }
  1141. } else if (methodstr == "free") {
  1142. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1143. if (base->is_ref()) {
  1144. err_text = "Attempted to free a reference.";
  1145. OPCODE_BREAK;
  1146. } else if (base->get_type() == Variant::OBJECT) {
  1147. err_text = "Attempted to free a locked object (calling or emitting).";
  1148. OPCODE_BREAK;
  1149. }
  1150. }
  1151. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1152. if (argc >= 1) {
  1153. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1154. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1155. err.argument += 1;
  1156. }
  1157. }
  1158. }
  1159. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1160. OPCODE_BREAK;
  1161. }
  1162. #endif
  1163. ip += 3;
  1164. }
  1165. DISPATCH_OPCODE;
  1166. OPCODE(OPCODE_CALL_METHOD_BIND)
  1167. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1168. CHECK_SPACE(3 + instr_arg_count);
  1169. bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  1170. ip += instr_arg_count;
  1171. int argc = _code_ptr[ip + 1];
  1172. GD_ERR_BREAK(argc < 0);
  1173. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1174. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1175. GET_INSTRUCTION_ARG(base, argc);
  1176. #ifdef DEBUG_ENABLED
  1177. bool freed = false;
  1178. Object *base_obj = base->get_validated_object_with_check(freed);
  1179. if (freed) {
  1180. err_text = "Trying to call a function on a previously freed instance.";
  1181. OPCODE_BREAK;
  1182. } else if (!base_obj) {
  1183. err_text = "Trying to call a function on a null value.";
  1184. OPCODE_BREAK;
  1185. }
  1186. #else
  1187. Object *base_obj = base->operator Object *();
  1188. #endif
  1189. Variant **argptrs = instruction_args;
  1190. #ifdef DEBUG_ENABLED
  1191. uint64_t call_time = 0;
  1192. if (GDScriptLanguage::get_singleton()->profiling) {
  1193. call_time = OS::get_singleton()->get_ticks_usec();
  1194. }
  1195. #endif
  1196. Callable::CallError err;
  1197. if (call_ret) {
  1198. GET_INSTRUCTION_ARG(ret, argc + 1);
  1199. *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1200. } else {
  1201. method->call(base_obj, (const Variant **)argptrs, argc, err);
  1202. }
  1203. #ifdef DEBUG_ENABLED
  1204. if (GDScriptLanguage::get_singleton()->profiling) {
  1205. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1206. }
  1207. if (err.error != Callable::CallError::CALL_OK) {
  1208. String methodstr = method->get_name();
  1209. String basestr = _get_var_type(base);
  1210. if (methodstr == "call") {
  1211. if (argc >= 1) {
  1212. methodstr = String(*argptrs[0]) + " (via call)";
  1213. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1214. err.argument += 1;
  1215. }
  1216. }
  1217. } else if (methodstr == "free") {
  1218. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1219. if (base->is_ref()) {
  1220. err_text = "Attempted to free a reference.";
  1221. OPCODE_BREAK;
  1222. } else if (base->get_type() == Variant::OBJECT) {
  1223. err_text = "Attempted to free a locked object (calling or emitting).";
  1224. OPCODE_BREAK;
  1225. }
  1226. }
  1227. }
  1228. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1229. OPCODE_BREAK;
  1230. }
  1231. #endif
  1232. ip += 3;
  1233. }
  1234. DISPATCH_OPCODE;
  1235. #ifdef DEBUG_ENABLED
  1236. #define OPCODE_CALL_PTR(m_type) \
  1237. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1238. CHECK_SPACE(3 + instr_arg_count); \
  1239. ip += instr_arg_count; \
  1240. int argc = _code_ptr[ip + 1]; \
  1241. GD_ERR_BREAK(argc < 0); \
  1242. GET_INSTRUCTION_ARG(base, argc); \
  1243. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
  1244. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1245. bool freed = false; \
  1246. Object *base_obj = base->get_validated_object_with_check(freed); \
  1247. if (freed) { \
  1248. err_text = "Trying to call a function on a previously freed instance."; \
  1249. OPCODE_BREAK; \
  1250. } else if (!base_obj) { \
  1251. err_text = "Trying to call a function on a null value."; \
  1252. OPCODE_BREAK; \
  1253. } \
  1254. const void **argptrs = call_args_ptr; \
  1255. for (int i = 0; i < argc; i++) { \
  1256. GET_INSTRUCTION_ARG(v, i); \
  1257. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1258. } \
  1259. uint64_t call_time = 0; \
  1260. if (GDScriptLanguage::get_singleton()->profiling) { \
  1261. call_time = OS::get_singleton()->get_ticks_usec(); \
  1262. } \
  1263. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1264. VariantInternal::initialize(ret, Variant::m_type); \
  1265. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1266. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1267. if (GDScriptLanguage::get_singleton()->profiling) { \
  1268. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
  1269. } \
  1270. ip += 3; \
  1271. } \
  1272. DISPATCH_OPCODE
  1273. #else
  1274. #define OPCODE_CALL_PTR(m_type) \
  1275. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1276. CHECK_SPACE(3 + instr_arg_count); \
  1277. int argc = _code_ptr[ip + 1]; \
  1278. GET_INSTRUCTION_ARG(base, argc); \
  1279. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1280. Object *base_obj = *VariantInternal::get_object(base); \
  1281. const void **argptrs = call_args_ptr; \
  1282. for (int i = 0; i < argc; i++) { \
  1283. GET_INSTRUCTION_ARG(v, i); \
  1284. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1285. } \
  1286. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1287. VariantInternal::initialize(ret, Variant::m_type); \
  1288. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1289. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1290. ip += 3; \
  1291. } \
  1292. DISPATCH_OPCODE
  1293. #endif
  1294. OPCODE_CALL_PTR(BOOL);
  1295. OPCODE_CALL_PTR(INT);
  1296. OPCODE_CALL_PTR(FLOAT);
  1297. OPCODE_CALL_PTR(STRING);
  1298. OPCODE_CALL_PTR(VECTOR2);
  1299. OPCODE_CALL_PTR(VECTOR2I);
  1300. OPCODE_CALL_PTR(RECT2);
  1301. OPCODE_CALL_PTR(RECT2I);
  1302. OPCODE_CALL_PTR(VECTOR3);
  1303. OPCODE_CALL_PTR(VECTOR3I);
  1304. OPCODE_CALL_PTR(TRANSFORM2D);
  1305. OPCODE_CALL_PTR(PLANE);
  1306. OPCODE_CALL_PTR(QUAT);
  1307. OPCODE_CALL_PTR(AABB);
  1308. OPCODE_CALL_PTR(BASIS);
  1309. OPCODE_CALL_PTR(TRANSFORM);
  1310. OPCODE_CALL_PTR(COLOR);
  1311. OPCODE_CALL_PTR(STRING_NAME);
  1312. OPCODE_CALL_PTR(NODE_PATH);
  1313. OPCODE_CALL_PTR(RID);
  1314. OPCODE_CALL_PTR(CALLABLE);
  1315. OPCODE_CALL_PTR(SIGNAL);
  1316. OPCODE_CALL_PTR(DICTIONARY);
  1317. OPCODE_CALL_PTR(ARRAY);
  1318. OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
  1319. OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
  1320. OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
  1321. OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
  1322. OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
  1323. OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
  1324. OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
  1325. OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
  1326. OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
  1327. OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
  1328. CHECK_SPACE(3 + instr_arg_count);
  1329. ip += instr_arg_count;
  1330. int argc = _code_ptr[ip + 1];
  1331. GD_ERR_BREAK(argc < 0);
  1332. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1333. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1334. GET_INSTRUCTION_ARG(base, argc);
  1335. #ifdef DEBUG_ENABLED
  1336. bool freed = false;
  1337. Object *base_obj = base->get_validated_object_with_check(freed);
  1338. if (freed) {
  1339. err_text = "Trying to call a function on a previously freed instance.";
  1340. OPCODE_BREAK;
  1341. } else if (!base_obj) {
  1342. err_text = "Trying to call a function on a null value.";
  1343. OPCODE_BREAK;
  1344. }
  1345. #else
  1346. Object *base_obj = *VariantInternal::get_object(base);
  1347. #endif
  1348. const void **argptrs = call_args_ptr;
  1349. for (int i = 0; i < argc; i++) {
  1350. GET_INSTRUCTION_ARG(v, i);
  1351. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1352. }
  1353. #ifdef DEBUG_ENABLED
  1354. uint64_t call_time = 0;
  1355. if (GDScriptLanguage::get_singleton()->profiling) {
  1356. call_time = OS::get_singleton()->get_ticks_usec();
  1357. }
  1358. #endif
  1359. GET_INSTRUCTION_ARG(ret, argc + 1);
  1360. VariantInternal::initialize(ret, Variant::OBJECT);
  1361. Object **ret_opaque = VariantInternal::get_object(ret);
  1362. method->ptrcall(base_obj, argptrs, ret_opaque);
  1363. VariantInternal::set_object(ret, *ret_opaque);
  1364. #ifdef DEBUG_ENABLED
  1365. if (GDScriptLanguage::get_singleton()->profiling) {
  1366. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1367. }
  1368. #endif
  1369. ip += 3;
  1370. }
  1371. DISPATCH_OPCODE;
  1372. OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
  1373. CHECK_SPACE(3 + instr_arg_count);
  1374. ip += instr_arg_count;
  1375. int argc = _code_ptr[ip + 1];
  1376. GD_ERR_BREAK(argc < 0);
  1377. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1378. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1379. GET_INSTRUCTION_ARG(base, argc);
  1380. #ifdef DEBUG_ENABLED
  1381. bool freed = false;
  1382. Object *base_obj = base->get_validated_object_with_check(freed);
  1383. if (freed) {
  1384. err_text = "Trying to call a function on a previously freed instance.";
  1385. OPCODE_BREAK;
  1386. } else if (!base_obj) {
  1387. err_text = "Trying to call a function on a null value.";
  1388. OPCODE_BREAK;
  1389. }
  1390. #else
  1391. Object *base_obj = *VariantInternal::get_object(base);
  1392. #endif
  1393. const void **argptrs = call_args_ptr;
  1394. for (int i = 0; i < argc; i++) {
  1395. GET_INSTRUCTION_ARG(v, i);
  1396. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1397. }
  1398. #ifdef DEBUG_ENABLED
  1399. uint64_t call_time = 0;
  1400. if (GDScriptLanguage::get_singleton()->profiling) {
  1401. call_time = OS::get_singleton()->get_ticks_usec();
  1402. }
  1403. #endif
  1404. GET_INSTRUCTION_ARG(ret, argc + 1);
  1405. VariantInternal::initialize(ret, Variant::NIL);
  1406. method->ptrcall(base_obj, argptrs, nullptr);
  1407. #ifdef DEBUG_ENABLED
  1408. if (GDScriptLanguage::get_singleton()->profiling) {
  1409. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1410. }
  1411. #endif
  1412. ip += 3;
  1413. }
  1414. DISPATCH_OPCODE;
  1415. OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
  1416. CHECK_SPACE(3 + instr_arg_count);
  1417. ip += instr_arg_count;
  1418. int argc = _code_ptr[ip + 1];
  1419. GD_ERR_BREAK(argc < 0);
  1420. GET_INSTRUCTION_ARG(base, argc);
  1421. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
  1422. Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
  1423. Variant **argptrs = instruction_args;
  1424. #ifdef DEBUG_ENABLED
  1425. uint64_t call_time = 0;
  1426. if (GDScriptLanguage::get_singleton()->profiling) {
  1427. call_time = OS::get_singleton()->get_ticks_usec();
  1428. }
  1429. #endif
  1430. GET_INSTRUCTION_ARG(ret, argc + 1);
  1431. method(base, (const Variant **)argptrs, argc, ret);
  1432. #ifdef DEBUG_ENABLED
  1433. if (GDScriptLanguage::get_singleton()->profiling) {
  1434. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1435. }
  1436. #endif
  1437. ip += 3;
  1438. }
  1439. DISPATCH_OPCODE;
  1440. OPCODE(OPCODE_CALL_BUILT_IN) {
  1441. CHECK_SPACE(3 + instr_arg_count);
  1442. ip += instr_arg_count;
  1443. int argc = _code_ptr[ip + 1];
  1444. GD_ERR_BREAK(argc < 0);
  1445. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 2]);
  1446. Variant **argptrs = instruction_args;
  1447. GET_INSTRUCTION_ARG(dst, argc);
  1448. Callable::CallError err;
  1449. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  1450. #ifdef DEBUG_ENABLED
  1451. if (err.error != Callable::CallError::CALL_OK) {
  1452. String methodstr = GDScriptFunctions::get_func_name(func);
  1453. if (dst->get_type() == Variant::STRING) {
  1454. //call provided error string
  1455. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  1456. } else {
  1457. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  1458. }
  1459. OPCODE_BREAK;
  1460. }
  1461. #endif
  1462. ip += 3;
  1463. }
  1464. DISPATCH_OPCODE;
  1465. OPCODE(OPCODE_CALL_SELF_BASE) {
  1466. CHECK_SPACE(3 + instr_arg_count);
  1467. ip += instr_arg_count;
  1468. int self_fun = _code_ptr[ip + 1];
  1469. #ifdef DEBUG_ENABLED
  1470. if (self_fun < 0 || self_fun >= _global_names_count) {
  1471. err_text = "compiler bug, function name not found";
  1472. OPCODE_BREAK;
  1473. }
  1474. #endif
  1475. const StringName *methodname = &_global_names_ptr[self_fun];
  1476. int argc = _code_ptr[ip + 2];
  1477. GD_ERR_BREAK(argc < 0);
  1478. Variant **argptrs = instruction_args;
  1479. GET_INSTRUCTION_ARG(dst, argc);
  1480. const GDScript *gds = _script;
  1481. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  1482. while (gds->base.ptr()) {
  1483. gds = gds->base.ptr();
  1484. E = gds->member_functions.find(*methodname);
  1485. if (E) {
  1486. break;
  1487. }
  1488. }
  1489. Callable::CallError err;
  1490. if (E) {
  1491. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1492. } else if (gds->native.ptr()) {
  1493. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1494. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1495. if (!mb) {
  1496. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1497. } else {
  1498. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1499. }
  1500. } else {
  1501. err.error = Callable::CallError::CALL_OK;
  1502. }
  1503. } else {
  1504. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1505. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1506. } else {
  1507. err.error = Callable::CallError::CALL_OK;
  1508. }
  1509. }
  1510. if (err.error != Callable::CallError::CALL_OK) {
  1511. String methodstr = *methodname;
  1512. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1513. OPCODE_BREAK;
  1514. }
  1515. ip += 3;
  1516. }
  1517. DISPATCH_OPCODE;
  1518. OPCODE(OPCODE_AWAIT) {
  1519. CHECK_SPACE(2);
  1520. // Do the oneshot connect.
  1521. GET_INSTRUCTION_ARG(argobj, 0);
  1522. Signal sig;
  1523. bool is_signal = true;
  1524. {
  1525. Variant result = *argobj;
  1526. if (argobj->get_type() == Variant::OBJECT) {
  1527. bool was_freed = false;
  1528. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1529. if (was_freed) {
  1530. err_text = "Trying to await on a freed object.";
  1531. OPCODE_BREAK;
  1532. }
  1533. // Is this even possible to be null at this point?
  1534. if (obj) {
  1535. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1536. static StringName completed = _scs_create("completed");
  1537. result = Signal(obj, completed);
  1538. }
  1539. }
  1540. }
  1541. if (result.get_type() != Variant::SIGNAL) {
  1542. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1543. // The stack pointer should be the same, so we don't need to set a return value.
  1544. is_signal = false;
  1545. } else {
  1546. sig = result;
  1547. }
  1548. }
  1549. if (is_signal) {
  1550. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1551. gdfs->function = this;
  1552. gdfs->state.stack.resize(alloca_size);
  1553. //copy variant stack
  1554. for (int i = 0; i < _stack_size; i++) {
  1555. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1556. }
  1557. gdfs->state.stack_size = _stack_size;
  1558. gdfs->state.self = self;
  1559. gdfs->state.alloca_size = alloca_size;
  1560. gdfs->state.ip = ip + 2;
  1561. gdfs->state.line = line;
  1562. gdfs->state.script = _script;
  1563. {
  1564. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1565. _script->pending_func_states.add(&gdfs->scripts_list);
  1566. if (p_instance) {
  1567. gdfs->state.instance = p_instance;
  1568. p_instance->pending_func_states.add(&gdfs->instances_list);
  1569. } else {
  1570. gdfs->state.instance = nullptr;
  1571. }
  1572. }
  1573. #ifdef DEBUG_ENABLED
  1574. gdfs->state.function_name = name;
  1575. gdfs->state.script_path = _script->get_path();
  1576. #endif
  1577. gdfs->state.defarg = defarg;
  1578. gdfs->function = this;
  1579. retvalue = gdfs;
  1580. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1581. if (err != OK) {
  1582. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1583. OPCODE_BREAK;
  1584. }
  1585. #ifdef DEBUG_ENABLED
  1586. exit_ok = true;
  1587. awaited = true;
  1588. #endif
  1589. OPCODE_BREAK;
  1590. }
  1591. }
  1592. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1593. OPCODE(OPCODE_AWAIT_RESUME) {
  1594. CHECK_SPACE(2);
  1595. #ifdef DEBUG_ENABLED
  1596. if (!p_state) {
  1597. err_text = ("Invalid Resume (bug?)");
  1598. OPCODE_BREAK;
  1599. }
  1600. #endif
  1601. GET_INSTRUCTION_ARG(result, 0);
  1602. *result = p_state->result;
  1603. ip += 2;
  1604. }
  1605. DISPATCH_OPCODE;
  1606. OPCODE(OPCODE_JUMP) {
  1607. CHECK_SPACE(2);
  1608. int to = _code_ptr[ip + 1];
  1609. GD_ERR_BREAK(to < 0 || to > _code_size);
  1610. ip = to;
  1611. }
  1612. DISPATCH_OPCODE;
  1613. OPCODE(OPCODE_JUMP_IF) {
  1614. CHECK_SPACE(3);
  1615. GET_INSTRUCTION_ARG(test, 0);
  1616. bool result = test->booleanize();
  1617. if (result) {
  1618. int to = _code_ptr[ip + 2];
  1619. GD_ERR_BREAK(to < 0 || to > _code_size);
  1620. ip = to;
  1621. } else {
  1622. ip += 3;
  1623. }
  1624. }
  1625. DISPATCH_OPCODE;
  1626. OPCODE(OPCODE_JUMP_IF_NOT) {
  1627. CHECK_SPACE(3);
  1628. GET_INSTRUCTION_ARG(test, 0);
  1629. bool result = test->booleanize();
  1630. if (!result) {
  1631. int to = _code_ptr[ip + 2];
  1632. GD_ERR_BREAK(to < 0 || to > _code_size);
  1633. ip = to;
  1634. } else {
  1635. ip += 3;
  1636. }
  1637. }
  1638. DISPATCH_OPCODE;
  1639. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1640. CHECK_SPACE(2);
  1641. ip = _default_arg_ptr[defarg];
  1642. }
  1643. DISPATCH_OPCODE;
  1644. OPCODE(OPCODE_RETURN) {
  1645. CHECK_SPACE(2);
  1646. GET_INSTRUCTION_ARG(r, 0);
  1647. retvalue = *r;
  1648. #ifdef DEBUG_ENABLED
  1649. exit_ok = true;
  1650. #endif
  1651. OPCODE_BREAK;
  1652. }
  1653. OPCODE(OPCODE_ITERATE_BEGIN) {
  1654. CHECK_SPACE(8); // Space for this and a regular iterate.
  1655. GET_INSTRUCTION_ARG(counter, 0);
  1656. GET_INSTRUCTION_ARG(container, 1);
  1657. bool valid;
  1658. if (!container->iter_init(*counter, valid)) {
  1659. #ifdef DEBUG_ENABLED
  1660. if (!valid) {
  1661. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1662. OPCODE_BREAK;
  1663. }
  1664. #endif
  1665. int jumpto = _code_ptr[ip + 4];
  1666. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1667. ip = jumpto;
  1668. } else {
  1669. GET_INSTRUCTION_ARG(iterator, 2);
  1670. *iterator = container->iter_get(*counter, valid);
  1671. #ifdef DEBUG_ENABLED
  1672. if (!valid) {
  1673. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1674. OPCODE_BREAK;
  1675. }
  1676. #endif
  1677. ip += 5; // Skip regular iterate which is always next.
  1678. }
  1679. }
  1680. DISPATCH_OPCODE;
  1681. OPCODE(OPCODE_ITERATE) {
  1682. CHECK_SPACE(4);
  1683. GET_INSTRUCTION_ARG(counter, 0);
  1684. GET_INSTRUCTION_ARG(container, 1);
  1685. bool valid;
  1686. if (!container->iter_next(*counter, valid)) {
  1687. #ifdef DEBUG_ENABLED
  1688. if (!valid) {
  1689. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1690. OPCODE_BREAK;
  1691. }
  1692. #endif
  1693. int jumpto = _code_ptr[ip + 4];
  1694. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1695. ip = jumpto;
  1696. } else {
  1697. GET_INSTRUCTION_ARG(iterator, 2);
  1698. *iterator = container->iter_get(*counter, valid);
  1699. #ifdef DEBUG_ENABLED
  1700. if (!valid) {
  1701. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1702. OPCODE_BREAK;
  1703. }
  1704. #endif
  1705. ip += 5; //loop again
  1706. }
  1707. }
  1708. DISPATCH_OPCODE;
  1709. OPCODE(OPCODE_ASSERT) {
  1710. CHECK_SPACE(3);
  1711. #ifdef DEBUG_ENABLED
  1712. GET_INSTRUCTION_ARG(test, 0);
  1713. bool result = test->booleanize();
  1714. if (!result) {
  1715. String message_str;
  1716. if (_code_ptr[ip + 2] != 0) {
  1717. GET_INSTRUCTION_ARG(message, 1);
  1718. message_str = *message;
  1719. }
  1720. if (message_str.empty()) {
  1721. err_text = "Assertion failed.";
  1722. } else {
  1723. err_text = "Assertion failed: " + message_str;
  1724. }
  1725. OPCODE_BREAK;
  1726. }
  1727. #endif
  1728. ip += 3;
  1729. }
  1730. DISPATCH_OPCODE;
  1731. OPCODE(OPCODE_BREAKPOINT) {
  1732. #ifdef DEBUG_ENABLED
  1733. if (EngineDebugger::is_active()) {
  1734. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1735. }
  1736. #endif
  1737. ip += 1;
  1738. }
  1739. DISPATCH_OPCODE;
  1740. OPCODE(OPCODE_LINE) {
  1741. CHECK_SPACE(2);
  1742. line = _code_ptr[ip + 1];
  1743. ip += 2;
  1744. if (EngineDebugger::is_active()) {
  1745. // line
  1746. bool do_break = false;
  1747. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1748. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  1749. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1750. }
  1751. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  1752. do_break = true;
  1753. }
  1754. }
  1755. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  1756. do_break = true;
  1757. }
  1758. if (do_break) {
  1759. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1760. }
  1761. EngineDebugger::get_singleton()->line_poll();
  1762. }
  1763. }
  1764. DISPATCH_OPCODE;
  1765. OPCODE(OPCODE_END) {
  1766. #ifdef DEBUG_ENABLED
  1767. exit_ok = true;
  1768. #endif
  1769. OPCODE_BREAK;
  1770. }
  1771. #if 0 // Enable for debugging.
  1772. default: {
  1773. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1774. OPCODE_BREAK;
  1775. }
  1776. #endif
  1777. }
  1778. OPCODES_END
  1779. #ifdef DEBUG_ENABLED
  1780. if (exit_ok) {
  1781. OPCODE_OUT;
  1782. }
  1783. //error
  1784. // function, file, line, error, explanation
  1785. String err_file;
  1786. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  1787. err_file = p_instance->script->path;
  1788. } else if (script) {
  1789. err_file = script->path;
  1790. }
  1791. if (err_file == "") {
  1792. err_file = "<built-in>";
  1793. }
  1794. String err_func = name;
  1795. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  1796. err_func = p_instance->script->name + "." + err_func;
  1797. }
  1798. int err_line = line;
  1799. if (err_text == "") {
  1800. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1801. }
  1802. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1803. // debugger break did not happen
  1804. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1805. }
  1806. #endif
  1807. OPCODE_OUT;
  1808. }
  1809. OPCODES_OUT
  1810. #ifdef DEBUG_ENABLED
  1811. if (GDScriptLanguage::get_singleton()->profiling) {
  1812. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1813. profile.total_time += time_taken;
  1814. profile.self_time += time_taken - function_call_time;
  1815. profile.frame_total_time += time_taken;
  1816. profile.frame_self_time += time_taken - function_call_time;
  1817. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1818. }
  1819. // Check if this is the last time the function is resuming from await
  1820. // Will be true if never awaited as well
  1821. // When it's the last resume it will postpone the exit from stack,
  1822. // so the debugger knows which function triggered the resume of the next function (if any)
  1823. if (!p_state || awaited) {
  1824. if (EngineDebugger::is_active()) {
  1825. GDScriptLanguage::get_singleton()->exit_function();
  1826. }
  1827. #endif
  1828. if (_stack_size) {
  1829. //free stack
  1830. for (int i = 0; i < _stack_size; i++) {
  1831. stack[i].~Variant();
  1832. }
  1833. }
  1834. #ifdef DEBUG_ENABLED
  1835. }
  1836. #endif
  1837. return retvalue;
  1838. }