gd_function.cpp 33 KB

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