gdscript_function.cpp 49 KB

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