gd_function.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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**)&p_state->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. case OPCODE_EXTENDS_TEST: {
  248. CHECK_SPACE(4);
  249. GET_VARIANT_PTR(a,1);
  250. GET_VARIANT_PTR(b,2);
  251. GET_VARIANT_PTR(dst,3);
  252. #ifdef DEBUG_ENABLED
  253. if (a->get_type()!=Variant::OBJECT || a->operator Object*()==NULL) {
  254. err_text="Left operand of 'extends' is not an instance of anything.";
  255. break;
  256. }
  257. if (b->get_type()!=Variant::OBJECT || b->operator Object*()==NULL) {
  258. err_text="Right operand of 'extends' is not a class.";
  259. break;
  260. }
  261. #endif
  262. Object *obj_A = *a;
  263. Object *obj_B = *b;
  264. GDScript *scr_B = obj_B->cast_to<GDScript>();
  265. bool extends_ok=false;
  266. if (scr_B) {
  267. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  268. //in other situation, this shoul return false.
  269. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language()==GDScriptLanguage::get_singleton()) {
  270. GDScript *cmp = static_cast<GDScript*>(obj_A->get_script_instance()->get_script().ptr());
  271. //bool found=false;
  272. while(cmp) {
  273. if (cmp==scr_B) {
  274. //inherits from script, all ok
  275. extends_ok=true;
  276. break;
  277. }
  278. cmp=cmp->_base;
  279. }
  280. }
  281. } else {
  282. GDNativeClass *nc= obj_B->cast_to<GDNativeClass>();
  283. if (!nc) {
  284. err_text="Right operand of 'extends' is not a class (type: '"+obj_B->get_class()+"').";
  285. break;
  286. }
  287. extends_ok=ClassDB::is_parent_class(obj_A->get_class_name(),nc->get_name());
  288. }
  289. *dst=extends_ok;
  290. ip+=4;
  291. } continue;
  292. case OPCODE_SET: {
  293. CHECK_SPACE(3);
  294. GET_VARIANT_PTR(dst,1);
  295. GET_VARIANT_PTR(index,2);
  296. GET_VARIANT_PTR(value,3);
  297. bool valid;
  298. dst->set(*index,*value,&valid);
  299. if (!valid) {
  300. String v = index->operator String();
  301. if (v!="") {
  302. v="'"+v+"'";
  303. } else {
  304. v="of type '"+_get_var_type(index)+"'";
  305. }
  306. err_text="Invalid set index "+v+" (on base: '"+_get_var_type(dst)+"').";
  307. break;
  308. }
  309. ip+=4;
  310. } continue;
  311. case OPCODE_GET: {
  312. CHECK_SPACE(3);
  313. GET_VARIANT_PTR(src,1);
  314. GET_VARIANT_PTR(index,2);
  315. GET_VARIANT_PTR(dst,3);
  316. bool valid;
  317. #ifdef DEBUG_ENABLED
  318. //allow better error message in cases where src and dst are the same stack position
  319. Variant ret = src->get(*index,&valid);
  320. #else
  321. *dst = src->get(*index,&valid);
  322. #endif
  323. if (!valid) {
  324. String v = index->operator String();
  325. if (v!="") {
  326. v="'"+v+"'";
  327. } else {
  328. v="of type '"+_get_var_type(index)+"'";
  329. }
  330. err_text="Invalid get index "+v+" (on base: '"+_get_var_type(src)+"').";
  331. break;
  332. }
  333. #ifdef DEBUG_ENABLED
  334. *dst=ret;
  335. #endif
  336. ip+=4;
  337. } continue;
  338. case OPCODE_SET_NAMED: {
  339. CHECK_SPACE(3);
  340. GET_VARIANT_PTR(dst,1);
  341. GET_VARIANT_PTR(value,3);
  342. int indexname = _code_ptr[ip+2];
  343. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  344. const StringName *index = &_global_names_ptr[indexname];
  345. bool valid;
  346. dst->set_named(*index,*value,&valid);
  347. if (!valid) {
  348. String err_type;
  349. err_text="Invalid set index '"+String(*index)+"' (on base: '"+_get_var_type(dst)+"').";
  350. break;
  351. }
  352. ip+=4;
  353. } continue;
  354. case OPCODE_GET_NAMED: {
  355. CHECK_SPACE(4);
  356. GET_VARIANT_PTR(src,1);
  357. GET_VARIANT_PTR(dst,3);
  358. int indexname = _code_ptr[ip+2];
  359. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  360. const StringName *index = &_global_names_ptr[indexname];
  361. bool valid;
  362. #ifdef DEBUG_ENABLED
  363. //allow better error message in cases where src and dst are the same stack position
  364. Variant ret = src->get_named(*index,&valid);
  365. #else
  366. *dst = src->get_named(*index,&valid);
  367. #endif
  368. if (!valid) {
  369. if (src->has_method(*index)) {
  370. err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"'). Did you mean '."+index->operator String()+"()' ?";
  371. } else {
  372. err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"').";
  373. }
  374. break;
  375. }
  376. #ifdef DEBUG_ENABLED
  377. *dst=ret;
  378. #endif
  379. ip+=4;
  380. } continue;
  381. case OPCODE_SET_MEMBER: {
  382. CHECK_SPACE(3);
  383. int indexname = _code_ptr[ip+1];
  384. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  385. const StringName *index = &_global_names_ptr[indexname];
  386. GET_VARIANT_PTR(src,2);
  387. bool valid;
  388. bool ok = ClassDB::set_property(p_instance->owner,*index,*src,&valid);
  389. #ifdef DEBUG_ENABLED
  390. if (!ok) {
  391. err_text="Internal error setting property: "+String(*index);
  392. break;
  393. } else if (!valid) {
  394. err_text="Error setting property '"+String(*index)+"' with value of type "+Variant::get_type_name(src->get_type())+".";
  395. break;
  396. }
  397. #endif
  398. ip+=3;
  399. } continue;
  400. case OPCODE_GET_MEMBER: {
  401. CHECK_SPACE(3);
  402. int indexname = _code_ptr[ip+1];
  403. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  404. const StringName *index = &_global_names_ptr[indexname];
  405. GET_VARIANT_PTR(dst,2);
  406. bool ok = ClassDB::get_property(p_instance->owner,*index,*dst);
  407. #ifdef DEBUG_ENABLED
  408. if (!ok) {
  409. err_text="Internal error getting property: "+String(*index);
  410. break;
  411. }
  412. #endif
  413. ip+=3;
  414. } continue;
  415. case OPCODE_ASSIGN: {
  416. CHECK_SPACE(3);
  417. GET_VARIANT_PTR(dst,1);
  418. GET_VARIANT_PTR(src,2);
  419. *dst = *src;
  420. ip+=3;
  421. } continue;
  422. case OPCODE_ASSIGN_TRUE: {
  423. CHECK_SPACE(2);
  424. GET_VARIANT_PTR(dst,1);
  425. *dst = true;
  426. ip+=2;
  427. } continue;
  428. case OPCODE_ASSIGN_FALSE: {
  429. CHECK_SPACE(2);
  430. GET_VARIANT_PTR(dst,1);
  431. *dst = false;
  432. ip+=2;
  433. } continue;
  434. case OPCODE_CONSTRUCT: {
  435. CHECK_SPACE(2);
  436. Variant::Type t=Variant::Type(_code_ptr[ip+1]);
  437. int argc=_code_ptr[ip+2];
  438. CHECK_SPACE(argc+2);
  439. Variant **argptrs = call_args;
  440. for(int i=0;i<argc;i++) {
  441. GET_VARIANT_PTR(v,3+i);
  442. argptrs[i]=v;
  443. }
  444. GET_VARIANT_PTR(dst,3+argc);
  445. Variant::CallError err;
  446. *dst = Variant::construct(t,(const Variant**)argptrs,argc,err);
  447. if (err.error!=Variant::CallError::CALL_OK) {
  448. err_text=_get_call_error(err,"'"+Variant::get_type_name(t)+"' constructor",(const Variant**)argptrs);
  449. break;
  450. }
  451. ip+=4+argc;
  452. //construct a basic type
  453. } continue;
  454. case OPCODE_CONSTRUCT_ARRAY: {
  455. CHECK_SPACE(1);
  456. int argc=_code_ptr[ip+1];
  457. Array array; //arrays are always shared
  458. array.resize(argc);
  459. CHECK_SPACE(argc+2);
  460. for(int i=0;i<argc;i++) {
  461. GET_VARIANT_PTR(v,2+i);
  462. array[i]=*v;
  463. }
  464. GET_VARIANT_PTR(dst,2+argc);
  465. *dst=array;
  466. ip+=3+argc;
  467. } continue;
  468. case OPCODE_CONSTRUCT_DICTIONARY: {
  469. CHECK_SPACE(1);
  470. int argc=_code_ptr[ip+1];
  471. Dictionary dict; //arrays are always shared
  472. CHECK_SPACE(argc*2+2);
  473. for(int i=0;i<argc;i++) {
  474. GET_VARIANT_PTR(k,2+i*2+0);
  475. GET_VARIANT_PTR(v,2+i*2+1);
  476. dict[*k]=*v;
  477. }
  478. GET_VARIANT_PTR(dst,2+argc*2);
  479. *dst=dict;
  480. ip+=3+argc*2;
  481. } continue;
  482. case OPCODE_CALL_RETURN:
  483. case OPCODE_CALL: {
  484. CHECK_SPACE(4);
  485. bool call_ret = _code_ptr[ip]==OPCODE_CALL_RETURN;
  486. int argc=_code_ptr[ip+1];
  487. GET_VARIANT_PTR(base,2);
  488. int nameg=_code_ptr[ip+3];
  489. ERR_BREAK(nameg<0 || nameg>=_global_names_count);
  490. const StringName *methodname = &_global_names_ptr[nameg];
  491. ERR_BREAK(argc<0);
  492. ip+=4;
  493. CHECK_SPACE(argc+1);
  494. Variant **argptrs = call_args;
  495. for(int i=0;i<argc;i++) {
  496. GET_VARIANT_PTR(v,i);
  497. argptrs[i]=v;
  498. }
  499. #ifdef DEBUG_ENABLED
  500. uint64_t call_time;
  501. if (GDScriptLanguage::get_singleton()->profiling) {
  502. call_time=OS::get_singleton()->get_ticks_usec();
  503. }
  504. #endif
  505. Variant::CallError err;
  506. if (call_ret) {
  507. GET_VARIANT_PTR(ret,argc);
  508. base->call_ptr(*methodname,(const Variant**)argptrs,argc,ret,err);
  509. } else {
  510. base->call_ptr(*methodname,(const Variant**)argptrs,argc,NULL,err);
  511. }
  512. #ifdef DEBUG_ENABLED
  513. if (GDScriptLanguage::get_singleton()->profiling) {
  514. function_call_time+=OS::get_singleton()->get_ticks_usec() - call_time;
  515. }
  516. #endif
  517. if (err.error!=Variant::CallError::CALL_OK) {
  518. String methodstr = *methodname;
  519. String basestr = _get_var_type(base);
  520. if (methodstr=="call") {
  521. if (argc>=1) {
  522. methodstr=String(*argptrs[0])+" (via call)";
  523. if (err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  524. err.argument-=1;
  525. }
  526. }
  527. } if (methodstr=="free") {
  528. if (err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  529. if (base->is_ref()) {
  530. err_text="Attempted to free a reference.";
  531. break;
  532. } else if (base->get_type()==Variant::OBJECT) {
  533. err_text="Attempted to free a locked object (calling or emitting).";
  534. break;
  535. }
  536. }
  537. }
  538. err_text=_get_call_error(err,"function '"+methodstr+"' in base '"+basestr+"'",(const Variant**)argptrs);
  539. break;
  540. }
  541. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  542. ip+=argc+1;
  543. } continue;
  544. case OPCODE_CALL_BUILT_IN: {
  545. CHECK_SPACE(4);
  546. GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip+1]);
  547. int argc=_code_ptr[ip+2];
  548. ERR_BREAK(argc<0);
  549. ip+=3;
  550. CHECK_SPACE(argc+1);
  551. Variant **argptrs = call_args;
  552. for(int i=0;i<argc;i++) {
  553. GET_VARIANT_PTR(v,i);
  554. argptrs[i]=v;
  555. }
  556. GET_VARIANT_PTR(dst,argc);
  557. Variant::CallError err;
  558. GDFunctions::call(func,(const Variant**)argptrs,argc,*dst,err);
  559. if (err.error!=Variant::CallError::CALL_OK) {
  560. String methodstr = GDFunctions::get_func_name(func);
  561. if (dst->get_type()==Variant::STRING) {
  562. //call provided error string
  563. err_text="Error calling built-in function '"+methodstr+"': "+String(*dst);
  564. } else {
  565. err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
  566. }
  567. break;
  568. }
  569. ip+=argc+1;
  570. } continue;
  571. case OPCODE_CALL_SELF: {
  572. } break;
  573. case OPCODE_CALL_SELF_BASE: {
  574. CHECK_SPACE(2);
  575. int self_fun = _code_ptr[ip+1];
  576. #ifdef DEBUG_ENABLED
  577. if (self_fun<0 || self_fun>=_global_names_count) {
  578. err_text="compiler bug, function name not found";
  579. break;
  580. }
  581. #endif
  582. const StringName *methodname = &_global_names_ptr[self_fun];
  583. int argc=_code_ptr[ip+2];
  584. CHECK_SPACE(2+argc+1);
  585. Variant **argptrs = call_args;
  586. for(int i=0;i<argc;i++) {
  587. GET_VARIANT_PTR(v,i+3);
  588. argptrs[i]=v;
  589. }
  590. GET_VARIANT_PTR(dst,argc+3);
  591. const GDScript *gds = _script;
  592. const Map<StringName,GDFunction*>::Element *E=NULL;
  593. while (gds->base.ptr()) {
  594. gds=gds->base.ptr();
  595. E=gds->member_functions.find(*methodname);
  596. if (E)
  597. break;
  598. }
  599. Variant::CallError err;
  600. if (E) {
  601. *dst=E->get()->call(p_instance,(const Variant**)argptrs,argc,err);
  602. } else if (gds->native.ptr()) {
  603. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  604. MethodBind *mb = ClassDB::get_method(gds->native->get_name(),*methodname);
  605. if (!mb) {
  606. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  607. } else {
  608. *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
  609. }
  610. } else {
  611. err.error=Variant::CallError::CALL_OK;
  612. }
  613. } else {
  614. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  615. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  616. } else {
  617. err.error=Variant::CallError::CALL_OK;
  618. }
  619. }
  620. if (err.error!=Variant::CallError::CALL_OK) {
  621. String methodstr = *methodname;
  622. err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
  623. break;
  624. }
  625. ip+=4+argc;
  626. } continue;
  627. case OPCODE_YIELD:
  628. case OPCODE_YIELD_SIGNAL: {
  629. int ipofs=1;
  630. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  631. CHECK_SPACE(4);
  632. ipofs+=2;
  633. } else {
  634. CHECK_SPACE(2);
  635. }
  636. Ref<GDFunctionState> gdfs = memnew( GDFunctionState );
  637. gdfs->function=this;
  638. gdfs->state.stack.resize(alloca_size);
  639. //copy variant stack
  640. for(int i=0;i<_stack_size;i++) {
  641. memnew_placement(&gdfs->state.stack[sizeof(Variant)*i],Variant(stack[i]));
  642. }
  643. gdfs->state.stack_size=_stack_size;
  644. gdfs->state.self=self;
  645. gdfs->state.alloca_size=alloca_size;
  646. gdfs->state._class=_class;
  647. gdfs->state.ip=ip+ipofs;
  648. gdfs->state.line=line;
  649. gdfs->state.instance_id=(p_instance && p_instance->get_owner())?p_instance->get_owner()->get_instance_ID():0;
  650. gdfs->state.script_id=_class->get_instance_ID();
  651. //gdfs->state.result_pos=ip+ipofs-1;
  652. gdfs->state.defarg=defarg;
  653. gdfs->state.instance=p_instance;
  654. gdfs->function=this;
  655. retvalue=gdfs;
  656. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  657. GET_VARIANT_PTR(argobj,1);
  658. GET_VARIANT_PTR(argname,2);
  659. //do the oneshot connect
  660. if (argobj->get_type()!=Variant::OBJECT) {
  661. err_text="First argument of yield() not of type object.";
  662. break;
  663. }
  664. if (argname->get_type()!=Variant::STRING) {
  665. err_text="Second argument of yield() not a string (for signal name).";
  666. break;
  667. }
  668. Object *obj=argobj->operator Object *();
  669. String signal = argname->operator String();
  670. #ifdef DEBUG_ENABLED
  671. if (!obj) {
  672. err_text="First argument of yield() is null.";
  673. break;
  674. }
  675. if (ScriptDebugger::get_singleton()) {
  676. if (!ObjectDB::instance_validate(obj)) {
  677. err_text="First argument of yield() is a previously freed instance.";
  678. break;
  679. }
  680. }
  681. if (signal.length()==0) {
  682. err_text="Second argument of yield() is an empty string (for signal name).";
  683. break;
  684. }
  685. #endif
  686. Error err = obj->connect(signal,gdfs.ptr(),"_signal_callback",varray(gdfs),Object::CONNECT_ONESHOT);
  687. if (err!=OK) {
  688. err_text="Error connecting to signal: "+signal+" during yield().";
  689. break;
  690. }
  691. }
  692. exit_ok=true;
  693. } break;
  694. case OPCODE_YIELD_RESUME: {
  695. CHECK_SPACE(2);
  696. if (!p_state) {
  697. err_text=("Invalid Resume (bug?)");
  698. break;
  699. }
  700. GET_VARIANT_PTR(result,1);
  701. *result=p_state->result;
  702. ip+=2;
  703. } continue;
  704. case OPCODE_JUMP: {
  705. CHECK_SPACE(2);
  706. int to = _code_ptr[ip+1];
  707. ERR_BREAK(to<0 || to>_code_size);
  708. ip=to;
  709. } continue;
  710. case OPCODE_JUMP_IF: {
  711. CHECK_SPACE(3);
  712. GET_VARIANT_PTR(test,1);
  713. bool valid;
  714. bool result = test->booleanize(valid);
  715. #ifdef DEBUG_ENABLED
  716. if (!valid) {
  717. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  718. break;
  719. }
  720. #endif
  721. if (result) {
  722. int to = _code_ptr[ip+2];
  723. ERR_BREAK(to<0 || to>_code_size);
  724. ip=to;
  725. continue;
  726. }
  727. ip+=3;
  728. } continue;
  729. case OPCODE_JUMP_IF_NOT: {
  730. CHECK_SPACE(3);
  731. GET_VARIANT_PTR(test,1);
  732. bool valid;
  733. bool result = test->booleanize(valid);
  734. #ifdef DEBUG_ENABLED
  735. if (!valid) {
  736. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  737. break;
  738. }
  739. #endif
  740. if (!result) {
  741. int to = _code_ptr[ip+2];
  742. ERR_BREAK(to<0 || to>_code_size);
  743. ip=to;
  744. continue;
  745. }
  746. ip+=3;
  747. } continue;
  748. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  749. CHECK_SPACE(2);
  750. ip=_default_arg_ptr[defarg];
  751. } continue;
  752. case OPCODE_RETURN: {
  753. CHECK_SPACE(2);
  754. GET_VARIANT_PTR(r,1);
  755. retvalue=*r;
  756. exit_ok=true;
  757. } break;
  758. case OPCODE_ITERATE_BEGIN: {
  759. CHECK_SPACE(8); //space for this an regular iterate
  760. GET_VARIANT_PTR(counter,1);
  761. GET_VARIANT_PTR(container,2);
  762. bool valid;
  763. if (!container->iter_init(*counter,valid)) {
  764. if (!valid) {
  765. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
  766. break;
  767. }
  768. int jumpto=_code_ptr[ip+3];
  769. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  770. ip=jumpto;
  771. continue;
  772. }
  773. GET_VARIANT_PTR(iterator,4);
  774. *iterator=container->iter_get(*counter,valid);
  775. if (!valid) {
  776. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
  777. break;
  778. }
  779. ip+=5; //skip regular iterate which is always next
  780. } continue;
  781. case OPCODE_ITERATE: {
  782. CHECK_SPACE(4);
  783. GET_VARIANT_PTR(counter,1);
  784. GET_VARIANT_PTR(container,2);
  785. bool valid;
  786. if (!container->iter_next(*counter,valid)) {
  787. if (!valid) {
  788. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
  789. break;
  790. }
  791. int jumpto=_code_ptr[ip+3];
  792. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  793. ip=jumpto;
  794. continue;
  795. }
  796. GET_VARIANT_PTR(iterator,4);
  797. *iterator=container->iter_get(*counter,valid);
  798. if (!valid) {
  799. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
  800. break;
  801. }
  802. ip+=5; //loop again
  803. } continue;
  804. case OPCODE_ASSERT: {
  805. CHECK_SPACE(2);
  806. GET_VARIANT_PTR(test,1);
  807. #ifdef DEBUG_ENABLED
  808. bool valid;
  809. bool result = test->booleanize(valid);
  810. if (!valid) {
  811. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  812. break;
  813. }
  814. if (!result) {
  815. err_text="Assertion failed.";
  816. break;
  817. }
  818. #endif
  819. ip+=2;
  820. } continue;
  821. case OPCODE_BREAKPOINT: {
  822. #ifdef DEBUG_ENABLED
  823. if (ScriptDebugger::get_singleton()) {
  824. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement",true);
  825. }
  826. #endif
  827. ip+=1;
  828. } continue;
  829. case OPCODE_LINE: {
  830. CHECK_SPACE(2);
  831. line=_code_ptr[ip+1];
  832. ip+=2;
  833. if (ScriptDebugger::get_singleton()) {
  834. // line
  835. bool do_break=false;
  836. if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
  837. if (ScriptDebugger::get_singleton()->get_depth()<=0)
  838. ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
  839. if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
  840. do_break=true;
  841. }
  842. if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
  843. do_break=true;
  844. if (do_break) {
  845. GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
  846. }
  847. ScriptDebugger::get_singleton()->line_poll();
  848. }
  849. } continue;
  850. case OPCODE_END: {
  851. exit_ok=true;
  852. break;
  853. } break;
  854. default: {
  855. err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
  856. } break;
  857. }
  858. if (exit_ok)
  859. break;
  860. //error
  861. // function, file, line, error, explanation
  862. String err_file;
  863. if (p_instance)
  864. err_file=p_instance->script->path;
  865. else if (_class)
  866. err_file=_class->path;
  867. if (err_file=="")
  868. err_file="<built-in>";
  869. String err_func = name;
  870. if (p_instance && p_instance->script->name!="")
  871. err_func=p_instance->script->name+"."+err_func;
  872. int err_line=line;
  873. if (err_text=="") {
  874. err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
  875. }
  876. if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  877. // debugger break did not happen
  878. _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data(),ERR_HANDLER_SCRIPT);
  879. }
  880. break;
  881. }
  882. #ifdef DEBUG_ENABLED
  883. if (GDScriptLanguage::get_singleton()->profiling) {
  884. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  885. profile.total_time+=time_taken;
  886. profile.self_time+=time_taken-function_call_time;
  887. profile.frame_total_time+=time_taken;
  888. profile.frame_self_time+=time_taken-function_call_time;
  889. GDScriptLanguage::get_singleton()->script_frame_time+=time_taken-function_call_time;
  890. }
  891. #endif
  892. if (ScriptDebugger::get_singleton())
  893. GDScriptLanguage::get_singleton()->exit_function();
  894. if (_stack_size) {
  895. //free stack
  896. for(int i=0;i<_stack_size;i++)
  897. stack[i].~Variant();
  898. }
  899. return retvalue;
  900. }
  901. const int* GDFunction::get_code() const {
  902. return _code_ptr;
  903. }
  904. int GDFunction::get_code_size() const{
  905. return _code_size;
  906. }
  907. Variant GDFunction::get_constant(int p_idx) const {
  908. ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
  909. return constants[p_idx];
  910. }
  911. StringName GDFunction::get_global_name(int p_idx) const {
  912. ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
  913. return global_names[p_idx];
  914. }
  915. int GDFunction::get_default_argument_count() const {
  916. return default_arguments.size();
  917. }
  918. int GDFunction::get_default_argument_addr(int p_arg) const{
  919. ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
  920. return default_arguments[p_arg];
  921. }
  922. StringName GDFunction::get_name() const {
  923. return name;
  924. }
  925. int GDFunction::get_max_stack_size() const {
  926. return _stack_size;
  927. }
  928. struct _GDFKC {
  929. int order;
  930. List<int> pos;
  931. };
  932. struct _GDFKCS {
  933. int order;
  934. StringName id;
  935. int pos;
  936. bool operator<(const _GDFKCS &p_r) const {
  937. return order<p_r.order;
  938. }
  939. };
  940. void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
  941. int oc=0;
  942. Map<StringName,_GDFKC> sdmap;
  943. for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
  944. const StackDebug &sd=E->get();
  945. if (sd.line>p_line)
  946. break;
  947. if (sd.added) {
  948. if (!sdmap.has(sd.identifier)) {
  949. _GDFKC d;
  950. d.order=oc++;
  951. d.pos.push_back(sd.pos);
  952. sdmap[sd.identifier]=d;
  953. } else {
  954. sdmap[sd.identifier].pos.push_back(sd.pos);
  955. }
  956. } else {
  957. ERR_CONTINUE(!sdmap.has(sd.identifier));
  958. sdmap[sd.identifier].pos.pop_back();
  959. if (sdmap[sd.identifier].pos.empty())
  960. sdmap.erase(sd.identifier);
  961. }
  962. }
  963. List<_GDFKCS> stackpositions;
  964. for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
  965. _GDFKCS spp;
  966. spp.id=E->key();
  967. spp.order=E->get().order;
  968. spp.pos=E->get().pos.back()->get();
  969. stackpositions.push_back(spp);
  970. }
  971. stackpositions.sort();
  972. for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
  973. Pair<StringName,int> p;
  974. p.first=E->get().id;
  975. p.second=E->get().pos;
  976. r_stackvars->push_back(p);
  977. }
  978. }
  979. #if 0
  980. void GDFunction::clear() {
  981. name=StringName();
  982. constants.clear();
  983. _stack_size=0;
  984. code.clear();
  985. _constants_ptr=NULL;
  986. _constant_count=0;
  987. _global_names_ptr=NULL;
  988. _global_names_count=0;
  989. _code_ptr=NULL;
  990. _code_size=0;
  991. }
  992. #endif
  993. GDFunction::GDFunction() : function_list(this) {
  994. _stack_size=0;
  995. _call_size=0;
  996. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  997. name="<anonymous>";
  998. #ifdef DEBUG_ENABLED
  999. _func_cname=NULL;
  1000. if (GDScriptLanguage::get_singleton()->lock) {
  1001. GDScriptLanguage::get_singleton()->lock->lock();
  1002. }
  1003. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1004. if (GDScriptLanguage::get_singleton()->lock) {
  1005. GDScriptLanguage::get_singleton()->lock->unlock();
  1006. }
  1007. profile.call_count=0;
  1008. profile.self_time=0;
  1009. profile.total_time=0;
  1010. profile.frame_call_count=0;
  1011. profile.frame_self_time=0;
  1012. profile.frame_total_time=0;
  1013. profile.last_frame_call_count=0;
  1014. profile.last_frame_self_time=0;
  1015. profile.last_frame_total_time=0;
  1016. #endif
  1017. }
  1018. GDFunction::~GDFunction() {
  1019. #ifdef DEBUG_ENABLED
  1020. if (GDScriptLanguage::get_singleton()->lock) {
  1021. GDScriptLanguage::get_singleton()->lock->lock();
  1022. }
  1023. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1024. if (GDScriptLanguage::get_singleton()->lock) {
  1025. GDScriptLanguage::get_singleton()->lock->unlock();
  1026. }
  1027. #endif
  1028. }
  1029. /////////////////////
  1030. Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  1031. #ifdef DEBUG_ENABLED
  1032. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1033. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1034. ERR_FAIL_V(Variant());
  1035. }
  1036. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1037. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1038. ERR_FAIL_V(Variant());
  1039. }
  1040. #endif
  1041. Variant arg;
  1042. r_error.error=Variant::CallError::CALL_OK;
  1043. ERR_FAIL_COND_V(!function,Variant());
  1044. if (p_argcount==0) {
  1045. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1046. r_error.argument=1;
  1047. return Variant();
  1048. } else if (p_argcount==1) {
  1049. //noooneee
  1050. } else if (p_argcount==2) {
  1051. arg=*p_args[0];
  1052. } else {
  1053. Array extra_args;
  1054. for(int i=0;i<p_argcount-1;i++) {
  1055. extra_args.push_back(*p_args[i]);
  1056. }
  1057. arg=extra_args;
  1058. }
  1059. Ref<GDFunctionState> self = *p_args[p_argcount-1];
  1060. if (self.is_null()) {
  1061. r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1062. r_error.argument=p_argcount-1;
  1063. r_error.expected=Variant::OBJECT;
  1064. return Variant();
  1065. }
  1066. state.result=arg;
  1067. Variant ret = function->call(NULL,NULL,0,r_error,&state);
  1068. function=NULL; //cleaned up;
  1069. state.result=Variant();
  1070. return ret;
  1071. }
  1072. bool GDFunctionState::is_valid() const {
  1073. return function!=NULL;
  1074. }
  1075. Variant GDFunctionState::resume(const Variant& p_arg) {
  1076. ERR_FAIL_COND_V(!function,Variant());
  1077. #ifdef DEBUG_ENABLED
  1078. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1079. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1080. ERR_FAIL_V(Variant());
  1081. }
  1082. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1083. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1084. ERR_FAIL_V(Variant());
  1085. }
  1086. #endif
  1087. state.result=p_arg;
  1088. Variant::CallError err;
  1089. Variant ret = function->call(NULL,NULL,0,err,&state);
  1090. function=NULL; //cleaned up;
  1091. state.result=Variant();
  1092. return ret;
  1093. }
  1094. void GDFunctionState::_bind_methods() {
  1095. ClassDB::bind_method(_MD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
  1096. ClassDB::bind_method(_MD("is_valid"),&GDFunctionState::is_valid);
  1097. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
  1098. }
  1099. GDFunctionState::GDFunctionState() {
  1100. function=NULL;
  1101. }
  1102. GDFunctionState::~GDFunctionState() {
  1103. if (function!=NULL) {
  1104. //never called, deinitialize stack
  1105. for(int i=0;i<state.stack_size;i++) {
  1106. Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
  1107. v->~Variant();
  1108. }
  1109. }
  1110. }