as_scriptengine.cpp 118 KB

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