gdscript_function.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "gdscript.h"
  32. #include "gdscript_functions.h"
  33. #include "os/os.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, 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 NULL;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &p_script->_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 NULL;
  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 *o = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, NULL);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (o) {
  68. GDScript *s = o;
  69. while (s) {
  70. Map<StringName, Variant>::Element *E = s->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. s = s->_base;
  75. }
  76. o = o->_owner;
  77. }
  78. ERR_EXPLAIN("GDScriptCompiler bug...");
  79. ERR_FAIL_V(NULL);
  80. } break;
  81. case ADDR_TYPE_LOCAL_CONSTANT: {
  82. #ifdef DEBUG_ENABLED
  83. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  84. #endif
  85. return &_constants_ptr[address];
  86. } break;
  87. case ADDR_TYPE_STACK:
  88. case ADDR_TYPE_STACK_VARIABLE: {
  89. #ifdef DEBUG_ENABLED
  90. ERR_FAIL_INDEX_V(address, _stack_size, NULL);
  91. #endif
  92. return &p_stack[address];
  93. } break;
  94. case ADDR_TYPE_GLOBAL: {
  95. #ifdef DEBUG_ENABLED
  96. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL);
  97. #endif
  98. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  99. } break;
  100. #ifdef TOOLS_ENABLED
  101. case ADDR_TYPE_NAMED_GLOBAL: {
  102. #ifdef DEBUG_ENABLED
  103. ERR_FAIL_INDEX_V(address, _named_globals_count, NULL);
  104. #endif
  105. StringName id = _named_globals_ptr[address];
  106. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  107. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  108. } else {
  109. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  110. return NULL;
  111. }
  112. } break;
  113. #endif
  114. case ADDR_TYPE_NIL: {
  115. return &nil;
  116. } break;
  117. }
  118. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  119. ERR_FAIL_V(NULL);
  120. return NULL;
  121. }
  122. String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  123. String err_text;
  124. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  125. int errorarg = p_err.argument;
  126. 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(p_err.expected) + ".";
  127. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  128. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  129. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  130. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  131. } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  132. err_text = "Invalid call. Nonexistent " + p_where + ".";
  133. } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  134. err_text = "Attempt to call " + p_where + " on a null instance.";
  135. } else {
  136. err_text = "Bug, call error: #" + itos(p_err.error);
  137. }
  138. return err_text;
  139. }
  140. static String _get_var_type(const Variant *p_type) {
  141. String basestr;
  142. if (p_type->get_type() == Variant::OBJECT) {
  143. Object *bobj = *p_type;
  144. if (!bobj) {
  145. basestr = "null instance";
  146. } else {
  147. #ifdef DEBUG_ENABLED
  148. if (ObjectDB::instance_validate(bobj)) {
  149. if (bobj->get_script_instance())
  150. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  151. else
  152. basestr = bobj->get_class();
  153. } else {
  154. basestr = "previously freed instance";
  155. }
  156. #else
  157. basestr = "Object";
  158. #endif
  159. }
  160. } else {
  161. basestr = Variant::get_type_name(p_type->get_type());
  162. }
  163. return basestr;
  164. }
  165. #if defined(__GNUC__)
  166. #define OPCODES_TABLE \
  167. static const void *switch_table_ops[] = { \
  168. &&OPCODE_OPERATOR, \
  169. &&OPCODE_EXTENDS_TEST, \
  170. &&OPCODE_SET, \
  171. &&OPCODE_GET, \
  172. &&OPCODE_SET_NAMED, \
  173. &&OPCODE_GET_NAMED, \
  174. &&OPCODE_SET_MEMBER, \
  175. &&OPCODE_GET_MEMBER, \
  176. &&OPCODE_ASSIGN, \
  177. &&OPCODE_ASSIGN_TRUE, \
  178. &&OPCODE_ASSIGN_FALSE, \
  179. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  180. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  181. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  182. &&OPCODE_CAST_TO_BUILTIN, \
  183. &&OPCODE_CAST_TO_NATIVE, \
  184. &&OPCODE_CAST_TO_SCRIPT, \
  185. &&OPCODE_CONSTRUCT, \
  186. &&OPCODE_CONSTRUCT_ARRAY, \
  187. &&OPCODE_CONSTRUCT_DICTIONARY, \
  188. &&OPCODE_CALL, \
  189. &&OPCODE_CALL_RETURN, \
  190. &&OPCODE_CALL_BUILT_IN, \
  191. &&OPCODE_CALL_SELF, \
  192. &&OPCODE_CALL_SELF_BASE, \
  193. &&OPCODE_YIELD, \
  194. &&OPCODE_YIELD_SIGNAL, \
  195. &&OPCODE_YIELD_RESUME, \
  196. &&OPCODE_JUMP, \
  197. &&OPCODE_JUMP_IF, \
  198. &&OPCODE_JUMP_IF_NOT, \
  199. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  200. &&OPCODE_RETURN, \
  201. &&OPCODE_ITERATE_BEGIN, \
  202. &&OPCODE_ITERATE, \
  203. &&OPCODE_ASSERT, \
  204. &&OPCODE_BREAKPOINT, \
  205. &&OPCODE_LINE, \
  206. &&OPCODE_END \
  207. };
  208. #define OPCODE(m_op) \
  209. m_op:
  210. #define OPCODE_WHILE(m_test)
  211. #define OPCODES_END \
  212. OPSEXIT:
  213. #define OPCODES_OUT \
  214. OPSOUT:
  215. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  216. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  217. #define OPCODE_BREAK goto OPSEXIT
  218. #define OPCODE_OUT goto OPSOUT
  219. #else
  220. #define OPCODES_TABLE
  221. #define OPCODE(m_op) case m_op:
  222. #define OPCODE_WHILE(m_test) while (m_test)
  223. #define OPCODES_END
  224. #define OPCODES_OUT
  225. #define DISPATCH_OPCODE continue
  226. #define OPCODE_SWITCH(m_test) switch (m_test)
  227. #define OPCODE_BREAK break
  228. #define OPCODE_OUT break
  229. #endif
  230. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  231. OPCODES_TABLE;
  232. if (!_code_ptr) {
  233. return Variant();
  234. }
  235. r_err.error = Variant::CallError::CALL_OK;
  236. Variant self;
  237. Variant retvalue;
  238. Variant *stack = NULL;
  239. Variant **call_args;
  240. int defarg = 0;
  241. #ifdef DEBUG_ENABLED
  242. //GDScriptLanguage::get_singleton()->calls++;
  243. #endif
  244. uint32_t alloca_size = 0;
  245. GDScript *_class;
  246. int ip = 0;
  247. int line = _initial_line;
  248. if (p_state) {
  249. //use existing (supplied) state (yielded)
  250. stack = (Variant *)p_state->stack.ptr();
  251. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  252. line = p_state->line;
  253. ip = p_state->ip;
  254. alloca_size = p_state->stack.size();
  255. _class = p_state->_class;
  256. p_instance = p_state->instance;
  257. defarg = p_state->defarg;
  258. self = p_state->self;
  259. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  260. } else {
  261. if (p_argcount != _argument_count) {
  262. if (p_argcount > _argument_count) {
  263. r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  264. r_err.argument = _argument_count;
  265. return Variant();
  266. } else if (p_argcount < _argument_count - _default_arg_count) {
  267. r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  268. r_err.argument = _argument_count - _default_arg_count;
  269. return Variant();
  270. } else {
  271. defarg = _argument_count - p_argcount;
  272. }
  273. }
  274. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  275. if (alloca_size) {
  276. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  277. if (_stack_size) {
  278. stack = (Variant *)aptr;
  279. for (int i = 0; i < p_argcount; i++) {
  280. if (!argument_types[i].has_type) {
  281. memnew_placement(&stack[i], Variant(*p_args[i]));
  282. continue;
  283. }
  284. if (!argument_types[i].is_type(*p_args[i], true)) {
  285. r_err.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  286. r_err.argument = i;
  287. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  288. return Variant();
  289. }
  290. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  291. Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err);
  292. memnew_placement(&stack[i], Variant(arg));
  293. } else {
  294. memnew_placement(&stack[i], Variant(*p_args[i]));
  295. }
  296. }
  297. for (int i = p_argcount; i < _stack_size; i++) {
  298. memnew_placement(&stack[i], Variant);
  299. }
  300. } else {
  301. stack = NULL;
  302. }
  303. if (_call_size) {
  304. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  305. } else {
  306. call_args = NULL;
  307. }
  308. } else {
  309. stack = NULL;
  310. call_args = NULL;
  311. }
  312. if (p_instance) {
  313. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  314. self = REF(static_cast<Reference *>(p_instance->owner));
  315. } else {
  316. self = p_instance->owner;
  317. }
  318. _class = p_instance->script.ptr();
  319. } else {
  320. _class = _script;
  321. }
  322. }
  323. String err_text;
  324. #ifdef DEBUG_ENABLED
  325. if (ScriptDebugger::get_singleton())
  326. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  327. #define GD_ERR_BREAK(m_cond) \
  328. { \
  329. if (unlikely(m_cond)) { \
  330. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  331. OPCODE_BREAK; \
  332. } else \
  333. _err_error_exists = false; \
  334. }
  335. #define CHECK_SPACE(m_space) \
  336. GD_ERR_BREAK((ip + m_space) > _code_size)
  337. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  338. Variant *m_v; \
  339. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text); \
  340. if (unlikely(!m_v)) \
  341. OPCODE_BREAK;
  342. #else
  343. #define GD_ERR_BREAK(m_cond)
  344. #define CHECK_SPACE(m_space)
  345. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  346. Variant *m_v; \
  347. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text);
  348. #endif
  349. #ifdef DEBUG_ENABLED
  350. uint64_t function_start_time = 0;
  351. uint64_t function_call_time = 0;
  352. if (GDScriptLanguage::get_singleton()->profiling) {
  353. function_start_time = OS::get_singleton()->get_ticks_usec();
  354. function_call_time = 0;
  355. profile.call_count++;
  356. profile.frame_call_count++;
  357. }
  358. #endif
  359. bool exit_ok = false;
  360. #ifdef DEBUG_ENABLED
  361. OPCODE_WHILE(ip < _code_size) {
  362. int last_opcode = _code_ptr[ip];
  363. #else
  364. OPCODE_WHILE(true) {
  365. #endif
  366. OPCODE_SWITCH(_code_ptr[ip]) {
  367. OPCODE(OPCODE_OPERATOR) {
  368. CHECK_SPACE(5);
  369. bool valid;
  370. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  371. GD_ERR_BREAK(op >= Variant::OP_MAX);
  372. GET_VARIANT_PTR(a, 2);
  373. GET_VARIANT_PTR(b, 3);
  374. GET_VARIANT_PTR(dst, 4);
  375. #ifdef DEBUG_ENABLED
  376. Variant ret;
  377. Variant::evaluate(op, *a, *b, ret, valid);
  378. #else
  379. Variant::evaluate(op, *a, *b, *dst, valid);
  380. #endif
  381. #ifdef DEBUG_ENABLED
  382. if (!valid) {
  383. if (ret.get_type() == Variant::STRING) {
  384. //return a string when invalid with the error
  385. err_text = ret;
  386. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  387. } else {
  388. 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) + "'.";
  389. }
  390. OPCODE_BREAK;
  391. }
  392. *dst = ret;
  393. #endif
  394. ip += 5;
  395. }
  396. DISPATCH_OPCODE;
  397. OPCODE(OPCODE_EXTENDS_TEST) {
  398. CHECK_SPACE(4);
  399. GET_VARIANT_PTR(a, 1);
  400. GET_VARIANT_PTR(b, 2);
  401. GET_VARIANT_PTR(dst, 3);
  402. #ifdef DEBUG_ENABLED
  403. if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) {
  404. err_text = "Left operand of 'is' is not an instance of anything.";
  405. OPCODE_BREAK;
  406. }
  407. if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) {
  408. err_text = "Right operand of 'is' is not a class.";
  409. OPCODE_BREAK;
  410. }
  411. #endif
  412. Object *obj_A = *a;
  413. Object *obj_B = *b;
  414. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  415. bool extends_ok = false;
  416. if (scr_B) {
  417. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  418. //in other situation, this shoul return false.
  419. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  420. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  421. //bool found=false;
  422. while (cmp) {
  423. if (cmp == scr_B) {
  424. //inherits from script, all ok
  425. extends_ok = true;
  426. break;
  427. }
  428. cmp = cmp->_base;
  429. }
  430. }
  431. } else {
  432. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  433. #ifdef DEBUG_ENABLED
  434. if (!nc) {
  435. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  436. OPCODE_BREAK;
  437. }
  438. #endif
  439. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  440. }
  441. *dst = extends_ok;
  442. ip += 4;
  443. }
  444. DISPATCH_OPCODE;
  445. OPCODE(OPCODE_SET) {
  446. CHECK_SPACE(3);
  447. GET_VARIANT_PTR(dst, 1);
  448. GET_VARIANT_PTR(index, 2);
  449. GET_VARIANT_PTR(value, 3);
  450. bool valid;
  451. dst->set(*index, *value, &valid);
  452. #ifdef DEBUG_ENABLED
  453. if (!valid) {
  454. String v = index->operator String();
  455. if (v != "") {
  456. v = "'" + v + "'";
  457. } else {
  458. v = "of type '" + _get_var_type(index) + "'";
  459. }
  460. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  461. OPCODE_BREAK;
  462. }
  463. #endif
  464. ip += 4;
  465. }
  466. DISPATCH_OPCODE;
  467. OPCODE(OPCODE_GET) {
  468. CHECK_SPACE(3);
  469. GET_VARIANT_PTR(src, 1);
  470. GET_VARIANT_PTR(index, 2);
  471. GET_VARIANT_PTR(dst, 3);
  472. bool valid;
  473. #ifdef DEBUG_ENABLED
  474. //allow better error message in cases where src and dst are the same stack position
  475. Variant ret = src->get(*index, &valid);
  476. #else
  477. *dst = src->get(*index, &valid);
  478. #endif
  479. #ifdef DEBUG_ENABLED
  480. if (!valid) {
  481. String v = index->operator String();
  482. if (v != "") {
  483. v = "'" + v + "'";
  484. } else {
  485. v = "of type '" + _get_var_type(index) + "'";
  486. }
  487. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  488. OPCODE_BREAK;
  489. }
  490. *dst = ret;
  491. #endif
  492. ip += 4;
  493. }
  494. DISPATCH_OPCODE;
  495. OPCODE(OPCODE_SET_NAMED) {
  496. CHECK_SPACE(3);
  497. GET_VARIANT_PTR(dst, 1);
  498. GET_VARIANT_PTR(value, 3);
  499. int indexname = _code_ptr[ip + 2];
  500. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  501. const StringName *index = &_global_names_ptr[indexname];
  502. bool valid;
  503. dst->set_named(*index, *value, &valid);
  504. #ifdef DEBUG_ENABLED
  505. if (!valid) {
  506. String err_type;
  507. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  508. OPCODE_BREAK;
  509. }
  510. #endif
  511. ip += 4;
  512. }
  513. DISPATCH_OPCODE;
  514. OPCODE(OPCODE_GET_NAMED) {
  515. CHECK_SPACE(4);
  516. GET_VARIANT_PTR(src, 1);
  517. GET_VARIANT_PTR(dst, 3);
  518. int indexname = _code_ptr[ip + 2];
  519. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  520. const StringName *index = &_global_names_ptr[indexname];
  521. bool valid;
  522. #ifdef DEBUG_ENABLED
  523. //allow better error message in cases where src and dst are the same stack position
  524. Variant ret = src->get_named(*index, &valid);
  525. #else
  526. *dst = src->get_named(*index, &valid);
  527. #endif
  528. #ifdef DEBUG_ENABLED
  529. if (!valid) {
  530. if (src->has_method(*index)) {
  531. 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() + "\") ?";
  532. } else {
  533. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  534. }
  535. OPCODE_BREAK;
  536. }
  537. *dst = ret;
  538. #endif
  539. ip += 4;
  540. }
  541. DISPATCH_OPCODE;
  542. OPCODE(OPCODE_SET_MEMBER) {
  543. CHECK_SPACE(3);
  544. int indexname = _code_ptr[ip + 1];
  545. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  546. const StringName *index = &_global_names_ptr[indexname];
  547. GET_VARIANT_PTR(src, 2);
  548. bool valid;
  549. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  550. #ifdef DEBUG_ENABLED
  551. if (!ok) {
  552. err_text = "Internal error setting property: " + String(*index);
  553. OPCODE_BREAK;
  554. } else if (!valid) {
  555. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  556. OPCODE_BREAK;
  557. }
  558. #endif
  559. ip += 3;
  560. }
  561. DISPATCH_OPCODE;
  562. OPCODE(OPCODE_GET_MEMBER) {
  563. CHECK_SPACE(3);
  564. int indexname = _code_ptr[ip + 1];
  565. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  566. const StringName *index = &_global_names_ptr[indexname];
  567. GET_VARIANT_PTR(dst, 2);
  568. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  569. #ifdef DEBUG_ENABLED
  570. if (!ok) {
  571. err_text = "Internal error getting property: " + String(*index);
  572. OPCODE_BREAK;
  573. }
  574. #endif
  575. ip += 3;
  576. }
  577. DISPATCH_OPCODE;
  578. OPCODE(OPCODE_ASSIGN) {
  579. CHECK_SPACE(3);
  580. GET_VARIANT_PTR(dst, 1);
  581. GET_VARIANT_PTR(src, 2);
  582. *dst = *src;
  583. ip += 3;
  584. }
  585. DISPATCH_OPCODE;
  586. OPCODE(OPCODE_ASSIGN_TRUE) {
  587. CHECK_SPACE(2);
  588. GET_VARIANT_PTR(dst, 1);
  589. *dst = true;
  590. ip += 2;
  591. }
  592. DISPATCH_OPCODE;
  593. OPCODE(OPCODE_ASSIGN_FALSE) {
  594. CHECK_SPACE(2);
  595. GET_VARIANT_PTR(dst, 1);
  596. *dst = false;
  597. ip += 2;
  598. }
  599. DISPATCH_OPCODE;
  600. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  601. CHECK_SPACE(4);
  602. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  603. GET_VARIANT_PTR(dst, 2);
  604. GET_VARIANT_PTR(src, 3);
  605. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  606. if (src->get_type() != var_type) {
  607. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  608. Variant::CallError ce;
  609. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  610. } else {
  611. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  612. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  613. OPCODE_BREAK;
  614. }
  615. } else {
  616. *dst = *src;
  617. }
  618. ip += 4;
  619. }
  620. DISPATCH_OPCODE;
  621. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  622. CHECK_SPACE(4);
  623. GET_VARIANT_PTR(type, 1);
  624. GET_VARIANT_PTR(dst, 2);
  625. GET_VARIANT_PTR(src, 3);
  626. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  627. GD_ERR_BREAK(!nc);
  628. Object *src_obj = src->operator Object *();
  629. GD_ERR_BREAK(!src_obj);
  630. if (!ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  631. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  632. "' to a variable of type '" + nc->get_name() + "'.";
  633. OPCODE_BREAK;
  634. }
  635. *dst = *src;
  636. ip += 4;
  637. }
  638. DISPATCH_OPCODE;
  639. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  640. CHECK_SPACE(4);
  641. GET_VARIANT_PTR(type, 1);
  642. GET_VARIANT_PTR(dst, 2);
  643. GET_VARIANT_PTR(src, 3);
  644. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  645. GD_ERR_BREAK(!base_type);
  646. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  647. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  648. OPCODE_BREAK;
  649. }
  650. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  651. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  652. if (!scr_inst) {
  653. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  654. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  655. OPCODE_BREAK;
  656. }
  657. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  658. bool valid = false;
  659. while (src_type) {
  660. if (src_type == base_type) {
  661. valid = true;
  662. break;
  663. }
  664. src_type = src_type->get_base_script().ptr();
  665. }
  666. if (!valid) {
  667. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  668. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  669. OPCODE_BREAK;
  670. }
  671. }
  672. *dst = *src;
  673. ip += 4;
  674. }
  675. DISPATCH_OPCODE;
  676. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  677. CHECK_SPACE(4);
  678. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  679. GET_VARIANT_PTR(src, 2);
  680. GET_VARIANT_PTR(dst, 3);
  681. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  682. Variant::CallError err;
  683. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  684. #ifdef DEBUG_ENABLED
  685. if (err.error != Variant::CallError::CALL_OK) {
  686. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  687. OPCODE_BREAK;
  688. }
  689. #endif
  690. ip += 4;
  691. }
  692. DISPATCH_OPCODE;
  693. OPCODE(OPCODE_CAST_TO_NATIVE) {
  694. CHECK_SPACE(4);
  695. GET_VARIANT_PTR(to_type, 1);
  696. GET_VARIANT_PTR(src, 2);
  697. GET_VARIANT_PTR(dst, 3);
  698. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  699. GD_ERR_BREAK(!nc);
  700. #ifdef DEBUG_ENABLED
  701. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  702. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  703. OPCODE_BREAK;
  704. }
  705. #endif
  706. Object *src_obj = src->operator Object *();
  707. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  708. *dst = Variant(); // invalid cast, assign NULL
  709. } else {
  710. *dst = *src;
  711. }
  712. ip += 4;
  713. }
  714. DISPATCH_OPCODE;
  715. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  716. CHECK_SPACE(4);
  717. GET_VARIANT_PTR(to_type, 1);
  718. GET_VARIANT_PTR(src, 2);
  719. GET_VARIANT_PTR(dst, 3);
  720. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  721. GD_ERR_BREAK(!base_type);
  722. #ifdef DEBUG_ENABLED
  723. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  724. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  725. OPCODE_BREAK;
  726. }
  727. #endif
  728. bool valid = false;
  729. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  730. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  731. if (scr_inst) {
  732. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  733. while (src_type) {
  734. if (src_type == base_type) {
  735. valid = true;
  736. break;
  737. }
  738. src_type = src_type->get_base_script().ptr();
  739. }
  740. }
  741. }
  742. if (valid) {
  743. *dst = *src; // Valid cast, copy the source object
  744. } else {
  745. *dst = Variant(); // invalid cast, assign NULL
  746. }
  747. ip += 4;
  748. }
  749. DISPATCH_OPCODE;
  750. OPCODE(OPCODE_CONSTRUCT) {
  751. CHECK_SPACE(2);
  752. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  753. int argc = _code_ptr[ip + 2];
  754. CHECK_SPACE(argc + 2);
  755. Variant **argptrs = call_args;
  756. for (int i = 0; i < argc; i++) {
  757. GET_VARIANT_PTR(v, 3 + i);
  758. argptrs[i] = v;
  759. }
  760. GET_VARIANT_PTR(dst, 3 + argc);
  761. Variant::CallError err;
  762. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  763. #ifdef DEBUG_ENABLED
  764. if (err.error != Variant::CallError::CALL_OK) {
  765. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  766. OPCODE_BREAK;
  767. }
  768. #endif
  769. ip += 4 + argc;
  770. //construct a basic type
  771. }
  772. DISPATCH_OPCODE;
  773. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  774. CHECK_SPACE(1);
  775. int argc = _code_ptr[ip + 1];
  776. Array array; //arrays are always shared
  777. array.resize(argc);
  778. CHECK_SPACE(argc + 2);
  779. for (int i = 0; i < argc; i++) {
  780. GET_VARIANT_PTR(v, 2 + i);
  781. array[i] = *v;
  782. }
  783. GET_VARIANT_PTR(dst, 2 + argc);
  784. *dst = array;
  785. ip += 3 + argc;
  786. }
  787. DISPATCH_OPCODE;
  788. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  789. CHECK_SPACE(1);
  790. int argc = _code_ptr[ip + 1];
  791. Dictionary dict; //arrays are always shared
  792. CHECK_SPACE(argc * 2 + 2);
  793. for (int i = 0; i < argc; i++) {
  794. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  795. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  796. dict[*k] = *v;
  797. }
  798. GET_VARIANT_PTR(dst, 2 + argc * 2);
  799. *dst = dict;
  800. ip += 3 + argc * 2;
  801. }
  802. DISPATCH_OPCODE;
  803. OPCODE(OPCODE_CALL_RETURN)
  804. OPCODE(OPCODE_CALL) {
  805. CHECK_SPACE(4);
  806. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  807. int argc = _code_ptr[ip + 1];
  808. GET_VARIANT_PTR(base, 2);
  809. int nameg = _code_ptr[ip + 3];
  810. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  811. const StringName *methodname = &_global_names_ptr[nameg];
  812. GD_ERR_BREAK(argc < 0);
  813. ip += 4;
  814. CHECK_SPACE(argc + 1);
  815. Variant **argptrs = call_args;
  816. for (int i = 0; i < argc; i++) {
  817. GET_VARIANT_PTR(v, i);
  818. argptrs[i] = v;
  819. }
  820. #ifdef DEBUG_ENABLED
  821. uint64_t call_time = 0;
  822. if (GDScriptLanguage::get_singleton()->profiling) {
  823. call_time = OS::get_singleton()->get_ticks_usec();
  824. }
  825. #endif
  826. Variant::CallError err;
  827. if (call_ret) {
  828. GET_VARIANT_PTR(ret, argc);
  829. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  830. } else {
  831. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  832. }
  833. #ifdef DEBUG_ENABLED
  834. if (GDScriptLanguage::get_singleton()->profiling) {
  835. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  836. }
  837. if (err.error != Variant::CallError::CALL_OK) {
  838. String methodstr = *methodname;
  839. String basestr = _get_var_type(base);
  840. if (methodstr == "call") {
  841. if (argc >= 1) {
  842. methodstr = String(*argptrs[0]) + " (via call)";
  843. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  844. err.argument -= 1;
  845. }
  846. }
  847. } else if (methodstr == "free") {
  848. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  849. if (base->is_ref()) {
  850. err_text = "Attempted to free a reference.";
  851. OPCODE_BREAK;
  852. } else if (base->get_type() == Variant::OBJECT) {
  853. err_text = "Attempted to free a locked object (calling or emitting).";
  854. OPCODE_BREAK;
  855. }
  856. }
  857. }
  858. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  859. OPCODE_BREAK;
  860. }
  861. #endif
  862. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  863. ip += argc + 1;
  864. }
  865. DISPATCH_OPCODE;
  866. OPCODE(OPCODE_CALL_BUILT_IN) {
  867. CHECK_SPACE(4);
  868. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  869. int argc = _code_ptr[ip + 2];
  870. GD_ERR_BREAK(argc < 0);
  871. ip += 3;
  872. CHECK_SPACE(argc + 1);
  873. Variant **argptrs = call_args;
  874. for (int i = 0; i < argc; i++) {
  875. GET_VARIANT_PTR(v, i);
  876. argptrs[i] = v;
  877. }
  878. GET_VARIANT_PTR(dst, argc);
  879. Variant::CallError err;
  880. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  881. #ifdef DEBUG_ENABLED
  882. if (err.error != Variant::CallError::CALL_OK) {
  883. String methodstr = GDScriptFunctions::get_func_name(func);
  884. if (dst->get_type() == Variant::STRING) {
  885. //call provided error string
  886. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  887. } else {
  888. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  889. }
  890. OPCODE_BREAK;
  891. }
  892. #endif
  893. ip += argc + 1;
  894. }
  895. DISPATCH_OPCODE;
  896. OPCODE(OPCODE_CALL_SELF) {
  897. OPCODE_BREAK;
  898. }
  899. OPCODE(OPCODE_CALL_SELF_BASE) {
  900. CHECK_SPACE(2);
  901. int self_fun = _code_ptr[ip + 1];
  902. #ifdef DEBUG_ENABLED
  903. if (self_fun < 0 || self_fun >= _global_names_count) {
  904. err_text = "compiler bug, function name not found";
  905. OPCODE_BREAK;
  906. }
  907. #endif
  908. const StringName *methodname = &_global_names_ptr[self_fun];
  909. int argc = _code_ptr[ip + 2];
  910. CHECK_SPACE(2 + argc + 1);
  911. Variant **argptrs = call_args;
  912. for (int i = 0; i < argc; i++) {
  913. GET_VARIANT_PTR(v, i + 3);
  914. argptrs[i] = v;
  915. }
  916. GET_VARIANT_PTR(dst, argc + 3);
  917. const GDScript *gds = _script;
  918. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  919. while (gds->base.ptr()) {
  920. gds = gds->base.ptr();
  921. E = gds->member_functions.find(*methodname);
  922. if (E)
  923. break;
  924. }
  925. Variant::CallError err;
  926. if (E) {
  927. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  928. } else if (gds->native.ptr()) {
  929. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  930. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  931. if (!mb) {
  932. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  933. } else {
  934. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  935. }
  936. } else {
  937. err.error = Variant::CallError::CALL_OK;
  938. }
  939. } else {
  940. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  941. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  942. } else {
  943. err.error = Variant::CallError::CALL_OK;
  944. }
  945. }
  946. if (err.error != Variant::CallError::CALL_OK) {
  947. String methodstr = *methodname;
  948. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  949. OPCODE_BREAK;
  950. }
  951. ip += 4 + argc;
  952. }
  953. DISPATCH_OPCODE;
  954. OPCODE(OPCODE_YIELD)
  955. OPCODE(OPCODE_YIELD_SIGNAL) {
  956. int ipofs = 1;
  957. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  958. CHECK_SPACE(4);
  959. ipofs += 2;
  960. } else {
  961. CHECK_SPACE(2);
  962. }
  963. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  964. gdfs->function = this;
  965. gdfs->state.stack.resize(alloca_size);
  966. //copy variant stack
  967. for (int i = 0; i < _stack_size; i++) {
  968. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  969. }
  970. gdfs->state.stack_size = _stack_size;
  971. gdfs->state.self = self;
  972. gdfs->state.alloca_size = alloca_size;
  973. gdfs->state._class = _class;
  974. gdfs->state.ip = ip + ipofs;
  975. gdfs->state.line = line;
  976. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
  977. gdfs->state.script_id = _class->get_instance_id();
  978. //gdfs->state.result_pos=ip+ipofs-1;
  979. gdfs->state.defarg = defarg;
  980. gdfs->state.instance = p_instance;
  981. gdfs->function = this;
  982. retvalue = gdfs;
  983. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  984. //do the oneshot connect
  985. GET_VARIANT_PTR(argobj, 1);
  986. GET_VARIANT_PTR(argname, 2);
  987. #ifdef DEBUG_ENABLED
  988. if (argobj->get_type() != Variant::OBJECT) {
  989. err_text = "First argument of yield() not of type object.";
  990. OPCODE_BREAK;
  991. }
  992. if (argname->get_type() != Variant::STRING) {
  993. err_text = "Second argument of yield() not a string (for signal name).";
  994. OPCODE_BREAK;
  995. }
  996. #endif
  997. Object *obj = argobj->operator Object *();
  998. String signal = argname->operator String();
  999. #ifdef DEBUG_ENABLED
  1000. if (!obj) {
  1001. err_text = "First argument of yield() is null.";
  1002. OPCODE_BREAK;
  1003. }
  1004. if (ScriptDebugger::get_singleton()) {
  1005. if (!ObjectDB::instance_validate(obj)) {
  1006. err_text = "First argument of yield() is a previously freed instance.";
  1007. OPCODE_BREAK;
  1008. }
  1009. }
  1010. if (signal.length() == 0) {
  1011. err_text = "Second argument of yield() is an empty string (for signal name).";
  1012. OPCODE_BREAK;
  1013. }
  1014. #endif
  1015. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1016. #ifdef DEBUG_ENABLED
  1017. if (err != OK) {
  1018. err_text = "Error connecting to signal: " + signal + " during yield().";
  1019. OPCODE_BREAK;
  1020. }
  1021. #endif
  1022. }
  1023. exit_ok = true;
  1024. OPCODE_BREAK;
  1025. }
  1026. OPCODE(OPCODE_YIELD_RESUME) {
  1027. CHECK_SPACE(2);
  1028. #ifdef DEBUG_ENABLED
  1029. if (!p_state) {
  1030. err_text = ("Invalid Resume (bug?)");
  1031. OPCODE_BREAK;
  1032. }
  1033. #endif
  1034. GET_VARIANT_PTR(result, 1);
  1035. *result = p_state->result;
  1036. ip += 2;
  1037. }
  1038. DISPATCH_OPCODE;
  1039. OPCODE(OPCODE_JUMP) {
  1040. CHECK_SPACE(2);
  1041. int to = _code_ptr[ip + 1];
  1042. GD_ERR_BREAK(to < 0 || to > _code_size);
  1043. ip = to;
  1044. }
  1045. DISPATCH_OPCODE;
  1046. OPCODE(OPCODE_JUMP_IF) {
  1047. CHECK_SPACE(3);
  1048. GET_VARIANT_PTR(test, 1);
  1049. bool result = test->booleanize();
  1050. if (result) {
  1051. int to = _code_ptr[ip + 2];
  1052. GD_ERR_BREAK(to < 0 || to > _code_size);
  1053. ip = to;
  1054. } else {
  1055. ip += 3;
  1056. }
  1057. }
  1058. DISPATCH_OPCODE;
  1059. OPCODE(OPCODE_JUMP_IF_NOT) {
  1060. CHECK_SPACE(3);
  1061. GET_VARIANT_PTR(test, 1);
  1062. bool result = test->booleanize();
  1063. if (!result) {
  1064. int to = _code_ptr[ip + 2];
  1065. GD_ERR_BREAK(to < 0 || to > _code_size);
  1066. ip = to;
  1067. } else {
  1068. ip += 3;
  1069. }
  1070. }
  1071. DISPATCH_OPCODE;
  1072. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1073. CHECK_SPACE(2);
  1074. ip = _default_arg_ptr[defarg];
  1075. }
  1076. DISPATCH_OPCODE;
  1077. OPCODE(OPCODE_RETURN) {
  1078. CHECK_SPACE(2);
  1079. GET_VARIANT_PTR(r, 1);
  1080. retvalue = *r;
  1081. exit_ok = true;
  1082. OPCODE_BREAK;
  1083. }
  1084. OPCODE(OPCODE_ITERATE_BEGIN) {
  1085. CHECK_SPACE(8); //space for this a regular iterate
  1086. GET_VARIANT_PTR(counter, 1);
  1087. GET_VARIANT_PTR(container, 2);
  1088. bool valid;
  1089. if (!container->iter_init(*counter, valid)) {
  1090. #ifdef DEBUG_ENABLED
  1091. if (!valid) {
  1092. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1093. OPCODE_BREAK;
  1094. }
  1095. #endif
  1096. int jumpto = _code_ptr[ip + 3];
  1097. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1098. ip = jumpto;
  1099. } else {
  1100. GET_VARIANT_PTR(iterator, 4);
  1101. *iterator = container->iter_get(*counter, valid);
  1102. #ifdef DEBUG_ENABLED
  1103. if (!valid) {
  1104. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1105. OPCODE_BREAK;
  1106. }
  1107. #endif
  1108. ip += 5; //skip regular iterate which is always next
  1109. }
  1110. }
  1111. DISPATCH_OPCODE;
  1112. OPCODE(OPCODE_ITERATE) {
  1113. CHECK_SPACE(4);
  1114. GET_VARIANT_PTR(counter, 1);
  1115. GET_VARIANT_PTR(container, 2);
  1116. bool valid;
  1117. if (!container->iter_next(*counter, valid)) {
  1118. #ifdef DEBUG_ENABLED
  1119. if (!valid) {
  1120. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1121. OPCODE_BREAK;
  1122. }
  1123. #endif
  1124. int jumpto = _code_ptr[ip + 3];
  1125. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1126. ip = jumpto;
  1127. } else {
  1128. GET_VARIANT_PTR(iterator, 4);
  1129. *iterator = container->iter_get(*counter, valid);
  1130. #ifdef DEBUG_ENABLED
  1131. if (!valid) {
  1132. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1133. OPCODE_BREAK;
  1134. }
  1135. #endif
  1136. ip += 5; //loop again
  1137. }
  1138. }
  1139. DISPATCH_OPCODE;
  1140. OPCODE(OPCODE_ASSERT) {
  1141. CHECK_SPACE(2);
  1142. GET_VARIANT_PTR(test, 1);
  1143. #ifdef DEBUG_ENABLED
  1144. bool result = test->booleanize();
  1145. if (!result) {
  1146. err_text = "Assertion failed.";
  1147. OPCODE_BREAK;
  1148. }
  1149. #endif
  1150. ip += 2;
  1151. }
  1152. DISPATCH_OPCODE;
  1153. OPCODE(OPCODE_BREAKPOINT) {
  1154. #ifdef DEBUG_ENABLED
  1155. if (ScriptDebugger::get_singleton()) {
  1156. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1157. }
  1158. #endif
  1159. ip += 1;
  1160. }
  1161. DISPATCH_OPCODE;
  1162. OPCODE(OPCODE_LINE) {
  1163. CHECK_SPACE(2);
  1164. line = _code_ptr[ip + 1];
  1165. ip += 2;
  1166. if (ScriptDebugger::get_singleton()) {
  1167. // line
  1168. bool do_break = false;
  1169. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1170. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1171. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1172. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1173. do_break = true;
  1174. }
  1175. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  1176. do_break = true;
  1177. if (do_break) {
  1178. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1179. }
  1180. ScriptDebugger::get_singleton()->line_poll();
  1181. }
  1182. }
  1183. DISPATCH_OPCODE;
  1184. OPCODE(OPCODE_END) {
  1185. exit_ok = true;
  1186. OPCODE_BREAK;
  1187. }
  1188. // Enable for debugging
  1189. #if 0
  1190. default: {
  1191. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1192. OPCODE_BREAK;
  1193. }
  1194. #endif
  1195. }
  1196. OPCODES_END
  1197. #ifdef DEBUG_ENABLED
  1198. if (exit_ok)
  1199. OPCODE_OUT;
  1200. //error
  1201. // function, file, line, error, explanation
  1202. String err_file;
  1203. if (p_instance)
  1204. err_file = p_instance->script->path;
  1205. else if (_class)
  1206. err_file = _class->path;
  1207. if (err_file == "")
  1208. err_file = "<built-in>";
  1209. String err_func = name;
  1210. if (p_instance && p_instance->script->name != "")
  1211. err_func = p_instance->script->name + "." + err_func;
  1212. int err_line = line;
  1213. if (err_text == "") {
  1214. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1215. }
  1216. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1217. // debugger break did not happen
  1218. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1219. }
  1220. #endif
  1221. OPCODE_OUT;
  1222. }
  1223. OPCODES_OUT
  1224. #ifdef DEBUG_ENABLED
  1225. if (GDScriptLanguage::get_singleton()->profiling) {
  1226. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1227. profile.total_time += time_taken;
  1228. profile.self_time += time_taken - function_call_time;
  1229. profile.frame_total_time += time_taken;
  1230. profile.frame_self_time += time_taken - function_call_time;
  1231. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1232. }
  1233. if (ScriptDebugger::get_singleton())
  1234. GDScriptLanguage::get_singleton()->exit_function();
  1235. #endif
  1236. if (_stack_size) {
  1237. //free stack
  1238. for (int i = 0; i < _stack_size; i++)
  1239. stack[i].~Variant();
  1240. }
  1241. return retvalue;
  1242. }
  1243. const int *GDScriptFunction::get_code() const {
  1244. return _code_ptr;
  1245. }
  1246. int GDScriptFunction::get_code_size() const {
  1247. return _code_size;
  1248. }
  1249. Variant GDScriptFunction::get_constant(int p_idx) const {
  1250. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1251. return constants[p_idx];
  1252. }
  1253. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1254. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1255. return global_names[p_idx];
  1256. }
  1257. int GDScriptFunction::get_default_argument_count() const {
  1258. return _default_arg_count;
  1259. }
  1260. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1261. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1262. return default_arguments[p_idx];
  1263. }
  1264. GDScriptDataType GDScriptFunction::get_return_type() const {
  1265. return return_type;
  1266. }
  1267. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1268. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1269. return argument_types[p_idx];
  1270. }
  1271. StringName GDScriptFunction::get_name() const {
  1272. return name;
  1273. }
  1274. int GDScriptFunction::get_max_stack_size() const {
  1275. return _stack_size;
  1276. }
  1277. struct _GDFKC {
  1278. int order;
  1279. List<int> pos;
  1280. };
  1281. struct _GDFKCS {
  1282. int order;
  1283. StringName id;
  1284. int pos;
  1285. bool operator<(const _GDFKCS &p_r) const {
  1286. return order < p_r.order;
  1287. }
  1288. };
  1289. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1290. int oc = 0;
  1291. Map<StringName, _GDFKC> sdmap;
  1292. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1293. const StackDebug &sd = E->get();
  1294. if (sd.line > p_line)
  1295. break;
  1296. if (sd.added) {
  1297. if (!sdmap.has(sd.identifier)) {
  1298. _GDFKC d;
  1299. d.order = oc++;
  1300. d.pos.push_back(sd.pos);
  1301. sdmap[sd.identifier] = d;
  1302. } else {
  1303. sdmap[sd.identifier].pos.push_back(sd.pos);
  1304. }
  1305. } else {
  1306. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1307. sdmap[sd.identifier].pos.pop_back();
  1308. if (sdmap[sd.identifier].pos.empty())
  1309. sdmap.erase(sd.identifier);
  1310. }
  1311. }
  1312. List<_GDFKCS> stackpositions;
  1313. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1314. _GDFKCS spp;
  1315. spp.id = E->key();
  1316. spp.order = E->get().order;
  1317. spp.pos = E->get().pos.back()->get();
  1318. stackpositions.push_back(spp);
  1319. }
  1320. stackpositions.sort();
  1321. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1322. Pair<StringName, int> p;
  1323. p.first = E->get().id;
  1324. p.second = E->get().pos;
  1325. r_stackvars->push_back(p);
  1326. }
  1327. }
  1328. GDScriptFunction::GDScriptFunction() :
  1329. function_list(this) {
  1330. _stack_size = 0;
  1331. _call_size = 0;
  1332. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1333. name = "<anonymous>";
  1334. #ifdef DEBUG_ENABLED
  1335. _func_cname = NULL;
  1336. if (GDScriptLanguage::get_singleton()->lock) {
  1337. GDScriptLanguage::get_singleton()->lock->lock();
  1338. }
  1339. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1340. if (GDScriptLanguage::get_singleton()->lock) {
  1341. GDScriptLanguage::get_singleton()->lock->unlock();
  1342. }
  1343. profile.call_count = 0;
  1344. profile.self_time = 0;
  1345. profile.total_time = 0;
  1346. profile.frame_call_count = 0;
  1347. profile.frame_self_time = 0;
  1348. profile.frame_total_time = 0;
  1349. profile.last_frame_call_count = 0;
  1350. profile.last_frame_self_time = 0;
  1351. profile.last_frame_total_time = 0;
  1352. #endif
  1353. }
  1354. GDScriptFunction::~GDScriptFunction() {
  1355. #ifdef DEBUG_ENABLED
  1356. if (GDScriptLanguage::get_singleton()->lock) {
  1357. GDScriptLanguage::get_singleton()->lock->lock();
  1358. }
  1359. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1360. if (GDScriptLanguage::get_singleton()->lock) {
  1361. GDScriptLanguage::get_singleton()->lock->unlock();
  1362. }
  1363. #endif
  1364. }
  1365. /////////////////////
  1366. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1367. #ifdef DEBUG_ENABLED
  1368. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1369. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1370. ERR_FAIL_V(Variant());
  1371. }
  1372. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1373. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1374. ERR_FAIL_V(Variant());
  1375. }
  1376. #endif
  1377. Variant arg;
  1378. r_error.error = Variant::CallError::CALL_OK;
  1379. ERR_FAIL_COND_V(!function, Variant());
  1380. if (p_argcount == 0) {
  1381. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1382. r_error.argument = 1;
  1383. return Variant();
  1384. } else if (p_argcount == 1) {
  1385. //noooneee
  1386. } else if (p_argcount == 2) {
  1387. arg = *p_args[0];
  1388. } else {
  1389. Array extra_args;
  1390. for (int i = 0; i < p_argcount - 1; i++) {
  1391. extra_args.push_back(*p_args[i]);
  1392. }
  1393. arg = extra_args;
  1394. }
  1395. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1396. if (self.is_null()) {
  1397. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1398. r_error.argument = p_argcount - 1;
  1399. r_error.expected = Variant::OBJECT;
  1400. return Variant();
  1401. }
  1402. state.result = arg;
  1403. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1404. bool completed = true;
  1405. // If the return value is a GDScriptFunctionState reference,
  1406. // then the function did yield again after resuming.
  1407. if (ret.is_ref()) {
  1408. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1409. if (gdfs && gdfs->function == function) {
  1410. completed = false;
  1411. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1412. }
  1413. }
  1414. function = NULL; //cleaned up;
  1415. state.result = Variant();
  1416. if (completed) {
  1417. if (first_state.is_valid()) {
  1418. first_state->emit_signal("completed", ret);
  1419. } else {
  1420. emit_signal("completed", ret);
  1421. }
  1422. }
  1423. return ret;
  1424. }
  1425. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1426. if (function == NULL)
  1427. return false;
  1428. if (p_extended_check) {
  1429. //class instance gone?
  1430. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1431. return false;
  1432. //script gone?
  1433. if (state.script_id && !ObjectDB::get_instance(state.script_id))
  1434. return false;
  1435. }
  1436. return true;
  1437. }
  1438. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1439. ERR_FAIL_COND_V(!function, Variant());
  1440. #ifdef DEBUG_ENABLED
  1441. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1442. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1443. ERR_FAIL_V(Variant());
  1444. }
  1445. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1446. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1447. ERR_FAIL_V(Variant());
  1448. }
  1449. #endif
  1450. state.result = p_arg;
  1451. Variant::CallError err;
  1452. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1453. bool completed = true;
  1454. // If the return value is a GDScriptFunctionState reference,
  1455. // then the function did yield again after resuming.
  1456. if (ret.is_ref()) {
  1457. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1458. if (gdfs && gdfs->function == function) {
  1459. completed = false;
  1460. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1461. }
  1462. }
  1463. function = NULL; //cleaned up;
  1464. state.result = Variant();
  1465. if (completed) {
  1466. if (first_state.is_valid()) {
  1467. first_state->emit_signal("completed", ret);
  1468. } else {
  1469. emit_signal("completed", ret);
  1470. }
  1471. }
  1472. return ret;
  1473. }
  1474. void GDScriptFunctionState::_bind_methods() {
  1475. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1476. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1477. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1478. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1479. }
  1480. GDScriptFunctionState::GDScriptFunctionState() {
  1481. function = NULL;
  1482. }
  1483. GDScriptFunctionState::~GDScriptFunctionState() {
  1484. if (function != NULL) {
  1485. //never called, deinitialize stack
  1486. for (int i = 0; i < state.stack_size; i++) {
  1487. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1488. v->~Variant();
  1489. }
  1490. }
  1491. }