gdscript_function.cpp 52 KB

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