gd_script.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116
  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-2016 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. #include "os/os.h"
  36. Variant *GDFunction::_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self, Variant *p_stack,String& r_error) const{
  37. int address = p_address&ADDR_MASK;
  38. //sequential table (jump table generated by compiler)
  39. switch((p_address&ADDR_TYPE_MASK)>>ADDR_BITS) {
  40. case ADDR_TYPE_SELF: {
  41. if (!p_instance) {
  42. r_error="Cannot access self without instance.";
  43. return NULL;
  44. }
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &p_script->_static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. //member indexing is O(1)
  52. if (!p_instance) {
  53. r_error="Cannot access member without instance.";
  54. return NULL;
  55. }
  56. return &p_instance->members[address];
  57. } break;
  58. case ADDR_TYPE_CLASS_CONSTANT: {
  59. //todo change to index!
  60. GDScript *o=p_script;
  61. ERR_FAIL_INDEX_V(address,_global_names_count,NULL);
  62. const StringName *sn = &_global_names_ptr[address];
  63. while(o) {
  64. GDScript *s=o;
  65. while(s) {
  66. Map<StringName,Variant>::Element *E=s->constants.find(*sn);
  67. if (E) {
  68. return &E->get();
  69. }
  70. s=s->_base;
  71. }
  72. o=o->_owner;
  73. }
  74. ERR_EXPLAIN("GDCompiler bug..");
  75. ERR_FAIL_V(NULL);
  76. } break;
  77. case ADDR_TYPE_LOCAL_CONSTANT: {
  78. ERR_FAIL_INDEX_V(address,_constant_count,NULL);
  79. return &_constants_ptr[address];
  80. } break;
  81. case ADDR_TYPE_STACK:
  82. case ADDR_TYPE_STACK_VARIABLE: {
  83. ERR_FAIL_INDEX_V(address,_stack_size,NULL);
  84. return &p_stack[address];
  85. } break;
  86. case ADDR_TYPE_GLOBAL: {
  87. ERR_FAIL_INDEX_V(address,GDScriptLanguage::get_singleton()->get_global_array_size(),NULL);
  88. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  89. } break;
  90. case ADDR_TYPE_NIL: {
  91. return &nil;
  92. } break;
  93. }
  94. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  95. ERR_FAIL_V(NULL);
  96. return NULL;
  97. }
  98. String GDFunction::_get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const {
  99. String err_text;
  100. if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  101. int errorarg=p_err.argument;
  102. 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)+".";
  103. } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  104. err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
  105. } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  106. err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
  107. } else if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  108. err_text="Invalid call. Nonexistent "+p_where+".";
  109. } else if (p_err.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  110. err_text="Attempt to call "+p_where+" on a null instance.";
  111. } else {
  112. err_text="Bug, call error: #"+itos(p_err.error);
  113. }
  114. return err_text;
  115. }
  116. static String _get_var_type(const Variant* p_type) {
  117. String basestr;
  118. if (p_type->get_type()==Variant::OBJECT) {
  119. Object *bobj = *p_type;
  120. if (!bobj) {
  121. basestr = "null instance";
  122. } else {
  123. #ifdef DEBUG_ENABLED
  124. if (ObjectDB::instance_validate(bobj)) {
  125. if (bobj->get_script_instance())
  126. basestr= bobj->get_type()+" ("+bobj->get_script_instance()->get_script()->get_path().get_file()+")";
  127. else
  128. basestr = bobj->get_type();
  129. } else {
  130. basestr="previously freed instance";
  131. }
  132. #else
  133. basestr="Object";
  134. #endif
  135. }
  136. } else {
  137. basestr = Variant::get_type_name(p_type->get_type());
  138. }
  139. return basestr;
  140. }
  141. Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError& r_err, CallState *p_state) {
  142. if (!_code_ptr) {
  143. return Variant();
  144. }
  145. r_err.error=Variant::CallError::CALL_OK;
  146. Variant self;
  147. Variant retvalue;
  148. Variant *stack = NULL;
  149. Variant **call_args;
  150. int defarg=0;
  151. #ifdef DEBUG_ENABLED
  152. //GDScriptLanguage::get_singleton()->calls++;
  153. #endif
  154. uint32_t alloca_size=0;
  155. GDScript *_class;
  156. int ip=0;
  157. int line=_initial_line;
  158. if (p_state) {
  159. //use existing (supplied) state (yielded)
  160. stack=(Variant*)p_state->stack.ptr();
  161. call_args=(Variant**)&p_state->stack[sizeof(Variant)*p_state->stack_size];
  162. line=p_state->line;
  163. ip=p_state->ip;
  164. alloca_size=p_state->stack.size();
  165. _class=p_state->_class;
  166. p_instance=p_state->instance;
  167. defarg=p_state->defarg;
  168. self=p_state->self;
  169. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  170. } else {
  171. if (p_argcount!=_argument_count) {
  172. if (p_argcount>_argument_count) {
  173. r_err.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  174. r_err.argument=_argument_count;
  175. return Variant();
  176. } else if (p_argcount < _argument_count - _default_arg_count) {
  177. r_err.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  178. r_err.argument=_argument_count - _default_arg_count;
  179. return Variant();
  180. } else {
  181. defarg=_argument_count-p_argcount;
  182. }
  183. }
  184. alloca_size = sizeof(Variant*)*_call_size + sizeof(Variant)*_stack_size;
  185. if (alloca_size) {
  186. uint8_t *aptr = (uint8_t*)alloca(alloca_size);
  187. if (_stack_size) {
  188. stack=(Variant*)aptr;
  189. for(int i=0;i<p_argcount;i++)
  190. memnew_placement(&stack[i],Variant(*p_args[i]));
  191. for(int i=p_argcount;i<_stack_size;i++)
  192. memnew_placement(&stack[i],Variant);
  193. } else {
  194. stack=NULL;
  195. }
  196. if (_call_size) {
  197. call_args = (Variant**)&aptr[sizeof(Variant)*_stack_size];
  198. } else {
  199. call_args=NULL;
  200. }
  201. } else {
  202. stack=NULL;
  203. call_args=NULL;
  204. }
  205. if (p_instance) {
  206. if (p_instance->base_ref && static_cast<Reference*>(p_instance->owner)->is_referenced()) {
  207. self=REF(static_cast<Reference*>(p_instance->owner));
  208. } else {
  209. self=p_instance->owner;
  210. }
  211. _class=p_instance->script.ptr();
  212. } else {
  213. _class=_script;
  214. }
  215. }
  216. String err_text;
  217. #ifdef DEBUG_ENABLED
  218. if (ScriptDebugger::get_singleton())
  219. GDScriptLanguage::get_singleton()->enter_function(p_instance,this,stack,&ip,&line);
  220. #define CHECK_SPACE(m_space)\
  221. ERR_BREAK((ip+m_space)>_code_size)
  222. #define GET_VARIANT_PTR(m_v,m_code_ofs) \
  223. Variant *m_v; \
  224. m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);\
  225. if (!m_v)\
  226. break;
  227. #else
  228. #define CHECK_SPACE(m_space)
  229. #define GET_VARIANT_PTR(m_v,m_code_ofs) \
  230. Variant *m_v; \
  231. m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);
  232. #endif
  233. #ifdef DEBUG_ENABLED
  234. uint64_t function_start_time;
  235. uint64_t function_call_time;
  236. if (GDScriptLanguage::get_singleton()->profiling) {
  237. function_start_time=OS::get_singleton()->get_ticks_usec();
  238. function_call_time=0;
  239. profile.call_count++;
  240. profile.frame_call_count++;
  241. }
  242. #endif
  243. bool exit_ok=false;
  244. while(ip<_code_size) {
  245. int last_opcode=_code_ptr[ip];
  246. switch(_code_ptr[ip]) {
  247. case OPCODE_OPERATOR: {
  248. CHECK_SPACE(5);
  249. bool valid;
  250. Variant::Operator op = (Variant::Operator)_code_ptr[ip+1];
  251. ERR_BREAK(op>=Variant::OP_MAX);
  252. GET_VARIANT_PTR(a,2);
  253. GET_VARIANT_PTR(b,3);
  254. GET_VARIANT_PTR(dst,4);
  255. #ifdef DEBUG_ENABLED
  256. Variant ret;
  257. Variant::evaluate(op,*a,*b,ret,valid);
  258. #else
  259. Variant::evaluate(op,*a,*b,*dst,valid);
  260. #endif
  261. if (!valid) {
  262. #ifdef DEBUG_ENABLED
  263. if (ret.get_type()==Variant::STRING) {
  264. //return a string when invalid with the error
  265. err_text=ret;
  266. err_text += " in operator '"+Variant::get_operator_name(op)+"'.";
  267. } else {
  268. 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)+"'.";
  269. }
  270. #endif
  271. break;
  272. }
  273. #ifdef DEBUG_ENABLED
  274. *dst=ret;
  275. #endif
  276. ip+=5;
  277. } continue;
  278. case OPCODE_EXTENDS_TEST: {
  279. CHECK_SPACE(4);
  280. GET_VARIANT_PTR(a,1);
  281. GET_VARIANT_PTR(b,2);
  282. GET_VARIANT_PTR(dst,3);
  283. #ifdef DEBUG_ENABLED
  284. if (a->get_type()!=Variant::OBJECT || a->operator Object*()==NULL) {
  285. err_text="Left operand of 'extends' is not an instance of anything.";
  286. break;
  287. }
  288. if (b->get_type()!=Variant::OBJECT || b->operator Object*()==NULL) {
  289. err_text="Right operand of 'extends' is not a class.";
  290. break;
  291. }
  292. #endif
  293. Object *obj_A = *a;
  294. Object *obj_B = *b;
  295. GDScript *scr_B = obj_B->cast_to<GDScript>();
  296. bool extends_ok=false;
  297. if (scr_B) {
  298. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  299. //in other situation, this shoul return false.
  300. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language()==GDScriptLanguage::get_singleton()) {
  301. GDInstance *ins = static_cast<GDInstance*>(obj_A->get_script_instance());
  302. GDScript *cmp = ins->script.ptr();
  303. //bool found=false;
  304. while(cmp) {
  305. if (cmp==scr_B) {
  306. //inherits from script, all ok
  307. extends_ok=true;
  308. break;
  309. }
  310. cmp=cmp->_base;
  311. }
  312. }
  313. } else {
  314. GDNativeClass *nc= obj_B->cast_to<GDNativeClass>();
  315. if (!nc) {
  316. err_text="Right operand of 'extends' is not a class (type: '"+obj_B->get_type()+"').";
  317. break;
  318. }
  319. extends_ok=ObjectTypeDB::is_type(obj_A->get_type_name(),nc->get_name());
  320. }
  321. *dst=extends_ok;
  322. ip+=4;
  323. } continue;
  324. case OPCODE_SET: {
  325. CHECK_SPACE(3);
  326. GET_VARIANT_PTR(dst,1);
  327. GET_VARIANT_PTR(index,2);
  328. GET_VARIANT_PTR(value,3);
  329. bool valid;
  330. dst->set(*index,*value,&valid);
  331. if (!valid) {
  332. String v = index->operator String();
  333. if (v!="") {
  334. v="'"+v+"'";
  335. } else {
  336. v="of type '"+_get_var_type(index)+"'";
  337. }
  338. err_text="Invalid set index "+v+" (on base: '"+_get_var_type(dst)+"').";
  339. break;
  340. }
  341. ip+=4;
  342. } continue;
  343. case OPCODE_GET: {
  344. CHECK_SPACE(3);
  345. GET_VARIANT_PTR(src,1);
  346. GET_VARIANT_PTR(index,2);
  347. GET_VARIANT_PTR(dst,3);
  348. bool valid;
  349. #ifdef DEBUG_ENABLED
  350. //allow better error message in cases where src and dst are the same stack position
  351. Variant ret = src->get(*index,&valid);
  352. #else
  353. *dst = src->get(*index,&valid);
  354. #endif
  355. if (!valid) {
  356. String v = index->operator String();
  357. if (v!="") {
  358. v="'"+v+"'";
  359. } else {
  360. v="of type '"+_get_var_type(index)+"'";
  361. }
  362. err_text="Invalid get index "+v+" (on base: '"+_get_var_type(src)+"').";
  363. break;
  364. }
  365. #ifdef DEBUG_ENABLED
  366. *dst=ret;
  367. #endif
  368. ip+=4;
  369. } continue;
  370. case OPCODE_SET_NAMED: {
  371. CHECK_SPACE(3);
  372. GET_VARIANT_PTR(dst,1);
  373. GET_VARIANT_PTR(value,3);
  374. int indexname = _code_ptr[ip+2];
  375. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  376. const StringName *index = &_global_names_ptr[indexname];
  377. bool valid;
  378. dst->set_named(*index,*value,&valid);
  379. if (!valid) {
  380. String err_type;
  381. err_text="Invalid set index '"+String(*index)+"' (on base: '"+_get_var_type(dst)+"').";
  382. break;
  383. }
  384. ip+=4;
  385. } continue;
  386. case OPCODE_GET_NAMED: {
  387. CHECK_SPACE(3);
  388. GET_VARIANT_PTR(src,1);
  389. GET_VARIANT_PTR(dst,3);
  390. int indexname = _code_ptr[ip+2];
  391. ERR_BREAK(indexname<0 || indexname>=_global_names_count);
  392. const StringName *index = &_global_names_ptr[indexname];
  393. bool valid;
  394. #ifdef DEBUG_ENABLED
  395. //allow better error message in cases where src and dst are the same stack position
  396. Variant ret = src->get_named(*index,&valid);
  397. #else
  398. *dst = src->get_named(*index,&valid);
  399. #endif
  400. if (!valid) {
  401. if (src->has_method(*index)) {
  402. err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"'). Did you mean '."+index->operator String()+"()' ?";
  403. } else {
  404. err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"').";
  405. }
  406. break;
  407. }
  408. #ifdef DEBUG_ENABLED
  409. *dst=ret;
  410. #endif
  411. ip+=4;
  412. } continue;
  413. case OPCODE_ASSIGN: {
  414. CHECK_SPACE(3);
  415. GET_VARIANT_PTR(dst,1);
  416. GET_VARIANT_PTR(src,2);
  417. *dst = *src;
  418. ip+=3;
  419. } continue;
  420. case OPCODE_ASSIGN_TRUE: {
  421. CHECK_SPACE(2);
  422. GET_VARIANT_PTR(dst,1);
  423. *dst = true;
  424. ip+=2;
  425. } continue;
  426. case OPCODE_ASSIGN_FALSE: {
  427. CHECK_SPACE(2);
  428. GET_VARIANT_PTR(dst,1);
  429. *dst = false;
  430. ip+=2;
  431. } continue;
  432. case OPCODE_CONSTRUCT: {
  433. CHECK_SPACE(2);
  434. Variant::Type t=Variant::Type(_code_ptr[ip+1]);
  435. int argc=_code_ptr[ip+2];
  436. CHECK_SPACE(argc+2);
  437. Variant **argptrs = call_args;
  438. for(int i=0;i<argc;i++) {
  439. GET_VARIANT_PTR(v,3+i);
  440. argptrs[i]=v;
  441. }
  442. GET_VARIANT_PTR(dst,3+argc);
  443. Variant::CallError err;
  444. *dst = Variant::construct(t,(const Variant**)argptrs,argc,err);
  445. if (err.error!=Variant::CallError::CALL_OK) {
  446. err_text=_get_call_error(err,"'"+Variant::get_type_name(t)+"' constructor",(const Variant**)argptrs);
  447. break;
  448. }
  449. ip+=4+argc;
  450. //construct a basic type
  451. } continue;
  452. case OPCODE_CONSTRUCT_ARRAY: {
  453. CHECK_SPACE(1);
  454. int argc=_code_ptr[ip+1];
  455. Array array(true); //arrays are always shared
  456. array.resize(argc);
  457. CHECK_SPACE(argc+2);
  458. for(int i=0;i<argc;i++) {
  459. GET_VARIANT_PTR(v,2+i);
  460. array[i]=*v;
  461. }
  462. GET_VARIANT_PTR(dst,2+argc);
  463. *dst=array;
  464. ip+=3+argc;
  465. } continue;
  466. case OPCODE_CONSTRUCT_DICTIONARY: {
  467. CHECK_SPACE(1);
  468. int argc=_code_ptr[ip+1];
  469. Dictionary dict(true); //arrays are always shared
  470. CHECK_SPACE(argc*2+2);
  471. for(int i=0;i<argc;i++) {
  472. GET_VARIANT_PTR(k,2+i*2+0);
  473. GET_VARIANT_PTR(v,2+i*2+1);
  474. dict[*k]=*v;
  475. }
  476. GET_VARIANT_PTR(dst,2+argc*2);
  477. *dst=dict;
  478. ip+=3+argc*2;
  479. } continue;
  480. case OPCODE_CALL_RETURN:
  481. case OPCODE_CALL: {
  482. CHECK_SPACE(4);
  483. bool call_ret = _code_ptr[ip]==OPCODE_CALL_RETURN;
  484. int argc=_code_ptr[ip+1];
  485. GET_VARIANT_PTR(base,2);
  486. int nameg=_code_ptr[ip+3];
  487. ERR_BREAK(nameg<0 || nameg>=_global_names_count);
  488. const StringName *methodname = &_global_names_ptr[nameg];
  489. ERR_BREAK(argc<0);
  490. ip+=4;
  491. CHECK_SPACE(argc+1);
  492. Variant **argptrs = call_args;
  493. for(int i=0;i<argc;i++) {
  494. GET_VARIANT_PTR(v,i);
  495. argptrs[i]=v;
  496. }
  497. #ifdef DEBUG_ENABLED
  498. uint64_t call_time;
  499. if (GDScriptLanguage::get_singleton()->profiling) {
  500. call_time=OS::get_singleton()->get_ticks_usec();
  501. }
  502. #endif
  503. Variant::CallError err;
  504. if (call_ret) {
  505. GET_VARIANT_PTR(ret,argc);
  506. *ret = base->call(*methodname,(const Variant**)argptrs,argc,err);
  507. } else {
  508. base->call(*methodname,(const Variant**)argptrs,argc,err);
  509. }
  510. #ifdef DEBUG_ENABLED
  511. if (GDScriptLanguage::get_singleton()->profiling) {
  512. function_call_time+=OS::get_singleton()->get_ticks_usec() - call_time;
  513. }
  514. #endif
  515. if (err.error!=Variant::CallError::CALL_OK) {
  516. String methodstr = *methodname;
  517. String basestr = _get_var_type(base);
  518. if (methodstr=="call") {
  519. if (argc>=1) {
  520. methodstr=String(*argptrs[0])+" (via call)";
  521. if (err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  522. err.argument-=1;
  523. }
  524. }
  525. } if (methodstr=="free") {
  526. if (err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  527. if (base->is_ref()) {
  528. err_text="Attempted to free a reference.";
  529. break;
  530. } else if (base->get_type()==Variant::OBJECT) {
  531. err_text="Attempted to free a locked object (calling or emitting).";
  532. break;
  533. }
  534. }
  535. }
  536. err_text=_get_call_error(err,"function '"+methodstr+"' in base '"+basestr+"'",(const Variant**)argptrs);
  537. break;
  538. }
  539. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  540. ip+=argc+1;
  541. } continue;
  542. case OPCODE_CALL_BUILT_IN: {
  543. CHECK_SPACE(4);
  544. GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip+1]);
  545. int argc=_code_ptr[ip+2];
  546. ERR_BREAK(argc<0);
  547. ip+=3;
  548. CHECK_SPACE(argc+1);
  549. Variant **argptrs = call_args;
  550. for(int i=0;i<argc;i++) {
  551. GET_VARIANT_PTR(v,i);
  552. argptrs[i]=v;
  553. }
  554. GET_VARIANT_PTR(dst,argc);
  555. Variant::CallError err;
  556. GDFunctions::call(func,(const Variant**)argptrs,argc,*dst,err);
  557. if (err.error!=Variant::CallError::CALL_OK) {
  558. String methodstr = GDFunctions::get_func_name(func);
  559. err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
  560. break;
  561. }
  562. ip+=argc+1;
  563. } continue;
  564. case OPCODE_CALL_SELF: {
  565. } break;
  566. case OPCODE_CALL_SELF_BASE: {
  567. CHECK_SPACE(2);
  568. int self_fun = _code_ptr[ip+1];
  569. #ifdef DEBUG_ENABLED
  570. if (self_fun<0 || self_fun>=_global_names_count) {
  571. err_text="compiler bug, function name not found";
  572. break;
  573. }
  574. #endif
  575. const StringName *methodname = &_global_names_ptr[self_fun];
  576. int argc=_code_ptr[ip+2];
  577. CHECK_SPACE(2+argc+1);
  578. Variant **argptrs = call_args;
  579. for(int i=0;i<argc;i++) {
  580. GET_VARIANT_PTR(v,i+3);
  581. argptrs[i]=v;
  582. }
  583. GET_VARIANT_PTR(dst,argc+3);
  584. const GDScript *gds = _script;
  585. const Map<StringName,GDFunction*>::Element *E=NULL;
  586. while (gds->base.ptr()) {
  587. gds=gds->base.ptr();
  588. E=gds->member_functions.find(*methodname);
  589. if (E)
  590. break;
  591. }
  592. Variant::CallError err;
  593. if (E) {
  594. *dst=E->get()->call(p_instance,(const Variant**)argptrs,argc,err);
  595. } else if (gds->native.ptr()) {
  596. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  597. MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(),*methodname);
  598. if (!mb) {
  599. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  600. } else {
  601. *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
  602. }
  603. } else {
  604. err.error=Variant::CallError::CALL_OK;
  605. }
  606. } else {
  607. if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
  608. err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  609. } else {
  610. err.error=Variant::CallError::CALL_OK;
  611. }
  612. }
  613. if (err.error!=Variant::CallError::CALL_OK) {
  614. String methodstr = *methodname;
  615. err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
  616. break;
  617. }
  618. ip+=4+argc;
  619. } continue;
  620. case OPCODE_YIELD:
  621. case OPCODE_YIELD_SIGNAL: {
  622. int ipofs=1;
  623. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  624. CHECK_SPACE(4);
  625. ipofs+=2;
  626. } else {
  627. CHECK_SPACE(2);
  628. }
  629. Ref<GDFunctionState> gdfs = memnew( GDFunctionState );
  630. gdfs->function=this;
  631. gdfs->state.stack.resize(alloca_size);
  632. //copy variant stack
  633. for(int i=0;i<_stack_size;i++) {
  634. memnew_placement(&gdfs->state.stack[sizeof(Variant)*i],Variant(stack[i]));
  635. }
  636. gdfs->state.stack_size=_stack_size;
  637. gdfs->state.self=self;
  638. gdfs->state.alloca_size=alloca_size;
  639. gdfs->state._class=_class;
  640. gdfs->state.ip=ip+ipofs;
  641. gdfs->state.line=line;
  642. //gdfs->state.result_pos=ip+ipofs-1;
  643. gdfs->state.defarg=defarg;
  644. gdfs->state.instance=p_instance;
  645. gdfs->function=this;
  646. retvalue=gdfs;
  647. if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
  648. GET_VARIANT_PTR(argobj,1);
  649. GET_VARIANT_PTR(argname,2);
  650. //do the oneshot connect
  651. if (argobj->get_type()!=Variant::OBJECT) {
  652. err_text="First argument of yield() not of type object.";
  653. break;
  654. }
  655. if (argname->get_type()!=Variant::STRING) {
  656. err_text="Second argument of yield() not a string (for signal name).";
  657. break;
  658. }
  659. Object *obj=argobj->operator Object *();
  660. String signal = argname->operator String();
  661. #ifdef DEBUG_ENABLED
  662. if (!obj) {
  663. err_text="First argument of yield() is null.";
  664. break;
  665. }
  666. if (ScriptDebugger::get_singleton()) {
  667. if (!ObjectDB::instance_validate(obj)) {
  668. err_text="First argument of yield() is a previously freed instance.";
  669. break;
  670. }
  671. }
  672. if (signal.length()==0) {
  673. err_text="Second argument of yield() is an empty string (for signal name).";
  674. break;
  675. }
  676. #endif
  677. Error err = obj->connect(signal,gdfs.ptr(),"_signal_callback",varray(gdfs),Object::CONNECT_ONESHOT);
  678. if (err!=OK) {
  679. err_text="Error connecting to signal: "+signal+" during yield().";
  680. break;
  681. }
  682. }
  683. exit_ok=true;
  684. } break;
  685. case OPCODE_YIELD_RESUME: {
  686. CHECK_SPACE(2);
  687. if (!p_state) {
  688. err_text=("Invalid Resume (bug?)");
  689. break;
  690. }
  691. GET_VARIANT_PTR(result,1);
  692. *result=p_state->result;
  693. ip+=2;
  694. } continue;
  695. case OPCODE_JUMP: {
  696. CHECK_SPACE(2);
  697. int to = _code_ptr[ip+1];
  698. ERR_BREAK(to<0 || to>_code_size);
  699. ip=to;
  700. } continue;
  701. case OPCODE_JUMP_IF: {
  702. CHECK_SPACE(3);
  703. GET_VARIANT_PTR(test,1);
  704. bool valid;
  705. bool result = test->booleanize(valid);
  706. #ifdef DEBUG_ENABLED
  707. if (!valid) {
  708. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  709. break;
  710. }
  711. #endif
  712. if (result) {
  713. int to = _code_ptr[ip+2];
  714. ERR_BREAK(to<0 || to>_code_size);
  715. ip=to;
  716. continue;
  717. }
  718. ip+=3;
  719. } continue;
  720. case OPCODE_JUMP_IF_NOT: {
  721. CHECK_SPACE(3);
  722. GET_VARIANT_PTR(test,1);
  723. bool valid;
  724. bool result = test->booleanize(valid);
  725. #ifdef DEBUG_ENABLED
  726. if (!valid) {
  727. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  728. break;
  729. }
  730. #endif
  731. if (!result) {
  732. int to = _code_ptr[ip+2];
  733. ERR_BREAK(to<0 || to>_code_size);
  734. ip=to;
  735. continue;
  736. }
  737. ip+=3;
  738. } continue;
  739. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  740. CHECK_SPACE(2);
  741. ip=_default_arg_ptr[defarg];
  742. } continue;
  743. case OPCODE_RETURN: {
  744. CHECK_SPACE(2);
  745. GET_VARIANT_PTR(r,1);
  746. retvalue=*r;
  747. exit_ok=true;
  748. } break;
  749. case OPCODE_ITERATE_BEGIN: {
  750. CHECK_SPACE(8); //space for this an regular iterate
  751. GET_VARIANT_PTR(counter,1);
  752. GET_VARIANT_PTR(container,2);
  753. bool valid;
  754. if (!container->iter_init(*counter,valid)) {
  755. if (!valid) {
  756. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
  757. break;
  758. }
  759. int jumpto=_code_ptr[ip+3];
  760. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  761. ip=jumpto;
  762. continue;
  763. }
  764. GET_VARIANT_PTR(iterator,4);
  765. *iterator=container->iter_get(*counter,valid);
  766. if (!valid) {
  767. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
  768. break;
  769. }
  770. ip+=5; //skip regular iterate which is always next
  771. } continue;
  772. case OPCODE_ITERATE: {
  773. CHECK_SPACE(4);
  774. GET_VARIANT_PTR(counter,1);
  775. GET_VARIANT_PTR(container,2);
  776. bool valid;
  777. if (!container->iter_next(*counter,valid)) {
  778. if (!valid) {
  779. err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
  780. break;
  781. }
  782. int jumpto=_code_ptr[ip+3];
  783. ERR_BREAK(jumpto<0 || jumpto>_code_size);
  784. ip=jumpto;
  785. continue;
  786. }
  787. GET_VARIANT_PTR(iterator,4);
  788. *iterator=container->iter_get(*counter,valid);
  789. if (!valid) {
  790. err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
  791. break;
  792. }
  793. ip+=5; //loop again
  794. } continue;
  795. case OPCODE_ASSERT: {
  796. CHECK_SPACE(2);
  797. GET_VARIANT_PTR(test,1);
  798. #ifdef DEBUG_ENABLED
  799. bool valid;
  800. bool result = test->booleanize(valid);
  801. if (!valid) {
  802. err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
  803. break;
  804. }
  805. if (!result) {
  806. err_text="Assertion failed.";
  807. break;
  808. }
  809. #endif
  810. ip+=2;
  811. } continue;
  812. case OPCODE_BREAKPOINT: {
  813. #ifdef DEBUG_ENABLED
  814. if (ScriptDebugger::get_singleton()) {
  815. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement",true);
  816. }
  817. #endif
  818. ip+=1;
  819. } continue;
  820. case OPCODE_LINE: {
  821. CHECK_SPACE(2);
  822. line=_code_ptr[ip+1];
  823. ip+=2;
  824. if (ScriptDebugger::get_singleton()) {
  825. // line
  826. bool do_break=false;
  827. if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
  828. if (ScriptDebugger::get_singleton()->get_depth()<=0)
  829. ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
  830. if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
  831. do_break=true;
  832. }
  833. if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
  834. do_break=true;
  835. if (do_break) {
  836. GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
  837. }
  838. ScriptDebugger::get_singleton()->line_poll();
  839. }
  840. } continue;
  841. case OPCODE_END: {
  842. exit_ok=true;
  843. break;
  844. } break;
  845. default: {
  846. err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
  847. } break;
  848. }
  849. if (exit_ok)
  850. break;
  851. //error
  852. // function, file, line, error, explanation
  853. String err_file;
  854. if (p_instance)
  855. err_file=p_instance->script->path;
  856. else if (_class)
  857. err_file=_class->path;
  858. if (err_file=="")
  859. err_file="<built-in>";
  860. String err_func = name;
  861. if (p_instance && p_instance->script->name!="")
  862. err_func=p_instance->script->name+"."+err_func;
  863. int err_line=line;
  864. if (err_text=="") {
  865. err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
  866. }
  867. if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  868. // debugger break did not happen
  869. _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data(),ERR_HANDLER_SCRIPT);
  870. }
  871. break;
  872. }
  873. #ifdef DEBUG_ENABLED
  874. if (GDScriptLanguage::get_singleton()->profiling) {
  875. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  876. profile.total_time+=time_taken;
  877. profile.self_time+=time_taken-function_call_time;
  878. profile.frame_total_time+=time_taken;
  879. profile.frame_self_time+=time_taken-function_call_time;
  880. GDScriptLanguage::get_singleton()->script_frame_time+=time_taken-function_call_time;
  881. }
  882. #endif
  883. if (ScriptDebugger::get_singleton())
  884. GDScriptLanguage::get_singleton()->exit_function();
  885. if (_stack_size) {
  886. //free stack
  887. for(int i=0;i<_stack_size;i++)
  888. stack[i].~Variant();
  889. }
  890. return retvalue;
  891. }
  892. const int* GDFunction::get_code() const {
  893. return _code_ptr;
  894. }
  895. int GDFunction::get_code_size() const{
  896. return _code_size;
  897. }
  898. Variant GDFunction::get_constant(int p_idx) const {
  899. ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
  900. return constants[p_idx];
  901. }
  902. StringName GDFunction::get_global_name(int p_idx) const {
  903. ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
  904. return global_names[p_idx];
  905. }
  906. int GDFunction::get_default_argument_count() const {
  907. return default_arguments.size();
  908. }
  909. int GDFunction::get_default_argument_addr(int p_arg) const{
  910. ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
  911. return default_arguments[p_arg];
  912. }
  913. StringName GDFunction::get_name() const {
  914. return name;
  915. }
  916. int GDFunction::get_max_stack_size() const {
  917. return _stack_size;
  918. }
  919. struct _GDFKC {
  920. int order;
  921. List<int> pos;
  922. };
  923. struct _GDFKCS {
  924. int order;
  925. StringName id;
  926. int pos;
  927. bool operator<(const _GDFKCS &p_r) const {
  928. return order<p_r.order;
  929. }
  930. };
  931. void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
  932. int oc=0;
  933. Map<StringName,_GDFKC> sdmap;
  934. for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
  935. const StackDebug &sd=E->get();
  936. if (sd.line>p_line)
  937. break;
  938. if (sd.added) {
  939. if (!sdmap.has(sd.identifier)) {
  940. _GDFKC d;
  941. d.order=oc++;
  942. d.pos.push_back(sd.pos);
  943. sdmap[sd.identifier]=d;
  944. } else {
  945. sdmap[sd.identifier].pos.push_back(sd.pos);
  946. }
  947. } else {
  948. ERR_CONTINUE(!sdmap.has(sd.identifier));
  949. sdmap[sd.identifier].pos.pop_back();
  950. if (sdmap[sd.identifier].pos.empty())
  951. sdmap.erase(sd.identifier);
  952. }
  953. }
  954. List<_GDFKCS> stackpositions;
  955. for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
  956. _GDFKCS spp;
  957. spp.id=E->key();
  958. spp.order=E->get().order;
  959. spp.pos=E->get().pos.back()->get();
  960. stackpositions.push_back(spp);
  961. }
  962. stackpositions.sort();
  963. for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
  964. Pair<StringName,int> p;
  965. p.first=E->get().id;
  966. p.second=E->get().pos;
  967. r_stackvars->push_back(p);
  968. }
  969. }
  970. #if 0
  971. void GDFunction::clear() {
  972. name=StringName();
  973. constants.clear();
  974. _stack_size=0;
  975. code.clear();
  976. _constants_ptr=NULL;
  977. _constant_count=0;
  978. _global_names_ptr=NULL;
  979. _global_names_count=0;
  980. _code_ptr=NULL;
  981. _code_size=0;
  982. }
  983. #endif
  984. GDFunction::GDFunction() : function_list(this) {
  985. _stack_size=0;
  986. _call_size=0;
  987. name="<anonymous>";
  988. #ifdef DEBUG_ENABLED
  989. _func_cname=NULL;
  990. if (GDScriptLanguage::get_singleton()->lock) {
  991. GDScriptLanguage::get_singleton()->lock->lock();
  992. }
  993. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  994. if (GDScriptLanguage::get_singleton()->lock) {
  995. GDScriptLanguage::get_singleton()->lock->unlock();
  996. }
  997. profile.call_count=0;
  998. profile.self_time=0;
  999. profile.total_time=0;
  1000. profile.frame_call_count=0;
  1001. profile.frame_self_time=0;
  1002. profile.frame_total_time=0;
  1003. profile.last_frame_call_count=0;
  1004. profile.last_frame_self_time=0;
  1005. profile.last_frame_total_time=0;
  1006. #endif
  1007. }
  1008. GDFunction::~GDFunction() {
  1009. #ifdef DEBUG_ENABLED
  1010. if (GDScriptLanguage::get_singleton()->lock) {
  1011. GDScriptLanguage::get_singleton()->lock->lock();
  1012. }
  1013. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1014. if (GDScriptLanguage::get_singleton()->lock) {
  1015. GDScriptLanguage::get_singleton()->lock->unlock();
  1016. }
  1017. #endif
  1018. }
  1019. /////////////////////
  1020. Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  1021. Variant arg;
  1022. r_error.error=Variant::CallError::CALL_OK;
  1023. ERR_FAIL_COND_V(!function,Variant());
  1024. if (p_argcount==0) {
  1025. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1026. r_error.argument=1;
  1027. return Variant();
  1028. } else if (p_argcount==1) {
  1029. //noooneee
  1030. } else if (p_argcount==2) {
  1031. arg=*p_args[0];
  1032. } else {
  1033. Array extra_args;
  1034. for(int i=0;i<p_argcount-1;i++) {
  1035. extra_args.push_back(*p_args[i]);
  1036. }
  1037. arg=extra_args;
  1038. }
  1039. Ref<GDFunctionState> self = *p_args[p_argcount-1];
  1040. if (self.is_null()) {
  1041. r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1042. r_error.argument=p_argcount-1;
  1043. r_error.expected=Variant::OBJECT;
  1044. return Variant();
  1045. }
  1046. state.result=arg;
  1047. Variant ret = function->call(NULL,NULL,0,r_error,&state);
  1048. function=NULL; //cleaned up;
  1049. state.result=Variant();
  1050. return ret;
  1051. }
  1052. bool GDFunctionState::is_valid() const {
  1053. return function!=NULL;
  1054. }
  1055. Variant GDFunctionState::resume(const Variant& p_arg) {
  1056. ERR_FAIL_COND_V(!function,Variant());
  1057. state.result=p_arg;
  1058. Variant::CallError err;
  1059. Variant ret = function->call(NULL,NULL,0,err,&state);
  1060. function=NULL; //cleaned up;
  1061. state.result=Variant();
  1062. return ret;
  1063. }
  1064. void GDFunctionState::_bind_methods() {
  1065. ObjectTypeDB::bind_method(_MD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
  1066. ObjectTypeDB::bind_method(_MD("is_valid"),&GDFunctionState::is_valid);
  1067. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
  1068. }
  1069. GDFunctionState::GDFunctionState() {
  1070. function=NULL;
  1071. }
  1072. GDFunctionState::~GDFunctionState() {
  1073. if (function!=NULL) {
  1074. //never called, deinitialize stack
  1075. for(int i=0;i<state.stack_size;i++) {
  1076. Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
  1077. v->~Variant();
  1078. }
  1079. }
  1080. }
  1081. ///////////////////////////
  1082. GDNativeClass::GDNativeClass(const StringName& p_name) {
  1083. name=p_name;
  1084. }
  1085. /*void GDNativeClass::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount){
  1086. }*/
  1087. bool GDNativeClass::_get(const StringName& p_name,Variant &r_ret) const {
  1088. bool ok;
  1089. int v = ObjectTypeDB::get_integer_constant(name, p_name, &ok);
  1090. if (ok) {
  1091. r_ret=v;
  1092. return true;
  1093. } else {
  1094. return false;
  1095. }
  1096. }
  1097. void GDNativeClass::_bind_methods() {
  1098. ObjectTypeDB::bind_method(_MD("new"),&GDNativeClass::_new);
  1099. }
  1100. Variant GDNativeClass::_new() {
  1101. Object *o = instance();
  1102. if (!o) {
  1103. ERR_EXPLAIN("Class type: '"+String(name)+"' is not instantiable.");
  1104. ERR_FAIL_COND_V(!o,Variant());
  1105. }
  1106. Reference *ref = o->cast_to<Reference>();
  1107. if (ref) {
  1108. return REF(ref);
  1109. } else {
  1110. return o;
  1111. }
  1112. }
  1113. Object *GDNativeClass::instance() {
  1114. return ObjectTypeDB::instance(name);
  1115. }
  1116. GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError& r_error) {
  1117. /* STEP 1, CREATE */
  1118. GDInstance* instance = memnew( GDInstance );
  1119. instance->base_ref=p_isref;
  1120. instance->members.resize(member_indices.size());
  1121. instance->script=Ref<GDScript>(this);
  1122. instance->owner=p_owner;
  1123. instance->owner->set_script_instance(instance);
  1124. /* STEP 2, INITIALIZE AND CONSRTUCT */
  1125. instances.insert(instance->owner);
  1126. initializer->call(instance,p_args,p_argcount,r_error);
  1127. if (r_error.error!=Variant::CallError::CALL_OK) {
  1128. instance->script=Ref<GDScript>();
  1129. instance->owner->set_script_instance(NULL);
  1130. instances.erase(p_owner);
  1131. ERR_FAIL_COND_V(r_error.error!=Variant::CallError::CALL_OK, NULL); //error constructing
  1132. }
  1133. //@TODO make thread safe
  1134. return instance;
  1135. }
  1136. Variant GDScript::_new(const Variant** p_args,int p_argcount,Variant::CallError& r_error) {
  1137. /* STEP 1, CREATE */
  1138. if (!valid) {
  1139. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1140. return Variant();
  1141. }
  1142. r_error.error=Variant::CallError::CALL_OK;
  1143. REF ref;
  1144. Object *owner=NULL;
  1145. GDScript *_baseptr=this;
  1146. while (_baseptr->_base) {
  1147. _baseptr=_baseptr->_base;
  1148. }
  1149. if (_baseptr->native.ptr()) {
  1150. owner=_baseptr->native->instance();
  1151. } else {
  1152. owner=memnew( Reference ); //by default, no base means use reference
  1153. }
  1154. Reference *r=owner->cast_to<Reference>();
  1155. if (r) {
  1156. ref=REF(r);
  1157. }
  1158. GDInstance* instance = _create_instance(p_args,p_argcount,owner,r!=NULL,r_error);
  1159. if (!instance) {
  1160. if (ref.is_null()) {
  1161. memdelete(owner); //no owner, sorry
  1162. }
  1163. return Variant();
  1164. }
  1165. if (ref.is_valid()) {
  1166. return ref;
  1167. } else {
  1168. return owner;
  1169. }
  1170. }
  1171. bool GDScript::can_instance() const {
  1172. //return valid; //any script in GDscript can instance
  1173. return valid || (!tool && !ScriptServer::is_scripting_enabled());
  1174. }
  1175. StringName GDScript::get_instance_base_type() const {
  1176. if (native.is_valid())
  1177. return native->get_name();
  1178. if (base.is_valid())
  1179. return base->get_instance_base_type();
  1180. return StringName();
  1181. }
  1182. struct _GDScriptMemberSort {
  1183. int index;
  1184. StringName name;
  1185. _FORCE_INLINE_ bool operator<(const _GDScriptMemberSort& p_member) const { return index < p_member.index; }
  1186. };
  1187. #ifdef TOOLS_ENABLED
  1188. void GDScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  1189. placeholders.erase(p_placeholder);
  1190. }
  1191. /*
  1192. void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  1193. List<PropertyInfo> plist;
  1194. GDScript *scr=this;
  1195. Map<StringName,Variant> default_values;
  1196. while(scr) {
  1197. Vector<_GDScriptMemberSort> msort;
  1198. for(Map<StringName,PropertyInfo>::Element *E=scr->member_info.front();E;E=E->next()) {
  1199. _GDScriptMemberSort ms;
  1200. ERR_CONTINUE(!scr->member_indices.has(E->key()));
  1201. ms.index=scr->member_indices[E->key()].index;
  1202. ms.name=E->key();
  1203. msort.push_back(ms);
  1204. }
  1205. msort.sort();
  1206. msort.invert();
  1207. for(int i=0;i<msort.size();i++) {
  1208. plist.push_front(scr->member_info[msort[i].name]);
  1209. if (scr->member_default_values.has(msort[i].name))
  1210. default_values[msort[i].name]=scr->member_default_values[msort[i].name];
  1211. else {
  1212. Variant::CallError err;
  1213. default_values[msort[i].name]=Variant::construct(scr->member_info[msort[i].name].type,NULL,0,err);
  1214. }
  1215. }
  1216. scr=scr->_base;
  1217. }
  1218. p_placeholder->update(plist,default_values);
  1219. }*/
  1220. #endif
  1221. bool GDScript::get_property_default_value(const StringName& p_property, Variant &r_value) const {
  1222. #ifdef TOOLS_ENABLED
  1223. //for (const Map<StringName,Variant>::Element *I=member_default_values.front();I;I=I->next()) {
  1224. // print_line("\t"+String(String(I->key())+":"+String(I->get())));
  1225. //}
  1226. const Map<StringName,Variant>::Element *E=member_default_values_cache.find(p_property);
  1227. if (E) {
  1228. r_value=E->get();
  1229. return true;
  1230. }
  1231. if (base_cache.is_valid()) {
  1232. return base_cache->get_property_default_value(p_property,r_value);
  1233. }
  1234. #endif
  1235. return false;
  1236. }
  1237. ScriptInstance* GDScript::instance_create(Object *p_this) {
  1238. if (!tool && !ScriptServer::is_scripting_enabled()) {
  1239. #ifdef TOOLS_ENABLED
  1240. //instance a fake script for editing the values
  1241. //plist.invert();
  1242. /*print_line("CREATING PLACEHOLDER");
  1243. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1244. print_line(E->get().name);
  1245. }*/
  1246. PlaceHolderScriptInstance *si = memnew( PlaceHolderScriptInstance(GDScriptLanguage::get_singleton(),Ref<Script>(this),p_this) );
  1247. placeholders.insert(si);
  1248. //_update_placeholder(si);
  1249. _update_exports();
  1250. return si;
  1251. #else
  1252. return NULL;
  1253. #endif
  1254. }
  1255. GDScript *top=this;
  1256. while(top->_base)
  1257. top=top->_base;
  1258. if (top->native.is_valid()) {
  1259. if (!ObjectTypeDB::is_type(p_this->get_type_name(),top->native->get_name())) {
  1260. if (ScriptDebugger::get_singleton()) {
  1261. 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()+"'");
  1262. }
  1263. 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()+"'");
  1264. ERR_FAIL_V(NULL);
  1265. }
  1266. }
  1267. Variant::CallError unchecked_error;
  1268. return _create_instance(NULL,0,p_this,p_this->cast_to<Reference>(),unchecked_error);
  1269. }
  1270. bool GDScript::instance_has(const Object *p_this) const {
  1271. return instances.has((Object*)p_this);
  1272. }
  1273. bool GDScript::has_source_code() const {
  1274. return source!="";
  1275. }
  1276. String GDScript::get_source_code() const {
  1277. return source;
  1278. }
  1279. void GDScript::set_source_code(const String& p_code) {
  1280. if (source==p_code)
  1281. return;
  1282. source=p_code;
  1283. #ifdef TOOLS_ENABLED
  1284. source_changed_cache=true;
  1285. //print_line("SC CHANGED "+get_path());
  1286. #endif
  1287. }
  1288. #ifdef TOOLS_ENABLED
  1289. void GDScript::_update_exports_values(Map<StringName,Variant>& values, List<PropertyInfo> &propnames) {
  1290. if (base_cache.is_valid()) {
  1291. base_cache->_update_exports_values(values,propnames);
  1292. }
  1293. for(Map<StringName,Variant>::Element *E=member_default_values_cache.front();E;E=E->next()) {
  1294. values[E->key()]=E->get();
  1295. }
  1296. for (List<PropertyInfo>::Element *E=members_cache.front();E;E=E->next()) {
  1297. propnames.push_back(E->get());
  1298. }
  1299. }
  1300. #endif
  1301. bool GDScript::_update_exports() {
  1302. #ifdef TOOLS_ENABLED
  1303. bool changed=false;
  1304. if (source_changed_cache) {
  1305. //print_line("updating source for "+get_path());
  1306. source_changed_cache=false;
  1307. changed=true;
  1308. String basedir=path;
  1309. if (basedir=="")
  1310. basedir=get_path();
  1311. if (basedir!="")
  1312. basedir=basedir.get_base_dir();
  1313. GDParser parser;
  1314. Error err = parser.parse(source,basedir,true,path);
  1315. if (err==OK) {
  1316. const GDParser::Node* root = parser.get_parse_tree();
  1317. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,false);
  1318. const GDParser::ClassNode *c = static_cast<const GDParser::ClassNode*>(root);
  1319. if (base_cache.is_valid()) {
  1320. base_cache->inheriters_cache.erase(get_instance_ID());
  1321. base_cache=Ref<GDScript>();
  1322. }
  1323. if (c->extends_used && String(c->extends_file)!="" && String(c->extends_file) != get_path()) {
  1324. String path = c->extends_file;
  1325. if (path.is_rel_path()) {
  1326. String base = get_path();
  1327. if (base=="" || base.is_rel_path()) {
  1328. ERR_PRINT(("Could not resolve relative path for parent class: "+path).utf8().get_data());
  1329. } else {
  1330. path=base.get_base_dir().plus_file(path);
  1331. }
  1332. }
  1333. if (path!=get_path()) {
  1334. Ref<GDScript> bf = ResourceLoader::load(path);
  1335. if (bf.is_valid()) {
  1336. //print_line("parent is: "+bf->get_path());
  1337. base_cache=bf;
  1338. bf->inheriters_cache.insert(get_instance_ID());
  1339. //bf->_update_exports(p_instances,true,false);
  1340. }
  1341. } else {
  1342. ERR_PRINT(("Path extending itself in "+path).utf8().get_data());
  1343. }
  1344. }
  1345. members_cache.clear();;
  1346. member_default_values_cache.clear();
  1347. for(int i=0;i<c->variables.size();i++) {
  1348. if (c->variables[i]._export.type==Variant::NIL)
  1349. continue;
  1350. members_cache.push_back(c->variables[i]._export);
  1351. //print_line("found "+c->variables[i]._export.name);
  1352. member_default_values_cache[c->variables[i].identifier]=c->variables[i].default_value;
  1353. }
  1354. _signals.clear();
  1355. for(int i=0;i<c->_signals.size();i++) {
  1356. _signals[c->_signals[i].name]=c->_signals[i].arguments;
  1357. }
  1358. }
  1359. } else {
  1360. //print_line("unchaged is "+get_path());
  1361. }
  1362. if (base_cache.is_valid()) {
  1363. if (base_cache->_update_exports()) {
  1364. changed = true;
  1365. }
  1366. }
  1367. if (/*changed &&*/ placeholders.size()) { //hm :(
  1368. //print_line("updating placeholders for "+get_path());
  1369. //update placeholders if any
  1370. Map<StringName,Variant> values;
  1371. List<PropertyInfo> propnames;
  1372. _update_exports_values(values,propnames);
  1373. for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  1374. E->get()->update(propnames,values);
  1375. }
  1376. }
  1377. return changed;
  1378. #endif
  1379. return false;
  1380. }
  1381. void GDScript::update_exports() {
  1382. #ifdef TOOLS_ENABLED
  1383. _update_exports();
  1384. Set<ObjectID> copy=inheriters_cache; //might get modified
  1385. //print_line("update exports for "+get_path()+" ic: "+itos(copy.size()));
  1386. for(Set<ObjectID>::Element *E=copy.front();E;E=E->next()) {
  1387. Object *id=ObjectDB::get_instance(E->get());
  1388. if (!id)
  1389. continue;
  1390. GDScript *s=id->cast_to<GDScript>();
  1391. if (!s)
  1392. continue;
  1393. s->update_exports();
  1394. }
  1395. #endif
  1396. }
  1397. void GDScript::_set_subclass_path(Ref<GDScript>& p_sc,const String& p_path) {
  1398. p_sc->path=p_path;
  1399. for(Map<StringName,Ref<GDScript> >::Element *E=p_sc->subclasses.front();E;E=E->next()) {
  1400. _set_subclass_path(E->get(),p_path);
  1401. }
  1402. }
  1403. Error GDScript::reload() {
  1404. ERR_FAIL_COND_V(instances.size(),ERR_ALREADY_IN_USE);
  1405. String basedir=path;
  1406. if (basedir=="")
  1407. basedir=get_path();
  1408. if (basedir!="")
  1409. basedir=basedir.get_base_dir();
  1410. valid=false;
  1411. GDParser parser;
  1412. Error err = parser.parse(source,basedir,false,path);
  1413. if (err) {
  1414. if (ScriptDebugger::get_singleton()) {
  1415. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),parser.get_error_line(),"Parser Error: "+parser.get_error());
  1416. }
  1417. _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(),ERR_HANDLER_SCRIPT);
  1418. ERR_FAIL_V(ERR_PARSE_ERROR);
  1419. }
  1420. bool can_run = ScriptServer::is_scripting_enabled() || parser.is_tool_script();
  1421. GDCompiler compiler;
  1422. err = compiler.compile(&parser,this);
  1423. if (err) {
  1424. if (can_run) {
  1425. if (ScriptDebugger::get_singleton()) {
  1426. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),compiler.get_error_line(),"Parser Error: "+compiler.get_error());
  1427. }
  1428. _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(),ERR_HANDLER_SCRIPT);
  1429. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1430. } else {
  1431. return err;
  1432. }
  1433. }
  1434. valid=true;
  1435. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1436. _set_subclass_path(E->get(),path);
  1437. }
  1438. #ifdef TOOLS_ENABLED
  1439. /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  1440. _update_placeholder(E->get());
  1441. }*/
  1442. #endif
  1443. return OK;
  1444. }
  1445. String GDScript::get_node_type() const {
  1446. return ""; // ?
  1447. }
  1448. ScriptLanguage *GDScript::get_language() const {
  1449. return GDScriptLanguage::get_singleton();
  1450. }
  1451. Variant GDScript::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1452. GDScript *top=this;
  1453. while(top) {
  1454. Map<StringName,GDFunction*>::Element *E=top->member_functions.find(p_method);
  1455. if (E) {
  1456. if (!E->get()->is_static()) {
  1457. WARN_PRINT(String("Can't call non-static function: '"+String(p_method)+"' in script.").utf8().get_data());
  1458. }
  1459. return E->get()->call(NULL,p_args,p_argcount,r_error);
  1460. }
  1461. top=top->_base;
  1462. }
  1463. //none found, regular
  1464. return Script::call(p_method,p_args,p_argcount,r_error);
  1465. }
  1466. bool GDScript::_get(const StringName& p_name,Variant &r_ret) const {
  1467. {
  1468. const GDScript *top=this;
  1469. while(top) {
  1470. {
  1471. const Map<StringName,Variant>::Element *E=top->constants.find(p_name);
  1472. if (E) {
  1473. r_ret= E->get();
  1474. return true;
  1475. }
  1476. }
  1477. {
  1478. const Map<StringName,Ref<GDScript> >::Element *E=subclasses.find(p_name);
  1479. if (E) {
  1480. r_ret=E->get();
  1481. return true;
  1482. }
  1483. }
  1484. top=top->_base;
  1485. }
  1486. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1487. r_ret=get_source_code();
  1488. return true;
  1489. }
  1490. }
  1491. return false;
  1492. }
  1493. bool GDScript::_set(const StringName& p_name, const Variant& p_value) {
  1494. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  1495. set_source_code(p_value);
  1496. reload();
  1497. } else
  1498. return false;
  1499. return true;
  1500. }
  1501. void GDScript::_get_property_list(List<PropertyInfo> *p_properties) const {
  1502. p_properties->push_back( PropertyInfo(Variant::STRING,"script/source",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  1503. }
  1504. void GDScript::_bind_methods() {
  1505. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"new",&GDScript::_new,MethodInfo("new"));
  1506. ObjectTypeDB::bind_method(_MD("get_as_byte_code"),&GDScript::get_as_byte_code);
  1507. }
  1508. Vector<uint8_t> GDScript::get_as_byte_code() const {
  1509. GDTokenizerBuffer tokenizer;
  1510. return tokenizer.parse_code_string(source);
  1511. };
  1512. Error GDScript::load_byte_code(const String& p_path) {
  1513. Vector<uint8_t> bytecode;
  1514. if (p_path.ends_with("gde")) {
  1515. FileAccess *fa = FileAccess::open(p_path,FileAccess::READ);
  1516. ERR_FAIL_COND_V(!fa,ERR_CANT_OPEN);
  1517. FileAccessEncrypted *fae = memnew( FileAccessEncrypted );
  1518. ERR_FAIL_COND_V(!fae,ERR_CANT_OPEN);
  1519. Vector<uint8_t> key;
  1520. key.resize(32);
  1521. for(int i=0;i<key.size();i++) {
  1522. key[i]=script_encryption_key[i];
  1523. }
  1524. Error err = fae->open_and_parse(fa,key,FileAccessEncrypted::MODE_READ);
  1525. ERR_FAIL_COND_V(err,err);
  1526. bytecode.resize(fae->get_len());
  1527. fae->get_buffer(bytecode.ptr(),bytecode.size());
  1528. memdelete(fae);
  1529. } else {
  1530. bytecode = FileAccess::get_file_as_array(p_path);
  1531. }
  1532. ERR_FAIL_COND_V(bytecode.size()==0,ERR_PARSE_ERROR);
  1533. path=p_path;
  1534. String basedir=path;
  1535. if (basedir=="")
  1536. basedir=get_path();
  1537. if (basedir!="")
  1538. basedir=basedir.get_base_dir();
  1539. valid=false;
  1540. GDParser parser;
  1541. Error err = parser.parse_bytecode(bytecode,basedir,get_path());
  1542. if (err) {
  1543. _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(),ERR_HANDLER_SCRIPT);
  1544. ERR_FAIL_V(ERR_PARSE_ERROR);
  1545. }
  1546. GDCompiler compiler;
  1547. err = compiler.compile(&parser,this);
  1548. if (err) {
  1549. _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(),ERR_HANDLER_SCRIPT);
  1550. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  1551. }
  1552. valid=true;
  1553. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  1554. _set_subclass_path(E->get(),path);
  1555. }
  1556. return OK;
  1557. }
  1558. Error GDScript::load_source_code(const String& p_path) {
  1559. DVector<uint8_t> sourcef;
  1560. Error err;
  1561. FileAccess *f=FileAccess::open(p_path,FileAccess::READ,&err);
  1562. if (err) {
  1563. ERR_FAIL_COND_V(err,err);
  1564. }
  1565. int len = f->get_len();
  1566. sourcef.resize(len+1);
  1567. DVector<uint8_t>::Write w = sourcef.write();
  1568. int r = f->get_buffer(w.ptr(),len);
  1569. f->close();
  1570. memdelete(f);
  1571. ERR_FAIL_COND_V(r!=len,ERR_CANT_OPEN);
  1572. w[len]=0;
  1573. String s;
  1574. if (s.parse_utf8((const char*)w.ptr())) {
  1575. 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.");
  1576. ERR_FAIL_V(ERR_INVALID_DATA);
  1577. }
  1578. source=s;
  1579. #ifdef TOOLS_ENABLED
  1580. source_changed_cache=true;
  1581. #endif
  1582. //print_line("LSC :"+get_path());
  1583. path=p_path;
  1584. return OK;
  1585. }
  1586. const Map<StringName,GDFunction*>& GDScript::debug_get_member_functions() const {
  1587. return member_functions;
  1588. }
  1589. StringName GDScript::debug_get_member_by_index(int p_idx) const {
  1590. for(const Map<StringName,MemberInfo>::Element *E=member_indices.front();E;E=E->next()) {
  1591. if (E->get().index==p_idx)
  1592. return E->key();
  1593. }
  1594. return "<error>";
  1595. }
  1596. Ref<GDScript> GDScript::get_base() const {
  1597. return base;
  1598. }
  1599. bool GDScript::has_script_signal(const StringName& p_signal) const {
  1600. if (_signals.has(p_signal))
  1601. return true;
  1602. if (base.is_valid()) {
  1603. return base->has_script_signal(p_signal);
  1604. }
  1605. #ifdef TOOLS_ENABLED
  1606. else if (base_cache.is_valid()){
  1607. return base_cache->has_script_signal(p_signal);
  1608. }
  1609. #endif
  1610. return false;
  1611. }
  1612. void GDScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  1613. for(const Map<StringName,Vector<StringName> >::Element *E=_signals.front();E;E=E->next()) {
  1614. MethodInfo mi;
  1615. mi.name=E->key();
  1616. for(int i=0;i<E->get().size();i++) {
  1617. PropertyInfo arg;
  1618. arg.name=E->get()[i];
  1619. mi.arguments.push_back(arg);
  1620. }
  1621. r_signals->push_back(mi);
  1622. }
  1623. if (base.is_valid()) {
  1624. base->get_script_signal_list(r_signals);
  1625. }
  1626. #ifdef TOOLS_ENABLED
  1627. else if (base_cache.is_valid()){
  1628. base_cache->get_script_signal_list(r_signals);
  1629. }
  1630. #endif
  1631. }
  1632. GDScript::GDScript() {
  1633. _static_ref=this;
  1634. valid=false;
  1635. subclass_count=0;
  1636. initializer=NULL;
  1637. _base=NULL;
  1638. _owner=NULL;
  1639. tool=false;
  1640. #ifdef TOOLS_ENABLED
  1641. source_changed_cache=false;
  1642. #endif
  1643. }
  1644. GDScript::~GDScript() {
  1645. for (Map<StringName,GDFunction*>::Element *E=member_functions.front();E;E=E->next()) {
  1646. memdelete( E->get() );
  1647. }
  1648. }
  1649. //////////////////////////////
  1650. // INSTANCE //
  1651. //////////////////////////////
  1652. bool GDInstance::set(const StringName& p_name, const Variant& p_value) {
  1653. //member
  1654. {
  1655. const Map<StringName,GDScript::MemberInfo>::Element *E = script->member_indices.find(p_name);
  1656. if (E) {
  1657. if (E->get().setter) {
  1658. const Variant *val=&p_value;
  1659. Variant::CallError err;
  1660. call(E->get().setter,&val,1,err);
  1661. if (err.error==Variant::CallError::CALL_OK) {
  1662. return true; //function exists, call was successful
  1663. }
  1664. }
  1665. else
  1666. members[E->get().index] = p_value;
  1667. return true;
  1668. }
  1669. }
  1670. GDScript *sptr=script.ptr();
  1671. while(sptr) {
  1672. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
  1673. if (E) {
  1674. Variant name=p_name;
  1675. const Variant *args[2]={&name,&p_value};
  1676. Variant::CallError err;
  1677. Variant ret = E->get()->call(this,(const Variant**)args,2,err);
  1678. if (err.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::BOOL && ret.operator bool())
  1679. return true;
  1680. }
  1681. sptr = sptr->_base;
  1682. }
  1683. return false;
  1684. }
  1685. bool GDInstance::get(const StringName& p_name, Variant &r_ret) const {
  1686. const GDScript *sptr=script.ptr();
  1687. while(sptr) {
  1688. {
  1689. const Map<StringName,GDScript::MemberInfo>::Element *E = script->member_indices.find(p_name);
  1690. if (E) {
  1691. if (E->get().getter) {
  1692. Variant::CallError err;
  1693. r_ret=const_cast<GDInstance*>(this)->call(E->get().getter,NULL,0,err);
  1694. if (err.error==Variant::CallError::CALL_OK) {
  1695. return true;
  1696. }
  1697. }
  1698. r_ret=members[E->get().index];
  1699. return true; //index found
  1700. }
  1701. }
  1702. {
  1703. const Map<StringName,Variant>::Element *E = script->constants.find(p_name);
  1704. if (E) {
  1705. r_ret=E->get();
  1706. return true; //index found
  1707. }
  1708. }
  1709. {
  1710. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
  1711. if (E) {
  1712. Variant name=p_name;
  1713. const Variant *args[1]={&name};
  1714. Variant::CallError err;
  1715. Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
  1716. if (err.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  1717. r_ret=ret;
  1718. return true;
  1719. }
  1720. }
  1721. }
  1722. sptr = sptr->_base;
  1723. }
  1724. return false;
  1725. }
  1726. Variant::Type GDInstance::get_property_type(const StringName& p_name,bool *r_is_valid) const {
  1727. const GDScript *sptr=script.ptr();
  1728. while(sptr) {
  1729. if (sptr->member_info.has(p_name)) {
  1730. if (r_is_valid)
  1731. *r_is_valid=true;
  1732. return sptr->member_info[p_name].type;
  1733. }
  1734. sptr = sptr->_base;
  1735. }
  1736. if (r_is_valid)
  1737. *r_is_valid=false;
  1738. return Variant::NIL;
  1739. }
  1740. void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  1741. // exported members, not doen yet!
  1742. const GDScript *sptr=script.ptr();
  1743. List<PropertyInfo> props;
  1744. while(sptr) {
  1745. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
  1746. if (E) {
  1747. Variant::CallError err;
  1748. Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
  1749. if (err.error==Variant::CallError::CALL_OK) {
  1750. if (ret.get_type()!=Variant::ARRAY) {
  1751. ERR_EXPLAIN("Wrong type for _get_property list, must be an array of dictionaries.");
  1752. ERR_FAIL();
  1753. }
  1754. Array arr = ret;
  1755. for(int i=0;i<arr.size();i++) {
  1756. Dictionary d = arr[i];
  1757. ERR_CONTINUE(!d.has("name"));
  1758. ERR_CONTINUE(!d.has("type"));
  1759. PropertyInfo pinfo;
  1760. pinfo.type = Variant::Type( d["type"].operator int());
  1761. ERR_CONTINUE(pinfo.type<0 || pinfo.type>=Variant::VARIANT_MAX );
  1762. pinfo.name = d["name"];
  1763. ERR_CONTINUE(pinfo.name=="");
  1764. if (d.has("hint"))
  1765. pinfo.hint=PropertyHint(d["hint"].operator int());
  1766. if (d.has("hint_string"))
  1767. pinfo.hint_string=d["hint_string"];
  1768. if (d.has("usage"))
  1769. pinfo.usage=d["usage"];
  1770. props.push_back(pinfo);
  1771. }
  1772. }
  1773. }
  1774. //instance a fake script for editing the values
  1775. Vector<_GDScriptMemberSort> msort;
  1776. for(Map<StringName,PropertyInfo>::Element *E=sptr->member_info.front();E;E=E->next()) {
  1777. _GDScriptMemberSort ms;
  1778. ERR_CONTINUE(!sptr->member_indices.has(E->key()));
  1779. ms.index=sptr->member_indices[E->key()].index;
  1780. ms.name=E->key();
  1781. msort.push_back(ms);
  1782. }
  1783. msort.sort();
  1784. msort.invert();
  1785. for(int i=0;i<msort.size();i++) {
  1786. props.push_front(sptr->member_info[msort[i].name]);
  1787. }
  1788. #if 0
  1789. if (sptr->member_functions.has("_get_property_list")) {
  1790. Variant::CallError err;
  1791. GDFunction *f = const_cast<GDFunction*>(sptr->member_functions["_get_property_list"]);
  1792. Variant plv = f->call(const_cast<GDInstance*>(this),NULL,0,err);
  1793. if (plv.get_type()!=Variant::ARRAY) {
  1794. ERR_PRINT("_get_property_list: expected array returned");
  1795. } else {
  1796. Array pl=plv;
  1797. for(int i=0;i<pl.size();i++) {
  1798. Dictionary p = pl[i];
  1799. PropertyInfo pinfo;
  1800. if (!p.has("name")) {
  1801. ERR_PRINT("_get_property_list: expected 'name' key of type string.")
  1802. continue;
  1803. }
  1804. if (!p.has("type")) {
  1805. ERR_PRINT("_get_property_list: expected 'type' key of type integer.")
  1806. continue;
  1807. }
  1808. pinfo.name=p["name"];
  1809. pinfo.type=Variant::Type(int(p["type"]));
  1810. if (p.has("hint"))
  1811. pinfo.hint=PropertyHint(int(p["hint"]));
  1812. if (p.has("hint_string"))
  1813. pinfo.hint_string=p["hint_string"];
  1814. if (p.has("usage"))
  1815. pinfo.usage=p["usage"];
  1816. props.push_back(pinfo);
  1817. }
  1818. }
  1819. }
  1820. #endif
  1821. sptr = sptr->_base;
  1822. }
  1823. //props.invert();
  1824. for (List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  1825. p_properties->push_back(E->get());
  1826. }
  1827. }
  1828. void GDInstance::get_method_list(List<MethodInfo> *p_list) const {
  1829. const GDScript *sptr=script.ptr();
  1830. while(sptr) {
  1831. for (Map<StringName,GDFunction*>::Element *E = sptr->member_functions.front();E;E=E->next()) {
  1832. MethodInfo mi;
  1833. mi.name=E->key();
  1834. mi.flags|=METHOD_FLAG_FROM_SCRIPT;
  1835. for(int i=0;i<E->get()->get_argument_count();i++)
  1836. mi.arguments.push_back(PropertyInfo(Variant::NIL,"arg"+itos(i)));
  1837. p_list->push_back(mi);
  1838. }
  1839. sptr = sptr->_base;
  1840. }
  1841. }
  1842. bool GDInstance::has_method(const StringName& p_method) const {
  1843. const GDScript *sptr=script.ptr();
  1844. while(sptr) {
  1845. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  1846. if (E)
  1847. return true;
  1848. sptr = sptr->_base;
  1849. }
  1850. return false;
  1851. }
  1852. Variant GDInstance::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  1853. //printf("calling %ls:%i method %ls\n", script->get_path().c_str(), -1, String(p_method).c_str());
  1854. GDScript *sptr=script.ptr();
  1855. while(sptr) {
  1856. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  1857. if (E) {
  1858. return E->get()->call(this,p_args,p_argcount,r_error);
  1859. }
  1860. sptr = sptr->_base;
  1861. }
  1862. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1863. return Variant();
  1864. }
  1865. void GDInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1866. GDScript *sptr=script.ptr();
  1867. Variant::CallError ce;
  1868. while(sptr) {
  1869. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  1870. if (E) {
  1871. E->get()->call(this,p_args,p_argcount,ce);
  1872. }
  1873. sptr = sptr->_base;
  1874. }
  1875. }
  1876. void GDInstance::_ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount) {
  1877. if (sptr->_base)
  1878. _ml_call_reversed(sptr->_base,p_method,p_args,p_argcount);
  1879. Variant::CallError ce;
  1880. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  1881. if (E) {
  1882. E->get()->call(this,p_args,p_argcount,ce);
  1883. }
  1884. }
  1885. void GDInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  1886. if (script.ptr()) {
  1887. _ml_call_reversed(script.ptr(),p_method,p_args,p_argcount);
  1888. }
  1889. }
  1890. void GDInstance::notification(int p_notification) {
  1891. //notification is not virutal, it gets called at ALL levels just like in C.
  1892. Variant value=p_notification;
  1893. const Variant *args[1]={&value };
  1894. GDScript *sptr=script.ptr();
  1895. while(sptr) {
  1896. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
  1897. if (E) {
  1898. Variant::CallError err;
  1899. E->get()->call(this,args,1,err);
  1900. if (err.error!=Variant::CallError::CALL_OK) {
  1901. //print error about notification call
  1902. }
  1903. }
  1904. sptr = sptr->_base;
  1905. }
  1906. }
  1907. Ref<Script> GDInstance::get_script() const {
  1908. return script;
  1909. }
  1910. ScriptLanguage *GDInstance::get_language() {
  1911. return GDScriptLanguage::get_singleton();
  1912. }
  1913. GDInstance::GDInstance() {
  1914. owner=NULL;
  1915. base_ref=false;
  1916. }
  1917. GDInstance::~GDInstance() {
  1918. if (script.is_valid() && owner) {
  1919. script->instances.erase(owner);
  1920. }
  1921. }
  1922. /************* SCRIPT LANGUAGE **************/
  1923. /************* SCRIPT LANGUAGE **************/
  1924. /************* SCRIPT LANGUAGE **************/
  1925. /************* SCRIPT LANGUAGE **************/
  1926. /************* SCRIPT LANGUAGE **************/
  1927. GDScriptLanguage *GDScriptLanguage::singleton=NULL;
  1928. String GDScriptLanguage::get_name() const {
  1929. return "GDScript";
  1930. }
  1931. /* LANGUAGE FUNCTIONS */
  1932. void GDScriptLanguage::_add_global(const StringName& p_name,const Variant& p_value) {
  1933. if (globals.has(p_name)) {
  1934. //overwrite existing
  1935. global_array[globals[p_name]]=p_value;
  1936. return;
  1937. }
  1938. globals[p_name]=global_array.size();
  1939. global_array.push_back(p_value);
  1940. _global_array=global_array.ptr();
  1941. }
  1942. void GDScriptLanguage::add_global_constant(const StringName& p_variable,const Variant& p_value) {
  1943. _add_global(p_variable,p_value);
  1944. }
  1945. void GDScriptLanguage::init() {
  1946. //populate global constants
  1947. int gcc=GlobalConstants::get_global_constant_count();
  1948. for(int i=0;i<gcc;i++) {
  1949. _add_global(StaticCString::create(GlobalConstants::get_global_constant_name(i)),GlobalConstants::get_global_constant_value(i));
  1950. }
  1951. _add_global(StaticCString::create("PI"),Math_PI);
  1952. //populate native classes
  1953. List<StringName> class_list;
  1954. ObjectTypeDB::get_type_list(&class_list);
  1955. for(List<StringName>::Element *E=class_list.front();E;E=E->next()) {
  1956. StringName n = E->get();
  1957. String s = String(n);
  1958. if (s.begins_with("_"))
  1959. n=s.substr(1,s.length());
  1960. if (globals.has(n))
  1961. continue;
  1962. Ref<GDNativeClass> nc = memnew( GDNativeClass(E->get()) );
  1963. _add_global(n,nc);
  1964. }
  1965. //populate singletons
  1966. List<Globals::Singleton> singletons;
  1967. Globals::get_singleton()->get_singletons(&singletons);
  1968. for(List<Globals::Singleton>::Element *E=singletons.front();E;E=E->next()) {
  1969. _add_global(E->get().name,E->get().ptr);
  1970. }
  1971. }
  1972. String GDScriptLanguage::get_type() const {
  1973. return "GDScript";
  1974. }
  1975. String GDScriptLanguage::get_extension() const {
  1976. return "gd";
  1977. }
  1978. Error GDScriptLanguage::execute_file(const String& p_path) {
  1979. // ??
  1980. return OK;
  1981. }
  1982. void GDScriptLanguage::finish() {
  1983. }
  1984. void GDScriptLanguage::profiling_start() {
  1985. #ifdef DEBUG_ENABLED
  1986. if (lock) {
  1987. lock->lock();
  1988. }
  1989. SelfList<GDFunction> *elem=function_list.first();
  1990. while(elem) {
  1991. elem->self()->profile.call_count=0;
  1992. elem->self()->profile.self_time=0;
  1993. elem->self()->profile.total_time=0;
  1994. elem->self()->profile.frame_call_count=0;
  1995. elem->self()->profile.frame_self_time=0;
  1996. elem->self()->profile.frame_total_time=0;
  1997. elem->self()->profile.last_frame_call_count=0;
  1998. elem->self()->profile.last_frame_self_time=0;
  1999. elem->self()->profile.last_frame_total_time=0;
  2000. elem=elem->next();
  2001. }
  2002. profiling=true;
  2003. if (lock) {
  2004. lock->unlock();
  2005. }
  2006. #endif
  2007. }
  2008. void GDScriptLanguage::profiling_stop() {
  2009. #ifdef DEBUG_ENABLED
  2010. if (lock) {
  2011. lock->lock();
  2012. }
  2013. profiling=false;
  2014. if (lock) {
  2015. lock->unlock();
  2016. }
  2017. #endif
  2018. }
  2019. int GDScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max) {
  2020. int current=0;
  2021. #ifdef DEBUG_ENABLED
  2022. if (lock) {
  2023. lock->lock();
  2024. }
  2025. SelfList<GDFunction> *elem=function_list.first();
  2026. while(elem) {
  2027. if (current>=p_info_max)
  2028. break;
  2029. p_info_arr[current].call_count=elem->self()->profile.call_count;
  2030. p_info_arr[current].self_time=elem->self()->profile.self_time;
  2031. p_info_arr[current].total_time=elem->self()->profile.total_time;
  2032. p_info_arr[current].signature=elem->self()->profile.signature;
  2033. elem=elem->next();
  2034. current++;
  2035. }
  2036. if (lock) {
  2037. lock->unlock();
  2038. }
  2039. #endif
  2040. return current;
  2041. }
  2042. int GDScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max) {
  2043. int current=0;
  2044. #ifdef DEBUG_ENABLED
  2045. if (lock) {
  2046. lock->lock();
  2047. }
  2048. SelfList<GDFunction> *elem=function_list.first();
  2049. while(elem) {
  2050. if (current>=p_info_max)
  2051. break;
  2052. if (elem->self()->profile.last_frame_call_count>0) {
  2053. p_info_arr[current].call_count=elem->self()->profile.last_frame_call_count;
  2054. p_info_arr[current].self_time=elem->self()->profile.last_frame_self_time;
  2055. p_info_arr[current].total_time=elem->self()->profile.last_frame_total_time;
  2056. p_info_arr[current].signature=elem->self()->profile.signature;
  2057. //print_line(String(elem->self()->profile.signature)+": "+itos(elem->self()->profile.last_frame_call_count));
  2058. current++;
  2059. }
  2060. elem=elem->next();
  2061. }
  2062. if (lock) {
  2063. lock->unlock();
  2064. }
  2065. #endif
  2066. return current;
  2067. }
  2068. void GDScriptLanguage::frame() {
  2069. // print_line("calls: "+itos(calls));
  2070. calls=0;
  2071. #ifdef DEBUG_ENABLED
  2072. if (profiling) {
  2073. if (lock) {
  2074. lock->lock();
  2075. }
  2076. SelfList<GDFunction> *elem=function_list.first();
  2077. while(elem) {
  2078. elem->self()->profile.last_frame_call_count=elem->self()->profile.frame_call_count;
  2079. elem->self()->profile.last_frame_self_time=elem->self()->profile.frame_self_time;
  2080. elem->self()->profile.last_frame_total_time=elem->self()->profile.frame_total_time;
  2081. elem->self()->profile.frame_call_count=0;
  2082. elem->self()->profile.frame_self_time=0;
  2083. elem->self()->profile.frame_total_time=0;
  2084. elem=elem->next();
  2085. }
  2086. if (lock) {
  2087. lock->unlock();
  2088. }
  2089. }
  2090. #endif
  2091. }
  2092. /* EDITOR FUNCTIONS */
  2093. void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
  2094. static const char *_reserved_words[]={
  2095. // operators
  2096. "and",
  2097. "in",
  2098. "not",
  2099. "or",
  2100. // types and values
  2101. "false",
  2102. "float",
  2103. "int",
  2104. "null",
  2105. "PI",
  2106. "self",
  2107. "true",
  2108. // functions
  2109. "assert",
  2110. "breakpoint",
  2111. "class",
  2112. "extends",
  2113. "func",
  2114. "preload",
  2115. "setget",
  2116. "signal",
  2117. "tool",
  2118. "yield",
  2119. // var
  2120. "const",
  2121. "enum",
  2122. "export",
  2123. "onready",
  2124. "static",
  2125. "var",
  2126. // control flow
  2127. "break",
  2128. "continue",
  2129. "if",
  2130. "elif",
  2131. "else",
  2132. "for",
  2133. "pass",
  2134. "return",
  2135. "while",
  2136. 0};
  2137. const char **w=_reserved_words;
  2138. while (*w) {
  2139. p_words->push_back(*w);
  2140. w++;
  2141. }
  2142. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  2143. p_words->push_back(GDFunctions::get_func_name(GDFunctions::Function(i)));
  2144. }
  2145. }
  2146. GDScriptLanguage::GDScriptLanguage() {
  2147. calls=0;
  2148. ERR_FAIL_COND(singleton);
  2149. singleton=this;
  2150. strings._init = StaticCString::create("_init");
  2151. strings._notification = StaticCString::create("_notification");
  2152. strings._set= StaticCString::create("_set");
  2153. strings._get= StaticCString::create("_get");
  2154. strings._get_property_list= StaticCString::create("_get_property_list");
  2155. strings._script_source=StaticCString::create("script/source");
  2156. _debug_parse_err_line=-1;
  2157. _debug_parse_err_file="";
  2158. #ifdef NO_THREADS
  2159. lock=NULL;
  2160. #else
  2161. lock = Mutex::create();
  2162. #endif
  2163. profiling=false;
  2164. script_frame_time=0;
  2165. _debug_call_stack_pos=0;
  2166. int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
  2167. if (ScriptDebugger::get_singleton()) {
  2168. //debugging enabled!
  2169. _debug_max_call_stack = dmcs;
  2170. if (_debug_max_call_stack<1024)
  2171. _debug_max_call_stack=1024;
  2172. _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
  2173. } else {
  2174. _debug_max_call_stack=0;
  2175. _call_stack=NULL;
  2176. }
  2177. }
  2178. GDScriptLanguage::~GDScriptLanguage() {
  2179. if (lock) {
  2180. memdelete(lock);
  2181. lock=NULL;
  2182. }
  2183. if (_call_stack) {
  2184. memdelete_arr(_call_stack);
  2185. }
  2186. singleton=NULL;
  2187. }
  2188. /*************** RESOURCE ***************/
  2189. RES ResourceFormatLoaderGDScript::load(const String &p_path, const String& p_original_path, Error *r_error) {
  2190. if (r_error)
  2191. *r_error=ERR_FILE_CANT_OPEN;
  2192. GDScript *script = memnew( GDScript );
  2193. Ref<GDScript> scriptres(script);
  2194. if (p_path.ends_with(".gde") || p_path.ends_with(".gdc")) {
  2195. script->set_script_path(p_original_path); // script needs this.
  2196. script->set_path(p_original_path);
  2197. Error err = script->load_byte_code(p_path);
  2198. if (err!=OK) {
  2199. ERR_FAIL_COND_V(err!=OK, RES());
  2200. }
  2201. } else {
  2202. Error err = script->load_source_code(p_path);
  2203. if (err!=OK) {
  2204. ERR_FAIL_COND_V(err!=OK, RES());
  2205. }
  2206. script->set_script_path(p_original_path); // script needs this.
  2207. script->set_path(p_original_path);
  2208. //script->set_name(p_path.get_file());
  2209. script->reload();
  2210. }
  2211. if (r_error)
  2212. *r_error=OK;
  2213. return scriptres;
  2214. }
  2215. void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
  2216. p_extensions->push_back("gd");
  2217. p_extensions->push_back("gdc");
  2218. p_extensions->push_back("gde");
  2219. }
  2220. bool ResourceFormatLoaderGDScript::handles_type(const String& p_type) const {
  2221. return (p_type=="Script" || p_type=="GDScript");
  2222. }
  2223. String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
  2224. String el = p_path.extension().to_lower();
  2225. if (el=="gd" || el=="gdc" || el=="gde")
  2226. return "GDScript";
  2227. return "";
  2228. }
  2229. Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  2230. Ref<GDScript> sqscr = p_resource;
  2231. ERR_FAIL_COND_V(sqscr.is_null(),ERR_INVALID_PARAMETER);
  2232. String source = sqscr->get_source_code();
  2233. Error err;
  2234. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  2235. if (err) {
  2236. ERR_FAIL_COND_V(err,err);
  2237. }
  2238. file->store_string(source);
  2239. if (file->get_error()!=OK && file->get_error()!=ERR_FILE_EOF) {
  2240. memdelete(file);
  2241. return ERR_CANT_CREATE;
  2242. }
  2243. file->close();
  2244. memdelete(file);
  2245. return OK;
  2246. }
  2247. void ResourceFormatSaverGDScript::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  2248. if (p_resource->cast_to<GDScript>()) {
  2249. p_extensions->push_back("gd");
  2250. }
  2251. }
  2252. bool ResourceFormatSaverGDScript::recognize(const RES& p_resource) const {
  2253. return p_resource->cast_to<GDScript>()!=NULL;
  2254. }