gdscript_vm.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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_EXTENDS_TEST, \
  176. &&OPCODE_IS_BUILTIN, \
  177. &&OPCODE_SET, \
  178. &&OPCODE_GET, \
  179. &&OPCODE_SET_NAMED, \
  180. &&OPCODE_GET_NAMED, \
  181. &&OPCODE_SET_MEMBER, \
  182. &&OPCODE_GET_MEMBER, \
  183. &&OPCODE_ASSIGN, \
  184. &&OPCODE_ASSIGN_TRUE, \
  185. &&OPCODE_ASSIGN_FALSE, \
  186. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  187. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  188. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  189. &&OPCODE_CAST_TO_BUILTIN, \
  190. &&OPCODE_CAST_TO_NATIVE, \
  191. &&OPCODE_CAST_TO_SCRIPT, \
  192. &&OPCODE_CONSTRUCT, \
  193. &&OPCODE_CONSTRUCT_ARRAY, \
  194. &&OPCODE_CONSTRUCT_DICTIONARY, \
  195. &&OPCODE_CALL, \
  196. &&OPCODE_CALL_RETURN, \
  197. &&OPCODE_CALL_ASYNC, \
  198. &&OPCODE_CALL_BUILT_IN, \
  199. &&OPCODE_CALL_SELF_BASE, \
  200. &&OPCODE_AWAIT, \
  201. &&OPCODE_AWAIT_RESUME, \
  202. &&OPCODE_JUMP, \
  203. &&OPCODE_JUMP_IF, \
  204. &&OPCODE_JUMP_IF_NOT, \
  205. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  206. &&OPCODE_RETURN, \
  207. &&OPCODE_ITERATE_BEGIN, \
  208. &&OPCODE_ITERATE, \
  209. &&OPCODE_ASSERT, \
  210. &&OPCODE_BREAKPOINT, \
  211. &&OPCODE_LINE, \
  212. &&OPCODE_END \
  213. }; \
  214. 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.");
  215. #define OPCODE(m_op) \
  216. m_op:
  217. #define OPCODE_WHILE(m_test) \
  218. OPSWHILE:
  219. #define OPCODES_END \
  220. OPSEXIT:
  221. #define OPCODES_OUT \
  222. OPSOUT:
  223. #define DISPATCH_OPCODE goto OPSWHILE
  224. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  225. #define OPCODE_BREAK goto OPSEXIT
  226. #define OPCODE_OUT goto OPSOUT
  227. #else
  228. #define OPCODES_TABLE
  229. #define OPCODE(m_op) case m_op:
  230. #define OPCODE_WHILE(m_test) while (m_test)
  231. #define OPCODES_END
  232. #define OPCODES_OUT
  233. #define DISPATCH_OPCODE continue
  234. #define OPCODE_SWITCH(m_test) switch (m_test)
  235. #define OPCODE_BREAK break
  236. #define OPCODE_OUT break
  237. #endif
  238. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  239. OPCODES_TABLE;
  240. if (!_code_ptr) {
  241. return Variant();
  242. }
  243. r_err.error = Callable::CallError::CALL_OK;
  244. Variant self;
  245. Variant static_ref;
  246. Variant retvalue;
  247. Variant *stack = nullptr;
  248. Variant **instruction_args;
  249. int defarg = 0;
  250. #ifdef DEBUG_ENABLED
  251. //GDScriptLanguage::get_singleton()->calls++;
  252. #endif
  253. uint32_t alloca_size = 0;
  254. GDScript *script;
  255. int ip = 0;
  256. int line = _initial_line;
  257. if (p_state) {
  258. //use existing (supplied) state (awaited)
  259. stack = (Variant *)p_state->stack.ptr();
  260. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  261. line = p_state->line;
  262. ip = p_state->ip;
  263. alloca_size = p_state->stack.size();
  264. script = p_state->script;
  265. p_instance = p_state->instance;
  266. defarg = p_state->defarg;
  267. self = p_state->self;
  268. } else {
  269. if (p_argcount != _argument_count) {
  270. if (p_argcount > _argument_count) {
  271. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  272. r_err.argument = _argument_count;
  273. return Variant();
  274. } else if (p_argcount < _argument_count - _default_arg_count) {
  275. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  276. r_err.argument = _argument_count - _default_arg_count;
  277. return Variant();
  278. } else {
  279. defarg = _argument_count - p_argcount;
  280. }
  281. }
  282. alloca_size = sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  283. if (alloca_size) {
  284. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  285. if (_stack_size) {
  286. stack = (Variant *)aptr;
  287. for (int i = 0; i < p_argcount; i++) {
  288. if (!argument_types[i].has_type) {
  289. memnew_placement(&stack[i], Variant(*p_args[i]));
  290. continue;
  291. }
  292. if (!argument_types[i].is_type(*p_args[i], true)) {
  293. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  294. r_err.argument = i;
  295. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  296. return Variant();
  297. }
  298. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  299. Variant arg;
  300. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  301. memnew_placement(&stack[i], Variant(arg));
  302. } else {
  303. memnew_placement(&stack[i], Variant(*p_args[i]));
  304. }
  305. }
  306. for (int i = p_argcount; i < _stack_size; i++) {
  307. memnew_placement(&stack[i], Variant);
  308. }
  309. } else {
  310. stack = nullptr;
  311. }
  312. if (_instruction_args_size) {
  313. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  314. } else {
  315. instruction_args = nullptr;
  316. }
  317. } else {
  318. stack = nullptr;
  319. instruction_args = nullptr;
  320. }
  321. if (p_instance) {
  322. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  323. self = REF(static_cast<Reference *>(p_instance->owner));
  324. } else {
  325. self = p_instance->owner;
  326. }
  327. script = p_instance->script.ptr();
  328. } else {
  329. script = _script;
  330. }
  331. }
  332. static_ref = script;
  333. String err_text;
  334. #ifdef DEBUG_ENABLED
  335. if (EngineDebugger::is_active()) {
  336. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  337. }
  338. #define GD_ERR_BREAK(m_cond) \
  339. { \
  340. if (unlikely(m_cond)) { \
  341. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  342. OPCODE_BREAK; \
  343. } \
  344. }
  345. #define CHECK_SPACE(m_space) \
  346. GD_ERR_BREAK((ip + m_space) > _code_size)
  347. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  348. Variant *m_v; \
  349. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  350. if (unlikely(!m_v)) \
  351. OPCODE_BREAK;
  352. #else
  353. #define GD_ERR_BREAK(m_cond)
  354. #define CHECK_SPACE(m_space)
  355. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  356. Variant *m_v; \
  357. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  358. #endif
  359. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  360. Variant *m_v = instruction_args[m_idx]
  361. #ifdef DEBUG_ENABLED
  362. uint64_t function_start_time = 0;
  363. uint64_t function_call_time = 0;
  364. if (GDScriptLanguage::get_singleton()->profiling) {
  365. function_start_time = OS::get_singleton()->get_ticks_usec();
  366. function_call_time = 0;
  367. profile.call_count++;
  368. profile.frame_call_count++;
  369. }
  370. bool exit_ok = false;
  371. bool awaited = false;
  372. #endif
  373. #ifdef DEBUG_ENABLED
  374. OPCODE_WHILE(ip < _code_size) {
  375. int last_opcode = _code_ptr[ip];
  376. #else
  377. OPCODE_WHILE(true) {
  378. #endif
  379. // Load arguments for the instruction before each instruction.
  380. int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS;
  381. for (int i = 0; i < instr_arg_count; i++) {
  382. GET_VARIANT_PTR(v, i + 1);
  383. instruction_args[i] = v;
  384. }
  385. OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) {
  386. OPCODE(OPCODE_OPERATOR) {
  387. CHECK_SPACE(5);
  388. bool valid;
  389. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  390. GD_ERR_BREAK(op >= Variant::OP_MAX);
  391. GET_INSTRUCTION_ARG(a, 0);
  392. GET_INSTRUCTION_ARG(b, 1);
  393. GET_INSTRUCTION_ARG(dst, 2);
  394. #ifdef DEBUG_ENABLED
  395. Variant ret;
  396. Variant::evaluate(op, *a, *b, ret, valid);
  397. #else
  398. Variant::evaluate(op, *a, *b, *dst, valid);
  399. #endif
  400. #ifdef DEBUG_ENABLED
  401. if (!valid) {
  402. if (ret.get_type() == Variant::STRING) {
  403. //return a string when invalid with the error
  404. err_text = ret;
  405. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  406. } else {
  407. 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) + "'.";
  408. }
  409. OPCODE_BREAK;
  410. }
  411. *dst = ret;
  412. #endif
  413. ip += 5;
  414. }
  415. DISPATCH_OPCODE;
  416. OPCODE(OPCODE_EXTENDS_TEST) {
  417. CHECK_SPACE(4);
  418. GET_INSTRUCTION_ARG(a, 0);
  419. GET_INSTRUCTION_ARG(b, 1);
  420. GET_INSTRUCTION_ARG(dst, 2);
  421. #ifdef DEBUG_ENABLED
  422. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  423. err_text = "Right operand of 'is' is not a class.";
  424. OPCODE_BREAK;
  425. }
  426. #endif
  427. bool extends_ok = false;
  428. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  429. #ifdef DEBUG_ENABLED
  430. bool was_freed;
  431. Object *obj_A = a->get_validated_object_with_check(was_freed);
  432. if (was_freed) {
  433. err_text = "Left operand of 'is' is a previously freed instance.";
  434. OPCODE_BREAK;
  435. }
  436. Object *obj_B = b->get_validated_object_with_check(was_freed);
  437. if (was_freed) {
  438. err_text = "Right operand of 'is' is a previously freed instance.";
  439. OPCODE_BREAK;
  440. }
  441. #else
  442. Object *obj_A = *a;
  443. Object *obj_B = *b;
  444. #endif // DEBUG_ENABLED
  445. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  446. if (scr_B) {
  447. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  448. //in other situation, this shoul return false.
  449. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  450. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  451. //bool found=false;
  452. while (cmp) {
  453. if (cmp == scr_B) {
  454. //inherits from script, all ok
  455. extends_ok = true;
  456. break;
  457. }
  458. cmp = cmp->_base;
  459. }
  460. }
  461. } else {
  462. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  463. #ifdef DEBUG_ENABLED
  464. if (!nc) {
  465. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  466. OPCODE_BREAK;
  467. }
  468. #endif
  469. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  470. }
  471. }
  472. *dst = extends_ok;
  473. ip += 4;
  474. }
  475. DISPATCH_OPCODE;
  476. OPCODE(OPCODE_IS_BUILTIN) {
  477. CHECK_SPACE(4);
  478. GET_INSTRUCTION_ARG(value, 0);
  479. GET_INSTRUCTION_ARG(dst, 1);
  480. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  481. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  482. *dst = value->get_type() == var_type;
  483. ip += 4;
  484. }
  485. DISPATCH_OPCODE;
  486. OPCODE(OPCODE_SET) {
  487. CHECK_SPACE(3);
  488. GET_INSTRUCTION_ARG(dst, 0);
  489. GET_INSTRUCTION_ARG(index, 1);
  490. GET_INSTRUCTION_ARG(value, 2);
  491. bool valid;
  492. dst->set(*index, *value, &valid);
  493. #ifdef DEBUG_ENABLED
  494. if (!valid) {
  495. String v = index->operator String();
  496. if (v != "") {
  497. v = "'" + v + "'";
  498. } else {
  499. v = "of type '" + _get_var_type(index) + "'";
  500. }
  501. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  502. OPCODE_BREAK;
  503. }
  504. #endif
  505. ip += 4;
  506. }
  507. DISPATCH_OPCODE;
  508. OPCODE(OPCODE_GET) {
  509. CHECK_SPACE(3);
  510. GET_INSTRUCTION_ARG(src, 0);
  511. GET_INSTRUCTION_ARG(index, 1);
  512. GET_INSTRUCTION_ARG(dst, 2);
  513. bool valid;
  514. #ifdef DEBUG_ENABLED
  515. //allow better error message in cases where src and dst are the same stack position
  516. Variant ret = src->get(*index, &valid);
  517. #else
  518. *dst = src->get(*index, &valid);
  519. #endif
  520. #ifdef DEBUG_ENABLED
  521. if (!valid) {
  522. String v = index->operator String();
  523. if (v != "") {
  524. v = "'" + v + "'";
  525. } else {
  526. v = "of type '" + _get_var_type(index) + "'";
  527. }
  528. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  529. OPCODE_BREAK;
  530. }
  531. *dst = ret;
  532. #endif
  533. ip += 4;
  534. }
  535. DISPATCH_OPCODE;
  536. OPCODE(OPCODE_SET_NAMED) {
  537. CHECK_SPACE(3);
  538. GET_INSTRUCTION_ARG(dst, 0);
  539. GET_INSTRUCTION_ARG(value, 1);
  540. int indexname = _code_ptr[ip + 3];
  541. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  542. const StringName *index = &_global_names_ptr[indexname];
  543. bool valid;
  544. dst->set_named(*index, *value, valid);
  545. #ifdef DEBUG_ENABLED
  546. if (!valid) {
  547. String err_type;
  548. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  549. OPCODE_BREAK;
  550. }
  551. #endif
  552. ip += 4;
  553. }
  554. DISPATCH_OPCODE;
  555. OPCODE(OPCODE_GET_NAMED) {
  556. CHECK_SPACE(4);
  557. GET_INSTRUCTION_ARG(src, 0);
  558. GET_INSTRUCTION_ARG(dst, 1);
  559. int indexname = _code_ptr[ip + 3];
  560. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  561. const StringName *index = &_global_names_ptr[indexname];
  562. bool valid;
  563. #ifdef DEBUG_ENABLED
  564. //allow better error message in cases where src and dst are the same stack position
  565. Variant ret = src->get_named(*index, valid);
  566. #else
  567. *dst = src->get_named(*index, valid);
  568. #endif
  569. #ifdef DEBUG_ENABLED
  570. if (!valid) {
  571. if (src->has_method(*index)) {
  572. 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() + "\") ?";
  573. } else {
  574. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  575. }
  576. OPCODE_BREAK;
  577. }
  578. *dst = ret;
  579. #endif
  580. ip += 4;
  581. }
  582. DISPATCH_OPCODE;
  583. OPCODE(OPCODE_SET_MEMBER) {
  584. CHECK_SPACE(3);
  585. GET_INSTRUCTION_ARG(src, 0);
  586. int indexname = _code_ptr[ip + 2];
  587. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  588. const StringName *index = &_global_names_ptr[indexname];
  589. bool valid;
  590. #ifndef DEBUG_ENABLED
  591. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  592. #else
  593. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  594. if (!ok) {
  595. err_text = "Internal error setting property: " + String(*index);
  596. OPCODE_BREAK;
  597. } else if (!valid) {
  598. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  599. OPCODE_BREAK;
  600. }
  601. #endif
  602. ip += 3;
  603. }
  604. DISPATCH_OPCODE;
  605. OPCODE(OPCODE_GET_MEMBER) {
  606. CHECK_SPACE(3);
  607. GET_INSTRUCTION_ARG(dst, 0);
  608. int indexname = _code_ptr[ip + 2];
  609. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  610. const StringName *index = &_global_names_ptr[indexname];
  611. #ifndef DEBUG_ENABLED
  612. ClassDB::get_property(p_instance->owner, *index, *dst);
  613. #else
  614. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  615. if (!ok) {
  616. err_text = "Internal error getting property: " + String(*index);
  617. OPCODE_BREAK;
  618. }
  619. #endif
  620. ip += 3;
  621. }
  622. DISPATCH_OPCODE;
  623. OPCODE(OPCODE_ASSIGN) {
  624. CHECK_SPACE(3);
  625. GET_INSTRUCTION_ARG(dst, 0);
  626. GET_INSTRUCTION_ARG(src, 1);
  627. *dst = *src;
  628. ip += 3;
  629. }
  630. DISPATCH_OPCODE;
  631. OPCODE(OPCODE_ASSIGN_TRUE) {
  632. CHECK_SPACE(2);
  633. GET_INSTRUCTION_ARG(dst, 0);
  634. *dst = true;
  635. ip += 2;
  636. }
  637. DISPATCH_OPCODE;
  638. OPCODE(OPCODE_ASSIGN_FALSE) {
  639. CHECK_SPACE(2);
  640. GET_INSTRUCTION_ARG(dst, 0);
  641. *dst = false;
  642. ip += 2;
  643. }
  644. DISPATCH_OPCODE;
  645. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  646. CHECK_SPACE(4);
  647. GET_INSTRUCTION_ARG(dst, 0);
  648. GET_INSTRUCTION_ARG(src, 1);
  649. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  650. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  651. if (src->get_type() != var_type) {
  652. #ifdef DEBUG_ENABLED
  653. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  654. #endif // DEBUG_ENABLED
  655. Callable::CallError ce;
  656. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  657. } else {
  658. #ifdef DEBUG_ENABLED
  659. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  660. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  661. OPCODE_BREAK;
  662. }
  663. } else {
  664. #endif // DEBUG_ENABLED
  665. *dst = *src;
  666. }
  667. ip += 4;
  668. }
  669. DISPATCH_OPCODE;
  670. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  671. CHECK_SPACE(4);
  672. GET_INSTRUCTION_ARG(dst, 0);
  673. GET_INSTRUCTION_ARG(src, 1);
  674. #ifdef DEBUG_ENABLED
  675. GET_INSTRUCTION_ARG(type, 2);
  676. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  677. GD_ERR_BREAK(!nc);
  678. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  679. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  680. "' to a variable of type '" + nc->get_name() + "'.";
  681. OPCODE_BREAK;
  682. }
  683. Object *src_obj = src->operator Object *();
  684. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  685. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  686. "' to a variable of type '" + nc->get_name() + "'.";
  687. OPCODE_BREAK;
  688. }
  689. #endif // DEBUG_ENABLED
  690. *dst = *src;
  691. ip += 4;
  692. }
  693. DISPATCH_OPCODE;
  694. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  695. CHECK_SPACE(4);
  696. GET_INSTRUCTION_ARG(dst, 0);
  697. GET_INSTRUCTION_ARG(src, 1);
  698. #ifdef DEBUG_ENABLED
  699. GET_INSTRUCTION_ARG(type, 2);
  700. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  701. GD_ERR_BREAK(!base_type);
  702. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  703. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  704. OPCODE_BREAK;
  705. }
  706. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  707. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  708. if (!scr_inst) {
  709. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  710. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  711. OPCODE_BREAK;
  712. }
  713. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  714. bool valid = false;
  715. while (src_type) {
  716. if (src_type == base_type) {
  717. valid = true;
  718. break;
  719. }
  720. src_type = src_type->get_base_script().ptr();
  721. }
  722. if (!valid) {
  723. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  724. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  725. OPCODE_BREAK;
  726. }
  727. }
  728. #endif // DEBUG_ENABLED
  729. *dst = *src;
  730. ip += 4;
  731. }
  732. DISPATCH_OPCODE;
  733. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  734. CHECK_SPACE(4);
  735. GET_INSTRUCTION_ARG(src, 0);
  736. GET_INSTRUCTION_ARG(dst, 1);
  737. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  738. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  739. Callable::CallError err;
  740. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  741. #ifdef DEBUG_ENABLED
  742. if (err.error != Callable::CallError::CALL_OK) {
  743. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  744. OPCODE_BREAK;
  745. }
  746. #endif
  747. ip += 4;
  748. }
  749. DISPATCH_OPCODE;
  750. OPCODE(OPCODE_CAST_TO_NATIVE) {
  751. CHECK_SPACE(4);
  752. GET_INSTRUCTION_ARG(src, 0);
  753. GET_INSTRUCTION_ARG(dst, 1);
  754. GET_INSTRUCTION_ARG(to_type, 2);
  755. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  756. GD_ERR_BREAK(!nc);
  757. #ifdef DEBUG_ENABLED
  758. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  759. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  760. OPCODE_BREAK;
  761. }
  762. #endif
  763. Object *src_obj = src->operator Object *();
  764. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  765. *dst = Variant(); // invalid cast, assign NULL
  766. } else {
  767. *dst = *src;
  768. }
  769. ip += 4;
  770. }
  771. DISPATCH_OPCODE;
  772. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  773. CHECK_SPACE(4);
  774. GET_INSTRUCTION_ARG(src, 0);
  775. GET_INSTRUCTION_ARG(dst, 1);
  776. GET_INSTRUCTION_ARG(to_type, 2);
  777. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  778. GD_ERR_BREAK(!base_type);
  779. #ifdef DEBUG_ENABLED
  780. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  781. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  782. OPCODE_BREAK;
  783. }
  784. #endif
  785. bool valid = false;
  786. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  787. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  788. if (scr_inst) {
  789. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  790. while (src_type) {
  791. if (src_type == base_type) {
  792. valid = true;
  793. break;
  794. }
  795. src_type = src_type->get_base_script().ptr();
  796. }
  797. }
  798. }
  799. if (valid) {
  800. *dst = *src; // Valid cast, copy the source object
  801. } else {
  802. *dst = Variant(); // invalid cast, assign NULL
  803. }
  804. ip += 4;
  805. }
  806. DISPATCH_OPCODE;
  807. OPCODE(OPCODE_CONSTRUCT) {
  808. CHECK_SPACE(2 + instr_arg_count);
  809. ip += instr_arg_count;
  810. int argc = _code_ptr[ip + 1];
  811. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  812. Variant **argptrs = instruction_args;
  813. GET_INSTRUCTION_ARG(dst, argc);
  814. Callable::CallError err;
  815. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  816. #ifdef DEBUG_ENABLED
  817. if (err.error != Callable::CallError::CALL_OK) {
  818. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  819. OPCODE_BREAK;
  820. }
  821. #endif
  822. ip += 3;
  823. }
  824. DISPATCH_OPCODE;
  825. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  826. CHECK_SPACE(1 + instr_arg_count);
  827. ip += instr_arg_count;
  828. int argc = _code_ptr[ip + 1];
  829. Array array;
  830. array.resize(argc);
  831. for (int i = 0; i < argc; i++) {
  832. array[i] = *(instruction_args[i]);
  833. }
  834. GET_INSTRUCTION_ARG(dst, argc);
  835. *dst = array;
  836. ip += 2;
  837. }
  838. DISPATCH_OPCODE;
  839. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  840. CHECK_SPACE(2 + instr_arg_count);
  841. ip += instr_arg_count;
  842. int argc = _code_ptr[ip + 1];
  843. Dictionary dict;
  844. for (int i = 0; i < argc; i++) {
  845. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  846. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  847. dict[*k] = *v;
  848. }
  849. GET_INSTRUCTION_ARG(dst, argc * 2);
  850. *dst = dict;
  851. ip += 2;
  852. }
  853. DISPATCH_OPCODE;
  854. OPCODE(OPCODE_CALL_ASYNC)
  855. OPCODE(OPCODE_CALL_RETURN)
  856. OPCODE(OPCODE_CALL) {
  857. CHECK_SPACE(3 + instr_arg_count);
  858. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  859. #ifdef DEBUG_ENABLED
  860. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  861. #endif
  862. ip += instr_arg_count;
  863. int argc = _code_ptr[ip + 1];
  864. GD_ERR_BREAK(argc < 0);
  865. int methodname_idx = _code_ptr[ip + 2];
  866. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  867. const StringName *methodname = &_global_names_ptr[methodname_idx];
  868. GET_INSTRUCTION_ARG(base, argc);
  869. Variant **argptrs = instruction_args;
  870. #ifdef DEBUG_ENABLED
  871. uint64_t call_time = 0;
  872. if (GDScriptLanguage::get_singleton()->profiling) {
  873. call_time = OS::get_singleton()->get_ticks_usec();
  874. }
  875. #endif
  876. Callable::CallError err;
  877. if (call_ret) {
  878. GET_INSTRUCTION_ARG(ret, argc + 1);
  879. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  880. #ifdef DEBUG_ENABLED
  881. if (!call_async && ret->get_type() == Variant::OBJECT) {
  882. // Check if getting a function state without await.
  883. bool was_freed = false;
  884. Object *obj = ret->get_validated_object_with_check(was_freed);
  885. if (was_freed) {
  886. err_text = "Got a freed object as a result of the call.";
  887. OPCODE_BREAK;
  888. }
  889. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  890. err_text = R"(Trying to call an async function without "await".)";
  891. OPCODE_BREAK;
  892. }
  893. }
  894. #endif
  895. } else {
  896. Variant ret;
  897. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  898. }
  899. #ifdef DEBUG_ENABLED
  900. if (GDScriptLanguage::get_singleton()->profiling) {
  901. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  902. }
  903. if (err.error != Callable::CallError::CALL_OK) {
  904. String methodstr = *methodname;
  905. String basestr = _get_var_type(base);
  906. if (methodstr == "call") {
  907. if (argc >= 1) {
  908. methodstr = String(*argptrs[0]) + " (via call)";
  909. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  910. err.argument += 1;
  911. }
  912. }
  913. } else if (methodstr == "free") {
  914. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  915. if (base->is_ref()) {
  916. err_text = "Attempted to free a reference.";
  917. OPCODE_BREAK;
  918. } else if (base->get_type() == Variant::OBJECT) {
  919. err_text = "Attempted to free a locked object (calling or emitting).";
  920. OPCODE_BREAK;
  921. }
  922. }
  923. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  924. if (argc >= 1) {
  925. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  926. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  927. err.argument += 1;
  928. }
  929. }
  930. }
  931. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  932. OPCODE_BREAK;
  933. }
  934. #endif
  935. ip += 3;
  936. }
  937. DISPATCH_OPCODE;
  938. OPCODE(OPCODE_CALL_BUILT_IN) {
  939. CHECK_SPACE(3 + instr_arg_count);
  940. ip += instr_arg_count;
  941. int argc = _code_ptr[ip + 1];
  942. GD_ERR_BREAK(argc < 0);
  943. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 2]);
  944. Variant **argptrs = instruction_args;
  945. GET_INSTRUCTION_ARG(dst, argc);
  946. Callable::CallError err;
  947. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  948. #ifdef DEBUG_ENABLED
  949. if (err.error != Callable::CallError::CALL_OK) {
  950. String methodstr = GDScriptFunctions::get_func_name(func);
  951. if (dst->get_type() == Variant::STRING) {
  952. //call provided error string
  953. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  954. } else {
  955. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  956. }
  957. OPCODE_BREAK;
  958. }
  959. #endif
  960. ip += 3;
  961. }
  962. DISPATCH_OPCODE;
  963. OPCODE(OPCODE_CALL_SELF_BASE) {
  964. CHECK_SPACE(3 + instr_arg_count);
  965. ip += instr_arg_count;
  966. int self_fun = _code_ptr[ip + 1];
  967. #ifdef DEBUG_ENABLED
  968. if (self_fun < 0 || self_fun >= _global_names_count) {
  969. err_text = "compiler bug, function name not found";
  970. OPCODE_BREAK;
  971. }
  972. #endif
  973. const StringName *methodname = &_global_names_ptr[self_fun];
  974. int argc = _code_ptr[ip + 2];
  975. GD_ERR_BREAK(argc < 0);
  976. Variant **argptrs = instruction_args;
  977. GET_INSTRUCTION_ARG(dst, argc);
  978. const GDScript *gds = _script;
  979. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  980. while (gds->base.ptr()) {
  981. gds = gds->base.ptr();
  982. E = gds->member_functions.find(*methodname);
  983. if (E) {
  984. break;
  985. }
  986. }
  987. Callable::CallError err;
  988. if (E) {
  989. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  990. } else if (gds->native.ptr()) {
  991. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  992. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  993. if (!mb) {
  994. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  995. } else {
  996. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  997. }
  998. } else {
  999. err.error = Callable::CallError::CALL_OK;
  1000. }
  1001. } else {
  1002. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1003. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1004. } else {
  1005. err.error = Callable::CallError::CALL_OK;
  1006. }
  1007. }
  1008. if (err.error != Callable::CallError::CALL_OK) {
  1009. String methodstr = *methodname;
  1010. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1011. OPCODE_BREAK;
  1012. }
  1013. ip += 3;
  1014. }
  1015. DISPATCH_OPCODE;
  1016. OPCODE(OPCODE_AWAIT) {
  1017. CHECK_SPACE(2);
  1018. // Do the oneshot connect.
  1019. GET_INSTRUCTION_ARG(argobj, 0);
  1020. Signal sig;
  1021. bool is_signal = true;
  1022. {
  1023. Variant result = *argobj;
  1024. if (argobj->get_type() == Variant::OBJECT) {
  1025. bool was_freed = false;
  1026. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1027. if (was_freed) {
  1028. err_text = "Trying to await on a freed object.";
  1029. OPCODE_BREAK;
  1030. }
  1031. // Is this even possible to be null at this point?
  1032. if (obj) {
  1033. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1034. static StringName completed = _scs_create("completed");
  1035. result = Signal(obj, completed);
  1036. }
  1037. }
  1038. }
  1039. if (result.get_type() != Variant::SIGNAL) {
  1040. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1041. // The stack pointer should be the same, so we don't need to set a return value.
  1042. is_signal = false;
  1043. } else {
  1044. sig = result;
  1045. }
  1046. }
  1047. if (is_signal) {
  1048. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1049. gdfs->function = this;
  1050. gdfs->state.stack.resize(alloca_size);
  1051. //copy variant stack
  1052. for (int i = 0; i < _stack_size; i++) {
  1053. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1054. }
  1055. gdfs->state.stack_size = _stack_size;
  1056. gdfs->state.self = self;
  1057. gdfs->state.alloca_size = alloca_size;
  1058. gdfs->state.ip = ip + 2;
  1059. gdfs->state.line = line;
  1060. gdfs->state.script = _script;
  1061. {
  1062. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1063. _script->pending_func_states.add(&gdfs->scripts_list);
  1064. if (p_instance) {
  1065. gdfs->state.instance = p_instance;
  1066. p_instance->pending_func_states.add(&gdfs->instances_list);
  1067. } else {
  1068. gdfs->state.instance = nullptr;
  1069. }
  1070. }
  1071. #ifdef DEBUG_ENABLED
  1072. gdfs->state.function_name = name;
  1073. gdfs->state.script_path = _script->get_path();
  1074. #endif
  1075. gdfs->state.defarg = defarg;
  1076. gdfs->function = this;
  1077. retvalue = gdfs;
  1078. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1079. if (err != OK) {
  1080. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1081. OPCODE_BREAK;
  1082. }
  1083. #ifdef DEBUG_ENABLED
  1084. exit_ok = true;
  1085. awaited = true;
  1086. #endif
  1087. OPCODE_BREAK;
  1088. }
  1089. }
  1090. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1091. OPCODE(OPCODE_AWAIT_RESUME) {
  1092. CHECK_SPACE(2);
  1093. #ifdef DEBUG_ENABLED
  1094. if (!p_state) {
  1095. err_text = ("Invalid Resume (bug?)");
  1096. OPCODE_BREAK;
  1097. }
  1098. #endif
  1099. GET_INSTRUCTION_ARG(result, 0);
  1100. *result = p_state->result;
  1101. ip += 2;
  1102. }
  1103. DISPATCH_OPCODE;
  1104. OPCODE(OPCODE_JUMP) {
  1105. CHECK_SPACE(2);
  1106. int to = _code_ptr[ip + 1];
  1107. GD_ERR_BREAK(to < 0 || to > _code_size);
  1108. ip = to;
  1109. }
  1110. DISPATCH_OPCODE;
  1111. OPCODE(OPCODE_JUMP_IF) {
  1112. CHECK_SPACE(3);
  1113. GET_INSTRUCTION_ARG(test, 0);
  1114. bool result = test->booleanize();
  1115. if (result) {
  1116. int to = _code_ptr[ip + 2];
  1117. GD_ERR_BREAK(to < 0 || to > _code_size);
  1118. ip = to;
  1119. } else {
  1120. ip += 3;
  1121. }
  1122. }
  1123. DISPATCH_OPCODE;
  1124. OPCODE(OPCODE_JUMP_IF_NOT) {
  1125. CHECK_SPACE(3);
  1126. GET_INSTRUCTION_ARG(test, 0);
  1127. bool result = test->booleanize();
  1128. if (!result) {
  1129. int to = _code_ptr[ip + 2];
  1130. GD_ERR_BREAK(to < 0 || to > _code_size);
  1131. ip = to;
  1132. } else {
  1133. ip += 3;
  1134. }
  1135. }
  1136. DISPATCH_OPCODE;
  1137. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1138. CHECK_SPACE(2);
  1139. ip = _default_arg_ptr[defarg];
  1140. }
  1141. DISPATCH_OPCODE;
  1142. OPCODE(OPCODE_RETURN) {
  1143. CHECK_SPACE(2);
  1144. GET_INSTRUCTION_ARG(r, 0);
  1145. retvalue = *r;
  1146. #ifdef DEBUG_ENABLED
  1147. exit_ok = true;
  1148. #endif
  1149. OPCODE_BREAK;
  1150. }
  1151. OPCODE(OPCODE_ITERATE_BEGIN) {
  1152. CHECK_SPACE(8); // Space for this and a regular iterate.
  1153. GET_INSTRUCTION_ARG(counter, 0);
  1154. GET_INSTRUCTION_ARG(container, 1);
  1155. bool valid;
  1156. if (!container->iter_init(*counter, valid)) {
  1157. #ifdef DEBUG_ENABLED
  1158. if (!valid) {
  1159. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1160. OPCODE_BREAK;
  1161. }
  1162. #endif
  1163. int jumpto = _code_ptr[ip + 4];
  1164. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1165. ip = jumpto;
  1166. } else {
  1167. GET_INSTRUCTION_ARG(iterator, 2);
  1168. *iterator = container->iter_get(*counter, valid);
  1169. #ifdef DEBUG_ENABLED
  1170. if (!valid) {
  1171. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1172. OPCODE_BREAK;
  1173. }
  1174. #endif
  1175. ip += 5; // Skip regular iterate which is always next.
  1176. }
  1177. }
  1178. DISPATCH_OPCODE;
  1179. OPCODE(OPCODE_ITERATE) {
  1180. CHECK_SPACE(4);
  1181. GET_INSTRUCTION_ARG(counter, 0);
  1182. GET_INSTRUCTION_ARG(container, 1);
  1183. bool valid;
  1184. if (!container->iter_next(*counter, valid)) {
  1185. #ifdef DEBUG_ENABLED
  1186. if (!valid) {
  1187. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1188. OPCODE_BREAK;
  1189. }
  1190. #endif
  1191. int jumpto = _code_ptr[ip + 4];
  1192. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1193. ip = jumpto;
  1194. } else {
  1195. GET_INSTRUCTION_ARG(iterator, 2);
  1196. *iterator = container->iter_get(*counter, valid);
  1197. #ifdef DEBUG_ENABLED
  1198. if (!valid) {
  1199. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1200. OPCODE_BREAK;
  1201. }
  1202. #endif
  1203. ip += 5; //loop again
  1204. }
  1205. }
  1206. DISPATCH_OPCODE;
  1207. OPCODE(OPCODE_ASSERT) {
  1208. CHECK_SPACE(3);
  1209. #ifdef DEBUG_ENABLED
  1210. GET_INSTRUCTION_ARG(test, 0);
  1211. bool result = test->booleanize();
  1212. if (!result) {
  1213. String message_str;
  1214. if (_code_ptr[ip + 2] != 0) {
  1215. GET_INSTRUCTION_ARG(message, 1);
  1216. message_str = *message;
  1217. }
  1218. if (message_str.empty()) {
  1219. err_text = "Assertion failed.";
  1220. } else {
  1221. err_text = "Assertion failed: " + message_str;
  1222. }
  1223. OPCODE_BREAK;
  1224. }
  1225. #endif
  1226. ip += 3;
  1227. }
  1228. DISPATCH_OPCODE;
  1229. OPCODE(OPCODE_BREAKPOINT) {
  1230. #ifdef DEBUG_ENABLED
  1231. if (EngineDebugger::is_active()) {
  1232. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1233. }
  1234. #endif
  1235. ip += 1;
  1236. }
  1237. DISPATCH_OPCODE;
  1238. OPCODE(OPCODE_LINE) {
  1239. CHECK_SPACE(2);
  1240. line = _code_ptr[ip + 1];
  1241. ip += 2;
  1242. if (EngineDebugger::is_active()) {
  1243. // line
  1244. bool do_break = false;
  1245. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  1246. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  1247. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  1248. }
  1249. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  1250. do_break = true;
  1251. }
  1252. }
  1253. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  1254. do_break = true;
  1255. }
  1256. if (do_break) {
  1257. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1258. }
  1259. EngineDebugger::get_singleton()->line_poll();
  1260. }
  1261. }
  1262. DISPATCH_OPCODE;
  1263. OPCODE(OPCODE_END) {
  1264. #ifdef DEBUG_ENABLED
  1265. exit_ok = true;
  1266. #endif
  1267. OPCODE_BREAK;
  1268. }
  1269. #if 0 // Enable for debugging.
  1270. default: {
  1271. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1272. OPCODE_BREAK;
  1273. }
  1274. #endif
  1275. }
  1276. OPCODES_END
  1277. #ifdef DEBUG_ENABLED
  1278. if (exit_ok) {
  1279. OPCODE_OUT;
  1280. }
  1281. //error
  1282. // function, file, line, error, explanation
  1283. String err_file;
  1284. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  1285. err_file = p_instance->script->path;
  1286. } else if (script) {
  1287. err_file = script->path;
  1288. }
  1289. if (err_file == "") {
  1290. err_file = "<built-in>";
  1291. }
  1292. String err_func = name;
  1293. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  1294. err_func = p_instance->script->name + "." + err_func;
  1295. }
  1296. int err_line = line;
  1297. if (err_text == "") {
  1298. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1299. }
  1300. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1301. // debugger break did not happen
  1302. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1303. }
  1304. #endif
  1305. OPCODE_OUT;
  1306. }
  1307. OPCODES_OUT
  1308. #ifdef DEBUG_ENABLED
  1309. if (GDScriptLanguage::get_singleton()->profiling) {
  1310. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1311. profile.total_time += time_taken;
  1312. profile.self_time += time_taken - function_call_time;
  1313. profile.frame_total_time += time_taken;
  1314. profile.frame_self_time += time_taken - function_call_time;
  1315. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1316. }
  1317. // Check if this is the last time the function is resuming from await
  1318. // Will be true if never awaited as well
  1319. // When it's the last resume it will postpone the exit from stack,
  1320. // so the debugger knows which function triggered the resume of the next function (if any)
  1321. if (!p_state || awaited) {
  1322. if (EngineDebugger::is_active()) {
  1323. GDScriptLanguage::get_singleton()->exit_function();
  1324. }
  1325. #endif
  1326. if (_stack_size) {
  1327. //free stack
  1328. for (int i = 0; i < _stack_size; i++) {
  1329. stack[i].~Variant();
  1330. }
  1331. }
  1332. #ifdef DEBUG_ENABLED
  1333. }
  1334. #endif
  1335. return retvalue;
  1336. }