as_scriptengine.cpp 118 KB

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