gd_script.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  1. /*************************************************************************/
  2. /* gd_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "gd_script.h"
  30. #include "globals.h"
  31. #include "global_constants.h"
  32. #include "gd_compiler.h"
  33. #include "os/file_access.h"
  34. /* TODO:
  35. *populate globals
  36. *do checks as close to debugger as possible (but don't do debugger)
  37. *const check plz
  38. *check arguments and default arguments in GDFunction
  39. -get property list in instance?
  40. *missing opcodes
  41. -const checks
  42. -make thread safe
  43. */
  44. Variant *GDFunction::_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self, Variant *p_stack,String& r_error) const{
  45. int address = p_address&ADDR_MASK;
  46. //sequential table (jump table generated by compiler)
  47. switch((p_address&ADDR_TYPE_MASK)>>ADDR_BITS) {
  48. case ADDR_TYPE_SELF: {
  49. if (!p_instance) {
  50. r_error="Cannot access self without instance.";
  51. return NULL;
  52. }
  53. return &self;
  54. } break;
  55. case ADDR_TYPE_CLASS: {
  56. return &p_script->_static_ref;
  57. } break;
  58. case ADDR_TYPE_MEMBER: {
  59. //member indexing is O(1)
  60. if (!p_instance) {
  61. r_error="Cannot access member without instance.";
  62. return NULL;
  63. }
  64. return &p_instance->members[address];
  65. } break;
  66. case ADDR_TYPE_CLASS_CONSTANT: {
  67. //todo change to index!
  68. GDScript *o=p_script;
  69. ERR_FAIL_INDEX_V(address,_global_names_count,NULL);
  70. const StringName *sn = &_global_names_ptr[address];
  71. while(o) {
  72. GDScript *s=o;
  73. while(s) {
  74. Map<StringName,Variant>::Element *E=s->constants.find(*sn);
  75. if (E) {
  76. return &E->get();
  77. }
  78. s=s->_base;
  79. }
  80. o=o->_owner;
  81. }
  82. ERR_EXPLAIN("GDCompiler bug..");
  83. ERR_FAIL_V(NULL);
  84. } break;
  85. case ADDR_TYPE_LOCAL_CONSTANT: {
  86. ERR_FAIL_INDEX_V(address,_constant_count,NULL);
  87. return &_constants_ptr[address];
  88. } break;
  89. case ADDR_TYPE_STACK:
  90. case ADDR_TYPE_STACK_VARIABLE: {
  91. ERR_FAIL_INDEX_V(address,_stack_size,NULL);
  92. return &p_stack[address];
  93. } break;
  94. case ADDR_TYPE_GLOBAL: {
  95. ERR_FAIL_INDEX_V(address,GDScriptLanguage::get_singleton()->get_global_array_size(),NULL);
  96. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  97. } break;
  98. case ADDR_TYPE_NIL: {
  99. return &nil;
  100. } break;
  101. }
  102. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  103. ERR_FAIL_V(NULL);
  104. return NULL;
  105. }
  106. String GDFunction::_get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const {
  107. String err_text;
  108. if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  109. int errorarg=p_err.argument;
  110. 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)+".";
  111. } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  112. err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
  113. } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  114. err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
  115. } else if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  116. err_text="Invalid call. Unexisting "+p_where+".";
  117. } else if (p_err.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  118. err_text="Attempt to call "+p_where+" on a null instance.";
  119. } else {
  120. err_text="Bug, call error: #"+itos(p_err.error);
  121. }
  122. return err_text;
  123. }
  124. static String _get_var_type(const Variant* p_type) {
  125. String basestr;
  126. if (p_type->get_type()==Variant::OBJECT) {
  127. Object *bobj = *p_type;
  128. if (!bobj) {
  129. basestr = "null instance";
  130. } else {
  131. #ifdef DEBUG_ENABLED
  132. if (ObjectDB::instance_validate(bobj)) {
  133. if (bobj->get_script_instance())
  134. basestr= bobj->get_type()+" ("+bobj->get_script_instance()->get_script()->get_path().get_file()+")";
  135. else
  136. basestr = bobj->get_type();
  137. } else {
  138. basestr="previously freed instance";
  139. }
  140. #else
  141. basestr="Object";
  142. #endif
  143. }
  144. } else {
  145. basestr = Variant::get_type_name(p_type->get_type());
  146. }
  147. return basestr;
  148. }
  149. Variant GDFunction::call(GDInstance *p_instance,const Variant **p_args, int p_argcount,Variant::CallError& r_err) {
  150. if (!_code_ptr) {
  151. return Variant();
  152. }
  153. r_err.error=Variant::CallError::CALL_OK;
  154. Variant self;
  155. Variant retvalue;
  156. Variant *stack = NULL;
  157. Variant **call_args;
  158. int defarg=0;
  159. #ifdef DEBUG_ENABLED
  160. //GDScriptLanguage::get_singleton()->calls++;
  161. #endif
  162. if (p_argcount!=_argument_count) {
  163. if (p_argcount>_argument_count) {
  164. r_err.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  165. r_err.argument=_argument_count;
  166. return Variant();
  167. } else if (p_argcount < _argument_count - _default_arg_count) {
  168. r_err.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  169. r_err.argument=_argument_count - _default_arg_count;
  170. return Variant();
  171. } else {
  172. defarg=_argument_count-p_argcount;
  173. }
  174. }
  175. uint32_t alloca_size = sizeof(Variant*)*_call_size + sizeof(Variant)*_stack_size;
  176. if (alloca_size) {
  177. uint8_t *aptr = (uint8_t*)alloca(alloca_size);
  178. if (_stack_size) {
  179. stack=(Variant*)aptr;
  180. for(int i=0;i<p_argcount;i++)
  181. memnew_placement(&stack[i],Variant(*p_args[i]));
  182. for(int i=p_argcount;i<_stack_size;i++)
  183. memnew_placement(&stack[i],Variant);
  184. } else {
  185. stack=NULL;
  186. }
  187. if (_call_size) {
  188. call_args = (Variant**)&aptr[sizeof(Variant)*_stack_size];
  189. } else {
  190. call_args=NULL;
  191. }
  192. } else {
  193. stack=NULL;
  194. call_args=NULL;
  195. }
  196. GDScript *_class;
  197. if (p_instance) {
  198. if (p_instance->base_ref && static_cast<Reference*>(p_instance->owner)->is_referenced()) {
  199. self=REF(static_cast<Reference*>(p_instance->owner));
  200. } else {
  201. self=p_instance->owner;
  202. }
  203. _class=p_instance->script.ptr();
  204. } else {
  205. _class=_script;
  206. }
  207. int ip=0;
  208. int line=_initial_line;
  209. String err_text;
  210. #ifdef DEBUG_ENABLED
  211. if (ScriptDebugger::get_singleton())
  212. GDScriptLanguage::get_singleton()->enter_function(p_instance,this,stack,&ip,&line);
  213. #define CHECK_SPACE(m_space)\
  214. ERR_BREAK((ip+m_space)>_code_size)
  215. #define GET_VARIANT_PTR(m_v,m_code_ofs) \
  216. Variant *m_v; \
  217. m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);\
  218. if (!m_v)\
  219. break;
  220. #else
  221. #define CHECK_SPACE(m_space)
  222. #define GET_VARIANT_PTR(m_v,m_code_ofs) \
  223. Variant *m_v; \
  224. m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);
  225. #endif
  226. bool exit_ok=false;
  227. while(ip<_code_size) {
  228. int last_opcode=_code_ptr[ip];
  229. switch(_code_ptr[ip]) {
  230. case OPCODE_OPERATOR: {
  231. CHECK_SPACE(5);
  232. bool valid;
  233. Variant::Operator op = (Variant::Operator)_code_ptr[ip+1];
  234. ERR_BREAK(op>=Variant::OP_MAX);
  235. GET_VARIANT_PTR(a,2);
  236. GET_VARIANT_PTR(b,3);
  237. GET_VARIANT_PTR(dst,4);
  238. Variant::evaluate(op,*a,*b,*dst,valid);
  239. if (!valid) {
  240. if (false && dst->get_type()==Variant::STRING) {
  241. //return a string when invalid with the error
  242. err_text=*dst;
  243. } else {
  244. 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)+"'.";
  245. }
  246. break;
  247. }
  248. ip+=5;
  249. } continue;
  250. case OPCODE_EXTENDS_TEST: {
  251. CHECK_SPACE(4);
  252. GET_VARIANT_PTR(a,1);
  253. GET_VARIANT_PTR(b,2);
  254. GET_VARIANT_PTR(dst,3);
  255. #ifdef DEBUG_ENABLED
  256. if (a->get_type()!=Variant::OBJECT || a->operator Object*()==NULL) {
  257. err_text="Left operand of 'extends' is not an instance of anything.";
  258. break;
  259. }
  260. if (b->get_type()!=Variant::OBJECT || b->operator Object*()==NULL) {
  261. err_text="Right operand of 'extends' is not a class.";
  262. break;
  263. }
  264. #endif
  265. Object *obj_A = *a;
  266. Object *obj_B = *b;
  267. GDScript *scr_B = obj_B->cast_to<GDScript>();
  268. bool extends_ok=false;
  269. if (scr_B) {
  270. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  271. //in other situation, this shoul return false.
  272. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language()==GDScriptLanguage::get_singleton()) {
  273. GDInstance *ins = static_cast<GDInstance*>(obj_A->get_script_instance());
  274. GDScript *cmp = ins->script.ptr();
  275. //bool found=false;
  276. while(cmp) {
  277. if (cmp==scr_B) {
  278. //inherits from script, all ok
  279. extends_ok=true;
  280. break;
  281. }
  282. cmp=cmp->_base;
  283. }
  284. }
  285. } else {
  286. GDNativeClass *nc= obj_B->cast_to<GDNativeClass>();
  287. if (!nc) {
  288. err_text="Right operand of 'extends' is not a class (type: '"+obj_B->get_type()+"').";
  289. break;
  290. }
  291. extends_ok=ObjectTypeDB::is_type(obj_A->get_type_name(),nc->get_name());
  292. }
  293. *dst=extends_ok;
  294. ip+=4;
  295. } continue;
  296. case OPCODE_SET: {
  297. CHECK_SPACE(3);
  298. GET_VARIANT_PTR(dst,1);
  299. GET_VARIANT_PTR(index,2);
  300. GET_VARIANT_PTR(value,3);
  301. bool valid;
  302. dst->set(*index,*value,&valid);
  303. if (!valid) {
  304. String v = index->operator String();
  305. if (v!="") {
  306. v="'"+v+"'";
  307. } else {
  308. v="of type '"+_get_var_type(index)+"'";
  309. }
  310. err_text="Invalid set index "+v+" (on base: '"+_get_var_type(dst)+"').";
  311. break;
  312. }
  313. ip+=4;
  314. } continue;
  315. case OPCODE_GET: {
  316. CHECK_SPACE(3);
  317. GET_VARIANT_PTR(src,1);
  318. GET_VARIANT_PTR(index,2);
  319. GET_VARIANT_PTR(dst,3);
  320. bool valid;
  321. *dst = src->get(*index,&valid);
  322. if (!valid) {
  323. String v = index->operator String();
  324. if (v!="") {
  325. v="'"+v+"'";
  326. } else {
  327. v="of type '"+_get_var_type(index)+"'";
  328. }
  329. err_text="Invalid get index "+v+" (on base: '"+_get_var_type(src)+"').";
  330. break;
  331. }
  332. ip+=4;
  333. } continue;
  334. case OPCODE_SET_NAMED: {
  335. CHECK_SPACE(3);
  336. GET_VARIANT_PTR(dst,1);
  337. GET_VARIANT_PTR(value,3);
  338. int indexname = _code_ptr[ip+2];
  339. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  340. const StringName *index = &_global_names_ptr[indexname];
  341. bool valid;
  342. dst->set_named(*index,*value,&valid);
  343. if (!valid) {
  344. String err_type;
  345. err_text="Invalid set index '"+String(*index)+"' (on base: '"+_get_var_type(dst)+"').";
  346. break;
  347. }
  348. ip+=4;
  349. } continue;
  350. case OPCODE_GET_NAMED: {
  351. CHECK_SPACE(3);
  352. GET_VARIANT_PTR(src,1);
  353. GET_VARIANT_PTR(dst,3);
  354. int indexname = _code_ptr[ip+2];
  355. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  356. const StringName *index = &_global_names_ptr[indexname];
  357. bool valid;
  358. *dst = src->get_named(*index,&valid);
  359. if (!valid) {
  360. err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"').";
  361. break;
  362. }
  363. ip+=4;
  364. } continue;
  365. case OPCODE_ASSIGN: {
  366. CHECK_SPACE(3);
  367. GET_VARIANT_PTR(dst,1);
  368. GET_VARIANT_PTR(src,2);
  369. *dst = *src;
  370. ip+=3;
  371. } continue;
  372. case OPCODE_ASSIGN_TRUE: {
  373. CHECK_SPACE(2);
  374. GET_VARIANT_PTR(dst,1);
  375. *dst = true;
  376. ip+=2;
  377. } continue;
  378. case OPCODE_ASSIGN_FALSE: {
  379. CHECK_SPACE(2);
  380. GET_VARIANT_PTR(dst,1);
  381. *dst = false;
  382. ip+=2;
  383. } continue;
  384. case OPCODE_CONSTRUCT: {
  385. CHECK_SPACE(2);
  386. Variant::Type t=Variant::Type(_code_ptr[ip+1]);
  387. int argc=_code_ptr[ip+2];
  388. CHECK_SPACE(argc+2);
  389. Variant **argptrs = call_args;
  390. for(int i=0;i<argc;i++) {
  391. GET_VARIANT_PTR(v,3+i);
  392. argptrs[i]=v;
  393. }
  394. GET_VARIANT_PTR(dst,3+argc);
  395. Variant::CallError err;
  396. *dst = Variant::construct(t,(const Variant**)argptrs,argc,err);
  397. if (err.error!=Variant::CallError::CALL_OK) {
  398. err_text=_get_call_error(err,"'"+Variant::get_type_name(t)+"' constructor",(const Variant**)argptrs);
  399. break;
  400. }
  401. ip+=4+argc;
  402. //construct a basic type
  403. } continue;
  404. case OPCODE_CONSTRUCT_ARRAY: {
  405. CHECK_SPACE(1);
  406. int argc=_code_ptr[ip+1];
  407. Array array(true); //arrays are always shared
  408. array.resize(argc);
  409. CHECK_SPACE(argc+2);
  410. for(int i=0;i<argc;i++) {
  411. GET_VARIANT_PTR(v,2+i);
  412. array[i]=*v;
  413. }
  414. GET_VARIANT_PTR(dst,2+argc);
  415. *dst=array;
  416. ip+=3+argc;
  417. } continue;
  418. case OPCODE_CONSTRUCT_DICTIONARY: {
  419. CHECK_SPACE(1);
  420. int argc=_code_ptr[ip+1];
  421. Dictionary dict(true); //arrays are always shared
  422. CHECK_SPACE(argc*2+2);
  423. for(int i=0;i<argc;i++) {
  424. GET_VARIANT_PTR(k,2+i*2+0);
  425. GET_VARIANT_PTR(v,2+i*2+1);
  426. dict[*k]=*v;
  427. }
  428. GET_VARIANT_PTR(dst,2+argc*2);
  429. *dst=dict;
  430. ip+=3+argc*2;
  431. } continue;
  432. case OPCODE_CALL_RETURN:
  433. case OPCODE_CALL: {
  434. CHECK_SPACE(4);
  435. bool call_ret = _code_ptr[ip]==OPCODE_CALL_RETURN;
  436. int argc=_code_ptr[ip+1];
  437. GET_VARIANT_PTR(base,2);
  438. int nameg=_code_ptr[ip+3];
  439. ERR_BREAK(nameg<0 || nameg>=_global_names_count);
  440. const StringName *methodname = &_global_names_ptr[nameg];
  441. ERR_BREAK(argc<0);
  442. ip+=4;
  443. CHECK_SPACE(argc+1);
  444. Variant **argptrs = call_args;
  445. for(int i=0;i<argc;i++) {
  446. GET_VARIANT_PTR(v,i);
  447. argptrs[i]=v;
  448. }
  449. Variant::CallError err;
  450. if (call_ret) {
  451. GET_VARIANT_PTR(ret,argc);
  452. *ret = base->call(*methodname,(const Variant**)argptrs,argc,err);
  453. } else {
  454. base->call(*methodname,(const Variant**)argptrs,argc,err);
  455. }
  456. if (err.error!=Variant::CallError::CALL_OK) {
  457. String methodstr = *methodname;
  458. String basestr = _get_var_type(base);
  459. if (methodstr=="call") {
  460. if (argc>=1) {
  461. methodstr=String(*argptrs[0])+" (via call)";
  462. if (err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  463. err.argument-=1;
  464. }
  465. }
  466. } if (methodstr=="free") {
  467. if (err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  468. if (base->is_ref()) {
  469. err_text="Attempted to free a reference.";
  470. break;
  471. } else if (base->get_type()==Variant::OBJECT) {
  472. err_text="Attempted to free a locked object (calling or emitting).";
  473. break;
  474. }
  475. }
  476. }
  477. err_text=_get_call_error(err,"function '"+methodstr+"' in base '"+basestr+"'",(const Variant**)argptrs);
  478. break;
  479. }
  480. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  481. ip+=argc+1;
  482. } continue;
  483. case OPCODE_CALL_BUILT_IN: {
  484. CHECK_SPACE(4);
  485. GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip+1]);
  486. int argc=_code_ptr[ip+2];
  487. ERR_BREAK(argc<0);
  488. ip+=3;
  489. CHECK_SPACE(argc+1);
  490. Variant **argptrs = call_args;
  491. for(int i=0;i<argc;i++) {
  492. GET_VARIANT_PTR(v,i);
  493. argptrs[i]=v;
  494. }
  495. GET_VARIANT_PTR(dst,argc);
  496. Variant::CallError err;
  497. GDFunctions::call(func,(const Variant**)argptrs,argc,*dst,err);
  498. if (err.error!=Variant::CallError::CALL_OK) {
  499. String methodstr = GDFunctions::get_func_name(func);
  500. err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
  501. break;
  502. }
  503. ip+=argc+1;
  504. } continue;
  505. case OPCODE_CALL_SELF: {
  506. } break;
  507. case OPCODE_CALL_SELF_BASE: {
  508. CHECK_SPACE(2);
  509. int self_fun = _code_ptr[ip+1];
  510. #ifdef DEBUG_ENABLED
  511. if (self_fun<0 || self_fun>=_global_names_count) {
  512. err_text="compiler bug, function name not found";
  513. break;
  514. }
  515. #endif
  516. const StringName *methodname = &_global_names_ptr[self_fun];
  517. int argc=_code_ptr[ip+2];
  518. CHECK_SPACE(2+argc+1);
  519. Variant **argptrs = call_args;
  520. for(int i=0;i<argc;i++) {
  521. GET_VARIANT_PTR(v,i+3);
  522. argptrs[i]=v;
  523. }
  524. GET_VARIANT_PTR(dst,argc+3);
  525. const GDScript *gds = _script;
  526. const Map<StringName,GDFunction>::Element *E=NULL;
  527. while (gds->base.ptr()) {
  528. gds=gds->base.ptr();
  529. E=gds->member_functions.find(*methodname);
  530. if (E)
  531. break;
  532. }
  533. Variant::CallError err;
  534. if (E) {
  535. *dst=((GDFunction*)&E->get())->call(p_instance,(const Variant**)argptrs,argc,err);
  536. } else if (gds->native.ptr()) {
  537. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  538. MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(),*methodname);
  539. if (!mb) {
  540. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  541. } else {
  542. *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
  543. }
  544. } else {
  545. err.error=Variant::CallError::CALL_OK;
  546. }
  547. } else {
  548. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  549. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  550. } else {
  551. err.error=Variant::CallError::CALL_OK;
  552. }
  553. }
  554. if (err.error!=Variant::CallError::CALL_OK) {
  555. String methodstr = *methodname;
  556. err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
  557. break;
  558. }
  559. ip+=4+argc;
  560. } continue;
  561. case OPCODE_JUMP: {
  562. CHECK_SPACE(2);
  563. int to = _code_ptr[ip+1];
  564. ERR_BREAK(to<0 || to>_code_size);
  565. ip=to;
  566. } continue;
  567. case OPCODE_JUMP_IF: {
  568. CHECK_SPACE(3);
  569. GET_VARIANT_PTR(test,1);
  570. bool valid;
  571. bool result = test->booleanize(valid);
  572. #ifdef DEBUG_ENABLED
  573. if (!valid) {
  574. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  575. break;
  576. }
  577. #endif
  578. if (result) {
  579. int to = _code_ptr[ip+2];
  580. ERR_BREAK(to<0 || to>_code_size);
  581. ip=to;
  582. continue;
  583. }
  584. ip+=3;
  585. } continue;
  586. case OPCODE_JUMP_IF_NOT: {
  587. CHECK_SPACE(3);
  588. GET_VARIANT_PTR(test,1);
  589. bool valid;
  590. bool result = test->booleanize(valid);
  591. #ifdef DEBUG_ENABLED
  592. if (!valid) {
  593. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  594. break;
  595. }
  596. #endif
  597. if (!result) {
  598. int to = _code_ptr[ip+2];
  599. ERR_BREAK(to<0 || to>_code_size);
  600. ip=to;
  601. continue;
  602. }
  603. ip+=3;
  604. } continue;
  605. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  606. CHECK_SPACE(2);
  607. ip=_default_arg_ptr[defarg];
  608. } continue;
  609. case OPCODE_RETURN: {
  610. CHECK_SPACE(2);
  611. GET_VARIANT_PTR(r,1);
  612. retvalue=*r;
  613. exit_ok=true;
  614. } break;
  615. case OPCODE_ITERATE_BEGIN: {
  616. CHECK_SPACE(8); //space for this an regular iterate
  617. GET_VARIANT_PTR(counter,1);
  618. GET_VARIANT_PTR(container,2);
  619. bool valid;
  620. if (!container->iter_init(*counter,valid)) {
  621. if (!valid) {
  622. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
  623. break;
  624. }
  625. int jumpto=_code_ptr[ip+3];
  626. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  627. ip=jumpto;
  628. continue;
  629. }
  630. GET_VARIANT_PTR(iterator,4);
  631. *iterator=container->iter_get(*counter,valid);
  632. if (!valid) {
  633. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
  634. break;
  635. }
  636. ip+=5; //skip regular iterate which is always next
  637. } continue;
  638. case OPCODE_ITERATE: {
  639. CHECK_SPACE(4);
  640. GET_VARIANT_PTR(counter,1);
  641. GET_VARIANT_PTR(container,2);
  642. bool valid;
  643. if (!container->iter_next(*counter,valid)) {
  644. if (!valid) {
  645. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
  646. break;
  647. }
  648. int jumpto=_code_ptr[ip+3];
  649. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  650. ip=jumpto;
  651. continue;
  652. }
  653. GET_VARIANT_PTR(iterator,4);
  654. *iterator=container->iter_get(*counter,valid);
  655. if (!valid) {
  656. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
  657. break;
  658. }
  659. ip+=5; //loop again
  660. } continue;
  661. case OPCODE_ASSERT: {
  662. CHECK_SPACE(2);
  663. GET_VARIANT_PTR(test,1);
  664. #ifdef DEBUG_ENABLED
  665. bool valid;
  666. bool result = test->booleanize(valid);
  667. if (!valid) {
  668. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  669. break;
  670. }
  671. if (!result) {
  672. err_text="Assertion failed.";
  673. break;
  674. }
  675. #endif
  676. ip+=2;
  677. } continue;
  678. case OPCODE_LINE: {
  679. CHECK_SPACE(2);
  680. line=_code_ptr[ip+1];
  681. ip+=2;
  682. if (ScriptDebugger::get_singleton()) {
  683. // line
  684. bool do_break=false;
  685. if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
  686. if (ScriptDebugger::get_singleton()->get_depth()<=0)
  687. ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
  688. if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
  689. do_break=true;
  690. }
  691. if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
  692. do_break=true;
  693. if (do_break) {
  694. GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
  695. }
  696. ScriptDebugger::get_singleton()->line_poll();
  697. }
  698. } continue;
  699. case OPCODE_END: {
  700. exit_ok=true;
  701. break;
  702. } break;
  703. default: {
  704. err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
  705. } break;
  706. }
  707. if (exit_ok)
  708. break;
  709. //error
  710. // function, file, line, error, explanation
  711. String err_file;
  712. if (p_instance)
  713. err_file=p_instance->script->path;
  714. else if (_class)
  715. err_file=_class->path;
  716. if (err_file=="")
  717. err_file="<built-in>";
  718. String err_func = name;
  719. if (p_instance && p_instance->script->name!="")
  720. err_func=p_instance->script->name+"."+err_func;
  721. int err_line=line;
  722. if (err_text=="") {
  723. err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
  724. }
  725. if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  726. // debugger break did not happen
  727. _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data());
  728. }
  729. break;
  730. }
  731. if (ScriptDebugger::get_singleton())
  732. GDScriptLanguage::get_singleton()->exit_function();
  733. if (_stack_size) {
  734. //free stack
  735. for(int i=0;i<_stack_size;i++)
  736. stack[i].~Variant();
  737. }
  738. return retvalue;
  739. }
  740. const int* GDFunction::get_code() const {
  741. return _code_ptr;
  742. }
  743. int GDFunction::get_code_size() const{
  744. return _code_size;
  745. }
  746. Variant GDFunction::get_constant(int p_idx) const {
  747. ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
  748. return constants[p_idx];
  749. }
  750. StringName GDFunction::get_global_name(int p_idx) const {
  751. ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
  752. return global_names[p_idx];
  753. }
  754. int GDFunction::get_default_argument_count() const {
  755. return default_arguments.size();
  756. }
  757. int GDFunction::get_default_argument_addr(int p_arg) const{
  758. ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
  759. return default_arguments[p_arg];
  760. }
  761. StringName GDFunction::get_name() const {
  762. return name;
  763. }
  764. int GDFunction::get_max_stack_size() const {
  765. return _stack_size;
  766. }
  767. struct _GDFKC {
  768. int order;
  769. List<int> pos;
  770. };
  771. struct _GDFKCS {
  772. int order;
  773. StringName id;
  774. int pos;
  775. bool operator<(const _GDFKCS &p_r) const {
  776. return order<p_r.order;
  777. }
  778. };
  779. void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
  780. int oc=0;
  781. Map<StringName,_GDFKC> sdmap;
  782. for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
  783. const StackDebug &sd=E->get();
  784. if (sd.line>p_line)
  785. break;
  786. if (sd.added) {
  787. if (!sdmap.has(sd.identifier)) {
  788. _GDFKC d;
  789. d.order=oc++;
  790. d.pos.push_back(sd.pos);
  791. sdmap[sd.identifier]=d;
  792. } else {
  793. sdmap[sd.identifier].pos.push_back(sd.pos);
  794. }
  795. } else {
  796. ERR_CONTINUE(!sdmap.has(sd.identifier));
  797. sdmap[sd.identifier].pos.pop_back();
  798. if (sdmap[sd.identifier].pos.empty())
  799. sdmap.erase(sd.identifier);
  800. }
  801. }
  802. List<_GDFKCS> stackpositions;
  803. for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
  804. _GDFKCS spp;
  805. spp.id=E->key();
  806. spp.order=E->get().order;
  807. spp.pos=E->get().pos.back()->get();
  808. stackpositions.push_back(spp);
  809. }
  810. stackpositions.sort();
  811. for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
  812. Pair<StringName,int> p;
  813. p.first=E->get().id;
  814. p.second=E->get().pos;
  815. r_stackvars->push_back(p);
  816. }
  817. }
  818. #if 0
  819. void GDFunction::clear() {
  820. name=StringName();
  821. constants.clear();
  822. _stack_size=0;
  823. code.clear();
  824. _constants_ptr=NULL;
  825. _constant_count=0;
  826. _global_names_ptr=NULL;
  827. _global_names_count=0;
  828. _code_ptr=NULL;
  829. _code_size=0;
  830. }
  831. #endif
  832. GDFunction::GDFunction() {
  833. _stack_size=0;
  834. _call_size=0;
  835. name="<anonymous>";
  836. }
  837. GDNativeClass::GDNativeClass(const StringName& p_name) {
  838. name=p_name;
  839. }
  840. /*void GDNativeClass::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount){
  841. }*/
  842. bool GDNativeClass::_get(const StringName& p_name,Variant &r_ret) const {
  843. bool ok;
  844. int v = ObjectTypeDB::get_integer_constant(name, p_name, &ok);
  845. if (ok) {
  846. r_ret=v;
  847. return true;
  848. } else {
  849. return false;
  850. }
  851. }
  852. void GDNativeClass::_bind_methods() {
  853. ObjectTypeDB::bind_method(_MD("new"),&GDNativeClass::_new);
  854. }
  855. Variant GDNativeClass::_new() {
  856. Object *o = instance();
  857. if (!o) {
  858. ERR_EXPLAIN("Class type: '"+String(name)+"' is not instantiable.");
  859. ERR_FAIL_COND_V(!o,Variant());
  860. }
  861. Reference *ref = o->cast_to<Reference>();
  862. if (ref) {
  863. return REF(ref);
  864. } else {
  865. return o;
  866. }
  867. }
  868. Object *GDNativeClass::instance() {
  869. return ObjectTypeDB::instance(name);
  870. }
  871. GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref) {
  872. /* STEP 1, CREATE */
  873. GDInstance* instance = memnew( GDInstance );
  874. instance->base_ref=p_isref;
  875. instance->members.resize(member_indices.size());
  876. instance->script=Ref<GDScript>(this);
  877. instance->owner=p_owner;
  878. instance->owner->set_script_instance(instance);
  879. /* STEP 2, INITIALIZE AND CONSRTUCT */
  880. instances.insert(instance->owner);
  881. Variant::CallError err;
  882. initializer->call(instance,p_args,p_argcount,err);
  883. if (err.error!=Variant::CallError::CALL_OK) {
  884. instance->script=Ref<GDScript>();
  885. instances.erase(p_owner);
  886. memdelete(instance);
  887. ERR_FAIL_COND_V(err.error!=Variant::CallError::CALL_OK, NULL); //error consrtucting
  888. }
  889. //@TODO make thread safe
  890. return instance;
  891. }
  892. Variant GDScript::_new(const Variant** p_args,int p_argcount,Variant::CallError& r_error) {
  893. /* STEP 1, CREATE */
  894. r_error.error=Variant::CallError::CALL_OK;
  895. REF ref;
  896. Object *owner=NULL;
  897. GDScript *_baseptr=this;
  898. while (_baseptr->_base) {
  899. _baseptr=_baseptr->_base;
  900. }
  901. if (_baseptr->native.ptr()) {
  902. owner=_baseptr->native->instance();
  903. } else {
  904. owner=memnew( Reference ); //by default, no base means use reference
  905. }
  906. Reference *r=owner->cast_to<Reference>();
  907. if (r) {
  908. ref=REF(r);
  909. }
  910. GDInstance* instance = _create_instance(p_args,p_argcount,owner,r!=NULL);
  911. if (!instance) {
  912. if (ref.is_null()) {
  913. memdelete(owner); //no owner, sorry
  914. }
  915. return Variant();
  916. }
  917. if (ref.is_valid()) {
  918. return ref;
  919. } else {
  920. return owner;
  921. }
  922. }
  923. bool GDScript::can_instance() const {
  924. return valid; //any script in GDscript can instance
  925. }
  926. StringName GDScript::get_instance_base_type() const {
  927. if (native.is_valid())
  928. return native->get_name();
  929. if (base.is_valid())
  930. return base->get_instance_base_type();
  931. return StringName();
  932. }
  933. struct _GDScriptMemberSort {
  934. int index;
  935. StringName name;
  936. _FORCE_INLINE_ bool operator<(const _GDScriptMemberSort& p_member) const { return index < p_member.index; }
  937. };
  938. #ifdef TOOLS_ENABLED
  939. void GDScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  940. placeholders.erase(p_placeholder);
  941. }
  942. void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  943. List<PropertyInfo> plist;
  944. GDScript *scr=this;
  945. Map<StringName,Variant> default_values;
  946. while(scr) {
  947. Vector<_GDScriptMemberSort> msort;
  948. for(Map<StringName,PropertyInfo>::Element *E=scr->member_info.front();E;E=E->next()) {
  949. _GDScriptMemberSort ms;
  950. ERR_CONTINUE(!scr->member_indices.has(E->key()));
  951. ms.index=scr->member_indices[E->key()];
  952. ms.name=E->key();
  953. msort.push_back(ms);
  954. }
  955. msort.sort();
  956. msort.invert();
  957. for(int i=0;i<msort.size();i++) {
  958. plist.push_front(scr->member_info[msort[i].name]);
  959. if (scr->member_default_values.has(msort[i].name))
  960. default_values[msort[i].name]=scr->member_default_values[msort[i].name];
  961. else {
  962. Variant::CallError err;
  963. default_values[msort[i].name]=Variant::construct(scr->member_info[msort[i].name].type,NULL,0,err);
  964. }
  965. }
  966. scr=scr->_base;
  967. }
  968. p_placeholder->update(plist,default_values);
  969. }
  970. #endif
  971. ScriptInstance* GDScript::instance_create(Object *p_this) {
  972. if (!tool && !ScriptServer::is_scripting_enabled()) {
  973. #ifdef TOOLS_ENABLED
  974. //instance a fake script for editing the values
  975. //plist.invert();
  976. /*print_line("CREATING PLACEHOLDER");
  977. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  978. print_line(E->get().name);
  979. }*/
  980. PlaceHolderScriptInstance *si = memnew( PlaceHolderScriptInstance(GDScriptLanguage::get_singleton(),Ref<Script>(this),p_this) );
  981. placeholders.insert(si);
  982. _update_placeholder(si);
  983. return si;
  984. #else
  985. return NULL;
  986. #endif
  987. }
  988. GDScript *top=this;
  989. while(top->_base)
  990. top=top->_base;
  991. if (top->native.is_valid()) {
  992. if (!ObjectTypeDB::is_type(p_this->get_type_name(),top->native->get_name())) {
  993. if (ScriptDebugger::get_singleton()) {
  994. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),0,"Script inherits from native type '"+String(top->native->get_name())+"', so it can't be instanced in object of type: '"+p_this->get_type()+"'");
  995. }
  996. ERR_EXPLAIN("Script inherits from native type '"+String(top->native->get_name())+"', so it can't be instanced in object of type: '"+p_this->get_type()+"'");
  997. ERR_FAIL_V(NULL);
  998. }
  999. }
  1000. return _create_instance(NULL,0,p_this,p_this->cast_to<Reference>());
  1001. }
  1002. bool GDScript::instance_has(const Object *p_this) const {
  1003. return instances.has((Object*)p_this);
  1004. }
  1005. bool GDScript::has_source_code() const {
  1006. return source!="";
  1007. }
  1008. String GDScript::get_source_code() const {
  1009. return source;
  1010. }
  1011. void GDScript::set_source_code(const String& p_code) {
  1012. source=p_code;
  1013. }
  1014. void GDScript::_set_subclass_path(Ref<GDScript>& p_sc,const String& p_path) {
  1015. p_sc->path=p_path;
  1016. for(Map<StringName,Ref<GDScript> >::Element *E=p_sc->subclasses.front();E;E=E->next()) {
  1017. _set_subclass_path(E->get(),p_path);
  1018. }
  1019. }
  1020. Error GDScript::reload() {
  1021. ERR_FAIL_COND_V(instances.size(),ERR_ALREADY_IN_USE);
  1022. String basedir=path;
  1023. if (basedir=="")
  1024. basedir=get_path();
  1025. if (basedir!="")
  1026. basedir=basedir.get_base_dir();
  1027. valid=false;
  1028. GDParser parser;
  1029. Error err = parser.parse(source,basedir);
  1030. if (err) {
  1031. if (ScriptDebugger::get_singleton()) {
  1032. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),parser.get_error_line(),"Parser Error: "+parser.get_error());
  1033. }
  1034. _err_print_error("GDScript::reload",path.empty()?"built-in":(const char*)path.utf8().get_data(),parser.get_error_line(),("Parse Error: "+parser.get_error()).utf8().get_data());
  1035. ERR_FAIL_V(ERR_PARSE_ERROR);
  1036. }
  1037. GDCompiler compiler;
  1038. err = compiler.compile(&parser,this);
  1039. if (err) {
  1040. if (ScriptDebugger::get_singleton()) {
  1041. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),compiler.get_error_line(),"Parser Error: "+compiler.get_error());
  1042. }
  1043. _err_print_error("GDScript::reload",path.empty()?"built-in":(const char*)path.utf8().get_data(),compiler.get_error_line(),("Compile Error: "+compiler.get_error()).utf8().get_data());
  1044. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1045. }
  1046. valid=true;
  1047. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1048. _set_subclass_path(E->get(),path);
  1049. }
  1050. #ifdef TOOLS_ENABLED
  1051. for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  1052. _update_placeholder(E->get());
  1053. }
  1054. #endif
  1055. return OK;
  1056. }
  1057. String GDScript::get_node_type() const {
  1058. return ""; // ?
  1059. }
  1060. ScriptLanguage *GDScript::get_language() const {
  1061. return GDScriptLanguage::get_singleton();
  1062. }
  1063. Variant GDScript::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1064. GDScript *top=this;
  1065. while(top) {
  1066. Map<StringName,GDFunction>::Element *E=top->member_functions.find(p_method);
  1067. if (E) {
  1068. if (!E->get().is_static()) {
  1069. WARN_PRINT(String("Can't call non-static function: '"+String(p_method)+"' in script.").utf8().get_data());
  1070. }
  1071. return E->get().call(NULL,p_args,p_argcount,r_error);
  1072. }
  1073. top=top->_base;
  1074. }
  1075. //none found, regular
  1076. return Script::call(p_method,p_args,p_argcount,r_error);
  1077. }
  1078. bool GDScript::_get(const StringName& p_name,Variant &r_ret) const {
  1079. {
  1080. const GDScript *top=this;
  1081. while(top) {
  1082. {
  1083. const Map<StringName,Variant>::Element *E=top->constants.find(p_name);
  1084. if (E) {
  1085. r_ret= E->get();
  1086. return true;
  1087. }
  1088. }
  1089. {
  1090. const Map<StringName,Ref<GDScript> >::Element *E=subclasses.find(p_name);
  1091. if (E) {
  1092. r_ret=E->get();
  1093. return true;
  1094. }
  1095. }
  1096. top=top->_base;
  1097. }
  1098. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1099. r_ret=get_source_code();
  1100. return true;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. bool GDScript::_set(const StringName& p_name, const Variant& p_value) {
  1106. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1107. set_source_code(p_value);
  1108. reload();
  1109. } else
  1110. return false;
  1111. return true;
  1112. }
  1113. void GDScript::_get_property_list(List<PropertyInfo> *p_properties) const {
  1114. p_properties->push_back( PropertyInfo(Variant::STRING,"script/source",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  1115. }
  1116. void GDScript::_bind_methods() {
  1117. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"new",&GDScript::_new,MethodInfo("new"));
  1118. }
  1119. Error GDScript::load_byte_code(const String& p_path) {
  1120. Vector<uint8_t> bytecode = FileAccess::get_file_as_array(p_path);
  1121. ERR_FAIL_COND_V(bytecode.size()==0,ERR_PARSE_ERROR);
  1122. path=p_path;
  1123. String basedir=path;
  1124. if (basedir=="")
  1125. basedir=get_path();
  1126. if (basedir!="")
  1127. basedir=basedir.get_base_dir();
  1128. valid=false;
  1129. GDParser parser;
  1130. Error err = parser.parse_bytecode(bytecode,basedir);
  1131. if (err) {
  1132. _err_print_error("GDScript::load_byte_code",path.empty()?"built-in":(const char*)path.utf8().get_data(),parser.get_error_line(),("Parse Error: "+parser.get_error()).utf8().get_data());
  1133. ERR_FAIL_V(ERR_PARSE_ERROR);
  1134. }
  1135. GDCompiler compiler;
  1136. err = compiler.compile(&parser,this);
  1137. if (err) {
  1138. _err_print_error("GDScript::load_byte_code",path.empty()?"built-in":(const char*)path.utf8().get_data(),compiler.get_error_line(),("Compile Error: "+compiler.get_error()).utf8().get_data());
  1139. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1140. }
  1141. valid=true;
  1142. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1143. _set_subclass_path(E->get(),path);
  1144. }
  1145. return OK;
  1146. }
  1147. Error GDScript::load_source_code(const String& p_path) {
  1148. DVector<uint8_t> sourcef;
  1149. Error err;
  1150. FileAccess *f=FileAccess::open(p_path,FileAccess::READ,&err);
  1151. if (err) {
  1152. ERR_FAIL_COND_V(err,err);
  1153. }
  1154. int len = f->get_len();
  1155. sourcef.resize(len+1);
  1156. DVector<uint8_t>::Write w = sourcef.write();
  1157. int r = f->get_buffer(w.ptr(),len);
  1158. f->close();
  1159. memdelete(f);
  1160. ERR_FAIL_COND_V(r!=len,ERR_CANT_OPEN);
  1161. w[len]=0;
  1162. String s;
  1163. if (s.parse_utf8((const char*)w.ptr())) {
  1164. ERR_EXPLAIN("Script '"+p_path+"' contains invalid unicode (utf-8), so it was not loaded. Please ensure that scripts are saved in valid utf-8 unicode.");
  1165. ERR_FAIL_V(ERR_INVALID_DATA);
  1166. }
  1167. source=s;
  1168. path=p_path;
  1169. return OK;
  1170. }
  1171. const Map<StringName,GDFunction>& GDScript::debug_get_member_functions() const {
  1172. return member_functions;
  1173. }
  1174. StringName GDScript::debug_get_member_by_index(int p_idx) const {
  1175. for(const Map<StringName,int>::Element *E=member_indices.front();E;E=E->next()) {
  1176. if (E->get()==p_idx)
  1177. return E->key();
  1178. }
  1179. return "<error>";
  1180. }
  1181. Ref<GDScript> GDScript::get_base() const {
  1182. return base;
  1183. }
  1184. GDScript::GDScript() {
  1185. _static_ref=this;
  1186. valid=false;
  1187. subclass_count=0;
  1188. initializer=NULL;
  1189. _base=NULL;
  1190. _owner=NULL;
  1191. tool=false;
  1192. }
  1193. //////////////////////////////
  1194. // INSTANCE //
  1195. //////////////////////////////
  1196. bool GDInstance::set(const StringName& p_name, const Variant& p_value) {
  1197. //member
  1198. {
  1199. const Map<StringName,int>::Element *E = script->member_indices.find(p_name);
  1200. if (E) {
  1201. members[E->get()]=p_value;
  1202. return true;
  1203. }
  1204. }
  1205. GDScript *sptr=script.ptr();
  1206. while(sptr) {
  1207. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
  1208. if (E) {
  1209. Variant name=p_name;
  1210. const Variant *args[2]={&name,&p_value};
  1211. Variant::CallError err;
  1212. Variant ret = E->get().call(this,(const Variant**)args,2,err);
  1213. if (err.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::BOOL && ret.operator bool())
  1214. return true;
  1215. }
  1216. sptr = sptr->_base;
  1217. }
  1218. return false;
  1219. }
  1220. bool GDInstance::get(const StringName& p_name, Variant &r_ret) const {
  1221. const GDScript *sptr=script.ptr();
  1222. while(sptr) {
  1223. {
  1224. const Map<StringName,int>::Element *E = script->member_indices.find(p_name);
  1225. if (E) {
  1226. r_ret=members[E->get()];
  1227. return true; //index found
  1228. }
  1229. }
  1230. {
  1231. const Map<StringName,Variant>::Element *E = script->constants.find(p_name);
  1232. if (E) {
  1233. r_ret=E->get();
  1234. return true; //index found
  1235. }
  1236. }
  1237. {
  1238. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
  1239. if (E) {
  1240. Variant name=p_name;
  1241. const Variant *args[1]={&name};
  1242. Variant::CallError err;
  1243. Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
  1244. if (err.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  1245. r_ret=ret;
  1246. return true;
  1247. }
  1248. }
  1249. }
  1250. sptr = sptr->_base;
  1251. }
  1252. return false;
  1253. }
  1254. void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  1255. // exported members, not doen yet!
  1256. const GDScript *sptr=script.ptr();
  1257. List<PropertyInfo> props;
  1258. while(sptr) {
  1259. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
  1260. if (E) {
  1261. Variant::CallError err;
  1262. Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
  1263. if (err.error==Variant::CallError::CALL_OK) {
  1264. if (ret.get_type()!=Variant::ARRAY) {
  1265. ERR_EXPLAIN("Wrong type for _get_property list, must be an array of dictionaries.");
  1266. ERR_FAIL();
  1267. }
  1268. Array arr = ret;
  1269. for(int i=0;i<arr.size();i++) {
  1270. Dictionary d = arr[i];
  1271. ERR_CONTINUE(!d.has("name"));
  1272. ERR_CONTINUE(!d.has("type"));
  1273. PropertyInfo pinfo;
  1274. pinfo.type = Variant::Type( d["type"].operator int());
  1275. ERR_CONTINUE(pinfo.type<0 || pinfo.type>=Variant::VARIANT_MAX );
  1276. pinfo.name = d["name"];
  1277. ERR_CONTINUE(pinfo.name=="");
  1278. if (d.has("hint"))
  1279. pinfo.hint=PropertyHint(d["hint"].operator int());
  1280. if (d.has("hint_string"))
  1281. pinfo.hint_string=d["hint_string"];
  1282. if (d.has("usage"))
  1283. pinfo.usage=d["usage"];
  1284. props.push_back(pinfo);
  1285. }
  1286. }
  1287. }
  1288. //instance a fake script for editing the values
  1289. Vector<_GDScriptMemberSort> msort;
  1290. for(Map<StringName,PropertyInfo>::Element *E=sptr->member_info.front();E;E=E->next()) {
  1291. _GDScriptMemberSort ms;
  1292. ERR_CONTINUE(!sptr->member_indices.has(E->key()));
  1293. ms.index=sptr->member_indices[E->key()];
  1294. ms.name=E->key();
  1295. msort.push_back(ms);
  1296. }
  1297. msort.sort();
  1298. msort.invert();
  1299. for(int i=0;i<msort.size();i++) {
  1300. props.push_front(sptr->member_info[msort[i].name]);
  1301. }
  1302. #if 0
  1303. if (sptr->member_functions.has("_get_property_list")) {
  1304. Variant::CallError err;
  1305. GDFunction *f = const_cast<GDFunction*>(&sptr->member_functions["_get_property_list"]);
  1306. Variant plv = f->call(const_cast<GDInstance*>(this),NULL,0,err);
  1307. if (plv.get_type()!=Variant::ARRAY) {
  1308. ERR_PRINT("_get_property_list: expected array returned");
  1309. } else {
  1310. Array pl=plv;
  1311. for(int i=0;i<pl.size();i++) {
  1312. Dictionary p = pl[i];
  1313. PropertyInfo pinfo;
  1314. if (!p.has("name")) {
  1315. ERR_PRINT("_get_property_list: expected 'name' key of type string.")
  1316. continue;
  1317. }
  1318. if (!p.has("type")) {
  1319. ERR_PRINT("_get_property_list: expected 'type' key of type integer.")
  1320. continue;
  1321. }
  1322. pinfo.name=p["name"];
  1323. pinfo.type=Variant::Type(int(p["type"]));
  1324. if (p.has("hint"))
  1325. pinfo.hint=PropertyHint(int(p["hint"]));
  1326. if (p.has("hint_string"))
  1327. pinfo.hint_string=p["hint_string"];
  1328. if (p.has("usage"))
  1329. pinfo.usage=p["usage"];
  1330. props.push_back(pinfo);
  1331. }
  1332. }
  1333. }
  1334. #endif
  1335. sptr = sptr->_base;
  1336. }
  1337. //props.invert();
  1338. for (List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  1339. p_properties->push_back(E->get());
  1340. }
  1341. }
  1342. void GDInstance::get_method_list(List<MethodInfo> *p_list) const {
  1343. const GDScript *sptr=script.ptr();
  1344. while(sptr) {
  1345. for (Map<StringName,GDFunction>::Element *E = sptr->member_functions.front();E;E=E->next()) {
  1346. MethodInfo mi;
  1347. mi.name=E->key();
  1348. for(int i=0;i<E->get().get_argument_count();i++)
  1349. mi.arguments.push_back(PropertyInfo(Variant::NIL,"arg"+itos(i)));
  1350. p_list->push_back(mi);
  1351. }
  1352. sptr = sptr->_base;
  1353. }
  1354. }
  1355. bool GDInstance::has_method(const StringName& p_method) const {
  1356. const GDScript *sptr=script.ptr();
  1357. while(sptr) {
  1358. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1359. if (E)
  1360. return true;
  1361. sptr = sptr->_base;
  1362. }
  1363. return false;
  1364. }
  1365. Variant GDInstance::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1366. //printf("calling %ls:%i method %ls\n", script->get_path().c_str(), -1, String(p_method).c_str());
  1367. GDScript *sptr=script.ptr();
  1368. while(sptr) {
  1369. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1370. if (E) {
  1371. return E->get().call(this,p_args,p_argcount,r_error);
  1372. }
  1373. sptr = sptr->_base;
  1374. }
  1375. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1376. return Variant();
  1377. }
  1378. void GDInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1379. GDScript *sptr=script.ptr();
  1380. Variant::CallError ce;
  1381. while(sptr) {
  1382. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1383. if (E) {
  1384. E->get().call(this,p_args,p_argcount,ce);
  1385. }
  1386. sptr = sptr->_base;
  1387. }
  1388. }
  1389. void GDInstance::_ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount) {
  1390. if (sptr->_base)
  1391. _ml_call_reversed(sptr->_base,p_method,p_args,p_argcount);
  1392. Variant::CallError ce;
  1393. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1394. if (E) {
  1395. E->get().call(this,p_args,p_argcount,ce);
  1396. }
  1397. }
  1398. void GDInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1399. if (script.ptr()) {
  1400. _ml_call_reversed(script.ptr(),p_method,p_args,p_argcount);
  1401. }
  1402. }
  1403. void GDInstance::notification(int p_notification) {
  1404. //notification is not virutal, it gets called at ALL levels just like in C.
  1405. Variant value=p_notification;
  1406. const Variant *args[1]={&value };
  1407. GDScript *sptr=script.ptr();
  1408. while(sptr) {
  1409. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
  1410. if (E) {
  1411. Variant::CallError err;
  1412. E->get().call(this,args,1,err);
  1413. if (err.error!=Variant::CallError::CALL_OK) {
  1414. //print error about notification call
  1415. }
  1416. }
  1417. sptr = sptr->_base;
  1418. }
  1419. }
  1420. Ref<Script> GDInstance::get_script() const {
  1421. return script;
  1422. }
  1423. ScriptLanguage *GDInstance::get_language() {
  1424. return GDScriptLanguage::get_singleton();
  1425. }
  1426. GDInstance::GDInstance() {
  1427. owner=NULL;
  1428. base_ref=false;
  1429. }
  1430. GDInstance::~GDInstance() {
  1431. if (script.is_valid() && owner) {
  1432. script->instances.erase(owner);
  1433. }
  1434. }
  1435. /************* SCRIPT LANGUAGE **************/
  1436. /************* SCRIPT LANGUAGE **************/
  1437. /************* SCRIPT LANGUAGE **************/
  1438. /************* SCRIPT LANGUAGE **************/
  1439. /************* SCRIPT LANGUAGE **************/
  1440. GDScriptLanguage *GDScriptLanguage::singleton=NULL;
  1441. String GDScriptLanguage::get_name() const {
  1442. return "GDScript";
  1443. }
  1444. /* LANGUAGE FUNCTIONS */
  1445. void GDScriptLanguage::_add_global(const StringName& p_name,const Variant& p_value) {
  1446. if (globals.has(p_name)) {
  1447. //overwrite existing
  1448. global_array[globals[p_name]]=p_value;
  1449. return;
  1450. }
  1451. globals[p_name]=global_array.size();
  1452. global_array.push_back(p_value);
  1453. _global_array=global_array.ptr();
  1454. }
  1455. void GDScriptLanguage::init() {
  1456. //populate global constants
  1457. int gcc=GlobalConstants::get_global_constant_count();
  1458. for(int i=0;i<gcc;i++) {
  1459. _add_global(StaticCString::create(GlobalConstants::get_global_constant_name(i)),GlobalConstants::get_global_constant_value(i));
  1460. }
  1461. _add_global(StaticCString::create("PI"),Math_PI);
  1462. //populate native classes
  1463. List<String> class_list;
  1464. ObjectTypeDB::get_type_list(&class_list);
  1465. for(List<String>::Element *E=class_list.front();E;E=E->next()) {
  1466. StringName n = E->get();
  1467. String s = String(n);
  1468. if (s.begins_with("_"))
  1469. n=s.substr(1,s.length());
  1470. if (globals.has(n))
  1471. continue;
  1472. Ref<GDNativeClass> nc = memnew( GDNativeClass(E->get()) );
  1473. _add_global(n,nc);
  1474. }
  1475. //populate singletons
  1476. List<Globals::Singleton> singletons;
  1477. Globals::get_singleton()->get_singletons(&singletons);
  1478. for(List<Globals::Singleton>::Element *E=singletons.front();E;E=E->next()) {
  1479. _add_global(E->get().name,E->get().ptr);
  1480. }
  1481. }
  1482. String GDScriptLanguage::get_type() const {
  1483. return "GDScript";
  1484. }
  1485. String GDScriptLanguage::get_extension() const {
  1486. return "gd";
  1487. }
  1488. Error GDScriptLanguage::execute_file(const String& p_path) {
  1489. // ??
  1490. return OK;
  1491. }
  1492. void GDScriptLanguage::finish() {
  1493. }
  1494. void GDScriptLanguage::frame() {
  1495. // print_line("calls: "+itos(calls));
  1496. calls=0;
  1497. }
  1498. /* EDITOR FUNCTIONS */
  1499. void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
  1500. static const char *_reserved_words[]={
  1501. "break",
  1502. "class",
  1503. "continue",
  1504. "const",
  1505. "else",
  1506. "elif",
  1507. "enum",
  1508. "extends" ,
  1509. "for" ,
  1510. "func" ,
  1511. "if" ,
  1512. "in" ,
  1513. "varl",
  1514. "null" ,
  1515. "return" ,
  1516. "self" ,
  1517. "while" ,
  1518. "true" ,
  1519. "false" ,
  1520. "tool",
  1521. "var",
  1522. "pass",
  1523. "and",
  1524. "or",
  1525. "export",
  1526. 0};
  1527. const char **w=_reserved_words;
  1528. while (*w) {
  1529. p_words->push_back(*w);
  1530. w++;
  1531. }
  1532. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1533. p_words->push_back(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1534. }
  1535. }
  1536. GDScriptLanguage::GDScriptLanguage() {
  1537. calls=0;
  1538. ERR_FAIL_COND(singleton);
  1539. singleton=this;
  1540. strings._init = StaticCString::create("_init");
  1541. strings._notification = StaticCString::create("_notification");
  1542. strings._set= StaticCString::create("_set");
  1543. strings._get= StaticCString::create("_get");
  1544. strings._get_property_list= StaticCString::create("_get_property_list");
  1545. strings._script_source=StaticCString::create("script/source");
  1546. _debug_parse_err_line=-1;
  1547. _debug_parse_err_file="";
  1548. _debug_call_stack_pos=0;
  1549. int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
  1550. if (ScriptDebugger::get_singleton()) {
  1551. //debugging enabled!
  1552. _debug_max_call_stack = dmcs;
  1553. if (_debug_max_call_stack<1024)
  1554. _debug_max_call_stack=1024;
  1555. _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
  1556. } else {
  1557. _debug_max_call_stack=0;
  1558. _call_stack=NULL;
  1559. }
  1560. }
  1561. GDScriptLanguage::~GDScriptLanguage() {
  1562. if (_call_stack) {
  1563. memdelete_arr(_call_stack);
  1564. }
  1565. singleton=NULL;
  1566. }
  1567. /*************** RESOURCE ***************/
  1568. RES ResourceFormatLoaderGDScript::load(const String &p_path,const String& p_original_path) {
  1569. GDScript *script = memnew( GDScript );
  1570. Ref<GDScript> scriptres(script);
  1571. if (p_path.ends_with(".gdc")) {
  1572. script->set_script_path(p_original_path); // script needs this.
  1573. script->set_path(p_original_path);
  1574. Error err = script->load_byte_code(p_path);
  1575. if (err!=OK) {
  1576. ERR_FAIL_COND_V(err!=OK, RES());
  1577. }
  1578. } else {
  1579. Error err = script->load_source_code(p_path);
  1580. if (err!=OK) {
  1581. ERR_FAIL_COND_V(err!=OK, RES());
  1582. }
  1583. script->set_script_path(p_original_path); // script needs this.
  1584. script->set_path(p_original_path);
  1585. //script->set_name(p_path.get_file());
  1586. script->reload();
  1587. }
  1588. return scriptres;
  1589. }
  1590. void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
  1591. p_extensions->push_back("gd");
  1592. p_extensions->push_back("gdc");
  1593. }
  1594. bool ResourceFormatLoaderGDScript::handles_type(const String& p_type) const {
  1595. return (p_type=="Script" || p_type=="GDScript");
  1596. }
  1597. String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
  1598. String el = p_path.extension().to_lower();
  1599. if (el=="gd" || el=="gdc")
  1600. return "GDScript";
  1601. return "";
  1602. }
  1603. Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1604. Ref<GDScript> sqscr = p_resource;
  1605. ERR_FAIL_COND_V(sqscr.is_null(),ERR_INVALID_PARAMETER);
  1606. String source = sqscr->get_source_code();
  1607. Error err;
  1608. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  1609. if (err) {
  1610. ERR_FAIL_COND_V(err,err);
  1611. }
  1612. file->store_string(source);
  1613. file->close();
  1614. memdelete(file);
  1615. return OK;
  1616. }
  1617. void ResourceFormatSaverGDScript::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1618. if (p_resource->cast_to<GDScript>()) {
  1619. p_extensions->push_back("gd");
  1620. }
  1621. }
  1622. bool ResourceFormatSaverGDScript::recognize(const RES& p_resource) const {
  1623. return p_resource->cast_to<GDScript>()!=NULL;
  1624. }