gd_function.cpp 32 KB

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