gd_function.cpp 31 KB

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