gd_function.cpp 37 KB

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