as_scriptengine.cpp 119 KB

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