as_scriptengine.cpp 167 KB

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