2
0

gd_script.cpp 50 KB

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