gdscript_function.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 *script;
  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. script = p_state->script.ptr();
  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. script = p_instance->script.ptr();
  282. } else {
  283. script = _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, script, 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, script, 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.script = Ref<GDScript>(_script);
  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.result_pos=ip+ipofs-1;
  791. gdfs->state.defarg = defarg;
  792. gdfs->state.instance = p_instance;
  793. gdfs->function = this;
  794. retvalue = gdfs;
  795. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  796. //do the oneshot connect
  797. GET_VARIANT_PTR(argobj, 1);
  798. GET_VARIANT_PTR(argname, 2);
  799. #ifdef DEBUG_ENABLED
  800. if (argobj->get_type() != Variant::OBJECT) {
  801. err_text = "First argument of yield() not of type object.";
  802. OPCODE_BREAK;
  803. }
  804. if (argname->get_type() != Variant::STRING) {
  805. err_text = "Second argument of yield() not a string (for signal name).";
  806. OPCODE_BREAK;
  807. }
  808. #endif
  809. Object *obj = argobj->operator Object *();
  810. String signal = argname->operator String();
  811. #ifdef DEBUG_ENABLED
  812. if (!obj) {
  813. err_text = "First argument of yield() is null.";
  814. OPCODE_BREAK;
  815. }
  816. if (ScriptDebugger::get_singleton()) {
  817. if (!ObjectDB::instance_validate(obj)) {
  818. err_text = "First argument of yield() is a previously freed instance.";
  819. OPCODE_BREAK;
  820. }
  821. }
  822. if (signal.length() == 0) {
  823. err_text = "Second argument of yield() is an empty string (for signal name).";
  824. OPCODE_BREAK;
  825. }
  826. #endif
  827. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  828. #ifdef DEBUG_ENABLED
  829. if (err != OK) {
  830. err_text = "Error connecting to signal: " + signal + " during yield().";
  831. OPCODE_BREAK;
  832. }
  833. #endif
  834. }
  835. exit_ok = true;
  836. OPCODE_BREAK;
  837. }
  838. OPCODE(OPCODE_YIELD_RESUME) {
  839. CHECK_SPACE(2);
  840. #ifdef DEBUG_ENABLED
  841. if (!p_state) {
  842. err_text = ("Invalid Resume (bug?)");
  843. OPCODE_BREAK;
  844. }
  845. #endif
  846. GET_VARIANT_PTR(result, 1);
  847. *result = p_state->result;
  848. ip += 2;
  849. }
  850. DISPATCH_OPCODE;
  851. OPCODE(OPCODE_JUMP) {
  852. CHECK_SPACE(2);
  853. int to = _code_ptr[ip + 1];
  854. GD_ERR_BREAK(to < 0 || to > _code_size);
  855. ip = to;
  856. }
  857. DISPATCH_OPCODE;
  858. OPCODE(OPCODE_JUMP_IF) {
  859. CHECK_SPACE(3);
  860. GET_VARIANT_PTR(test, 1);
  861. bool result = test->booleanize();
  862. if (result) {
  863. int to = _code_ptr[ip + 2];
  864. GD_ERR_BREAK(to < 0 || to > _code_size);
  865. ip = to;
  866. } else {
  867. ip += 3;
  868. }
  869. }
  870. DISPATCH_OPCODE;
  871. OPCODE(OPCODE_JUMP_IF_NOT) {
  872. CHECK_SPACE(3);
  873. GET_VARIANT_PTR(test, 1);
  874. bool result = test->booleanize();
  875. if (!result) {
  876. int to = _code_ptr[ip + 2];
  877. GD_ERR_BREAK(to < 0 || to > _code_size);
  878. ip = to;
  879. } else {
  880. ip += 3;
  881. }
  882. }
  883. DISPATCH_OPCODE;
  884. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  885. CHECK_SPACE(2);
  886. ip = _default_arg_ptr[defarg];
  887. }
  888. DISPATCH_OPCODE;
  889. OPCODE(OPCODE_RETURN) {
  890. CHECK_SPACE(2);
  891. GET_VARIANT_PTR(r, 1);
  892. retvalue = *r;
  893. exit_ok = true;
  894. OPCODE_BREAK;
  895. }
  896. OPCODE(OPCODE_ITERATE_BEGIN) {
  897. CHECK_SPACE(8); //space for this a regular iterate
  898. GET_VARIANT_PTR(counter, 1);
  899. GET_VARIANT_PTR(container, 2);
  900. bool valid;
  901. if (!container->iter_init(*counter, valid)) {
  902. #ifdef DEBUG_ENABLED
  903. if (!valid) {
  904. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  905. OPCODE_BREAK;
  906. }
  907. #endif
  908. int jumpto = _code_ptr[ip + 3];
  909. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  910. ip = jumpto;
  911. } else {
  912. GET_VARIANT_PTR(iterator, 4);
  913. *iterator = container->iter_get(*counter, valid);
  914. #ifdef DEBUG_ENABLED
  915. if (!valid) {
  916. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  917. OPCODE_BREAK;
  918. }
  919. #endif
  920. ip += 5; //skip regular iterate which is always next
  921. }
  922. }
  923. DISPATCH_OPCODE;
  924. OPCODE(OPCODE_ITERATE) {
  925. CHECK_SPACE(4);
  926. GET_VARIANT_PTR(counter, 1);
  927. GET_VARIANT_PTR(container, 2);
  928. bool valid;
  929. if (!container->iter_next(*counter, valid)) {
  930. #ifdef DEBUG_ENABLED
  931. if (!valid) {
  932. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  933. OPCODE_BREAK;
  934. }
  935. #endif
  936. int jumpto = _code_ptr[ip + 3];
  937. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  938. ip = jumpto;
  939. } else {
  940. GET_VARIANT_PTR(iterator, 4);
  941. *iterator = container->iter_get(*counter, valid);
  942. #ifdef DEBUG_ENABLED
  943. if (!valid) {
  944. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  945. OPCODE_BREAK;
  946. }
  947. #endif
  948. ip += 5; //loop again
  949. }
  950. }
  951. DISPATCH_OPCODE;
  952. OPCODE(OPCODE_ASSERT) {
  953. CHECK_SPACE(2);
  954. GET_VARIANT_PTR(test, 1);
  955. #ifdef DEBUG_ENABLED
  956. bool result = test->booleanize();
  957. if (!result) {
  958. err_text = "Assertion failed.";
  959. OPCODE_BREAK;
  960. }
  961. #endif
  962. ip += 2;
  963. }
  964. DISPATCH_OPCODE;
  965. OPCODE(OPCODE_BREAKPOINT) {
  966. #ifdef DEBUG_ENABLED
  967. if (ScriptDebugger::get_singleton()) {
  968. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  969. }
  970. #endif
  971. ip += 1;
  972. }
  973. DISPATCH_OPCODE;
  974. OPCODE(OPCODE_LINE) {
  975. CHECK_SPACE(2);
  976. line = _code_ptr[ip + 1];
  977. ip += 2;
  978. if (ScriptDebugger::get_singleton()) {
  979. // line
  980. bool do_break = false;
  981. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  982. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  983. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  984. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  985. do_break = true;
  986. }
  987. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  988. do_break = true;
  989. if (do_break) {
  990. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  991. }
  992. ScriptDebugger::get_singleton()->line_poll();
  993. }
  994. }
  995. DISPATCH_OPCODE;
  996. OPCODE(OPCODE_END) {
  997. exit_ok = true;
  998. OPCODE_BREAK;
  999. }
  1000. // Enable for debugging
  1001. #if 0
  1002. default: {
  1003. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1004. OPCODE_BREAK;
  1005. }
  1006. #endif
  1007. }
  1008. OPCODES_END
  1009. #ifdef DEBUG_ENABLED
  1010. if (exit_ok)
  1011. OPCODE_OUT;
  1012. //error
  1013. // function, file, line, error, explanation
  1014. String err_file;
  1015. if (p_instance)
  1016. err_file = p_instance->script->path;
  1017. else if (script)
  1018. err_file = script->path;
  1019. if (err_file == "")
  1020. err_file = "<built-in>";
  1021. String err_func = name;
  1022. if (p_instance && p_instance->script->name != "")
  1023. err_func = p_instance->script->name + "." + err_func;
  1024. int err_line = line;
  1025. if (err_text == "") {
  1026. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1027. }
  1028. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1029. // debugger break did not happen
  1030. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1031. }
  1032. #endif
  1033. OPCODE_OUT;
  1034. }
  1035. OPCODES_OUT
  1036. #ifdef DEBUG_ENABLED
  1037. if (GDScriptLanguage::get_singleton()->profiling) {
  1038. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1039. profile.total_time += time_taken;
  1040. profile.self_time += time_taken - function_call_time;
  1041. profile.frame_total_time += time_taken;
  1042. profile.frame_self_time += time_taken - function_call_time;
  1043. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1044. }
  1045. if (ScriptDebugger::get_singleton())
  1046. GDScriptLanguage::get_singleton()->exit_function();
  1047. #endif
  1048. if (_stack_size) {
  1049. //free stack
  1050. for (int i = 0; i < _stack_size; i++)
  1051. stack[i].~Variant();
  1052. }
  1053. return retvalue;
  1054. }
  1055. const int *GDScriptFunction::get_code() const {
  1056. return _code_ptr;
  1057. }
  1058. int GDScriptFunction::get_code_size() const {
  1059. return _code_size;
  1060. }
  1061. Variant GDScriptFunction::get_constant(int p_idx) const {
  1062. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1063. return constants[p_idx];
  1064. }
  1065. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1066. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1067. return global_names[p_idx];
  1068. }
  1069. int GDScriptFunction::get_default_argument_count() const {
  1070. return default_arguments.size();
  1071. }
  1072. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1073. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1074. return default_arguments[p_idx];
  1075. }
  1076. StringName GDScriptFunction::get_name() const {
  1077. return name;
  1078. }
  1079. int GDScriptFunction::get_max_stack_size() const {
  1080. return _stack_size;
  1081. }
  1082. struct _GDFKC {
  1083. int order;
  1084. List<int> pos;
  1085. };
  1086. struct _GDFKCS {
  1087. int order;
  1088. StringName id;
  1089. int pos;
  1090. bool operator<(const _GDFKCS &p_r) const {
  1091. return order < p_r.order;
  1092. }
  1093. };
  1094. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1095. int oc = 0;
  1096. Map<StringName, _GDFKC> sdmap;
  1097. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1098. const StackDebug &sd = E->get();
  1099. if (sd.line > p_line)
  1100. break;
  1101. if (sd.added) {
  1102. if (!sdmap.has(sd.identifier)) {
  1103. _GDFKC d;
  1104. d.order = oc++;
  1105. d.pos.push_back(sd.pos);
  1106. sdmap[sd.identifier] = d;
  1107. } else {
  1108. sdmap[sd.identifier].pos.push_back(sd.pos);
  1109. }
  1110. } else {
  1111. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1112. sdmap[sd.identifier].pos.pop_back();
  1113. if (sdmap[sd.identifier].pos.empty())
  1114. sdmap.erase(sd.identifier);
  1115. }
  1116. }
  1117. List<_GDFKCS> stackpositions;
  1118. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1119. _GDFKCS spp;
  1120. spp.id = E->key();
  1121. spp.order = E->get().order;
  1122. spp.pos = E->get().pos.back()->get();
  1123. stackpositions.push_back(spp);
  1124. }
  1125. stackpositions.sort();
  1126. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1127. Pair<StringName, int> p;
  1128. p.first = E->get().id;
  1129. p.second = E->get().pos;
  1130. r_stackvars->push_back(p);
  1131. }
  1132. }
  1133. GDScriptFunction::GDScriptFunction() :
  1134. function_list(this) {
  1135. _stack_size = 0;
  1136. _call_size = 0;
  1137. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  1138. name = "<anonymous>";
  1139. #ifdef DEBUG_ENABLED
  1140. _func_cname = NULL;
  1141. if (GDScriptLanguage::get_singleton()->lock) {
  1142. GDScriptLanguage::get_singleton()->lock->lock();
  1143. }
  1144. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1145. if (GDScriptLanguage::get_singleton()->lock) {
  1146. GDScriptLanguage::get_singleton()->lock->unlock();
  1147. }
  1148. profile.call_count = 0;
  1149. profile.self_time = 0;
  1150. profile.total_time = 0;
  1151. profile.frame_call_count = 0;
  1152. profile.frame_self_time = 0;
  1153. profile.frame_total_time = 0;
  1154. profile.last_frame_call_count = 0;
  1155. profile.last_frame_self_time = 0;
  1156. profile.last_frame_total_time = 0;
  1157. #endif
  1158. }
  1159. GDScriptFunction::~GDScriptFunction() {
  1160. #ifdef DEBUG_ENABLED
  1161. if (GDScriptLanguage::get_singleton()->lock) {
  1162. GDScriptLanguage::get_singleton()->lock->lock();
  1163. }
  1164. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1165. if (GDScriptLanguage::get_singleton()->lock) {
  1166. GDScriptLanguage::get_singleton()->lock->unlock();
  1167. }
  1168. #endif
  1169. }
  1170. /////////////////////
  1171. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1172. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1173. #ifdef DEBUG_ENABLED
  1174. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1175. ERR_FAIL_V(Variant());
  1176. #else
  1177. return Variant();
  1178. #endif
  1179. }
  1180. Variant arg;
  1181. r_error.error = Variant::CallError::CALL_OK;
  1182. ERR_FAIL_COND_V(!function, Variant());
  1183. if (p_argcount == 0) {
  1184. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1185. r_error.argument = 1;
  1186. return Variant();
  1187. } else if (p_argcount == 1) {
  1188. //noooneee
  1189. } else if (p_argcount == 2) {
  1190. arg = *p_args[0];
  1191. } else {
  1192. Array extra_args;
  1193. for (int i = 0; i < p_argcount - 1; i++) {
  1194. extra_args.push_back(*p_args[i]);
  1195. }
  1196. arg = extra_args;
  1197. }
  1198. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1199. if (self.is_null()) {
  1200. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1201. r_error.argument = p_argcount - 1;
  1202. r_error.expected = Variant::OBJECT;
  1203. return Variant();
  1204. }
  1205. state.result = arg;
  1206. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1207. bool completed = true;
  1208. // If the return value is a GDScriptFunctionState reference,
  1209. // then the function did yield again after resuming.
  1210. if (ret.is_ref()) {
  1211. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1212. if (gdfs && gdfs->function == function) {
  1213. completed = false;
  1214. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1215. }
  1216. }
  1217. function = NULL; //cleaned up;
  1218. state.result = Variant();
  1219. if (completed) {
  1220. if (first_state.is_valid()) {
  1221. first_state->emit_signal("completed", ret);
  1222. } else {
  1223. emit_signal("completed", ret);
  1224. }
  1225. }
  1226. return ret;
  1227. }
  1228. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1229. if (function == NULL)
  1230. return false;
  1231. if (p_extended_check) {
  1232. //class instance gone?
  1233. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1234. return false;
  1235. }
  1236. return true;
  1237. }
  1238. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1239. ERR_FAIL_COND_V(!function, Variant());
  1240. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1241. #ifdef DEBUG_ENABLED
  1242. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1243. ERR_FAIL_V(Variant());
  1244. #else
  1245. return Variant();
  1246. #endif
  1247. }
  1248. state.result = p_arg;
  1249. Variant::CallError err;
  1250. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1251. bool completed = true;
  1252. // If the return value is a GDScriptFunctionState reference,
  1253. // then the function did yield again after resuming.
  1254. if (ret.is_ref()) {
  1255. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1256. if (gdfs && gdfs->function == function) {
  1257. completed = false;
  1258. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1259. }
  1260. }
  1261. function = NULL; //cleaned up;
  1262. state.result = Variant();
  1263. if (completed) {
  1264. if (first_state.is_valid()) {
  1265. first_state->emit_signal("completed", ret);
  1266. } else {
  1267. emit_signal("completed", ret);
  1268. }
  1269. }
  1270. return ret;
  1271. }
  1272. void GDScriptFunctionState::_bind_methods() {
  1273. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1274. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1275. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1276. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1277. }
  1278. GDScriptFunctionState::GDScriptFunctionState() {
  1279. function = NULL;
  1280. }
  1281. GDScriptFunctionState::~GDScriptFunctionState() {
  1282. if (function != NULL) {
  1283. //never called, deinitialize stack
  1284. for (int i = 0; i < state.stack_size; i++) {
  1285. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1286. v->~Variant();
  1287. }
  1288. }
  1289. }