gd_script.cpp 51 KB

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