as_scriptengine.cpp 136 KB

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