gdscript_function.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. #ifdef DEBUG_ENABLED
  607. if (src->get_type() != var_type) {
  608. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  609. Variant::CallError ce;
  610. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  611. } else {
  612. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  613. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  614. OPCODE_BREAK;
  615. }
  616. } else {
  617. #endif // DEBUG_ENABLED
  618. *dst = *src;
  619. #ifdef DEBUG_ENABLED
  620. }
  621. #endif // DEBUG_ENABLED
  622. ip += 4;
  623. }
  624. DISPATCH_OPCODE;
  625. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  626. CHECK_SPACE(4);
  627. GET_VARIANT_PTR(type, 1);
  628. GET_VARIANT_PTR(dst, 2);
  629. GET_VARIANT_PTR(src, 3);
  630. #ifdef DEBUG_ENABLED
  631. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  632. GD_ERR_BREAK(!nc);
  633. if (!src->get_type() != Variant::OBJECT && !src->get_type() != Variant::NIL) {
  634. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  635. "' to a variable of type '" + nc->get_name() + "'.";
  636. OPCODE_BREAK;
  637. }
  638. Object *src_obj = src->operator Object *();
  639. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  640. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  641. "' to a variable of type '" + nc->get_name() + "'.";
  642. OPCODE_BREAK;
  643. }
  644. #endif // DEBUG_ENABLED
  645. *dst = *src;
  646. ip += 4;
  647. }
  648. DISPATCH_OPCODE;
  649. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  650. CHECK_SPACE(4);
  651. GET_VARIANT_PTR(type, 1);
  652. GET_VARIANT_PTR(dst, 2);
  653. GET_VARIANT_PTR(src, 3);
  654. #ifdef DEBUG_ENABLED
  655. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  656. GD_ERR_BREAK(!base_type);
  657. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  658. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  659. OPCODE_BREAK;
  660. }
  661. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  662. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  663. if (!scr_inst) {
  664. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  665. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  666. OPCODE_BREAK;
  667. }
  668. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  669. bool valid = false;
  670. while (src_type) {
  671. if (src_type == base_type) {
  672. valid = true;
  673. break;
  674. }
  675. src_type = src_type->get_base_script().ptr();
  676. }
  677. if (!valid) {
  678. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  679. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  680. OPCODE_BREAK;
  681. }
  682. }
  683. #endif // DEBUG_ENABLED
  684. *dst = *src;
  685. ip += 4;
  686. }
  687. DISPATCH_OPCODE;
  688. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  689. CHECK_SPACE(4);
  690. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  691. GET_VARIANT_PTR(src, 2);
  692. GET_VARIANT_PTR(dst, 3);
  693. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  694. Variant::CallError err;
  695. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  696. #ifdef DEBUG_ENABLED
  697. if (err.error != Variant::CallError::CALL_OK) {
  698. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  699. OPCODE_BREAK;
  700. }
  701. #endif
  702. ip += 4;
  703. }
  704. DISPATCH_OPCODE;
  705. OPCODE(OPCODE_CAST_TO_NATIVE) {
  706. CHECK_SPACE(4);
  707. GET_VARIANT_PTR(to_type, 1);
  708. GET_VARIANT_PTR(src, 2);
  709. GET_VARIANT_PTR(dst, 3);
  710. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  711. GD_ERR_BREAK(!nc);
  712. #ifdef DEBUG_ENABLED
  713. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  714. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  715. OPCODE_BREAK;
  716. }
  717. #endif
  718. Object *src_obj = src->operator Object *();
  719. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  720. *dst = Variant(); // invalid cast, assign NULL
  721. } else {
  722. *dst = *src;
  723. }
  724. ip += 4;
  725. }
  726. DISPATCH_OPCODE;
  727. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  728. CHECK_SPACE(4);
  729. GET_VARIANT_PTR(to_type, 1);
  730. GET_VARIANT_PTR(src, 2);
  731. GET_VARIANT_PTR(dst, 3);
  732. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  733. GD_ERR_BREAK(!base_type);
  734. #ifdef DEBUG_ENABLED
  735. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  736. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  737. OPCODE_BREAK;
  738. }
  739. #endif
  740. bool valid = false;
  741. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  742. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  743. if (scr_inst) {
  744. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  745. while (src_type) {
  746. if (src_type == base_type) {
  747. valid = true;
  748. break;
  749. }
  750. src_type = src_type->get_base_script().ptr();
  751. }
  752. }
  753. }
  754. if (valid) {
  755. *dst = *src; // Valid cast, copy the source object
  756. } else {
  757. *dst = Variant(); // invalid cast, assign NULL
  758. }
  759. ip += 4;
  760. }
  761. DISPATCH_OPCODE;
  762. OPCODE(OPCODE_CONSTRUCT) {
  763. CHECK_SPACE(2);
  764. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  765. int argc = _code_ptr[ip + 2];
  766. CHECK_SPACE(argc + 2);
  767. Variant **argptrs = call_args;
  768. for (int i = 0; i < argc; i++) {
  769. GET_VARIANT_PTR(v, 3 + i);
  770. argptrs[i] = v;
  771. }
  772. GET_VARIANT_PTR(dst, 3 + argc);
  773. Variant::CallError err;
  774. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  775. #ifdef DEBUG_ENABLED
  776. if (err.error != Variant::CallError::CALL_OK) {
  777. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  778. OPCODE_BREAK;
  779. }
  780. #endif
  781. ip += 4 + argc;
  782. //construct a basic type
  783. }
  784. DISPATCH_OPCODE;
  785. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  786. CHECK_SPACE(1);
  787. int argc = _code_ptr[ip + 1];
  788. Array array; //arrays are always shared
  789. array.resize(argc);
  790. CHECK_SPACE(argc + 2);
  791. for (int i = 0; i < argc; i++) {
  792. GET_VARIANT_PTR(v, 2 + i);
  793. array[i] = *v;
  794. }
  795. GET_VARIANT_PTR(dst, 2 + argc);
  796. *dst = array;
  797. ip += 3 + argc;
  798. }
  799. DISPATCH_OPCODE;
  800. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  801. CHECK_SPACE(1);
  802. int argc = _code_ptr[ip + 1];
  803. Dictionary dict; //arrays are always shared
  804. CHECK_SPACE(argc * 2 + 2);
  805. for (int i = 0; i < argc; i++) {
  806. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  807. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  808. dict[*k] = *v;
  809. }
  810. GET_VARIANT_PTR(dst, 2 + argc * 2);
  811. *dst = dict;
  812. ip += 3 + argc * 2;
  813. }
  814. DISPATCH_OPCODE;
  815. OPCODE(OPCODE_CALL_RETURN)
  816. OPCODE(OPCODE_CALL) {
  817. CHECK_SPACE(4);
  818. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  819. int argc = _code_ptr[ip + 1];
  820. GET_VARIANT_PTR(base, 2);
  821. int nameg = _code_ptr[ip + 3];
  822. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  823. const StringName *methodname = &_global_names_ptr[nameg];
  824. GD_ERR_BREAK(argc < 0);
  825. ip += 4;
  826. CHECK_SPACE(argc + 1);
  827. Variant **argptrs = call_args;
  828. for (int i = 0; i < argc; i++) {
  829. GET_VARIANT_PTR(v, i);
  830. argptrs[i] = v;
  831. }
  832. #ifdef DEBUG_ENABLED
  833. uint64_t call_time = 0;
  834. if (GDScriptLanguage::get_singleton()->profiling) {
  835. call_time = OS::get_singleton()->get_ticks_usec();
  836. }
  837. #endif
  838. Variant::CallError err;
  839. if (call_ret) {
  840. GET_VARIANT_PTR(ret, argc);
  841. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  842. } else {
  843. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  844. }
  845. #ifdef DEBUG_ENABLED
  846. if (GDScriptLanguage::get_singleton()->profiling) {
  847. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  848. }
  849. if (err.error != Variant::CallError::CALL_OK) {
  850. String methodstr = *methodname;
  851. String basestr = _get_var_type(base);
  852. if (methodstr == "call") {
  853. if (argc >= 1) {
  854. methodstr = String(*argptrs[0]) + " (via call)";
  855. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  856. err.argument -= 1;
  857. }
  858. }
  859. } else if (methodstr == "free") {
  860. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  861. if (base->is_ref()) {
  862. err_text = "Attempted to free a reference.";
  863. OPCODE_BREAK;
  864. } else if (base->get_type() == Variant::OBJECT) {
  865. err_text = "Attempted to free a locked object (calling or emitting).";
  866. OPCODE_BREAK;
  867. }
  868. }
  869. }
  870. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  871. OPCODE_BREAK;
  872. }
  873. #endif
  874. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  875. ip += argc + 1;
  876. }
  877. DISPATCH_OPCODE;
  878. OPCODE(OPCODE_CALL_BUILT_IN) {
  879. CHECK_SPACE(4);
  880. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  881. int argc = _code_ptr[ip + 2];
  882. GD_ERR_BREAK(argc < 0);
  883. ip += 3;
  884. CHECK_SPACE(argc + 1);
  885. Variant **argptrs = call_args;
  886. for (int i = 0; i < argc; i++) {
  887. GET_VARIANT_PTR(v, i);
  888. argptrs[i] = v;
  889. }
  890. GET_VARIANT_PTR(dst, argc);
  891. Variant::CallError err;
  892. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  893. #ifdef DEBUG_ENABLED
  894. if (err.error != Variant::CallError::CALL_OK) {
  895. String methodstr = GDScriptFunctions::get_func_name(func);
  896. if (dst->get_type() == Variant::STRING) {
  897. //call provided error string
  898. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  899. } else {
  900. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  901. }
  902. OPCODE_BREAK;
  903. }
  904. #endif
  905. ip += argc + 1;
  906. }
  907. DISPATCH_OPCODE;
  908. OPCODE(OPCODE_CALL_SELF) {
  909. OPCODE_BREAK;
  910. }
  911. OPCODE(OPCODE_CALL_SELF_BASE) {
  912. CHECK_SPACE(2);
  913. int self_fun = _code_ptr[ip + 1];
  914. #ifdef DEBUG_ENABLED
  915. if (self_fun < 0 || self_fun >= _global_names_count) {
  916. err_text = "compiler bug, function name not found";
  917. OPCODE_BREAK;
  918. }
  919. #endif
  920. const StringName *methodname = &_global_names_ptr[self_fun];
  921. int argc = _code_ptr[ip + 2];
  922. CHECK_SPACE(2 + argc + 1);
  923. Variant **argptrs = call_args;
  924. for (int i = 0; i < argc; i++) {
  925. GET_VARIANT_PTR(v, i + 3);
  926. argptrs[i] = v;
  927. }
  928. GET_VARIANT_PTR(dst, argc + 3);
  929. const GDScript *gds = _script;
  930. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  931. while (gds->base.ptr()) {
  932. gds = gds->base.ptr();
  933. E = gds->member_functions.find(*methodname);
  934. if (E)
  935. break;
  936. }
  937. Variant::CallError err;
  938. if (E) {
  939. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  940. } else if (gds->native.ptr()) {
  941. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  942. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  943. if (!mb) {
  944. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  945. } else {
  946. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  947. }
  948. } else {
  949. err.error = Variant::CallError::CALL_OK;
  950. }
  951. } else {
  952. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  953. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  954. } else {
  955. err.error = Variant::CallError::CALL_OK;
  956. }
  957. }
  958. if (err.error != Variant::CallError::CALL_OK) {
  959. String methodstr = *methodname;
  960. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  961. OPCODE_BREAK;
  962. }
  963. ip += 4 + argc;
  964. }
  965. DISPATCH_OPCODE;
  966. OPCODE(OPCODE_YIELD)
  967. OPCODE(OPCODE_YIELD_SIGNAL) {
  968. int ipofs = 1;
  969. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  970. CHECK_SPACE(4);
  971. ipofs += 2;
  972. } else {
  973. CHECK_SPACE(2);
  974. }
  975. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  976. gdfs->function = this;
  977. gdfs->state.stack.resize(alloca_size);
  978. //copy variant stack
  979. for (int i = 0; i < _stack_size; i++) {
  980. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  981. }
  982. gdfs->state.stack_size = _stack_size;
  983. gdfs->state.self = self;
  984. gdfs->state.alloca_size = alloca_size;
  985. gdfs->state._class = _class;
  986. gdfs->state.ip = ip + ipofs;
  987. gdfs->state.line = line;
  988. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
  989. gdfs->state.script_id = _class->get_instance_id();
  990. //gdfs->state.result_pos=ip+ipofs-1;
  991. gdfs->state.defarg = defarg;
  992. gdfs->state.instance = p_instance;
  993. gdfs->function = this;
  994. retvalue = gdfs;
  995. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  996. //do the oneshot connect
  997. GET_VARIANT_PTR(argobj, 1);
  998. GET_VARIANT_PTR(argname, 2);
  999. #ifdef DEBUG_ENABLED
  1000. if (argobj->get_type() != Variant::OBJECT) {
  1001. err_text = "First argument of yield() not of type object.";
  1002. OPCODE_BREAK;
  1003. }
  1004. if (argname->get_type() != Variant::STRING) {
  1005. err_text = "Second argument of yield() not a string (for signal name).";
  1006. OPCODE_BREAK;
  1007. }
  1008. #endif
  1009. Object *obj = argobj->operator Object *();
  1010. String signal = argname->operator String();
  1011. #ifdef DEBUG_ENABLED
  1012. if (!obj) {
  1013. err_text = "First argument of yield() is null.";
  1014. OPCODE_BREAK;
  1015. }
  1016. if (ScriptDebugger::get_singleton()) {
  1017. if (!ObjectDB::instance_validate(obj)) {
  1018. err_text = "First argument of yield() is a previously freed instance.";
  1019. OPCODE_BREAK;
  1020. }
  1021. }
  1022. if (signal.length() == 0) {
  1023. err_text = "Second argument of yield() is an empty string (for signal name).";
  1024. OPCODE_BREAK;
  1025. }
  1026. #endif
  1027. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1028. #ifdef DEBUG_ENABLED
  1029. if (err != OK) {
  1030. err_text = "Error connecting to signal: " + signal + " during yield().";
  1031. OPCODE_BREAK;
  1032. }
  1033. #endif
  1034. }
  1035. exit_ok = true;
  1036. OPCODE_BREAK;
  1037. }
  1038. OPCODE(OPCODE_YIELD_RESUME) {
  1039. CHECK_SPACE(2);
  1040. #ifdef DEBUG_ENABLED
  1041. if (!p_state) {
  1042. err_text = ("Invalid Resume (bug?)");
  1043. OPCODE_BREAK;
  1044. }
  1045. #endif
  1046. GET_VARIANT_PTR(result, 1);
  1047. *result = p_state->result;
  1048. ip += 2;
  1049. }
  1050. DISPATCH_OPCODE;
  1051. OPCODE(OPCODE_JUMP) {
  1052. CHECK_SPACE(2);
  1053. int to = _code_ptr[ip + 1];
  1054. GD_ERR_BREAK(to < 0 || to > _code_size);
  1055. ip = to;
  1056. }
  1057. DISPATCH_OPCODE;
  1058. OPCODE(OPCODE_JUMP_IF) {
  1059. CHECK_SPACE(3);
  1060. GET_VARIANT_PTR(test, 1);
  1061. bool result = test->booleanize();
  1062. if (result) {
  1063. int to = _code_ptr[ip + 2];
  1064. GD_ERR_BREAK(to < 0 || to > _code_size);
  1065. ip = to;
  1066. } else {
  1067. ip += 3;
  1068. }
  1069. }
  1070. DISPATCH_OPCODE;
  1071. OPCODE(OPCODE_JUMP_IF_NOT) {
  1072. CHECK_SPACE(3);
  1073. GET_VARIANT_PTR(test, 1);
  1074. bool result = test->booleanize();
  1075. if (!result) {
  1076. int to = _code_ptr[ip + 2];
  1077. GD_ERR_BREAK(to < 0 || to > _code_size);
  1078. ip = to;
  1079. } else {
  1080. ip += 3;
  1081. }
  1082. }
  1083. DISPATCH_OPCODE;
  1084. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1085. CHECK_SPACE(2);
  1086. ip = _default_arg_ptr[defarg];
  1087. }
  1088. DISPATCH_OPCODE;
  1089. OPCODE(OPCODE_RETURN) {
  1090. CHECK_SPACE(2);
  1091. GET_VARIANT_PTR(r, 1);
  1092. retvalue = *r;
  1093. exit_ok = true;
  1094. OPCODE_BREAK;
  1095. }
  1096. OPCODE(OPCODE_ITERATE_BEGIN) {
  1097. CHECK_SPACE(8); //space for this a regular iterate
  1098. GET_VARIANT_PTR(counter, 1);
  1099. GET_VARIANT_PTR(container, 2);
  1100. bool valid;
  1101. if (!container->iter_init(*counter, valid)) {
  1102. #ifdef DEBUG_ENABLED
  1103. if (!valid) {
  1104. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1105. OPCODE_BREAK;
  1106. }
  1107. #endif
  1108. int jumpto = _code_ptr[ip + 3];
  1109. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1110. ip = jumpto;
  1111. } else {
  1112. GET_VARIANT_PTR(iterator, 4);
  1113. *iterator = container->iter_get(*counter, valid);
  1114. #ifdef DEBUG_ENABLED
  1115. if (!valid) {
  1116. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1117. OPCODE_BREAK;
  1118. }
  1119. #endif
  1120. ip += 5; //skip regular iterate which is always next
  1121. }
  1122. }
  1123. DISPATCH_OPCODE;
  1124. OPCODE(OPCODE_ITERATE) {
  1125. CHECK_SPACE(4);
  1126. GET_VARIANT_PTR(counter, 1);
  1127. GET_VARIANT_PTR(container, 2);
  1128. bool valid;
  1129. if (!container->iter_next(*counter, valid)) {
  1130. #ifdef DEBUG_ENABLED
  1131. if (!valid) {
  1132. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1133. OPCODE_BREAK;
  1134. }
  1135. #endif
  1136. int jumpto = _code_ptr[ip + 3];
  1137. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1138. ip = jumpto;
  1139. } else {
  1140. GET_VARIANT_PTR(iterator, 4);
  1141. *iterator = container->iter_get(*counter, valid);
  1142. #ifdef DEBUG_ENABLED
  1143. if (!valid) {
  1144. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1145. OPCODE_BREAK;
  1146. }
  1147. #endif
  1148. ip += 5; //loop again
  1149. }
  1150. }
  1151. DISPATCH_OPCODE;
  1152. OPCODE(OPCODE_ASSERT) {
  1153. CHECK_SPACE(2);
  1154. GET_VARIANT_PTR(test, 1);
  1155. #ifdef DEBUG_ENABLED
  1156. bool result = test->booleanize();
  1157. if (!result) {
  1158. err_text = "Assertion failed.";
  1159. OPCODE_BREAK;
  1160. }
  1161. #endif
  1162. ip += 2;
  1163. }
  1164. DISPATCH_OPCODE;
  1165. OPCODE(OPCODE_BREAKPOINT) {
  1166. #ifdef DEBUG_ENABLED
  1167. if (ScriptDebugger::get_singleton()) {
  1168. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1169. }
  1170. #endif
  1171. ip += 1;
  1172. }
  1173. DISPATCH_OPCODE;
  1174. OPCODE(OPCODE_LINE) {
  1175. CHECK_SPACE(2);
  1176. line = _code_ptr[ip + 1];
  1177. ip += 2;
  1178. if (ScriptDebugger::get_singleton()) {
  1179. // line
  1180. bool do_break = false;
  1181. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1182. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1183. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1184. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1185. do_break = true;
  1186. }
  1187. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  1188. do_break = true;
  1189. if (do_break) {
  1190. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1191. }
  1192. ScriptDebugger::get_singleton()->line_poll();
  1193. }
  1194. }
  1195. DISPATCH_OPCODE;
  1196. OPCODE(OPCODE_END) {
  1197. exit_ok = true;
  1198. OPCODE_BREAK;
  1199. }
  1200. // Enable for debugging
  1201. #if 0
  1202. default: {
  1203. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1204. OPCODE_BREAK;
  1205. }
  1206. #endif
  1207. }
  1208. OPCODES_END
  1209. #ifdef DEBUG_ENABLED
  1210. if (exit_ok)
  1211. OPCODE_OUT;
  1212. //error
  1213. // function, file, line, error, explanation
  1214. String err_file;
  1215. if (p_instance)
  1216. err_file = p_instance->script->path;
  1217. else if (_class)
  1218. err_file = _class->path;
  1219. if (err_file == "")
  1220. err_file = "<built-in>";
  1221. String err_func = name;
  1222. if (p_instance && p_instance->script->name != "")
  1223. err_func = p_instance->script->name + "." + err_func;
  1224. int err_line = line;
  1225. if (err_text == "") {
  1226. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1227. }
  1228. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1229. // debugger break did not happen
  1230. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1231. }
  1232. #endif
  1233. OPCODE_OUT;
  1234. }
  1235. OPCODES_OUT
  1236. #ifdef DEBUG_ENABLED
  1237. if (GDScriptLanguage::get_singleton()->profiling) {
  1238. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1239. profile.total_time += time_taken;
  1240. profile.self_time += time_taken - function_call_time;
  1241. profile.frame_total_time += time_taken;
  1242. profile.frame_self_time += time_taken - function_call_time;
  1243. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1244. }
  1245. if (ScriptDebugger::get_singleton())
  1246. GDScriptLanguage::get_singleton()->exit_function();
  1247. #endif
  1248. if (_stack_size) {
  1249. //free stack
  1250. for (int i = 0; i < _stack_size; i++)
  1251. stack[i].~Variant();
  1252. }
  1253. return retvalue;
  1254. }
  1255. const int *GDScriptFunction::get_code() const {
  1256. return _code_ptr;
  1257. }
  1258. int GDScriptFunction::get_code_size() const {
  1259. return _code_size;
  1260. }
  1261. Variant GDScriptFunction::get_constant(int p_idx) const {
  1262. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1263. return constants[p_idx];
  1264. }
  1265. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1266. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1267. return global_names[p_idx];
  1268. }
  1269. int GDScriptFunction::get_default_argument_count() const {
  1270. return _default_arg_count;
  1271. }
  1272. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1273. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1274. return default_arguments[p_idx];
  1275. }
  1276. GDScriptDataType GDScriptFunction::get_return_type() const {
  1277. return return_type;
  1278. }
  1279. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1280. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1281. return argument_types[p_idx];
  1282. }
  1283. StringName GDScriptFunction::get_name() const {
  1284. return name;
  1285. }
  1286. int GDScriptFunction::get_max_stack_size() const {
  1287. return _stack_size;
  1288. }
  1289. struct _GDFKC {
  1290. int order;
  1291. List<int> pos;
  1292. };
  1293. struct _GDFKCS {
  1294. int order;
  1295. StringName id;
  1296. int pos;
  1297. bool operator<(const _GDFKCS &p_r) const {
  1298. return order < p_r.order;
  1299. }
  1300. };
  1301. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1302. int oc = 0;
  1303. Map<StringName, _GDFKC> sdmap;
  1304. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1305. const StackDebug &sd = E->get();
  1306. if (sd.line > p_line)
  1307. break;
  1308. if (sd.added) {
  1309. if (!sdmap.has(sd.identifier)) {
  1310. _GDFKC d;
  1311. d.order = oc++;
  1312. d.pos.push_back(sd.pos);
  1313. sdmap[sd.identifier] = d;
  1314. } else {
  1315. sdmap[sd.identifier].pos.push_back(sd.pos);
  1316. }
  1317. } else {
  1318. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1319. sdmap[sd.identifier].pos.pop_back();
  1320. if (sdmap[sd.identifier].pos.empty())
  1321. sdmap.erase(sd.identifier);
  1322. }
  1323. }
  1324. List<_GDFKCS> stackpositions;
  1325. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1326. _GDFKCS spp;
  1327. spp.id = E->key();
  1328. spp.order = E->get().order;
  1329. spp.pos = E->get().pos.back()->get();
  1330. stackpositions.push_back(spp);
  1331. }
  1332. stackpositions.sort();
  1333. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1334. Pair<StringName, int> p;
  1335. p.first = E->get().id;
  1336. p.second = E->get().pos;
  1337. r_stackvars->push_back(p);
  1338. }
  1339. }
  1340. GDScriptFunction::GDScriptFunction() :
  1341. function_list(this) {
  1342. _stack_size = 0;
  1343. _call_size = 0;
  1344. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1345. name = "<anonymous>";
  1346. #ifdef DEBUG_ENABLED
  1347. _func_cname = NULL;
  1348. if (GDScriptLanguage::get_singleton()->lock) {
  1349. GDScriptLanguage::get_singleton()->lock->lock();
  1350. }
  1351. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1352. if (GDScriptLanguage::get_singleton()->lock) {
  1353. GDScriptLanguage::get_singleton()->lock->unlock();
  1354. }
  1355. profile.call_count = 0;
  1356. profile.self_time = 0;
  1357. profile.total_time = 0;
  1358. profile.frame_call_count = 0;
  1359. profile.frame_self_time = 0;
  1360. profile.frame_total_time = 0;
  1361. profile.last_frame_call_count = 0;
  1362. profile.last_frame_self_time = 0;
  1363. profile.last_frame_total_time = 0;
  1364. #endif
  1365. }
  1366. GDScriptFunction::~GDScriptFunction() {
  1367. #ifdef DEBUG_ENABLED
  1368. if (GDScriptLanguage::get_singleton()->lock) {
  1369. GDScriptLanguage::get_singleton()->lock->lock();
  1370. }
  1371. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1372. if (GDScriptLanguage::get_singleton()->lock) {
  1373. GDScriptLanguage::get_singleton()->lock->unlock();
  1374. }
  1375. #endif
  1376. }
  1377. /////////////////////
  1378. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1379. #ifdef DEBUG_ENABLED
  1380. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1381. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1382. ERR_FAIL_V(Variant());
  1383. }
  1384. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1385. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1386. ERR_FAIL_V(Variant());
  1387. }
  1388. #endif
  1389. Variant arg;
  1390. r_error.error = Variant::CallError::CALL_OK;
  1391. ERR_FAIL_COND_V(!function, Variant());
  1392. if (p_argcount == 0) {
  1393. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1394. r_error.argument = 1;
  1395. return Variant();
  1396. } else if (p_argcount == 1) {
  1397. //noooneee
  1398. } else if (p_argcount == 2) {
  1399. arg = *p_args[0];
  1400. } else {
  1401. Array extra_args;
  1402. for (int i = 0; i < p_argcount - 1; i++) {
  1403. extra_args.push_back(*p_args[i]);
  1404. }
  1405. arg = extra_args;
  1406. }
  1407. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1408. if (self.is_null()) {
  1409. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1410. r_error.argument = p_argcount - 1;
  1411. r_error.expected = Variant::OBJECT;
  1412. return Variant();
  1413. }
  1414. state.result = arg;
  1415. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1416. bool completed = true;
  1417. // If the return value is a GDScriptFunctionState reference,
  1418. // then the function did yield again after resuming.
  1419. if (ret.is_ref()) {
  1420. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1421. if (gdfs && gdfs->function == function) {
  1422. completed = false;
  1423. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1424. }
  1425. }
  1426. function = NULL; //cleaned up;
  1427. state.result = Variant();
  1428. if (completed) {
  1429. if (first_state.is_valid()) {
  1430. first_state->emit_signal("completed", ret);
  1431. } else {
  1432. emit_signal("completed", ret);
  1433. }
  1434. }
  1435. return ret;
  1436. }
  1437. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1438. if (function == NULL)
  1439. return false;
  1440. if (p_extended_check) {
  1441. //class instance gone?
  1442. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1443. return false;
  1444. //script gone?
  1445. if (state.script_id && !ObjectDB::get_instance(state.script_id))
  1446. return false;
  1447. }
  1448. return true;
  1449. }
  1450. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1451. ERR_FAIL_COND_V(!function, Variant());
  1452. #ifdef DEBUG_ENABLED
  1453. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1454. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1455. ERR_FAIL_V(Variant());
  1456. }
  1457. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1458. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1459. ERR_FAIL_V(Variant());
  1460. }
  1461. #endif
  1462. state.result = p_arg;
  1463. Variant::CallError err;
  1464. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1465. bool completed = true;
  1466. // If the return value is a GDScriptFunctionState reference,
  1467. // then the function did yield again after resuming.
  1468. if (ret.is_ref()) {
  1469. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1470. if (gdfs && gdfs->function == function) {
  1471. completed = false;
  1472. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1473. }
  1474. }
  1475. function = NULL; //cleaned up;
  1476. state.result = Variant();
  1477. if (completed) {
  1478. if (first_state.is_valid()) {
  1479. first_state->emit_signal("completed", ret);
  1480. } else {
  1481. emit_signal("completed", ret);
  1482. }
  1483. }
  1484. return ret;
  1485. }
  1486. void GDScriptFunctionState::_bind_methods() {
  1487. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1488. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1489. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1490. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1491. }
  1492. GDScriptFunctionState::GDScriptFunctionState() {
  1493. function = NULL;
  1494. }
  1495. GDScriptFunctionState::~GDScriptFunctionState() {
  1496. if (function != NULL) {
  1497. //never called, deinitialize stack
  1498. for (int i = 0; i < state.stack_size; i++) {
  1499. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1500. v->~Variant();
  1501. }
  1502. }
  1503. }