as_scriptengine.cpp 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181
  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 | asOBJ_TEMPLATE) )
  1001. return ConfigError(asINVALID_ARG);
  1002. // flags are exclusive
  1003. if( (flags & asOBJ_POD) && (flags & asOBJ_ASHANDLE) )
  1004. return ConfigError(asINVALID_ARG);
  1005. // If the app type is given, we must validate the flags
  1006. if( flags & asOBJ_APP_CLASS )
  1007. {
  1008. // Must not set the primitive or float flag
  1009. if( flags & (asOBJ_APP_PRIMITIVE |
  1010. asOBJ_APP_FLOAT) )
  1011. return ConfigError(asINVALID_ARG);
  1012. }
  1013. else if( flags & asOBJ_APP_PRIMITIVE )
  1014. {
  1015. // Must not set the class flags nor the float flag
  1016. if( flags & (asOBJ_APP_CLASS |
  1017. asOBJ_APP_CLASS_CONSTRUCTOR |
  1018. asOBJ_APP_CLASS_DESTRUCTOR |
  1019. asOBJ_APP_CLASS_ASSIGNMENT |
  1020. asOBJ_APP_FLOAT) )
  1021. return ConfigError(asINVALID_ARG);
  1022. }
  1023. else if( flags & asOBJ_APP_FLOAT )
  1024. {
  1025. // Must not set the class flags nor the primitive flag
  1026. if( flags & (asOBJ_APP_CLASS |
  1027. asOBJ_APP_CLASS_CONSTRUCTOR |
  1028. asOBJ_APP_CLASS_DESTRUCTOR |
  1029. asOBJ_APP_CLASS_ASSIGNMENT |
  1030. asOBJ_APP_PRIMITIVE) )
  1031. return ConfigError(asINVALID_ARG);
  1032. }
  1033. else if( flags & (asOBJ_APP_CLASS_CONSTRUCTOR |
  1034. asOBJ_APP_CLASS_DESTRUCTOR |
  1035. asOBJ_APP_CLASS_ASSIGNMENT) )
  1036. {
  1037. // Must not set the class properties, without the class flag
  1038. return ConfigError(asINVALID_ARG);
  1039. }
  1040. }
  1041. else
  1042. return ConfigError(asINVALID_ARG);
  1043. // Don't allow anything else than the defined flags
  1044. if( flags - (flags & asOBJ_MASK_VALID_FLAGS) )
  1045. return ConfigError(asINVALID_ARG);
  1046. // Value types must have a defined size
  1047. if( (flags & asOBJ_VALUE) && byteSize == 0 )
  1048. {
  1049. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_VALUE_TYPE_MUST_HAVE_SIZE);
  1050. return ConfigError(asINVALID_ARG);
  1051. }
  1052. // Verify type name
  1053. if( name == 0 )
  1054. return ConfigError(asINVALID_NAME);
  1055. asCString typeName;
  1056. asCBuilder bld(this, 0);
  1057. if( flags & asOBJ_TEMPLATE )
  1058. {
  1059. asCString subtypeName;
  1060. r = bld.ParseTemplateDecl(name, &typeName, &subtypeName);
  1061. if( r < 0 )
  1062. return r;
  1063. // Verify that the template name hasn't been registered as a type already
  1064. asUINT n;
  1065. for( n = 0; n < objectTypes.GetLength(); n++ )
  1066. {
  1067. if( objectTypes[n] && objectTypes[n]->name == typeName )
  1068. return asALREADY_REGISTERED;
  1069. }
  1070. asCObjectType *type = asNEW(asCObjectType)(this);
  1071. type->name = typeName;
  1072. type->size = byteSize;
  1073. type->flags = flags;
  1074. // Store it in the object types
  1075. objectTypes.PushLast(type);
  1076. // Define a template subtype
  1077. asCObjectType *subtype = 0;
  1078. for( n = 0; n < templateSubTypes.GetLength(); n++ )
  1079. {
  1080. if( templateSubTypes[n]->name == subtypeName )
  1081. {
  1082. subtype = templateSubTypes[n];
  1083. break;
  1084. }
  1085. }
  1086. if( subtype == 0 )
  1087. {
  1088. // Create the new subtype if not already existing
  1089. subtype = asNEW(asCObjectType)(this);
  1090. subtype->name = subtypeName;
  1091. subtype->size = 0;
  1092. subtype->flags = asOBJ_TEMPLATE_SUBTYPE;
  1093. templateSubTypes.PushLast(subtype);
  1094. subtype->AddRef();
  1095. }
  1096. type->templateSubType = asCDataType::CreateObject(subtype, false);
  1097. subtype->AddRef();
  1098. currentGroup->objTypes.PushLast(type);
  1099. registeredObjTypes.PushLast(type);
  1100. }
  1101. else
  1102. {
  1103. typeName = name;
  1104. // Verify if the name has been registered as a type already
  1105. asUINT n;
  1106. for( n = 0; n < objectTypes.GetLength(); n++ )
  1107. {
  1108. if( objectTypes[n] && objectTypes[n]->name == typeName )
  1109. return asALREADY_REGISTERED;
  1110. }
  1111. for( n = 0; n < templateTypes.GetLength(); n++ )
  1112. {
  1113. if( templateTypes[n] && templateTypes[n]->name == typeName )
  1114. return asALREADY_REGISTERED;
  1115. }
  1116. // Verify the most recently created template instance type
  1117. asCObjectType *mostRecentTemplateInstanceType = 0;
  1118. if( templateInstanceTypes.GetLength() )
  1119. mostRecentTemplateInstanceType = templateInstanceTypes[templateInstanceTypes.GetLength()-1];
  1120. // Use builder to parse the datatype
  1121. asCDataType dt;
  1122. bool oldMsgCallback = msgCallback; msgCallback = false;
  1123. r = bld.ParseDataType(name, &dt);
  1124. msgCallback = oldMsgCallback;
  1125. // If the builder fails, then the type name
  1126. // is new and it should be registered
  1127. if( r < 0 )
  1128. {
  1129. // Make sure the name is not a reserved keyword
  1130. asCTokenizer t(this);
  1131. size_t tokenLen;
  1132. int token = t.GetToken(name, typeName.GetLength(), &tokenLen);
  1133. if( token != ttIdentifier || typeName.GetLength() != tokenLen )
  1134. return ConfigError(asINVALID_NAME);
  1135. int r = bld.CheckNameConflict(name, 0, 0);
  1136. if( r < 0 )
  1137. return ConfigError(asNAME_TAKEN);
  1138. // Don't have to check against members of object
  1139. // types as they are allowed to use the names
  1140. // Put the data type in the list
  1141. asCObjectType *type = asNEW(asCObjectType)(this);
  1142. type->name = typeName;
  1143. type->size = byteSize;
  1144. type->flags = flags;
  1145. objectTypes.PushLast(type);
  1146. registeredObjTypes.PushLast(type);
  1147. currentGroup->objTypes.PushLast(type);
  1148. }
  1149. else
  1150. {
  1151. // The application is registering a template specialization so we
  1152. // need to replace the template instance type with the new type.
  1153. // TODO: Template: We don't require the lower dimensions to be registered first for registered template types
  1154. // int[][] must not be allowed to be registered
  1155. // if int[] hasn't been registered first
  1156. if( dt.GetSubType().IsTemplate() )
  1157. return ConfigError(asLOWER_ARRAY_DIMENSION_NOT_REGISTERED);
  1158. if( dt.IsReadOnly() ||
  1159. dt.IsReference() )
  1160. return ConfigError(asINVALID_TYPE);
  1161. // Was the template instance type created before?
  1162. if( templateInstanceTypes[templateInstanceTypes.GetLength()-1] == mostRecentTemplateInstanceType ||
  1163. mostRecentTemplateInstanceType == dt.GetObjectType() )
  1164. // TODO: Should have a better error message
  1165. return ConfigError(asNOT_SUPPORTED);
  1166. // TODO: Add this again. The type is used by the factory stubs so we need to discount that
  1167. // Is the template instance type already being used?
  1168. // if( dt.GetObjectType()->GetRefCount() > 1 )
  1169. // return ConfigError(asNOT_SUPPORTED);
  1170. // Put the data type in the list
  1171. asCObjectType *type = asNEW(asCObjectType)(this);
  1172. type->name = dt.GetObjectType()->name;
  1173. type->templateSubType = dt.GetSubType();
  1174. if( type->templateSubType.GetObjectType() ) type->templateSubType.GetObjectType()->AddRef();
  1175. type->size = byteSize;
  1176. type->flags = flags;
  1177. templateTypes.PushLast(type);
  1178. currentGroup->objTypes.PushLast(type);
  1179. // Remove the template instance type, which will no longer be used.
  1180. RemoveTemplateInstanceType(dt.GetObjectType());
  1181. }
  1182. }
  1183. // Return the type id as the success (except for template types)
  1184. if( flags & asOBJ_TEMPLATE )
  1185. return asSUCCESS;
  1186. return GetTypeIdByDecl(name);
  1187. }
  1188. // interface
  1189. int asCScriptEngine::RegisterObjectBehaviour(const char *datatype, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1190. {
  1191. if( datatype == 0 ) return ConfigError(asINVALID_ARG);
  1192. // Determine the object type
  1193. asCBuilder bld(this, 0);
  1194. asCDataType type;
  1195. int r = bld.ParseDataType(datatype, &type);
  1196. if( r < 0 )
  1197. return ConfigError(r);
  1198. if( type.GetObjectType() == 0 )
  1199. return ConfigError(asINVALID_TYPE);
  1200. if( type.IsReadOnly() || type.IsReference() )
  1201. return ConfigError(asINVALID_TYPE);
  1202. return RegisterBehaviourToObjectType(type.GetObjectType(), behaviour, decl, funcPointer, callConv);
  1203. }
  1204. // internal
  1205. int asCScriptEngine::RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1206. {
  1207. asSSystemFunctionInterface internal;
  1208. if( behaviour == asBEHAVE_FACTORY ||
  1209. behaviour == asBEHAVE_LIST_FACTORY ||
  1210. behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1211. {
  1212. #ifdef AS_MAX_PORTABILITY
  1213. if( callConv != asCALL_GENERIC )
  1214. return ConfigError(asNOT_SUPPORTED);
  1215. #endif
  1216. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1217. if( r < 0 )
  1218. return ConfigError(r);
  1219. }
  1220. else
  1221. {
  1222. #ifdef AS_MAX_PORTABILITY
  1223. if( callConv != asCALL_GENERIC )
  1224. return ConfigError(asNOT_SUPPORTED);
  1225. #else
  1226. if( callConv != asCALL_THISCALL &&
  1227. callConv != asCALL_CDECL_OBJLAST &&
  1228. callConv != asCALL_CDECL_OBJFIRST &&
  1229. callConv != asCALL_GENERIC )
  1230. return ConfigError(asNOT_SUPPORTED);
  1231. #endif
  1232. int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
  1233. if( r < 0 )
  1234. return ConfigError(r);
  1235. }
  1236. isPrepared = false;
  1237. asSTypeBehaviour *beh = &objectType->beh;
  1238. // Verify function declaration
  1239. asCScriptFunction func(this, 0, asFUNC_DUMMY);
  1240. asCBuilder bld(this, 0);
  1241. int r = bld.ParseFunctionDeclaration(objectType, decl, &func, true, &internal.paramAutoHandles, &internal.returnAutoHandle);
  1242. if( r < 0 )
  1243. return ConfigError(asINVALID_DECLARATION);
  1244. func.name.Format("_beh_%d_", behaviour);
  1245. if( behaviour != asBEHAVE_FACTORY && behaviour != asBEHAVE_LIST_FACTORY )
  1246. func.objectType = objectType;
  1247. // Check if the method restricts that use of the template to value types or reference types
  1248. if( objectType->flags & asOBJ_TEMPLATE )
  1249. {
  1250. if( func.returnType.GetObjectType() == objectType->templateSubType.GetObjectType() )
  1251. {
  1252. if( func.returnType.IsObjectHandle() )
  1253. objectType->acceptValueSubType = false;
  1254. else if( !func.returnType.IsReference() )
  1255. objectType->acceptRefSubType = false;
  1256. }
  1257. for( asUINT n = 0; n < func.parameterTypes.GetLength(); n++ )
  1258. {
  1259. if( func.parameterTypes[n].GetObjectType() == objectType->templateSubType.GetObjectType() )
  1260. {
  1261. // TODO: If unsafe references are allowed, then inout references allow value types
  1262. if( func.parameterTypes[n].IsObjectHandle() || (func.parameterTypes[n].IsReference() && func.inOutFlags[n] == asTM_INOUTREF) )
  1263. objectType->acceptValueSubType = false;
  1264. else if( !func.parameterTypes[n].IsReference() )
  1265. objectType->acceptRefSubType = false;
  1266. }
  1267. }
  1268. }
  1269. if( behaviour == asBEHAVE_CONSTRUCT )
  1270. {
  1271. // TODO: Add asBEHAVE_IMPLICIT_CONSTRUCT
  1272. // Verify that the return type is void
  1273. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1274. return ConfigError(asINVALID_DECLARATION);
  1275. if( objectType->flags & asOBJ_SCRIPT_OBJECT )
  1276. {
  1277. // The script object is a special case
  1278. asASSERT(func.parameterTypes.GetLength() == 1);
  1279. beh->construct = AddBehaviourFunction(func, internal);
  1280. beh->factory = beh->construct;
  1281. scriptFunctions[beh->factory]->AddRef();
  1282. beh->constructors.PushLast(beh->construct);
  1283. beh->factories.PushLast(beh->factory);
  1284. func.id = beh->construct;
  1285. }
  1286. else
  1287. {
  1288. // Verify that it is a value type
  1289. if( !(func.objectType->flags & asOBJ_VALUE) )
  1290. {
  1291. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1292. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1293. }
  1294. // TODO: Add support for implicit constructors
  1295. // TODO: Verify that the same constructor hasn't been registered already
  1296. // Store all constructors in a list
  1297. func.id = AddBehaviourFunction(func, internal);
  1298. beh->constructors.PushLast(func.id);
  1299. if( func.parameterTypes.GetLength() == 0 )
  1300. {
  1301. beh->construct = func.id;
  1302. }
  1303. else if( func.parameterTypes.GetLength() == 1 )
  1304. {
  1305. // Is this the copy constructor?
  1306. asCDataType paramType = func.parameterTypes[0];
  1307. // If the parameter is object, and const reference for input,
  1308. // and same type as this class, then this is a copy constructor.
  1309. if( paramType.IsObject() && paramType.IsReference() && paramType.IsReadOnly() && func.inOutFlags[0] == asTM_INREF && paramType.GetObjectType() == objectType )
  1310. beh->copyconstruct = func.id;
  1311. }
  1312. }
  1313. }
  1314. else if( behaviour == asBEHAVE_DESTRUCT )
  1315. {
  1316. // Must be a value type
  1317. if( !(func.objectType->flags & asOBJ_VALUE) )
  1318. {
  1319. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1320. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1321. }
  1322. if( beh->destruct )
  1323. return ConfigError(asALREADY_REGISTERED);
  1324. // Verify that the return type is void
  1325. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1326. return ConfigError(asINVALID_DECLARATION);
  1327. // Verify that there are no parameters
  1328. if( func.parameterTypes.GetLength() > 0 )
  1329. return ConfigError(asINVALID_DECLARATION);
  1330. func.id = beh->destruct = AddBehaviourFunction(func, internal);
  1331. }
  1332. else if( behaviour == asBEHAVE_FACTORY || behaviour == asBEHAVE_LIST_FACTORY )
  1333. {
  1334. // Must be a ref type and must not have asOBJ_NOHANDLE
  1335. if( !(objectType->flags & asOBJ_REF) || (objectType->flags & asOBJ_NOHANDLE) )
  1336. {
  1337. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1338. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1339. }
  1340. // Verify that the return type is a handle to the type
  1341. if( func.returnType != asCDataType::CreateObjectHandle(objectType, false) )
  1342. return ConfigError(asINVALID_DECLARATION);
  1343. // TODO: Add support for implicit factories
  1344. // TODO: Verify that the same factory function hasn't been registered already
  1345. // The templates take a hidden parameter with the object type
  1346. if( (objectType->flags & asOBJ_TEMPLATE) &&
  1347. (func.parameterTypes.GetLength() == 0 ||
  1348. !func.parameterTypes[0].IsReference()) )
  1349. {
  1350. return ConfigError(asINVALID_DECLARATION);
  1351. }
  1352. // Store all factory functions in a list
  1353. func.id = AddBehaviourFunction(func, internal);
  1354. // The list factory is a special factory and isn't stored together with the rest
  1355. if( behaviour != asBEHAVE_LIST_FACTORY )
  1356. beh->factories.PushLast(func.id);
  1357. if( (func.parameterTypes.GetLength() == 0) ||
  1358. (func.parameterTypes.GetLength() == 1 && (objectType->flags & asOBJ_TEMPLATE)) )
  1359. {
  1360. beh->factory = func.id;
  1361. }
  1362. else if( (func.parameterTypes.GetLength() == 1) ||
  1363. (func.parameterTypes.GetLength() == 2 && (objectType->flags & asOBJ_TEMPLATE)) )
  1364. {
  1365. if( behaviour == asBEHAVE_LIST_FACTORY )
  1366. beh->listFactory = func.id;
  1367. else
  1368. {
  1369. // Is this the copy factory?
  1370. asCDataType paramType = func.parameterTypes[func.parameterTypes.GetLength()-1];
  1371. // If the parameter is object, and const reference for input,
  1372. // and same type as this class, then this is a copy constructor.
  1373. if( paramType.IsObject() && paramType.IsReference() && paramType.IsReadOnly() && func.inOutFlags[func.parameterTypes.GetLength()-1] == asTM_INREF && paramType.GetObjectType() == objectType )
  1374. beh->copyfactory = func.id;
  1375. }
  1376. }
  1377. }
  1378. else if( behaviour == asBEHAVE_ADDREF )
  1379. {
  1380. // Must be a ref type and must not have asOBJ_NOHANDLE, nor asOBJ_SCOPED
  1381. if( !(func.objectType->flags & asOBJ_REF) ||
  1382. (func.objectType->flags & asOBJ_NOHANDLE) ||
  1383. (func.objectType->flags & asOBJ_SCOPED) )
  1384. {
  1385. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1386. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1387. }
  1388. if( beh->addref )
  1389. return ConfigError(asALREADY_REGISTERED);
  1390. // Verify that the return type is void
  1391. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1392. return ConfigError(asINVALID_DECLARATION);
  1393. // Verify that there are no parameters
  1394. if( func.parameterTypes.GetLength() > 0 )
  1395. return ConfigError(asINVALID_DECLARATION);
  1396. func.id = beh->addref = AddBehaviourFunction(func, internal);
  1397. }
  1398. else if( behaviour == asBEHAVE_RELEASE )
  1399. {
  1400. // Must be a ref type and must not have asOBJ_NOHANDLE
  1401. if( !(func.objectType->flags & asOBJ_REF) || (func.objectType->flags & asOBJ_NOHANDLE) )
  1402. {
  1403. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1404. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1405. }
  1406. if( beh->release )
  1407. return ConfigError(asALREADY_REGISTERED);
  1408. // Verify that the return type is void
  1409. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1410. return ConfigError(asINVALID_DECLARATION);
  1411. // Verify that there are no parameters
  1412. if( func.parameterTypes.GetLength() > 0 )
  1413. return ConfigError(asINVALID_DECLARATION);
  1414. func.id = beh->release = AddBehaviourFunction(func, internal);
  1415. }
  1416. else if( behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1417. {
  1418. // Must be a template type
  1419. if( !(func.objectType->flags & asOBJ_TEMPLATE) )
  1420. {
  1421. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1422. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1423. }
  1424. if( beh->templateCallback )
  1425. return ConfigError(asALREADY_REGISTERED);
  1426. // Verify that the return type is bool
  1427. if( func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1428. return ConfigError(asINVALID_DECLARATION);
  1429. // Verify that there is one parameters
  1430. if( func.parameterTypes.GetLength() != 1 )
  1431. return ConfigError(asINVALID_DECLARATION);
  1432. func.id = beh->templateCallback = AddBehaviourFunction(func, internal);
  1433. }
  1434. else if( behaviour >= asBEHAVE_FIRST_GC &&
  1435. behaviour <= asBEHAVE_LAST_GC )
  1436. {
  1437. // Only allow GC behaviours for types registered to be garbage collected
  1438. if( !(func.objectType->flags & asOBJ_GC) )
  1439. {
  1440. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1441. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1442. }
  1443. // Verify parameter count
  1444. if( (behaviour == asBEHAVE_GETREFCOUNT ||
  1445. behaviour == asBEHAVE_SETGCFLAG ||
  1446. behaviour == asBEHAVE_GETGCFLAG) &&
  1447. func.parameterTypes.GetLength() != 0 )
  1448. return ConfigError(asINVALID_DECLARATION);
  1449. if( (behaviour == asBEHAVE_ENUMREFS ||
  1450. behaviour == asBEHAVE_RELEASEREFS) &&
  1451. func.parameterTypes.GetLength() != 1 )
  1452. return ConfigError(asINVALID_DECLARATION);
  1453. // Verify return type
  1454. if( behaviour == asBEHAVE_GETREFCOUNT &&
  1455. func.returnType != asCDataType::CreatePrimitive(ttInt, false) )
  1456. return ConfigError(asINVALID_DECLARATION);
  1457. if( behaviour == asBEHAVE_GETGCFLAG &&
  1458. func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1459. return ConfigError(asINVALID_DECLARATION);
  1460. if( (behaviour == asBEHAVE_SETGCFLAG ||
  1461. behaviour == asBEHAVE_ENUMREFS ||
  1462. behaviour == asBEHAVE_RELEASEREFS) &&
  1463. func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1464. return ConfigError(asINVALID_DECLARATION);
  1465. if( behaviour == asBEHAVE_GETREFCOUNT )
  1466. func.id = beh->gcGetRefCount = AddBehaviourFunction(func, internal);
  1467. else if( behaviour == asBEHAVE_SETGCFLAG )
  1468. func.id = beh->gcSetFlag = AddBehaviourFunction(func, internal);
  1469. else if( behaviour == asBEHAVE_GETGCFLAG )
  1470. func.id = beh->gcGetFlag = AddBehaviourFunction(func, internal);
  1471. else if( behaviour == asBEHAVE_ENUMREFS )
  1472. func.id = beh->gcEnumReferences = AddBehaviourFunction(func, internal);
  1473. else if( behaviour == asBEHAVE_RELEASEREFS )
  1474. func.id = beh->gcReleaseAllReferences = AddBehaviourFunction(func, internal);
  1475. }
  1476. else if( behaviour == asBEHAVE_IMPLICIT_VALUE_CAST ||
  1477. behaviour == asBEHAVE_VALUE_CAST )
  1478. {
  1479. // Verify parameter count
  1480. if( func.parameterTypes.GetLength() != 0 )
  1481. return ConfigError(asINVALID_DECLARATION);
  1482. // Verify return type
  1483. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, false)) )
  1484. return ConfigError(asNOT_SUPPORTED);
  1485. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  1486. return ConfigError(asINVALID_DECLARATION);
  1487. // TODO: verify that the same cast is not registered already (const or non-const is treated the same for the return type)
  1488. beh->operators.PushLast(behaviour);
  1489. func.id = AddBehaviourFunction(func, internal);
  1490. beh->operators.PushLast(func.id);
  1491. }
  1492. else if( behaviour == asBEHAVE_REF_CAST ||
  1493. behaviour == asBEHAVE_IMPLICIT_REF_CAST )
  1494. {
  1495. // There are two allowed signatures
  1496. // 1. obj @f()
  1497. // 2. void f(?&out)
  1498. if( !(func.parameterTypes.GetLength() == 0 && func.returnType.IsObjectHandle()) &&
  1499. !(func.parameterTypes.GetLength() == 1 && func.parameterTypes[0].GetTokenType() == ttQuestion && func.inOutFlags[0] == asTM_OUTREF && func.returnType.GetTokenType() == ttVoid) )
  1500. {
  1501. return ConfigError(asINVALID_DECLARATION);
  1502. }
  1503. // TODO: verify that the same cast is not registered already (cosnt or non-const is treated the same for the return type)
  1504. beh->operators.PushLast(behaviour);
  1505. func.id = AddBehaviourFunction(func, internal);
  1506. beh->operators.PushLast(func.id);
  1507. }
  1508. else
  1509. {
  1510. asASSERT(false);
  1511. return ConfigError(asINVALID_ARG);
  1512. }
  1513. // Return function id as success
  1514. return func.id;
  1515. }
  1516. int asCScriptEngine::VerifyVarTypeNotInFunction(asCScriptFunction *func)
  1517. {
  1518. // Don't allow var type in this function
  1519. if( func->returnType.GetTokenType() == ttQuestion )
  1520. return asINVALID_DECLARATION;
  1521. for( unsigned int n = 0; n < func->parameterTypes.GetLength(); n++ )
  1522. if( func->parameterTypes[n].GetTokenType() == ttQuestion )
  1523. return asINVALID_DECLARATION;
  1524. return 0;
  1525. }
  1526. int asCScriptEngine::AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal)
  1527. {
  1528. asUINT n;
  1529. int id = GetNextScriptFunctionId();
  1530. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface);
  1531. newInterface->func = internal.func;
  1532. newInterface->baseOffset = internal.baseOffset;
  1533. newInterface->callConv = internal.callConv;
  1534. newInterface->scriptReturnSize = internal.scriptReturnSize;
  1535. newInterface->hostReturnInMemory = internal.hostReturnInMemory;
  1536. newInterface->hostReturnFloat = internal.hostReturnFloat;
  1537. newInterface->hostReturnSize = internal.hostReturnSize;
  1538. newInterface->paramSize = internal.paramSize;
  1539. newInterface->takesObjByVal = internal.takesObjByVal;
  1540. newInterface->paramAutoHandles = internal.paramAutoHandles;
  1541. newInterface->returnAutoHandle = internal.returnAutoHandle;
  1542. newInterface->hasAutoHandles = internal.hasAutoHandles;
  1543. asCScriptFunction *f = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1544. asASSERT(func.name != "" && func.name != "f");
  1545. f->name = func.name;
  1546. f->sysFuncIntf = newInterface;
  1547. f->returnType = func.returnType;
  1548. f->objectType = func.objectType;
  1549. f->id = id;
  1550. f->isReadOnly = func.isReadOnly;
  1551. for( n = 0; n < func.parameterTypes.GetLength(); n++ )
  1552. {
  1553. f->parameterTypes.PushLast(func.parameterTypes[n]);
  1554. f->inOutFlags.PushLast(func.inOutFlags[n]);
  1555. }
  1556. SetScriptFunction(f);
  1557. // If parameter type from other groups are used, add references
  1558. if( f->returnType.GetObjectType() )
  1559. {
  1560. asCConfigGroup *group = FindConfigGroupForObjectType(f->returnType.GetObjectType());
  1561. currentGroup->RefConfigGroup(group);
  1562. }
  1563. for( n = 0; n < f->parameterTypes.GetLength(); n++ )
  1564. {
  1565. if( f->parameterTypes[n].GetObjectType() )
  1566. {
  1567. asCConfigGroup *group = FindConfigGroupForObjectType(f->parameterTypes[n].GetObjectType());
  1568. currentGroup->RefConfigGroup(group);
  1569. }
  1570. }
  1571. return id;
  1572. }
  1573. // interface
  1574. int asCScriptEngine::RegisterGlobalProperty(const char *declaration, void *pointer)
  1575. {
  1576. asCDataType type;
  1577. asCString name;
  1578. int r;
  1579. asCBuilder bld(this, 0);
  1580. if( (r = bld.VerifyProperty(0, declaration, name, type)) < 0 )
  1581. return ConfigError(r);
  1582. // Don't allow registering references as global properties
  1583. if( type.IsReference() )
  1584. return ConfigError(asINVALID_TYPE);
  1585. // Store the property info
  1586. asCGlobalProperty *prop = AllocateGlobalProperty();
  1587. prop->name = name;
  1588. prop->type = type;
  1589. prop->SetRegisteredAddress(pointer);
  1590. registeredGlobalProps.PushLast(prop);
  1591. currentGroup->globalProps.PushLast(prop);
  1592. // If from another group add reference
  1593. if( type.GetObjectType() )
  1594. {
  1595. asCConfigGroup *group = FindConfigGroupForObjectType(type.GetObjectType());
  1596. currentGroup->RefConfigGroup(group);
  1597. }
  1598. return asSUCCESS;
  1599. }
  1600. // internal
  1601. asCGlobalProperty *asCScriptEngine::AllocateGlobalProperty()
  1602. {
  1603. asCGlobalProperty *prop = asNEW(asCGlobalProperty);
  1604. // First check the availability of a free slot
  1605. if( freeGlobalPropertyIds.GetLength() )
  1606. {
  1607. prop->id = freeGlobalPropertyIds.PopLast();
  1608. globalProperties[prop->id] = prop;
  1609. return prop;
  1610. }
  1611. prop->id = (asUINT)globalProperties.GetLength();
  1612. globalProperties.PushLast(prop);
  1613. return prop;
  1614. }
  1615. // internal
  1616. void asCScriptEngine::FreeUnusedGlobalProperties()
  1617. {
  1618. for( asUINT n = 0; n < globalProperties.GetLength(); n++ )
  1619. {
  1620. if( globalProperties[n] && globalProperties[n]->GetRefCount() == 0 )
  1621. {
  1622. freeGlobalPropertyIds.PushLast(n);
  1623. asDELETE(globalProperties[n], asCGlobalProperty);
  1624. globalProperties[n] = 0;
  1625. }
  1626. }
  1627. }
  1628. // interface
  1629. int asCScriptEngine::GetGlobalPropertyCount() const
  1630. {
  1631. return (int)registeredGlobalProps.GetLength();
  1632. }
  1633. // interface
  1634. // TODO: If the typeId ever encodes the const flag, then the isConst parameter should be removed
  1635. int asCScriptEngine::GetGlobalPropertyByIndex(asUINT index, const char **name, int *typeId, bool *isConst, const char **configGroup, void **pointer) const
  1636. {
  1637. if( index >= registeredGlobalProps.GetLength() )
  1638. return asINVALID_ARG;
  1639. if( name )
  1640. *name = registeredGlobalProps[index]->name.AddressOf();
  1641. if( configGroup )
  1642. {
  1643. asCConfigGroup *group = FindConfigGroupForGlobalVar(index);
  1644. if( group )
  1645. *configGroup = group->groupName.AddressOf();
  1646. else
  1647. *configGroup = 0;
  1648. }
  1649. if( typeId )
  1650. *typeId = GetTypeIdFromDataType(registeredGlobalProps[index]->type);
  1651. if( isConst )
  1652. *isConst = registeredGlobalProps[index]->type.IsReadOnly();
  1653. if( pointer )
  1654. *pointer = registeredGlobalProps[index]->GetRegisteredAddress();
  1655. return asSUCCESS;
  1656. }
  1657. // interface
  1658. int asCScriptEngine::RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1659. {
  1660. if( obj == 0 )
  1661. return ConfigError(asINVALID_ARG);
  1662. // Determine the object type
  1663. asCDataType dt;
  1664. asCBuilder bld(this, 0);
  1665. int r = bld.ParseDataType(obj, &dt);
  1666. if( r < 0 )
  1667. return ConfigError(r);
  1668. if( dt.GetObjectType() == 0 )
  1669. return ConfigError(asINVALID_ARG);
  1670. return RegisterMethodToObjectType(dt.GetObjectType(), declaration, funcPointer, callConv);
  1671. }
  1672. // internal
  1673. int asCScriptEngine::RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1674. {
  1675. asSSystemFunctionInterface internal;
  1676. int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
  1677. if( r < 0 )
  1678. return ConfigError(r);
  1679. // We only support these calling conventions for object methods
  1680. #ifdef AS_MAX_PORTABILITY
  1681. if( callConv != asCALL_GENERIC )
  1682. return ConfigError(asNOT_SUPPORTED);
  1683. #else
  1684. if( callConv != asCALL_THISCALL &&
  1685. callConv != asCALL_CDECL_OBJLAST &&
  1686. callConv != asCALL_CDECL_OBJFIRST &&
  1687. callConv != asCALL_GENERIC )
  1688. return ConfigError(asNOT_SUPPORTED);
  1689. #endif
  1690. isPrepared = false;
  1691. // Put the system function in the list of system functions
  1692. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  1693. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1694. func->sysFuncIntf = newInterface;
  1695. func->objectType = objectType;
  1696. asCBuilder bld(this, 0);
  1697. r = bld.ParseFunctionDeclaration(func->objectType, declaration, func, true, &newInterface->paramAutoHandles, &newInterface->returnAutoHandle);
  1698. if( r < 0 )
  1699. {
  1700. // Set as dummy function before deleting
  1701. func->funcType = asFUNC_DUMMY;
  1702. asDELETE(func,asCScriptFunction);
  1703. return ConfigError(asINVALID_DECLARATION);
  1704. }
  1705. // Check name conflicts
  1706. r = bld.CheckNameConflictMember(objectType, func->name.AddressOf(), 0, 0, false);
  1707. if( r < 0 )
  1708. {
  1709. asDELETE(func,asCScriptFunction);
  1710. return ConfigError(asNAME_TAKEN);
  1711. }
  1712. func->id = GetNextScriptFunctionId();
  1713. func->objectType->methods.PushLast(func->id);
  1714. SetScriptFunction(func);
  1715. // TODO: This code is repeated in many places
  1716. // If parameter type from other groups are used, add references
  1717. if( func->returnType.GetObjectType() )
  1718. {
  1719. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  1720. currentGroup->RefConfigGroup(group);
  1721. }
  1722. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1723. {
  1724. if( func->parameterTypes[n].GetObjectType() )
  1725. {
  1726. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  1727. currentGroup->RefConfigGroup(group);
  1728. }
  1729. }
  1730. // Check if the method restricts that use of the template to value types or reference types
  1731. if( func->objectType->flags & asOBJ_TEMPLATE )
  1732. {
  1733. if( func->returnType.GetObjectType() == func->objectType->templateSubType.GetObjectType() )
  1734. {
  1735. if( func->returnType.IsObjectHandle() )
  1736. func->objectType->acceptValueSubType = false;
  1737. else if( !func->returnType.IsReference() )
  1738. func->objectType->acceptRefSubType = false;
  1739. }
  1740. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1741. {
  1742. if( func->parameterTypes[n].GetObjectType() == func->objectType->templateSubType.GetObjectType() )
  1743. {
  1744. // TODO: If unsafe references are allowed, then inout references allow value types
  1745. if( func->parameterTypes[n].IsObjectHandle() || (func->parameterTypes[n].IsReference() && func->inOutFlags[n] == asTM_INOUTREF) )
  1746. func->objectType->acceptValueSubType = false;
  1747. else if( !func->parameterTypes[n].IsReference() )
  1748. func->objectType->acceptRefSubType = false;
  1749. }
  1750. }
  1751. }
  1752. // TODO: beh.copy member will be removed, so this is not necessary
  1753. // Is this the default copy behaviour?
  1754. if( func->name == "opAssign" && func->parameterTypes.GetLength() == 1 && func->isReadOnly == false &&
  1755. (objectType->flags & asOBJ_SCRIPT_OBJECT || func->parameterTypes[0].IsEqualExceptRefAndConst(asCDataType::CreateObject(func->objectType, false))) )
  1756. {
  1757. func->objectType->beh.copy = func->id;
  1758. func->AddRef();
  1759. }
  1760. // Return the function id as success
  1761. return func->id;
  1762. }
  1763. // interface
  1764. int asCScriptEngine::RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
  1765. {
  1766. asSSystemFunctionInterface internal;
  1767. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1768. if( r < 0 )
  1769. return ConfigError(r);
  1770. #ifdef AS_MAX_PORTABILITY
  1771. if( callConv != asCALL_GENERIC )
  1772. return ConfigError(asNOT_SUPPORTED);
  1773. #else
  1774. if( callConv != asCALL_CDECL &&
  1775. callConv != asCALL_STDCALL &&
  1776. callConv != asCALL_GENERIC )
  1777. return ConfigError(asNOT_SUPPORTED);
  1778. #endif
  1779. isPrepared = false;
  1780. // Put the system function in the list of system functions
  1781. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  1782. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1783. func->sysFuncIntf = newInterface;
  1784. asCBuilder bld(this, 0);
  1785. r = bld.ParseFunctionDeclaration(0, declaration, func, true, &newInterface->paramAutoHandles, &newInterface->returnAutoHandle);
  1786. if( r < 0 )
  1787. {
  1788. // Set as dummy function before deleting
  1789. func->funcType = asFUNC_DUMMY;
  1790. asDELETE(func,asCScriptFunction);
  1791. return ConfigError(asINVALID_DECLARATION);
  1792. }
  1793. // Check name conflicts
  1794. r = bld.CheckNameConflict(func->name.AddressOf(), 0, 0);
  1795. if( r < 0 )
  1796. {
  1797. asDELETE(func,asCScriptFunction);
  1798. return ConfigError(asNAME_TAKEN);
  1799. }
  1800. func->id = GetNextScriptFunctionId();
  1801. SetScriptFunction(func);
  1802. currentGroup->scriptFunctions.PushLast(func);
  1803. registeredGlobalFuncs.PushLast(func);
  1804. // If parameter type from other groups are used, add references
  1805. if( func->returnType.GetObjectType() )
  1806. {
  1807. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  1808. currentGroup->RefConfigGroup(group);
  1809. }
  1810. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  1811. {
  1812. if( func->parameterTypes[n].GetObjectType() )
  1813. {
  1814. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  1815. currentGroup->RefConfigGroup(group);
  1816. }
  1817. }
  1818. // Return the function id as success
  1819. return func->id;
  1820. }
  1821. // interface
  1822. int asCScriptEngine::GetGlobalFunctionCount() const
  1823. {
  1824. return (int)registeredGlobalFuncs.GetLength();
  1825. }
  1826. // interface
  1827. int asCScriptEngine::GetGlobalFunctionIdByIndex(asUINT index) const
  1828. {
  1829. if( index >= registeredGlobalFuncs.GetLength() )
  1830. return asINVALID_ARG;
  1831. return registeredGlobalFuncs[index]->id;
  1832. }
  1833. asCObjectType *asCScriptEngine::GetObjectType(const char *type)
  1834. {
  1835. // TODO: optimize: Improve linear search
  1836. for( asUINT n = 0; n < objectTypes.GetLength(); n++ )
  1837. if( objectTypes[n] &&
  1838. objectTypes[n]->name == type ) // TODO: template: Should we check the subtype in case of template instances?
  1839. return objectTypes[n];
  1840. return 0;
  1841. }
  1842. void asCScriptEngine::PrepareEngine()
  1843. {
  1844. if( isPrepared ) return;
  1845. if( configFailed ) return;
  1846. asUINT n;
  1847. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  1848. {
  1849. // Determine the host application interface
  1850. if( scriptFunctions[n] && scriptFunctions[n]->funcType == asFUNC_SYSTEM )
  1851. {
  1852. if( scriptFunctions[n]->sysFuncIntf->callConv == ICC_GENERIC_FUNC ||
  1853. scriptFunctions[n]->sysFuncIntf->callConv == ICC_GENERIC_METHOD )
  1854. PrepareSystemFunctionGeneric(scriptFunctions[n], scriptFunctions[n]->sysFuncIntf, this);
  1855. else
  1856. PrepareSystemFunction(scriptFunctions[n], scriptFunctions[n]->sysFuncIntf, this);
  1857. }
  1858. }
  1859. // Validate object type registrations
  1860. for( n = 0; n < objectTypes.GetLength(); n++ )
  1861. {
  1862. if( objectTypes[n] && !(objectTypes[n]->flags & asOBJ_SCRIPT_OBJECT) )
  1863. {
  1864. bool missingBehaviour = false;
  1865. const char *infoMsg = 0;
  1866. // Verify that GC types have all behaviours
  1867. if( objectTypes[n]->flags & asOBJ_GC )
  1868. {
  1869. if( objectTypes[n]->beh.addref == 0 ||
  1870. objectTypes[n]->beh.release == 0 ||
  1871. objectTypes[n]->beh.gcGetRefCount == 0 ||
  1872. objectTypes[n]->beh.gcSetFlag == 0 ||
  1873. objectTypes[n]->beh.gcGetFlag == 0 ||
  1874. objectTypes[n]->beh.gcEnumReferences == 0 ||
  1875. objectTypes[n]->beh.gcReleaseAllReferences == 0 )
  1876. {
  1877. infoMsg = TXT_GC_REQUIRE_ADD_REL_GC_BEHAVIOUR;
  1878. missingBehaviour = true;
  1879. }
  1880. }
  1881. // Verify that scoped ref types have the release behaviour
  1882. else if( objectTypes[n]->flags & asOBJ_SCOPED )
  1883. {
  1884. if( objectTypes[n]->beh.release == 0 )
  1885. {
  1886. infoMsg = TXT_SCOPE_REQUIRE_REL_BEHAVIOUR;
  1887. missingBehaviour = true;
  1888. }
  1889. }
  1890. // Verify that ref types have add ref and release behaviours
  1891. else if( (objectTypes[n]->flags & asOBJ_REF) &&
  1892. !(objectTypes[n]->flags & asOBJ_NOHANDLE) )
  1893. {
  1894. if( objectTypes[n]->beh.addref == 0 ||
  1895. objectTypes[n]->beh.release == 0 )
  1896. {
  1897. infoMsg = TXT_REF_REQUIRE_ADD_REL_BEHAVIOUR;
  1898. missingBehaviour = true;
  1899. }
  1900. }
  1901. // Verify that non-pod value types have the constructor and destructor registered
  1902. else if( (objectTypes[n]->flags & asOBJ_VALUE) &&
  1903. !(objectTypes[n]->flags & asOBJ_POD) )
  1904. {
  1905. if( objectTypes[n]->beh.construct == 0 ||
  1906. objectTypes[n]->beh.destruct == 0 )
  1907. {
  1908. infoMsg = TXT_NON_POD_REQUIRE_CONSTR_DESTR_BEHAVIOUR;
  1909. missingBehaviour = true;
  1910. }
  1911. }
  1912. if( missingBehaviour )
  1913. {
  1914. asCString str;
  1915. str.Format(TXT_TYPE_s_IS_MISSING_BEHAVIOURS, objectTypes[n]->name.AddressOf());
  1916. WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1917. WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, infoMsg);
  1918. ConfigError(asINVALID_CONFIGURATION);
  1919. }
  1920. }
  1921. }
  1922. isPrepared = true;
  1923. }
  1924. int asCScriptEngine::ConfigError(int err)
  1925. {
  1926. configFailed = true;
  1927. return err;
  1928. }
  1929. // interface
  1930. int asCScriptEngine::RegisterDefaultArrayType(const char *type)
  1931. {
  1932. asCBuilder bld(this, 0);
  1933. asCDataType dt;
  1934. int r = bld.ParseDataType(type, &dt);
  1935. if( r < 0 ) return r;
  1936. if( dt.GetObjectType() == 0 ||
  1937. !(dt.GetObjectType()->GetFlags() & asOBJ_TEMPLATE) )
  1938. return asINVALID_TYPE;
  1939. defaultArrayObjectType = dt.GetObjectType();
  1940. defaultArrayObjectType->AddRef();
  1941. return 0;
  1942. }
  1943. // interface
  1944. int asCScriptEngine::GetDefaultArrayTypeId() const
  1945. {
  1946. if( defaultArrayObjectType )
  1947. return GetTypeIdFromDataType(asCDataType::CreateObject(defaultArrayObjectType, false));
  1948. return asINVALID_TYPE;
  1949. }
  1950. // interface
  1951. int asCScriptEngine::RegisterStringFactory(const char *datatype, const asSFuncPtr &funcPointer, asDWORD callConv)
  1952. {
  1953. asSSystemFunctionInterface internal;
  1954. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1955. if( r < 0 )
  1956. return ConfigError(r);
  1957. #ifdef AS_MAX_PORTABILITY
  1958. if( callConv != asCALL_GENERIC )
  1959. return ConfigError(asNOT_SUPPORTED);
  1960. #else
  1961. if( callConv != asCALL_CDECL &&
  1962. callConv != asCALL_STDCALL &&
  1963. callConv != asCALL_GENERIC )
  1964. return ConfigError(asNOT_SUPPORTED);
  1965. #endif
  1966. // Put the system function in the list of system functions
  1967. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
  1968. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1969. func->name = "_string_factory_";
  1970. func->sysFuncIntf = newInterface;
  1971. asCBuilder bld(this, 0);
  1972. asCDataType dt;
  1973. r = bld.ParseDataType(datatype, &dt);
  1974. if( r < 0 )
  1975. {
  1976. // Set as dummy before deleting
  1977. func->funcType = asFUNC_DUMMY;
  1978. asDELETE(func,asCScriptFunction);
  1979. return ConfigError(asINVALID_TYPE);
  1980. }
  1981. func->returnType = dt;
  1982. func->parameterTypes.PushLast(asCDataType::CreatePrimitive(ttInt, true));
  1983. asCDataType parm1 = asCDataType::CreatePrimitive(ttUInt8, true);
  1984. parm1.MakeReference(true);
  1985. func->parameterTypes.PushLast(parm1);
  1986. func->id = GetNextScriptFunctionId();
  1987. SetScriptFunction(func);
  1988. stringFactory = func;
  1989. if( func->returnType.GetObjectType() )
  1990. {
  1991. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  1992. if( group == 0 ) group = &defaultGroup;
  1993. group->scriptFunctions.PushLast(func);
  1994. }
  1995. // Register function id as success
  1996. return func->id;
  1997. }
  1998. // interface
  1999. int asCScriptEngine::GetStringFactoryReturnTypeId() const
  2000. {
  2001. if( stringFactory == 0 )
  2002. return asNO_FUNCTION;
  2003. return GetTypeIdFromDataType(stringFactory->returnType);
  2004. }
  2005. // interface
  2006. asCModule *asCScriptEngine::GetModule(const char *_name, bool create)
  2007. {
  2008. // Accept null as well as zero-length string
  2009. const char *name = "";
  2010. if( _name != 0 ) name = _name;
  2011. if( lastModule && lastModule->name == name )
  2012. return lastModule;
  2013. // TODO: optimize: Improve linear search
  2014. for( asUINT n = 0; n < scriptModules.GetLength(); ++n )
  2015. if( scriptModules[n] && scriptModules[n]->name == name )
  2016. {
  2017. lastModule = scriptModules[n];
  2018. return lastModule;
  2019. }
  2020. if( create )
  2021. {
  2022. asCModule *module = asNEW(asCModule)(name, this);
  2023. scriptModules.PushLast(module);
  2024. lastModule = module;
  2025. return lastModule;
  2026. }
  2027. return 0;
  2028. }
  2029. asCModule *asCScriptEngine::GetModuleFromFuncId(int id)
  2030. {
  2031. if( id < 0 ) return 0;
  2032. if( id >= (int)scriptFunctions.GetLength() ) return 0;
  2033. asCScriptFunction *func = scriptFunctions[id];
  2034. if( func == 0 ) return 0;
  2035. return func->module;
  2036. }
  2037. // internal
  2038. int asCScriptEngine::RequestBuild()
  2039. {
  2040. ENTERCRITICALSECTION(engineCritical);
  2041. if( isBuilding )
  2042. {
  2043. LEAVECRITICALSECTION(engineCritical);
  2044. return asBUILD_IN_PROGRESS;
  2045. }
  2046. isBuilding = true;
  2047. LEAVECRITICALSECTION(engineCritical);
  2048. return 0;
  2049. }
  2050. // internal
  2051. void asCScriptEngine::BuildCompleted()
  2052. {
  2053. // Always free up pooled memory after a completed build
  2054. memoryMgr.FreeUnusedMemory();
  2055. isBuilding = false;
  2056. }
  2057. void asCScriptEngine::RemoveTemplateInstanceType(asCObjectType *t)
  2058. {
  2059. int n;
  2060. // Destroy the factory stubs
  2061. for( n = 0; n < (int)t->beh.factories.GetLength(); n++ )
  2062. {
  2063. // Make sure the factory stub isn't referencing this object anymore
  2064. scriptFunctions[t->beh.factories[n]]->ReleaseAllHandles(this);
  2065. scriptFunctions[t->beh.factories[n]]->Release();
  2066. }
  2067. t->beh.factories.SetLength(0);
  2068. // Destroy the stub for the list factory too
  2069. if( t->beh.listFactory )
  2070. {
  2071. scriptFunctions[t->beh.listFactory]->ReleaseAllHandles(this);
  2072. scriptFunctions[t->beh.listFactory]->Release();
  2073. t->beh.listFactory = 0;
  2074. }
  2075. // Destroy the specialized functions
  2076. for( n = 1; n < (int)t->beh.operators.GetLength(); n += 2 )
  2077. {
  2078. if( t->beh.operators[n] && scriptFunctions[t->beh.operators[n]]->objectType == t )
  2079. {
  2080. scriptFunctions[t->beh.operators[n]]->Release();
  2081. }
  2082. }
  2083. t->beh.operators.SetLength(0);
  2084. // Start searching from the end of the list, as most of
  2085. // the time it will be the last two types
  2086. for( n = (int)templateTypes.GetLength()-1; n >= 0; n-- )
  2087. {
  2088. if( templateTypes[n] == t )
  2089. {
  2090. if( n == (signed)templateTypes.GetLength()-1 )
  2091. templateTypes.PopLast();
  2092. else
  2093. templateTypes[n] = templateTypes.PopLast();
  2094. }
  2095. }
  2096. for( n = (int)templateInstanceTypes.GetLength()-1; n >= 0; n-- )
  2097. {
  2098. if( templateInstanceTypes[n] == t )
  2099. {
  2100. if( n == (signed)templateInstanceTypes.GetLength()-1 )
  2101. templateInstanceTypes.PopLast();
  2102. else
  2103. templateInstanceTypes[n] = templateInstanceTypes.PopLast();
  2104. }
  2105. }
  2106. asDELETE(t,asCObjectType);
  2107. }
  2108. // internal
  2109. asCObjectType *asCScriptEngine::GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType)
  2110. {
  2111. asUINT n;
  2112. // Is there any template instance type or template specialization already with this subtype?
  2113. for( n = 0; n < templateTypes.GetLength(); n++ )
  2114. {
  2115. if( templateTypes[n] &&
  2116. templateTypes[n]->name == templateType->name &&
  2117. templateTypes[n]->templateSubType == subType )
  2118. return templateTypes[n];
  2119. }
  2120. // No previous template instance exists
  2121. // Make sure the template has factories as it doesn't have a default factory
  2122. if( templateType->beh.factories.GetLength() == 0 )
  2123. return 0;
  2124. // Make sure this template supports the subtype
  2125. if( !templateType->acceptValueSubType && (subType.IsPrimitive() || (subType.GetObjectType()->flags & asOBJ_VALUE)) )
  2126. return 0;
  2127. if( !templateType->acceptRefSubType && (subType.IsObject() && (subType.GetObjectType()->flags & asOBJ_REF)) )
  2128. return 0;
  2129. // Create a new template instance type based on the templateType
  2130. asCObjectType *ot = asNEW(asCObjectType)(this);
  2131. ot->templateSubType = subType;
  2132. ot->flags = templateType->flags;
  2133. ot->size = templateType->size;
  2134. ot->name = templateType->name;
  2135. // Before filling in the methods, call the template instance callback behaviour to validate the type
  2136. if( templateType->beh.templateCallback )
  2137. {
  2138. asCScriptFunction *callback = scriptFunctions[templateType->beh.templateCallback];
  2139. if( !CallGlobalFunctionRetBool(ot, 0, callback->sysFuncIntf, callback) )
  2140. {
  2141. // The type cannot be instanciated
  2142. ot->templateSubType = asCDataType();
  2143. asDELETE(ot, asCObjectType);
  2144. return 0;
  2145. }
  2146. }
  2147. ot->methods = templateType->methods;
  2148. for( n = 0; n < ot->methods.GetLength(); n++ )
  2149. scriptFunctions[ot->methods[n]]->AddRef();
  2150. // Store the real factory in the constructor. This is used by the CreateScriptObject function.
  2151. // Otherwise it wouldn't be necessary to store the real factory ids.
  2152. ot->beh.construct = templateType->beh.factory;
  2153. ot->beh.constructors = templateType->beh.factories;
  2154. for( n = 0; n < ot->beh.constructors.GetLength(); n++ )
  2155. scriptFunctions[ot->beh.constructors[n]]->AddRef();
  2156. // Generate factory stubs for each of the factories
  2157. for( n = 0; n < templateType->beh.factories.GetLength(); n++ )
  2158. {
  2159. asCScriptFunction *func = GenerateTemplateFactoryStub(templateType, ot, templateType->beh.factories[n]);
  2160. // The function's refCount was already initialized to 1
  2161. ot->beh.factories.PushLast(func->id);
  2162. }
  2163. if( ot->beh.factories.GetLength() )
  2164. ot->beh.factory = ot->beh.factories[0];
  2165. else
  2166. {
  2167. asASSERT(false);
  2168. ot->beh.factory = templateType->beh.factory;
  2169. }
  2170. // Generate stub for the list factory as well
  2171. if( templateType->beh.listFactory )
  2172. {
  2173. asCScriptFunction *func = GenerateTemplateFactoryStub(templateType, ot, templateType->beh.listFactory);
  2174. // The function's refCount was already initialized to 1
  2175. ot->beh.listFactory = func->id;
  2176. }
  2177. ot->beh.addref = templateType->beh.addref;
  2178. if( scriptFunctions[ot->beh.addref] ) scriptFunctions[ot->beh.addref]->AddRef();
  2179. ot->beh.release = templateType->beh.release;
  2180. if( scriptFunctions[ot->beh.release] ) scriptFunctions[ot->beh.release]->AddRef();
  2181. ot->beh.copy = templateType->beh.copy;
  2182. if( scriptFunctions[ot->beh.copy] ) scriptFunctions[ot->beh.copy]->AddRef();
  2183. ot->beh.operators = templateType->beh.operators;
  2184. for( n = 1; n < ot->beh.operators.GetLength(); n += 2 )
  2185. {
  2186. scriptFunctions[ot->beh.operators[n]]->AddRef();
  2187. }
  2188. ot->beh.gcGetRefCount = templateType->beh.gcGetRefCount;
  2189. if( scriptFunctions[ot->beh.gcGetRefCount] ) scriptFunctions[ot->beh.gcGetRefCount]->AddRef();
  2190. ot->beh.gcSetFlag = templateType->beh.gcSetFlag;
  2191. if( scriptFunctions[ot->beh.gcSetFlag] ) scriptFunctions[ot->beh.gcSetFlag]->AddRef();
  2192. ot->beh.gcGetFlag = templateType->beh.gcGetFlag;
  2193. if( scriptFunctions[ot->beh.gcGetFlag] ) scriptFunctions[ot->beh.gcGetFlag]->AddRef();
  2194. ot->beh.gcEnumReferences = templateType->beh.gcEnumReferences;
  2195. if( scriptFunctions[ot->beh.gcEnumReferences] ) scriptFunctions[ot->beh.gcEnumReferences]->AddRef();
  2196. ot->beh.gcReleaseAllReferences = templateType->beh.gcReleaseAllReferences;
  2197. if( scriptFunctions[ot->beh.gcReleaseAllReferences] ) scriptFunctions[ot->beh.gcReleaseAllReferences]->AddRef();
  2198. // As the new template type is instanciated, the engine should
  2199. // generate new functions to substitute the ones with the template subtype.
  2200. for( n = 1; n < ot->beh.operators.GetLength(); n += 2 )
  2201. {
  2202. int funcId = ot->beh.operators[n];
  2203. asCScriptFunction *func = scriptFunctions[funcId];
  2204. if( GenerateNewTemplateFunction(templateType, ot, subType, func, &func) )
  2205. {
  2206. // Release the old function, the new one already has its ref count set to 1
  2207. scriptFunctions[ot->beh.operators[n]]->Release();
  2208. ot->beh.operators[n] = func->id;
  2209. }
  2210. }
  2211. // As the new template type is instanciated, the engine should
  2212. // generate new functions to substitute the ones with the template subtype.
  2213. for( n = 0; n < ot->methods.GetLength(); n++ )
  2214. {
  2215. int funcId = ot->methods[n];
  2216. asCScriptFunction *func = scriptFunctions[funcId];
  2217. if( GenerateNewTemplateFunction(templateType, ot, subType, func, &func) )
  2218. {
  2219. // Release the old function, the new one already has its ref count set to 1
  2220. scriptFunctions[ot->methods[n]]->Release();
  2221. ot->methods[n] = func->id;
  2222. }
  2223. }
  2224. // Increase ref counter for sub type if it is an object type
  2225. if( ot->templateSubType.GetObjectType() ) ot->templateSubType.GetObjectType()->AddRef();
  2226. // Verify if the subtype contains a garbage collected object, in which case this template is a potential circular reference.
  2227. // A handle can potentially hold derived types, which may be garbage collected so to be safe we have to set the GC flag.
  2228. if( ot->templateSubType.IsObjectHandle() || (ot->templateSubType.GetObjectType() && (ot->templateSubType.GetObjectType()->flags & asOBJ_GC)) )
  2229. ot->flags |= asOBJ_GC;
  2230. templateTypes.PushLast(ot);
  2231. // We need to store the object type somewhere for clean-up later
  2232. // TODO: Why do we need both templateTypes and templateInstanceTypes? It is possible to differ between template instance and template specialization by checking for the asOBJ_TEMPLATE flag
  2233. templateInstanceTypes.PushLast(ot);
  2234. return ot;
  2235. }
  2236. // internal
  2237. asCScriptFunction *asCScriptEngine::GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *ot, int factoryId)
  2238. {
  2239. asCScriptFunction *factory = scriptFunctions[factoryId];
  2240. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SCRIPT);
  2241. func->name = "factstub";
  2242. func->id = GetNextScriptFunctionId();
  2243. func->returnType = asCDataType::CreateObjectHandle(ot, false);
  2244. // Skip the first parameter as this is the object type pointer that the stub will add
  2245. func->parameterTypes.SetLength(factory->parameterTypes.GetLength()-1);
  2246. func->inOutFlags.SetLength(factory->inOutFlags.GetLength()-1);
  2247. for( asUINT p = 1; p < factory->parameterTypes.GetLength(); p++ )
  2248. {
  2249. if( factory->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() )
  2250. {
  2251. func->parameterTypes[p-1] = ot->templateSubType;
  2252. if( factory->parameterTypes[p].IsObjectHandle() )
  2253. func->parameterTypes[p-1].MakeHandle(true);
  2254. func->parameterTypes[p-1].MakeReference(factory->parameterTypes[p].IsReference());
  2255. func->parameterTypes[p-1].MakeReadOnly(factory->parameterTypes[p].IsReference());
  2256. }
  2257. else if( factory->parameterTypes[p].GetObjectType() == templateType )
  2258. {
  2259. if( factory->parameterTypes[p].IsObjectHandle() )
  2260. func->parameterTypes[p-1] = asCDataType::CreateObjectHandle(ot, false);
  2261. else
  2262. func->parameterTypes[p-1] = asCDataType::CreateObject(ot, false);
  2263. func->parameterTypes[p-1].MakeReference(factory->parameterTypes[p].IsReference());
  2264. func->parameterTypes[p-1].MakeReadOnly(factory->parameterTypes[p].IsReadOnly());
  2265. }
  2266. else
  2267. {
  2268. func->parameterTypes[p-1] = factory->parameterTypes[p];
  2269. }
  2270. func->inOutFlags[p-1] = factory->inOutFlags[p];
  2271. }
  2272. SetScriptFunction(func);
  2273. asCBuilder builder(this, 0);
  2274. asCCompiler compiler(this);
  2275. compiler.CompileTemplateFactoryStub(&builder, factoryId, ot, func);
  2276. return func;
  2277. }
  2278. bool asCScriptEngine::GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *ot, asCDataType &subType, asCScriptFunction *func, asCScriptFunction **newFunc)
  2279. {
  2280. bool needNewFunc = false;
  2281. if( func->returnType.GetObjectType() == templateType->templateSubType.GetObjectType() ||
  2282. func->returnType.GetObjectType() == templateType )
  2283. needNewFunc = true;
  2284. else
  2285. {
  2286. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  2287. {
  2288. if( func->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() ||
  2289. func->parameterTypes[p].GetObjectType() == templateType )
  2290. {
  2291. needNewFunc = true;
  2292. break;
  2293. }
  2294. }
  2295. }
  2296. if( needNewFunc )
  2297. {
  2298. asCScriptFunction *func2 = asNEW(asCScriptFunction)(this, 0, func->funcType);
  2299. func2->name = func->name;
  2300. func2->id = GetNextScriptFunctionId();
  2301. if( func->returnType.GetObjectType() == templateType->templateSubType.GetObjectType() )
  2302. {
  2303. func2->returnType = subType;
  2304. if( func->returnType.IsObjectHandle() )
  2305. func2->returnType.MakeHandle(true, true);
  2306. func2->returnType.MakeReference(func->returnType.IsReference());
  2307. func2->returnType.MakeReadOnly(func->returnType.IsReadOnly());
  2308. }
  2309. else if( func->returnType.GetObjectType() == templateType )
  2310. {
  2311. if( func2->returnType.IsObjectHandle() )
  2312. func2->returnType = asCDataType::CreateObjectHandle(ot, false);
  2313. else
  2314. func2->returnType = asCDataType::CreateObject(ot, false);
  2315. func2->returnType.MakeReference(func->returnType.IsReference());
  2316. func2->returnType.MakeReadOnly(func->returnType.IsReadOnly());
  2317. }
  2318. else
  2319. func2->returnType = func->returnType;
  2320. func2->parameterTypes.SetLength(func->parameterTypes.GetLength());
  2321. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  2322. {
  2323. if( func->parameterTypes[p].GetObjectType() == templateType->templateSubType.GetObjectType() )
  2324. {
  2325. func2->parameterTypes[p] = subType;
  2326. if( func->parameterTypes[p].IsObjectHandle() )
  2327. func2->parameterTypes[p].MakeHandle(true);
  2328. func2->parameterTypes[p].MakeReference(func->parameterTypes[p].IsReference());
  2329. func2->parameterTypes[p].MakeReadOnly(func->parameterTypes[p].IsReference());
  2330. }
  2331. else if( func->parameterTypes[p].GetObjectType() == templateType )
  2332. {
  2333. if( func->parameterTypes[p].IsObjectHandle() )
  2334. func2->parameterTypes[p] = asCDataType::CreateObjectHandle(ot, false);
  2335. else
  2336. func2->parameterTypes[p] = asCDataType::CreateObject(ot, false);
  2337. func2->parameterTypes[p].MakeReference(func->parameterTypes[p].IsReference());
  2338. func2->parameterTypes[p].MakeReadOnly(func->parameterTypes[p].IsReadOnly());
  2339. }
  2340. else
  2341. func2->parameterTypes[p] = func->parameterTypes[p];
  2342. }
  2343. // TODO: template: Must be careful when instanciating templates for garbage collected types
  2344. // If the template hasn't been registered with the behaviours, it shouldn't
  2345. // permit instanciation of garbage collected types that in turn may refer to
  2346. // this instance.
  2347. func2->inOutFlags = func->inOutFlags;
  2348. func2->isReadOnly = func->isReadOnly;
  2349. func2->objectType = ot;
  2350. func2->stackNeeded = func->stackNeeded;
  2351. func2->sysFuncIntf = asNEW(asSSystemFunctionInterface)(*func->sysFuncIntf);
  2352. SetScriptFunction(func2);
  2353. // Return the new function
  2354. *newFunc = func2;
  2355. }
  2356. return needNewFunc;
  2357. }
  2358. void asCScriptEngine::CallObjectMethod(void *obj, int func)
  2359. {
  2360. asCScriptFunction *s = scriptFunctions[func];
  2361. CallObjectMethod(obj, s->sysFuncIntf, s);
  2362. }
  2363. void asCScriptEngine::CallObjectMethod(void *obj, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2364. {
  2365. #ifdef __GNUC__
  2366. if( i->callConv == ICC_GENERIC_METHOD )
  2367. {
  2368. asCGeneric gen(this, s, obj, 0);
  2369. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2370. f(&gen);
  2371. }
  2372. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2373. {
  2374. // For virtual thiscalls we must call the method as a true class method
  2375. // so that the compiler will lookup the function address in the vftable
  2376. union
  2377. {
  2378. asSIMPLEMETHOD_t mthd;
  2379. struct
  2380. {
  2381. asFUNCTION_t func;
  2382. asPWORD baseOffset; // Same size as the pointer
  2383. } f;
  2384. } p;
  2385. p.f.func = (void (*)())(i->func);
  2386. p.f.baseOffset = asPWORD(i->baseOffset);
  2387. void (asCSimpleDummy::*f)() = p.mthd;
  2388. (((asCSimpleDummy*)obj)->*f)();
  2389. }
  2390. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2391. {
  2392. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2393. void (*f)(void *) = (void (*)(void *))(i->func);
  2394. f(obj);
  2395. }
  2396. #else
  2397. #ifndef AS_NO_CLASS_METHODS
  2398. if( i->callConv == ICC_THISCALL )
  2399. {
  2400. union
  2401. {
  2402. asSIMPLEMETHOD_t mthd;
  2403. asFUNCTION_t func;
  2404. } p;
  2405. p.func = (void (*)())(i->func);
  2406. void (asCSimpleDummy::*f)() = p.mthd;
  2407. obj = (void*)(size_t(obj) + i->baseOffset);
  2408. (((asCSimpleDummy*)obj)->*f)();
  2409. }
  2410. else
  2411. #endif
  2412. if( i->callConv == ICC_GENERIC_METHOD )
  2413. {
  2414. asCGeneric gen(this, s, obj, 0);
  2415. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2416. f(&gen);
  2417. }
  2418. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2419. {
  2420. void (*f)(void *) = (void (*)(void *))(i->func);
  2421. f(obj);
  2422. }
  2423. #endif
  2424. }
  2425. bool asCScriptEngine::CallObjectMethodRetBool(void *obj, int func)
  2426. {
  2427. asCScriptFunction *s = scriptFunctions[func];
  2428. asSSystemFunctionInterface *i = s->sysFuncIntf;
  2429. #ifdef __GNUC__
  2430. if( i->callConv == ICC_GENERIC_METHOD )
  2431. {
  2432. asCGeneric gen(this, s, obj, 0);
  2433. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2434. f(&gen);
  2435. return *(bool*)gen.GetReturnPointer();
  2436. }
  2437. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2438. {
  2439. // 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
  2440. union
  2441. {
  2442. asSIMPLEMETHOD_t mthd;
  2443. struct
  2444. {
  2445. asFUNCTION_t func;
  2446. asDWORD baseOffset;
  2447. } f;
  2448. } p;
  2449. p.f.func = (void (*)())(i->func);
  2450. p.f.baseOffset = i->baseOffset;
  2451. bool (asCSimpleDummy::*f)() = (bool (asCSimpleDummy::*)())(p.mthd);
  2452. return (((asCSimpleDummy*)obj)->*f)();
  2453. }
  2454. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2455. {
  2456. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2457. bool (*f)(void *) = (bool (*)(void *))(i->func);
  2458. return f(obj);
  2459. }
  2460. #else
  2461. #ifndef AS_NO_CLASS_METHODS
  2462. if( i->callConv == ICC_THISCALL )
  2463. {
  2464. union
  2465. {
  2466. asSIMPLEMETHOD_t mthd;
  2467. asFUNCTION_t func;
  2468. } p;
  2469. p.func = (void (*)())(i->func);
  2470. bool (asCSimpleDummy::*f)() = (bool (asCSimpleDummy::*)())p.mthd;
  2471. obj = (void*)(size_t(obj) + i->baseOffset);
  2472. return (((asCSimpleDummy*)obj)->*f)();
  2473. }
  2474. else
  2475. #endif
  2476. if( i->callConv == ICC_GENERIC_METHOD )
  2477. {
  2478. asCGeneric gen(this, s, obj, 0);
  2479. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2480. f(&gen);
  2481. return *(bool*)gen.GetReturnPointer();
  2482. }
  2483. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2484. {
  2485. bool (*f)(void *) = (bool (*)(void *))(i->func);
  2486. return f(obj);
  2487. }
  2488. #endif
  2489. }
  2490. int asCScriptEngine::CallObjectMethodRetInt(void *obj, int func)
  2491. {
  2492. asCScriptFunction *s = scriptFunctions[func];
  2493. asSSystemFunctionInterface *i = s->sysFuncIntf;
  2494. #ifdef __GNUC__
  2495. if( i->callConv == ICC_GENERIC_METHOD )
  2496. {
  2497. asCGeneric gen(this, s, obj, 0);
  2498. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2499. f(&gen);
  2500. return *(int*)gen.GetReturnPointer();
  2501. }
  2502. else if( i->callConv == ICC_VIRTUAL_THISCALL )
  2503. {
  2504. // 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
  2505. union
  2506. {
  2507. asSIMPLEMETHOD_t mthd;
  2508. struct
  2509. {
  2510. asFUNCTION_t func;
  2511. asDWORD baseOffset;
  2512. } f;
  2513. } p;
  2514. p.f.func = (void (*)())(i->func);
  2515. p.f.baseOffset = i->baseOffset;
  2516. int (asCSimpleDummy::*f)() = (int (asCSimpleDummy::*)())(p.mthd);
  2517. return (((asCSimpleDummy*)obj)->*f)();
  2518. }
  2519. else /*if( i->callConv == ICC_THISCALL || i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2520. {
  2521. // Non-virtual thiscall can be called just like any global function, passing the object as the first parameter
  2522. int (*f)(void *) = (int (*)(void *))(i->func);
  2523. return f(obj);
  2524. }
  2525. #else
  2526. #ifndef AS_NO_CLASS_METHODS
  2527. if( i->callConv == ICC_THISCALL )
  2528. {
  2529. union
  2530. {
  2531. asSIMPLEMETHOD_t mthd;
  2532. asFUNCTION_t func;
  2533. } p;
  2534. p.func = (void (*)())(i->func);
  2535. int (asCSimpleDummy::*f)() = (int (asCSimpleDummy::*)())p.mthd;
  2536. obj = (void*)(size_t(obj) + i->baseOffset);
  2537. return (((asCSimpleDummy*)obj)->*f)();
  2538. }
  2539. else
  2540. #endif
  2541. if( i->callConv == ICC_GENERIC_METHOD )
  2542. {
  2543. asCGeneric gen(this, s, obj, 0);
  2544. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2545. f(&gen);
  2546. return *(int*)gen.GetReturnPointer();
  2547. }
  2548. else /*if( i->callConv == ICC_CDECL_OBJLAST || i->callConv == ICC_CDECL_OBJFIRST )*/
  2549. {
  2550. int (*f)(void *) = (int (*)(void *))(i->func);
  2551. return f(obj);
  2552. }
  2553. #endif
  2554. }
  2555. void *asCScriptEngine::CallGlobalFunctionRetPtr(int func)
  2556. {
  2557. asCScriptFunction *s = scriptFunctions[func];
  2558. return CallGlobalFunctionRetPtr(s->sysFuncIntf, s);
  2559. }
  2560. void *asCScriptEngine::CallGlobalFunctionRetPtr(int func, void *param1)
  2561. {
  2562. asCScriptFunction *s = scriptFunctions[func];
  2563. return CallGlobalFunctionRetPtr(s->sysFuncIntf, s, param1);
  2564. }
  2565. void *asCScriptEngine::CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s)
  2566. {
  2567. if( i->callConv == ICC_CDECL )
  2568. {
  2569. void *(*f)() = (void *(*)())(i->func);
  2570. return f();
  2571. }
  2572. else if( i->callConv == ICC_STDCALL )
  2573. {
  2574. void *(STDCALL *f)() = (void *(STDCALL *)())(i->func);
  2575. return f();
  2576. }
  2577. else
  2578. {
  2579. asCGeneric gen(this, s, 0, 0);
  2580. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2581. f(&gen);
  2582. return *(void**)gen.GetReturnPointer();
  2583. }
  2584. }
  2585. void *asCScriptEngine::CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1)
  2586. {
  2587. if( i->callConv == ICC_CDECL )
  2588. {
  2589. void *(*f)(void *) = (void *(*)(void *))(i->func);
  2590. return f(param1);
  2591. }
  2592. else if( i->callConv == ICC_STDCALL )
  2593. {
  2594. void *(STDCALL *f)(void *) = (void *(STDCALL *)(void *))(i->func);
  2595. return f(param1);
  2596. }
  2597. else
  2598. {
  2599. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2600. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2601. f(&gen);
  2602. return *(void**)gen.GetReturnPointer();
  2603. }
  2604. }
  2605. void asCScriptEngine::CallObjectMethod(void *obj, void *param, int func)
  2606. {
  2607. asCScriptFunction *s = scriptFunctions[func];
  2608. CallObjectMethod(obj, param, s->sysFuncIntf, s);
  2609. }
  2610. void asCScriptEngine::CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2611. {
  2612. #ifdef __GNUC__
  2613. if( i->callConv == ICC_CDECL_OBJLAST )
  2614. {
  2615. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2616. f(param, obj);
  2617. }
  2618. else if( i->callConv == ICC_GENERIC_METHOD )
  2619. {
  2620. asCGeneric gen(this, s, obj, (asDWORD*)&param);
  2621. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2622. f(&gen);
  2623. }
  2624. else /*if( i->callConv == ICC_CDECL_OBJFIRST || i->callConv == ICC_THISCALL )*/
  2625. {
  2626. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2627. f(obj, param);
  2628. }
  2629. #else
  2630. #ifndef AS_NO_CLASS_METHODS
  2631. if( i->callConv == ICC_THISCALL )
  2632. {
  2633. union
  2634. {
  2635. asSIMPLEMETHOD_t mthd;
  2636. asFUNCTION_t func;
  2637. } p;
  2638. p.func = (void (*)())(i->func);
  2639. void (asCSimpleDummy::*f)(void *) = (void (asCSimpleDummy::*)(void *))(p.mthd);
  2640. obj = (void*)(size_t(obj) + i->baseOffset);
  2641. (((asCSimpleDummy*)obj)->*f)(param);
  2642. }
  2643. else
  2644. #endif
  2645. if( i->callConv == ICC_CDECL_OBJLAST )
  2646. {
  2647. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2648. f(param, obj);
  2649. }
  2650. else if( i->callConv == ICC_GENERIC_METHOD )
  2651. {
  2652. asCGeneric gen(this, s, obj, (asDWORD*)&param);
  2653. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2654. f(&gen);
  2655. }
  2656. else /*if( i->callConv == ICC_CDECL_OBJFIRST )*/
  2657. {
  2658. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2659. f(obj, param);
  2660. }
  2661. #endif
  2662. }
  2663. void asCScriptEngine::CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2664. {
  2665. if( i->callConv == ICC_CDECL )
  2666. {
  2667. void (*f)(void *, void *) = (void (*)(void *, void *))(i->func);
  2668. f(param1, param2);
  2669. }
  2670. else if( i->callConv == ICC_STDCALL )
  2671. {
  2672. void (STDCALL *f)(void *, void *) = (void (STDCALL *)(void *, void *))(i->func);
  2673. f(param1, param2);
  2674. }
  2675. else
  2676. {
  2677. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2678. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2679. f(&gen);
  2680. }
  2681. }
  2682. bool asCScriptEngine::CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *i, asCScriptFunction *s)
  2683. {
  2684. if( i->callConv == ICC_CDECL )
  2685. {
  2686. bool (*f)(void *, void *) = (bool (*)(void *, void *))(i->func);
  2687. return f(param1, param2);
  2688. }
  2689. else if( i->callConv == ICC_STDCALL )
  2690. {
  2691. bool (STDCALL *f)(void *, void *) = (bool (STDCALL *)(void *, void *))(i->func);
  2692. return f(param1, param2);
  2693. }
  2694. else
  2695. {
  2696. // TODO: When simulating a 64bit environment by defining AS_64BIT_PTR on a 32bit platform this code
  2697. // fails, because the stack given to asCGeneric is not prepared with two 64bit arguments.
  2698. asCGeneric gen(this, s, 0, (asDWORD*)&param1);
  2699. void (*f)(asIScriptGeneric *) = (void (*)(asIScriptGeneric *))(i->func);
  2700. f(&gen);
  2701. return *(bool*)gen.GetReturnPointer();
  2702. }
  2703. }
  2704. void *asCScriptEngine::CallAlloc(asCObjectType *type) const
  2705. {
  2706. // Allocate 4 bytes as the smallest size. Otherwise CallSystemFunction may try to
  2707. // copy a DWORD onto a smaller memory block, in case the object type is return in registers.
  2708. #if defined(AS_DEBUG)
  2709. return ((asALLOCFUNCDEBUG_t)(userAlloc))(type->size < 4 ? 4 : type->size, __FILE__, __LINE__);
  2710. #else
  2711. return userAlloc(type->size < 4 ? 4 : type->size);
  2712. #endif
  2713. }
  2714. void asCScriptEngine::CallFree(void *obj) const
  2715. {
  2716. userFree(obj);
  2717. }
  2718. // interface
  2719. void asCScriptEngine::NotifyGarbageCollectorOfNewObject(void *obj, int typeId)
  2720. {
  2721. asCObjectType *objType = GetObjectTypeFromTypeId(typeId);
  2722. gc.AddScriptObjectToGC(obj, objType);
  2723. }
  2724. // interface
  2725. int asCScriptEngine::GarbageCollect(asDWORD flags)
  2726. {
  2727. return gc.GarbageCollect(flags);
  2728. }
  2729. // interface
  2730. void asCScriptEngine::GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const
  2731. {
  2732. gc.GetStatistics(currentSize, totalDestroyed, totalDetected, newObjects, totalNewDestroyed);
  2733. }
  2734. // interface
  2735. void asCScriptEngine::GCEnumCallback(void *reference)
  2736. {
  2737. gc.GCEnumCallback(reference);
  2738. }
  2739. // TODO: multithread: The mapTypeIdToDataType must be protected with critical sections in all functions that access it
  2740. // Urho3D: modified for id caching
  2741. int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dt) const
  2742. {
  2743. if( dt.IsNullHandle() ) return 0;
  2744. // ASHANDLE is mimicking a handle, but it really is a value
  2745. // type so only the non-handle form should be registered.
  2746. if( dt.GetObjectType() && dt.GetObjectType()->flags & asOBJ_ASHANDLE && dt.IsObjectHandle() )
  2747. {
  2748. asCDataType dtNoHandle(dt);
  2749. dtNoHandle.MakeHandle(false);
  2750. return GetTypeIdFromDataType(dtNoHandle);
  2751. }
  2752. // Urho3D: check first for cached id in the type itself
  2753. int typeId = dt.GetCachedTypeId();
  2754. if( typeId ) return typeId;
  2755. // Find the existing type id
  2756. asSMapNode<int,asCDataType*> *cursor = 0;
  2757. mapTypeIdToDataType.MoveFirst(&cursor);
  2758. while( cursor )
  2759. {
  2760. if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
  2761. {
  2762. typeId = mapTypeIdToDataType.GetKey(cursor);
  2763. dt.SetCachedTypeId(typeId);
  2764. return typeId;
  2765. }
  2766. mapTypeIdToDataType.MoveNext(&cursor, cursor);
  2767. }
  2768. // The type id doesn't exist, create it
  2769. // Setup the basic type id
  2770. typeId = typeIdSeqNbr++;
  2771. if( dt.GetObjectType() )
  2772. {
  2773. if( dt.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT ) typeId |= asTYPEID_SCRIPTOBJECT;
  2774. else if( dt.GetObjectType()->flags & asOBJ_TEMPLATE ) typeId |= asTYPEID_TEMPLATE;
  2775. else if( dt.GetObjectType()->flags & asOBJ_ENUM ); // TODO: Should we have a specific bit for this?
  2776. else typeId |= asTYPEID_APPOBJECT;
  2777. }
  2778. // Insert the basic object type
  2779. asCDataType *newDt = asNEW(asCDataType)(dt);
  2780. newDt->MakeReference(false);
  2781. newDt->MakeReadOnly(false);
  2782. newDt->MakeHandle(false);
  2783. mapTypeIdToDataType.Insert(typeId, newDt);
  2784. // If the object type supports object handles then register those types as well
  2785. // Note: Don't check for addref, as asOBJ_SCOPED don't have this
  2786. if( dt.IsObject() && dt.GetObjectType()->beh.release )
  2787. {
  2788. newDt = asNEW(asCDataType)(dt);
  2789. newDt->MakeReference(false);
  2790. newDt->MakeReadOnly(false);
  2791. newDt->MakeHandle(true);
  2792. newDt->MakeHandleToConst(false);
  2793. mapTypeIdToDataType.Insert(typeId | asTYPEID_OBJHANDLE, newDt);
  2794. newDt = asNEW(asCDataType)(dt);
  2795. newDt->MakeReference(false);
  2796. newDt->MakeReadOnly(false);
  2797. newDt->MakeHandle(true);
  2798. newDt->MakeHandleToConst(true);
  2799. mapTypeIdToDataType.Insert(typeId | asTYPEID_OBJHANDLE | asTYPEID_HANDLETOCONST, newDt);
  2800. }
  2801. // Call the method recursively to get the correct type id
  2802. return GetTypeIdFromDataType(dt);
  2803. }
  2804. const asCDataType *asCScriptEngine::GetDataTypeFromTypeId(int typeId) const
  2805. {
  2806. asSMapNode<int,asCDataType*> *cursor = 0;
  2807. if( mapTypeIdToDataType.MoveTo(&cursor, typeId) )
  2808. return mapTypeIdToDataType.GetValue(cursor);
  2809. return 0;
  2810. }
  2811. asCObjectType *asCScriptEngine::GetObjectTypeFromTypeId(int typeId) const
  2812. {
  2813. asSMapNode<int,asCDataType*> *cursor = 0;
  2814. if( mapTypeIdToDataType.MoveTo(&cursor, typeId) )
  2815. return mapTypeIdToDataType.GetValue(cursor)->GetObjectType();
  2816. return 0;
  2817. }
  2818. void asCScriptEngine::RemoveFromTypeIdMap(asCObjectType *type)
  2819. {
  2820. asSMapNode<int,asCDataType*> *cursor = 0;
  2821. mapTypeIdToDataType.MoveFirst(&cursor);
  2822. while( cursor )
  2823. {
  2824. asCDataType *dt = mapTypeIdToDataType.GetValue(cursor);
  2825. asSMapNode<int,asCDataType*> *old = cursor;
  2826. mapTypeIdToDataType.MoveNext(&cursor, cursor);
  2827. if( dt->GetObjectType() == type )
  2828. {
  2829. asDELETE(dt,asCDataType);
  2830. mapTypeIdToDataType.Erase(old);
  2831. }
  2832. }
  2833. }
  2834. // interface
  2835. int asCScriptEngine::GetTypeIdByDecl(const char *decl) const
  2836. {
  2837. asCDataType dt;
  2838. // This cast is ok, because we are not changing anything in the engine
  2839. asCBuilder bld(const_cast<asCScriptEngine*>(this), 0);
  2840. int r = bld.ParseDataType(decl, &dt);
  2841. if( r < 0 )
  2842. return asINVALID_TYPE;
  2843. return GetTypeIdFromDataType(dt);
  2844. }
  2845. const char *asCScriptEngine::GetTypeDeclaration(int typeId) const
  2846. {
  2847. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2848. if( dt == 0 ) return 0;
  2849. asASSERT(threadManager);
  2850. asCString *tempString = &threadManager->GetLocalData()->string;
  2851. *tempString = dt->Format();
  2852. return tempString->AddressOf();
  2853. }
  2854. int asCScriptEngine::GetSizeOfPrimitiveType(int typeId) const
  2855. {
  2856. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2857. if( dt == 0 ) return 0;
  2858. if( !dt->IsPrimitive() ) return 0;
  2859. return dt->GetSizeInMemoryBytes();
  2860. }
  2861. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  2862. void *asCScriptEngine::CreateScriptObject(int typeId)
  2863. {
  2864. // Make sure the type id is for an object type, and not a primitive or a handle
  2865. if( (typeId & (asTYPEID_MASK_OBJECT | asTYPEID_MASK_SEQNBR)) != typeId ) return 0;
  2866. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return 0;
  2867. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2868. // Is the type id valid?
  2869. if( !dt ) return 0;
  2870. // Allocate the memory
  2871. asCObjectType *objType = dt->GetObjectType();
  2872. void *ptr = 0;
  2873. // Construct the object
  2874. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  2875. ptr = ScriptObjectFactory(objType, this);
  2876. else if( objType->flags & asOBJ_TEMPLATE )
  2877. // The registered factory is moved to the construct behaviour when the type is instanciated
  2878. ptr = CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  2879. else if( objType->flags & asOBJ_REF )
  2880. ptr = CallGlobalFunctionRetPtr(objType->beh.factory);
  2881. else
  2882. {
  2883. ptr = CallAlloc(objType);
  2884. int funcIndex = objType->beh.construct;
  2885. if( funcIndex )
  2886. CallObjectMethod(ptr, funcIndex);
  2887. }
  2888. return ptr;
  2889. }
  2890. void *asCScriptEngine::CreateScriptObjectCopy(void *origObj, int typeId)
  2891. {
  2892. void *newObj = CreateScriptObject(typeId);
  2893. if( newObj == 0 ) return 0;
  2894. CopyScriptObject(newObj, origObj, typeId);
  2895. return newObj;
  2896. }
  2897. void asCScriptEngine::CopyScriptObject(void *dstObj, void *srcObj, int typeId)
  2898. {
  2899. // TODO: optimize: Use the copy constructor when available
  2900. // Make sure the type id is for an object type, and not a primitive or a handle
  2901. if( (typeId & (asTYPEID_MASK_OBJECT | asTYPEID_MASK_SEQNBR)) != typeId ) return;
  2902. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return;
  2903. // Copy the contents from the original object, using the assignment operator
  2904. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2905. // Is the type id valid?
  2906. if( !dt ) return;
  2907. asCObjectType *objType = dt->GetObjectType();
  2908. // TODO: beh.copy will be removed, so we need to find the default opAssign method instead
  2909. // TODO: Must not copy if the opAssign is not available and the object is not a POD object
  2910. if( objType->beh.copy )
  2911. {
  2912. CallObjectMethod(dstObj, srcObj, objType->beh.copy);
  2913. }
  2914. else if( objType->size )
  2915. {
  2916. memcpy(dstObj, srcObj, objType->size);
  2917. }
  2918. }
  2919. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  2920. void asCScriptEngine::AddRefScriptObject(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.addref )
  2931. {
  2932. // Call the addref behaviour
  2933. CallObjectMethod(obj, objType->beh.addref);
  2934. }
  2935. }
  2936. // TODO: interface: Should be able to take a pointer to asIObjectType directly
  2937. void asCScriptEngine::ReleaseScriptObject(void *obj, int typeId)
  2938. {
  2939. // Make sure it is not a null pointer
  2940. if( obj == 0 ) return;
  2941. // Make sure the type id is for an object type or a handle
  2942. if( (typeId & asTYPEID_MASK_OBJECT) == 0 ) return;
  2943. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  2944. // Is the type id valid?
  2945. if( !dt ) return;
  2946. asCObjectType *objType = dt->GetObjectType();
  2947. if( objType->beh.release )
  2948. {
  2949. // Call the release behaviour
  2950. CallObjectMethod(obj, objType->beh.release);
  2951. }
  2952. else
  2953. {
  2954. // Call the destructor
  2955. if( objType->beh.destruct )
  2956. CallObjectMethod(obj, objType->beh.destruct);
  2957. // Then free the memory
  2958. CallFree(obj);
  2959. }
  2960. }
  2961. bool asCScriptEngine::IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const
  2962. {
  2963. // if equal, then it is obvious they are compatible
  2964. if( objTypeId == handleTypeId )
  2965. return true;
  2966. // Get the actual data types from the type ids
  2967. const asCDataType *objDt = GetDataTypeFromTypeId(objTypeId);
  2968. const asCDataType *hdlDt = GetDataTypeFromTypeId(handleTypeId);
  2969. // A handle to const cannot be passed to a handle that is not referencing a const object
  2970. if( objDt->IsHandleToConst() && !hdlDt->IsHandleToConst() )
  2971. return false;
  2972. if( objDt->GetObjectType() == hdlDt->GetObjectType() )
  2973. {
  2974. // The object type is equal
  2975. return true;
  2976. }
  2977. else if( objDt->IsScriptObject() && obj )
  2978. {
  2979. // There's still a chance the object implements the requested interface
  2980. asCObjectType *objType = ((asCScriptObject*)obj)->objType;
  2981. if( objType->Implements(hdlDt->GetObjectType()) )
  2982. return true;
  2983. }
  2984. return false;
  2985. }
  2986. int asCScriptEngine::BeginConfigGroup(const char *groupName)
  2987. {
  2988. // Make sure the group name doesn't already exist
  2989. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  2990. {
  2991. if( configGroups[n]->groupName == groupName )
  2992. return asNAME_TAKEN;
  2993. }
  2994. if( currentGroup != &defaultGroup )
  2995. return asNOT_SUPPORTED;
  2996. asCConfigGroup *group = asNEW(asCConfigGroup)();
  2997. group->groupName = groupName;
  2998. configGroups.PushLast(group);
  2999. currentGroup = group;
  3000. return 0;
  3001. }
  3002. int asCScriptEngine::EndConfigGroup()
  3003. {
  3004. // Raise error if trying to end the default config
  3005. if( currentGroup == &defaultGroup )
  3006. return asNOT_SUPPORTED;
  3007. currentGroup = &defaultGroup;
  3008. return 0;
  3009. }
  3010. int asCScriptEngine::RemoveConfigGroup(const char *groupName)
  3011. {
  3012. // It is not allowed to remove a group that is still in use.
  3013. // It would be possible to change the code in such a way that
  3014. // the group could be removed even though it was still in use,
  3015. // but that would cause severe negative impact on runtime
  3016. // performance, since the VM would then have to be able handle
  3017. // situations where the types, functions, and global variables
  3018. // can be removed at any time.
  3019. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3020. {
  3021. if( configGroups[n]->groupName == groupName )
  3022. {
  3023. asCConfigGroup *group = configGroups[n];
  3024. // Make sure the group isn't referenced by anyone
  3025. if( group->refCount > 0 )
  3026. return asCONFIG_GROUP_IS_IN_USE;
  3027. // Verify if any objects registered in this group is still alive
  3028. if( group->HasLiveObjects() )
  3029. return asCONFIG_GROUP_IS_IN_USE;
  3030. // Remove the group from the list
  3031. if( n == configGroups.GetLength() - 1 )
  3032. configGroups.PopLast();
  3033. else
  3034. configGroups[n] = configGroups.PopLast();
  3035. // Remove the configurations registered with this group
  3036. group->RemoveConfiguration(this);
  3037. asDELETE(group,asCConfigGroup);
  3038. }
  3039. }
  3040. return 0;
  3041. }
  3042. asCConfigGroup *asCScriptEngine::FindConfigGroupForFunction(int funcId) const
  3043. {
  3044. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3045. {
  3046. // Check global functions
  3047. asUINT m;
  3048. for( m = 0; m < configGroups[n]->scriptFunctions.GetLength(); m++ )
  3049. {
  3050. if( configGroups[n]->scriptFunctions[m]->id == funcId )
  3051. return configGroups[n];
  3052. }
  3053. }
  3054. return 0;
  3055. }
  3056. asCConfigGroup *asCScriptEngine::FindConfigGroupForGlobalVar(int gvarId) const
  3057. {
  3058. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3059. {
  3060. for( asUINT m = 0; m < configGroups[n]->globalProps.GetLength(); m++ )
  3061. {
  3062. if( int(configGroups[n]->globalProps[m]->id) == gvarId )
  3063. return configGroups[n];
  3064. }
  3065. }
  3066. return 0;
  3067. }
  3068. asCConfigGroup *asCScriptEngine::FindConfigGroupForObjectType(const asCObjectType *objType) const
  3069. {
  3070. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3071. {
  3072. for( asUINT m = 0; m < configGroups[n]->objTypes.GetLength(); m++ )
  3073. {
  3074. if( configGroups[n]->objTypes[m] == objType )
  3075. return configGroups[n];
  3076. }
  3077. }
  3078. return 0;
  3079. }
  3080. asCConfigGroup *asCScriptEngine::FindConfigGroupForFuncDef(asCScriptFunction *funcDef) const
  3081. {
  3082. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3083. {
  3084. if( configGroups[n]->funcDefs.Exists(funcDef) )
  3085. return configGroups[n];
  3086. }
  3087. return 0;
  3088. }
  3089. int asCScriptEngine::SetConfigGroupModuleAccess(const char *groupName, const char *module, bool hasAccess)
  3090. {
  3091. asCConfigGroup *group = 0;
  3092. // Make sure the group name doesn't already exist
  3093. for( asUINT n = 0; n < configGroups.GetLength(); n++ )
  3094. {
  3095. if( configGroups[n]->groupName == groupName )
  3096. {
  3097. group = configGroups[n];
  3098. break;
  3099. }
  3100. }
  3101. if( group == 0 )
  3102. return asWRONG_CONFIG_GROUP;
  3103. return group->SetModuleAccess(module, hasAccess);
  3104. }
  3105. int asCScriptEngine::GetNextScriptFunctionId()
  3106. {
  3107. if( freeScriptFunctionIds.GetLength() )
  3108. return freeScriptFunctionIds.PopLast();
  3109. int id = (int)scriptFunctions.GetLength();
  3110. scriptFunctions.PushLast(0);
  3111. return id;
  3112. }
  3113. void asCScriptEngine::SetScriptFunction(asCScriptFunction *func)
  3114. {
  3115. scriptFunctions[func->id] = func;
  3116. }
  3117. void asCScriptEngine::FreeScriptFunctionId(int id)
  3118. {
  3119. if( id < 0 ) return;
  3120. id &= 0xFFFF;
  3121. if( id >= (int)scriptFunctions.GetLength() ) return;
  3122. if( scriptFunctions[id] )
  3123. {
  3124. asCScriptFunction *func = scriptFunctions[id];
  3125. // Remove the function from the list of script functions
  3126. if( id == (int)scriptFunctions.GetLength() - 1 )
  3127. {
  3128. scriptFunctions.PopLast();
  3129. }
  3130. else
  3131. {
  3132. scriptFunctions[id] = 0;
  3133. freeScriptFunctionIds.PushLast(id);
  3134. }
  3135. // Is the function used as signature id?
  3136. if( func->signatureId == id )
  3137. {
  3138. // Remove the signature id
  3139. signatureIds.RemoveValue(func);
  3140. // Update all functions using the signature id
  3141. int newSigId = 0;
  3142. for( asUINT n = 0; n < scriptFunctions.GetLength(); n++ )
  3143. {
  3144. if( scriptFunctions[n] && scriptFunctions[n]->signatureId == id )
  3145. {
  3146. if( newSigId == 0 )
  3147. {
  3148. newSigId = scriptFunctions[n]->id;
  3149. signatureIds.PushLast(scriptFunctions[n]);
  3150. }
  3151. scriptFunctions[n]->signatureId = newSigId;
  3152. }
  3153. }
  3154. }
  3155. }
  3156. }
  3157. // interface
  3158. int asCScriptEngine::RegisterFuncdef(const char *decl)
  3159. {
  3160. if( decl == 0 ) return ConfigError(asINVALID_ARG);
  3161. // Parse the function declaration
  3162. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_FUNCDEF);
  3163. asCBuilder bld(this, 0);
  3164. int r = bld.ParseFunctionDeclaration(0, decl, func, false, 0, 0);
  3165. if( r < 0 )
  3166. {
  3167. // Set as dummy function before deleting
  3168. func->funcType = asFUNC_DUMMY;
  3169. asDELETE(func,asCScriptFunction);
  3170. return ConfigError(asINVALID_DECLARATION);
  3171. }
  3172. // Check name conflicts
  3173. r = bld.CheckNameConflict(func->name.AddressOf(), 0, 0);
  3174. if( r < 0 )
  3175. {
  3176. asDELETE(func,asCScriptFunction);
  3177. return ConfigError(asNAME_TAKEN);
  3178. }
  3179. func->id = GetNextScriptFunctionId();
  3180. SetScriptFunction(func);
  3181. funcDefs.PushLast(func);
  3182. registeredFuncDefs.PushLast(func);
  3183. currentGroup->funcDefs.PushLast(func);
  3184. // If parameter type from other groups are used, add references
  3185. if( func->returnType.GetObjectType() )
  3186. {
  3187. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  3188. currentGroup->RefConfigGroup(group);
  3189. }
  3190. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  3191. {
  3192. if( func->parameterTypes[n].GetObjectType() )
  3193. {
  3194. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  3195. currentGroup->RefConfigGroup(group);
  3196. }
  3197. }
  3198. // Return the function id as success
  3199. return func->id;
  3200. }
  3201. // interface
  3202. int asCScriptEngine::GetFuncdefCount() const
  3203. {
  3204. return (int)registeredFuncDefs.GetLength();
  3205. }
  3206. // interface
  3207. asIScriptFunction *asCScriptEngine::GetFuncdefByIndex(asUINT index, const char **configGroup) const
  3208. {
  3209. if( index >= registeredFuncDefs.GetLength() )
  3210. return 0;
  3211. if( configGroup )
  3212. {
  3213. asCConfigGroup *group = FindConfigGroupForFuncDef(registeredFuncDefs[index]);
  3214. if( group )
  3215. *configGroup = group->groupName.AddressOf();
  3216. else
  3217. *configGroup = 0;
  3218. }
  3219. return registeredFuncDefs[index];
  3220. }
  3221. // interface
  3222. // TODO: typedef: Accept complex types for the typedefs
  3223. int asCScriptEngine::RegisterTypedef(const char *type, const char *decl)
  3224. {
  3225. if( type == 0 ) return ConfigError(asINVALID_NAME);
  3226. // Verify if the name has been registered as a type already
  3227. asUINT n;
  3228. for( n = 0; n < objectTypes.GetLength(); n++ )
  3229. {
  3230. if( objectTypes[n] && objectTypes[n]->name == type )
  3231. return asALREADY_REGISTERED;
  3232. }
  3233. // Grab the data type
  3234. asCTokenizer t(this);
  3235. size_t tokenLen;
  3236. eTokenType token;
  3237. asCDataType dataType;
  3238. // Create the data type
  3239. token = t.GetToken(decl, strlen(decl), &tokenLen);
  3240. switch(token)
  3241. {
  3242. case ttBool:
  3243. case ttInt:
  3244. case ttInt8:
  3245. case ttInt16:
  3246. case ttInt64:
  3247. case ttUInt:
  3248. case ttUInt8:
  3249. case ttUInt16:
  3250. case ttUInt64:
  3251. case ttFloat:
  3252. case ttDouble:
  3253. if( strlen(decl) != tokenLen )
  3254. {
  3255. return ConfigError(asINVALID_TYPE);
  3256. }
  3257. break;
  3258. default:
  3259. return ConfigError(asINVALID_TYPE);
  3260. }
  3261. dataType = asCDataType::CreatePrimitive(token, false);
  3262. // Make sure the name is not a reserved keyword
  3263. token = t.GetToken(type, strlen(type), &tokenLen);
  3264. if( token != ttIdentifier || strlen(type) != tokenLen )
  3265. return ConfigError(asINVALID_NAME);
  3266. asCBuilder bld(this, 0);
  3267. int r = bld.CheckNameConflict(type, 0, 0);
  3268. if( r < 0 )
  3269. return ConfigError(asNAME_TAKEN);
  3270. // Don't have to check against members of object
  3271. // types as they are allowed to use the names
  3272. // Put the data type in the list
  3273. asCObjectType *object= asNEW(asCObjectType)(this);
  3274. object->flags = asOBJ_TYPEDEF;
  3275. object->size = dataType.GetSizeInMemoryBytes();
  3276. object->name = type;
  3277. object->templateSubType = dataType;
  3278. objectTypes.PushLast(object);
  3279. registeredTypeDefs.PushLast(object);
  3280. currentGroup->objTypes.PushLast(object);
  3281. return asSUCCESS;
  3282. }
  3283. // interface
  3284. int asCScriptEngine::GetTypedefCount() const
  3285. {
  3286. return (int)registeredTypeDefs.GetLength();
  3287. }
  3288. // interface
  3289. const char *asCScriptEngine::GetTypedefByIndex(asUINT index, int *typeId, const char **configGroup) const
  3290. {
  3291. if( index >= registeredTypeDefs.GetLength() )
  3292. return 0;
  3293. if( typeId )
  3294. *typeId = GetTypeIdByDecl(registeredTypeDefs[index]->name.AddressOf());
  3295. if( configGroup )
  3296. {
  3297. asCConfigGroup *group = FindConfigGroupForObjectType(registeredTypeDefs[index]);
  3298. if( group )
  3299. *configGroup = group->groupName.AddressOf();
  3300. else
  3301. *configGroup = 0;
  3302. }
  3303. return registeredTypeDefs[index]->name.AddressOf();
  3304. }
  3305. // interface
  3306. int asCScriptEngine::RegisterEnum(const char *name)
  3307. {
  3308. // Check the name
  3309. if( NULL == name )
  3310. return ConfigError(asINVALID_NAME);
  3311. // Verify if the name has been registered as a type already
  3312. asUINT n;
  3313. for( n = 0; n < objectTypes.GetLength(); n++ )
  3314. if( objectTypes[n] && objectTypes[n]->name == name )
  3315. return asALREADY_REGISTERED;
  3316. // Use builder to parse the datatype
  3317. asCDataType dt;
  3318. asCBuilder bld(this, 0);
  3319. bool oldMsgCallback = msgCallback; msgCallback = false;
  3320. int r = bld.ParseDataType(name, &dt);
  3321. msgCallback = oldMsgCallback;
  3322. if( r >= 0 )
  3323. return ConfigError(asERROR);
  3324. // Make sure the name is not a reserved keyword
  3325. asCTokenizer t(this);
  3326. size_t tokenLen;
  3327. int token = t.GetToken(name, strlen(name), &tokenLen);
  3328. if( token != ttIdentifier || strlen(name) != tokenLen )
  3329. return ConfigError(asINVALID_NAME);
  3330. r = bld.CheckNameConflict(name, 0, 0);
  3331. if( r < 0 )
  3332. return ConfigError(asNAME_TAKEN);
  3333. asCObjectType *st = asNEW(asCObjectType)(this);
  3334. asCDataType dataType;
  3335. dataType.CreatePrimitive(ttInt, false);
  3336. st->flags = asOBJ_ENUM;
  3337. st->size = 4;
  3338. st->name = name;
  3339. objectTypes.PushLast(st);
  3340. registeredEnums.PushLast(st);
  3341. currentGroup->objTypes.PushLast(st);
  3342. return asSUCCESS;
  3343. }
  3344. // interface
  3345. int asCScriptEngine::RegisterEnumValue(const char *typeName, const char *valueName, int value)
  3346. {
  3347. // Verify that the correct config group is used
  3348. if( currentGroup->FindType(typeName) == 0 )
  3349. return asWRONG_CONFIG_GROUP;
  3350. asCDataType dt;
  3351. int r;
  3352. asCBuilder bld(this, 0);
  3353. r = bld.ParseDataType(typeName, &dt);
  3354. if( r < 0 )
  3355. return ConfigError(r);
  3356. // Store the enum value
  3357. asCObjectType *ot = dt.GetObjectType();
  3358. if( ot == 0 || !(ot->flags & asOBJ_ENUM) )
  3359. return ConfigError(asINVALID_TYPE);
  3360. if( NULL == valueName )
  3361. return ConfigError(asINVALID_NAME);
  3362. int tokenLen;
  3363. asETokenClass tokenClass = ParseToken(valueName, 0, &tokenLen);
  3364. if( tokenClass != asTC_IDENTIFIER || tokenLen != (int)strlen(valueName) )
  3365. return ConfigError(asINVALID_NAME);
  3366. for( unsigned int n = 0; n < ot->enumValues.GetLength(); n++ )
  3367. {
  3368. if( ot->enumValues[n]->name == valueName )
  3369. return ConfigError(asALREADY_REGISTERED);
  3370. }
  3371. asSEnumValue *e = asNEW(asSEnumValue);
  3372. e->name = valueName;
  3373. e->value = value;
  3374. ot->enumValues.PushLast(e);
  3375. return asSUCCESS;
  3376. }
  3377. // interface
  3378. int asCScriptEngine::GetEnumCount() const
  3379. {
  3380. return (int)registeredEnums.GetLength();
  3381. }
  3382. // interface
  3383. const char *asCScriptEngine::GetEnumByIndex(asUINT index, int *enumTypeId, const char **configGroup) const
  3384. {
  3385. if( index >= registeredEnums.GetLength() )
  3386. return 0;
  3387. if( configGroup )
  3388. {
  3389. asCConfigGroup *group = FindConfigGroupForObjectType(registeredEnums[index]);
  3390. if( group )
  3391. *configGroup = group->groupName.AddressOf();
  3392. else
  3393. *configGroup = 0;
  3394. }
  3395. if( enumTypeId )
  3396. *enumTypeId = GetTypeIdByDecl(registeredEnums[index]->name.AddressOf());
  3397. return registeredEnums[index]->name.AddressOf();
  3398. }
  3399. // interface
  3400. int asCScriptEngine::GetEnumValueCount(int enumTypeId) const
  3401. {
  3402. const asCDataType *dt = GetDataTypeFromTypeId(enumTypeId);
  3403. asCObjectType *t = dt->GetObjectType();
  3404. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  3405. return asINVALID_TYPE;
  3406. return (int)t->enumValues.GetLength();
  3407. }
  3408. // interface
  3409. const char *asCScriptEngine::GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const
  3410. {
  3411. // TODO: This same function is implemented in as_module.cpp as well. Perhaps it should be moved to asCObjectType?
  3412. const asCDataType *dt = GetDataTypeFromTypeId(enumTypeId);
  3413. asCObjectType *t = dt->GetObjectType();
  3414. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  3415. return 0;
  3416. if( index >= t->enumValues.GetLength() )
  3417. return 0;
  3418. if( outValue )
  3419. *outValue = t->enumValues[index]->value;
  3420. return t->enumValues[index]->name.AddressOf();
  3421. }
  3422. // interface
  3423. int asCScriptEngine::GetObjectTypeCount() const
  3424. {
  3425. return (int)registeredObjTypes.GetLength();
  3426. }
  3427. // interface
  3428. asIObjectType *asCScriptEngine::GetObjectTypeByIndex(asUINT index) const
  3429. {
  3430. if( index >= registeredObjTypes.GetLength() )
  3431. return 0;
  3432. return registeredObjTypes[index];
  3433. }
  3434. // interface
  3435. asIObjectType *asCScriptEngine::GetObjectTypeById(int typeId) const
  3436. {
  3437. const asCDataType *dt = GetDataTypeFromTypeId(typeId);
  3438. // Is the type id valid?
  3439. if( !dt ) return 0;
  3440. // Enum types are not objects, so we shouldn't return an object type for them
  3441. if( dt->GetObjectType() && dt->GetObjectType()->GetFlags() & asOBJ_ENUM )
  3442. return 0;
  3443. return dt->GetObjectType();
  3444. }
  3445. asIScriptFunction *asCScriptEngine::GetFunctionDescriptorById(int funcId) const
  3446. {
  3447. return GetScriptFunction(funcId);
  3448. }
  3449. // internal
  3450. bool asCScriptEngine::IsTemplateType(const char *name) const
  3451. {
  3452. // TODO: optimize: Improve linear search
  3453. for( unsigned int n = 0; n < objectTypes.GetLength(); n++ )
  3454. {
  3455. if( objectTypes[n] && objectTypes[n]->name == name )
  3456. {
  3457. return objectTypes[n]->flags & asOBJ_TEMPLATE ? true : false;
  3458. }
  3459. }
  3460. return false;
  3461. }
  3462. // internal
  3463. int asCScriptEngine::AddConstantString(const char *str, size_t len)
  3464. {
  3465. // The str may contain null chars, so we cannot use strlen, or strcmp, or strcpy
  3466. // TODO: optimize: Improve linear search
  3467. // Has the string been registered before?
  3468. for( size_t n = 0; n < stringConstants.GetLength(); n++ )
  3469. {
  3470. if( stringConstants[n]->Compare(str, len) == 0 )
  3471. {
  3472. return (int)n;
  3473. }
  3474. }
  3475. // No match was found, add the string
  3476. asCString *cstr = asNEW(asCString)(str, len);
  3477. stringConstants.PushLast(cstr);
  3478. // The VM currently doesn't handle string ids larger than 65535
  3479. asASSERT(stringConstants.GetLength() <= 65536);
  3480. return (int)stringConstants.GetLength() - 1;
  3481. }
  3482. // internal
  3483. const asCString &asCScriptEngine::GetConstantString(int id)
  3484. {
  3485. return *stringConstants[id];
  3486. }
  3487. // internal
  3488. int asCScriptEngine::GetScriptSectionNameIndex(const char *name)
  3489. {
  3490. // TODO: These names are only released when the engine is freed. The assumption is that
  3491. // the same script section names will be reused instead of there always being new
  3492. // names. Is this assumption valid? Do we need to add reference counting?
  3493. // Store the script section names for future reference
  3494. for( asUINT n = 0; n < scriptSectionNames.GetLength(); n++ )
  3495. {
  3496. if( scriptSectionNames[n]->Compare(name) == 0 )
  3497. return n;
  3498. }
  3499. scriptSectionNames.PushLast(asNEW(asCString)(name));
  3500. return int(scriptSectionNames.GetLength()-1);
  3501. }
  3502. // Urho3D: modified for smaller executable size
  3503. asSFuncPtr::asSFuncPtr()
  3504. {
  3505. asMemClear(this, sizeof(asSFuncPtr));
  3506. }
  3507. asSFuncPtr::asSFuncPtr(asBYTE f)
  3508. {
  3509. asMemClear(this, sizeof(asSFuncPtr));
  3510. flag = f;
  3511. }
  3512. END_AS_NAMESPACE