gd_function.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. base->call_ptr(*methodname,(const Variant**)argptrs,argc,ret,err);
  476. } else {
  477. base->call_ptr(*methodname,(const Variant**)argptrs,argc,NULL,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. if (dst->get_type()==Variant::STRING) {
  529. //call provided error string
  530. err_text="Error calling built-in function '"+methodstr+"': "+String(*dst);
  531. } else {
  532. err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
  533. }
  534. break;
  535. }
  536. ip+=argc+1;
  537. } continue;
  538. case OPCODE_CALL_SELF: {
  539. } break;
  540. case OPCODE_CALL_SELF_BASE: {
  541. CHECK_SPACE(2);
  542. int self_fun = _code_ptr[ip+1];
  543. #ifdef DEBUG_ENABLED
  544. if (self_fun<0 || self_fun>=_global_names_count) {
  545. err_text="compiler bug, function name not found";
  546. break;
  547. }
  548. #endif
  549. const StringName *methodname = &_global_names_ptr[self_fun];
  550. int argc=_code_ptr[ip+2];
  551. CHECK_SPACE(2+argc+1);
  552. Variant **argptrs = call_args;
  553. for(int i=0;i<argc;i++) {
  554. GET_VARIANT_PTR(v,i+3);
  555. argptrs[i]=v;
  556. }
  557. GET_VARIANT_PTR(dst,argc+3);
  558. const GDScript *gds = _script;
  559. const Map<StringName,GDFunction*>::Element *E=NULL;
  560. while (gds->base.ptr()) {
  561. gds=gds->base.ptr();
  562. E=gds->member_functions.find(*methodname);
  563. if (E)
  564. break;
  565. }
  566. Variant::CallError err;
  567. if (E) {
  568. *dst=E->get()->call(p_instance,(const Variant**)argptrs,argc,err);
  569. } else if (gds->native.ptr()) {
  570. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  571. MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(),*methodname);
  572. if (!mb) {
  573. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  574. } else {
  575. *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
  576. }
  577. } else {
  578. err.error=Variant::CallError::CALL_OK;
  579. }
  580. } else {
  581. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  582. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  583. } else {
  584. err.error=Variant::CallError::CALL_OK;
  585. }
  586. }
  587. if (err.error!=Variant::CallError::CALL_OK) {
  588. String methodstr = *methodname;
  589. err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
  590. break;
  591. }
  592. ip+=4+argc;
  593. } continue;
  594. case OPCODE_YIELD:
  595. case OPCODE_YIELD_SIGNAL: {
  596. int ipofs=1;
  597. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  598. CHECK_SPACE(4);
  599. ipofs+=2;
  600. } else {
  601. CHECK_SPACE(2);
  602. }
  603. Ref<GDFunctionState> gdfs = memnew( GDFunctionState );
  604. gdfs->function=this;
  605. gdfs->state.stack.resize(alloca_size);
  606. //copy variant stack
  607. for(int i=0;i<_stack_size;i++) {
  608. memnew_placement(&gdfs->state.stack[sizeof(Variant)*i],Variant(stack[i]));
  609. }
  610. gdfs->state.stack_size=_stack_size;
  611. gdfs->state.self=self;
  612. gdfs->state.alloca_size=alloca_size;
  613. gdfs->state._class=_class;
  614. gdfs->state.ip=ip+ipofs;
  615. gdfs->state.line=line;
  616. gdfs->state.instance_id=(p_instance && p_instance->get_owner())?p_instance->get_owner()->get_instance_ID():0;
  617. gdfs->state.script_id=_class->get_instance_ID();
  618. //gdfs->state.result_pos=ip+ipofs-1;
  619. gdfs->state.defarg=defarg;
  620. gdfs->state.instance=p_instance;
  621. gdfs->function=this;
  622. retvalue=gdfs;
  623. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  624. GET_VARIANT_PTR(argobj,1);
  625. GET_VARIANT_PTR(argname,2);
  626. //do the oneshot connect
  627. if (argobj->get_type()!=Variant::OBJECT) {
  628. err_text="First argument of yield() not of type object.";
  629. break;
  630. }
  631. if (argname->get_type()!=Variant::STRING) {
  632. err_text="Second argument of yield() not a string (for signal name).";
  633. break;
  634. }
  635. Object *obj=argobj->operator Object *();
  636. String signal = argname->operator String();
  637. #ifdef DEBUG_ENABLED
  638. if (!obj) {
  639. err_text="First argument of yield() is null.";
  640. break;
  641. }
  642. if (ScriptDebugger::get_singleton()) {
  643. if (!ObjectDB::instance_validate(obj)) {
  644. err_text="First argument of yield() is a previously freed instance.";
  645. break;
  646. }
  647. }
  648. if (signal.length()==0) {
  649. err_text="Second argument of yield() is an empty string (for signal name).";
  650. break;
  651. }
  652. #endif
  653. Error err = obj->connect(signal,gdfs.ptr(),"_signal_callback",varray(gdfs),Object::CONNECT_ONESHOT);
  654. if (err!=OK) {
  655. err_text="Error connecting to signal: "+signal+" during yield().";
  656. break;
  657. }
  658. }
  659. exit_ok=true;
  660. } break;
  661. case OPCODE_YIELD_RESUME: {
  662. CHECK_SPACE(2);
  663. if (!p_state) {
  664. err_text=("Invalid Resume (bug?)");
  665. break;
  666. }
  667. GET_VARIANT_PTR(result,1);
  668. *result=p_state->result;
  669. ip+=2;
  670. } continue;
  671. case OPCODE_JUMP: {
  672. CHECK_SPACE(2);
  673. int to = _code_ptr[ip+1];
  674. ERR_BREAK(to<0 || to>_code_size);
  675. ip=to;
  676. } continue;
  677. case OPCODE_JUMP_IF: {
  678. CHECK_SPACE(3);
  679. GET_VARIANT_PTR(test,1);
  680. bool valid;
  681. bool result = test->booleanize(valid);
  682. #ifdef DEBUG_ENABLED
  683. if (!valid) {
  684. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  685. break;
  686. }
  687. #endif
  688. if (result) {
  689. int to = _code_ptr[ip+2];
  690. ERR_BREAK(to<0 || to>_code_size);
  691. ip=to;
  692. continue;
  693. }
  694. ip+=3;
  695. } continue;
  696. case OPCODE_JUMP_IF_NOT: {
  697. CHECK_SPACE(3);
  698. GET_VARIANT_PTR(test,1);
  699. bool valid;
  700. bool result = test->booleanize(valid);
  701. #ifdef DEBUG_ENABLED
  702. if (!valid) {
  703. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  704. break;
  705. }
  706. #endif
  707. if (!result) {
  708. int to = _code_ptr[ip+2];
  709. ERR_BREAK(to<0 || to>_code_size);
  710. ip=to;
  711. continue;
  712. }
  713. ip+=3;
  714. } continue;
  715. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  716. CHECK_SPACE(2);
  717. ip=_default_arg_ptr[defarg];
  718. } continue;
  719. case OPCODE_RETURN: {
  720. CHECK_SPACE(2);
  721. GET_VARIANT_PTR(r,1);
  722. retvalue=*r;
  723. exit_ok=true;
  724. } break;
  725. case OPCODE_ITERATE_BEGIN: {
  726. CHECK_SPACE(8); //space for this an regular iterate
  727. GET_VARIANT_PTR(counter,1);
  728. GET_VARIANT_PTR(container,2);
  729. bool valid;
  730. if (!container->iter_init(*counter,valid)) {
  731. if (!valid) {
  732. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
  733. break;
  734. }
  735. int jumpto=_code_ptr[ip+3];
  736. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  737. ip=jumpto;
  738. continue;
  739. }
  740. GET_VARIANT_PTR(iterator,4);
  741. *iterator=container->iter_get(*counter,valid);
  742. if (!valid) {
  743. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
  744. break;
  745. }
  746. ip+=5; //skip regular iterate which is always next
  747. } continue;
  748. case OPCODE_ITERATE: {
  749. CHECK_SPACE(4);
  750. GET_VARIANT_PTR(counter,1);
  751. GET_VARIANT_PTR(container,2);
  752. bool valid;
  753. if (!container->iter_next(*counter,valid)) {
  754. if (!valid) {
  755. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
  756. break;
  757. }
  758. int jumpto=_code_ptr[ip+3];
  759. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  760. ip=jumpto;
  761. continue;
  762. }
  763. GET_VARIANT_PTR(iterator,4);
  764. *iterator=container->iter_get(*counter,valid);
  765. if (!valid) {
  766. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
  767. break;
  768. }
  769. ip+=5; //loop again
  770. } continue;
  771. case OPCODE_ASSERT: {
  772. CHECK_SPACE(2);
  773. GET_VARIANT_PTR(test,1);
  774. #ifdef DEBUG_ENABLED
  775. bool valid;
  776. bool result = test->booleanize(valid);
  777. if (!valid) {
  778. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  779. break;
  780. }
  781. if (!result) {
  782. err_text="Assertion failed.";
  783. break;
  784. }
  785. #endif
  786. ip+=2;
  787. } continue;
  788. case OPCODE_BREAKPOINT: {
  789. #ifdef DEBUG_ENABLED
  790. if (ScriptDebugger::get_singleton()) {
  791. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement",true);
  792. }
  793. #endif
  794. ip+=1;
  795. } continue;
  796. case OPCODE_LINE: {
  797. CHECK_SPACE(2);
  798. line=_code_ptr[ip+1];
  799. ip+=2;
  800. if (ScriptDebugger::get_singleton()) {
  801. // line
  802. bool do_break=false;
  803. if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
  804. if (ScriptDebugger::get_singleton()->get_depth()<=0)
  805. ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
  806. if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
  807. do_break=true;
  808. }
  809. if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
  810. do_break=true;
  811. if (do_break) {
  812. GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
  813. }
  814. ScriptDebugger::get_singleton()->line_poll();
  815. }
  816. } continue;
  817. case OPCODE_END: {
  818. exit_ok=true;
  819. break;
  820. } break;
  821. default: {
  822. err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
  823. } break;
  824. }
  825. if (exit_ok)
  826. break;
  827. //error
  828. // function, file, line, error, explanation
  829. String err_file;
  830. if (p_instance)
  831. err_file=p_instance->script->path;
  832. else if (_class)
  833. err_file=_class->path;
  834. if (err_file=="")
  835. err_file="<built-in>";
  836. String err_func = name;
  837. if (p_instance && p_instance->script->name!="")
  838. err_func=p_instance->script->name+"."+err_func;
  839. int err_line=line;
  840. if (err_text=="") {
  841. err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
  842. }
  843. if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  844. // debugger break did not happen
  845. _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data(),ERR_HANDLER_SCRIPT);
  846. }
  847. break;
  848. }
  849. #ifdef DEBUG_ENABLED
  850. if (GDScriptLanguage::get_singleton()->profiling) {
  851. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  852. profile.total_time+=time_taken;
  853. profile.self_time+=time_taken-function_call_time;
  854. profile.frame_total_time+=time_taken;
  855. profile.frame_self_time+=time_taken-function_call_time;
  856. GDScriptLanguage::get_singleton()->script_frame_time+=time_taken-function_call_time;
  857. }
  858. #endif
  859. if (ScriptDebugger::get_singleton())
  860. GDScriptLanguage::get_singleton()->exit_function();
  861. if (_stack_size) {
  862. //free stack
  863. for(int i=0;i<_stack_size;i++)
  864. stack[i].~Variant();
  865. }
  866. return retvalue;
  867. }
  868. const int* GDFunction::get_code() const {
  869. return _code_ptr;
  870. }
  871. int GDFunction::get_code_size() const{
  872. return _code_size;
  873. }
  874. Variant GDFunction::get_constant(int p_idx) const {
  875. ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
  876. return constants[p_idx];
  877. }
  878. StringName GDFunction::get_global_name(int p_idx) const {
  879. ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
  880. return global_names[p_idx];
  881. }
  882. int GDFunction::get_default_argument_count() const {
  883. return default_arguments.size();
  884. }
  885. int GDFunction::get_default_argument_addr(int p_arg) const{
  886. ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
  887. return default_arguments[p_arg];
  888. }
  889. StringName GDFunction::get_name() const {
  890. return name;
  891. }
  892. int GDFunction::get_max_stack_size() const {
  893. return _stack_size;
  894. }
  895. struct _GDFKC {
  896. int order;
  897. List<int> pos;
  898. };
  899. struct _GDFKCS {
  900. int order;
  901. StringName id;
  902. int pos;
  903. bool operator<(const _GDFKCS &p_r) const {
  904. return order<p_r.order;
  905. }
  906. };
  907. void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
  908. int oc=0;
  909. Map<StringName,_GDFKC> sdmap;
  910. for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
  911. const StackDebug &sd=E->get();
  912. if (sd.line>p_line)
  913. break;
  914. if (sd.added) {
  915. if (!sdmap.has(sd.identifier)) {
  916. _GDFKC d;
  917. d.order=oc++;
  918. d.pos.push_back(sd.pos);
  919. sdmap[sd.identifier]=d;
  920. } else {
  921. sdmap[sd.identifier].pos.push_back(sd.pos);
  922. }
  923. } else {
  924. ERR_CONTINUE(!sdmap.has(sd.identifier));
  925. sdmap[sd.identifier].pos.pop_back();
  926. if (sdmap[sd.identifier].pos.empty())
  927. sdmap.erase(sd.identifier);
  928. }
  929. }
  930. List<_GDFKCS> stackpositions;
  931. for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
  932. _GDFKCS spp;
  933. spp.id=E->key();
  934. spp.order=E->get().order;
  935. spp.pos=E->get().pos.back()->get();
  936. stackpositions.push_back(spp);
  937. }
  938. stackpositions.sort();
  939. for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
  940. Pair<StringName,int> p;
  941. p.first=E->get().id;
  942. p.second=E->get().pos;
  943. r_stackvars->push_back(p);
  944. }
  945. }
  946. #if 0
  947. void GDFunction::clear() {
  948. name=StringName();
  949. constants.clear();
  950. _stack_size=0;
  951. code.clear();
  952. _constants_ptr=NULL;
  953. _constant_count=0;
  954. _global_names_ptr=NULL;
  955. _global_names_count=0;
  956. _code_ptr=NULL;
  957. _code_size=0;
  958. }
  959. #endif
  960. GDFunction::GDFunction() : function_list(this) {
  961. _stack_size=0;
  962. _call_size=0;
  963. name="<anonymous>";
  964. #ifdef DEBUG_ENABLED
  965. _func_cname=NULL;
  966. if (GDScriptLanguage::get_singleton()->lock) {
  967. GDScriptLanguage::get_singleton()->lock->lock();
  968. }
  969. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  970. if (GDScriptLanguage::get_singleton()->lock) {
  971. GDScriptLanguage::get_singleton()->lock->unlock();
  972. }
  973. profile.call_count=0;
  974. profile.self_time=0;
  975. profile.total_time=0;
  976. profile.frame_call_count=0;
  977. profile.frame_self_time=0;
  978. profile.frame_total_time=0;
  979. profile.last_frame_call_count=0;
  980. profile.last_frame_self_time=0;
  981. profile.last_frame_total_time=0;
  982. #endif
  983. }
  984. GDFunction::~GDFunction() {
  985. #ifdef DEBUG_ENABLED
  986. if (GDScriptLanguage::get_singleton()->lock) {
  987. GDScriptLanguage::get_singleton()->lock->lock();
  988. }
  989. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  990. if (GDScriptLanguage::get_singleton()->lock) {
  991. GDScriptLanguage::get_singleton()->lock->unlock();
  992. }
  993. #endif
  994. }
  995. /////////////////////
  996. Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  997. #ifdef DEBUG_ENABLED
  998. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  999. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1000. ERR_FAIL_V(Variant());
  1001. }
  1002. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1003. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1004. ERR_FAIL_V(Variant());
  1005. }
  1006. #endif
  1007. Variant arg;
  1008. r_error.error=Variant::CallError::CALL_OK;
  1009. ERR_FAIL_COND_V(!function,Variant());
  1010. if (p_argcount==0) {
  1011. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1012. r_error.argument=1;
  1013. return Variant();
  1014. } else if (p_argcount==1) {
  1015. //noooneee
  1016. } else if (p_argcount==2) {
  1017. arg=*p_args[0];
  1018. } else {
  1019. Array extra_args;
  1020. for(int i=0;i<p_argcount-1;i++) {
  1021. extra_args.push_back(*p_args[i]);
  1022. }
  1023. arg=extra_args;
  1024. }
  1025. Ref<GDFunctionState> self = *p_args[p_argcount-1];
  1026. if (self.is_null()) {
  1027. r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1028. r_error.argument=p_argcount-1;
  1029. r_error.expected=Variant::OBJECT;
  1030. return Variant();
  1031. }
  1032. state.result=arg;
  1033. Variant ret = function->call(NULL,NULL,0,r_error,&state);
  1034. function=NULL; //cleaned up;
  1035. state.result=Variant();
  1036. return ret;
  1037. }
  1038. bool GDFunctionState::is_valid() const {
  1039. return function!=NULL;
  1040. }
  1041. Variant GDFunctionState::resume(const Variant& p_arg) {
  1042. ERR_FAIL_COND_V(!function,Variant());
  1043. #ifdef DEBUG_ENABLED
  1044. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1045. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1046. ERR_FAIL_V(Variant());
  1047. }
  1048. if (state.script_id && !ObjectDB::get_instance(state.script_id)) {
  1049. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1050. ERR_FAIL_V(Variant());
  1051. }
  1052. #endif
  1053. state.result=p_arg;
  1054. Variant::CallError err;
  1055. Variant ret = function->call(NULL,NULL,0,err,&state);
  1056. function=NULL; //cleaned up;
  1057. state.result=Variant();
  1058. return ret;
  1059. }
  1060. void GDFunctionState::_bind_methods() {
  1061. ObjectTypeDB::bind_method(_MD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
  1062. ObjectTypeDB::bind_method(_MD("is_valid"),&GDFunctionState::is_valid);
  1063. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
  1064. }
  1065. GDFunctionState::GDFunctionState() {
  1066. function=NULL;
  1067. }
  1068. GDFunctionState::~GDFunctionState() {
  1069. if (function!=NULL) {
  1070. //never called, deinitialize stack
  1071. for(int i=0;i<state.stack_size;i++) {
  1072. Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
  1073. v->~Variant();
  1074. }
  1075. }
  1076. }