gd_script.cpp 51 KB

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