gd_function.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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_script.h"
  31. #include "os/os.h"
  32. #include "gd_functions.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**)stack + sizeof(Variant)*p_state->stack_size;
  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() : function_list(this) {
  1053. _stack_size=0;
  1054. _call_size=0;
  1055. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  1056. name="<anonymous>";
  1057. #ifdef DEBUG_ENABLED
  1058. _func_cname=NULL;
  1059. if (GDScriptLanguage::get_singleton()->lock) {
  1060. GDScriptLanguage::get_singleton()->lock->lock();
  1061. }
  1062. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1063. if (GDScriptLanguage::get_singleton()->lock) {
  1064. GDScriptLanguage::get_singleton()->lock->unlock();
  1065. }
  1066. profile.call_count=0;
  1067. profile.self_time=0;
  1068. profile.total_time=0;
  1069. profile.frame_call_count=0;
  1070. profile.frame_self_time=0;
  1071. profile.frame_total_time=0;
  1072. profile.last_frame_call_count=0;
  1073. profile.last_frame_self_time=0;
  1074. profile.last_frame_total_time=0;
  1075. #endif
  1076. }
  1077. GDFunction::~GDFunction() {
  1078. #ifdef DEBUG_ENABLED
  1079. if (GDScriptLanguage::get_singleton()->lock) {
  1080. GDScriptLanguage::get_singleton()->lock->lock();
  1081. }
  1082. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1083. if (GDScriptLanguage::get_singleton()->lock) {
  1084. GDScriptLanguage::get_singleton()->lock->unlock();
  1085. }
  1086. #endif
  1087. }
  1088. /////////////////////
  1089. Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  1090. #ifdef DEBUG_ENABLED
  1091. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1092. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1093. ERR_FAIL_V(Variant());
  1094. }
  1095. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1096. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1097. ERR_FAIL_V(Variant());
  1098. }
  1099. #endif
  1100. Variant arg;
  1101. r_error.error=Variant::CallError::CALL_OK;
  1102. ERR_FAIL_COND_V(!function,Variant());
  1103. if (p_argcount==0) {
  1104. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1105. r_error.argument=1;
  1106. return Variant();
  1107. } else if (p_argcount==1) {
  1108. //noooneee
  1109. } else if (p_argcount==2) {
  1110. arg=*p_args[0];
  1111. } else {
  1112. Array extra_args;
  1113. for(int i=0;i<p_argcount-1;i++) {
  1114. extra_args.push_back(*p_args[i]);
  1115. }
  1116. arg=extra_args;
  1117. }
  1118. Ref<GDFunctionState> self = *p_args[p_argcount-1];
  1119. if (self.is_null()) {
  1120. r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1121. r_error.argument=p_argcount-1;
  1122. r_error.expected=Variant::OBJECT;
  1123. return Variant();
  1124. }
  1125. state.result=arg;
  1126. Variant ret = function->call(NULL,NULL,0,r_error,&state);
  1127. function=NULL; //cleaned up;
  1128. state.result=Variant();
  1129. return ret;
  1130. }
  1131. bool GDFunctionState::is_valid() const {
  1132. return function!=NULL;
  1133. }
  1134. Variant GDFunctionState::resume(const Variant& p_arg) {
  1135. ERR_FAIL_COND_V(!function,Variant());
  1136. #ifdef DEBUG_ENABLED
  1137. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1138. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1139. ERR_FAIL_V(Variant());
  1140. }
  1141. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1142. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1143. ERR_FAIL_V(Variant());
  1144. }
  1145. #endif
  1146. state.result=p_arg;
  1147. Variant::CallError err;
  1148. Variant ret = function->call(NULL,NULL,0,err,&state);
  1149. function=NULL; //cleaned up;
  1150. state.result=Variant();
  1151. return ret;
  1152. }
  1153. void GDFunctionState::_bind_methods() {
  1154. ClassDB::bind_method(D_METHOD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
  1155. ClassDB::bind_method(D_METHOD("is_valid"),&GDFunctionState::is_valid);
  1156. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
  1157. }
  1158. GDFunctionState::GDFunctionState() {
  1159. function=NULL;
  1160. }
  1161. GDFunctionState::~GDFunctionState() {
  1162. if (function!=NULL) {
  1163. //never called, deinitialize stack
  1164. for(int i=0;i<state.stack_size;i++) {
  1165. Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
  1166. v->~Variant();
  1167. }
  1168. }
  1169. }