gdscript_function.cpp 42 KB

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