as_scriptengine.cpp 136 KB

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