gd_script.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  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. "assert",
  1527. 0};
  1528. const char **w=_reserved_words;
  1529. while (*w) {
  1530. p_words->push_back(*w);
  1531. w++;
  1532. }
  1533. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1534. p_words->push_back(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1535. }
  1536. }
  1537. GDScriptLanguage::GDScriptLanguage() {
  1538. calls=0;
  1539. ERR_FAIL_COND(singleton);
  1540. singleton=this;
  1541. strings._init = StaticCString::create("_init");
  1542. strings._notification = StaticCString::create("_notification");
  1543. strings._set= StaticCString::create("_set");
  1544. strings._get= StaticCString::create("_get");
  1545. strings._get_property_list= StaticCString::create("_get_property_list");
  1546. strings._script_source=StaticCString::create("script/source");
  1547. _debug_parse_err_line=-1;
  1548. _debug_parse_err_file="";
  1549. _debug_call_stack_pos=0;
  1550. int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
  1551. if (ScriptDebugger::get_singleton()) {
  1552. //debugging enabled!
  1553. _debug_max_call_stack = dmcs;
  1554. if (_debug_max_call_stack<1024)
  1555. _debug_max_call_stack=1024;
  1556. _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
  1557. } else {
  1558. _debug_max_call_stack=0;
  1559. _call_stack=NULL;
  1560. }
  1561. }
  1562. GDScriptLanguage::~GDScriptLanguage() {
  1563. if (_call_stack) {
  1564. memdelete_arr(_call_stack);
  1565. }
  1566. singleton=NULL;
  1567. }
  1568. /*************** RESOURCE ***************/
  1569. RES ResourceFormatLoaderGDScript::load(const String &p_path,const String& p_original_path) {
  1570. GDScript *script = memnew( GDScript );
  1571. Ref<GDScript> scriptres(script);
  1572. if (p_path.ends_with(".gdc")) {
  1573. script->set_script_path(p_original_path); // script needs this.
  1574. script->set_path(p_original_path);
  1575. Error err = script->load_byte_code(p_path);
  1576. if (err!=OK) {
  1577. ERR_FAIL_COND_V(err!=OK, RES());
  1578. }
  1579. } else {
  1580. Error err = script->load_source_code(p_path);
  1581. if (err!=OK) {
  1582. ERR_FAIL_COND_V(err!=OK, RES());
  1583. }
  1584. script->set_script_path(p_original_path); // script needs this.
  1585. script->set_path(p_original_path);
  1586. //script->set_name(p_path.get_file());
  1587. script->reload();
  1588. }
  1589. return scriptres;
  1590. }
  1591. void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
  1592. p_extensions->push_back("gd");
  1593. p_extensions->push_back("gdc");
  1594. }
  1595. bool ResourceFormatLoaderGDScript::handles_type(const String& p_type) const {
  1596. return (p_type=="Script" || p_type=="GDScript");
  1597. }
  1598. String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
  1599. String el = p_path.extension().to_lower();
  1600. if (el=="gd" || el=="gdc")
  1601. return "GDScript";
  1602. return "";
  1603. }
  1604. Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1605. Ref<GDScript> sqscr = p_resource;
  1606. ERR_FAIL_COND_V(sqscr.is_null(),ERR_INVALID_PARAMETER);
  1607. String source = sqscr->get_source_code();
  1608. Error err;
  1609. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  1610. if (err) {
  1611. ERR_FAIL_COND_V(err,err);
  1612. }
  1613. file->store_string(source);
  1614. file->close();
  1615. memdelete(file);
  1616. return OK;
  1617. }
  1618. void ResourceFormatSaverGDScript::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1619. if (p_resource->cast_to<GDScript>()) {
  1620. p_extensions->push_back("gd");
  1621. }
  1622. }
  1623. bool ResourceFormatSaverGDScript::recognize(const RES& p_resource) const {
  1624. return p_resource->cast_to<GDScript>()!=NULL;
  1625. }