gd_script.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  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. #ifdef DEBUG_ENABLED
  837. _func_cname=NULL;
  838. #endif
  839. }
  840. GDNativeClass::GDNativeClass(const StringName& p_name) {
  841. name=p_name;
  842. }
  843. /*void GDNativeClass::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount){
  844. }*/
  845. bool GDNativeClass::_get(const StringName& p_name,Variant &r_ret) const {
  846. bool ok;
  847. int v = ObjectTypeDB::get_integer_constant(name, p_name, &ok);
  848. if (ok) {
  849. r_ret=v;
  850. return true;
  851. } else {
  852. return false;
  853. }
  854. }
  855. void GDNativeClass::_bind_methods() {
  856. ObjectTypeDB::bind_method(_MD("new"),&GDNativeClass::_new);
  857. }
  858. Variant GDNativeClass::_new() {
  859. Object *o = instance();
  860. if (!o) {
  861. ERR_EXPLAIN("Class type: '"+String(name)+"' is not instantiable.");
  862. ERR_FAIL_COND_V(!o,Variant());
  863. }
  864. Reference *ref = o->cast_to<Reference>();
  865. if (ref) {
  866. return REF(ref);
  867. } else {
  868. return o;
  869. }
  870. }
  871. Object *GDNativeClass::instance() {
  872. return ObjectTypeDB::instance(name);
  873. }
  874. GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref) {
  875. /* STEP 1, CREATE */
  876. GDInstance* instance = memnew( GDInstance );
  877. instance->base_ref=p_isref;
  878. instance->members.resize(member_indices.size());
  879. instance->script=Ref<GDScript>(this);
  880. instance->owner=p_owner;
  881. instance->owner->set_script_instance(instance);
  882. /* STEP 2, INITIALIZE AND CONSRTUCT */
  883. instances.insert(instance->owner);
  884. Variant::CallError err;
  885. initializer->call(instance,p_args,p_argcount,err);
  886. if (err.error!=Variant::CallError::CALL_OK) {
  887. instance->script=Ref<GDScript>();
  888. instances.erase(p_owner);
  889. memdelete(instance);
  890. ERR_FAIL_COND_V(err.error!=Variant::CallError::CALL_OK, NULL); //error consrtucting
  891. }
  892. //@TODO make thread safe
  893. return instance;
  894. }
  895. Variant GDScript::_new(const Variant** p_args,int p_argcount,Variant::CallError& r_error) {
  896. /* STEP 1, CREATE */
  897. r_error.error=Variant::CallError::CALL_OK;
  898. REF ref;
  899. Object *owner=NULL;
  900. GDScript *_baseptr=this;
  901. while (_baseptr->_base) {
  902. _baseptr=_baseptr->_base;
  903. }
  904. if (_baseptr->native.ptr()) {
  905. owner=_baseptr->native->instance();
  906. } else {
  907. owner=memnew( Reference ); //by default, no base means use reference
  908. }
  909. Reference *r=owner->cast_to<Reference>();
  910. if (r) {
  911. ref=REF(r);
  912. }
  913. GDInstance* instance = _create_instance(p_args,p_argcount,owner,r!=NULL);
  914. if (!instance) {
  915. if (ref.is_null()) {
  916. memdelete(owner); //no owner, sorry
  917. }
  918. return Variant();
  919. }
  920. if (ref.is_valid()) {
  921. return ref;
  922. } else {
  923. return owner;
  924. }
  925. }
  926. bool GDScript::can_instance() const {
  927. return valid; //any script in GDscript can instance
  928. }
  929. StringName GDScript::get_instance_base_type() const {
  930. if (native.is_valid())
  931. return native->get_name();
  932. if (base.is_valid())
  933. return base->get_instance_base_type();
  934. return StringName();
  935. }
  936. struct _GDScriptMemberSort {
  937. int index;
  938. StringName name;
  939. _FORCE_INLINE_ bool operator<(const _GDScriptMemberSort& p_member) const { return index < p_member.index; }
  940. };
  941. #ifdef TOOLS_ENABLED
  942. void GDScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  943. placeholders.erase(p_placeholder);
  944. }
  945. void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  946. List<PropertyInfo> plist;
  947. GDScript *scr=this;
  948. Map<StringName,Variant> default_values;
  949. while(scr) {
  950. Vector<_GDScriptMemberSort> msort;
  951. for(Map<StringName,PropertyInfo>::Element *E=scr->member_info.front();E;E=E->next()) {
  952. _GDScriptMemberSort ms;
  953. ERR_CONTINUE(!scr->member_indices.has(E->key()));
  954. ms.index=scr->member_indices[E->key()];
  955. ms.name=E->key();
  956. msort.push_back(ms);
  957. }
  958. msort.sort();
  959. msort.invert();
  960. for(int i=0;i<msort.size();i++) {
  961. plist.push_front(scr->member_info[msort[i].name]);
  962. if (scr->member_default_values.has(msort[i].name))
  963. default_values[msort[i].name]=scr->member_default_values[msort[i].name];
  964. else {
  965. Variant::CallError err;
  966. default_values[msort[i].name]=Variant::construct(scr->member_info[msort[i].name].type,NULL,0,err);
  967. }
  968. }
  969. scr=scr->_base;
  970. }
  971. p_placeholder->update(plist,default_values);
  972. }
  973. #endif
  974. ScriptInstance* GDScript::instance_create(Object *p_this) {
  975. if (!tool && !ScriptServer::is_scripting_enabled()) {
  976. #ifdef TOOLS_ENABLED
  977. //instance a fake script for editing the values
  978. //plist.invert();
  979. /*print_line("CREATING PLACEHOLDER");
  980. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  981. print_line(E->get().name);
  982. }*/
  983. PlaceHolderScriptInstance *si = memnew( PlaceHolderScriptInstance(GDScriptLanguage::get_singleton(),Ref<Script>(this),p_this) );
  984. placeholders.insert(si);
  985. _update_placeholder(si);
  986. return si;
  987. #else
  988. return NULL;
  989. #endif
  990. }
  991. GDScript *top=this;
  992. while(top->_base)
  993. top=top->_base;
  994. if (top->native.is_valid()) {
  995. if (!ObjectTypeDB::is_type(p_this->get_type_name(),top->native->get_name())) {
  996. if (ScriptDebugger::get_singleton()) {
  997. 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()+"'");
  998. }
  999. 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()+"'");
  1000. ERR_FAIL_V(NULL);
  1001. }
  1002. }
  1003. return _create_instance(NULL,0,p_this,p_this->cast_to<Reference>());
  1004. }
  1005. bool GDScript::instance_has(const Object *p_this) const {
  1006. return instances.has((Object*)p_this);
  1007. }
  1008. bool GDScript::has_source_code() const {
  1009. return source!="";
  1010. }
  1011. String GDScript::get_source_code() const {
  1012. return source;
  1013. }
  1014. void GDScript::set_source_code(const String& p_code) {
  1015. source=p_code;
  1016. }
  1017. void GDScript::_set_subclass_path(Ref<GDScript>& p_sc,const String& p_path) {
  1018. p_sc->path=p_path;
  1019. for(Map<StringName,Ref<GDScript> >::Element *E=p_sc->subclasses.front();E;E=E->next()) {
  1020. _set_subclass_path(E->get(),p_path);
  1021. }
  1022. }
  1023. Error GDScript::reload() {
  1024. ERR_FAIL_COND_V(instances.size(),ERR_ALREADY_IN_USE);
  1025. String basedir=path;
  1026. if (basedir=="")
  1027. basedir=get_path();
  1028. if (basedir!="")
  1029. basedir=basedir.get_base_dir();
  1030. valid=false;
  1031. GDParser parser;
  1032. Error err = parser.parse(source,basedir);
  1033. if (err) {
  1034. if (ScriptDebugger::get_singleton()) {
  1035. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),parser.get_error_line(),"Parser Error: "+parser.get_error());
  1036. }
  1037. _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());
  1038. ERR_FAIL_V(ERR_PARSE_ERROR);
  1039. }
  1040. GDCompiler compiler;
  1041. err = compiler.compile(&parser,this);
  1042. if (err) {
  1043. if (ScriptDebugger::get_singleton()) {
  1044. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),compiler.get_error_line(),"Parser Error: "+compiler.get_error());
  1045. }
  1046. _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());
  1047. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1048. }
  1049. valid=true;
  1050. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1051. _set_subclass_path(E->get(),path);
  1052. }
  1053. #ifdef TOOLS_ENABLED
  1054. for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  1055. _update_placeholder(E->get());
  1056. }
  1057. #endif
  1058. return OK;
  1059. }
  1060. String GDScript::get_node_type() const {
  1061. return ""; // ?
  1062. }
  1063. ScriptLanguage *GDScript::get_language() const {
  1064. return GDScriptLanguage::get_singleton();
  1065. }
  1066. Variant GDScript::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1067. GDScript *top=this;
  1068. while(top) {
  1069. Map<StringName,GDFunction>::Element *E=top->member_functions.find(p_method);
  1070. if (E) {
  1071. if (!E->get().is_static()) {
  1072. WARN_PRINT(String("Can't call non-static function: '"+String(p_method)+"' in script.").utf8().get_data());
  1073. }
  1074. return E->get().call(NULL,p_args,p_argcount,r_error);
  1075. }
  1076. top=top->_base;
  1077. }
  1078. //none found, regular
  1079. return Script::call(p_method,p_args,p_argcount,r_error);
  1080. }
  1081. bool GDScript::_get(const StringName& p_name,Variant &r_ret) const {
  1082. {
  1083. const GDScript *top=this;
  1084. while(top) {
  1085. {
  1086. const Map<StringName,Variant>::Element *E=top->constants.find(p_name);
  1087. if (E) {
  1088. r_ret= E->get();
  1089. return true;
  1090. }
  1091. }
  1092. {
  1093. const Map<StringName,Ref<GDScript> >::Element *E=subclasses.find(p_name);
  1094. if (E) {
  1095. r_ret=E->get();
  1096. return true;
  1097. }
  1098. }
  1099. top=top->_base;
  1100. }
  1101. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1102. r_ret=get_source_code();
  1103. return true;
  1104. }
  1105. }
  1106. return false;
  1107. }
  1108. bool GDScript::_set(const StringName& p_name, const Variant& p_value) {
  1109. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1110. set_source_code(p_value);
  1111. reload();
  1112. } else
  1113. return false;
  1114. return true;
  1115. }
  1116. void GDScript::_get_property_list(List<PropertyInfo> *p_properties) const {
  1117. p_properties->push_back( PropertyInfo(Variant::STRING,"script/source",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  1118. }
  1119. void GDScript::_bind_methods() {
  1120. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"new",&GDScript::_new,MethodInfo("new"));
  1121. }
  1122. Error GDScript::load_byte_code(const String& p_path) {
  1123. Vector<uint8_t> bytecode = FileAccess::get_file_as_array(p_path);
  1124. ERR_FAIL_COND_V(bytecode.size()==0,ERR_PARSE_ERROR);
  1125. path=p_path;
  1126. String basedir=path;
  1127. if (basedir=="")
  1128. basedir=get_path();
  1129. if (basedir!="")
  1130. basedir=basedir.get_base_dir();
  1131. valid=false;
  1132. GDParser parser;
  1133. Error err = parser.parse_bytecode(bytecode,basedir);
  1134. if (err) {
  1135. _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());
  1136. ERR_FAIL_V(ERR_PARSE_ERROR);
  1137. }
  1138. GDCompiler compiler;
  1139. err = compiler.compile(&parser,this);
  1140. if (err) {
  1141. _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());
  1142. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1143. }
  1144. valid=true;
  1145. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1146. _set_subclass_path(E->get(),path);
  1147. }
  1148. return OK;
  1149. }
  1150. Error GDScript::load_source_code(const String& p_path) {
  1151. DVector<uint8_t> sourcef;
  1152. Error err;
  1153. FileAccess *f=FileAccess::open(p_path,FileAccess::READ,&err);
  1154. if (err) {
  1155. ERR_FAIL_COND_V(err,err);
  1156. }
  1157. int len = f->get_len();
  1158. sourcef.resize(len+1);
  1159. DVector<uint8_t>::Write w = sourcef.write();
  1160. int r = f->get_buffer(w.ptr(),len);
  1161. f->close();
  1162. memdelete(f);
  1163. ERR_FAIL_COND_V(r!=len,ERR_CANT_OPEN);
  1164. w[len]=0;
  1165. String s;
  1166. if (s.parse_utf8((const char*)w.ptr())) {
  1167. 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.");
  1168. ERR_FAIL_V(ERR_INVALID_DATA);
  1169. }
  1170. source=s;
  1171. path=p_path;
  1172. return OK;
  1173. }
  1174. const Map<StringName,GDFunction>& GDScript::debug_get_member_functions() const {
  1175. return member_functions;
  1176. }
  1177. StringName GDScript::debug_get_member_by_index(int p_idx) const {
  1178. for(const Map<StringName,int>::Element *E=member_indices.front();E;E=E->next()) {
  1179. if (E->get()==p_idx)
  1180. return E->key();
  1181. }
  1182. return "<error>";
  1183. }
  1184. Ref<GDScript> GDScript::get_base() const {
  1185. return base;
  1186. }
  1187. GDScript::GDScript() {
  1188. _static_ref=this;
  1189. valid=false;
  1190. subclass_count=0;
  1191. initializer=NULL;
  1192. _base=NULL;
  1193. _owner=NULL;
  1194. tool=false;
  1195. }
  1196. //////////////////////////////
  1197. // INSTANCE //
  1198. //////////////////////////////
  1199. bool GDInstance::set(const StringName& p_name, const Variant& p_value) {
  1200. //member
  1201. {
  1202. const Map<StringName,int>::Element *E = script->member_indices.find(p_name);
  1203. if (E) {
  1204. members[E->get()]=p_value;
  1205. return true;
  1206. }
  1207. }
  1208. GDScript *sptr=script.ptr();
  1209. while(sptr) {
  1210. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
  1211. if (E) {
  1212. Variant name=p_name;
  1213. const Variant *args[2]={&name,&p_value};
  1214. Variant::CallError err;
  1215. Variant ret = E->get().call(this,(const Variant**)args,2,err);
  1216. if (err.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::BOOL && ret.operator bool())
  1217. return true;
  1218. }
  1219. sptr = sptr->_base;
  1220. }
  1221. return false;
  1222. }
  1223. bool GDInstance::get(const StringName& p_name, Variant &r_ret) const {
  1224. const GDScript *sptr=script.ptr();
  1225. while(sptr) {
  1226. {
  1227. const Map<StringName,int>::Element *E = script->member_indices.find(p_name);
  1228. if (E) {
  1229. r_ret=members[E->get()];
  1230. return true; //index found
  1231. }
  1232. }
  1233. {
  1234. const Map<StringName,Variant>::Element *E = script->constants.find(p_name);
  1235. if (E) {
  1236. r_ret=E->get();
  1237. return true; //index found
  1238. }
  1239. }
  1240. {
  1241. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
  1242. if (E) {
  1243. Variant name=p_name;
  1244. const Variant *args[1]={&name};
  1245. Variant::CallError err;
  1246. Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
  1247. if (err.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  1248. r_ret=ret;
  1249. return true;
  1250. }
  1251. }
  1252. }
  1253. sptr = sptr->_base;
  1254. }
  1255. return false;
  1256. }
  1257. void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  1258. // exported members, not doen yet!
  1259. const GDScript *sptr=script.ptr();
  1260. List<PropertyInfo> props;
  1261. while(sptr) {
  1262. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
  1263. if (E) {
  1264. Variant::CallError err;
  1265. Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
  1266. if (err.error==Variant::CallError::CALL_OK) {
  1267. if (ret.get_type()!=Variant::ARRAY) {
  1268. ERR_EXPLAIN("Wrong type for _get_property list, must be an array of dictionaries.");
  1269. ERR_FAIL();
  1270. }
  1271. Array arr = ret;
  1272. for(int i=0;i<arr.size();i++) {
  1273. Dictionary d = arr[i];
  1274. ERR_CONTINUE(!d.has("name"));
  1275. ERR_CONTINUE(!d.has("type"));
  1276. PropertyInfo pinfo;
  1277. pinfo.type = Variant::Type( d["type"].operator int());
  1278. ERR_CONTINUE(pinfo.type<0 || pinfo.type>=Variant::VARIANT_MAX );
  1279. pinfo.name = d["name"];
  1280. ERR_CONTINUE(pinfo.name=="");
  1281. if (d.has("hint"))
  1282. pinfo.hint=PropertyHint(d["hint"].operator int());
  1283. if (d.has("hint_string"))
  1284. pinfo.hint_string=d["hint_string"];
  1285. if (d.has("usage"))
  1286. pinfo.usage=d["usage"];
  1287. props.push_back(pinfo);
  1288. }
  1289. }
  1290. }
  1291. //instance a fake script for editing the values
  1292. Vector<_GDScriptMemberSort> msort;
  1293. for(Map<StringName,PropertyInfo>::Element *E=sptr->member_info.front();E;E=E->next()) {
  1294. _GDScriptMemberSort ms;
  1295. ERR_CONTINUE(!sptr->member_indices.has(E->key()));
  1296. ms.index=sptr->member_indices[E->key()];
  1297. ms.name=E->key();
  1298. msort.push_back(ms);
  1299. }
  1300. msort.sort();
  1301. msort.invert();
  1302. for(int i=0;i<msort.size();i++) {
  1303. props.push_front(sptr->member_info[msort[i].name]);
  1304. }
  1305. #if 0
  1306. if (sptr->member_functions.has("_get_property_list")) {
  1307. Variant::CallError err;
  1308. GDFunction *f = const_cast<GDFunction*>(&sptr->member_functions["_get_property_list"]);
  1309. Variant plv = f->call(const_cast<GDInstance*>(this),NULL,0,err);
  1310. if (plv.get_type()!=Variant::ARRAY) {
  1311. ERR_PRINT("_get_property_list: expected array returned");
  1312. } else {
  1313. Array pl=plv;
  1314. for(int i=0;i<pl.size();i++) {
  1315. Dictionary p = pl[i];
  1316. PropertyInfo pinfo;
  1317. if (!p.has("name")) {
  1318. ERR_PRINT("_get_property_list: expected 'name' key of type string.")
  1319. continue;
  1320. }
  1321. if (!p.has("type")) {
  1322. ERR_PRINT("_get_property_list: expected 'type' key of type integer.")
  1323. continue;
  1324. }
  1325. pinfo.name=p["name"];
  1326. pinfo.type=Variant::Type(int(p["type"]));
  1327. if (p.has("hint"))
  1328. pinfo.hint=PropertyHint(int(p["hint"]));
  1329. if (p.has("hint_string"))
  1330. pinfo.hint_string=p["hint_string"];
  1331. if (p.has("usage"))
  1332. pinfo.usage=p["usage"];
  1333. props.push_back(pinfo);
  1334. }
  1335. }
  1336. }
  1337. #endif
  1338. sptr = sptr->_base;
  1339. }
  1340. //props.invert();
  1341. for (List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  1342. p_properties->push_back(E->get());
  1343. }
  1344. }
  1345. void GDInstance::get_method_list(List<MethodInfo> *p_list) const {
  1346. const GDScript *sptr=script.ptr();
  1347. while(sptr) {
  1348. for (Map<StringName,GDFunction>::Element *E = sptr->member_functions.front();E;E=E->next()) {
  1349. MethodInfo mi;
  1350. mi.name=E->key();
  1351. for(int i=0;i<E->get().get_argument_count();i++)
  1352. mi.arguments.push_back(PropertyInfo(Variant::NIL,"arg"+itos(i)));
  1353. p_list->push_back(mi);
  1354. }
  1355. sptr = sptr->_base;
  1356. }
  1357. }
  1358. bool GDInstance::has_method(const StringName& p_method) const {
  1359. const GDScript *sptr=script.ptr();
  1360. while(sptr) {
  1361. const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1362. if (E)
  1363. return true;
  1364. sptr = sptr->_base;
  1365. }
  1366. return false;
  1367. }
  1368. Variant GDInstance::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1369. //printf("calling %ls:%i method %ls\n", script->get_path().c_str(), -1, String(p_method).c_str());
  1370. GDScript *sptr=script.ptr();
  1371. while(sptr) {
  1372. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1373. if (E) {
  1374. return E->get().call(this,p_args,p_argcount,r_error);
  1375. }
  1376. sptr = sptr->_base;
  1377. }
  1378. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1379. return Variant();
  1380. }
  1381. void GDInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1382. GDScript *sptr=script.ptr();
  1383. Variant::CallError ce;
  1384. while(sptr) {
  1385. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1386. if (E) {
  1387. E->get().call(this,p_args,p_argcount,ce);
  1388. }
  1389. sptr = sptr->_base;
  1390. }
  1391. }
  1392. void GDInstance::_ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount) {
  1393. if (sptr->_base)
  1394. _ml_call_reversed(sptr->_base,p_method,p_args,p_argcount);
  1395. Variant::CallError ce;
  1396. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
  1397. if (E) {
  1398. E->get().call(this,p_args,p_argcount,ce);
  1399. }
  1400. }
  1401. void GDInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1402. if (script.ptr()) {
  1403. _ml_call_reversed(script.ptr(),p_method,p_args,p_argcount);
  1404. }
  1405. }
  1406. void GDInstance::notification(int p_notification) {
  1407. //notification is not virutal, it gets called at ALL levels just like in C.
  1408. Variant value=p_notification;
  1409. const Variant *args[1]={&value };
  1410. GDScript *sptr=script.ptr();
  1411. while(sptr) {
  1412. Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
  1413. if (E) {
  1414. Variant::CallError err;
  1415. E->get().call(this,args,1,err);
  1416. if (err.error!=Variant::CallError::CALL_OK) {
  1417. //print error about notification call
  1418. }
  1419. }
  1420. sptr = sptr->_base;
  1421. }
  1422. }
  1423. Ref<Script> GDInstance::get_script() const {
  1424. return script;
  1425. }
  1426. ScriptLanguage *GDInstance::get_language() {
  1427. return GDScriptLanguage::get_singleton();
  1428. }
  1429. GDInstance::GDInstance() {
  1430. owner=NULL;
  1431. base_ref=false;
  1432. }
  1433. GDInstance::~GDInstance() {
  1434. if (script.is_valid() && owner) {
  1435. script->instances.erase(owner);
  1436. }
  1437. }
  1438. /************* SCRIPT LANGUAGE **************/
  1439. /************* SCRIPT LANGUAGE **************/
  1440. /************* SCRIPT LANGUAGE **************/
  1441. /************* SCRIPT LANGUAGE **************/
  1442. /************* SCRIPT LANGUAGE **************/
  1443. GDScriptLanguage *GDScriptLanguage::singleton=NULL;
  1444. String GDScriptLanguage::get_name() const {
  1445. return "GDScript";
  1446. }
  1447. /* LANGUAGE FUNCTIONS */
  1448. void GDScriptLanguage::_add_global(const StringName& p_name,const Variant& p_value) {
  1449. if (globals.has(p_name)) {
  1450. //overwrite existing
  1451. global_array[globals[p_name]]=p_value;
  1452. return;
  1453. }
  1454. globals[p_name]=global_array.size();
  1455. global_array.push_back(p_value);
  1456. _global_array=global_array.ptr();
  1457. }
  1458. void GDScriptLanguage::init() {
  1459. //populate global constants
  1460. int gcc=GlobalConstants::get_global_constant_count();
  1461. for(int i=0;i<gcc;i++) {
  1462. _add_global(StaticCString::create(GlobalConstants::get_global_constant_name(i)),GlobalConstants::get_global_constant_value(i));
  1463. }
  1464. _add_global(StaticCString::create("PI"),Math_PI);
  1465. //populate native classes
  1466. List<String> class_list;
  1467. ObjectTypeDB::get_type_list(&class_list);
  1468. for(List<String>::Element *E=class_list.front();E;E=E->next()) {
  1469. StringName n = E->get();
  1470. String s = String(n);
  1471. if (s.begins_with("_"))
  1472. n=s.substr(1,s.length());
  1473. if (globals.has(n))
  1474. continue;
  1475. Ref<GDNativeClass> nc = memnew( GDNativeClass(E->get()) );
  1476. _add_global(n,nc);
  1477. }
  1478. //populate singletons
  1479. List<Globals::Singleton> singletons;
  1480. Globals::get_singleton()->get_singletons(&singletons);
  1481. for(List<Globals::Singleton>::Element *E=singletons.front();E;E=E->next()) {
  1482. _add_global(E->get().name,E->get().ptr);
  1483. }
  1484. }
  1485. String GDScriptLanguage::get_type() const {
  1486. return "GDScript";
  1487. }
  1488. String GDScriptLanguage::get_extension() const {
  1489. return "gd";
  1490. }
  1491. Error GDScriptLanguage::execute_file(const String& p_path) {
  1492. // ??
  1493. return OK;
  1494. }
  1495. void GDScriptLanguage::finish() {
  1496. }
  1497. void GDScriptLanguage::frame() {
  1498. // print_line("calls: "+itos(calls));
  1499. calls=0;
  1500. }
  1501. /* EDITOR FUNCTIONS */
  1502. void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
  1503. static const char *_reserved_words[]={
  1504. "break",
  1505. "class",
  1506. "continue",
  1507. "const",
  1508. "else",
  1509. "elif",
  1510. "enum",
  1511. "extends" ,
  1512. "for" ,
  1513. "func" ,
  1514. "if" ,
  1515. "in" ,
  1516. "varl",
  1517. "null" ,
  1518. "return" ,
  1519. "self" ,
  1520. "while" ,
  1521. "true" ,
  1522. "false" ,
  1523. "tool",
  1524. "var",
  1525. "pass",
  1526. "and",
  1527. "or",
  1528. "export",
  1529. "assert",
  1530. 0};
  1531. const char **w=_reserved_words;
  1532. while (*w) {
  1533. p_words->push_back(*w);
  1534. w++;
  1535. }
  1536. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1537. p_words->push_back(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1538. }
  1539. }
  1540. GDScriptLanguage::GDScriptLanguage() {
  1541. calls=0;
  1542. ERR_FAIL_COND(singleton);
  1543. singleton=this;
  1544. strings._init = StaticCString::create("_init");
  1545. strings._notification = StaticCString::create("_notification");
  1546. strings._set= StaticCString::create("_set");
  1547. strings._get= StaticCString::create("_get");
  1548. strings._get_property_list= StaticCString::create("_get_property_list");
  1549. strings._script_source=StaticCString::create("script/source");
  1550. _debug_parse_err_line=-1;
  1551. _debug_parse_err_file="";
  1552. _debug_call_stack_pos=0;
  1553. int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
  1554. if (ScriptDebugger::get_singleton()) {
  1555. //debugging enabled!
  1556. _debug_max_call_stack = dmcs;
  1557. if (_debug_max_call_stack<1024)
  1558. _debug_max_call_stack=1024;
  1559. _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
  1560. } else {
  1561. _debug_max_call_stack=0;
  1562. _call_stack=NULL;
  1563. }
  1564. }
  1565. GDScriptLanguage::~GDScriptLanguage() {
  1566. if (_call_stack) {
  1567. memdelete_arr(_call_stack);
  1568. }
  1569. singleton=NULL;
  1570. }
  1571. /*************** RESOURCE ***************/
  1572. RES ResourceFormatLoaderGDScript::load(const String &p_path,const String& p_original_path) {
  1573. GDScript *script = memnew( GDScript );
  1574. Ref<GDScript> scriptres(script);
  1575. if (p_path.ends_with(".gdc")) {
  1576. script->set_script_path(p_original_path); // script needs this.
  1577. script->set_path(p_original_path);
  1578. Error err = script->load_byte_code(p_path);
  1579. if (err!=OK) {
  1580. ERR_FAIL_COND_V(err!=OK, RES());
  1581. }
  1582. } else {
  1583. Error err = script->load_source_code(p_path);
  1584. if (err!=OK) {
  1585. ERR_FAIL_COND_V(err!=OK, RES());
  1586. }
  1587. script->set_script_path(p_original_path); // script needs this.
  1588. script->set_path(p_original_path);
  1589. //script->set_name(p_path.get_file());
  1590. script->reload();
  1591. }
  1592. return scriptres;
  1593. }
  1594. void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
  1595. p_extensions->push_back("gd");
  1596. p_extensions->push_back("gdc");
  1597. }
  1598. bool ResourceFormatLoaderGDScript::handles_type(const String& p_type) const {
  1599. return (p_type=="Script" || p_type=="GDScript");
  1600. }
  1601. String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
  1602. String el = p_path.extension().to_lower();
  1603. if (el=="gd" || el=="gdc")
  1604. return "GDScript";
  1605. return "";
  1606. }
  1607. Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1608. Ref<GDScript> sqscr = p_resource;
  1609. ERR_FAIL_COND_V(sqscr.is_null(),ERR_INVALID_PARAMETER);
  1610. String source = sqscr->get_source_code();
  1611. Error err;
  1612. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  1613. if (err) {
  1614. ERR_FAIL_COND_V(err,err);
  1615. }
  1616. file->store_string(source);
  1617. file->close();
  1618. memdelete(file);
  1619. return OK;
  1620. }
  1621. void ResourceFormatSaverGDScript::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1622. if (p_resource->cast_to<GDScript>()) {
  1623. p_extensions->push_back("gd");
  1624. }
  1625. }
  1626. bool ResourceFormatSaverGDScript::recognize(const RES& p_resource) const {
  1627. return p_resource->cast_to<GDScript>()!=NULL;
  1628. }