gd_function.cpp 31 KB

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