as_scriptengine.cpp 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. // Modified by Lasse Öörni for Urho3D
  24. //
  25. // as_scriptengine.cpp
  26. //
  27. // The implementation of the script engine interface
  28. //
  29. #include <stdlib.h>
  30. #include "as_config.h"
  31. #include "as_scriptengine.h"
  32. #include "as_builder.h"
  33. #include "as_context.h"
  34. #include "as_string_util.h"
  35. #include "as_tokenizer.h"
  36. #include "as_texts.h"
  37. #include "as_module.h"
  38. #include "as_callfunc.h"
  39. #include "as_generic.h"
  40. #include "as_scriptobject.h"
  41. #include "as_compiler.h"
  42. BEGIN_AS_NAMESPACE
  43. extern "C"
  44. {
  45. AS_API const char * asGetLibraryVersion()
  46. {
  47. #ifdef _DEBUG
  48. return ANGELSCRIPT_VERSION_STRING " DEBUG";
  49. #else
  50. return ANGELSCRIPT_VERSION_STRING;
  51. #endif
  52. }
  53. AS_API const char * asGetLibraryOptions()
  54. {
  55. const char *string = " "
  56. // Options
  57. #ifdef AS_MAX_PORTABILITY
  58. "AS_MAX_PORTABILITY "
  59. #endif
  60. #ifdef AS_DEBUG
  61. "AS_DEBUG "
  62. #endif
  63. #ifdef AS_NO_CLASS_METHODS
  64. "AS_NO_CLASS_METHODS "
  65. #endif
  66. #ifdef AS_USE_DOUBLE_AS_FLOAT
  67. "AS_USE_DOUBLE_AS_FLOAT "
  68. #endif
  69. #ifdef AS_64BIT_PTR
  70. "AS_64BIT_PTR "
  71. #endif
  72. #ifdef AS_NO_THREADS
  73. "AS_NO_THREADS "
  74. #endif
  75. #ifdef AS_NO_ATOMIC
  76. "AS_NO_ATOMIC "
  77. #endif
  78. // Target system
  79. #ifdef AS_WIN
  80. "AS_WIN "
  81. #endif
  82. #ifdef AS_LINUX
  83. "AS_LINUX "
  84. #endif
  85. #ifdef AS_MAC
  86. "AS_MAC "
  87. #endif
  88. #ifdef AS_BSD
  89. "AS_BSD "
  90. #endif
  91. #ifdef AS_XBOX
  92. "AS_XBOX "
  93. #endif
  94. #ifdef AS_XBOX360
  95. "AS_XBOX360 "
  96. #endif
  97. #ifdef AS_PSP
  98. "AS_PSP "
  99. #endif
  100. #ifdef AS_PS2
  101. "AS_PS2 "
  102. #endif
  103. #ifdef AS_PS3
  104. "AS_PS3 "
  105. #endif
  106. #ifdef AS_DC
  107. "AS_DC "
  108. #endif
  109. #ifdef AS_GC
  110. "AS_GC "
  111. #endif
  112. #ifdef AS_WII
  113. "AS_WII "
  114. #endif
  115. #ifdef AS_IPHONE
  116. "AS_IPHONE "
  117. #endif
  118. #ifdef AS_ANDROID
  119. "AS_ANDROID "
  120. #endif
  121. #ifdef AS_HAIKU
  122. "AS_HAIKU "
  123. #endif
  124. #ifdef AS_ILLUMOS
  125. "AS_ILLUMOS "
  126. #endif
  127. // CPU family
  128. #ifdef AS_PPC
  129. "AS_PPC "
  130. #endif
  131. #ifdef AS_PPC_64
  132. "AS_PPC_64 "
  133. #endif
  134. #ifdef AS_X86
  135. "AS_X86 "
  136. #endif
  137. #ifdef AS_MIPS
  138. "AS_MIPS "
  139. #endif
  140. #ifdef AS_SH4
  141. "AS_SH4 "
  142. #endif
  143. #ifdef AS_XENON
  144. "AS_XENON "
  145. #endif
  146. #ifdef AS_ARM
  147. "AS_ARM "
  148. #endif
  149. #ifdef AS_X64_GCC
  150. "AS_X64_GCC "
  151. #endif
  152. #ifdef AS_X64_MSVC
  153. "AS_X64_MSVC "
  154. #endif
  155. ;
  156. return string;
  157. }
  158. AS_API asIScriptEngine *asCreateScriptEngine(asDWORD version)
  159. {
  160. // Verify the version that the application expects
  161. if( (version/10000) != (ANGELSCRIPT_VERSION/10000) )
  162. return 0;
  163. if( (version/100)%100 != (ANGELSCRIPT_VERSION/100)%100 )
  164. return 0;
  165. if( (version%100) > (ANGELSCRIPT_VERSION%100) )
  166. return 0;
  167. // Verify the size of the types
  168. asASSERT( sizeof(asBYTE) == 1 );
  169. asASSERT( sizeof(asWORD) == 2 );
  170. asASSERT( sizeof(asDWORD) == 4 );
  171. asASSERT( sizeof(asQWORD) == 8 );
  172. asASSERT( sizeof(asPWORD) == sizeof(void*) );
  173. // Verify the boolean type
  174. asASSERT( sizeof(bool) == AS_SIZEOF_BOOL );
  175. asASSERT( true == VALUE_OF_BOOLEAN_TRUE );
  176. // Verify endianess
  177. #ifdef AS_BIG_ENDIAN
  178. asASSERT( *(asDWORD*)"\x00\x01\x02\x03" == 0x00010203 );
  179. asASSERT( *(asQWORD*)"\x00\x01\x02\x03\x04\x05\x06\x07" == I64(0x0001020304050607) );
  180. #else
  181. asASSERT( *(asDWORD*)"\x00\x01\x02\x03" == 0x03020100 );
  182. asASSERT( *(asQWORD*)"\x00\x01\x02\x03\x04\x05\x06\x07" == I64(0x0706050403020100) );
  183. #endif
  184. return asNEW(asCScriptEngine)();
  185. }
  186. int asCScriptEngine::SetEngineProperty(asEEngineProp property, asPWORD value)
  187. {
  188. switch( property )
  189. {
  190. case asEP_ALLOW_UNSAFE_REFERENCES:
  191. ep.allowUnsafeReferences = value ? true : false;
  192. break;
  193. case asEP_OPTIMIZE_BYTECODE:
  194. ep.optimizeByteCode = value ? true : false;
  195. break;
  196. case asEP_COPY_SCRIPT_SECTIONS:
  197. ep.copyScriptSections = value ? true : false;
  198. break;
  199. case asEP_MAX_STACK_SIZE:
  200. // The size is given in bytes, but we only store dwords
  201. ep.maximumContextStackSize = (int)value/4;
  202. if( initialContextStackSize > ep.maximumContextStackSize )
  203. initialContextStackSize = ep.maximumContextStackSize;
  204. break;
  205. case asEP_USE_CHARACTER_LITERALS:
  206. ep.useCharacterLiterals = value ? true : false;
  207. break;
  208. case asEP_ALLOW_MULTILINE_STRINGS:
  209. ep.allowMultilineStrings = value ? true : false;
  210. break;
  211. case asEP_ALLOW_IMPLICIT_HANDLE_TYPES:
  212. ep.allowImplicitHandleTypes = value ? true : false;
  213. break;
  214. case asEP_BUILD_WITHOUT_LINE_CUES:
  215. ep.buildWithoutLineCues = value ? true : false;
  216. break;
  217. case asEP_INIT_GLOBAL_VARS_AFTER_BUILD:
  218. ep.initGlobalVarsAfterBuild = value ? true : false;
  219. break;
  220. case asEP_REQUIRE_ENUM_SCOPE:
  221. ep.requireEnumScope = value ? true : false;
  222. break;
  223. case asEP_SCRIPT_SCANNER:
  224. if( value <= 1 )
  225. ep.scanner = (int)value;
  226. else
  227. return asINVALID_ARG;
  228. break;
  229. case asEP_INCLUDE_JIT_INSTRUCTIONS:
  230. ep.includeJitInstructions = value ? true : false;
  231. break;
  232. case asEP_STRING_ENCODING:
  233. if( value <= 1 )
  234. ep.stringEncoding = (int)value;
  235. else
  236. return asINVALID_ARG;
  237. break;
  238. case asEP_PROPERTY_ACCESSOR_MODE:
  239. if( value <= 2 )
  240. ep.propertyAccessorMode = (int)value;
  241. else
  242. return asINVALID_ARG;
  243. break;
  244. case asEP_EXPAND_DEF_ARRAY_TO_TMPL:
  245. ep.expandDefaultArrayToTemplate = value ? true : false;
  246. break;
  247. case asEP_AUTO_GARBAGE_COLLECT:
  248. ep.autoGarbageCollect = value ? true : false;
  249. break;
  250. case asEP_DISALLOW_GLOBAL_VARS:
  251. ep.disallowGlobalVars = value ? true : false;
  252. break;
  253. default:
  254. return asINVALID_ARG;
  255. }
  256. return asSUCCESS;
  257. }
  258. asPWORD asCScriptEngine::GetEngineProperty(asEEngineProp property) const
  259. {
  260. switch( property )
  261. {
  262. case asEP_ALLOW_UNSAFE_REFERENCES:
  263. return ep.allowUnsafeReferences;
  264. case asEP_OPTIMIZE_BYTECODE:
  265. return ep.optimizeByteCode;
  266. case asEP_COPY_SCRIPT_SECTIONS:
  267. return ep.copyScriptSections;
  268. case asEP_MAX_STACK_SIZE:
  269. return ep.maximumContextStackSize*4;
  270. case asEP_USE_CHARACTER_LITERALS:
  271. return ep.useCharacterLiterals;
  272. case asEP_ALLOW_MULTILINE_STRINGS:
  273. return ep.allowMultilineStrings;
  274. case asEP_ALLOW_IMPLICIT_HANDLE_TYPES:
  275. return ep.allowImplicitHandleTypes;
  276. case asEP_BUILD_WITHOUT_LINE_CUES:
  277. return ep.buildWithoutLineCues;
  278. case asEP_INIT_GLOBAL_VARS_AFTER_BUILD:
  279. return ep.initGlobalVarsAfterBuild;
  280. case asEP_REQUIRE_ENUM_SCOPE:
  281. return ep.requireEnumScope;
  282. case asEP_SCRIPT_SCANNER:
  283. return ep.scanner;
  284. case asEP_INCLUDE_JIT_INSTRUCTIONS:
  285. return ep.includeJitInstructions;
  286. case asEP_STRING_ENCODING:
  287. return ep.stringEncoding;
  288. case asEP_PROPERTY_ACCESSOR_MODE:
  289. return ep.propertyAccessorMode;
  290. case asEP_EXPAND_DEF_ARRAY_TO_TMPL:
  291. return ep.expandDefaultArrayToTemplate;
  292. case asEP_AUTO_GARBAGE_COLLECT:
  293. return ep.autoGarbageCollect;
  294. case asEP_DISALLOW_GLOBAL_VARS:
  295. return ep.disallowGlobalVars;
  296. }
  297. return 0;
  298. }
  299. } // extern "C"
  300. asCScriptEngine::asCScriptEngine()
  301. {
  302. // Instanciate the thread manager
  303. ENTERCRITICALSECTION(engineCritical);
  304. if( threadManager == 0 )
  305. threadManager = asNEW(asCThreadManager);
  306. else
  307. threadManager->AddRef();
  308. LEAVECRITICALSECTION(engineCritical);
  309. // Engine properties
  310. {
  311. ep.allowUnsafeReferences = false;
  312. ep.optimizeByteCode = true;
  313. ep.copyScriptSections = true;
  314. ep.maximumContextStackSize = 0; // no limit
  315. ep.useCharacterLiterals = false;
  316. ep.allowMultilineStrings = false;
  317. ep.allowImplicitHandleTypes = false;
  318. // TODO: optimize: Maybe this should be turned off by default? If a debugger is not used
  319. // then this is just slowing down the execution. The exception handler
  320. // should still be able to determine the line number from the bytecode
  321. // position.
  322. ep.buildWithoutLineCues = false;
  323. ep.initGlobalVarsAfterBuild = true;
  324. ep.requireEnumScope = false;
  325. ep.scanner = 1; // utf8. 0 = ascii
  326. ep.includeJitInstructions = false;
  327. ep.stringEncoding = 0; // utf8. 1 = utf16
  328. ep.propertyAccessorMode = 2; // 0 = disable, 1 = app registered only, 2 = app and script created
  329. ep.expandDefaultArrayToTemplate = false;
  330. ep.autoGarbageCollect = true;
  331. ep.disallowGlobalVars = false;
  332. }
  333. gc.engine = this;
  334. refCount.set(1);
  335. stringFactory = 0;
  336. configFailed = false;
  337. isPrepared = false;
  338. isBuilding = false;
  339. lastModule = 0;
  340. // User data
  341. userData = 0;
  342. cleanEngineFunc = 0;
  343. cleanModuleFunc = 0;
  344. cleanContextFunc = 0;
  345. cleanFunctionFunc = 0;
  346. cleanObjectTypeFunc = 0;
  347. initialContextStackSize = 1024; // 4 KB (1024 * sizeof(asDWORD)
  348. typeIdSeqNbr = 0;
  349. currentGroup = &defaultGroup;
  350. defaultAccessMask = 1;
  351. msgCallback = 0;
  352. jitCompiler = 0;
  353. // Reserve function id 0 for no function
  354. scriptFunctions.PushLast(0);
  355. // Make sure typeId for the built-in primitives are defined according to asETypeIdFlags
  356. int id;
  357. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttVoid, false)); asASSERT( id == asTYPEID_VOID );
  358. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttBool, false)); asASSERT( id == asTYPEID_BOOL );
  359. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt8, false)); asASSERT( id == asTYPEID_INT8 );
  360. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt16, false)); asASSERT( id == asTYPEID_INT16 );
  361. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt, false)); asASSERT( id == asTYPEID_INT32 );
  362. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt64, false)); asASSERT( id == asTYPEID_INT64 );
  363. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt8, false)); asASSERT( id == asTYPEID_UINT8 );
  364. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt16, false)); asASSERT( id == asTYPEID_UINT16 );
  365. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt, false)); asASSERT( id == asTYPEID_UINT32 );
  366. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt64, false)); asASSERT( id == asTYPEID_UINT64 );
  367. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttFloat, false)); asASSERT( id == asTYPEID_FLOAT );
  368. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttDouble, false)); asASSERT( id == asTYPEID_DOUBLE );
  369. defaultArrayObjectType = 0;
  370. RegisterScriptObject(this);
  371. RegisterScriptFunction(this);
  372. RegisterObjectTypeGCBehaviours(this);
  373. asCGlobalProperty::RegisterGCBehaviours(this);
  374. }
  375. asCScriptEngine::~asCScriptEngine()
  376. {
  377. asASSERT(refCount.get() == 0);
  378. asUINT n;
  379. // The modules must be deleted first, as they may use
  380. // object types from the config groups
  381. for( n = (asUINT)scriptModules.GetLength(); n-- > 0; )
  382. {
  383. if( scriptModules[n] )
  384. {
  385. asDELETE(scriptModules[n],asCModule);
  386. }
  387. }
  388. scriptModules.SetLength(0);
  389. GarbageCollect(asGC_FULL_CYCLE);
  390. // Delete the functions for template types that may references object types
  391. for( n = 0; n < templateTypes.GetLength(); n++ )
  392. {
  393. if( templateTypes[n] )
  394. {
  395. asUINT f;
  396. // Delete the factory stubs first
  397. for( f = 0; f < templateTypes[n]->beh.factories.GetLength(); f++ )
  398. {
  399. scriptFunctions[templateTypes[n]->beh.factories[f]]->Release();
  400. }
  401. templateTypes[n]->beh.factories.Allocate(0, false);
  402. // The list factory is not stored in the list with the rest of the factories
  403. if( templateTypes[n]->beh.listFactory )
  404. {
  405. scriptFunctions[templateTypes[n]->beh.listFactory]->Release();
  406. templateTypes[n]->beh.listFactory = 0;
  407. }
  408. // Delete the specialized functions
  409. for( f = 1; f < templateTypes[n]->beh.operators.GetLength(); f += 2 )
  410. {
  411. if( scriptFunctions[templateTypes[n]->beh.operators[f]]->objectType == templateTypes[n] )
  412. {
  413. scriptFunctions[templateTypes[n]->beh.operators[f]]->Release();
  414. templateTypes[n]->beh.operators[f] = 0;
  415. }
  416. }
  417. for( f = 0; f < templateTypes[n]->methods.GetLength(); f++ )
  418. {
  419. if( scriptFunctions[templateTypes[n]->methods[f]]->objectType == templateTypes[n] )
  420. {
  421. scriptFunctions[templateTypes[n]->methods[f]]->Release();
  422. templateTypes[n]->methods[f] = 0;
  423. }
  424. }
  425. }
  426. }
  427. // Do one more garbage collect to free gc objects that were global variables
  428. GarbageCollect(asGC_FULL_CYCLE);
  429. FreeUnusedGlobalProperties();
  430. ClearUnusedTypes();
  431. // Break all relationship between remaining class types and functions
  432. for( n = 0; n < classTypes.GetLength(); n++ )
  433. {
  434. if( classTypes[n] )
  435. classTypes[n]->ReleaseAllFunctions();
  436. if( classTypes[n]->derivedFrom )
  437. {
  438. classTypes[n]->derivedFrom->Release();
  439. classTypes[n]->derivedFrom = 0;
  440. }
  441. }
  442. GarbageCollect(asGC_FULL_CYCLE);
  443. FreeUnusedGlobalProperties();
  444. ClearUnusedTypes();
  445. // There may be instances where one more gc cycle must be run
  446. GarbageCollect(asGC_FULL_CYCLE);
  447. ClearUnusedTypes();
  448. // If the application hasn't registered GC behaviours for all types
  449. // that can form circular references with script types, then there
  450. // may still be objects in the GC.
  451. gc.ReportUndestroyedObjects();
  452. asSMapNode<int,asCDataType*> *cursor = 0;
  453. while( mapTypeIdToDataType.MoveFirst(&cursor) )
  454. {
  455. asDELETE(mapTypeIdToDataType.GetValue(cursor),asCDataType);
  456. mapTypeIdToDataType.Erase(cursor);
  457. }
  458. // First remove what is not used, so that other groups can be deleted safely
  459. defaultGroup.RemoveConfiguration(this, true);
  460. while( configGroups.GetLength() )
  461. {
  462. // Delete config groups in the right order
  463. asCConfigGroup *grp = configGroups.PopLast();
  464. if( grp )
  465. {
  466. grp->RemoveConfiguration(this);
  467. asDELETE(grp,asCConfigGroup);
  468. }
  469. }
  470. // Remove what is remaining
  471. defaultGroup.RemoveConfiguration(this);
  472. for( n = 0; n < registeredGlobalProps.GetLength(); n++ )
  473. {
  474. if( registeredGlobalProps[n] )
  475. registeredGlobalProps[n]->Release();
  476. }
  477. registeredGlobalProps.SetLength(0);
  478. FreeUnusedGlobalProperties();
  479. for( n = 0; n < templateTypes.GetLength(); n++ )
  480. {
  481. if( templateTypes[n] )
  482. {
  483. // Clear the sub type before deleting the template type so that the sub type isn't freed to soon
  484. templateTypes[n]->templateSubType = asCDataType::CreateNullHandle();
  485. asDELETE(templateTypes[n],asCObjectType);
  486. }
  487. }
  488. templateTypes.SetLength(0);
  489. for( n = 0; n < objectTypes.GetLength(); n++ )
  490. {
  491. if( objectTypes[n] )
  492. {
  493. // Clear the sub type before deleting the template type so that the sub type isn't freed to soon
  494. objectTypes[n]->templateSubType = asCDataType::CreateNullHandle();
  495. asDELETE(objectTypes[n],asCObjectType);
  496. }
  497. }
  498. objectTypes.SetLength(0);
  499. for( n = 0; n < templateSubTypes.GetLength(); n++ )
  500. {
  501. if( templateSubTypes[n] )
  502. {
  503. asDELETE(templateSubTypes[n], asCObjectType);
  504. }
  505. }
  506. templateSubTypes.SetLength(0);
  507. registeredTypeDefs.SetLength(0);
  508. registeredEnums.SetLength(0);
  509. registeredObjTypes.SetLength(0);
  510. for( n = 0; n < registeredGlobalFuncs.GetLength(); n++ )
  511. {
  512. if( registeredGlobalFuncs[n] )
  513. registeredGlobalFuncs[n]->Release();
  514. }
  515. registeredGlobalFuncs.SetLength(0);
  516. scriptTypeBehaviours.ReleaseAllFunctions();
  517. functionBehaviours.ReleaseAllFunctions();
  518. objectTypeBehaviours.ReleaseAllFunctions();
  519. globalPropertyBehaviours.ReleaseAllFunctions();
  520. // Free string constants
  521. for( n = 0; n < stringConstants.GetLength(); n++ )
  522. {
  523. asDELETE(stringConstants[n],asCString);
  524. }
  525. stringConstants.SetLength(0);
  526. // Free the script section names
  527. for( n = 0; n < scriptSectionNames.GetLength(); n++ )
  528. {
  529. asDELETE(scriptSectionNames[n],asCString);
  530. }
  531. scriptSectionNames.SetLength(0);
  532. // Clean the user data
  533. if( userData && cleanEngineFunc )
  534. cleanEngineFunc(this);
  535. // Release the thread manager
  536. threadManager->Release();
  537. }
  538. // interface
  539. int asCScriptEngine::AddRef() const
  540. {
  541. return refCount.atomicInc();
  542. }
  543. // interface
  544. int asCScriptEngine::Release() const
  545. {
  546. int r = refCount.atomicDec();
  547. if( r == 0 )
  548. {
  549. asDELETE(const_cast<asCScriptEngine*>(this),asCScriptEngine);
  550. return 0;
  551. }
  552. return r;
  553. }
  554. // interface
  555. void *asCScriptEngine::SetUserData(void *data)
  556. {
  557. void *old = userData;
  558. userData = data;
  559. return old;
  560. }
  561. // interface
  562. void *asCScriptEngine::GetUserData() const
  563. {
  564. return userData;
  565. }
  566. // interface
  567. int asCScriptEngine::SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv)
  568. {
  569. msgCallback = true;
  570. msgCallbackObj = obj;
  571. bool isObj = false;
  572. if( (unsigned)callConv == asCALL_GENERIC )
  573. {
  574. msgCallback = false;
  575. return asNOT_SUPPORTED;
  576. }
  577. if( (unsigned)callConv >= asCALL_THISCALL )
  578. {
  579. isObj = true;
  580. if( obj == 0 )
  581. {
  582. msgCallback = false;
  583. return asINVALID_ARG;
  584. }
  585. }
  586. int r = DetectCallingConvention(isObj, callback, callConv, &msgCallbackFunc);
  587. if( r < 0 ) msgCallback = false;
  588. return r;
  589. }
  590. // interface
  591. int asCScriptEngine::ClearMessageCallback()
  592. {
  593. msgCallback = false;
  594. return 0;
  595. }
  596. // interface
  597. int asCScriptEngine::WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message)
  598. {
  599. // Validate input parameters
  600. if( section == 0 ||
  601. message == 0 )
  602. return asINVALID_ARG;
  603. // If there is no callback then there's nothing to do
  604. if( !msgCallback )
  605. return 0;
  606. asSMessageInfo msg;
  607. msg.section = section;
  608. msg.row = row;
  609. msg.col = col;
  610. msg.type = type;
  611. msg.message = message;
  612. if( msgCallbackFunc.callConv < ICC_THISCALL )
  613. CallGlobalFunction(&msg, msgCallbackObj, &msgCallbackFunc, 0);
  614. else
  615. CallObjectMethod(msgCallbackObj, &msg, &msgCallbackFunc, 0);
  616. return 0;
  617. }
  618. int asCScriptEngine::SetJITCompiler(asIJITCompiler *compiler)
  619. {
  620. jitCompiler = compiler;
  621. return asSUCCESS;
  622. }
  623. asIJITCompiler *asCScriptEngine::GetJITCompiler() const
  624. {
  625. return jitCompiler;
  626. }
  627. // interface
  628. asETokenClass asCScriptEngine::ParseToken(const char *string, size_t stringLength, int *tokenLength) const
  629. {
  630. if( stringLength == 0 )
  631. stringLength = strlen(string);
  632. size_t len;
  633. asCTokenizer t(this);
  634. asETokenClass tc;
  635. t.GetToken(string, stringLength, &len, &tc);
  636. if( tokenLength )
  637. *tokenLength = (int)len;
  638. return tc;
  639. }
  640. // interface
  641. asIScriptModule *asCScriptEngine::GetModule(const char *module, asEGMFlags flag)
  642. {
  643. asCModule *mod = GetModule(module, false);
  644. if( flag == asGM_ALWAYS_CREATE )
  645. {
  646. if( mod != 0 )
  647. {
  648. asDELETE(mod, asCModule);
  649. }
  650. return GetModule(module, true);
  651. }
  652. if( mod == 0 && flag == asGM_CREATE_IF_NOT_EXISTS )
  653. {
  654. return GetModule(module, true);
  655. }
  656. return mod;
  657. }
  658. // interface
  659. int asCScriptEngine::DiscardModule(const char *module)
  660. {
  661. asCModule *mod = GetModule(module, false);
  662. if( mod == 0 ) return asNO_MODULE;
  663. asDELETE(mod, asCModule);
  664. FreeUnusedGlobalProperties();
  665. ClearUnusedTypes();
  666. if( ep.autoGarbageCollect )
  667. GarbageCollect();
  668. return 0;
  669. }
  670. void asCScriptEngine::ClearUnusedTypes()
  671. {
  672. // Build a list of all types to check for
  673. asCArray<asCObjectType*> types;
  674. types = classTypes;
  675. types.Concatenate(templateInstanceTypes);
  676. // Go through all modules
  677. asUINT n;
  678. for( n = 0; n < scriptModules.GetLength() && types.GetLength(); n++ )
  679. {
  680. asCModule *mod = scriptModules[n];
  681. if( mod )
  682. {
  683. // Functions/Methods/Globals are handled after this
  684. // Go through all type declarations
  685. asUINT m;
  686. for( m = 0; m < mod->classTypes.GetLength() && types.GetLength(); m++ )
  687. RemoveTypeAndRelatedFromList(types, mod->classTypes[m]);
  688. for( m = 0; m < mod->enumTypes.GetLength() && types.GetLength(); m++ )
  689. RemoveTypeAndRelatedFromList(types, mod->enumTypes[m]);
  690. for( m = 0; m < mod->typeDefs.GetLength() && types.GetLength(); m++ )
  691. RemoveTypeAndRelatedFromList(types, mod->typeDefs[m]);
  692. }
  693. }
  694. // Go through all function parameters and remove used types
  695. for( n = 0; n < scriptFunctions.GetLength() && types.GetLength(); n++ )
  696. {
  697. asCScriptFunction *func = scriptFunctions[n];
  698. if( func )
  699. {
  700. // Ignore factory stubs
  701. if( func->name == "factstub" )
  702. continue;
  703. asCObjectType *ot = func->returnType.GetObjectType();
  704. if( ot != 0 && ot != func->objectType )
  705. if( func->name != ot->name )
  706. RemoveTypeAndRelatedFromList(types, ot);
  707. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  708. {
  709. ot = func->parameterTypes[p].GetObjectType();
  710. if( ot != 0 && ot != func->objectType )
  711. if( func->name != ot->name )
  712. RemoveTypeAndRelatedFromList(types, ot);
  713. }
  714. }
  715. }
  716. // Go through all global properties
  717. for( n = 0; n < globalProperties.GetLength() && types.GetLength(); n++ )
  718. {
  719. if( globalProperties[n] && globalProperties[n]->type.GetObjectType() )
  720. RemoveTypeAndRelatedFromList(types, globalProperties[n]->type.GetObjectType());
  721. }
  722. // All that remains in the list after this can be discarded, since they are no longer used
  723. for(;;)
  724. {
  725. bool didClearTemplateInstanceType = false;
  726. for( n = 0; n < types.GetLength(); n++ )
  727. {
  728. // Template types and script classes will have two references for each factory stub
  729. int refCount = 0;
  730. if( (types[n]->flags & asOBJ_TEMPLATE) || (types[n]->flags & asOBJ_SCRIPT_OBJECT) )
  731. {
  732. refCount = 2*(int)types[n]->beh.factories.GetLength();
  733. if( types[n]->beh.listFactory )
  734. refCount += 2;
  735. }
  736. if( types[n]->GetRefCount() == refCount )
  737. {
  738. if( types[n]->flags & asOBJ_TEMPLATE )
  739. {
  740. didClearTemplateInstanceType = true;
  741. RemoveTemplateInstanceType(types[n]);
  742. }
  743. else
  744. {
  745. RemoveFromTypeIdMap(types[n]);
  746. asDELETE(types[n],asCObjectType);
  747. int i = classTypes.IndexOf(types[n]);
  748. if( i == (signed)classTypes.GetLength() - 1 )
  749. classTypes.PopLast();
  750. else
  751. classTypes[i] = classTypes.PopLast();
  752. }
  753. // Remove the type from the array
  754. if( n < types.GetLength() - 1 )
  755. types[n] = types.PopLast();
  756. else
  757. types.PopLast();
  758. n--;
  759. }
  760. }
  761. if( didClearTemplateInstanceType == false )
  762. break;
  763. }
  764. }
  765. void asCScriptEngine::RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot)
  766. {
  767. // Remove the type from the list
  768. int i = types.IndexOf(ot);
  769. if( i == -1 ) return;
  770. if( i == (signed)types.GetLength() - 1 )
  771. types.PopLast();
  772. else
  773. types[i] = types.PopLast();
  774. // If the type is an template type, then remove all sub types as well
  775. if( ot->templateSubType.GetObjectType() )
  776. {
  777. while( ot->templateSubType.GetObjectType() )
  778. {
  779. ot = ot->templateSubType.GetObjectType();
  780. RemoveTypeAndRelatedFromList(types, ot);
  781. }
  782. return;
  783. }
  784. // If the type is a class, then remove all properties types as well
  785. if( ot->properties.GetLength() )
  786. {
  787. for( asUINT n = 0; n < ot->properties.GetLength(); n++ )
  788. RemoveTypeAndRelatedFromList(types, ot->properties[n]->type.GetObjectType());
  789. }
  790. }
  791. // internal
  792. int asCScriptEngine::GetFactoryIdByDecl(const asCObjectType *ot, const char *decl)
  793. {
  794. asCModule *mod = 0;
  795. // Is this a script class?
  796. if( ot->flags & asOBJ_SCRIPT_OBJECT && ot->size > 0 )
  797. mod = scriptFunctions[ot->beh.factory]->module;
  798. asCBuilder bld(this, mod);
  799. asCScriptFunction func(this, mod, asFUNC_DUMMY);
  800. int r = bld.ParseFunctionDeclaration(0, decl, &func, false);
  801. if( r < 0 )
  802. return asINVALID_DECLARATION;
  803. // Search for matching factory function
  804. int id = -1;
  805. for( size_t n = 0; n < ot->beh.factories.GetLength(); n++ )
  806. {
  807. asCScriptFunction *f = scriptFunctions[ot->beh.factories[n]];
  808. if( f->IsSignatureEqual(&func) )
  809. {
  810. id = ot->beh.factories[n];
  811. break;
  812. }
  813. }
  814. if( id == -1 ) return asNO_FUNCTION;
  815. return id;
  816. }
  817. // internal
  818. int asCScriptEngine::GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod)
  819. {
  820. asCBuilder bld(this, mod);
  821. asCScriptFunction func(this, mod, asFUNC_DUMMY);
  822. // Set the object type so that the signature can be properly compared
  823. // This cast is OK, it will only be used for comparison
  824. func.objectType = const_cast<asCObjectType*>(ot);
  825. int r = bld.ParseFunctionDeclaration(func.objectType, decl, &func, false);
  826. if( r < 0 )
  827. return asINVALID_DECLARATION;
  828. // Search script functions for matching interface
  829. int id = -1;
  830. for( size_t n = 0; n < ot->methods.GetLength(); ++n )
  831. {
  832. if( func.IsSignatureEqual(scriptFunctions[ot->methods[n]]) )
  833. {
  834. if( id == -1 )
  835. id = ot->methods[n];
  836. else
  837. return asMULTIPLE_FUNCTIONS;
  838. }
  839. }
  840. if( id == -1 ) return asNO_FUNCTION;
  841. return id;
  842. }
  843. // Internal
  844. asCString asCScriptEngine::GetFunctionDeclaration(int funcID)
  845. {
  846. asCString str;
  847. asCScriptFunction *func = GetScriptFunction(funcID);
  848. if( func )
  849. str = func->GetDeclarationStr();
  850. return str;
  851. }
  852. asCScriptFunction *asCScriptEngine::GetScriptFunction(int funcId) const
  853. {
  854. if( funcId < 0 || funcId >= (int)scriptFunctions.GetLength() )
  855. return 0;
  856. return scriptFunctions[funcId];
  857. }
  858. asIScriptContext *asCScriptEngine::CreateContext()
  859. {
  860. asIScriptContext *ctx = 0;
  861. CreateContext(&ctx, false);
  862. return ctx;
  863. }
  864. int asCScriptEngine::CreateContext(asIScriptContext **context, bool isInternal)
  865. {
  866. *context = asNEW(asCContext)(this, !isInternal);
  867. // We need to make sure the engine has been
  868. // prepared before any context is executed
  869. PrepareEngine();
  870. return 0;
  871. }
  872. int asCScriptEngine::RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset)
  873. {
  874. int r;
  875. asCDataType dt;
  876. asCBuilder bld(this, 0);
  877. r = bld.ParseDataType(obj, &dt);
  878. if( r < 0 )
  879. return ConfigError(r);
  880. // Verify that the correct config group is used
  881. if( currentGroup->FindType(dt.GetObjectType()->name.AddressOf()) == 0 )
  882. return ConfigError(asWRONG_CONFIG_GROUP);
  883. asCDataType type;
  884. asCString name;
  885. if( (r = bld.VerifyProperty(&dt, declaration, name, type)) < 0 )
  886. return ConfigError(r);
  887. // Store the property info
  888. if( dt.GetObjectType() == 0 )
  889. return ConfigError(asINVALID_OBJECT);
  890. asCObjectProperty *prop = asNEW(asCObjectProperty);
  891. prop->name = name;
  892. prop->type = type;
  893. prop->byteOffset = byteOffset;
  894. prop->isPrivate = false;
  895. prop->accessMask = defaultAccessMask;
  896. dt.GetObjectType()->properties.PushLast(prop);
  897. currentGroup->RefConfigGroup(FindConfigGroupForObjectType(type.GetObjectType()));
  898. return asSUCCESS;
  899. }
  900. int asCScriptEngine::RegisterInterface(const char *name)
  901. {
  902. if( name == 0 ) return ConfigError(asINVALID_NAME);
  903. // Verify if the name has been registered as a type already
  904. asUINT n;
  905. for( n = 0; n < objectTypes.GetLength(); n++ )
  906. {
  907. if( objectTypes[n] && objectTypes[n]->name == name )
  908. return asALREADY_REGISTERED;
  909. }
  910. // Use builder to parse the datatype
  911. asCDataType dt;
  912. asCBuilder bld(this, 0);
  913. bool oldMsgCallback = msgCallback; msgCallback = false;
  914. int r = bld.ParseDataType(name, &dt);
  915. msgCallback = oldMsgCallback;
  916. if( r >= 0 ) return ConfigError(asERROR);
  917. // Make sure the name is not a reserved keyword
  918. asCTokenizer t(this);
  919. size_t tokenLen;
  920. int token = t.GetToken(name, strlen(name), &tokenLen);
  921. if( token != ttIdentifier || strlen(name) != tokenLen )
  922. return ConfigError(asINVALID_NAME);
  923. r = bld.CheckNameConflict(name, 0, 0);
  924. if( r < 0 )
  925. return ConfigError(asNAME_TAKEN);
  926. // Don't have to check against members of object
  927. // types as they are allowed to use the names
  928. // Register the object type for the interface
  929. asCObjectType *st = asNEW(asCObjectType)(this);
  930. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT | asOBJ_SHARED;
  931. st->size = 0; // Cannot be instanciated
  932. st->name = name;
  933. // Use the default script class behaviours
  934. st->beh.factory = 0;
  935. st->beh.addref = scriptTypeBehaviours.beh.addref;
  936. scriptFunctions[st->beh.addref]->AddRef();
  937. st->beh.release = scriptTypeBehaviours.beh.release;
  938. scriptFunctions[st->beh.release]->AddRef();
  939. st->beh.copy = 0;
  940. objectTypes.PushLast(st);
  941. registeredObjTypes.PushLast(st);
  942. currentGroup->objTypes.PushLast(st);
  943. return asSUCCESS;
  944. }
  945. int asCScriptEngine::RegisterInterfaceMethod(const char *intf, const char *declaration)
  946. {
  947. // Verify that the correct config group is set.
  948. if( currentGroup->FindType(intf) == 0 )
  949. return ConfigError(asWRONG_CONFIG_GROUP);
  950. asCDataType dt;
  951. asCBuilder bld(this, 0);
  952. int r = bld.ParseDataType(intf, &dt);
  953. if( r < 0 )
  954. return ConfigError(r);
  955. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_INTERFACE);
  956. func->objectType = dt.GetObjectType();
  957. r = bld.ParseFunctionDeclaration(func->objectType, declaration, func, false);
  958. if( r < 0 )
  959. {
  960. asDELETE(func,asCScriptFunction);
  961. return ConfigError(asINVALID_DECLARATION);
  962. }
  963. // Check name conflicts
  964. r = bld.CheckNameConflictMember(dt.GetObjectType(), func->name.AddressOf(), 0, 0, false);
  965. if( r < 0 )
  966. {
  967. asDELETE(func,asCScriptFunction);
  968. return ConfigError(asNAME_TAKEN);
  969. }
  970. func->id = GetNextScriptFunctionId();
  971. SetScriptFunction(func);
  972. func->objectType->methods.PushLast(func->id);
  973. // The refCount was already set to 1
  974. func->ComputeSignatureId();
  975. // If parameter type from other groups are used, add references
  976. // TODO: The code for adding references to config groups is repeated in a lot of places
  977. if( func->returnType.GetObjectType() )
  978. {
  979. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  980. currentGroup->RefConfigGroup(group);
  981. }
  982. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  983. {
  984. if( func->parameterTypes[n].GetObjectType() )
  985. {
  986. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  987. currentGroup->RefConfigGroup(group);
  988. }
  989. }
  990. // Return function id as success
  991. return func->id;
  992. }
  993. int asCScriptEngine::RegisterObjectType(const char *name, int byteSize, asDWORD flags)
  994. {
  995. int r;
  996. isPrepared = false;
  997. // Verify flags
  998. // Must have either asOBJ_REF or asOBJ_VALUE
  999. if( flags & asOBJ_REF )
  1000. {
  1001. // Can optionally have the asOBJ_GC, asOBJ_NOHANDLE, asOBJ_SCOPED, or asOBJ_TEMPLATE flag set, but nothing else
  1002. if( flags & ~(asOBJ_REF | asOBJ_GC | asOBJ_NOHANDLE | asOBJ_SCOPED | asOBJ_TEMPLATE) )
  1003. return ConfigError(asINVALID_ARG);
  1004. // flags are exclusive
  1005. if( (flags & asOBJ_GC) && (flags & (asOBJ_NOHANDLE|asOBJ_SCOPED)) )
  1006. return ConfigError(asINVALID_ARG);
  1007. if( (flags & asOBJ_NOHANDLE) && (flags & (asOBJ_GC|asOBJ_SCOPED)) )
  1008. return ConfigError(asINVALID_ARG);
  1009. if( (flags & asOBJ_SCOPED) && (flags & (asOBJ_GC|asOBJ_NOHANDLE)) )
  1010. return ConfigError(asINVALID_ARG);
  1011. }
  1012. else if( flags & asOBJ_VALUE )
  1013. {
  1014. // Cannot use reference flags
  1015. // TODO: template: Should be possible to register a value type as template type
  1016. if( flags & (asOBJ_REF | asOBJ_GC | asOBJ_SCOPED | asOBJ_TEMPLATE) )
  1017. return ConfigError(asINVALID_ARG);
  1018. // flags are exclusive
  1019. if( (flags & asOBJ_POD) && (flags & asOBJ_ASHANDLE) )
  1020. return ConfigError(asINVALID_ARG);
  1021. // If the app type is given, we must validate the flags
  1022. if( flags & asOBJ_APP_CLASS )
  1023. {
  1024. // Must not set the primitive or float flag
  1025. if( flags & (asOBJ_APP_PRIMITIVE |
  1026. asOBJ_APP_FLOAT) )
  1027. return ConfigError(asINVALID_ARG);
  1028. }
  1029. else if( flags & asOBJ_APP_PRIMITIVE )
  1030. {
  1031. // Must not set the class flags nor the float flag
  1032. if( flags & (asOBJ_APP_CLASS |
  1033. asOBJ_APP_CLASS_CONSTRUCTOR |
  1034. asOBJ_APP_CLASS_DESTRUCTOR |
  1035. asOBJ_APP_CLASS_ASSIGNMENT |
  1036. asOBJ_APP_CLASS_COPY_CONSTRUCTOR |
  1037. asOBJ_APP_FLOAT |
  1038. asOBJ_APP_CLASS_ALLINTS |
  1039. asOBJ_APP_CLASS_ALLFLOATS) )
  1040. return ConfigError(asINVALID_ARG);
  1041. }
  1042. else if( flags & asOBJ_APP_FLOAT )
  1043. {
  1044. // Must not set the class flags nor the primitive flag
  1045. if( flags & (asOBJ_APP_CLASS |
  1046. asOBJ_APP_CLASS_CONSTRUCTOR |
  1047. asOBJ_APP_CLASS_DESTRUCTOR |
  1048. asOBJ_APP_CLASS_ASSIGNMENT |
  1049. asOBJ_APP_CLASS_COPY_CONSTRUCTOR |
  1050. asOBJ_APP_PRIMITIVE |
  1051. asOBJ_APP_CLASS_ALLINTS |
  1052. asOBJ_APP_CLASS_ALLFLOATS) )
  1053. return ConfigError(asINVALID_ARG);
  1054. }
  1055. else if( flags & (asOBJ_APP_CLASS_CONSTRUCTOR |
  1056. asOBJ_APP_CLASS_DESTRUCTOR |
  1057. asOBJ_APP_CLASS_ASSIGNMENT |
  1058. asOBJ_APP_CLASS_COPY_CONSTRUCTOR |
  1059. asOBJ_APP_CLASS_ALLINTS |
  1060. asOBJ_APP_CLASS_ALLFLOATS) )
  1061. {
  1062. // Must not set the class properties, without the class flag
  1063. return ConfigError(asINVALID_ARG);
  1064. }
  1065. }
  1066. else
  1067. return ConfigError(asINVALID_ARG);
  1068. // Don't allow anything else than the defined flags
  1069. if( flags - (flags & asOBJ_MASK_VALID_FLAGS) )
  1070. return ConfigError(asINVALID_ARG);
  1071. // Value types must have a defined size
  1072. if( (flags & asOBJ_VALUE) && byteSize == 0 )
  1073. {
  1074. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_VALUE_TYPE_MUST_HAVE_SIZE);
  1075. return ConfigError(asINVALID_ARG);
  1076. }
  1077. // Verify type name
  1078. if( name == 0 )
  1079. return ConfigError(asINVALID_NAME);
  1080. asCString typeName;
  1081. asCBuilder bld(this, 0);
  1082. if( flags & asOBJ_TEMPLATE )
  1083. {
  1084. asCString subtypeName;
  1085. r = bld.ParseTemplateDecl(name, &typeName, &subtypeName);
  1086. if( r < 0 )
  1087. return r;
  1088. // Verify that the template name hasn't been registered as a type already
  1089. asUINT n;
  1090. for( n = 0; n < objectTypes.GetLength(); n++ )
  1091. {
  1092. if( objectTypes[n] && objectTypes[n]->name == typeName )
  1093. return asALREADY_REGISTERED;
  1094. }
  1095. asCObjectType *type = asNEW(asCObjectType)(this);
  1096. type->name = typeName;
  1097. type->size = byteSize;
  1098. type->flags = flags;
  1099. type->accessMask = defaultAccessMask;
  1100. // Store it in the object types
  1101. objectTypes.PushLast(type);
  1102. // Define a template subtype
  1103. asCObjectType *subtype = 0;
  1104. for( n = 0; n < templateSubTypes.GetLength(); n++ )
  1105. {
  1106. if( templateSubTypes[n]->name == subtypeName )
  1107. {
  1108. subtype = templateSubTypes[n];
  1109. break;
  1110. }
  1111. }
  1112. if( subtype == 0 )
  1113. {
  1114. // Create the new subtype if not already existing
  1115. subtype = asNEW(asCObjectType)(this);
  1116. subtype->name = subtypeName;
  1117. subtype->size = 0;
  1118. subtype->flags = asOBJ_TEMPLATE_SUBTYPE;
  1119. templateSubTypes.PushLast(subtype);
  1120. subtype->AddRef();
  1121. }
  1122. type->templateSubType = asCDataType::CreateObject(subtype, false);
  1123. subtype->AddRef();
  1124. currentGroup->objTypes.PushLast(type);
  1125. registeredObjTypes.PushLast(type);
  1126. }
  1127. else
  1128. {
  1129. typeName = name;
  1130. // Verify if the name has been registered as a type already
  1131. asUINT n;
  1132. for( n = 0; n < objectTypes.GetLength(); n++ )
  1133. {
  1134. if( objectTypes[n] && objectTypes[n]->name == typeName )
  1135. return asALREADY_REGISTERED;
  1136. }
  1137. for( n = 0; n < templateTypes.GetLength(); n++ )
  1138. {
  1139. if( templateTypes[n] && templateTypes[n]->name == typeName )
  1140. return asALREADY_REGISTERED;
  1141. }
  1142. // Verify the most recently created template instance type
  1143. asCObjectType *mostRecentTemplateInstanceType = 0;
  1144. if( templateInstanceTypes.GetLength() )
  1145. mostRecentTemplateInstanceType = templateInstanceTypes[templateInstanceTypes.GetLength()-1];
  1146. // Use builder to parse the datatype
  1147. asCDataType dt;
  1148. bool oldMsgCallback = msgCallback; msgCallback = false;
  1149. r = bld.ParseDataType(name, &dt);
  1150. msgCallback = oldMsgCallback;
  1151. // If the builder fails, then the type name
  1152. // is new and it should be registered
  1153. if( r < 0 )
  1154. {
  1155. // Make sure the name is not a reserved keyword
  1156. asCTokenizer t(this);
  1157. size_t tokenLen;
  1158. int token = t.GetToken(name, typeName.GetLength(), &tokenLen);
  1159. if( token != ttIdentifier || typeName.GetLength() != tokenLen )
  1160. return ConfigError(asINVALID_NAME);
  1161. int r = bld.CheckNameConflict(name, 0, 0);
  1162. if( r < 0 )
  1163. return ConfigError(asNAME_TAKEN);
  1164. // Don't have to check against members of object
  1165. // types as they are allowed to use the names
  1166. // Put the data type in the list
  1167. asCObjectType *type = asNEW(asCObjectType)(this);
  1168. type->name = typeName;
  1169. type->size = byteSize;
  1170. type->flags = flags;
  1171. type->accessMask = defaultAccessMask;
  1172. objectTypes.PushLast(type);
  1173. registeredObjTypes.PushLast(type);
  1174. currentGroup->objTypes.PushLast(type);
  1175. }
  1176. else
  1177. {
  1178. // The application is registering a template specialization so we
  1179. // need to replace the template instance type with the new type.
  1180. // TODO: Template: We don't require the lower dimensions to be registered first for registered template types
  1181. // int[][] must not be allowed to be registered
  1182. // if int[] hasn't been registered first
  1183. if( dt.GetSubType().IsTemplate() )
  1184. return ConfigError(asLOWER_ARRAY_DIMENSION_NOT_REGISTERED);
  1185. if( dt.IsReadOnly() ||
  1186. dt.IsReference() )
  1187. return ConfigError(asINVALID_TYPE);
  1188. // Was the template instance type created before?
  1189. if( templateInstanceTypes[templateInstanceTypes.GetLength()-1] == mostRecentTemplateInstanceType ||
  1190. mostRecentTemplateInstanceType == dt.GetObjectType() )
  1191. // TODO: Should have a better error message
  1192. return ConfigError(asNOT_SUPPORTED);
  1193. // TODO: Add this again. The type is used by the factory stubs so we need to discount that
  1194. // Is the template instance type already being used?
  1195. // if( dt.GetObjectType()->GetRefCount() > 1 )
  1196. // return ConfigError(asNOT_SUPPORTED);
  1197. // Put the data type in the list
  1198. asCObjectType *type = asNEW(asCObjectType)(this);
  1199. type->name = dt.GetObjectType()->name;
  1200. type->templateSubType = dt.GetSubType();
  1201. if( type->templateSubType.GetObjectType() ) type->templateSubType.GetObjectType()->AddRef();
  1202. type->size = byteSize;
  1203. type->flags = flags;
  1204. type->accessMask = defaultAccessMask;
  1205. templateTypes.PushLast(type);
  1206. currentGroup->objTypes.PushLast(type);
  1207. // Remove the template instance type, which will no longer be used.
  1208. RemoveTemplateInstanceType(dt.GetObjectType());
  1209. }
  1210. }
  1211. // Return the type id as the success (except for template types)
  1212. if( flags & asOBJ_TEMPLATE )
  1213. return asSUCCESS;
  1214. return GetTypeIdByDecl(name);
  1215. }
  1216. // interface
  1217. int asCScriptEngine::RegisterObjectBehaviour(const char *datatype, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1218. {
  1219. if( datatype == 0 ) return ConfigError(asINVALID_ARG);
  1220. // Determine the object type
  1221. asCBuilder bld(this, 0);
  1222. asCDataType type;
  1223. int r = bld.ParseDataType(datatype, &type);
  1224. if( r < 0 )
  1225. return ConfigError(r);
  1226. if( type.GetObjectType() == 0 )
  1227. return ConfigError(asINVALID_TYPE);
  1228. if( type.IsReadOnly() || type.IsReference() )
  1229. return ConfigError(asINVALID_TYPE);
  1230. return RegisterBehaviourToObjectType(type.GetObjectType(), behaviour, decl, funcPointer, callConv);
  1231. }
  1232. // internal
  1233. int asCScriptEngine::RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1234. {
  1235. asSSystemFunctionInterface internal;
  1236. if( behaviour == asBEHAVE_FACTORY ||
  1237. behaviour == asBEHAVE_LIST_FACTORY ||
  1238. behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1239. {
  1240. #ifdef AS_MAX_PORTABILITY
  1241. if( callConv != asCALL_GENERIC )
  1242. return ConfigError(asNOT_SUPPORTED);
  1243. #endif
  1244. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1245. if( r < 0 )
  1246. return ConfigError(r);
  1247. }
  1248. else
  1249. {
  1250. #ifdef AS_MAX_PORTABILITY
  1251. if( callConv != asCALL_GENERIC )
  1252. return ConfigError(asNOT_SUPPORTED);
  1253. #else
  1254. if( callConv != asCALL_THISCALL &&
  1255. callConv != asCALL_CDECL_OBJLAST &&
  1256. callConv != asCALL_CDECL_OBJFIRST &&
  1257. callConv != asCALL_GENERIC )
  1258. return ConfigError(asNOT_SUPPORTED);
  1259. #endif
  1260. int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
  1261. if( r < 0 )
  1262. return ConfigError(r);
  1263. }
  1264. isPrepared = false;
  1265. asSTypeBehaviour *beh = &objectType->beh;
  1266. // Verify function declaration
  1267. asCScriptFunction func(this, 0, asFUNC_DUMMY);
  1268. asCBuilder bld(this, 0);
  1269. int r = bld.ParseFunctionDeclaration(objectType, decl, &func, true, &internal.paramAutoHandles, &internal.returnAutoHandle);
  1270. if( r < 0 )
  1271. return ConfigError(asINVALID_DECLARATION);
  1272. func.name.Format("_beh_%d_", behaviour);
  1273. if( behaviour != asBEHAVE_FACTORY && behaviour != asBEHAVE_LIST_FACTORY )
  1274. func.objectType = objectType;
  1275. // Check if the method restricts that use of the template to value types or reference types
  1276. if( objectType->flags & asOBJ_TEMPLATE )
  1277. {
  1278. if( func.returnType.GetObjectType() == objectType->templateSubType.GetObjectType() )
  1279. {
  1280. if( func.returnType.IsObjectHandle() )
  1281. objectType->acceptValueSubType = false;
  1282. else if( !func.returnType.IsReference() )
  1283. objectType->acceptRefSubType = false;
  1284. }
  1285. for( asUINT n = 0; n < func.parameterTypes.GetLength(); n++ )
  1286. {
  1287. if( func.parameterTypes[n].GetObjectType() == objectType->templateSubType.GetObjectType() )
  1288. {
  1289. // TODO: If unsafe references are allowed, then inout references allow value types
  1290. if( func.parameterTypes[n].IsObjectHandle() || (func.parameterTypes[n].IsReference() && func.inOutFlags[n] == asTM_INOUTREF) )
  1291. objectType->acceptValueSubType = false;
  1292. else if( !func.parameterTypes[n].IsReference() )
  1293. objectType->acceptRefSubType = false;
  1294. }
  1295. }
  1296. }
  1297. if( behaviour == asBEHAVE_CONSTRUCT )
  1298. {
  1299. // TODO: Add asBEHAVE_IMPLICIT_CONSTRUCT
  1300. // Verify that the return type is void
  1301. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1302. return ConfigError(asINVALID_DECLARATION);
  1303. if( objectType->flags & asOBJ_SCRIPT_OBJECT )
  1304. {
  1305. // The script object is a special case
  1306. asASSERT(func.parameterTypes.GetLength() == 1);
  1307. beh->construct = AddBehaviourFunction(func, internal);
  1308. beh->factory = beh->construct;
  1309. scriptFunctions[beh->factory]->AddRef();
  1310. beh->constructors.PushLast(beh->construct);
  1311. beh->factories.PushLast(beh->factory);
  1312. func.id = beh->construct;
  1313. }
  1314. else
  1315. {
  1316. // Verify that it is a value type
  1317. if( !(func.objectType->flags & asOBJ_VALUE) )
  1318. {
  1319. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1320. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1321. }
  1322. // TODO: Add support for implicit constructors
  1323. // TODO: Verify that the same constructor hasn't been registered already
  1324. // Store all constructors in a list
  1325. func.id = AddBehaviourFunction(func, internal);
  1326. beh->constructors.PushLast(func.id);
  1327. if( func.parameterTypes.GetLength() == 0 )
  1328. {
  1329. beh->construct = func.id;
  1330. }
  1331. else if( func.parameterTypes.GetLength() == 1 )
  1332. {
  1333. // Is this the copy constructor?
  1334. asCDataType paramType = func.parameterTypes[0];
  1335. // If the parameter is object, and const reference for input,
  1336. // and same type as this class, then this is a copy constructor.
  1337. if( paramType.IsObject() && paramType.IsReference() && paramType.IsReadOnly() && func.inOutFlags[0] == asTM_INREF && paramType.GetObjectType() == objectType )
  1338. beh->copyconstruct = func.id;
  1339. }
  1340. }
  1341. }
  1342. else if( behaviour == asBEHAVE_DESTRUCT )
  1343. {
  1344. // Must be a value type
  1345. if( !(func.objectType->flags & asOBJ_VALUE) )
  1346. {
  1347. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1348. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1349. }
  1350. if( beh->destruct )
  1351. return ConfigError(asALREADY_REGISTERED);
  1352. // Verify that the return type is void
  1353. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1354. return ConfigError(asINVALID_DECLARATION);
  1355. // Verify that there are no parameters
  1356. if( func.parameterTypes.GetLength() > 0 )
  1357. return ConfigError(asINVALID_DECLARATION);
  1358. func.id = beh->destruct = AddBehaviourFunction(func, internal);
  1359. }
  1360. else if( behaviour == asBEHAVE_FACTORY || behaviour == asBEHAVE_LIST_FACTORY )
  1361. {
  1362. // Must be a ref type and must not have asOBJ_NOHANDLE
  1363. if( !(objectType->flags & asOBJ_REF) || (objectType->flags & asOBJ_NOHANDLE) )
  1364. {
  1365. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1366. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1367. }
  1368. // Verify that the return type is a handle to the type
  1369. if( func.returnType != asCDataType::CreateObjectHandle(objectType, false) )
  1370. return ConfigError(asINVALID_DECLARATION);
  1371. // TODO: Add support for implicit factories
  1372. // TODO: Verify that the same factory function hasn't been registered already
  1373. // The templates take a hidden parameter with the object type
  1374. if( (objectType->flags & asOBJ_TEMPLATE) &&
  1375. (func.parameterTypes.GetLength() == 0 ||
  1376. !func.parameterTypes[0].IsReference()) )
  1377. {
  1378. return ConfigError(asINVALID_DECLARATION);
  1379. }
  1380. // Store all factory functions in a list
  1381. func.id = AddBehaviourFunction(func, internal);
  1382. // The list factory is a special factory and isn't stored together with the rest
  1383. if( behaviour != asBEHAVE_LIST_FACTORY )
  1384. beh->factories.PushLast(func.id);
  1385. if( (func.parameterTypes.GetLength() == 0) ||
  1386. (func.parameterTypes.GetLength() == 1 && (objectType->flags & asOBJ_TEMPLATE)) )
  1387. {
  1388. beh->factory = func.id;
  1389. }
  1390. else if( (func.parameterTypes.GetLength() == 1) ||
  1391. (func.parameterTypes.GetLength() == 2 && (objectType->flags & asOBJ_TEMPLATE)) )
  1392. {
  1393. if( behaviour == asBEHAVE_LIST_FACTORY )
  1394. beh->listFactory = func.id;
  1395. else
  1396. {
  1397. // Is this the copy factory?
  1398. asCDataType paramType = func.parameterTypes[func.parameterTypes.GetLength()-1];
  1399. // If the parameter is object, and const reference for input,
  1400. // and same type as this class, then this is a copy constructor.
  1401. if( paramType.IsObject() && paramType.IsReference() && paramType.IsReadOnly() && func.inOutFlags[func.parameterTypes.GetLength()-1] == asTM_INREF && paramType.GetObjectType() == objectType )
  1402. beh->copyfactory = func.id;
  1403. }
  1404. }
  1405. }
  1406. else if( behaviour == asBEHAVE_ADDREF )
  1407. {
  1408. // Must be a ref type and must not have asOBJ_NOHANDLE, nor asOBJ_SCOPED
  1409. if( !(func.objectType->flags & asOBJ_REF) ||
  1410. (func.objectType->flags & asOBJ_NOHANDLE) ||
  1411. (func.objectType->flags & asOBJ_SCOPED) )
  1412. {
  1413. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1414. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1415. }
  1416. if( beh->addref )
  1417. return ConfigError(asALREADY_REGISTERED);
  1418. // Verify that the return type is void
  1419. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1420. return ConfigError(asINVALID_DECLARATION);
  1421. // Verify that there are no parameters
  1422. if( func.parameterTypes.GetLength() > 0 )
  1423. return ConfigError(asINVALID_DECLARATION);
  1424. func.id = beh->addref = AddBehaviourFunction(func, internal);
  1425. }
  1426. else if( behaviour == asBEHAVE_RELEASE )
  1427. {
  1428. // Must be a ref type and must not have asOBJ_NOHANDLE
  1429. if( !(func.objectType->flags & asOBJ_REF) || (func.objectType->flags & asOBJ_NOHANDLE) )
  1430. {
  1431. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1432. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1433. }
  1434. if( beh->release )
  1435. return ConfigError(asALREADY_REGISTERED);
  1436. // Verify that the return type is void
  1437. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1438. return ConfigError(asINVALID_DECLARATION);
  1439. // Verify that there are no parameters
  1440. if( func.parameterTypes.GetLength() > 0 )
  1441. return ConfigError(asINVALID_DECLARATION);
  1442. func.id = beh->release = AddBehaviourFunction(func, internal);
  1443. }
  1444. else if( behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1445. {
  1446. // Must be a template type
  1447. if( !(func.objectType->flags & asOBJ_TEMPLATE) )
  1448. {
  1449. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1450. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1451. }
  1452. if( beh->templateCallback )
  1453. return ConfigError(asALREADY_REGISTERED);
  1454. // Verify that the return type is bool
  1455. if( func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1456. return ConfigError(asINVALID_DECLARATION);
  1457. // Verify that there is one parameters
  1458. if( func.parameterTypes.GetLength() != 1 )
  1459. return ConfigError(asINVALID_DECLARATION);
  1460. func.id = beh->templateCallback = AddBehaviourFunction(func, internal);
  1461. }
  1462. else if( behaviour >= asBEHAVE_FIRST_GC &&
  1463. behaviour <= asBEHAVE_LAST_GC )
  1464. {
  1465. // Only allow GC behaviours for types registered to be garbage collected
  1466. if( !(func.objectType->flags & asOBJ_GC) )
  1467. {
  1468. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1469. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1470. }
  1471. // Verify parameter count
  1472. if( (behaviour == asBEHAVE_GETREFCOUNT ||
  1473. behaviour == asBEHAVE_SETGCFLAG ||
  1474. behaviour == asBEHAVE_GETGCFLAG) &&
  1475. func.parameterTypes.GetLength() != 0 )
  1476. return ConfigError(asINVALID_DECLARATION);
  1477. if( (behaviour == asBEHAVE_ENUMREFS ||
  1478. behaviour == asBEHAVE_RELEASEREFS) &&
  1479. func.parameterTypes.GetLength() != 1 )
  1480. return ConfigError(asINVALID_DECLARATION);
  1481. // Verify return type
  1482. if( behaviour == asBEHAVE_GETREFCOUNT &&
  1483. func.returnType != asCDataType::CreatePrimitive(ttInt, false) )
  1484. return ConfigError(asINVALID_DECLARATION);
  1485. if( behaviour == asBEHAVE_GETGCFLAG &&
  1486. func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1487. return ConfigError(asINVALID_DECLARATION);
  1488. if( (behaviour == asBEHAVE_SETGCFLAG ||
  1489. behaviour == asBEHAVE_ENUMREFS ||
  1490. behaviour == asBEHAVE_RELEASEREFS) &&
  1491. func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1492. return ConfigError(asINVALID_DECLARATION);
  1493. if( behaviour == asBEHAVE_GETREFCOUNT )
  1494. func.id = beh->gcGetRefCount = AddBehaviourFunction(func, internal);
  1495. else if( behaviour == asBEHAVE_SETGCFLAG )
  1496. func.id = beh->gcSetFlag = AddBehaviourFunction(func, internal);
  1497. else if( behaviour == asBEHAVE_GETGCFLAG )
  1498. func.id = beh->gcGetFlag = AddBehaviourFunction(func, internal);
  1499. else if( behaviour == asBEHAVE_ENUMREFS )
  1500. func.id = beh->gcEnumReferences = AddBehaviourFunction(func, internal);
  1501. else if( behaviour == asBEHAVE_RELEASEREFS )
  1502. func.id = beh->gcReleaseAllReferences = AddBehaviourFunction(func, internal);
  1503. }
  1504. else if( behaviour == asBEHAVE_IMPLICIT_VALUE_CAST ||
  1505. behaviour == asBEHAVE_VALUE_CAST )
  1506. {
  1507. // Verify parameter count
  1508. if( func.parameterTypes.GetLength() != 0 )
  1509. return ConfigError(asINVALID_DECLARATION);
  1510. // Verify return type
  1511. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, false)) )
  1512. return ConfigError(asNOT_SUPPORTED);
  1513. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  1514. return ConfigError(asINVALID_DECLARATION);
  1515. // TODO: verify that the same cast is not registered already (const or non-const is treated the same for the return type)
  1516. beh->operators.PushLast(behaviour);
  1517. func.id = AddBehaviourFunction(func, internal);
  1518. beh->operators.PushLast(func.id);
  1519. }
  1520. else if( behaviour == asBEHAVE_REF_CAST ||
  1521. behaviour == asBEHAVE_IMPLICIT_REF_CAST )
  1522. {
  1523. // There are two allowed signatures
  1524. // 1. obj @f()
  1525. // 2. void f(?&out)
  1526. if( !(func.parameterTypes.GetLength() == 0 && func.returnType.IsObjectHandle()) &&
  1527. !(func.parameterTypes.GetLength() == 1 && func.parameterTypes[0].GetTokenType() == ttQuestion && func.inOutFlags[0] == asTM_OUTREF && func.returnType.GetTokenType() == ttVoid) )
  1528. {
  1529. return ConfigError(asINVALID_DECLARATION);
  1530. }
  1531. // TODO: verify that the same cast is not registered already (cosnt or non-const is treated the same for the return type)
  1532. beh->operators.PushLast(behaviour);
  1533. func.id = AddBehaviourFunction(func, internal);
  1534. beh->operators.PushLast(func.id);
  1535. }
  1536. else
  1537. {
  1538. asASSERT(false);
  1539. return ConfigError(asINVALID_ARG);
  1540. }
  1541. // Return function id as success
  1542. return func.id;
  1543. }
  1544. int asCScriptEngine::VerifyVarTypeNotInFunction(asCScriptFunction *func)
  1545. {
  1546. // Don't allow var type in this function
  1547. if( func->returnType.GetTokenType() == ttQuestion )
  1548. return asINVALID_DECLARATION;
  1549. for( unsigned int n = 0; n < func->parameterTypes.GetLength(); n++ )
  1550. if( func->parameterTypes[n].GetTokenType() == ttQuestion )
  1551. return asINVALID_DECLARATION;
  1552. return 0;
  1553. }
  1554. int asCScriptEngine::AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal)
  1555. {
  1556. asUINT n;
  1557. int id = GetNextScriptFunctionId();
  1558. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface);
  1559. newInterface->func = internal.func;
  1560. newInterface->baseOffset = internal.baseOffset;
  1561. newInterface->callConv = internal.callConv;
  1562. newInterface->scriptReturnSize = internal.scriptReturnSize;
  1563. newInterface->hostReturnInMemory = internal.hostReturnInMemory;
  1564. newInterface->hostReturnFloat = internal.hostReturnFloat;
  1565. newInterface->hostReturnSize = internal.hostReturnSize;
  1566. newInterface->paramSize = internal.paramSize;
  1567. newInterface->takesObjByVal = internal.takesObjByVal;
  1568. newInterface->paramAutoHandles = internal.paramAutoHandles;
  1569. newInterface->returnAutoHandle = internal.returnAutoHandle;
  1570. newInterface->hasAutoHandles = internal.hasAutoHandles;
  1571. asCScriptFunction *f = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1572. asASSERT(func.name != "" && func.name != "f");
  1573. f->name = func.name;
  1574. f->sysFuncIntf = newInterface;
  1575. f->returnType = func.returnType;
  1576. f->objectType = func.objectType;
  1577. f->id = id;
  1578. f->isReadOnly = func.isReadOnly;
  1579. f->accessMask = defaultAccessMask;
  1580. for( n = 0; n < func.parameterTypes.GetLength(); n++ )
  1581. {
  1582. f->parameterTypes.PushLast(func.parameterTypes[n]);
  1583. f->inOutFlags.PushLast(func.inOutFlags[n]);
  1584. }
  1585. SetScriptFunction(f);
  1586. // If parameter type from other groups are used, add references
  1587. if( f->returnType.GetObjectType() )
  1588. {
  1589. asCConfigGroup *group = FindConfigGroupForObjectType(f->returnType.GetObjectType());
  1590. currentGroup->RefConfigGroup(group);
  1591. }
  1592. for( n = 0; n < f->parameterTypes.GetLength(); n++ )
  1593. {
  1594. if( f->parameterTypes[n].GetObjectType() )
  1595. {
  1596. asCConfigGroup *group = FindConfigGroupForObjectType(f->parameterTypes[n].GetObjectType());
  1597. currentGroup->RefConfigGroup(group);
  1598. }
  1599. }
  1600. return id;
  1601. }
  1602. // interface
  1603. int asCScriptEngine::RegisterGlobalProperty(const char *declaration, void *pointer)
  1604. {
  1605. asCDataType type;
  1606. asCString name;
  1607. int r;
  1608. asCBuilder bld(this, 0);
  1609. if( (r = bld.VerifyProperty(0, declaration, name, type)) < 0 )
  1610. return ConfigError(r);
  1611. // Don't allow registering references as global properties
  1612. if( type.IsReference() )
  1613. return ConfigError(asINVALID_TYPE);
  1614. // Store the property info
  1615. asCGlobalProperty *prop = AllocateGlobalProperty();
  1616. prop->name = name;
  1617. prop->type = type;
  1618. prop->accessMask = defaultAccessMask;
  1619. prop->SetRegisteredAddress(pointer);
  1620. registeredGlobalProps.PushLast(prop);
  1621. currentGroup->globalProps.PushLast(prop);
  1622. // If from another group add reference
  1623. if( type.GetObjectType() )
  1624. {
  1625. asCConfigGroup *group = FindConfigGroupForObjectType(type.GetObjectType());
  1626. currentGroup->RefConfigGroup(group);
  1627. }
  1628. return asSUCCESS;
  1629. }
  1630. // internal
  1631. asCGlobalProperty *asCScriptEngine::AllocateGlobalProperty()
  1632. {
  1633. asCGlobalProperty *prop = asNEW(asCGlobalProperty);
  1634. // First check the availability of a free slot
  1635. if( freeGlobalPropertyIds.GetLength() )
  1636. {
  1637. prop->id = freeGlobalPropertyIds.PopLast();
  1638. globalProperties[prop->id] = prop;
  1639. return prop;
  1640. }
  1641. prop->id = (asUINT)globalProperties.GetLength();
  1642. globalProperties.PushLast(prop);
  1643. return prop;
  1644. }
  1645. // internal
  1646. void asCScriptEngine::FreeUnusedGlobalProperties()
  1647. {
  1648. for( asUINT n = 0; n < globalProperties.GetLength(); n++ )
  1649. {
  1650. if( globalProperties[n] && globalProperties[n]->GetRefCount() == 0 )
  1651. {
  1652. freeGlobalPropertyIds.PushLast(n);
  1653. asDELETE(globalProperties[n], asCGlobalProperty);
  1654. globalProperties[n] = 0;
  1655. }
  1656. }
  1657. }
  1658. // interface
  1659. asUINT asCScriptEngine::GetGlobalPropertyCount() const
  1660. {
  1661. return registeredGlobalProps.GetLength();
  1662. }
  1663. // interface
  1664. // TODO: If the typeId ever encodes the const flag, then the isConst parameter should be removed
  1665. int asCScriptEngine::GetGlobalPropertyByIndex(asUINT index, const char **name, int *typeId, bool *isConst, const char **configGroup, void **pointer) const
  1666. {
  1667. if( index >= registeredGlobalProps.GetLength() )
  1668. return asINVALID_ARG;
  1669. if( name )
  1670. *name = registeredGlobalProps[index]->name.AddressOf();
  1671. if( configGroup )
  1672. {
  1673. asCConfigGroup *group = FindConfigGroupForGlobalVar(index);
  1674. if( group )
  1675. *configGroup = group->groupName.AddressOf();
  1676. else
  1677. *configGroup = 0;
  1678. }
  1679. if( typeId )
  1680. *typeId = GetTypeIdFromDataType(registeredGlobalProps[index]->type);
  1681. if( isConst )
  1682. *isConst = registeredGlobalProps[index]->type.IsReadOnly();
  1683. if( pointer )
  1684. *pointer = registeredGlobalProps[index]->GetRegisteredAddress();
  1685. return asSUCCESS;
  1686. }
  1687. // interface
  1688. int asCScriptEngine::RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1689. {
  1690. if( obj == 0 )
  1691. return ConfigError(asINVALID_ARG);
  1692. // Determine the object type
  1693. asCDataType dt;
  1694. asCBuilder bld(this, 0);
  1695. int r = bld.ParseDataType(obj, &dt);
  1696. if( r < 0 )
  1697. return ConfigError(r);
  1698. if( dt.GetObjectType() == 0 )
  1699. return ConfigError(asINVALID_ARG);
  1700. return RegisterMethodToObjectType(dt.GetObjectType(), declaration, funcPointer, callConv);
  1701. }
  1702. // internal
  1703. int asCScriptEngine::RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1704. {
  1705. asSSystemFunctionInterface internal;
  1706. int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
  1707. if( r < 0 )
  1708. return ConfigError(r);
  1709. // We only support these calling conventions for object methods
  1710. #ifdef AS_MAX_PORTABILITY
  1711. if( callConv != asCALL_GENERIC )
  1712. return ConfigError(asNOT_SUPPORTED);
  1713. #else
  1714. if( callConv != asCALL_THISCALL &&
  1715. callConv != asCALL_CDECL_OBJLAST &&
  1716. callConv != asCALL_CDECL_OBJFIRST &&
  1717. callConv != asCALL_GENERIC )
  1718. return ConfigError(asNOT_SUPPORTED);
  1719. #endif
  1720. isPrepared = false;
  1721. // Put the system function in the list of system functions
  1722. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  1723. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1724. func->sysFuncIntf = newInterface;
  1725. func->objectType = objectType;
  1726. asCBuilder bld(this, 0);
  1727. r = bld.ParseFunctionDeclaration(func->objectType, declaration, func, true, &newInterface->paramAutoHandles, &newInterface->returnAutoHandle);
  1728. if( r < 0 )
  1729. {
  1730. // Set as dummy function before deleting
  1731. func->funcType = asFUNC_DUMMY;
  1732. asDELETE(func,asCScriptFunction);
  1733. return ConfigError(asINVALID_DECLARATION);
  1734. }
  1735. // Check name conflicts
  1736. r = bld.CheckNameConflictMember(objectType, func->name.AddressOf(), 0, 0, false);
  1737. if( r < 0 )
  1738. {
  1739. asDELETE(func,asCScriptFunction);
  1740. return ConfigError(asNAME_TAKEN);
  1741. }
  1742. func->id = GetNextScriptFunctionId();
  1743. func->objectType->methods.PushLast(func->id);
  1744. func->accessMask = defaultAccessMask;
  1745. SetScriptFunction(func);
  1746. // TODO: This code is repeated in many places
  1747. // If parameter type from other groups are used, add references
  1748. if( func->returnType.GetObjectType() )
  1749. {
  1750. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  1751. currentGroup->RefConfigGroup(group);
  1752. }
  1753. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1754. {
  1755. if( func->parameterTypes[n].GetObjectType() )
  1756. {
  1757. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  1758. currentGroup->RefConfigGroup(group);
  1759. }
  1760. }
  1761. // Check if the method restricts that use of the template to value types or reference types
  1762. if( func->objectType->flags & asOBJ_TEMPLATE )
  1763. {
  1764. if( func->returnType.GetObjectType() == func->objectType->templateSubType.GetObjectType() )
  1765. {
  1766. if( func->returnType.IsObjectHandle() )
  1767. func->objectType->acceptValueSubType = false;
  1768. else if( !func->returnType.IsReference() )
  1769. func->objectType->acceptRefSubType = false;
  1770. }
  1771. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1772. {
  1773. if( func->parameterTypes[n].GetObjectType() == func->objectType->templateSubType.GetObjectType() )
  1774. {
  1775. // TODO: If unsafe references are allowed, then inout references allow value types
  1776. if( func->parameterTypes[n].IsObjectHandle() || (func->parameterTypes[n].IsReference() && func->inOutFlags[n] == asTM_INOUTREF) )
  1777. func->objectType->acceptValueSubType = false;
  1778. else if( !func->parameterTypes[n].IsReference() )
  1779. func->objectType->acceptRefSubType = false;
  1780. }
  1781. }
  1782. }
  1783. // TODO: beh.copy member will be removed, so this is not necessary
  1784. // Is this the default copy behaviour?
  1785. if( func->name == "opAssign" && func->parameterTypes.GetLength() == 1 && func->isReadOnly == false &&
  1786. (objectType->flags & asOBJ_SCRIPT_OBJECT || func->parameterTypes[0].IsEqualExceptRefAndConst(asCDataType::CreateObject(func->objectType, false))) )
  1787. {
  1788. func->objectType->beh.copy = func->id;
  1789. func->AddRef();
  1790. }
  1791. // Return the function id as success
  1792. return func->id;
  1793. }
  1794. // interface
  1795. int asCScriptEngine::RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1796. {
  1797. asSSystemFunctionInterface internal;
  1798. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1799. if( r < 0 )
  1800. return ConfigError(r);
  1801. #ifdef AS_MAX_PORTABILITY
  1802. if( callConv != asCALL_GENERIC )
  1803. return ConfigError(asNOT_SUPPORTED);
  1804. #else
  1805. if( callConv != asCALL_CDECL &&
  1806. callConv != asCALL_STDCALL &&
  1807. callConv != asCALL_GENERIC )
  1808. return ConfigError(asNOT_SUPPORTED);
  1809. #endif
  1810. isPrepared = false;
  1811. // Put the system function in the list of system functions
  1812. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  1813. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1814. func->sysFuncIntf = newInterface;
  1815. asCBuilder bld(this, 0);
  1816. r = bld.ParseFunctionDeclaration(0, declaration, func, true, &newInterface->paramAutoHandles, &newInterface->returnAutoHandle);
  1817. if( r < 0 )
  1818. {
  1819. // Set as dummy function before deleting
  1820. func->funcType = asFUNC_DUMMY;
  1821. asDELETE(func,asCScriptFunction);
  1822. return ConfigError(asINVALID_DECLARATION);
  1823. }
  1824. // Check name conflicts
  1825. r = bld.CheckNameConflict(func->name.AddressOf(), 0, 0);
  1826. if( r < 0 )
  1827. {
  1828. asDELETE(func,asCScriptFunction);
  1829. return ConfigError(asNAME_TAKEN);
  1830. }
  1831. func->id = GetNextScriptFunctionId();
  1832. SetScriptFunction(func);
  1833. currentGroup->scriptFunctions.PushLast(func);
  1834. func->accessMask = defaultAccessMask;
  1835. registeredGlobalFuncs.PushLast(func);
  1836. // If parameter type from other groups are used, add references
  1837. if( func->returnType.GetObjectType() )
  1838. {
  1839. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  1840. currentGroup->RefConfigGroup(group);
  1841. }
  1842. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1843. {
  1844. if( func->parameterTypes[n].GetObjectType() )
  1845. {
  1846. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  1847. currentGroup->RefConfigGroup(group);
  1848. }
  1849. }
  1850. // Return the function id as success
  1851. return func->id;
  1852. }
  1853. // interface
  1854. asUINT asCScriptEngine::GetGlobalFunctionCount() const
  1855. {
  1856. return registeredGlobalFuncs.GetLength();
  1857. }
  1858. // interface
  1859. int asCScriptEngine::GetGlobalFunctionIdByIndex(asUINT index) const
  1860. {
  1861. if( index >= registeredGlobalFuncs.GetLength() )
  1862. return asINVALID_ARG;
  1863. return registeredGlobalFuncs[index]->id;
  1864. }
  1865. // interface
  1866. asIScriptFunction *asCScriptEngine::GetGlobalFunctionByIndex(asUINT index) const
  1867. {
  1868. if( index >= registeredGlobalFuncs.GetLength() )
  1869. return 0;
  1870. return registeredGlobalFuncs[index];
  1871. }
  1872. // interface
  1873. asIScriptFunction *asCScriptEngine::GetGlobalFunctionByDecl(const char *decl) const
  1874. {
  1875. asCBuilder bld(const_cast<asCScriptEngine*>(this), 0);
  1876. asCScriptFunction func(const_cast<asCScriptEngine*>(this), 0, asFUNC_DUMMY);
  1877. int r = bld.ParseFunctionDeclaration(0, decl, &func, false);
  1878. if( r < 0 )
  1879. return 0;
  1880. // TODO: optimize: Improve linear search
  1881. // Search registered functions for matching interface
  1882. int id = -1;
  1883. for( size_t n = 0; n < registeredGlobalFuncs.GetLength(); ++n )
  1884. {
  1885. if( registeredGlobalFuncs[n]->objectType == 0 &&
  1886. func.name == registeredGlobalFuncs[n]->name &&
  1887. func.returnType == registeredGlobalFuncs[n]->returnType &&
  1888. func.parameterTypes.GetLength() == registeredGlobalFuncs[n]->parameterTypes.GetLength() )
  1889. {
  1890. bool match = true;
  1891. for( size_t p = 0; p < func.parameterTypes.GetLength(); ++p )
  1892. {
  1893. if( func.parameterTypes[p] != registeredGlobalFuncs[n]->parameterTypes[p] )
  1894. {
  1895. match = false;
  1896. break;
  1897. }
  1898. }
  1899. if( match )
  1900. {
  1901. if( id == -1 )
  1902. id = registeredGlobalFuncs[n]->id;
  1903. else
  1904. return 0; // Multiple matches
  1905. }
  1906. }
  1907. }
  1908. if( id < 0 ) return 0; // No matches
  1909. return registeredGlobalFuncs[id];
  1910. }
  1911. asCObjectType *asCScriptEngine::GetObjectType(const char *type)
  1912. {
  1913. // TODO: optimize: Improve linear search
  1914. for( asUINT n = 0; n < objectTypes.GetLength(); n++ )
  1915. if( objectTypes[n] &&
  1916. objectTypes[n]->name == type ) // TODO: template: Should we check the subtype in case of template instances?
  1917. return objectTypes[n];
  1918. return 0;
  1919. }
  1920. void asCScriptEngine::PrepareEngine()
  1921. {
  1922. if( isPrepared ) return;
  1923. if( configFailed ) return;
  1924. asUINT n;
  1925. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  1926. {
  1927. // Determine the host application interface
  1928. if( scriptFunctions[n] && scriptFunctions[n]->funcType == asFUNC_SYSTEM )
  1929. {
  1930. if( scriptFunctions[n]->sysFuncIntf->callConv == ICC_GENERIC_FUNC ||
  1931. scriptFunctions[n]->sysFuncIntf->callConv == ICC_GENERIC_METHOD )
  1932. PrepareSystemFunctionGeneric(scriptFunctions[n], scriptFunctions[n]->sysFuncIntf, this);
  1933. else
  1934. PrepareSystemFunction(scriptFunctions[n], scriptFunctions[n]->sysFuncIntf, this);
  1935. }
  1936. }
  1937. // Validate object type registrations
  1938. for( n = 0; n < objectTypes.GetLength(); n++ )
  1939. {
  1940. if( objectTypes[n] && !(objectTypes[n]->flags & asOBJ_SCRIPT_OBJECT) )
  1941. {
  1942. bool missingBehaviour = false;
  1943. const char *infoMsg = 0;
  1944. // Verify that GC types have all behaviours
  1945. if( objectTypes[n]->flags & asOBJ_GC )
  1946. {
  1947. if( objectTypes[n]->beh.addref == 0 ||
  1948. objectTypes[n]->beh.release == 0 ||
  1949. objectTypes[n]->beh.gcGetRefCount == 0 ||
  1950. objectTypes[n]->beh.gcSetFlag == 0 ||
  1951. objectTypes[n]->beh.gcGetFlag == 0 ||
  1952. objectTypes[n]->beh.gcEnumReferences == 0 ||
  1953. objectTypes[n]->beh.gcReleaseAllReferences == 0 )
  1954. {
  1955. infoMsg = TXT_GC_REQUIRE_ADD_REL_GC_BEHAVIOUR;
  1956. missingBehaviour = true;
  1957. }
  1958. }
  1959. // Verify that scoped ref types have the release behaviour
  1960. else if( objectTypes[n]->flags & asOBJ_SCOPED )
  1961. {
  1962. if( objectTypes[n]->beh.release == 0 )
  1963. {
  1964. infoMsg = TXT_SCOPE_REQUIRE_REL_BEHAVIOUR;
  1965. missingBehaviour = true;
  1966. }
  1967. }
  1968. // Verify that ref types have add ref and release behaviours
  1969. else if( (objectTypes[n]->flags & asOBJ_REF) &&
  1970. !(objectTypes[n]->flags & asOBJ_NOHANDLE) )
  1971. {
  1972. if( objectTypes[n]->beh.addref == 0 ||
  1973. objectTypes[n]->beh.release == 0 )
  1974. {
  1975. infoMsg = TXT_REF_REQUIRE_ADD_REL_BEHAVIOUR;
  1976. missingBehaviour = true;
  1977. }
  1978. }
  1979. // Verify that non-pod value types have the constructor and destructor registered
  1980. else if( (objectTypes[n]->flags & asOBJ_VALUE) &&
  1981. !(objectTypes[n]->flags & asOBJ_POD) )
  1982. {
  1983. if( objectTypes[n]->beh.construct == 0 ||
  1984. objectTypes[n]->beh.destruct == 0 )
  1985. {
  1986. infoMsg = TXT_NON_POD_REQUIRE_CONSTR_DESTR_BEHAVIOUR;
  1987. missingBehaviour = true;
  1988. }
  1989. }
  1990. if( missingBehaviour )
  1991. {
  1992. asCString str;
  1993. str.Format(TXT_TYPE_s_IS_MISSING_BEHAVIOURS, objectTypes[n]->name.AddressOf());
  1994. WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1995. WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, infoMsg);
  1996. ConfigError(asINVALID_CONFIGURATION);
  1997. }
  1998. }
  1999. }
  2000. isPrepared = true;
  2001. }
  2002. int asCScriptEngine::ConfigError(int err)
  2003. {
  2004. configFailed = true;
  2005. return err;
  2006. }
  2007. // interface
  2008. int asCScriptEngine::RegisterDefaultArrayType(const char *type)
  2009. {
  2010. asCBuilder bld(this, 0);
  2011. asCDataType dt;
  2012. int r = bld.ParseDataType(type, &dt);
  2013. if( r < 0 ) return r;
  2014. if( dt.GetObjectType() == 0 ||
  2015. !(dt.GetObjectType()->GetFlags() & asOBJ_TEMPLATE) )
  2016. return asINVALID_TYPE;
  2017. defaultArrayObjectType = dt.GetObjectType();
  2018. defaultArrayObjectType->AddRef();
  2019. return 0;
  2020. }
  2021. // interface
  2022. int asCScriptEngine::GetDefaultArrayTypeId() const
  2023. {
  2024. if( defaultArrayObjectType )
  2025. return GetTypeIdFromDataType(asCDataType::CreateObject(defaultArrayObjectType, false));
  2026. return asINVALID_TYPE;
  2027. }
  2028. // interface
  2029. int asCScriptEngine::RegisterStringFactory(const char *datatype, const asSFuncPtr &funcPointer, asDWORD callConv)
  2030. {
  2031. asSSystemFunctionInterface internal;
  2032. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  2033. if( r < 0 )
  2034. return ConfigError(r);
  2035. #ifdef AS_MAX_PORTABILITY
  2036. if( callConv != asCALL_GENERIC )
  2037. return ConfigError(asNOT_SUPPORTED);
  2038. #else
  2039. if( callConv != asCALL_CDECL &&
  2040. callConv != asCALL_STDCALL &&
  2041. callConv != asCALL_GENERIC )
  2042. return ConfigError(asNOT_SUPPORTED);
  2043. #endif
  2044. // Put the system function in the list of system functions
  2045. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  2046. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  2047. func->name = "_string_factory_";
  2048. func->sysFuncIntf = newInterface;
  2049. asCBuilder bld(this, 0);
  2050. asCDataType dt;
  2051. r = bld.ParseDataType(datatype, &dt);
  2052. if( r < 0 )
  2053. {
  2054. // Set as dummy before deleting
  2055. func->funcType = asFUNC_DUMMY;
  2056. asDELETE(func,asCScriptFunction);
  2057. return ConfigError(asINVALID_TYPE);
  2058. }
  2059. func->returnType = dt;
  2060. func->parameterTypes.PushLast(asCDataType::CreatePrimitive(ttInt, true));
  2061. asCDataType parm1 = asCDataType::CreatePrimitive(ttUInt8, true);
  2062. parm1.MakeReference(true);
  2063. func->parameterTypes.PushLast(parm1);
  2064. func->id = GetNextScriptFunctionId();
  2065. SetScriptFunction(func);
  2066. stringFactory = func;
  2067. if( func->returnType.GetObjectType() )
  2068. {
  2069. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  2070. if( group == 0 ) group = &defaultGroup;
  2071. group->scriptFunctions.PushLast(func);
  2072. }
  2073. // Register function id as success
  2074. return func->id;
  2075. }
  2076. // interface
  2077. int asCScriptEngine::GetStringFactoryReturnTypeId() const
  2078. {
  2079. if( stringFactory == 0 )
  2080. return asNO_FUNCTION;
  2081. return GetTypeIdFromDataType(stringFactory->returnType);
  2082. }
  2083. // interface
  2084. asCModule *asCScriptEngine::GetModule(const char *_name, bool create)
  2085. {
  2086. // Accept null as well as zero-length string
  2087. const char *name = "";
  2088. if( _name != 0 ) name = _name;
  2089. if( lastModule && lastModule->name == name )
  2090. return lastModule;
  2091. // TODO: optimize: Improve linear search
  2092. for( asUINT n = 0; n < scriptModules.GetLength(); ++n )
  2093. if( scriptModules[n] && scriptModules[n]->name == name )
  2094. {
  2095. lastModule = scriptModules[n];
  2096. return lastModule;
  2097. }
  2098. if( create )
  2099. {
  2100. asCModule *module = asNEW(asCModule)(name, this);
  2101. scriptModules.PushLast(module);
  2102. lastModule = module;
  2103. return lastModule;
  2104. }
  2105. return 0;
  2106. }
  2107. asCModule *asCScriptEngine::GetModuleFromFuncId(int id)
  2108. {
  2109. if( id < 0 ) return 0;
  2110. if( id >= (int)scriptFunctions.GetLength() ) return 0;
  2111. asCScriptFunction *func = scriptFunctions[id];
  2112. if( func == 0 ) return 0;
  2113. return func->module;
  2114. }
  2115. // internal
  2116. int asCScriptEngine::RequestBuild()
  2117. {
  2118. ENTERCRITICALSECTION(engineCritical);
  2119. if( isBuilding )
  2120. {
  2121. LEAVECRITICALSECTION(engineCritical);
  2122. return asBUILD_IN_PROGRESS;
  2123. }
  2124. isBuilding = true;
  2125. LEAVECRITICALSECTION(engineCritical);
  2126. return 0;
  2127. }
  2128. // internal
  2129. void asCScriptEngine::BuildCompleted()
  2130. {
  2131. // Always free up pooled memory after a completed build
  2132. memoryMgr.FreeUnusedMemory();
  2133. isBuilding = false;
  2134. }
  2135. void asCScriptEngine::RemoveTemplateInstanceType(asCObjectType *t)
  2136. {
  2137. int n;
  2138. // Destroy the factory stubs
  2139. for( n = 0; n < (int)t->beh.factories.GetLength(); n++ )
  2140. {
  2141. // Make sure the factory stub isn't referencing this object anymore
  2142. scriptFunctions[t->beh.factories[n]]->ReleaseAllHandles(this);
  2143. scriptFunctions[t->beh.factories[n]]->Release();
  2144. }
  2145. t->beh.factories.SetLength(0);
  2146. // Destroy the stub for the list factory too
  2147. if( t->beh.listFactory )
  2148. {
  2149. scriptFunctions[t->beh.listFactory]->ReleaseAllHandles(this);
  2150. scriptFunctions[t->beh.listFactory]->Release();
  2151. t->beh.listFactory = 0;
  2152. }
  2153. // Destroy the specialized functions
  2154. for( n = 1; n < (int)t->beh.operators.GetLength(); n += 2 )
  2155. {
  2156. if( t->beh.operators[n] && scriptFunctions[t->beh.operators[n]]->objectType == t )
  2157. {
  2158. scriptFunctions[t->beh.operators[n]]->Release();
  2159. }
  2160. }
  2161. t->beh.operators.SetLength(0);
  2162. // Start searching from the end of the list, as most of
  2163. // the time it will be the last two types
  2164. for( n = (int)templateTypes.GetLength()-1; n >= 0; n-- )
  2165. {
  2166. if( templateTypes[n] == t )
  2167. {
  2168. if( n == (signed)templateTypes.GetLength()-1 )
  2169. templateTypes.PopLast();
  2170. else
  2171. templateTypes[n] = templateTypes.PopLast();
  2172. }
  2173. }
  2174. for( n = (int)templateInstanceTypes.GetLength()-1; n >= 0; n-- )
  2175. {
  2176. if( templateInstanceTypes[n] == t )
  2177. {
  2178. if( n == (signed)templateInstanceTypes.GetLength()-1 )
  2179. templateInstanceTypes.PopLast();
  2180. else
  2181. templateInstanceTypes[n] = templateInstanceTypes.PopLast();
  2182. }
  2183. }
  2184. asDELETE(t,asCObjectType);
  2185. }
  2186. // internal
  2187. asCObjectType *asCScriptEngine::GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType)
  2188. {
  2189. asUINT n;
  2190. // Is there any template instance type or template specialization already with this subtype?
  2191. for( n = 0; n < templateTypes.GetLength(); n++ )
  2192. {
  2193. if( templateTypes[n] &&
  2194. templateTypes[n]->name == templateType->name &&
  2195. templateTypes[n]->templateSubType == subType )
  2196. return templateTypes[n];
  2197. }
  2198. // No previous template instance exists
  2199. // Make sure the template has factories as it doesn't have a default factory
  2200. if( templateType->beh.factories.GetLength() == 0 )
  2201. return 0;
  2202. // Make sure this template supports the subtype
  2203. if( !templateType->acceptValueSubType && (subType.IsPrimitive() || (subType.GetObjectType()->flags & asOBJ_VALUE)) )
  2204. return 0;
  2205. if( !templateType->acceptRefSubType && (subType.IsObject() && (subType.GetObjectType()->flags & asOBJ_REF)) )
  2206. return 0;
  2207. // Create a new template instance type based on the templateType
  2208. asCObjectType *ot = asNEW(asCObjectType)(this);
  2209. ot->templateSubType = subType;
  2210. ot->flags = templateType->flags;
  2211. ot->size = templateType->size;
  2212. ot->name = templateType->name;
  2213. // Before filling in the methods, call the template instance callback behaviour to validate the type
  2214. if( templateType->beh.templateCallback )
  2215. {
  2216. asCScriptFunction *callback = scriptFunctions[templateType->beh.templateCallback];
  2217. if( !CallGlobalFunctionRetBool(ot, 0, callback->sysFuncIntf, callback) )
  2218. {
  2219. // The type cannot be instanciated
  2220. ot->templateSubType = asCDataType();
  2221. asDELETE(ot, asCObjectType);
  2222. return 0;
  2223. }
  2224. }
  2225. ot->methods = templateType->methods;
  2226. for( n = 0; n < ot->methods.GetLength(); n++ )
  2227. scriptFunctions[ot->methods[n]]->AddRef();
  2228. // Store the real factory in the constructor. This is used by the CreateScriptObject function.
  2229. // Otherwise it wouldn't be necessary to store the real factory ids.
  2230. ot->beh.construct = templateType->beh.factory;
  2231. ot->beh.constructors = templateType->beh.factories;
  2232. for( n = 0; n < ot->beh.constructors.GetLength(); n++ )
  2233. scriptFunctions[ot->beh.constructors[n]]->AddRef();
  2234. // Generate factory stubs for each of the factories
  2235. for( n = 0; n < templateType->beh.factories.GetLength(); n++ )
  2236. {
  2237. asCScriptFunction *func = GenerateTemplateFactoryStub(templateType, ot, templateType->beh.factories[n]);
  2238. // The function's refCount was already initialized to 1
  2239. ot->beh.factories.PushLast(func->id);
  2240. }
  2241. if( ot->beh.factories.GetLength() )
  2242. ot->beh.factory = ot->beh.factories[0];
  2243. else
  2244. {
  2245. asASSERT(false);
  2246. ot->beh.factory = templateType->beh.factory;
  2247. }
  2248. // Generate stub for the list factory as well
  2249. if( templateType->beh.listFactory )
  2250. {
  2251. asCScriptFunction *func = GenerateTemplateFactoryStub(templateType, ot, templateType->beh.listFactory);
  2252. // The function's refCount was already initialized to 1
  2253. ot->beh.listFactory = func->id;
  2254. }
  2255. ot->beh.addref = templateType->beh.addref;
  2256. if( scriptFunctions[ot->beh.addref] ) scriptFunctions[ot->beh.addref]->AddRef();
  2257. ot->beh.release = templateType->beh.release;
  2258. if( scriptFunctions[ot->beh.release] ) scriptFunctions[ot->beh.release]->AddRef();
  2259. ot->beh.copy = templateType->beh.copy;
  2260. if( scriptFunctions[ot->beh.copy] ) scriptFunctions[ot->beh.copy]->AddRef();
  2261. ot->beh.operators = templateType->beh.operators;
  2262. for( n = 1; n < ot->beh.operators.GetLength(); n += 2 )
  2263. {
  2264. scriptFunctions[ot->beh.operators[n]]->AddRef();
  2265. }
  2266. ot->beh.gcGetRefCount = templateType->beh.gcGetRefCount;
  2267. if( scriptFunctions[ot->beh.gcGetRefCount] ) scriptFunctions[ot->beh.gcGetRefCount]->AddRef();
  2268. ot->beh.gcSetFlag = templateType->beh.gcSetFlag;
  2269. if( scriptFunctions[ot->beh.gcSetFlag] ) scriptFunctions[ot->beh.gcSetFlag]->AddRef();
  2270. ot->beh.gcGetFlag = templateType->beh.gcGetFlag;
  2271. if( scriptFunctions[ot->beh.gcGetFlag] ) scriptFunctions[ot->beh.gcGetFlag]->AddRef();
  2272. ot->beh.gcEnumReferences = templateType->beh.gcEnumReferences;
  2273. if( scriptFunctions[ot->beh.gcEnumReferences] ) scriptFunctions[ot->beh.gcEnumReferences]->AddRef();
  2274. ot->beh.gcReleaseAllReferences = templateType->beh.gcReleaseAllReferences;
  2275. if( scriptFunctions[ot->beh.gcReleaseAllReferences] ) scriptFunctions[ot->beh.gcReleaseAllReferences]->AddRef();
  2276. // As the new template type is instanciated, the engine should
  2277. // generate new functions to substitute the ones with the template subtype.
  2278. for( n = 1; n < ot->beh.operators.GetLength(); n += 2 )
  2279. {
  2280. int funcId = ot->beh.operators[n];
  2281. asCScriptFunction *func = scriptFunctions[funcId];
  2282. if( GenerateNewTemplateFunction(templateType, ot, subType, func, &func) )
  2283. {
  2284. // Release the old function, the new one already has its ref count set to 1
  2285. scriptFunctions[ot->beh.operators[n]]->Release();
  2286. ot->beh.operators[n] = func->id;
  2287. }
  2288. }
  2289. // As the new template type is instanciated, the engine should
  2290. // generate new functions to substitute the ones with the template subtype.
  2291. for( n = 0; n < ot->methods.GetLength(); n++ )
  2292. {
  2293. int funcId = ot->methods[n];
  2294. asCScriptFunction *func = scriptFunctions[funcId];
  2295. if( GenerateNewTemplateFunction(templateType, ot, subType, func, &func) )
  2296. {
  2297. // Release the old function, the new one already has its ref count set to 1
  2298. scriptFunctions[ot->methods[n]]->Release();
  2299. ot->methods[n] = func->id;
  2300. }
  2301. }
  2302. // Increase ref counter for sub type if it is an object type
  2303. if( ot->templateSubType.GetObjectType() ) ot->templateSubType.GetObjectType()->AddRef();
  2304. // Verify if the subtype contains a garbage collected object, in which case this template is a potential circular reference.
  2305. // A handle can potentially hold derived types, which may be garbage collected so to be safe we have to set the GC flag.
  2306. if( ot->templateSubType.IsObjectHandle() || (ot->templateSubType.GetObjectType() && (ot->templateSubType.GetObjectType()->flags & asOBJ_GC)) )
  2307. ot->flags |= asOBJ_GC;
  2308. templateTypes.PushLast(ot);
  2309. // We need to store the object type somewhere for clean-up later
  2310. // TODO: Why do we need both templateTypes and templateInstanceTypes? It is possible to differ between template instance and template specialization by checking for the asOBJ_TEMPLATE flag
  2311. templateInstanceTypes.PushLast(ot);
  2312. return ot;
  2313. }
  2314. // internal
  2315. asCScriptFunction *asCScriptEngine::GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *ot, int factoryId)
  2316. {
  2317. asCScriptFunction *factory = scriptFunctions[factoryId];
  2318. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SCRIPT);
  2319. func->name = "factstub";
  2320. func->id = GetNextScriptFunctionId();
  2321. func->returnType = asCDataType::CreateObjectHandle(ot, false);
  2322. // Skip the first parameter as this is the object type pointer that the stub will add
  2323. func->parameterTypes.SetLength(factory->parameterTypes.GetLength()-1);
  2324. func->inOutFlags.SetLength(factory->inOutFlags.GetLength()-1);
  2325. for( asUINT p = 1; p < factory->parameterTypes.GetLength(); p++ )
  2326. {
  2327. if( factory->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() )
  2328. {
  2329. func->parameterTypes[p-1] = ot->templateSubType;
  2330. if( factory->parameterTypes[p].IsObjectHandle() )
  2331. func->parameterTypes[p-1].MakeHandle(true);
  2332. func->parameterTypes[p-1].MakeReference(factory->parameterTypes[p].IsReference());
  2333. func->parameterTypes[p-1].MakeReadOnly(factory->parameterTypes[p].IsReference());
  2334. }
  2335. else if( factory->parameterTypes[p].GetObjectType() == templateType )
  2336. {
  2337. if( factory->parameterTypes[p].IsObjectHandle() )
  2338. func->parameterTypes[p-1] = asCDataType::CreateObjectHandle(ot, false);
  2339. else
  2340. func->parameterTypes[p-1] = asCDataType::CreateObject(ot, false);
  2341. func->parameterTypes[p-1].MakeReference(factory->parameterTypes[p].IsReference());
  2342. func->parameterTypes[p-1].MakeReadOnly(factory->parameterTypes[p].IsReadOnly());
  2343. }
  2344. else
  2345. {
  2346. func->parameterTypes[p-1] = factory->parameterTypes[p];
  2347. }
  2348. func->inOutFlags[p-1] = factory->inOutFlags[p];
  2349. }
  2350. SetScriptFunction(func);
  2351. asCBuilder builder(this, 0);
  2352. asCCompiler compiler(this);
  2353. compiler.CompileTemplateFactoryStub(&builder, factoryId, ot, func);
  2354. return func;
  2355. }
  2356. bool asCScriptEngine::GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *ot, asCDataType &subType, asCScriptFunction *func, asCScriptFunction **newFunc)
  2357. {
  2358. bool needNewFunc = false;
  2359. if( func->returnType.GetObjectType() == templateType->templateSubType.GetObjectType() ||
  2360. func->returnType.GetObjectType() == templateType )
  2361. needNewFunc = true;
  2362. else
  2363. {
  2364. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  2365. {
  2366. if( func->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() ||
  2367. func->parameterTypes[p].GetObjectType() == templateType )
  2368. {
  2369. needNewFunc = true;
  2370. break;
  2371. }
  2372. }
  2373. }
  2374. if( needNewFunc )
  2375. {
  2376. asCScriptFunction *func2 = asNEW(asCScriptFunction)(this, 0, func->funcType);
  2377. func2->name = func->name;
  2378. func2->id = GetNextScriptFunctionId();
  2379. if( func->returnType.GetObjectType() == templateType->templateSubType.GetObjectType() )
  2380. {
  2381. func2->returnType = subType;
  2382. if( func->returnType.IsObjectHandle() )
  2383. func2->returnType.MakeHandle(true, true);
  2384. func2->returnType.MakeReference(func->returnType.IsReference());
  2385. func2->returnType.MakeReadOnly(func->returnType.IsReadOnly());
  2386. }
  2387. else if( func->returnType.GetObjectType() == templateType )
  2388. {
  2389. if( func2->returnType.IsObjectHandle() )
  2390. func2->returnType = asCDataType::CreateObjectHandle(ot, false);
  2391. else
  2392. func2->returnType = asCDataType::CreateObject(ot, false);
  2393. func2->returnType.MakeReference(func->returnType.IsReference());
  2394. func2->returnType.MakeReadOnly(func->returnType.IsReadOnly());
  2395. }
  2396. else
  2397. func2->returnType = func->returnType;
  2398. func2->parameterTypes.SetLength(func->parameterTypes.GetLength());
  2399. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  2400. {
  2401. if( func->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() )
  2402. {
  2403. func2->parameterTypes[p] = subType;
  2404. if( func->parameterTypes[p].IsObjectHandle() )
  2405. func2->parameterTypes[p].MakeHandle(true);
  2406. func2->parameterTypes[p].MakeReference(func->parameterTypes[p].IsReference());
  2407. func2->parameterTypes[p].MakeReadOnly(func->parameterTypes[p].IsReference());
  2408. }
  2409. else if( func->parameterTypes[p].GetObjectType() == templateType )
  2410. {
  2411. if( func->parameterTypes[p].IsObjectHandle() )
  2412. func2->parameterTypes[p] = asCDataType::CreateObjectHandle(ot, false);
  2413. else
  2414. func2->parameterTypes[p] = asCDataType::CreateObject(ot, false);
  2415. func2->parameterTypes[p].MakeReference(func->parameterTypes[p].IsReference());
  2416. func2->parameterTypes[p].MakeReadOnly(func->parameterTypes[p].IsReadOnly());
  2417. }
  2418. else
  2419. func2->parameterTypes[p] = func->parameterTypes[p];
  2420. }
  2421. // TODO: template: Must be careful when instanciating templates for garbage collected types
  2422. // If the template hasn't been registered with the behaviours, it shouldn't
  2423. // permit instanciation of garbage collected types that in turn may refer to
  2424. // this instance.
  2425. func2->inOutFlags = func->inOutFlags;
  2426. func2->isReadOnly = func->isReadOnly;
  2427. func2->objectType = ot;
  2428. func2->stackNeeded = func->stackNeeded;
  2429. func2->sysFuncIntf = asNEW(asSSystemFunctionInterface)(*func->sysFuncIntf);
  2430. SetScriptFunction(func2);
  2431. // Return the new function
  2432. *newFunc = func2;
  2433. }
  2434. return needNewFunc;
  2435. }
  2436. void asCScriptEngine::CallObjectMethod(void *obj, int func)
  2437. {
  2438. asCScriptFunction *s = scriptFunctions[func];
  2439. asASSERT( s != 0 );
  2440. CallObjectMethod(obj, s->sysFuncIntf, s);
  2441. }
  2442. void asCScriptEngine::CallObjectMethod(void *obj, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2443. {
  2444. #ifdef __GNUC__
  2445. if( i->callConv == ICC_GENERIC_METHOD )
  2446. {
  2447. asCGeneric gen(this, s, obj, 0);
  2448. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2449. f(&gen);
  2450. }
  2451. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2452. {
  2453. // For virtual thiscalls we must call the method as a true class method
  2454. // so that the compiler will lookup the function address in the vftable
  2455. union
  2456. {
  2457. asSIMPLEMETHOD_t mthd;
  2458. struct
  2459. {
  2460. asFUNCTION_t func;
  2461. asPWORD baseOffset; // Same size as the pointer
  2462. } f;
  2463. } p;
  2464. p.f.func = (void (*)())(i->func);
  2465. p.f.baseOffset = asPWORD(i->baseOffset);
  2466. void (asCSimpleDummy::*f)() = p.mthd;
  2467. (((asCSimpleDummy*)obj)->*f)();
  2468. }
  2469. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2470. {
  2471. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2472. void (*f)(void *) = (void (*)(void *))(i->func);
  2473. f(obj);
  2474. }
  2475. #else
  2476. #ifndef AS_NO_CLASS_METHODS
  2477. if( i->callConv == ICC_THISCALL )
  2478. {
  2479. union
  2480. {
  2481. asSIMPLEMETHOD_t mthd;
  2482. asFUNCTION_t func;
  2483. } p;
  2484. p.func = (void (*)())(i->func);
  2485. void (asCSimpleDummy::*f)() = p.mthd;
  2486. obj = (void*)(size_t(obj) + i->baseOffset);
  2487. (((asCSimpleDummy*)obj)->*f)();
  2488. }
  2489. else
  2490. #endif
  2491. if( i->callConv == ICC_GENERIC_METHOD )
  2492. {
  2493. asCGeneric gen(this, s, obj, 0);
  2494. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2495. f(&gen);
  2496. }
  2497. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2498. {
  2499. void (*f)(void *) = (void (*)(void *))(i->func);
  2500. f(obj);
  2501. }
  2502. #endif
  2503. }
  2504. bool asCScriptEngine::CallObjectMethodRetBool(void *obj, int func)
  2505. {
  2506. asCScriptFunction *s = scriptFunctions[func];
  2507. asASSERT( s != 0 );
  2508. asSSystemFunctionInterface *i = s->sysFuncIntf;
  2509. #ifdef __GNUC__
  2510. if( i->callConv == ICC_GENERIC_METHOD )
  2511. {
  2512. asCGeneric gen(this, s, obj, 0);
  2513. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2514. f(&gen);
  2515. return *(bool*)gen.GetReturnPointer();
  2516. }
  2517. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2518. {
  2519. // For virtual thiscalls we must call the method as a true class method so that the compiler will lookup the function address in the vftable
  2520. union
  2521. {
  2522. asSIMPLEMETHOD_t mthd;
  2523. struct
  2524. {
  2525. asFUNCTION_t func;
  2526. asDWORD baseOffset;
  2527. } f;
  2528. } p;
  2529. p.f.func = (void (*)())(i->func);
  2530. p.f.baseOffset = i->baseOffset;
  2531. bool (asCSimpleDummy::*f)() = (bool (asCSimpleDummy::*)())(p.mthd);
  2532. return (((asCSimpleDummy*)obj)->*f)();
  2533. }
  2534. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2535. {
  2536. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2537. bool (*f)(void *) = (bool (*)(void *))(i->func);
  2538. return f(obj);
  2539. }
  2540. #else
  2541. #ifndef AS_NO_CLASS_METHODS
  2542. if( i->callConv == ICC_THISCALL )
  2543. {
  2544. union
  2545. {
  2546. asSIMPLEMETHOD_t mthd;
  2547. asFUNCTION_t func;
  2548. } p;
  2549. p.func = (void (*)())(i->func);
  2550. bool (asCSimpleDummy::*f)() = (bool (asCSimpleDummy::*)())p.mthd;
  2551. obj = (void*)(size_t(obj) + i->baseOffset);
  2552. return (((asCSimpleDummy*)obj)->*f)();
  2553. }
  2554. else
  2555. #endif
  2556. if( i->callConv == ICC_GENERIC_METHOD )
  2557. {
  2558. asCGeneric gen(this, s, obj, 0);
  2559. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2560. f(&gen);
  2561. return *(bool*)gen.GetReturnPointer();
  2562. }
  2563. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2564. {
  2565. bool (*f)(void *) = (bool (*)(void *))(i->func);
  2566. return f(obj);
  2567. }
  2568. #endif
  2569. }
  2570. int asCScriptEngine::CallObjectMethodRetInt(void *obj, int func)
  2571. {
  2572. asCScriptFunction *s = scriptFunctions[func];
  2573. asASSERT( s != 0 );
  2574. asSSystemFunctionInterface *i = s->sysFuncIntf;
  2575. #ifdef __GNUC__
  2576. if( i->callConv == ICC_GENERIC_METHOD )
  2577. {
  2578. asCGeneric gen(this, s, obj, 0);
  2579. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2580. f(&gen);
  2581. return *(int*)gen.GetReturnPointer();
  2582. }
  2583. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2584. {
  2585. // For virtual thiscalls we must call the method as a true class method so that the compiler will lookup the function address in the vftable
  2586. union
  2587. {
  2588. asSIMPLEMETHOD_t mthd;
  2589. struct
  2590. {
  2591. asFUNCTION_t func;
  2592. asDWORD baseOffset;
  2593. } f;
  2594. } p;
  2595. p.f.func = (void (*)())(i->func);
  2596. p.f.baseOffset = i->baseOffset;
  2597. int (asCSimpleDummy::*f)() = (int (asCSimpleDummy::*)())(p.mthd);
  2598. return (((asCSimpleDummy*)obj)->*f)();
  2599. }
  2600. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2601. {
  2602. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2603. int (*f)(void *) = (int (*)(void *))(i->func);
  2604. return f(obj);
  2605. }
  2606. #else
  2607. #ifndef AS_NO_CLASS_METHODS
  2608. if( i->callConv == ICC_THISCALL )
  2609. {
  2610. union
  2611. {
  2612. asSIMPLEMETHOD_t mthd;
  2613. asFUNCTION_t func;
  2614. } p;
  2615. p.func = (void (*)())(i->func);
  2616. int (asCSimpleDummy::*f)() = (int (asCSimpleDummy::*)())p.mthd;
  2617. obj = (void*)(size_t(obj) + i->baseOffset);
  2618. return (((asCSimpleDummy*)obj)->*f)();
  2619. }
  2620. else
  2621. #endif
  2622. if( i->callConv == ICC_GENERIC_METHOD )
  2623. {
  2624. asCGeneric gen(this, s, obj, 0);
  2625. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2626. f(&gen);
  2627. return *(int*)gen.GetReturnPointer();
  2628. }
  2629. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2630. {
  2631. int (*f)(void *) = (int (*)(void *))(i->func);
  2632. return f(obj);
  2633. }
  2634. #endif
  2635. }
  2636. void *asCScriptEngine::CallGlobalFunctionRetPtr(int func)
  2637. {
  2638. asCScriptFunction *s = scriptFunctions[func];
  2639. asASSERT( s != 0 );
  2640. return CallGlobalFunctionRetPtr(s->sysFuncIntf, s);
  2641. }
  2642. void *asCScriptEngine::CallGlobalFunctionRetPtr(int func, void *param1)
  2643. {
  2644. asCScriptFunction *s = scriptFunctions[func];
  2645. asASSERT( s != 0 );
  2646. return CallGlobalFunctionRetPtr(s->sysFuncIntf, s, param1);
  2647. }
  2648. void *asCScriptEngine::CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s)
  2649. {
  2650. if( i->callConv == ICC_CDECL )
  2651. {
  2652. void *(*f)() = (void *(*)())(i->func);
  2653. return f();
  2654. }
  2655. else if( i->callConv == ICC_STDCALL )
  2656. {
  2657. void *(STDCALL *f)() = (void *(STDCALL *)())(i->func);
  2658. return f();
  2659. }
  2660. else
  2661. {
  2662. asCGeneric gen(this, s, 0, 0);
  2663. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2664. f(&gen);
  2665. return *(void**)gen.GetReturnPointer();
  2666. }
  2667. }
  2668. void *asCScriptEngine::CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1)
  2669. {
  2670. if( i->callConv == ICC_CDECL )
  2671. {
  2672. void *(*f)(void *) = (void *(*)(void *))(i->func);
  2673. return f(param1);
  2674. }
  2675. else if( i->callConv == ICC_STDCALL )
  2676. {
  2677. void *(STDCALL *f)(void *) = (void *(STDCALL *)(void *))(i->func);
  2678. return f(param1);
  2679. }
  2680. else
  2681. {
  2682. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2683. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2684. f(&gen);
  2685. return *(void**)gen.GetReturnPointer();
  2686. }
  2687. }
  2688. void asCScriptEngine::CallObjectMethod(void *obj, void *param, int func)
  2689. {
  2690. asCScriptFunction *s = scriptFunctions[func];
  2691. asASSERT( s != 0 );
  2692. CallObjectMethod(obj, param, s->sysFuncIntf, s);
  2693. }
  2694. void asCScriptEngine::CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2695. {
  2696. #ifdef __GNUC__
  2697. if( i->callConv == ICC_CDECL_OBJLAST )
  2698. {
  2699. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2700. f(param, obj);
  2701. }
  2702. else if( i->callConv == ICC_GENERIC_METHOD )
  2703. {
  2704. asCGeneric gen(this, s, obj, (asDWORD*)&param);
  2705. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2706. f(&gen);
  2707. }
  2708. else /*if( i->callConv == ICC_CDECL_OBJFIRST || i->callConv == ICC_THISCALL )*/
  2709. {
  2710. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2711. f(obj, param);
  2712. }
  2713. #else
  2714. #ifndef AS_NO_CLASS_METHODS
  2715. if( i->callConv == ICC_THISCALL )
  2716. {
  2717. union
  2718. {
  2719. asSIMPLEMETHOD_t mthd;
  2720. asFUNCTION_t func;
  2721. } p;
  2722. p.func = (void (*)())(i->func);
  2723. void (asCSimpleDummy::*f)(void *) = (void (asCSimpleDummy::*)(void *))(p.mthd);
  2724. obj = (void*)(size_t(obj) + i->baseOffset);
  2725. (((asCSimpleDummy*)obj)->*f)(param);
  2726. }
  2727. else
  2728. #endif
  2729. if( i->callConv == ICC_CDECL_OBJLAST )
  2730. {
  2731. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2732. f(param, obj);
  2733. }
  2734. else if( i->callConv == ICC_GENERIC_METHOD )
  2735. {
  2736. asCGeneric gen(this, s, obj, (asDWORD*)&param);
  2737. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2738. f(&gen);
  2739. }
  2740. else /*if( i->callConv == ICC_CDECL_OBJFIRST )*/
  2741. {
  2742. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2743. f(obj, param);
  2744. }
  2745. #endif
  2746. }
  2747. void asCScriptEngine::CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2748. {
  2749. if( i->callConv == ICC_CDECL )
  2750. {
  2751. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2752. f(param1, param2);
  2753. }
  2754. else if( i->callConv == ICC_STDCALL )
  2755. {
  2756. void (STDCALL *f)(void *, void *) = (void (STDCALL *)(void *, void *))(i->func);
  2757. f(param1, param2);
  2758. }
  2759. else
  2760. {
  2761. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2762. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2763. f(&gen);
  2764. }
  2765. }
  2766. bool asCScriptEngine::CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2767. {
  2768. if( i->callConv == ICC_CDECL )
  2769. {
  2770. bool (*f)(void *, void *) = (bool (*)(void *, void *))(i->func);
  2771. return f(param1, param2);
  2772. }
  2773. else if( i->callConv == ICC_STDCALL )
  2774. {
  2775. bool (STDCALL *f)(void *, void *) = (bool (STDCALL *)(void *, void *))(i->func);
  2776. return f(param1, param2);
  2777. }
  2778. else
  2779. {
  2780. // TODO: When simulating a 64bit environment by defining AS_64BIT_PTR on a 32bit platform this code
  2781. // fails, because the stack given to asCGeneric is not prepared with two 64bit arguments.
  2782. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2783. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2784. f(&gen);
  2785. return *(bool*)gen.GetReturnPointer();
  2786. }
  2787. }
  2788. void *asCScriptEngine::CallAlloc(asCObjectType *type) const
  2789. {
  2790. // Allocate 4 bytes as the smallest size. Otherwise CallSystemFunction may try to
  2791. // copy a DWORD onto a smaller memory block, in case the object type is return in registers.
  2792. #if defined(AS_DEBUG)
  2793. return ((asALLOCFUNCDEBUG_t)(userAlloc))(type->size < 4 ? 4 : type->size, __FILE__, __LINE__);
  2794. #else
  2795. return userAlloc(type->size < 4 ? 4 : type->size);
  2796. #endif
  2797. }
  2798. void asCScriptEngine::CallFree(void *obj) const
  2799. {
  2800. userFree(obj);
  2801. }
  2802. // interface
  2803. void asCScriptEngine::NotifyGarbageCollectorOfNewObject(void *obj, int typeId)
  2804. {
  2805. asCObjectType *objType = GetObjectTypeFromTypeId(typeId);
  2806. gc.AddScriptObjectToGC(obj, objType);
  2807. }
  2808. // interface
  2809. int asCScriptEngine::GarbageCollect(asDWORD flags)
  2810. {
  2811. return gc.GarbageCollect(flags);
  2812. }
  2813. // interface
  2814. void asCScriptEngine::GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const
  2815. {
  2816. gc.GetStatistics(currentSize, totalDestroyed, totalDetected, newObjects, totalNewDestroyed);
  2817. }
  2818. // interface
  2819. void asCScriptEngine::GCEnumCallback(void *reference)
  2820. {
  2821. gc.GCEnumCallback(reference);
  2822. }
  2823. // TODO: multithread: The mapTypeIdToDataType must be protected with critical sections in all functions that access it
  2824. // Urho3D: modified for id caching
  2825. int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dt) const
  2826. {
  2827. if( dt.IsNullHandle() ) return 0;
  2828. // ASHANDLE is mimicking a handle, but it really is a value
  2829. // type so only the non-handle form should be registered.
  2830. if( dt.GetObjectType() && dt.GetObjectType()->flags & asOBJ_ASHANDLE && dt.IsObjectHandle() )
  2831. {
  2832. asCDataType dtNoHandle(dt);
  2833. dtNoHandle.MakeHandle(false);
  2834. return GetTypeIdFromDataType(dtNoHandle);
  2835. }
  2836. // Urho3D: check first for cached id in the type itself
  2837. int typeId = dt.GetCachedTypeId();
  2838. if( typeId ) return typeId;
  2839. // Find the existing type id
  2840. asSMapNode<int,asCDataType*> *cursor = 0;
  2841. mapTypeIdToDataType.MoveFirst(&cursor);
  2842. while( cursor )
  2843. {
  2844. if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
  2845. {
  2846. typeId = mapTypeIdToDataType.GetKey(cursor);
  2847. dt.SetCachedTypeId(typeId);
  2848. return typeId;
  2849. }
  2850. mapTypeIdToDataType.MoveNext(&cursor, cursor);
  2851. }
  2852. // The type id doesn't exist, create it
  2853. // Setup the basic type id
  2854. typeId = typeIdSeqNbr++;
  2855. if( dt.GetObjectType() )
  2856. {
  2857. if( dt.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT ) typeId |= asTYPEID_SCRIPTOBJECT;
  2858. else if( dt.GetObjectType()->flags & asOBJ_TEMPLATE ) typeId |= asTYPEID_TEMPLATE;
  2859. else if( dt.GetObjectType()->flags & asOBJ_ENUM ); // TODO: Should we have a specific bit for this?
  2860. else typeId |= asTYPEID_APPOBJECT;
  2861. }
  2862. // Insert the basic object type
  2863. asCDataType *newDt = asNEW(asCDataType)(dt);
  2864. newDt->MakeReference(false);
  2865. newDt->MakeReadOnly(false);
  2866. newDt->MakeHandle(false);
  2867. mapTypeIdToDataType.Insert(typeId, newDt);
  2868. // If the object type supports object handles then register those types as well
  2869. // Note: Don't check for addref, as asOBJ_SCOPED don't have this
  2870. if( dt.IsObject() && dt.GetObjectType()->beh.release )
  2871. {
  2872. newDt = asNEW(asCDataType)(dt);
  2873. newDt->MakeReference(false);
  2874. newDt->MakeReadOnly(false);
  2875. newDt->MakeHandle(true);
  2876. newDt->MakeHandleToConst(false);
  2877. mapTypeIdToDataType.Insert(typeId | asTYPEID_OBJHANDLE, newDt);
  2878. newDt = asNEW(asCDataType)(dt);
  2879. newDt->MakeReference(false);
  2880. newDt->MakeReadOnly(false);
  2881. newDt->MakeHandle(true);
  2882. newDt->MakeHandleToConst(true);
  2883. mapTypeIdToDataType.Insert(typeId | asTYPEID_OBJHANDLE | asTYPEID_HANDLETOCONST, newDt);
  2884. }
  2885. // Call the method recursively to get the correct type id
  2886. return GetTypeIdFromDataType(dt);
  2887. }
  2888. const asCDataType *asCScriptEngine::GetDataTypeFromTypeId(int typeId) const
  2889. {
  2890. asSMapNode<int,asCDataType*> *cursor = 0;
  2891. if( mapTypeIdToDataType.MoveTo(&cursor, typeId) )
  2892. return mapTypeIdToDataType.GetValue(cursor);
  2893. return 0;
  2894. }
  2895. asCObjectType *asCScriptEngine::GetObjectTypeFromTypeId(int typeId) const
  2896. {
  2897. asSMapNode<int,asCDataType*> *cursor = 0;
  2898. if( mapTypeIdToDataType.MoveTo(&cursor, typeId) )
  2899. return mapTypeIdToDataType.GetValue(cursor)->GetObjectType();
  2900. return 0;
  2901. }
  2902. void asCScriptEngine::RemoveFromTypeIdMap(asCObjectType *type)
  2903. {
  2904. asSMapNode<int,asCDataType*> *cursor = 0;
  2905. mapTypeIdToDataType.MoveFirst(&cursor);
  2906. while( cursor )
  2907. {
  2908. asCDataType *dt = mapTypeIdToDataType.GetValue(cursor);
  2909. asSMapNode<int,asCDataType*> *old = cursor;
  2910. mapTypeIdToDataType.MoveNext(&cursor, cursor);
  2911. if( dt->GetObjectType() == type )
  2912. {
  2913. asDELETE(dt,asCDataType);
  2914. mapTypeIdToDataType.Erase(old);
  2915. }
  2916. }
  2917. }
  2918. // interface
  2919. int asCScriptEngine::GetTypeIdByDecl(const char *decl) const
  2920. {
  2921. asCDataType dt;
  2922. // This cast is ok, because we are not changing anything in the engine
  2923. asCBuilder bld(const_cast<asCScriptEngine*>(this), 0);
  2924. int r = bld.ParseDataType(decl, &dt);
  2925. if( r < 0 )
  2926. return asINVALID_TYPE;
  2927. return GetTypeIdFromDataType(dt);
  2928. }
  2929. const char *asCScriptEngine::GetTypeDeclaration(int typeId) const
  2930. {
  2931. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2932. if( dt == 0 ) return 0;
  2933. asASSERT(threadManager);
  2934. asCString *tempString = &threadManager->GetLocalData()->string;
  2935. *tempString = dt->Format();
  2936. return tempString->AddressOf();
  2937. }
  2938. int asCScriptEngine::GetSizeOfPrimitiveType(int typeId) const
  2939. {
  2940. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2941. if( dt == 0 ) return 0;
  2942. if( !dt->IsPrimitive() ) return 0;
  2943. return dt->GetSizeInMemoryBytes();
  2944. }
  2945. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  2946. void *asCScriptEngine::CreateScriptObject(int typeId)
  2947. {
  2948. // Make sure the type id is for an object type, and not a primitive or a handle
  2949. if( (typeId & (asTYPEID_MASK_OBJECT | asTYPEID_MASK_SEQNBR)) != typeId ) return 0;
  2950. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return 0;
  2951. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2952. // Is the type id valid?
  2953. if( !dt ) return 0;
  2954. // Allocate the memory
  2955. asCObjectType *objType = dt->GetObjectType();
  2956. void *ptr = 0;
  2957. // Construct the object
  2958. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  2959. ptr = ScriptObjectFactory(objType, this);
  2960. else if( objType->flags & asOBJ_TEMPLATE )
  2961. // The registered factory is moved to the construct behaviour when the type is instanciated
  2962. ptr = CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  2963. else if( objType->flags & asOBJ_REF )
  2964. ptr = CallGlobalFunctionRetPtr(objType->beh.factory);
  2965. else
  2966. {
  2967. ptr = CallAlloc(objType);
  2968. int funcIndex = objType->beh.construct;
  2969. if( funcIndex )
  2970. CallObjectMethod(ptr, funcIndex);
  2971. }
  2972. return ptr;
  2973. }
  2974. void *asCScriptEngine::CreateScriptObjectCopy(void *origObj, int typeId)
  2975. {
  2976. void *newObj = CreateScriptObject(typeId);
  2977. if( newObj == 0 ) return 0;
  2978. CopyScriptObject(newObj, origObj, typeId);
  2979. return newObj;
  2980. }
  2981. // internal
  2982. void asCScriptEngine::ConstructScriptObjectCopy(void *mem, void *obj, asCObjectType *type)
  2983. {
  2984. // This function is only meant to be used for value types
  2985. asASSERT( type->flags & asOBJ_VALUE );
  2986. // TODO: Should use the copy constructor when available
  2987. int funcIndex = type->beh.construct;
  2988. if( funcIndex )
  2989. CallObjectMethod(mem, funcIndex);
  2990. CopyScriptObject(mem, obj, type->GetTypeId());
  2991. }
  2992. void asCScriptEngine::CopyScriptObject(void *dstObj, void *srcObj, int typeId)
  2993. {
  2994. // TODO: optimize: Use the copy constructor when available
  2995. // Make sure the type id is for an object type, and not a primitive or a handle
  2996. if( (typeId & (asTYPEID_MASK_OBJECT | asTYPEID_MASK_SEQNBR)) != typeId ) return;
  2997. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return;
  2998. // Copy the contents from the original object, using the assignment operator
  2999. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  3000. // Is the type id valid?
  3001. if( !dt ) return;
  3002. asCObjectType *objType = dt->GetObjectType();
  3003. // TODO: beh.copy will be removed, so we need to find the default opAssign method instead
  3004. // TODO: Must not copy if the opAssign is not available and the object is not a POD object
  3005. if( objType->beh.copy )
  3006. {
  3007. CallObjectMethod(dstObj, srcObj, objType->beh.copy);
  3008. }
  3009. else if( objType->size )
  3010. {
  3011. memcpy(dstObj, srcObj, objType->size);
  3012. }
  3013. }
  3014. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  3015. void asCScriptEngine::AddRefScriptObject(void *obj, int typeId)
  3016. {
  3017. // Make sure it is not a null pointer
  3018. if( obj == 0 ) return;
  3019. // Make sure the type id is for an object type or a handle
  3020. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return;
  3021. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  3022. // Is the type id valid?
  3023. if( !dt ) return;
  3024. asCObjectType *objType = dt->GetObjectType();
  3025. if( objType->beh.addref )
  3026. {
  3027. // Call the addref behaviour
  3028. CallObjectMethod(obj, objType->beh.addref);
  3029. }
  3030. }
  3031. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  3032. void asCScriptEngine::ReleaseScriptObject(void *obj, int typeId)
  3033. {
  3034. // Make sure it is not a null pointer
  3035. if( obj == 0 ) return;
  3036. // Make sure the type id is for an object type or a handle
  3037. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return;
  3038. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  3039. // Is the type id valid?
  3040. if( !dt ) return;
  3041. asCObjectType *objType = dt->GetObjectType();
  3042. if( objType->beh.release )
  3043. {
  3044. // Call the release behaviour
  3045. CallObjectMethod(obj, objType->beh.release);
  3046. }
  3047. else
  3048. {
  3049. // Call the destructor
  3050. if( objType->beh.destruct )
  3051. CallObjectMethod(obj, objType->beh.destruct);
  3052. // Then free the memory
  3053. CallFree(obj);
  3054. }
  3055. }
  3056. bool asCScriptEngine::IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const
  3057. {
  3058. // if equal, then it is obvious they are compatible
  3059. if( objTypeId == handleTypeId )
  3060. return true;
  3061. // Get the actual data types from the type ids
  3062. const asCDataType *objDt = GetDataTypeFromTypeId(objTypeId);
  3063. const asCDataType *hdlDt = GetDataTypeFromTypeId(handleTypeId);
  3064. // A handle to const cannot be passed to a handle that is not referencing a const object
  3065. if( objDt->IsHandleToConst() && !hdlDt->IsHandleToConst() )
  3066. return false;
  3067. if( objDt->GetObjectType() == hdlDt->GetObjectType() )
  3068. {
  3069. // The object type is equal
  3070. return true;
  3071. }
  3072. else if( objDt->IsScriptObject() && obj )
  3073. {
  3074. // There's still a chance the object implements the requested interface
  3075. asCObjectType *objType = ((asCScriptObject*)obj)->objType;
  3076. if( objType->Implements(hdlDt->GetObjectType()) )
  3077. return true;
  3078. }
  3079. return false;
  3080. }
  3081. int asCScriptEngine::BeginConfigGroup(const char *groupName)
  3082. {
  3083. // Make sure the group name doesn't already exist
  3084. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3085. {
  3086. if( configGroups[n]->groupName == groupName )
  3087. return asNAME_TAKEN;
  3088. }
  3089. if( currentGroup != &defaultGroup )
  3090. return asNOT_SUPPORTED;
  3091. asCConfigGroup *group = asNEW(asCConfigGroup)();
  3092. group->groupName = groupName;
  3093. configGroups.PushLast(group);
  3094. currentGroup = group;
  3095. return 0;
  3096. }
  3097. int asCScriptEngine::EndConfigGroup()
  3098. {
  3099. // Raise error if trying to end the default config
  3100. if( currentGroup == &defaultGroup )
  3101. return asNOT_SUPPORTED;
  3102. currentGroup = &defaultGroup;
  3103. return 0;
  3104. }
  3105. int asCScriptEngine::RemoveConfigGroup(const char *groupName)
  3106. {
  3107. // It is not allowed to remove a group that is still in use.
  3108. // It would be possible to change the code in such a way that
  3109. // the group could be removed even though it was still in use,
  3110. // but that would cause severe negative impact on runtime
  3111. // performance, since the VM would then have to be able handle
  3112. // situations where the types, functions, and global variables
  3113. // can be removed at any time.
  3114. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3115. {
  3116. if( configGroups[n]->groupName == groupName )
  3117. {
  3118. asCConfigGroup *group = configGroups[n];
  3119. // Make sure the group isn't referenced by anyone
  3120. if( group->refCount > 0 )
  3121. return asCONFIG_GROUP_IS_IN_USE;
  3122. // Verify if any objects registered in this group is still alive
  3123. if( group->HasLiveObjects() )
  3124. return asCONFIG_GROUP_IS_IN_USE;
  3125. // Remove the group from the list
  3126. if( n == configGroups.GetLength() - 1 )
  3127. configGroups.PopLast();
  3128. else
  3129. configGroups[n] = configGroups.PopLast();
  3130. // Remove the configurations registered with this group
  3131. group->RemoveConfiguration(this);
  3132. asDELETE(group,asCConfigGroup);
  3133. }
  3134. }
  3135. return 0;
  3136. }
  3137. asCConfigGroup *asCScriptEngine::FindConfigGroupForFunction(int funcId) const
  3138. {
  3139. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3140. {
  3141. // Check global functions
  3142. asUINT m;
  3143. for( m = 0; m < configGroups[n]->scriptFunctions.GetLength(); m++ )
  3144. {
  3145. if( configGroups[n]->scriptFunctions[m]->id == funcId )
  3146. return configGroups[n];
  3147. }
  3148. }
  3149. return 0;
  3150. }
  3151. asCConfigGroup *asCScriptEngine::FindConfigGroupForGlobalVar(int gvarId) const
  3152. {
  3153. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3154. {
  3155. for( asUINT m = 0; m < configGroups[n]->globalProps.GetLength(); m++ )
  3156. {
  3157. if( int(configGroups[n]->globalProps[m]->id) == gvarId )
  3158. return configGroups[n];
  3159. }
  3160. }
  3161. return 0;
  3162. }
  3163. asCConfigGroup *asCScriptEngine::FindConfigGroupForObjectType(const asCObjectType *objType) const
  3164. {
  3165. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3166. {
  3167. for( asUINT m = 0; m < configGroups[n]->objTypes.GetLength(); m++ )
  3168. {
  3169. if( configGroups[n]->objTypes[m] == objType )
  3170. return configGroups[n];
  3171. }
  3172. }
  3173. return 0;
  3174. }
  3175. asCConfigGroup *asCScriptEngine::FindConfigGroupForFuncDef(asCScriptFunction *funcDef) const
  3176. {
  3177. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3178. {
  3179. if( configGroups[n]->funcDefs.Exists(funcDef) )
  3180. return configGroups[n];
  3181. }
  3182. return 0;
  3183. }
  3184. #ifdef AS_DEPRECATED
  3185. // deprecated since 2011-10-04
  3186. int asCScriptEngine::SetConfigGroupModuleAccess(const char *groupName, const char *module, bool hasAccess)
  3187. {
  3188. asCConfigGroup *group = 0;
  3189. // Make sure the group name doesn't already exist
  3190. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3191. {
  3192. if( configGroups[n]->groupName == groupName )
  3193. {
  3194. group = configGroups[n];
  3195. break;
  3196. }
  3197. }
  3198. if( group == 0 )
  3199. return asWRONG_CONFIG_GROUP;
  3200. return group->SetModuleAccess(module, hasAccess);
  3201. }
  3202. #endif
  3203. // interface
  3204. asDWORD asCScriptEngine::SetDefaultAccessMask(asDWORD defaultMask)
  3205. {
  3206. asDWORD old = defaultAccessMask;
  3207. defaultAccessMask = defaultMask;
  3208. return old;
  3209. }
  3210. int asCScriptEngine::GetNextScriptFunctionId()
  3211. {
  3212. if( freeScriptFunctionIds.GetLength() )
  3213. return freeScriptFunctionIds.PopLast();
  3214. int id = (int)scriptFunctions.GetLength();
  3215. scriptFunctions.PushLast(0);
  3216. return id;
  3217. }
  3218. void asCScriptEngine::SetScriptFunction(asCScriptFunction *func)
  3219. {
  3220. scriptFunctions[func->id] = func;
  3221. }
  3222. void asCScriptEngine::FreeScriptFunctionId(int id)
  3223. {
  3224. if( id < 0 ) return;
  3225. id &= 0xFFFF;
  3226. if( id >= (int)scriptFunctions.GetLength() ) return;
  3227. if( scriptFunctions[id] )
  3228. {
  3229. asCScriptFunction *func = scriptFunctions[id];
  3230. // Remove the function from the list of script functions
  3231. if( id == (int)scriptFunctions.GetLength() - 1 )
  3232. {
  3233. scriptFunctions.PopLast();
  3234. }
  3235. else
  3236. {
  3237. scriptFunctions[id] = 0;
  3238. freeScriptFunctionIds.PushLast(id);
  3239. }
  3240. // Is the function used as signature id?
  3241. if( func->signatureId == id )
  3242. {
  3243. // Remove the signature id
  3244. signatureIds.RemoveValue(func);
  3245. // Update all functions using the signature id
  3246. int newSigId = 0;
  3247. for( asUINT n = 0; n < scriptFunctions.GetLength(); n++ )
  3248. {
  3249. if( scriptFunctions[n] && scriptFunctions[n]->signatureId == id )
  3250. {
  3251. if( newSigId == 0 )
  3252. {
  3253. newSigId = scriptFunctions[n]->id;
  3254. signatureIds.PushLast(scriptFunctions[n]);
  3255. }
  3256. scriptFunctions[n]->signatureId = newSigId;
  3257. }
  3258. }
  3259. }
  3260. }
  3261. }
  3262. // interface
  3263. int asCScriptEngine::RegisterFuncdef(const char *decl)
  3264. {
  3265. if( decl == 0 ) return ConfigError(asINVALID_ARG);
  3266. // Parse the function declaration
  3267. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_FUNCDEF);
  3268. asCBuilder bld(this, 0);
  3269. int r = bld.ParseFunctionDeclaration(0, decl, func, false, 0, 0);
  3270. if( r < 0 )
  3271. {
  3272. // Set as dummy function before deleting
  3273. func->funcType = asFUNC_DUMMY;
  3274. asDELETE(func,asCScriptFunction);
  3275. return ConfigError(asINVALID_DECLARATION);
  3276. }
  3277. // Check name conflicts
  3278. r = bld.CheckNameConflict(func->name.AddressOf(), 0, 0);
  3279. if( r < 0 )
  3280. {
  3281. asDELETE(func,asCScriptFunction);
  3282. return ConfigError(asNAME_TAKEN);
  3283. }
  3284. func->id = GetNextScriptFunctionId();
  3285. SetScriptFunction(func);
  3286. funcDefs.PushLast(func);
  3287. registeredFuncDefs.PushLast(func);
  3288. currentGroup->funcDefs.PushLast(func);
  3289. // If parameter type from other groups are used, add references
  3290. if( func->returnType.GetObjectType() )
  3291. {
  3292. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  3293. currentGroup->RefConfigGroup(group);
  3294. }
  3295. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  3296. {
  3297. if( func->parameterTypes[n].GetObjectType() )
  3298. {
  3299. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  3300. currentGroup->RefConfigGroup(group);
  3301. }
  3302. }
  3303. // Return the function id as success
  3304. return func->id;
  3305. }
  3306. // interface
  3307. asUINT asCScriptEngine::GetFuncdefCount() const
  3308. {
  3309. return registeredFuncDefs.GetLength();
  3310. }
  3311. // interface
  3312. asIScriptFunction *asCScriptEngine::GetFuncdefByIndex(asUINT index, const char **configGroup) const
  3313. {
  3314. if( index >= registeredFuncDefs.GetLength() )
  3315. return 0;
  3316. if( configGroup )
  3317. {
  3318. asCConfigGroup *group = FindConfigGroupForFuncDef(registeredFuncDefs[index]);
  3319. if( group )
  3320. *configGroup = group->groupName.AddressOf();
  3321. else
  3322. *configGroup = 0;
  3323. }
  3324. return registeredFuncDefs[index];
  3325. }
  3326. // interface
  3327. // TODO: typedef: Accept complex types for the typedefs
  3328. int asCScriptEngine::RegisterTypedef(const char *type, const char *decl)
  3329. {
  3330. if( type == 0 ) return ConfigError(asINVALID_NAME);
  3331. // Verify if the name has been registered as a type already
  3332. asUINT n;
  3333. for( n = 0; n < objectTypes.GetLength(); n++ )
  3334. {
  3335. if( objectTypes[n] && objectTypes[n]->name == type )
  3336. return asALREADY_REGISTERED;
  3337. }
  3338. // Grab the data type
  3339. asCTokenizer t(this);
  3340. size_t tokenLen;
  3341. eTokenType token;
  3342. asCDataType dataType;
  3343. // Create the data type
  3344. token = t.GetToken(decl, strlen(decl), &tokenLen);
  3345. switch(token)
  3346. {
  3347. case ttBool:
  3348. case ttInt:
  3349. case ttInt8:
  3350. case ttInt16:
  3351. case ttInt64:
  3352. case ttUInt:
  3353. case ttUInt8:
  3354. case ttUInt16:
  3355. case ttUInt64:
  3356. case ttFloat:
  3357. case ttDouble:
  3358. if( strlen(decl) != tokenLen )
  3359. {
  3360. return ConfigError(asINVALID_TYPE);
  3361. }
  3362. break;
  3363. default:
  3364. return ConfigError(asINVALID_TYPE);
  3365. }
  3366. dataType = asCDataType::CreatePrimitive(token, false);
  3367. // Make sure the name is not a reserved keyword
  3368. token = t.GetToken(type, strlen(type), &tokenLen);
  3369. if( token != ttIdentifier || strlen(type) != tokenLen )
  3370. return ConfigError(asINVALID_NAME);
  3371. asCBuilder bld(this, 0);
  3372. int r = bld.CheckNameConflict(type, 0, 0);
  3373. if( r < 0 )
  3374. return ConfigError(asNAME_TAKEN);
  3375. // Don't have to check against members of object
  3376. // types as they are allowed to use the names
  3377. // Put the data type in the list
  3378. asCObjectType *object= asNEW(asCObjectType)(this);
  3379. object->flags = asOBJ_TYPEDEF;
  3380. object->size = dataType.GetSizeInMemoryBytes();
  3381. object->name = type;
  3382. object->templateSubType = dataType;
  3383. objectTypes.PushLast(object);
  3384. registeredTypeDefs.PushLast(object);
  3385. currentGroup->objTypes.PushLast(object);
  3386. return asSUCCESS;
  3387. }
  3388. // interface
  3389. asUINT asCScriptEngine::GetTypedefCount() const
  3390. {
  3391. return registeredTypeDefs.GetLength();
  3392. }
  3393. // interface
  3394. const char *asCScriptEngine::GetTypedefByIndex(asUINT index, int *typeId, const char **configGroup) const
  3395. {
  3396. if( index >= registeredTypeDefs.GetLength() )
  3397. return 0;
  3398. if( typeId )
  3399. *typeId = GetTypeIdByDecl(registeredTypeDefs[index]->name.AddressOf());
  3400. if( configGroup )
  3401. {
  3402. asCConfigGroup *group = FindConfigGroupForObjectType(registeredTypeDefs[index]);
  3403. if( group )
  3404. *configGroup = group->groupName.AddressOf();
  3405. else
  3406. *configGroup = 0;
  3407. }
  3408. return registeredTypeDefs[index]->name.AddressOf();
  3409. }
  3410. // interface
  3411. int asCScriptEngine::RegisterEnum(const char *name)
  3412. {
  3413. // Check the name
  3414. if( NULL == name )
  3415. return ConfigError(asINVALID_NAME);
  3416. // Verify if the name has been registered as a type already
  3417. asUINT n;
  3418. for( n = 0; n < objectTypes.GetLength(); n++ )
  3419. if( objectTypes[n] && objectTypes[n]->name == name )
  3420. return asALREADY_REGISTERED;
  3421. // Use builder to parse the datatype
  3422. asCDataType dt;
  3423. asCBuilder bld(this, 0);
  3424. bool oldMsgCallback = msgCallback; msgCallback = false;
  3425. int r = bld.ParseDataType(name, &dt);
  3426. msgCallback = oldMsgCallback;
  3427. if( r >= 0 )
  3428. return ConfigError(asERROR);
  3429. // Make sure the name is not a reserved keyword
  3430. asCTokenizer t(this);
  3431. size_t tokenLen;
  3432. int token = t.GetToken(name, strlen(name), &tokenLen);
  3433. if( token != ttIdentifier || strlen(name) != tokenLen )
  3434. return ConfigError(asINVALID_NAME);
  3435. r = bld.CheckNameConflict(name, 0, 0);
  3436. if( r < 0 )
  3437. return ConfigError(asNAME_TAKEN);
  3438. asCObjectType *st = asNEW(asCObjectType)(this);
  3439. asCDataType dataType;
  3440. dataType.CreatePrimitive(ttInt, false);
  3441. st->flags = asOBJ_ENUM | asOBJ_SHARED;
  3442. st->size = 4;
  3443. st->name = name;
  3444. objectTypes.PushLast(st);
  3445. registeredEnums.PushLast(st);
  3446. currentGroup->objTypes.PushLast(st);
  3447. return asSUCCESS;
  3448. }
  3449. // interface
  3450. int asCScriptEngine::RegisterEnumValue(const char *typeName, const char *valueName, int value)
  3451. {
  3452. // Verify that the correct config group is used
  3453. if( currentGroup->FindType(typeName) == 0 )
  3454. return asWRONG_CONFIG_GROUP;
  3455. asCDataType dt;
  3456. int r;
  3457. asCBuilder bld(this, 0);
  3458. r = bld.ParseDataType(typeName, &dt);
  3459. if( r < 0 )
  3460. return ConfigError(r);
  3461. // Store the enum value
  3462. asCObjectType *ot = dt.GetObjectType();
  3463. if( ot == 0 || !(ot->flags & asOBJ_ENUM) )
  3464. return ConfigError(asINVALID_TYPE);
  3465. if( NULL == valueName )
  3466. return ConfigError(asINVALID_NAME);
  3467. int tokenLen;
  3468. asETokenClass tokenClass = ParseToken(valueName, 0, &tokenLen);
  3469. if( tokenClass != asTC_IDENTIFIER || tokenLen != (int)strlen(valueName) )
  3470. return ConfigError(asINVALID_NAME);
  3471. for( unsigned int n = 0; n < ot->enumValues.GetLength(); n++ )
  3472. {
  3473. if( ot->enumValues[n]->name == valueName )
  3474. return ConfigError(asALREADY_REGISTERED);
  3475. }
  3476. asSEnumValue *e = asNEW(asSEnumValue);
  3477. e->name = valueName;
  3478. e->value = value;
  3479. ot->enumValues.PushLast(e);
  3480. return asSUCCESS;
  3481. }
  3482. // interface
  3483. asUINT asCScriptEngine::GetEnumCount() const
  3484. {
  3485. return registeredEnums.GetLength();
  3486. }
  3487. // interface
  3488. const char *asCScriptEngine::GetEnumByIndex(asUINT index, int *enumTypeId, const char **configGroup) const
  3489. {
  3490. if( index >= registeredEnums.GetLength() )
  3491. return 0;
  3492. if( configGroup )
  3493. {
  3494. asCConfigGroup *group = FindConfigGroupForObjectType(registeredEnums[index]);
  3495. if( group )
  3496. *configGroup = group->groupName.AddressOf();
  3497. else
  3498. *configGroup = 0;
  3499. }
  3500. if( enumTypeId )
  3501. *enumTypeId = GetTypeIdByDecl(registeredEnums[index]->name.AddressOf());
  3502. return registeredEnums[index]->name.AddressOf();
  3503. }
  3504. // interface
  3505. int asCScriptEngine::GetEnumValueCount(int enumTypeId) const
  3506. {
  3507. const asCDataType *dt = GetDataTypeFromTypeId(enumTypeId);
  3508. asCObjectType *t = dt->GetObjectType();
  3509. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  3510. return asINVALID_TYPE;
  3511. return (int)t->enumValues.GetLength();
  3512. }
  3513. // interface
  3514. const char *asCScriptEngine::GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const
  3515. {
  3516. // TODO: This same function is implemented in as_module.cpp as well. Perhaps it should be moved to asCObjectType?
  3517. const asCDataType *dt = GetDataTypeFromTypeId(enumTypeId);
  3518. asCObjectType *t = dt->GetObjectType();
  3519. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  3520. return 0;
  3521. if( index >= t->enumValues.GetLength() )
  3522. return 0;
  3523. if( outValue )
  3524. *outValue = t->enumValues[index]->value;
  3525. return t->enumValues[index]->name.AddressOf();
  3526. }
  3527. // interface
  3528. asUINT asCScriptEngine::GetObjectTypeCount() const
  3529. {
  3530. return registeredObjTypes.GetLength();
  3531. }
  3532. // interface
  3533. asIObjectType *asCScriptEngine::GetObjectTypeByIndex(asUINT index) const
  3534. {
  3535. if( index >= registeredObjTypes.GetLength() )
  3536. return 0;
  3537. return registeredObjTypes[index];
  3538. }
  3539. // interface
  3540. asIObjectType *asCScriptEngine::GetObjectTypeById(int typeId) const
  3541. {
  3542. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  3543. // Is the type id valid?
  3544. if( !dt ) return 0;
  3545. // Enum types are not objects, so we shouldn't return an object type for them
  3546. if( dt->GetObjectType() && dt->GetObjectType()->GetFlags() & asOBJ_ENUM )
  3547. return 0;
  3548. return dt->GetObjectType();
  3549. }
  3550. // interface
  3551. asIScriptFunction *asCScriptEngine::GetFunctionById(int funcId) const
  3552. {
  3553. return GetScriptFunction(funcId);
  3554. }
  3555. #ifdef AS_DEPRECATED
  3556. // deprecated since 2011-10-03
  3557. asIScriptFunction *asCScriptEngine::GetFunctionDescriptorById(int funcId) const
  3558. {
  3559. return GetScriptFunction(funcId);
  3560. }
  3561. #endif
  3562. // internal
  3563. bool asCScriptEngine::IsTemplateType(const char *name) const
  3564. {
  3565. // TODO: optimize: Improve linear search
  3566. for( unsigned int n = 0; n < objectTypes.GetLength(); n++ )
  3567. {
  3568. if( objectTypes[n] && objectTypes[n]->name == name )
  3569. {
  3570. return objectTypes[n]->flags & asOBJ_TEMPLATE ? true : false;
  3571. }
  3572. }
  3573. return false;
  3574. }
  3575. // internal
  3576. int asCScriptEngine::AddConstantString(const char *str, size_t len)
  3577. {
  3578. // The str may contain null chars, so we cannot use strlen, or strcmp, or strcpy
  3579. // TODO: optimize: Improve linear search
  3580. // Has the string been registered before?
  3581. for( size_t n = 0; n < stringConstants.GetLength(); n++ )
  3582. {
  3583. if( stringConstants[n]->Compare(str, len) == 0 )
  3584. {
  3585. return (int)n;
  3586. }
  3587. }
  3588. // No match was found, add the string
  3589. asCString *cstr = asNEW(asCString)(str, len);
  3590. stringConstants.PushLast(cstr);
  3591. // The VM currently doesn't handle string ids larger than 65535
  3592. asASSERT(stringConstants.GetLength() <= 65536);
  3593. return (int)stringConstants.GetLength() - 1;
  3594. }
  3595. // internal
  3596. const asCString &asCScriptEngine::GetConstantString(int id)
  3597. {
  3598. return *stringConstants[id];
  3599. }
  3600. // internal
  3601. int asCScriptEngine::GetScriptSectionNameIndex(const char *name)
  3602. {
  3603. // TODO: These names are only released when the engine is freed. The assumption is that
  3604. // the same script section names will be reused instead of there always being new
  3605. // names. Is this assumption valid? Do we need to add reference counting?
  3606. // Store the script section names for future reference
  3607. for( asUINT n = 0; n < scriptSectionNames.GetLength(); n++ )
  3608. {
  3609. if( scriptSectionNames[n]->Compare(name) == 0 )
  3610. return n;
  3611. }
  3612. scriptSectionNames.PushLast(asNEW(asCString)(name));
  3613. return int(scriptSectionNames.GetLength()-1);
  3614. }
  3615. // interface
  3616. void asCScriptEngine::SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback)
  3617. {
  3618. cleanEngineFunc = callback;
  3619. }
  3620. // interface
  3621. void asCScriptEngine::SetModuleUserDataCleanupCallback(asCLEANMODULEFUNC_t callback)
  3622. {
  3623. cleanModuleFunc = callback;
  3624. }
  3625. // interface
  3626. void asCScriptEngine::SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback)
  3627. {
  3628. cleanContextFunc = callback;
  3629. }
  3630. // interface
  3631. void asCScriptEngine::SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback)
  3632. {
  3633. cleanFunctionFunc = callback;
  3634. }
  3635. // interface
  3636. void asCScriptEngine::SetObjectTypeUserDataCleanupCallback(asCLEANOBJECTTYPEFUNC_t callback)
  3637. {
  3638. cleanObjectTypeFunc = callback;
  3639. }
  3640. // Urho3D: modified for smaller executable size
  3641. asSFuncPtr::asSFuncPtr()
  3642. {
  3643. asMemClear(this, sizeof(asSFuncPtr));
  3644. }
  3645. asSFuncPtr::asSFuncPtr(asBYTE f)
  3646. {
  3647. asMemClear(this, sizeof(asSFuncPtr));
  3648. flag = f;
  3649. }
  3650. END_AS_NAMESPACE