gdscript_function.cpp 43 KB

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