as_scriptengine.cpp 151 KB

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