as_scriptengine.cpp 125 KB

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