lib265.js 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394
  1. https://static.xverse.cn/wasm/codec-release/h265-dec-sw-wasm/v-0-9-1/libxv265dec.js
  2. // The Module object: Our interface to the outside world. We import
  3. // and export values on it. There are various ways Module can be used:
  4. // 1. Not defined. We create it here
  5. // 2. A function parameter, function(Module) { ..generated code.. }
  6. // 3. pre-run appended it, var Module = {}; ..generated code..
  7. // 4. External script tag defines var Module.
  8. // We need to check if Module already exists (e.g. case 3 above).
  9. // Substitution will be replaced with actual code on later stage of the build,
  10. // this way Closure Compiler will not mangle it (e.g. case 4. above).
  11. // Note that if you want to run closure, and also to use Module
  12. // after the generated code, you will need to define var Module = {};
  13. // before the code. Then that object will be used in the code, and you
  14. // can continue to use Module afterwards as well.
  15. var Module = typeof Module !== 'undefined' ? Module : {};
  16. // --pre-jses are emitted after the Module integration code, so that they can
  17. // refer to Module (if they choose; they can also define Module)
  18. // {{PRE_JSES}}
  19. // Sometimes an existing Module object exists with properties
  20. // meant to overwrite the default module functionality. Here
  21. // we collect those properties and reapply _after_ we configure
  22. // the current environment's defaults to avoid having to be so
  23. // defensive during initialization.
  24. var moduleOverrides = {};
  25. var key;
  26. for (key in Module) {
  27. if (Module.hasOwnProperty(key)) {
  28. moduleOverrides[key] = Module[key];
  29. }
  30. }
  31. var arguments_ = [];
  32. var thisProgram = './this.program';
  33. var quit_ = function(status, toThrow) {
  34. throw toThrow;
  35. };
  36. // Determine the runtime environment we are in. You can customize this by
  37. // setting the ENVIRONMENT setting at compile time (see settings.js).
  38. // Attempt to auto-detect the environment
  39. var ENVIRONMENT_IS_WEB = typeof window === 'object';
  40. var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
  41. // N.b. Electron.js environment is simultaneously a NODE-environment, but
  42. // also a web environment.
  43. var ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';
  44. var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
  45. // `/` should be present at the end if `scriptDirectory` is not empty
  46. var scriptDirectory = '';
  47. function locateFile(path) {
  48. if (Module['locateFile']) {
  49. return Module['locateFile'](path, scriptDirectory);
  50. }
  51. return scriptDirectory + path;
  52. }
  53. // Hooks that are implemented differently in different runtime environments.
  54. var read_,
  55. readAsync,
  56. readBinary,
  57. setWindowTitle;
  58. var nodeFS;
  59. var nodePath;
  60. if (ENVIRONMENT_IS_NODE) {
  61. if (ENVIRONMENT_IS_WORKER) {
  62. scriptDirectory = require('path').dirname(scriptDirectory) + '/';
  63. } else {
  64. scriptDirectory = __dirname + '/';
  65. }
  66. // include: node_shell_read.js
  67. read_ = function shell_read(filename, binary) {
  68. if (!nodeFS) nodeFS = require('fs');
  69. if (!nodePath) nodePath = require('path');
  70. filename = nodePath['normalize'](filename);
  71. return nodeFS['readFileSync'](filename, binary ? null : 'utf8');
  72. };
  73. readBinary = function readBinary(filename) {
  74. var ret = read_(filename, true);
  75. if (!ret.buffer) {
  76. ret = new Uint8Array(ret);
  77. }
  78. assert(ret.buffer);
  79. return ret;
  80. };
  81. readAsync = function readAsync(filename, onload, onerror) {
  82. if (!nodeFS) nodeFS = require('fs');
  83. if (!nodePath) nodePath = require('path');
  84. filename = nodePath['normalize'](filename);
  85. nodeFS['readFile'](filename, function(err, data) {
  86. if (err) onerror(err);
  87. else onload(data.buffer);
  88. });
  89. };
  90. // end include: node_shell_read.js
  91. if (process['argv'].length > 1) {
  92. thisProgram = process['argv'][1].replace(/\\/g, '/');
  93. }
  94. arguments_ = process['argv'].slice(2);
  95. if (typeof module !== 'undefined') {
  96. module['exports'] = Module;
  97. }
  98. process['on']('uncaughtException', function(ex) {
  99. // suppress ExitStatus exceptions from showing an error
  100. if (!(ex instanceof ExitStatus)) {
  101. throw ex;
  102. }
  103. });
  104. process['on']('unhandledRejection', abort);
  105. quit_ = function(status, toThrow) {
  106. if (keepRuntimeAlive()) {
  107. process['exitCode'] = status;
  108. throw toThrow;
  109. }
  110. process['exit'](status);
  111. };
  112. Module['inspect'] = function () { return '[Emscripten Module object]'; };
  113. } else
  114. // Note that this includes Node.js workers when relevant (pthreads is enabled).
  115. // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and
  116. // ENVIRONMENT_IS_NODE.
  117. if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
  118. if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
  119. scriptDirectory = self.location.href;
  120. } else if (typeof document !== 'undefined' && document.currentScript) { // web
  121. scriptDirectory = document.currentScript.src;
  122. }
  123. // blob urls look like blob:http://site.com/etc/etc and we cannot infer anything from them.
  124. // otherwise, slice off the final part of the url to find the script directory.
  125. // if scriptDirectory does not contain a slash, lastIndexOf will return -1,
  126. // and scriptDirectory will correctly be replaced with an empty string.
  127. if (scriptDirectory.indexOf('blob:') !== 0) {
  128. scriptDirectory = scriptDirectory.substr(0, scriptDirectory.lastIndexOf('/')+1);
  129. } else {
  130. scriptDirectory = '';
  131. }
  132. // Differentiate the Web Worker from the Node Worker case, as reading must
  133. // be done differently.
  134. {
  135. // include: web_or_worker_shell_read.js
  136. read_ = function(url) {
  137. var xhr = new XMLHttpRequest();
  138. xhr.open('GET', url, false);
  139. xhr.send(null);
  140. return xhr.responseText;
  141. };
  142. if (ENVIRONMENT_IS_WORKER) {
  143. readBinary = function(url) {
  144. var xhr = new XMLHttpRequest();
  145. xhr.open('GET', url, false);
  146. xhr.responseType = 'arraybuffer';
  147. xhr.send(null);
  148. return new Uint8Array(/** @type{!ArrayBuffer} */(xhr.response));
  149. };
  150. }
  151. readAsync = function(url, onload, onerror) {
  152. var xhr = new XMLHttpRequest();
  153. xhr.open('GET', url, true);
  154. xhr.responseType = 'arraybuffer';
  155. xhr.onload = function() {
  156. if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
  157. onload(xhr.response);
  158. return;
  159. }
  160. onerror();
  161. };
  162. xhr.onerror = onerror;
  163. xhr.send(null);
  164. };
  165. // end include: web_or_worker_shell_read.js
  166. }
  167. setWindowTitle = function(title) { document.title = title };
  168. } else
  169. {
  170. }
  171. // Set up the out() and err() hooks, which are how we can print to stdout or
  172. // stderr, respectively.
  173. var out = Module['print'] || console.log.bind(console);
  174. var err = Module['printErr'] || console.warn.bind(console);
  175. // Merge back in the overrides
  176. for (key in moduleOverrides) {
  177. if (moduleOverrides.hasOwnProperty(key)) {
  178. Module[key] = moduleOverrides[key];
  179. }
  180. }
  181. // Free the object hierarchy contained in the overrides, this lets the GC
  182. // reclaim data used e.g. in memoryInitializerRequest, which is a large typed array.
  183. moduleOverrides = null;
  184. // Emit code to handle expected values on the Module object. This applies Module.x
  185. // to the proper local x. This has two benefits: first, we only emit it if it is
  186. // expected to arrive, and second, by using a local everywhere else that can be
  187. // minified.
  188. if (Module['arguments']) arguments_ = Module['arguments'];
  189. if (Module['thisProgram']) thisProgram = Module['thisProgram'];
  190. if (Module['quit']) quit_ = Module['quit'];
  191. // perform assertions in shell.js after we set up out() and err(), as otherwise if an assertion fails it cannot print the message
  192. var STACK_ALIGN = 16;
  193. function getNativeTypeSize(type) {
  194. switch (type) {
  195. case 'i1': case 'i8': return 1;
  196. case 'i16': return 2;
  197. case 'i32': return 4;
  198. case 'i64': return 8;
  199. case 'float': return 4;
  200. case 'double': return 8;
  201. default: {
  202. if (type[type.length-1] === '*') {
  203. return 4; // A pointer
  204. } else if (type[0] === 'i') {
  205. var bits = Number(type.substr(1));
  206. assert(bits % 8 === 0, 'getNativeTypeSize invalid bits ' + bits + ', type ' + type);
  207. return bits / 8;
  208. } else {
  209. return 0;
  210. }
  211. }
  212. }
  213. }
  214. function warnOnce(text) {
  215. if (!warnOnce.shown) warnOnce.shown = {};
  216. if (!warnOnce.shown[text]) {
  217. warnOnce.shown[text] = 1;
  218. err(text);
  219. }
  220. }
  221. // include: runtime_functions.js
  222. // Wraps a JS function as a wasm function with a given signature.
  223. function convertJsFunctionToWasm(func, sig) {
  224. // If the type reflection proposal is available, use the new
  225. // "WebAssembly.Function" constructor.
  226. // Otherwise, construct a minimal wasm module importing the JS function and
  227. // re-exporting it.
  228. if (typeof WebAssembly.Function === "function") {
  229. var typeNames = {
  230. 'i': 'i32',
  231. 'j': 'i64',
  232. 'f': 'f32',
  233. 'd': 'f64'
  234. };
  235. var type = {
  236. parameters: [],
  237. results: sig[0] == 'v' ? [] : [typeNames[sig[0]]]
  238. };
  239. for (var i = 1; i < sig.length; ++i) {
  240. type.parameters.push(typeNames[sig[i]]);
  241. }
  242. return new WebAssembly.Function(type, func);
  243. }
  244. // The module is static, with the exception of the type section, which is
  245. // generated based on the signature passed in.
  246. var typeSection = [
  247. 0x01, // id: section,
  248. 0x00, // length: 0 (placeholder)
  249. 0x01, // count: 1
  250. 0x60, // form: func
  251. ];
  252. var sigRet = sig.slice(0, 1);
  253. var sigParam = sig.slice(1);
  254. var typeCodes = {
  255. 'i': 0x7f, // i32
  256. 'j': 0x7e, // i64
  257. 'f': 0x7d, // f32
  258. 'd': 0x7c, // f64
  259. };
  260. // Parameters, length + signatures
  261. typeSection.push(sigParam.length);
  262. for (var i = 0; i < sigParam.length; ++i) {
  263. typeSection.push(typeCodes[sigParam[i]]);
  264. }
  265. // Return values, length + signatures
  266. // With no multi-return in MVP, either 0 (void) or 1 (anything else)
  267. if (sigRet == 'v') {
  268. typeSection.push(0x00);
  269. } else {
  270. typeSection = typeSection.concat([0x01, typeCodes[sigRet]]);
  271. }
  272. // Write the overall length of the type section back into the section header
  273. // (excepting the 2 bytes for the section id and length)
  274. typeSection[1] = typeSection.length - 2;
  275. // Rest of the module is static
  276. var bytes = new Uint8Array([
  277. 0x00, 0x61, 0x73, 0x6d, // magic ("\0asm")
  278. 0x01, 0x00, 0x00, 0x00, // version: 1
  279. ].concat(typeSection, [
  280. 0x02, 0x07, // import section
  281. // (import "e" "f" (func 0 (type 0)))
  282. 0x01, 0x01, 0x65, 0x01, 0x66, 0x00, 0x00,
  283. 0x07, 0x05, // export section
  284. // (export "f" (func 0 (type 0)))
  285. 0x01, 0x01, 0x66, 0x00, 0x00,
  286. ]));
  287. // We can compile this wasm module synchronously because it is very small.
  288. // This accepts an import (at "e.f"), that it reroutes to an export (at "f")
  289. var module = new WebAssembly.Module(bytes);
  290. var instance = new WebAssembly.Instance(module, {
  291. 'e': {
  292. 'f': func
  293. }
  294. });
  295. var wrappedFunc = instance.exports['f'];
  296. return wrappedFunc;
  297. }
  298. var freeTableIndexes = [];
  299. // Weak map of functions in the table to their indexes, created on first use.
  300. var functionsInTableMap;
  301. function getEmptyTableSlot() {
  302. // Reuse a free index if there is one, otherwise grow.
  303. if (freeTableIndexes.length) {
  304. return freeTableIndexes.pop();
  305. }
  306. // Grow the table
  307. try {
  308. wasmTable.grow(1);
  309. } catch (err) {
  310. if (!(err instanceof RangeError)) {
  311. throw err;
  312. }
  313. throw 'Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.';
  314. }
  315. return wasmTable.length - 1;
  316. }
  317. // Add a wasm function to the table.
  318. function addFunctionWasm(func, sig) {
  319. // Check if the function is already in the table, to ensure each function
  320. // gets a unique index. First, create the map if this is the first use.
  321. if (!functionsInTableMap) {
  322. functionsInTableMap = new WeakMap();
  323. for (var i = 0; i < wasmTable.length; i++) {
  324. var item = wasmTable.get(i);
  325. // Ignore null values.
  326. if (item) {
  327. functionsInTableMap.set(item, i);
  328. }
  329. }
  330. }
  331. if (functionsInTableMap.has(func)) {
  332. return functionsInTableMap.get(func);
  333. }
  334. // It's not in the table, add it now.
  335. var ret = getEmptyTableSlot();
  336. // Set the new value.
  337. try {
  338. // Attempting to call this with JS function will cause of table.set() to fail
  339. wasmTable.set(ret, func);
  340. } catch (err) {
  341. if (!(err instanceof TypeError)) {
  342. throw err;
  343. }
  344. var wrapped = convertJsFunctionToWasm(func, sig);
  345. wasmTable.set(ret, wrapped);
  346. }
  347. functionsInTableMap.set(func, ret);
  348. return ret;
  349. }
  350. function removeFunction(index) {
  351. functionsInTableMap.delete(wasmTable.get(index));
  352. freeTableIndexes.push(index);
  353. }
  354. // 'sig' parameter is required for the llvm backend but only when func is not
  355. // already a WebAssembly function.
  356. function addFunction(func, sig) {
  357. return addFunctionWasm(func, sig);
  358. }
  359. // end include: runtime_functions.js
  360. // include: runtime_debug.js
  361. // end include: runtime_debug.js
  362. var tempRet0 = 0;
  363. var setTempRet0 = function(value) {
  364. tempRet0 = value;
  365. };
  366. var getTempRet0 = function() {
  367. return tempRet0;
  368. };
  369. // === Preamble library stuff ===
  370. // Documentation for the public APIs defined in this file must be updated in:
  371. // site/source/docs/api_reference/preamble.js.rst
  372. // A prebuilt local version of the documentation is available at:
  373. // site/build/text/docs/api_reference/preamble.js.txt
  374. // You can also build docs locally as HTML or other formats in site/
  375. // An online HTML version (which may be of a different version of Emscripten)
  376. // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
  377. var wasmBinary;
  378. if (Module['wasmBinary']) wasmBinary = Module['wasmBinary'];
  379. var noExitRuntime = Module['noExitRuntime'] || true;
  380. if (typeof WebAssembly !== 'object') {
  381. abort('no native wasm support detected');
  382. }
  383. // include: runtime_safe_heap.js
  384. // In MINIMAL_RUNTIME, setValue() and getValue() are only available when building with safe heap enabled, for heap safety checking.
  385. // In traditional runtime, setValue() and getValue() are always available (although their use is highly discouraged due to perf penalties)
  386. /** @param {number} ptr
  387. @param {number} value
  388. @param {string} type
  389. @param {number|boolean=} noSafe */
  390. function setValue(ptr, value, type, noSafe) {
  391. type = type || 'i8';
  392. if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit
  393. switch (type) {
  394. case 'i1': HEAP8[((ptr)>>0)] = value; break;
  395. case 'i8': HEAP8[((ptr)>>0)] = value; break;
  396. case 'i16': HEAP16[((ptr)>>1)] = value; break;
  397. case 'i32': HEAP32[((ptr)>>2)] = value; break;
  398. case 'i64': (tempI64 = [value>>>0,(tempDouble=value,(+(Math.abs(tempDouble))) >= 1.0 ? (tempDouble > 0.0 ? ((Math.min((+(Math.floor((tempDouble)/4294967296.0))), 4294967295.0))|0)>>>0 : (~~((+(Math.ceil((tempDouble - +(((~~(tempDouble)))>>>0))/4294967296.0)))))>>>0) : 0)],HEAP32[((ptr)>>2)] = tempI64[0],HEAP32[(((ptr)+(4))>>2)] = tempI64[1]); break;
  399. case 'float': HEAPF32[((ptr)>>2)] = value; break;
  400. case 'double': HEAPF64[((ptr)>>3)] = value; break;
  401. default: abort('invalid type for setValue: ' + type);
  402. }
  403. }
  404. /** @param {number} ptr
  405. @param {string} type
  406. @param {number|boolean=} noSafe */
  407. function getValue(ptr, type, noSafe) {
  408. type = type || 'i8';
  409. if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit
  410. switch (type) {
  411. case 'i1': return HEAP8[((ptr)>>0)];
  412. case 'i8': return HEAP8[((ptr)>>0)];
  413. case 'i16': return HEAP16[((ptr)>>1)];
  414. case 'i32': return HEAP32[((ptr)>>2)];
  415. case 'i64': return HEAP32[((ptr)>>2)];
  416. case 'float': return HEAPF32[((ptr)>>2)];
  417. case 'double': return HEAPF64[((ptr)>>3)];
  418. default: abort('invalid type for getValue: ' + type);
  419. }
  420. return null;
  421. }
  422. // end include: runtime_safe_heap.js
  423. // Wasm globals
  424. var wasmMemory;
  425. //========================================
  426. // Runtime essentials
  427. //========================================
  428. // whether we are quitting the application. no code should run after this.
  429. // set in exit() and abort()
  430. var ABORT = false;
  431. // set by exit() and abort(). Passed to 'onExit' handler.
  432. // NOTE: This is also used as the process return code code in shell environments
  433. // but only when noExitRuntime is false.
  434. var EXITSTATUS;
  435. /** @type {function(*, string=)} */
  436. function assert(condition, text) {
  437. if (!condition) {
  438. abort('Assertion failed: ' + text);
  439. }
  440. }
  441. // Returns the C function with a specified identifier (for C++, you need to do manual name mangling)
  442. function getCFunc(ident) {
  443. var func = Module['_' + ident]; // closure exported function
  444. assert(func, 'Cannot call unknown function ' + ident + ', make sure it is exported');
  445. return func;
  446. }
  447. // C calling interface.
  448. /** @param {string|null=} returnType
  449. @param {Array=} argTypes
  450. @param {Arguments|Array=} args
  451. @param {Object=} opts */
  452. function ccall(ident, returnType, argTypes, args, opts) {
  453. // For fast lookup of conversion functions
  454. var toC = {
  455. 'string': function(str) {
  456. var ret = 0;
  457. if (str !== null && str !== undefined && str !== 0) { // null string
  458. // at most 4 bytes per UTF-8 code point, +1 for the trailing '\0'
  459. var len = (str.length << 2) + 1;
  460. ret = stackAlloc(len);
  461. stringToUTF8(str, ret, len);
  462. }
  463. return ret;
  464. },
  465. 'array': function(arr) {
  466. var ret = stackAlloc(arr.length);
  467. writeArrayToMemory(arr, ret);
  468. return ret;
  469. }
  470. };
  471. function convertReturnValue(ret) {
  472. if (returnType === 'string') return UTF8ToString(ret);
  473. if (returnType === 'boolean') return Boolean(ret);
  474. return ret;
  475. }
  476. var func = getCFunc(ident);
  477. var cArgs = [];
  478. var stack = 0;
  479. if (args) {
  480. for (var i = 0; i < args.length; i++) {
  481. var converter = toC[argTypes[i]];
  482. if (converter) {
  483. if (stack === 0) stack = stackSave();
  484. cArgs[i] = converter(args[i]);
  485. } else {
  486. cArgs[i] = args[i];
  487. }
  488. }
  489. }
  490. var ret = func.apply(null, cArgs);
  491. function onDone(ret) {
  492. if (stack !== 0) stackRestore(stack);
  493. return convertReturnValue(ret);
  494. }
  495. ret = onDone(ret);
  496. return ret;
  497. }
  498. /** @param {string=} returnType
  499. @param {Array=} argTypes
  500. @param {Object=} opts */
  501. function cwrap(ident, returnType, argTypes, opts) {
  502. argTypes = argTypes || [];
  503. // When the function takes numbers and returns a number, we can just return
  504. // the original function
  505. var numericArgs = argTypes.every(function(type){ return type === 'number'});
  506. var numericRet = returnType !== 'string';
  507. if (numericRet && numericArgs && !opts) {
  508. return getCFunc(ident);
  509. }
  510. return function() {
  511. return ccall(ident, returnType, argTypes, arguments, opts);
  512. }
  513. }
  514. var ALLOC_NORMAL = 0; // Tries to use _malloc()
  515. var ALLOC_STACK = 1; // Lives for the duration of the current function call
  516. // allocate(): This is for internal use. You can use it yourself as well, but the interface
  517. // is a little tricky (see docs right below). The reason is that it is optimized
  518. // for multiple syntaxes to save space in generated code. So you should
  519. // normally not use allocate(), and instead allocate memory using _malloc(),
  520. // initialize it with setValue(), and so forth.
  521. // @slab: An array of data.
  522. // @allocator: How to allocate memory, see ALLOC_*
  523. /** @type {function((Uint8Array|Array<number>), number)} */
  524. function allocate(slab, allocator) {
  525. var ret;
  526. if (allocator == ALLOC_STACK) {
  527. ret = stackAlloc(slab.length);
  528. } else {
  529. ret = _malloc(slab.length);
  530. }
  531. if (slab.subarray || slab.slice) {
  532. HEAPU8.set(/** @type {!Uint8Array} */(slab), ret);
  533. } else {
  534. HEAPU8.set(new Uint8Array(slab), ret);
  535. }
  536. return ret;
  537. }
  538. // include: runtime_strings.js
  539. // runtime_strings.js: Strings related runtime functions that are part of both MINIMAL_RUNTIME and regular runtime.
  540. // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns
  541. // a copy of that string as a Javascript String object.
  542. var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined;
  543. /**
  544. * @param {number} idx
  545. * @param {number=} maxBytesToRead
  546. * @return {string}
  547. */
  548. function UTF8ArrayToString(heap, idx, maxBytesToRead) {
  549. var endIdx = idx + maxBytesToRead;
  550. var endPtr = idx;
  551. // TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
  552. // Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
  553. // (As a tiny code save trick, compare endPtr against endIdx using a negation, so that undefined means Infinity)
  554. while (heap[endPtr] && !(endPtr >= endIdx)) ++endPtr;
  555. if (endPtr - idx > 16 && heap.subarray && UTF8Decoder) {
  556. return UTF8Decoder.decode(heap.subarray(idx, endPtr));
  557. } else {
  558. var str = '';
  559. // If building with TextDecoder, we have already computed the string length above, so test loop end condition against that
  560. while (idx < endPtr) {
  561. // For UTF8 byte structure, see:
  562. // http://en.wikipedia.org/wiki/UTF-8#Description
  563. // https://www.ietf.org/rfc/rfc2279.txt
  564. // https://tools.ietf.org/html/rfc3629
  565. var u0 = heap[idx++];
  566. if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
  567. var u1 = heap[idx++] & 63;
  568. if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
  569. var u2 = heap[idx++] & 63;
  570. if ((u0 & 0xF0) == 0xE0) {
  571. u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
  572. } else {
  573. u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heap[idx++] & 63);
  574. }
  575. if (u0 < 0x10000) {
  576. str += String.fromCharCode(u0);
  577. } else {
  578. var ch = u0 - 0x10000;
  579. str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
  580. }
  581. }
  582. }
  583. return str;
  584. }
  585. // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the emscripten HEAP, returns a
  586. // copy of that string as a Javascript String object.
  587. // maxBytesToRead: an optional length that specifies the maximum number of bytes to read. You can omit
  588. // this parameter to scan the string until the first \0 byte. If maxBytesToRead is
  589. // passed, and the string at [ptr, ptr+maxBytesToReadr[ contains a null byte in the
  590. // middle, then the string will cut short at that byte index (i.e. maxBytesToRead will
  591. // not produce a string of exact length [ptr, ptr+maxBytesToRead[)
  592. // N.B. mixing frequent uses of UTF8ToString() with and without maxBytesToRead may
  593. // throw JS JIT optimizations off, so it is worth to consider consistently using one
  594. // style or the other.
  595. /**
  596. * @param {number} ptr
  597. * @param {number=} maxBytesToRead
  598. * @return {string}
  599. */
  600. function UTF8ToString(ptr, maxBytesToRead) {
  601. return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : '';
  602. }
  603. // Copies the given Javascript String object 'str' to the given byte array at address 'outIdx',
  604. // encoded in UTF8 form and null-terminated. The copy will require at most str.length*4+1 bytes of space in the HEAP.
  605. // Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write.
  606. // Parameters:
  607. // str: the Javascript string to copy.
  608. // heap: the array to copy to. Each index in this array is assumed to be one 8-byte element.
  609. // outIdx: The starting offset in the array to begin the copying.
  610. // maxBytesToWrite: The maximum number of bytes this function can write to the array.
  611. // This count should include the null terminator,
  612. // i.e. if maxBytesToWrite=1, only the null terminator will be written and nothing else.
  613. // maxBytesToWrite=0 does not write any bytes to the output, not even the null terminator.
  614. // Returns the number of bytes written, EXCLUDING the null terminator.
  615. function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
  616. if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes.
  617. return 0;
  618. var startIdx = outIdx;
  619. var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator.
  620. for (var i = 0; i < str.length; ++i) {
  621. // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8.
  622. // See http://unicode.org/faq/utf_bom.html#utf16-3
  623. // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629
  624. var u = str.charCodeAt(i); // possibly a lead surrogate
  625. if (u >= 0xD800 && u <= 0xDFFF) {
  626. var u1 = str.charCodeAt(++i);
  627. u = 0x10000 + ((u & 0x3FF) << 10) | (u1 & 0x3FF);
  628. }
  629. if (u <= 0x7F) {
  630. if (outIdx >= endIdx) break;
  631. heap[outIdx++] = u;
  632. } else if (u <= 0x7FF) {
  633. if (outIdx + 1 >= endIdx) break;
  634. heap[outIdx++] = 0xC0 | (u >> 6);
  635. heap[outIdx++] = 0x80 | (u & 63);
  636. } else if (u <= 0xFFFF) {
  637. if (outIdx + 2 >= endIdx) break;
  638. heap[outIdx++] = 0xE0 | (u >> 12);
  639. heap[outIdx++] = 0x80 | ((u >> 6) & 63);
  640. heap[outIdx++] = 0x80 | (u & 63);
  641. } else {
  642. if (outIdx + 3 >= endIdx) break;
  643. heap[outIdx++] = 0xF0 | (u >> 18);
  644. heap[outIdx++] = 0x80 | ((u >> 12) & 63);
  645. heap[outIdx++] = 0x80 | ((u >> 6) & 63);
  646. heap[outIdx++] = 0x80 | (u & 63);
  647. }
  648. }
  649. // Null-terminate the pointer to the buffer.
  650. heap[outIdx] = 0;
  651. return outIdx - startIdx;
  652. }
  653. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  654. // null-terminated and encoded in UTF8 form. The copy will require at most str.length*4+1 bytes of space in the HEAP.
  655. // Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write.
  656. // Returns the number of bytes written, EXCLUDING the null terminator.
  657. function stringToUTF8(str, outPtr, maxBytesToWrite) {
  658. return stringToUTF8Array(str, HEAPU8,outPtr, maxBytesToWrite);
  659. }
  660. // Returns the number of bytes the given Javascript string takes if encoded as a UTF8 byte array, EXCLUDING the null terminator byte.
  661. function lengthBytesUTF8(str) {
  662. var len = 0;
  663. for (var i = 0; i < str.length; ++i) {
  664. // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8.
  665. // See http://unicode.org/faq/utf_bom.html#utf16-3
  666. var u = str.charCodeAt(i); // possibly a lead surrogate
  667. if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
  668. if (u <= 0x7F) ++len;
  669. else if (u <= 0x7FF) len += 2;
  670. else if (u <= 0xFFFF) len += 3;
  671. else len += 4;
  672. }
  673. return len;
  674. }
  675. // end include: runtime_strings.js
  676. // include: runtime_strings_extra.js
  677. // runtime_strings_extra.js: Strings related runtime functions that are available only in regular runtime.
  678. // Given a pointer 'ptr' to a null-terminated ASCII-encoded string in the emscripten HEAP, returns
  679. // a copy of that string as a Javascript String object.
  680. function AsciiToString(ptr) {
  681. var str = '';
  682. while (1) {
  683. var ch = HEAPU8[((ptr++)>>0)];
  684. if (!ch) return str;
  685. str += String.fromCharCode(ch);
  686. }
  687. }
  688. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  689. // null-terminated and encoded in ASCII form. The copy will require at most str.length+1 bytes of space in the HEAP.
  690. function stringToAscii(str, outPtr) {
  691. return writeAsciiToMemory(str, outPtr, false);
  692. }
  693. // Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns
  694. // a copy of that string as a Javascript String object.
  695. var UTF16Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-16le') : undefined;
  696. function UTF16ToString(ptr, maxBytesToRead) {
  697. var endPtr = ptr;
  698. // TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
  699. // Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
  700. var idx = endPtr >> 1;
  701. var maxIdx = idx + maxBytesToRead / 2;
  702. // If maxBytesToRead is not passed explicitly, it will be undefined, and this
  703. // will always evaluate to true. This saves on code size.
  704. while (!(idx >= maxIdx) && HEAPU16[idx]) ++idx;
  705. endPtr = idx << 1;
  706. if (endPtr - ptr > 32 && UTF16Decoder) {
  707. return UTF16Decoder.decode(HEAPU8.subarray(ptr, endPtr));
  708. } else {
  709. var str = '';
  710. // If maxBytesToRead is not passed explicitly, it will be undefined, and the for-loop's condition
  711. // will always evaluate to true. The loop is then terminated on the first null char.
  712. for (var i = 0; !(i >= maxBytesToRead / 2); ++i) {
  713. var codeUnit = HEAP16[(((ptr)+(i*2))>>1)];
  714. if (codeUnit == 0) break;
  715. // fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through.
  716. str += String.fromCharCode(codeUnit);
  717. }
  718. return str;
  719. }
  720. }
  721. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  722. // null-terminated and encoded in UTF16 form. The copy will require at most str.length*4+2 bytes of space in the HEAP.
  723. // Use the function lengthBytesUTF16() to compute the exact number of bytes (excluding null terminator) that this function will write.
  724. // Parameters:
  725. // str: the Javascript string to copy.
  726. // outPtr: Byte address in Emscripten HEAP where to write the string to.
  727. // maxBytesToWrite: The maximum number of bytes this function can write to the array. This count should include the null
  728. // terminator, i.e. if maxBytesToWrite=2, only the null terminator will be written and nothing else.
  729. // maxBytesToWrite<2 does not write any bytes to the output, not even the null terminator.
  730. // Returns the number of bytes written, EXCLUDING the null terminator.
  731. function stringToUTF16(str, outPtr, maxBytesToWrite) {
  732. // Backwards compatibility: if max bytes is not specified, assume unsafe unbounded write is allowed.
  733. if (maxBytesToWrite === undefined) {
  734. maxBytesToWrite = 0x7FFFFFFF;
  735. }
  736. if (maxBytesToWrite < 2) return 0;
  737. maxBytesToWrite -= 2; // Null terminator.
  738. var startPtr = outPtr;
  739. var numCharsToWrite = (maxBytesToWrite < str.length*2) ? (maxBytesToWrite / 2) : str.length;
  740. for (var i = 0; i < numCharsToWrite; ++i) {
  741. // charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP.
  742. var codeUnit = str.charCodeAt(i); // possibly a lead surrogate
  743. HEAP16[((outPtr)>>1)] = codeUnit;
  744. outPtr += 2;
  745. }
  746. // Null-terminate the pointer to the HEAP.
  747. HEAP16[((outPtr)>>1)] = 0;
  748. return outPtr - startPtr;
  749. }
  750. // Returns the number of bytes the given Javascript string takes if encoded as a UTF16 byte array, EXCLUDING the null terminator byte.
  751. function lengthBytesUTF16(str) {
  752. return str.length*2;
  753. }
  754. function UTF32ToString(ptr, maxBytesToRead) {
  755. var i = 0;
  756. var str = '';
  757. // If maxBytesToRead is not passed explicitly, it will be undefined, and this
  758. // will always evaluate to true. This saves on code size.
  759. while (!(i >= maxBytesToRead / 4)) {
  760. var utf32 = HEAP32[(((ptr)+(i*4))>>2)];
  761. if (utf32 == 0) break;
  762. ++i;
  763. // Gotcha: fromCharCode constructs a character from a UTF-16 encoded code (pair), not from a Unicode code point! So encode the code point to UTF-16 for constructing.
  764. // See http://unicode.org/faq/utf_bom.html#utf16-3
  765. if (utf32 >= 0x10000) {
  766. var ch = utf32 - 0x10000;
  767. str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
  768. } else {
  769. str += String.fromCharCode(utf32);
  770. }
  771. }
  772. return str;
  773. }
  774. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  775. // null-terminated and encoded in UTF32 form. The copy will require at most str.length*4+4 bytes of space in the HEAP.
  776. // Use the function lengthBytesUTF32() to compute the exact number of bytes (excluding null terminator) that this function will write.
  777. // Parameters:
  778. // str: the Javascript string to copy.
  779. // outPtr: Byte address in Emscripten HEAP where to write the string to.
  780. // maxBytesToWrite: The maximum number of bytes this function can write to the array. This count should include the null
  781. // terminator, i.e. if maxBytesToWrite=4, only the null terminator will be written and nothing else.
  782. // maxBytesToWrite<4 does not write any bytes to the output, not even the null terminator.
  783. // Returns the number of bytes written, EXCLUDING the null terminator.
  784. function stringToUTF32(str, outPtr, maxBytesToWrite) {
  785. // Backwards compatibility: if max bytes is not specified, assume unsafe unbounded write is allowed.
  786. if (maxBytesToWrite === undefined) {
  787. maxBytesToWrite = 0x7FFFFFFF;
  788. }
  789. if (maxBytesToWrite < 4) return 0;
  790. var startPtr = outPtr;
  791. var endPtr = startPtr + maxBytesToWrite - 4;
  792. for (var i = 0; i < str.length; ++i) {
  793. // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! We must decode the string to UTF-32 to the heap.
  794. // See http://unicode.org/faq/utf_bom.html#utf16-3
  795. var codeUnit = str.charCodeAt(i); // possibly a lead surrogate
  796. if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) {
  797. var trailSurrogate = str.charCodeAt(++i);
  798. codeUnit = 0x10000 + ((codeUnit & 0x3FF) << 10) | (trailSurrogate & 0x3FF);
  799. }
  800. HEAP32[((outPtr)>>2)] = codeUnit;
  801. outPtr += 4;
  802. if (outPtr + 4 > endPtr) break;
  803. }
  804. // Null-terminate the pointer to the HEAP.
  805. HEAP32[((outPtr)>>2)] = 0;
  806. return outPtr - startPtr;
  807. }
  808. // Returns the number of bytes the given Javascript string takes if encoded as a UTF16 byte array, EXCLUDING the null terminator byte.
  809. function lengthBytesUTF32(str) {
  810. var len = 0;
  811. for (var i = 0; i < str.length; ++i) {
  812. // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! We must decode the string to UTF-32 to the heap.
  813. // See http://unicode.org/faq/utf_bom.html#utf16-3
  814. var codeUnit = str.charCodeAt(i);
  815. if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) ++i; // possibly a lead surrogate, so skip over the tail surrogate.
  816. len += 4;
  817. }
  818. return len;
  819. }
  820. // Allocate heap space for a JS string, and write it there.
  821. // It is the responsibility of the caller to free() that memory.
  822. function allocateUTF8(str) {
  823. var size = lengthBytesUTF8(str) + 1;
  824. var ret = _malloc(size);
  825. if (ret) stringToUTF8Array(str, HEAP8, ret, size);
  826. return ret;
  827. }
  828. // Allocate stack space for a JS string, and write it there.
  829. function allocateUTF8OnStack(str) {
  830. var size = lengthBytesUTF8(str) + 1;
  831. var ret = stackAlloc(size);
  832. stringToUTF8Array(str, HEAP8, ret, size);
  833. return ret;
  834. }
  835. // Deprecated: This function should not be called because it is unsafe and does not provide
  836. // a maximum length limit of how many bytes it is allowed to write. Prefer calling the
  837. // function stringToUTF8Array() instead, which takes in a maximum length that can be used
  838. // to be secure from out of bounds writes.
  839. /** @deprecated
  840. @param {boolean=} dontAddNull */
  841. function writeStringToMemory(string, buffer, dontAddNull) {
  842. warnOnce('writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!');
  843. var /** @type {number} */ lastChar, /** @type {number} */ end;
  844. if (dontAddNull) {
  845. // stringToUTF8Array always appends null. If we don't want to do that, remember the
  846. // character that existed at the location where the null will be placed, and restore
  847. // that after the write (below).
  848. end = buffer + lengthBytesUTF8(string);
  849. lastChar = HEAP8[end];
  850. }
  851. stringToUTF8(string, buffer, Infinity);
  852. if (dontAddNull) HEAP8[end] = lastChar; // Restore the value under the null character.
  853. }
  854. function writeArrayToMemory(array, buffer) {
  855. HEAP8.set(array, buffer);
  856. }
  857. /** @param {boolean=} dontAddNull */
  858. function writeAsciiToMemory(str, buffer, dontAddNull) {
  859. for (var i = 0; i < str.length; ++i) {
  860. HEAP8[((buffer++)>>0)] = str.charCodeAt(i);
  861. }
  862. // Null-terminate the pointer to the HEAP.
  863. if (!dontAddNull) HEAP8[((buffer)>>0)] = 0;
  864. }
  865. // end include: runtime_strings_extra.js
  866. // Memory management
  867. function alignUp(x, multiple) {
  868. if (x % multiple > 0) {
  869. x += multiple - (x % multiple);
  870. }
  871. return x;
  872. }
  873. var HEAP,
  874. /** @type {ArrayBuffer} */
  875. buffer,
  876. /** @type {Int8Array} */
  877. HEAP8,
  878. /** @type {Uint8Array} */
  879. HEAPU8,
  880. /** @type {Int16Array} */
  881. HEAP16,
  882. /** @type {Uint16Array} */
  883. HEAPU16,
  884. /** @type {Int32Array} */
  885. HEAP32,
  886. /** @type {Uint32Array} */
  887. HEAPU32,
  888. /** @type {Float32Array} */
  889. HEAPF32,
  890. /** @type {Float64Array} */
  891. HEAPF64;
  892. function updateGlobalBufferAndViews(buf) {
  893. buffer = buf;
  894. Module['HEAP8'] = HEAP8 = new Int8Array(buf);
  895. Module['HEAP16'] = HEAP16 = new Int16Array(buf);
  896. Module['HEAP32'] = HEAP32 = new Int32Array(buf);
  897. Module['HEAPU8'] = HEAPU8 = new Uint8Array(buf);
  898. Module['HEAPU16'] = HEAPU16 = new Uint16Array(buf);
  899. Module['HEAPU32'] = HEAPU32 = new Uint32Array(buf);
  900. Module['HEAPF32'] = HEAPF32 = new Float32Array(buf);
  901. Module['HEAPF64'] = HEAPF64 = new Float64Array(buf);
  902. }
  903. var TOTAL_STACK = 5242880;
  904. var INITIAL_MEMORY = Module['INITIAL_MEMORY'] || 67108864;
  905. // include: runtime_init_table.js
  906. // In regular non-RELOCATABLE mode the table is exported
  907. // from the wasm module and this will be assigned once
  908. // the exports are available.
  909. var wasmTable;
  910. // end include: runtime_init_table.js
  911. // include: runtime_stack_check.js
  912. // end include: runtime_stack_check.js
  913. // include: runtime_assertions.js
  914. // end include: runtime_assertions.js
  915. var __ATPRERUN__ = []; // functions called before the runtime is initialized
  916. var __ATINIT__ = []; // functions called during startup
  917. var __ATEXIT__ = []; // functions called during shutdown
  918. var __ATPOSTRUN__ = []; // functions called after the main() is called
  919. var runtimeInitialized = false;
  920. var runtimeExited = false;
  921. var runtimeKeepaliveCounter = 0;
  922. function keepRuntimeAlive() {
  923. return noExitRuntime || runtimeKeepaliveCounter > 0;
  924. }
  925. function preRun() {
  926. if (Module['preRun']) {
  927. if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
  928. while (Module['preRun'].length) {
  929. addOnPreRun(Module['preRun'].shift());
  930. }
  931. }
  932. callRuntimeCallbacks(__ATPRERUN__);
  933. }
  934. function initRuntime() {
  935. runtimeInitialized = true;
  936. if (!Module["noFSInit"] && !FS.init.initialized)
  937. FS.init();
  938. FS.ignorePermissions = false;
  939. TTY.init();
  940. callRuntimeCallbacks(__ATINIT__);
  941. }
  942. function exitRuntime() {
  943. runtimeExited = true;
  944. }
  945. function postRun() {
  946. if (Module['postRun']) {
  947. if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
  948. while (Module['postRun'].length) {
  949. addOnPostRun(Module['postRun'].shift());
  950. }
  951. }
  952. callRuntimeCallbacks(__ATPOSTRUN__);
  953. }
  954. function addOnPreRun(cb) {
  955. __ATPRERUN__.unshift(cb);
  956. }
  957. function addOnInit(cb) {
  958. __ATINIT__.unshift(cb);
  959. }
  960. function addOnExit(cb) {
  961. }
  962. function addOnPostRun(cb) {
  963. __ATPOSTRUN__.unshift(cb);
  964. }
  965. // include: runtime_math.js
  966. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
  967. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround
  968. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32
  969. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
  970. // end include: runtime_math.js
  971. // A counter of dependencies for calling run(). If we need to
  972. // do asynchronous work before running, increment this and
  973. // decrement it. Incrementing must happen in a place like
  974. // Module.preRun (used by emcc to add file preloading).
  975. // Note that you can add dependencies in preRun, even though
  976. // it happens right before run - run will be postponed until
  977. // the dependencies are met.
  978. var runDependencies = 0;
  979. var runDependencyWatcher = null;
  980. var dependenciesFulfilled = null; // overridden to take different actions when all run dependencies are fulfilled
  981. function getUniqueRunDependency(id) {
  982. return id;
  983. }
  984. function addRunDependency(id) {
  985. runDependencies++;
  986. if (Module['monitorRunDependencies']) {
  987. Module['monitorRunDependencies'](runDependencies);
  988. }
  989. }
  990. function removeRunDependency(id) {
  991. runDependencies--;
  992. if (Module['monitorRunDependencies']) {
  993. Module['monitorRunDependencies'](runDependencies);
  994. }
  995. if (runDependencies == 0) {
  996. if (runDependencyWatcher !== null) {
  997. clearInterval(runDependencyWatcher);
  998. runDependencyWatcher = null;
  999. }
  1000. if (dependenciesFulfilled) {
  1001. var callback = dependenciesFulfilled;
  1002. dependenciesFulfilled = null;
  1003. callback(); // can add another dependenciesFulfilled
  1004. }
  1005. }
  1006. }
  1007. Module["preloadedImages"] = {}; // maps url to image data
  1008. Module["preloadedAudios"] = {}; // maps url to audio data
  1009. /** @param {string|number=} what */
  1010. function abort(what) {
  1011. {
  1012. if (Module['onAbort']) {
  1013. Module['onAbort'](what);
  1014. }
  1015. }
  1016. what += '';
  1017. err(what);
  1018. ABORT = true;
  1019. EXITSTATUS = 1;
  1020. what = 'abort(' + what + '). Build with -s ASSERTIONS=1 for more info.';
  1021. // Use a wasm runtime error, because a JS error might be seen as a foreign
  1022. // exception, which means we'd run destructors on it. We need the error to
  1023. // simply make the program stop.
  1024. var e = new WebAssembly.RuntimeError(what);
  1025. // Throw the error whether or not MODULARIZE is set because abort is used
  1026. // in code paths apart from instantiation where an exception is expected
  1027. // to be thrown when abort is called.
  1028. throw e;
  1029. }
  1030. // {{MEM_INITIALIZER}}
  1031. // include: memoryprofiler.js
  1032. // end include: memoryprofiler.js
  1033. // include: URIUtils.js
  1034. // Prefix of data URIs emitted by SINGLE_FILE and related options.
  1035. var dataURIPrefix = 'data:application/octet-stream;base64,';
  1036. // Indicates whether filename is a base64 data URI.
  1037. function isDataURI(filename) {
  1038. // Prefix of data URIs emitted by SINGLE_FILE and related options.
  1039. return filename.startsWith(dataURIPrefix);
  1040. }
  1041. // Indicates whether filename is delivered via file protocol (as opposed to http/https)
  1042. function isFileURI(filename) {
  1043. return filename.startsWith('file://');
  1044. }
  1045. // end include: URIUtils.js
  1046. var wasmBinaryFile;
  1047. wasmBinaryFile = 'https://static.xverse.cn/wasm/codec-release/h265-dec-sw-wasm/v-0-9-1/libxv265dec.wasm';
  1048. if (!isDataURI(wasmBinaryFile)) {
  1049. wasmBinaryFile = locateFile(wasmBinaryFile);
  1050. }
  1051. function getBinary(file) {
  1052. try {
  1053. if (file == wasmBinaryFile && wasmBinary) {
  1054. return new Uint8Array(wasmBinary);
  1055. }
  1056. if (readBinary) {
  1057. return readBinary(file);
  1058. } else {
  1059. throw "both async and sync fetching of the wasm failed";
  1060. }
  1061. }
  1062. catch (err) {
  1063. abort(err);
  1064. }
  1065. }
  1066. function getBinaryPromise() {
  1067. // If we don't have the binary yet, try to to load it asynchronously.
  1068. // Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
  1069. // See https://github.com/github/fetch/pull/92#issuecomment-140665932
  1070. // Cordova or Electron apps are typically loaded from a file:// url.
  1071. // So use fetch if it is available and the url is not a file, otherwise fall back to XHR.
  1072. if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
  1073. if (typeof fetch === 'function'
  1074. && !isFileURI(wasmBinaryFile)
  1075. ) {
  1076. return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
  1077. if (!response['ok']) {
  1078. throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
  1079. }
  1080. return response['arrayBuffer']();
  1081. }).catch(function () {
  1082. return getBinary(wasmBinaryFile);
  1083. });
  1084. }
  1085. else {
  1086. if (readAsync) {
  1087. // fetch is not available or url is file => try XHR (readAsync uses XHR internally)
  1088. return new Promise(function(resolve, reject) {
  1089. readAsync(wasmBinaryFile, function(response) { resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))) }, reject)
  1090. });
  1091. }
  1092. }
  1093. }
  1094. // Otherwise, getBinary should be able to get it synchronously
  1095. return Promise.resolve().then(function() { return getBinary(wasmBinaryFile); });
  1096. }
  1097. // Create the wasm instance.
  1098. // Receives the wasm imports, returns the exports.
  1099. function createWasm() {
  1100. // prepare imports
  1101. var info = {
  1102. 'env': asmLibraryArg,
  1103. 'wasi_snapshot_preview1': asmLibraryArg,
  1104. };
  1105. // Load the wasm module and create an instance of using native support in the JS engine.
  1106. // handle a generated wasm instance, receiving its exports and
  1107. // performing other necessary setup
  1108. /** @param {WebAssembly.Module=} module*/
  1109. function receiveInstance(instance, module) {
  1110. var exports = instance.exports;
  1111. Module['asm'] = exports;
  1112. wasmMemory = Module['asm']['memory'];
  1113. updateGlobalBufferAndViews(wasmMemory.buffer);
  1114. wasmTable = Module['asm']['__indirect_function_table'];
  1115. addOnInit(Module['asm']['__wasm_call_ctors']);
  1116. removeRunDependency('wasm-instantiate');
  1117. }
  1118. // we can't run yet (except in a pthread, where we have a custom sync instantiator)
  1119. addRunDependency('wasm-instantiate');
  1120. // Prefer streaming instantiation if available.
  1121. function receiveInstantiationResult(result) {
  1122. // 'result' is a ResultObject object which has both the module and instance.
  1123. // receiveInstance() will swap in the exports (to Module.asm) so they can be called
  1124. // TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193, the above line no longer optimizes out down to the following line.
  1125. // When the regression is fixed, can restore the above USE_PTHREADS-enabled path.
  1126. receiveInstance(result['instance']);
  1127. }
  1128. function instantiateArrayBuffer(receiver) {
  1129. return getBinaryPromise().then(function(binary) {
  1130. return WebAssembly.instantiate(binary, info);
  1131. }).then(function (instance) {
  1132. return instance;
  1133. }).then(receiver, function(reason) {
  1134. err('failed to asynchronously prepare wasm: ' + reason);
  1135. abort(reason);
  1136. });
  1137. }
  1138. function instantiateAsync() {
  1139. if (!wasmBinary &&
  1140. typeof WebAssembly.instantiateStreaming === 'function' &&
  1141. !isDataURI(wasmBinaryFile) &&
  1142. // Don't use streaming for file:// delivered objects in a webview, fetch them synchronously.
  1143. !isFileURI(wasmBinaryFile) &&
  1144. typeof fetch === 'function') {
  1145. return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function (response) {
  1146. var result = WebAssembly.instantiateStreaming(response, info);
  1147. return result.then(
  1148. receiveInstantiationResult,
  1149. function(reason) {
  1150. // We expect the most common failure cause to be a bad MIME type for the binary,
  1151. // in which case falling back to ArrayBuffer instantiation should work.
  1152. err('wasm streaming compile failed: ' + reason);
  1153. err('falling back to ArrayBuffer instantiation');
  1154. return instantiateArrayBuffer(receiveInstantiationResult);
  1155. });
  1156. });
  1157. } else {
  1158. return instantiateArrayBuffer(receiveInstantiationResult);
  1159. }
  1160. }
  1161. // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
  1162. // to manually instantiate the Wasm module themselves. This allows pages to run the instantiation parallel
  1163. // to any other async startup actions they are performing.
  1164. if (Module['instantiateWasm']) {
  1165. try {
  1166. var exports = Module['instantiateWasm'](info, receiveInstance);
  1167. return exports;
  1168. } catch(e) {
  1169. err('Module.instantiateWasm callback failed with error: ' + e);
  1170. return false;
  1171. }
  1172. }
  1173. instantiateAsync();
  1174. return {}; // no exports yet; we'll fill them in later
  1175. }
  1176. // Globals used by JS i64 conversions (see makeSetValue)
  1177. var tempDouble;
  1178. var tempI64;
  1179. // === Body ===
  1180. var ASM_CONSTS = {
  1181. };
  1182. function callRuntimeCallbacks(callbacks) {
  1183. while (callbacks.length > 0) {
  1184. var callback = callbacks.shift();
  1185. if (typeof callback == 'function') {
  1186. callback(Module); // Pass the module as the first argument.
  1187. continue;
  1188. }
  1189. var func = callback.func;
  1190. if (typeof func === 'number') {
  1191. if (callback.arg === undefined) {
  1192. wasmTable.get(func)();
  1193. } else {
  1194. wasmTable.get(func)(callback.arg);
  1195. }
  1196. } else {
  1197. func(callback.arg === undefined ? null : callback.arg);
  1198. }
  1199. }
  1200. }
  1201. function demangle(func) {
  1202. return func;
  1203. }
  1204. function demangleAll(text) {
  1205. var regex =
  1206. /\b_Z[\w\d_]+/g;
  1207. return text.replace(regex,
  1208. function(x) {
  1209. var y = demangle(x);
  1210. return x === y ? x : (y + ' [' + x + ']');
  1211. });
  1212. }
  1213. function handleException(e) {
  1214. // Certain exception types we do not treat as errors since they are used for
  1215. // internal control flow.
  1216. // 1. ExitStatus, which is thrown by exit()
  1217. // 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others
  1218. // that wish to return to JS event loop.
  1219. if (e instanceof ExitStatus || e == 'unwind') {
  1220. return EXITSTATUS;
  1221. }
  1222. // Anything else is an unexpected exception and we treat it as hard error.
  1223. var toLog = e;
  1224. err('exception thrown: ' + toLog);
  1225. quit_(1, e);
  1226. }
  1227. function jsStackTrace() {
  1228. var error = new Error();
  1229. if (!error.stack) {
  1230. // IE10+ special cases: It does have callstack info, but it is only populated if an Error object is thrown,
  1231. // so try that as a special-case.
  1232. try {
  1233. throw new Error();
  1234. } catch(e) {
  1235. error = e;
  1236. }
  1237. if (!error.stack) {
  1238. return '(no stack trace available)';
  1239. }
  1240. }
  1241. return error.stack.toString();
  1242. }
  1243. function stackTrace() {
  1244. var js = jsStackTrace();
  1245. if (Module['extraStackTrace']) js += '\n' + Module['extraStackTrace']();
  1246. return demangleAll(js);
  1247. }
  1248. function ___assert_fail(condition, filename, line, func) {
  1249. abort('Assertion failed: ' + UTF8ToString(condition) + ', at: ' + [filename ? UTF8ToString(filename) : 'unknown filename', line, func ? UTF8ToString(func) : 'unknown function']);
  1250. }
  1251. function ___cxa_allocate_exception(size) {
  1252. // Thrown object is prepended by exception metadata block
  1253. return _malloc(size + 16) + 16;
  1254. }
  1255. function _atexit(func, arg) {
  1256. }
  1257. function ___cxa_atexit(a0,a1
  1258. ) {
  1259. return _atexit(a0,a1);
  1260. }
  1261. function ExceptionInfo(excPtr) {
  1262. this.excPtr = excPtr;
  1263. this.ptr = excPtr - 16;
  1264. this.set_type = function(type) {
  1265. HEAP32[(((this.ptr)+(4))>>2)] = type;
  1266. };
  1267. this.get_type = function() {
  1268. return HEAP32[(((this.ptr)+(4))>>2)];
  1269. };
  1270. this.set_destructor = function(destructor) {
  1271. HEAP32[(((this.ptr)+(8))>>2)] = destructor;
  1272. };
  1273. this.get_destructor = function() {
  1274. return HEAP32[(((this.ptr)+(8))>>2)];
  1275. };
  1276. this.set_refcount = function(refcount) {
  1277. HEAP32[((this.ptr)>>2)] = refcount;
  1278. };
  1279. this.set_caught = function (caught) {
  1280. caught = caught ? 1 : 0;
  1281. HEAP8[(((this.ptr)+(12))>>0)] = caught;
  1282. };
  1283. this.get_caught = function () {
  1284. return HEAP8[(((this.ptr)+(12))>>0)] != 0;
  1285. };
  1286. this.set_rethrown = function (rethrown) {
  1287. rethrown = rethrown ? 1 : 0;
  1288. HEAP8[(((this.ptr)+(13))>>0)] = rethrown;
  1289. };
  1290. this.get_rethrown = function () {
  1291. return HEAP8[(((this.ptr)+(13))>>0)] != 0;
  1292. };
  1293. // Initialize native structure fields. Should be called once after allocated.
  1294. this.init = function(type, destructor) {
  1295. this.set_type(type);
  1296. this.set_destructor(destructor);
  1297. this.set_refcount(0);
  1298. this.set_caught(false);
  1299. this.set_rethrown(false);
  1300. }
  1301. this.add_ref = function() {
  1302. var value = HEAP32[((this.ptr)>>2)];
  1303. HEAP32[((this.ptr)>>2)] = value + 1;
  1304. };
  1305. // Returns true if last reference released.
  1306. this.release_ref = function() {
  1307. var prev = HEAP32[((this.ptr)>>2)];
  1308. HEAP32[((this.ptr)>>2)] = prev - 1;
  1309. return prev === 1;
  1310. };
  1311. }
  1312. var exceptionLast = 0;
  1313. var uncaughtExceptionCount = 0;
  1314. function ___cxa_throw(ptr, type, destructor) {
  1315. var info = new ExceptionInfo(ptr);
  1316. // Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
  1317. info.init(type, destructor);
  1318. exceptionLast = ptr;
  1319. uncaughtExceptionCount++;
  1320. throw ptr;
  1321. }
  1322. function __embind_register_bigint(primitiveType, name, size, minRange, maxRange) {}
  1323. function getShiftFromSize(size) {
  1324. switch (size) {
  1325. case 1: return 0;
  1326. case 2: return 1;
  1327. case 4: return 2;
  1328. case 8: return 3;
  1329. default:
  1330. throw new TypeError('Unknown type size: ' + size);
  1331. }
  1332. }
  1333. function embind_init_charCodes() {
  1334. var codes = new Array(256);
  1335. for (var i = 0; i < 256; ++i) {
  1336. codes[i] = String.fromCharCode(i);
  1337. }
  1338. embind_charCodes = codes;
  1339. }
  1340. var embind_charCodes = undefined;
  1341. function readLatin1String(ptr) {
  1342. var ret = "";
  1343. var c = ptr;
  1344. while (HEAPU8[c]) {
  1345. ret += embind_charCodes[HEAPU8[c++]];
  1346. }
  1347. return ret;
  1348. }
  1349. var awaitingDependencies = {};
  1350. var registeredTypes = {};
  1351. var typeDependencies = {};
  1352. var char_0 = 48;
  1353. var char_9 = 57;
  1354. function makeLegalFunctionName(name) {
  1355. if (undefined === name) {
  1356. return '_unknown';
  1357. }
  1358. name = name.replace(/[^a-zA-Z0-9_]/g, '$');
  1359. var f = name.charCodeAt(0);
  1360. if (f >= char_0 && f <= char_9) {
  1361. return '_' + name;
  1362. } else {
  1363. return name;
  1364. }
  1365. }
  1366. function createNamedFunction(name, body) {
  1367. name = makeLegalFunctionName(name);
  1368. /*jshint evil:true*/
  1369. return new Function(
  1370. "body",
  1371. "return function " + name + "() {\n" +
  1372. " \"use strict\";" +
  1373. " return body.apply(this, arguments);\n" +
  1374. "};\n"
  1375. )(body);
  1376. }
  1377. function extendError(baseErrorType, errorName) {
  1378. var errorClass = createNamedFunction(errorName, function(message) {
  1379. this.name = errorName;
  1380. this.message = message;
  1381. var stack = (new Error(message)).stack;
  1382. if (stack !== undefined) {
  1383. this.stack = this.toString() + '\n' +
  1384. stack.replace(/^Error(:[^\n]*)?\n/, '');
  1385. }
  1386. });
  1387. errorClass.prototype = Object.create(baseErrorType.prototype);
  1388. errorClass.prototype.constructor = errorClass;
  1389. errorClass.prototype.toString = function() {
  1390. if (this.message === undefined) {
  1391. return this.name;
  1392. } else {
  1393. return this.name + ': ' + this.message;
  1394. }
  1395. };
  1396. return errorClass;
  1397. }
  1398. var BindingError = undefined;
  1399. function throwBindingError(message) {
  1400. throw new BindingError(message);
  1401. }
  1402. var InternalError = undefined;
  1403. function throwInternalError(message) {
  1404. throw new InternalError(message);
  1405. }
  1406. function whenDependentTypesAreResolved(myTypes, dependentTypes, getTypeConverters) {
  1407. myTypes.forEach(function(type) {
  1408. typeDependencies[type] = dependentTypes;
  1409. });
  1410. function onComplete(typeConverters) {
  1411. var myTypeConverters = getTypeConverters(typeConverters);
  1412. if (myTypeConverters.length !== myTypes.length) {
  1413. throwInternalError('Mismatched type converter count');
  1414. }
  1415. for (var i = 0; i < myTypes.length; ++i) {
  1416. registerType(myTypes[i], myTypeConverters[i]);
  1417. }
  1418. }
  1419. var typeConverters = new Array(dependentTypes.length);
  1420. var unregisteredTypes = [];
  1421. var registered = 0;
  1422. dependentTypes.forEach(function(dt, i) {
  1423. if (registeredTypes.hasOwnProperty(dt)) {
  1424. typeConverters[i] = registeredTypes[dt];
  1425. } else {
  1426. unregisteredTypes.push(dt);
  1427. if (!awaitingDependencies.hasOwnProperty(dt)) {
  1428. awaitingDependencies[dt] = [];
  1429. }
  1430. awaitingDependencies[dt].push(function() {
  1431. typeConverters[i] = registeredTypes[dt];
  1432. ++registered;
  1433. if (registered === unregisteredTypes.length) {
  1434. onComplete(typeConverters);
  1435. }
  1436. });
  1437. }
  1438. });
  1439. if (0 === unregisteredTypes.length) {
  1440. onComplete(typeConverters);
  1441. }
  1442. }
  1443. /** @param {Object=} options */
  1444. function registerType(rawType, registeredInstance, options) {
  1445. options = options || {};
  1446. if (!('argPackAdvance' in registeredInstance)) {
  1447. throw new TypeError('registerType registeredInstance requires argPackAdvance');
  1448. }
  1449. var name = registeredInstance.name;
  1450. if (!rawType) {
  1451. throwBindingError('type "' + name + '" must have a positive integer typeid pointer');
  1452. }
  1453. if (registeredTypes.hasOwnProperty(rawType)) {
  1454. if (options.ignoreDuplicateRegistrations) {
  1455. return;
  1456. } else {
  1457. throwBindingError("Cannot register type '" + name + "' twice");
  1458. }
  1459. }
  1460. registeredTypes[rawType] = registeredInstance;
  1461. delete typeDependencies[rawType];
  1462. if (awaitingDependencies.hasOwnProperty(rawType)) {
  1463. var callbacks = awaitingDependencies[rawType];
  1464. delete awaitingDependencies[rawType];
  1465. callbacks.forEach(function(cb) {
  1466. cb();
  1467. });
  1468. }
  1469. }
  1470. function __embind_register_bool(rawType, name, size, trueValue, falseValue) {
  1471. var shift = getShiftFromSize(size);
  1472. name = readLatin1String(name);
  1473. registerType(rawType, {
  1474. name: name,
  1475. 'fromWireType': function(wt) {
  1476. // ambiguous emscripten ABI: sometimes return values are
  1477. // true or false, and sometimes integers (0 or 1)
  1478. return !!wt;
  1479. },
  1480. 'toWireType': function(destructors, o) {
  1481. return o ? trueValue : falseValue;
  1482. },
  1483. 'argPackAdvance': 8,
  1484. 'readValueFromPointer': function(pointer) {
  1485. // TODO: if heap is fixed (like in asm.js) this could be executed outside
  1486. var heap;
  1487. if (size === 1) {
  1488. heap = HEAP8;
  1489. } else if (size === 2) {
  1490. heap = HEAP16;
  1491. } else if (size === 4) {
  1492. heap = HEAP32;
  1493. } else {
  1494. throw new TypeError("Unknown boolean type size: " + name);
  1495. }
  1496. return this['fromWireType'](heap[pointer >> shift]);
  1497. },
  1498. destructorFunction: null, // This type does not need a destructor
  1499. });
  1500. }
  1501. var emval_free_list = [];
  1502. var emval_handle_array = [{},{value:undefined},{value:null},{value:true},{value:false}];
  1503. function __emval_decref(handle) {
  1504. if (handle > 4 && 0 === --emval_handle_array[handle].refcount) {
  1505. emval_handle_array[handle] = undefined;
  1506. emval_free_list.push(handle);
  1507. }
  1508. }
  1509. function count_emval_handles() {
  1510. var count = 0;
  1511. for (var i = 5; i < emval_handle_array.length; ++i) {
  1512. if (emval_handle_array[i] !== undefined) {
  1513. ++count;
  1514. }
  1515. }
  1516. return count;
  1517. }
  1518. function get_first_emval() {
  1519. for (var i = 5; i < emval_handle_array.length; ++i) {
  1520. if (emval_handle_array[i] !== undefined) {
  1521. return emval_handle_array[i];
  1522. }
  1523. }
  1524. return null;
  1525. }
  1526. function init_emval() {
  1527. Module['count_emval_handles'] = count_emval_handles;
  1528. Module['get_first_emval'] = get_first_emval;
  1529. }
  1530. function __emval_register(value) {
  1531. switch (value) {
  1532. case undefined :{ return 1; }
  1533. case null :{ return 2; }
  1534. case true :{ return 3; }
  1535. case false :{ return 4; }
  1536. default:{
  1537. var handle = emval_free_list.length ?
  1538. emval_free_list.pop() :
  1539. emval_handle_array.length;
  1540. emval_handle_array[handle] = {refcount: 1, value: value};
  1541. return handle;
  1542. }
  1543. }
  1544. }
  1545. function simpleReadValueFromPointer(pointer) {
  1546. return this['fromWireType'](HEAPU32[pointer >> 2]);
  1547. }
  1548. function __embind_register_emval(rawType, name) {
  1549. name = readLatin1String(name);
  1550. registerType(rawType, {
  1551. name: name,
  1552. 'fromWireType': function(handle) {
  1553. var rv = emval_handle_array[handle].value;
  1554. __emval_decref(handle);
  1555. return rv;
  1556. },
  1557. 'toWireType': function(destructors, value) {
  1558. return __emval_register(value);
  1559. },
  1560. 'argPackAdvance': 8,
  1561. 'readValueFromPointer': simpleReadValueFromPointer,
  1562. destructorFunction: null, // This type does not need a destructor
  1563. // TODO: do we need a deleteObject here? write a test where
  1564. // emval is passed into JS via an interface
  1565. });
  1566. }
  1567. function _embind_repr(v) {
  1568. if (v === null) {
  1569. return 'null';
  1570. }
  1571. var t = typeof v;
  1572. if (t === 'object' || t === 'array' || t === 'function') {
  1573. return v.toString();
  1574. } else {
  1575. return '' + v;
  1576. }
  1577. }
  1578. function floatReadValueFromPointer(name, shift) {
  1579. switch (shift) {
  1580. case 2: return function(pointer) {
  1581. return this['fromWireType'](HEAPF32[pointer >> 2]);
  1582. };
  1583. case 3: return function(pointer) {
  1584. return this['fromWireType'](HEAPF64[pointer >> 3]);
  1585. };
  1586. default:
  1587. throw new TypeError("Unknown float type: " + name);
  1588. }
  1589. }
  1590. function __embind_register_float(rawType, name, size) {
  1591. var shift = getShiftFromSize(size);
  1592. name = readLatin1String(name);
  1593. registerType(rawType, {
  1594. name: name,
  1595. 'fromWireType': function(value) {
  1596. return value;
  1597. },
  1598. 'toWireType': function(destructors, value) {
  1599. // todo: Here we have an opportunity for -O3 level "unsafe" optimizations: we could
  1600. // avoid the following if() and assume value is of proper type.
  1601. if (typeof value !== "number" && typeof value !== "boolean") {
  1602. throw new TypeError('Cannot convert "' + _embind_repr(value) + '" to ' + this.name);
  1603. }
  1604. return value;
  1605. },
  1606. 'argPackAdvance': 8,
  1607. 'readValueFromPointer': floatReadValueFromPointer(name, shift),
  1608. destructorFunction: null, // This type does not need a destructor
  1609. });
  1610. }
  1611. function new_(constructor, argumentList) {
  1612. if (!(constructor instanceof Function)) {
  1613. throw new TypeError('new_ called with constructor type ' + typeof(constructor) + " which is not a function");
  1614. }
  1615. /*
  1616. * Previously, the following line was just:
  1617. function dummy() {};
  1618. * Unfortunately, Chrome was preserving 'dummy' as the object's name, even though at creation, the 'dummy' has the
  1619. * correct constructor name. Thus, objects created with IMVU.new would show up in the debugger as 'dummy', which
  1620. * isn't very helpful. Using IMVU.createNamedFunction addresses the issue. Doublely-unfortunately, there's no way
  1621. * to write a test for this behavior. -NRD 2013.02.22
  1622. */
  1623. var dummy = createNamedFunction(constructor.name || 'unknownFunctionName', function(){});
  1624. dummy.prototype = constructor.prototype;
  1625. var obj = new dummy;
  1626. var r = constructor.apply(obj, argumentList);
  1627. return (r instanceof Object) ? r : obj;
  1628. }
  1629. function runDestructors(destructors) {
  1630. while (destructors.length) {
  1631. var ptr = destructors.pop();
  1632. var del = destructors.pop();
  1633. del(ptr);
  1634. }
  1635. }
  1636. function craftInvokerFunction(humanName, argTypes, classType, cppInvokerFunc, cppTargetFunc) {
  1637. // humanName: a human-readable string name for the function to be generated.
  1638. // argTypes: An array that contains the embind type objects for all types in the function signature.
  1639. // argTypes[0] is the type object for the function return value.
  1640. // argTypes[1] is the type object for function this object/class type, or null if not crafting an invoker for a class method.
  1641. // argTypes[2...] are the actual function parameters.
  1642. // classType: The embind type object for the class to be bound, or null if this is not a method of a class.
  1643. // cppInvokerFunc: JS Function object to the C++-side function that interops into C++ code.
  1644. // cppTargetFunc: Function pointer (an integer to FUNCTION_TABLE) to the target C++ function the cppInvokerFunc will end up calling.
  1645. var argCount = argTypes.length;
  1646. if (argCount < 2) {
  1647. throwBindingError("argTypes array size mismatch! Must at least get return value and 'this' types!");
  1648. }
  1649. var isClassMethodFunc = (argTypes[1] !== null && classType !== null);
  1650. // Free functions with signature "void function()" do not need an invoker that marshalls between wire types.
  1651. // TODO: This omits argument count check - enable only at -O3 or similar.
  1652. // if (ENABLE_UNSAFE_OPTS && argCount == 2 && argTypes[0].name == "void" && !isClassMethodFunc) {
  1653. // return FUNCTION_TABLE[fn];
  1654. // }
  1655. // Determine if we need to use a dynamic stack to store the destructors for the function parameters.
  1656. // TODO: Remove this completely once all function invokers are being dynamically generated.
  1657. var needsDestructorStack = false;
  1658. for (var i = 1; i < argTypes.length; ++i) { // Skip return value at index 0 - it's not deleted here.
  1659. if (argTypes[i] !== null && argTypes[i].destructorFunction === undefined) { // The type does not define a destructor function - must use dynamic stack
  1660. needsDestructorStack = true;
  1661. break;
  1662. }
  1663. }
  1664. var returns = (argTypes[0].name !== "void");
  1665. var argsList = "";
  1666. var argsListWired = "";
  1667. for (var i = 0; i < argCount - 2; ++i) {
  1668. argsList += (i!==0?", ":"")+"arg"+i;
  1669. argsListWired += (i!==0?", ":"")+"arg"+i+"Wired";
  1670. }
  1671. var invokerFnBody =
  1672. "return function "+makeLegalFunctionName(humanName)+"("+argsList+") {\n" +
  1673. "if (arguments.length !== "+(argCount - 2)+") {\n" +
  1674. "throwBindingError('function "+humanName+" called with ' + arguments.length + ' arguments, expected "+(argCount - 2)+" args!');\n" +
  1675. "}\n";
  1676. if (needsDestructorStack) {
  1677. invokerFnBody +=
  1678. "var destructors = [];\n";
  1679. }
  1680. var dtorStack = needsDestructorStack ? "destructors" : "null";
  1681. var args1 = ["throwBindingError", "invoker", "fn", "runDestructors", "retType", "classParam"];
  1682. var args2 = [throwBindingError, cppInvokerFunc, cppTargetFunc, runDestructors, argTypes[0], argTypes[1]];
  1683. if (isClassMethodFunc) {
  1684. invokerFnBody += "var thisWired = classParam.toWireType("+dtorStack+", this);\n";
  1685. }
  1686. for (var i = 0; i < argCount - 2; ++i) {
  1687. invokerFnBody += "var arg"+i+"Wired = argType"+i+".toWireType("+dtorStack+", arg"+i+"); // "+argTypes[i+2].name+"\n";
  1688. args1.push("argType"+i);
  1689. args2.push(argTypes[i+2]);
  1690. }
  1691. if (isClassMethodFunc) {
  1692. argsListWired = "thisWired" + (argsListWired.length > 0 ? ", " : "") + argsListWired;
  1693. }
  1694. invokerFnBody +=
  1695. (returns?"var rv = ":"") + "invoker(fn"+(argsListWired.length>0?", ":"")+argsListWired+");\n";
  1696. if (needsDestructorStack) {
  1697. invokerFnBody += "runDestructors(destructors);\n";
  1698. } else {
  1699. for (var i = isClassMethodFunc?1:2; i < argTypes.length; ++i) { // Skip return value at index 0 - it's not deleted here. Also skip class type if not a method.
  1700. var paramName = (i === 1 ? "thisWired" : ("arg"+(i - 2)+"Wired"));
  1701. if (argTypes[i].destructorFunction !== null) {
  1702. invokerFnBody += paramName+"_dtor("+paramName+"); // "+argTypes[i].name+"\n";
  1703. args1.push(paramName+"_dtor");
  1704. args2.push(argTypes[i].destructorFunction);
  1705. }
  1706. }
  1707. }
  1708. if (returns) {
  1709. invokerFnBody += "var ret = retType.fromWireType(rv);\n" +
  1710. "return ret;\n";
  1711. } else {
  1712. }
  1713. invokerFnBody += "}\n";
  1714. args1.push(invokerFnBody);
  1715. var invokerFunction = new_(Function, args1).apply(null, args2);
  1716. return invokerFunction;
  1717. }
  1718. function ensureOverloadTable(proto, methodName, humanName) {
  1719. if (undefined === proto[methodName].overloadTable) {
  1720. var prevFunc = proto[methodName];
  1721. // Inject an overload resolver function that routes to the appropriate overload based on the number of arguments.
  1722. proto[methodName] = function() {
  1723. // TODO This check can be removed in -O3 level "unsafe" optimizations.
  1724. if (!proto[methodName].overloadTable.hasOwnProperty(arguments.length)) {
  1725. throwBindingError("Function '" + humanName + "' called with an invalid number of arguments (" + arguments.length + ") - expects one of (" + proto[methodName].overloadTable + ")!");
  1726. }
  1727. return proto[methodName].overloadTable[arguments.length].apply(this, arguments);
  1728. };
  1729. // Move the previous function into the overload table.
  1730. proto[methodName].overloadTable = [];
  1731. proto[methodName].overloadTable[prevFunc.argCount] = prevFunc;
  1732. }
  1733. }
  1734. /** @param {number=} numArguments */
  1735. function exposePublicSymbol(name, value, numArguments) {
  1736. if (Module.hasOwnProperty(name)) {
  1737. if (undefined === numArguments || (undefined !== Module[name].overloadTable && undefined !== Module[name].overloadTable[numArguments])) {
  1738. throwBindingError("Cannot register public name '" + name + "' twice");
  1739. }
  1740. // We are exposing a function with the same name as an existing function. Create an overload table and a function selector
  1741. // that routes between the two.
  1742. ensureOverloadTable(Module, name, name);
  1743. if (Module.hasOwnProperty(numArguments)) {
  1744. throwBindingError("Cannot register multiple overloads of a function with the same number of arguments (" + numArguments + ")!");
  1745. }
  1746. // Add the new function into the overload table.
  1747. Module[name].overloadTable[numArguments] = value;
  1748. }
  1749. else {
  1750. Module[name] = value;
  1751. if (undefined !== numArguments) {
  1752. Module[name].numArguments = numArguments;
  1753. }
  1754. }
  1755. }
  1756. function heap32VectorToArray(count, firstElement) {
  1757. var array = [];
  1758. for (var i = 0; i < count; i++) {
  1759. array.push(HEAP32[(firstElement >> 2) + i]);
  1760. }
  1761. return array;
  1762. }
  1763. /** @param {number=} numArguments */
  1764. function replacePublicSymbol(name, value, numArguments) {
  1765. if (!Module.hasOwnProperty(name)) {
  1766. throwInternalError('Replacing nonexistant public symbol');
  1767. }
  1768. // If there's an overload table for this symbol, replace the symbol in the overload table instead.
  1769. if (undefined !== Module[name].overloadTable && undefined !== numArguments) {
  1770. Module[name].overloadTable[numArguments] = value;
  1771. }
  1772. else {
  1773. Module[name] = value;
  1774. Module[name].argCount = numArguments;
  1775. }
  1776. }
  1777. function dynCallLegacy(sig, ptr, args) {
  1778. var f = Module["dynCall_" + sig];
  1779. return args && args.length ? f.apply(null, [ptr].concat(args)) : f.call(null, ptr);
  1780. }
  1781. function dynCall(sig, ptr, args) {
  1782. // Without WASM_BIGINT support we cannot directly call function with i64 as
  1783. // part of thier signature, so we rely the dynCall functions generated by
  1784. // wasm-emscripten-finalize
  1785. if (sig.includes('j')) {
  1786. return dynCallLegacy(sig, ptr, args);
  1787. }
  1788. return wasmTable.get(ptr).apply(null, args)
  1789. }
  1790. function getDynCaller(sig, ptr) {
  1791. var argCache = [];
  1792. return function() {
  1793. argCache.length = arguments.length;
  1794. for (var i = 0; i < arguments.length; i++) {
  1795. argCache[i] = arguments[i];
  1796. }
  1797. return dynCall(sig, ptr, argCache);
  1798. };
  1799. }
  1800. function embind__requireFunction(signature, rawFunction) {
  1801. signature = readLatin1String(signature);
  1802. function makeDynCaller() {
  1803. if (signature.includes('j')) {
  1804. return getDynCaller(signature, rawFunction);
  1805. }
  1806. return wasmTable.get(rawFunction);
  1807. }
  1808. var fp = makeDynCaller();
  1809. if (typeof fp !== "function") {
  1810. throwBindingError("unknown function pointer with signature " + signature + ": " + rawFunction);
  1811. }
  1812. return fp;
  1813. }
  1814. var UnboundTypeError = undefined;
  1815. function getTypeName(type) {
  1816. var ptr = ___getTypeName(type);
  1817. var rv = readLatin1String(ptr);
  1818. _free(ptr);
  1819. return rv;
  1820. }
  1821. function throwUnboundTypeError(message, types) {
  1822. var unboundTypes = [];
  1823. var seen = {};
  1824. function visit(type) {
  1825. if (seen[type]) {
  1826. return;
  1827. }
  1828. if (registeredTypes[type]) {
  1829. return;
  1830. }
  1831. if (typeDependencies[type]) {
  1832. typeDependencies[type].forEach(visit);
  1833. return;
  1834. }
  1835. unboundTypes.push(type);
  1836. seen[type] = true;
  1837. }
  1838. types.forEach(visit);
  1839. throw new UnboundTypeError(message + ': ' + unboundTypes.map(getTypeName).join([', ']));
  1840. }
  1841. function __embind_register_function(name, argCount, rawArgTypesAddr, signature, rawInvoker, fn) {
  1842. var argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
  1843. name = readLatin1String(name);
  1844. rawInvoker = embind__requireFunction(signature, rawInvoker);
  1845. exposePublicSymbol(name, function() {
  1846. throwUnboundTypeError('Cannot call ' + name + ' due to unbound types', argTypes);
  1847. }, argCount - 1);
  1848. whenDependentTypesAreResolved([], argTypes, function(argTypes) {
  1849. var invokerArgsArray = [argTypes[0] /* return value */, null /* no class 'this'*/].concat(argTypes.slice(1) /* actual params */);
  1850. replacePublicSymbol(name, craftInvokerFunction(name, invokerArgsArray, null /* no class 'this'*/, rawInvoker, fn), argCount - 1);
  1851. return [];
  1852. });
  1853. }
  1854. function integerReadValueFromPointer(name, shift, signed) {
  1855. // integers are quite common, so generate very specialized functions
  1856. switch (shift) {
  1857. case 0: return signed ?
  1858. function readS8FromPointer(pointer) { return HEAP8[pointer]; } :
  1859. function readU8FromPointer(pointer) { return HEAPU8[pointer]; };
  1860. case 1: return signed ?
  1861. function readS16FromPointer(pointer) { return HEAP16[pointer >> 1]; } :
  1862. function readU16FromPointer(pointer) { return HEAPU16[pointer >> 1]; };
  1863. case 2: return signed ?
  1864. function readS32FromPointer(pointer) { return HEAP32[pointer >> 2]; } :
  1865. function readU32FromPointer(pointer) { return HEAPU32[pointer >> 2]; };
  1866. default:
  1867. throw new TypeError("Unknown integer type: " + name);
  1868. }
  1869. }
  1870. function __embind_register_integer(primitiveType, name, size, minRange, maxRange) {
  1871. name = readLatin1String(name);
  1872. if (maxRange === -1) { // LLVM doesn't have signed and unsigned 32-bit types, so u32 literals come out as 'i32 -1'. Always treat those as max u32.
  1873. maxRange = 4294967295;
  1874. }
  1875. var shift = getShiftFromSize(size);
  1876. var fromWireType = function(value) {
  1877. return value;
  1878. };
  1879. if (minRange === 0) {
  1880. var bitshift = 32 - 8*size;
  1881. fromWireType = function(value) {
  1882. return (value << bitshift) >>> bitshift;
  1883. };
  1884. }
  1885. var isUnsignedType = (name.includes('unsigned'));
  1886. registerType(primitiveType, {
  1887. name: name,
  1888. 'fromWireType': fromWireType,
  1889. 'toWireType': function(destructors, value) {
  1890. // todo: Here we have an opportunity for -O3 level "unsafe" optimizations: we could
  1891. // avoid the following two if()s and assume value is of proper type.
  1892. if (typeof value !== "number" && typeof value !== "boolean") {
  1893. throw new TypeError('Cannot convert "' + _embind_repr(value) + '" to ' + this.name);
  1894. }
  1895. if (value < minRange || value > maxRange) {
  1896. throw new TypeError('Passing a number "' + _embind_repr(value) + '" from JS side to C/C++ side to an argument of type "' + name + '", which is outside the valid range [' + minRange + ', ' + maxRange + ']!');
  1897. }
  1898. return isUnsignedType ? (value >>> 0) : (value | 0);
  1899. },
  1900. 'argPackAdvance': 8,
  1901. 'readValueFromPointer': integerReadValueFromPointer(name, shift, minRange !== 0),
  1902. destructorFunction: null, // This type does not need a destructor
  1903. });
  1904. }
  1905. function __embind_register_memory_view(rawType, dataTypeIndex, name) {
  1906. var typeMapping = [
  1907. Int8Array,
  1908. Uint8Array,
  1909. Int16Array,
  1910. Uint16Array,
  1911. Int32Array,
  1912. Uint32Array,
  1913. Float32Array,
  1914. Float64Array,
  1915. ];
  1916. var TA = typeMapping[dataTypeIndex];
  1917. function decodeMemoryView(handle) {
  1918. handle = handle >> 2;
  1919. var heap = HEAPU32;
  1920. var size = heap[handle]; // in elements
  1921. var data = heap[handle + 1]; // byte offset into emscripten heap
  1922. return new TA(buffer, data, size);
  1923. }
  1924. name = readLatin1String(name);
  1925. registerType(rawType, {
  1926. name: name,
  1927. 'fromWireType': decodeMemoryView,
  1928. 'argPackAdvance': 8,
  1929. 'readValueFromPointer': decodeMemoryView,
  1930. }, {
  1931. ignoreDuplicateRegistrations: true,
  1932. });
  1933. }
  1934. function __embind_register_std_string(rawType, name) {
  1935. name = readLatin1String(name);
  1936. var stdStringIsUTF8
  1937. //process only std::string bindings with UTF8 support, in contrast to e.g. std::basic_string<unsigned char>
  1938. = (name === "std::string");
  1939. registerType(rawType, {
  1940. name: name,
  1941. 'fromWireType': function(value) {
  1942. var length = HEAPU32[value >> 2];
  1943. var str;
  1944. if (stdStringIsUTF8) {
  1945. var decodeStartPtr = value + 4;
  1946. // Looping here to support possible embedded '0' bytes
  1947. for (var i = 0; i <= length; ++i) {
  1948. var currentBytePtr = value + 4 + i;
  1949. if (i == length || HEAPU8[currentBytePtr] == 0) {
  1950. var maxRead = currentBytePtr - decodeStartPtr;
  1951. var stringSegment = UTF8ToString(decodeStartPtr, maxRead);
  1952. if (str === undefined) {
  1953. str = stringSegment;
  1954. } else {
  1955. str += String.fromCharCode(0);
  1956. str += stringSegment;
  1957. }
  1958. decodeStartPtr = currentBytePtr + 1;
  1959. }
  1960. }
  1961. } else {
  1962. var a = new Array(length);
  1963. for (var i = 0; i < length; ++i) {
  1964. a[i] = String.fromCharCode(HEAPU8[value + 4 + i]);
  1965. }
  1966. str = a.join('');
  1967. }
  1968. _free(value);
  1969. return str;
  1970. },
  1971. 'toWireType': function(destructors, value) {
  1972. if (value instanceof ArrayBuffer) {
  1973. value = new Uint8Array(value);
  1974. }
  1975. var getLength;
  1976. var valueIsOfTypeString = (typeof value === 'string');
  1977. if (!(valueIsOfTypeString || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int8Array)) {
  1978. throwBindingError('Cannot pass non-string to std::string');
  1979. }
  1980. if (stdStringIsUTF8 && valueIsOfTypeString) {
  1981. getLength = function() {return lengthBytesUTF8(value);};
  1982. } else {
  1983. getLength = function() {return value.length;};
  1984. }
  1985. // assumes 4-byte alignment
  1986. var length = getLength();
  1987. var ptr = _malloc(4 + length + 1);
  1988. HEAPU32[ptr >> 2] = length;
  1989. if (stdStringIsUTF8 && valueIsOfTypeString) {
  1990. stringToUTF8(value, ptr + 4, length + 1);
  1991. } else {
  1992. if (valueIsOfTypeString) {
  1993. for (var i = 0; i < length; ++i) {
  1994. var charCode = value.charCodeAt(i);
  1995. if (charCode > 255) {
  1996. _free(ptr);
  1997. throwBindingError('String has UTF-16 code units that do not fit in 8 bits');
  1998. }
  1999. HEAPU8[ptr + 4 + i] = charCode;
  2000. }
  2001. } else {
  2002. for (var i = 0; i < length; ++i) {
  2003. HEAPU8[ptr + 4 + i] = value[i];
  2004. }
  2005. }
  2006. }
  2007. if (destructors !== null) {
  2008. destructors.push(_free, ptr);
  2009. }
  2010. return ptr;
  2011. },
  2012. 'argPackAdvance': 8,
  2013. 'readValueFromPointer': simpleReadValueFromPointer,
  2014. destructorFunction: function(ptr) { _free(ptr); },
  2015. });
  2016. }
  2017. function __embind_register_std_wstring(rawType, charSize, name) {
  2018. name = readLatin1String(name);
  2019. var decodeString, encodeString, getHeap, lengthBytesUTF, shift;
  2020. if (charSize === 2) {
  2021. decodeString = UTF16ToString;
  2022. encodeString = stringToUTF16;
  2023. lengthBytesUTF = lengthBytesUTF16;
  2024. getHeap = function() { return HEAPU16; };
  2025. shift = 1;
  2026. } else if (charSize === 4) {
  2027. decodeString = UTF32ToString;
  2028. encodeString = stringToUTF32;
  2029. lengthBytesUTF = lengthBytesUTF32;
  2030. getHeap = function() { return HEAPU32; };
  2031. shift = 2;
  2032. }
  2033. registerType(rawType, {
  2034. name: name,
  2035. 'fromWireType': function(value) {
  2036. // Code mostly taken from _embind_register_std_string fromWireType
  2037. var length = HEAPU32[value >> 2];
  2038. var HEAP = getHeap();
  2039. var str;
  2040. var decodeStartPtr = value + 4;
  2041. // Looping here to support possible embedded '0' bytes
  2042. for (var i = 0; i <= length; ++i) {
  2043. var currentBytePtr = value + 4 + i * charSize;
  2044. if (i == length || HEAP[currentBytePtr >> shift] == 0) {
  2045. var maxReadBytes = currentBytePtr - decodeStartPtr;
  2046. var stringSegment = decodeString(decodeStartPtr, maxReadBytes);
  2047. if (str === undefined) {
  2048. str = stringSegment;
  2049. } else {
  2050. str += String.fromCharCode(0);
  2051. str += stringSegment;
  2052. }
  2053. decodeStartPtr = currentBytePtr + charSize;
  2054. }
  2055. }
  2056. _free(value);
  2057. return str;
  2058. },
  2059. 'toWireType': function(destructors, value) {
  2060. if (!(typeof value === 'string')) {
  2061. throwBindingError('Cannot pass non-string to C++ string type ' + name);
  2062. }
  2063. // assumes 4-byte alignment
  2064. var length = lengthBytesUTF(value);
  2065. var ptr = _malloc(4 + length + charSize);
  2066. HEAPU32[ptr >> 2] = length >> shift;
  2067. encodeString(value, ptr + 4, length + charSize);
  2068. if (destructors !== null) {
  2069. destructors.push(_free, ptr);
  2070. }
  2071. return ptr;
  2072. },
  2073. 'argPackAdvance': 8,
  2074. 'readValueFromPointer': simpleReadValueFromPointer,
  2075. destructorFunction: function(ptr) { _free(ptr); },
  2076. });
  2077. }
  2078. function __embind_register_void(rawType, name) {
  2079. name = readLatin1String(name);
  2080. registerType(rawType, {
  2081. isVoid: true, // void return values can be optimized out sometimes
  2082. name: name,
  2083. 'argPackAdvance': 0,
  2084. 'fromWireType': function() {
  2085. return undefined;
  2086. },
  2087. 'toWireType': function(destructors, o) {
  2088. // TODO: assert if anything else is given?
  2089. return undefined;
  2090. },
  2091. });
  2092. }
  2093. function _abort() {
  2094. abort();
  2095. }
  2096. function _emscripten_memcpy_big(dest, src, num) {
  2097. HEAPU8.copyWithin(dest, src, src + num);
  2098. }
  2099. function abortOnCannotGrowMemory(requestedSize) {
  2100. abort('OOM');
  2101. }
  2102. function _emscripten_resize_heap(requestedSize) {
  2103. var oldSize = HEAPU8.length;
  2104. requestedSize = requestedSize >>> 0;
  2105. abortOnCannotGrowMemory(requestedSize);
  2106. }
  2107. var PATH = {splitPath:function(filename) {
  2108. var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
  2109. return splitPathRe.exec(filename).slice(1);
  2110. },normalizeArray:function(parts, allowAboveRoot) {
  2111. // if the path tries to go above the root, `up` ends up > 0
  2112. var up = 0;
  2113. for (var i = parts.length - 1; i >= 0; i--) {
  2114. var last = parts[i];
  2115. if (last === '.') {
  2116. parts.splice(i, 1);
  2117. } else if (last === '..') {
  2118. parts.splice(i, 1);
  2119. up++;
  2120. } else if (up) {
  2121. parts.splice(i, 1);
  2122. up--;
  2123. }
  2124. }
  2125. // if the path is allowed to go above the root, restore leading ..s
  2126. if (allowAboveRoot) {
  2127. for (; up; up--) {
  2128. parts.unshift('..');
  2129. }
  2130. }
  2131. return parts;
  2132. },normalize:function(path) {
  2133. var isAbsolute = path.charAt(0) === '/',
  2134. trailingSlash = path.substr(-1) === '/';
  2135. // Normalize the path
  2136. path = PATH.normalizeArray(path.split('/').filter(function(p) {
  2137. return !!p;
  2138. }), !isAbsolute).join('/');
  2139. if (!path && !isAbsolute) {
  2140. path = '.';
  2141. }
  2142. if (path && trailingSlash) {
  2143. path += '/';
  2144. }
  2145. return (isAbsolute ? '/' : '') + path;
  2146. },dirname:function(path) {
  2147. var result = PATH.splitPath(path),
  2148. root = result[0],
  2149. dir = result[1];
  2150. if (!root && !dir) {
  2151. // No dirname whatsoever
  2152. return '.';
  2153. }
  2154. if (dir) {
  2155. // It has a dirname, strip trailing slash
  2156. dir = dir.substr(0, dir.length - 1);
  2157. }
  2158. return root + dir;
  2159. },basename:function(path) {
  2160. // EMSCRIPTEN return '/'' for '/', not an empty string
  2161. if (path === '/') return '/';
  2162. path = PATH.normalize(path);
  2163. path = path.replace(/\/$/, "");
  2164. var lastSlash = path.lastIndexOf('/');
  2165. if (lastSlash === -1) return path;
  2166. return path.substr(lastSlash+1);
  2167. },extname:function(path) {
  2168. return PATH.splitPath(path)[3];
  2169. },join:function() {
  2170. var paths = Array.prototype.slice.call(arguments, 0);
  2171. return PATH.normalize(paths.join('/'));
  2172. },join2:function(l, r) {
  2173. return PATH.normalize(l + '/' + r);
  2174. }};
  2175. function getRandomDevice() {
  2176. if (typeof crypto === 'object' && typeof crypto['getRandomValues'] === 'function') {
  2177. // for modern web browsers
  2178. var randomBuffer = new Uint8Array(1);
  2179. return function() { crypto.getRandomValues(randomBuffer); return randomBuffer[0]; };
  2180. } else
  2181. if (ENVIRONMENT_IS_NODE) {
  2182. // for nodejs with or without crypto support included
  2183. try {
  2184. var crypto_module = require('crypto');
  2185. // nodejs has crypto support
  2186. return function() { return crypto_module['randomBytes'](1)[0]; };
  2187. } catch (e) {
  2188. // nodejs doesn't have crypto support
  2189. }
  2190. }
  2191. // we couldn't find a proper implementation, as Math.random() is not suitable for /dev/random, see emscripten-core/emscripten/pull/7096
  2192. return function() { abort("randomDevice"); };
  2193. }
  2194. var PATH_FS = {resolve:function() {
  2195. var resolvedPath = '',
  2196. resolvedAbsolute = false;
  2197. for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  2198. var path = (i >= 0) ? arguments[i] : FS.cwd();
  2199. // Skip empty and invalid entries
  2200. if (typeof path !== 'string') {
  2201. throw new TypeError('Arguments to path.resolve must be strings');
  2202. } else if (!path) {
  2203. return ''; // an invalid portion invalidates the whole thing
  2204. }
  2205. resolvedPath = path + '/' + resolvedPath;
  2206. resolvedAbsolute = path.charAt(0) === '/';
  2207. }
  2208. // At this point the path should be resolved to a full absolute path, but
  2209. // handle relative paths to be safe (might happen when process.cwd() fails)
  2210. resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter(function(p) {
  2211. return !!p;
  2212. }), !resolvedAbsolute).join('/');
  2213. return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
  2214. },relative:function(from, to) {
  2215. from = PATH_FS.resolve(from).substr(1);
  2216. to = PATH_FS.resolve(to).substr(1);
  2217. function trim(arr) {
  2218. var start = 0;
  2219. for (; start < arr.length; start++) {
  2220. if (arr[start] !== '') break;
  2221. }
  2222. var end = arr.length - 1;
  2223. for (; end >= 0; end--) {
  2224. if (arr[end] !== '') break;
  2225. }
  2226. if (start > end) return [];
  2227. return arr.slice(start, end - start + 1);
  2228. }
  2229. var fromParts = trim(from.split('/'));
  2230. var toParts = trim(to.split('/'));
  2231. var length = Math.min(fromParts.length, toParts.length);
  2232. var samePartsLength = length;
  2233. for (var i = 0; i < length; i++) {
  2234. if (fromParts[i] !== toParts[i]) {
  2235. samePartsLength = i;
  2236. break;
  2237. }
  2238. }
  2239. var outputParts = [];
  2240. for (var i = samePartsLength; i < fromParts.length; i++) {
  2241. outputParts.push('..');
  2242. }
  2243. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  2244. return outputParts.join('/');
  2245. }};
  2246. var TTY = {ttys:[],init:function () {
  2247. // https://github.com/emscripten-core/emscripten/pull/1555
  2248. // if (ENVIRONMENT_IS_NODE) {
  2249. // // currently, FS.init does not distinguish if process.stdin is a file or TTY
  2250. // // device, it always assumes it's a TTY device. because of this, we're forcing
  2251. // // process.stdin to UTF8 encoding to at least make stdin reading compatible
  2252. // // with text files until FS.init can be refactored.
  2253. // process['stdin']['setEncoding']('utf8');
  2254. // }
  2255. },shutdown:function() {
  2256. // https://github.com/emscripten-core/emscripten/pull/1555
  2257. // if (ENVIRONMENT_IS_NODE) {
  2258. // // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)?
  2259. // // isaacs: because now it's reading from the stream, you've expressed interest in it, so that read() kicks off a _read() which creates a ReadReq operation
  2260. // // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists?
  2261. // // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle
  2262. // // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call
  2263. // process['stdin']['pause']();
  2264. // }
  2265. },register:function(dev, ops) {
  2266. TTY.ttys[dev] = { input: [], output: [], ops: ops };
  2267. FS.registerDevice(dev, TTY.stream_ops);
  2268. },stream_ops:{open:function(stream) {
  2269. var tty = TTY.ttys[stream.node.rdev];
  2270. if (!tty) {
  2271. throw new FS.ErrnoError(43);
  2272. }
  2273. stream.tty = tty;
  2274. stream.seekable = false;
  2275. },close:function(stream) {
  2276. // flush any pending line data
  2277. stream.tty.ops.flush(stream.tty);
  2278. },flush:function(stream) {
  2279. stream.tty.ops.flush(stream.tty);
  2280. },read:function(stream, buffer, offset, length, pos /* ignored */) {
  2281. if (!stream.tty || !stream.tty.ops.get_char) {
  2282. throw new FS.ErrnoError(60);
  2283. }
  2284. var bytesRead = 0;
  2285. for (var i = 0; i < length; i++) {
  2286. var result;
  2287. try {
  2288. result = stream.tty.ops.get_char(stream.tty);
  2289. } catch (e) {
  2290. throw new FS.ErrnoError(29);
  2291. }
  2292. if (result === undefined && bytesRead === 0) {
  2293. throw new FS.ErrnoError(6);
  2294. }
  2295. if (result === null || result === undefined) break;
  2296. bytesRead++;
  2297. buffer[offset+i] = result;
  2298. }
  2299. if (bytesRead) {
  2300. stream.node.timestamp = Date.now();
  2301. }
  2302. return bytesRead;
  2303. },write:function(stream, buffer, offset, length, pos) {
  2304. if (!stream.tty || !stream.tty.ops.put_char) {
  2305. throw new FS.ErrnoError(60);
  2306. }
  2307. try {
  2308. for (var i = 0; i < length; i++) {
  2309. stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
  2310. }
  2311. } catch (e) {
  2312. throw new FS.ErrnoError(29);
  2313. }
  2314. if (length) {
  2315. stream.node.timestamp = Date.now();
  2316. }
  2317. return i;
  2318. }},default_tty_ops:{get_char:function(tty) {
  2319. if (!tty.input.length) {
  2320. var result = null;
  2321. if (ENVIRONMENT_IS_NODE) {
  2322. // we will read data by chunks of BUFSIZE
  2323. var BUFSIZE = 256;
  2324. var buf = Buffer.alloc(BUFSIZE);
  2325. var bytesRead = 0;
  2326. try {
  2327. bytesRead = nodeFS.readSync(process.stdin.fd, buf, 0, BUFSIZE, null);
  2328. } catch(e) {
  2329. // Cross-platform differences: on Windows, reading EOF throws an exception, but on other OSes,
  2330. // reading EOF returns 0. Uniformize behavior by treating the EOF exception to return 0.
  2331. if (e.toString().includes('EOF')) bytesRead = 0;
  2332. else throw e;
  2333. }
  2334. if (bytesRead > 0) {
  2335. result = buf.slice(0, bytesRead).toString('utf-8');
  2336. } else {
  2337. result = null;
  2338. }
  2339. } else
  2340. if (typeof window != 'undefined' &&
  2341. typeof window.prompt == 'function') {
  2342. // Browser.
  2343. result = window.prompt('Input: '); // returns null on cancel
  2344. if (result !== null) {
  2345. result += '\n';
  2346. }
  2347. } else if (typeof readline == 'function') {
  2348. // Command line.
  2349. result = readline();
  2350. if (result !== null) {
  2351. result += '\n';
  2352. }
  2353. }
  2354. if (!result) {
  2355. return null;
  2356. }
  2357. tty.input = intArrayFromString(result, true);
  2358. }
  2359. return tty.input.shift();
  2360. },put_char:function(tty, val) {
  2361. if (val === null || val === 10) {
  2362. out(UTF8ArrayToString(tty.output, 0));
  2363. tty.output = [];
  2364. } else {
  2365. if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
  2366. }
  2367. },flush:function(tty) {
  2368. if (tty.output && tty.output.length > 0) {
  2369. out(UTF8ArrayToString(tty.output, 0));
  2370. tty.output = [];
  2371. }
  2372. }},default_tty1_ops:{put_char:function(tty, val) {
  2373. if (val === null || val === 10) {
  2374. err(UTF8ArrayToString(tty.output, 0));
  2375. tty.output = [];
  2376. } else {
  2377. if (val != 0) tty.output.push(val);
  2378. }
  2379. },flush:function(tty) {
  2380. if (tty.output && tty.output.length > 0) {
  2381. err(UTF8ArrayToString(tty.output, 0));
  2382. tty.output = [];
  2383. }
  2384. }}};
  2385. function zeroMemory(address, size) {
  2386. HEAPU8.fill(0, address, address + size);
  2387. }
  2388. function alignMemory(size, alignment) {
  2389. return Math.ceil(size / alignment) * alignment;
  2390. }
  2391. function mmapAlloc(size) {
  2392. abort();
  2393. }
  2394. var MEMFS = {ops_table:null,mount:function(mount) {
  2395. return MEMFS.createNode(null, '/', 16384 | 511 /* 0777 */, 0);
  2396. },createNode:function(parent, name, mode, dev) {
  2397. if (FS.isBlkdev(mode) || FS.isFIFO(mode)) {
  2398. // no supported
  2399. throw new FS.ErrnoError(63);
  2400. }
  2401. if (!MEMFS.ops_table) {
  2402. MEMFS.ops_table = {
  2403. dir: {
  2404. node: {
  2405. getattr: MEMFS.node_ops.getattr,
  2406. setattr: MEMFS.node_ops.setattr,
  2407. lookup: MEMFS.node_ops.lookup,
  2408. mknod: MEMFS.node_ops.mknod,
  2409. rename: MEMFS.node_ops.rename,
  2410. unlink: MEMFS.node_ops.unlink,
  2411. rmdir: MEMFS.node_ops.rmdir,
  2412. readdir: MEMFS.node_ops.readdir,
  2413. symlink: MEMFS.node_ops.symlink
  2414. },
  2415. stream: {
  2416. llseek: MEMFS.stream_ops.llseek
  2417. }
  2418. },
  2419. file: {
  2420. node: {
  2421. getattr: MEMFS.node_ops.getattr,
  2422. setattr: MEMFS.node_ops.setattr
  2423. },
  2424. stream: {
  2425. llseek: MEMFS.stream_ops.llseek,
  2426. read: MEMFS.stream_ops.read,
  2427. write: MEMFS.stream_ops.write,
  2428. allocate: MEMFS.stream_ops.allocate,
  2429. mmap: MEMFS.stream_ops.mmap,
  2430. msync: MEMFS.stream_ops.msync
  2431. }
  2432. },
  2433. link: {
  2434. node: {
  2435. getattr: MEMFS.node_ops.getattr,
  2436. setattr: MEMFS.node_ops.setattr,
  2437. readlink: MEMFS.node_ops.readlink
  2438. },
  2439. stream: {}
  2440. },
  2441. chrdev: {
  2442. node: {
  2443. getattr: MEMFS.node_ops.getattr,
  2444. setattr: MEMFS.node_ops.setattr
  2445. },
  2446. stream: FS.chrdev_stream_ops
  2447. }
  2448. };
  2449. }
  2450. var node = FS.createNode(parent, name, mode, dev);
  2451. if (FS.isDir(node.mode)) {
  2452. node.node_ops = MEMFS.ops_table.dir.node;
  2453. node.stream_ops = MEMFS.ops_table.dir.stream;
  2454. node.contents = {};
  2455. } else if (FS.isFile(node.mode)) {
  2456. node.node_ops = MEMFS.ops_table.file.node;
  2457. node.stream_ops = MEMFS.ops_table.file.stream;
  2458. node.usedBytes = 0; // The actual number of bytes used in the typed array, as opposed to contents.length which gives the whole capacity.
  2459. // When the byte data of the file is populated, this will point to either a typed array, or a normal JS array. Typed arrays are preferred
  2460. // for performance, and used by default. However, typed arrays are not resizable like normal JS arrays are, so there is a small disk size
  2461. // penalty involved for appending file writes that continuously grow a file similar to std::vector capacity vs used -scheme.
  2462. node.contents = null;
  2463. } else if (FS.isLink(node.mode)) {
  2464. node.node_ops = MEMFS.ops_table.link.node;
  2465. node.stream_ops = MEMFS.ops_table.link.stream;
  2466. } else if (FS.isChrdev(node.mode)) {
  2467. node.node_ops = MEMFS.ops_table.chrdev.node;
  2468. node.stream_ops = MEMFS.ops_table.chrdev.stream;
  2469. }
  2470. node.timestamp = Date.now();
  2471. // add the new node to the parent
  2472. if (parent) {
  2473. parent.contents[name] = node;
  2474. parent.timestamp = node.timestamp;
  2475. }
  2476. return node;
  2477. },getFileDataAsTypedArray:function(node) {
  2478. if (!node.contents) return new Uint8Array(0);
  2479. if (node.contents.subarray) return node.contents.subarray(0, node.usedBytes); // Make sure to not return excess unused bytes.
  2480. return new Uint8Array(node.contents);
  2481. },expandFileStorage:function(node, newCapacity) {
  2482. var prevCapacity = node.contents ? node.contents.length : 0;
  2483. if (prevCapacity >= newCapacity) return; // No need to expand, the storage was already large enough.
  2484. // Don't expand strictly to the given requested limit if it's only a very small increase, but instead geometrically grow capacity.
  2485. // For small filesizes (<1MB), perform size*2 geometric increase, but for large sizes, do a much more conservative size*1.125 increase to
  2486. // avoid overshooting the allocation cap by a very large margin.
  2487. var CAPACITY_DOUBLING_MAX = 1024 * 1024;
  2488. newCapacity = Math.max(newCapacity, (prevCapacity * (prevCapacity < CAPACITY_DOUBLING_MAX ? 2.0 : 1.125)) >>> 0);
  2489. if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); // At minimum allocate 256b for each file when expanding.
  2490. var oldContents = node.contents;
  2491. node.contents = new Uint8Array(newCapacity); // Allocate new storage.
  2492. if (node.usedBytes > 0) node.contents.set(oldContents.subarray(0, node.usedBytes), 0); // Copy old data over to the new storage.
  2493. },resizeFileStorage:function(node, newSize) {
  2494. if (node.usedBytes == newSize) return;
  2495. if (newSize == 0) {
  2496. node.contents = null; // Fully decommit when requesting a resize to zero.
  2497. node.usedBytes = 0;
  2498. } else {
  2499. var oldContents = node.contents;
  2500. node.contents = new Uint8Array(newSize); // Allocate new storage.
  2501. if (oldContents) {
  2502. node.contents.set(oldContents.subarray(0, Math.min(newSize, node.usedBytes))); // Copy old data over to the new storage.
  2503. }
  2504. node.usedBytes = newSize;
  2505. }
  2506. },node_ops:{getattr:function(node) {
  2507. var attr = {};
  2508. // device numbers reuse inode numbers.
  2509. attr.dev = FS.isChrdev(node.mode) ? node.id : 1;
  2510. attr.ino = node.id;
  2511. attr.mode = node.mode;
  2512. attr.nlink = 1;
  2513. attr.uid = 0;
  2514. attr.gid = 0;
  2515. attr.rdev = node.rdev;
  2516. if (FS.isDir(node.mode)) {
  2517. attr.size = 4096;
  2518. } else if (FS.isFile(node.mode)) {
  2519. attr.size = node.usedBytes;
  2520. } else if (FS.isLink(node.mode)) {
  2521. attr.size = node.link.length;
  2522. } else {
  2523. attr.size = 0;
  2524. }
  2525. attr.atime = new Date(node.timestamp);
  2526. attr.mtime = new Date(node.timestamp);
  2527. attr.ctime = new Date(node.timestamp);
  2528. // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize),
  2529. // but this is not required by the standard.
  2530. attr.blksize = 4096;
  2531. attr.blocks = Math.ceil(attr.size / attr.blksize);
  2532. return attr;
  2533. },setattr:function(node, attr) {
  2534. if (attr.mode !== undefined) {
  2535. node.mode = attr.mode;
  2536. }
  2537. if (attr.timestamp !== undefined) {
  2538. node.timestamp = attr.timestamp;
  2539. }
  2540. if (attr.size !== undefined) {
  2541. MEMFS.resizeFileStorage(node, attr.size);
  2542. }
  2543. },lookup:function(parent, name) {
  2544. throw FS.genericErrors[44];
  2545. },mknod:function(parent, name, mode, dev) {
  2546. return MEMFS.createNode(parent, name, mode, dev);
  2547. },rename:function(old_node, new_dir, new_name) {
  2548. // if we're overwriting a directory at new_name, make sure it's empty.
  2549. if (FS.isDir(old_node.mode)) {
  2550. var new_node;
  2551. try {
  2552. new_node = FS.lookupNode(new_dir, new_name);
  2553. } catch (e) {
  2554. }
  2555. if (new_node) {
  2556. for (var i in new_node.contents) {
  2557. throw new FS.ErrnoError(55);
  2558. }
  2559. }
  2560. }
  2561. // do the internal rewiring
  2562. delete old_node.parent.contents[old_node.name];
  2563. old_node.parent.timestamp = Date.now()
  2564. old_node.name = new_name;
  2565. new_dir.contents[new_name] = old_node;
  2566. new_dir.timestamp = old_node.parent.timestamp;
  2567. old_node.parent = new_dir;
  2568. },unlink:function(parent, name) {
  2569. delete parent.contents[name];
  2570. parent.timestamp = Date.now();
  2571. },rmdir:function(parent, name) {
  2572. var node = FS.lookupNode(parent, name);
  2573. for (var i in node.contents) {
  2574. throw new FS.ErrnoError(55);
  2575. }
  2576. delete parent.contents[name];
  2577. parent.timestamp = Date.now();
  2578. },readdir:function(node) {
  2579. var entries = ['.', '..'];
  2580. for (var key in node.contents) {
  2581. if (!node.contents.hasOwnProperty(key)) {
  2582. continue;
  2583. }
  2584. entries.push(key);
  2585. }
  2586. return entries;
  2587. },symlink:function(parent, newname, oldpath) {
  2588. var node = MEMFS.createNode(parent, newname, 511 /* 0777 */ | 40960, 0);
  2589. node.link = oldpath;
  2590. return node;
  2591. },readlink:function(node) {
  2592. if (!FS.isLink(node.mode)) {
  2593. throw new FS.ErrnoError(28);
  2594. }
  2595. return node.link;
  2596. }},stream_ops:{read:function(stream, buffer, offset, length, position) {
  2597. var contents = stream.node.contents;
  2598. if (position >= stream.node.usedBytes) return 0;
  2599. var size = Math.min(stream.node.usedBytes - position, length);
  2600. if (size > 8 && contents.subarray) { // non-trivial, and typed array
  2601. buffer.set(contents.subarray(position, position + size), offset);
  2602. } else {
  2603. for (var i = 0; i < size; i++) buffer[offset + i] = contents[position + i];
  2604. }
  2605. return size;
  2606. },write:function(stream, buffer, offset, length, position, canOwn) {
  2607. if (!length) return 0;
  2608. var node = stream.node;
  2609. node.timestamp = Date.now();
  2610. if (buffer.subarray && (!node.contents || node.contents.subarray)) { // This write is from a typed array to a typed array?
  2611. if (canOwn) {
  2612. node.contents = buffer.subarray(offset, offset + length);
  2613. node.usedBytes = length;
  2614. return length;
  2615. } else if (node.usedBytes === 0 && position === 0) { // If this is a simple first write to an empty file, do a fast set since we don't need to care about old data.
  2616. node.contents = buffer.slice(offset, offset + length);
  2617. node.usedBytes = length;
  2618. return length;
  2619. } else if (position + length <= node.usedBytes) { // Writing to an already allocated and used subrange of the file?
  2620. node.contents.set(buffer.subarray(offset, offset + length), position);
  2621. return length;
  2622. }
  2623. }
  2624. // Appending to an existing file and we need to reallocate, or source data did not come as a typed array.
  2625. MEMFS.expandFileStorage(node, position+length);
  2626. if (node.contents.subarray && buffer.subarray) {
  2627. // Use typed array write which is available.
  2628. node.contents.set(buffer.subarray(offset, offset + length), position);
  2629. } else {
  2630. for (var i = 0; i < length; i++) {
  2631. node.contents[position + i] = buffer[offset + i]; // Or fall back to manual write if not.
  2632. }
  2633. }
  2634. node.usedBytes = Math.max(node.usedBytes, position + length);
  2635. return length;
  2636. },llseek:function(stream, offset, whence) {
  2637. var position = offset;
  2638. if (whence === 1) {
  2639. position += stream.position;
  2640. } else if (whence === 2) {
  2641. if (FS.isFile(stream.node.mode)) {
  2642. position += stream.node.usedBytes;
  2643. }
  2644. }
  2645. if (position < 0) {
  2646. throw new FS.ErrnoError(28);
  2647. }
  2648. return position;
  2649. },allocate:function(stream, offset, length) {
  2650. MEMFS.expandFileStorage(stream.node, offset + length);
  2651. stream.node.usedBytes = Math.max(stream.node.usedBytes, offset + length);
  2652. },mmap:function(stream, address, length, position, prot, flags) {
  2653. if (address !== 0) {
  2654. // We don't currently support location hints for the address of the mapping
  2655. throw new FS.ErrnoError(28);
  2656. }
  2657. if (!FS.isFile(stream.node.mode)) {
  2658. throw new FS.ErrnoError(43);
  2659. }
  2660. var ptr;
  2661. var allocated;
  2662. var contents = stream.node.contents;
  2663. // Only make a new copy when MAP_PRIVATE is specified.
  2664. if (!(flags & 2) && contents.buffer === buffer) {
  2665. // We can't emulate MAP_SHARED when the file is not backed by the buffer
  2666. // we're mapping to (e.g. the HEAP buffer).
  2667. allocated = false;
  2668. ptr = contents.byteOffset;
  2669. } else {
  2670. // Try to avoid unnecessary slices.
  2671. if (position > 0 || position + length < contents.length) {
  2672. if (contents.subarray) {
  2673. contents = contents.subarray(position, position + length);
  2674. } else {
  2675. contents = Array.prototype.slice.call(contents, position, position + length);
  2676. }
  2677. }
  2678. allocated = true;
  2679. ptr = mmapAlloc(length);
  2680. if (!ptr) {
  2681. throw new FS.ErrnoError(48);
  2682. }
  2683. HEAP8.set(contents, ptr);
  2684. }
  2685. return { ptr: ptr, allocated: allocated };
  2686. },msync:function(stream, buffer, offset, length, mmapFlags) {
  2687. if (!FS.isFile(stream.node.mode)) {
  2688. throw new FS.ErrnoError(43);
  2689. }
  2690. if (mmapFlags & 2) {
  2691. // MAP_PRIVATE calls need not to be synced back to underlying fs
  2692. return 0;
  2693. }
  2694. var bytesWritten = MEMFS.stream_ops.write(stream, buffer, 0, length, offset, false);
  2695. // should we check if bytesWritten and length are the same?
  2696. return 0;
  2697. }}};
  2698. function asyncLoad(url, onload, onerror, noRunDep) {
  2699. var dep = !noRunDep ? getUniqueRunDependency('al ' + url) : '';
  2700. readAsync(url, function(arrayBuffer) {
  2701. assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).');
  2702. onload(new Uint8Array(arrayBuffer));
  2703. if (dep) removeRunDependency(dep);
  2704. }, function(event) {
  2705. if (onerror) {
  2706. onerror();
  2707. } else {
  2708. throw 'Loading data file "' + url + '" failed.';
  2709. }
  2710. });
  2711. if (dep) addRunDependency(dep);
  2712. }
  2713. var IDBFS = {dbs:{},indexedDB:function() {
  2714. if (typeof indexedDB !== 'undefined') return indexedDB;
  2715. var ret = null;
  2716. if (typeof window === 'object') ret = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
  2717. assert(ret, 'IDBFS used, but indexedDB not supported');
  2718. return ret;
  2719. },DB_VERSION:21,DB_STORE_NAME:"FILE_DATA",mount:function(mount) {
  2720. // reuse all of the core MEMFS functionality
  2721. return MEMFS.mount.apply(null, arguments);
  2722. },syncfs:function(mount, populate, callback) {
  2723. IDBFS.getLocalSet(mount, function(err, local) {
  2724. if (err) return callback(err);
  2725. IDBFS.getRemoteSet(mount, function(err, remote) {
  2726. if (err) return callback(err);
  2727. var src = populate ? remote : local;
  2728. var dst = populate ? local : remote;
  2729. IDBFS.reconcile(src, dst, callback);
  2730. });
  2731. });
  2732. },getDB:function(name, callback) {
  2733. // check the cache first
  2734. var db = IDBFS.dbs[name];
  2735. if (db) {
  2736. return callback(null, db);
  2737. }
  2738. var req;
  2739. try {
  2740. req = IDBFS.indexedDB().open(name, IDBFS.DB_VERSION);
  2741. } catch (e) {
  2742. return callback(e);
  2743. }
  2744. if (!req) {
  2745. return callback("Unable to connect to IndexedDB");
  2746. }
  2747. req.onupgradeneeded = function(e) {
  2748. var db = e.target.result;
  2749. var transaction = e.target.transaction;
  2750. var fileStore;
  2751. if (db.objectStoreNames.contains(IDBFS.DB_STORE_NAME)) {
  2752. fileStore = transaction.objectStore(IDBFS.DB_STORE_NAME);
  2753. } else {
  2754. fileStore = db.createObjectStore(IDBFS.DB_STORE_NAME);
  2755. }
  2756. if (!fileStore.indexNames.contains('timestamp')) {
  2757. fileStore.createIndex('timestamp', 'timestamp', { unique: false });
  2758. }
  2759. };
  2760. req.onsuccess = function() {
  2761. db = req.result;
  2762. // add to the cache
  2763. IDBFS.dbs[name] = db;
  2764. callback(null, db);
  2765. };
  2766. req.onerror = function(e) {
  2767. callback(this.error);
  2768. e.preventDefault();
  2769. };
  2770. },getLocalSet:function(mount, callback) {
  2771. var entries = {};
  2772. function isRealDir(p) {
  2773. return p !== '.' && p !== '..';
  2774. };
  2775. function toAbsolute(root) {
  2776. return function(p) {
  2777. return PATH.join2(root, p);
  2778. }
  2779. };
  2780. var check = FS.readdir(mount.mountpoint).filter(isRealDir).map(toAbsolute(mount.mountpoint));
  2781. while (check.length) {
  2782. var path = check.pop();
  2783. var stat;
  2784. try {
  2785. stat = FS.stat(path);
  2786. } catch (e) {
  2787. return callback(e);
  2788. }
  2789. if (FS.isDir(stat.mode)) {
  2790. check.push.apply(check, FS.readdir(path).filter(isRealDir).map(toAbsolute(path)));
  2791. }
  2792. entries[path] = { 'timestamp': stat.mtime };
  2793. }
  2794. return callback(null, { type: 'local', entries: entries });
  2795. },getRemoteSet:function(mount, callback) {
  2796. var entries = {};
  2797. IDBFS.getDB(mount.mountpoint, function(err, db) {
  2798. if (err) return callback(err);
  2799. try {
  2800. var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readonly');
  2801. transaction.onerror = function(e) {
  2802. callback(this.error);
  2803. e.preventDefault();
  2804. };
  2805. var store = transaction.objectStore(IDBFS.DB_STORE_NAME);
  2806. var index = store.index('timestamp');
  2807. index.openKeyCursor().onsuccess = function(event) {
  2808. var cursor = event.target.result;
  2809. if (!cursor) {
  2810. return callback(null, { type: 'remote', db: db, entries: entries });
  2811. }
  2812. entries[cursor.primaryKey] = { 'timestamp': cursor.key };
  2813. cursor.continue();
  2814. };
  2815. } catch (e) {
  2816. return callback(e);
  2817. }
  2818. });
  2819. },loadLocalEntry:function(path, callback) {
  2820. var stat, node;
  2821. try {
  2822. var lookup = FS.lookupPath(path);
  2823. node = lookup.node;
  2824. stat = FS.stat(path);
  2825. } catch (e) {
  2826. return callback(e);
  2827. }
  2828. if (FS.isDir(stat.mode)) {
  2829. return callback(null, { 'timestamp': stat.mtime, 'mode': stat.mode });
  2830. } else if (FS.isFile(stat.mode)) {
  2831. // Performance consideration: storing a normal JavaScript array to a IndexedDB is much slower than storing a typed array.
  2832. // Therefore always convert the file contents to a typed array first before writing the data to IndexedDB.
  2833. node.contents = MEMFS.getFileDataAsTypedArray(node);
  2834. return callback(null, { 'timestamp': stat.mtime, 'mode': stat.mode, 'contents': node.contents });
  2835. } else {
  2836. return callback(new Error('node type not supported'));
  2837. }
  2838. },storeLocalEntry:function(path, entry, callback) {
  2839. try {
  2840. if (FS.isDir(entry['mode'])) {
  2841. FS.mkdirTree(path, entry['mode']);
  2842. } else if (FS.isFile(entry['mode'])) {
  2843. FS.writeFile(path, entry['contents'], { canOwn: true });
  2844. } else {
  2845. return callback(new Error('node type not supported'));
  2846. }
  2847. FS.chmod(path, entry['mode']);
  2848. FS.utime(path, entry['timestamp'], entry['timestamp']);
  2849. } catch (e) {
  2850. return callback(e);
  2851. }
  2852. callback(null);
  2853. },removeLocalEntry:function(path, callback) {
  2854. try {
  2855. var lookup = FS.lookupPath(path);
  2856. var stat = FS.stat(path);
  2857. if (FS.isDir(stat.mode)) {
  2858. FS.rmdir(path);
  2859. } else if (FS.isFile(stat.mode)) {
  2860. FS.unlink(path);
  2861. }
  2862. } catch (e) {
  2863. return callback(e);
  2864. }
  2865. callback(null);
  2866. },loadRemoteEntry:function(store, path, callback) {
  2867. var req = store.get(path);
  2868. req.onsuccess = function(event) { callback(null, event.target.result); };
  2869. req.onerror = function(e) {
  2870. callback(this.error);
  2871. e.preventDefault();
  2872. };
  2873. },storeRemoteEntry:function(store, path, entry, callback) {
  2874. try {
  2875. var req = store.put(entry, path);
  2876. } catch (e) {
  2877. callback(e);
  2878. return;
  2879. }
  2880. req.onsuccess = function() { callback(null); };
  2881. req.onerror = function(e) {
  2882. callback(this.error);
  2883. e.preventDefault();
  2884. };
  2885. },removeRemoteEntry:function(store, path, callback) {
  2886. var req = store.delete(path);
  2887. req.onsuccess = function() { callback(null); };
  2888. req.onerror = function(e) {
  2889. callback(this.error);
  2890. e.preventDefault();
  2891. };
  2892. },reconcile:function(src, dst, callback) {
  2893. var total = 0;
  2894. var create = [];
  2895. Object.keys(src.entries).forEach(function (key) {
  2896. var e = src.entries[key];
  2897. var e2 = dst.entries[key];
  2898. if (!e2 || e['timestamp'].getTime() != e2['timestamp'].getTime()) {
  2899. create.push(key);
  2900. total++;
  2901. }
  2902. });
  2903. var remove = [];
  2904. Object.keys(dst.entries).forEach(function (key) {
  2905. if (!src.entries[key]) {
  2906. remove.push(key);
  2907. total++;
  2908. }
  2909. });
  2910. if (!total) {
  2911. return callback(null);
  2912. }
  2913. var errored = false;
  2914. var db = src.type === 'remote' ? src.db : dst.db;
  2915. var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite');
  2916. var store = transaction.objectStore(IDBFS.DB_STORE_NAME);
  2917. function done(err) {
  2918. if (err && !errored) {
  2919. errored = true;
  2920. return callback(err);
  2921. }
  2922. };
  2923. transaction.onerror = function(e) {
  2924. done(this.error);
  2925. e.preventDefault();
  2926. };
  2927. transaction.oncomplete = function(e) {
  2928. if (!errored) {
  2929. callback(null);
  2930. }
  2931. };
  2932. // sort paths in ascending order so directory entries are created
  2933. // before the files inside them
  2934. create.sort().forEach(function (path) {
  2935. if (dst.type === 'local') {
  2936. IDBFS.loadRemoteEntry(store, path, function (err, entry) {
  2937. if (err) return done(err);
  2938. IDBFS.storeLocalEntry(path, entry, done);
  2939. });
  2940. } else {
  2941. IDBFS.loadLocalEntry(path, function (err, entry) {
  2942. if (err) return done(err);
  2943. IDBFS.storeRemoteEntry(store, path, entry, done);
  2944. });
  2945. }
  2946. });
  2947. // sort paths in descending order so files are deleted before their
  2948. // parent directories
  2949. remove.sort().reverse().forEach(function(path) {
  2950. if (dst.type === 'local') {
  2951. IDBFS.removeLocalEntry(path, done);
  2952. } else {
  2953. IDBFS.removeRemoteEntry(store, path, done);
  2954. }
  2955. });
  2956. }};
  2957. var FS = {root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,ErrnoError:null,genericErrors:{},filesystems:null,syncFSRequests:0,lookupPath:function(path, opts) {
  2958. path = PATH_FS.resolve(FS.cwd(), path);
  2959. opts = opts || {};
  2960. if (!path) return { path: '', node: null };
  2961. var defaults = {
  2962. follow_mount: true,
  2963. recurse_count: 0
  2964. };
  2965. for (var key in defaults) {
  2966. if (opts[key] === undefined) {
  2967. opts[key] = defaults[key];
  2968. }
  2969. }
  2970. if (opts.recurse_count > 8) { // max recursive lookup of 8
  2971. throw new FS.ErrnoError(32);
  2972. }
  2973. // split the path
  2974. var parts = PATH.normalizeArray(path.split('/').filter(function(p) {
  2975. return !!p;
  2976. }), false);
  2977. // start at the root
  2978. var current = FS.root;
  2979. var current_path = '/';
  2980. for (var i = 0; i < parts.length; i++) {
  2981. var islast = (i === parts.length-1);
  2982. if (islast && opts.parent) {
  2983. // stop resolving
  2984. break;
  2985. }
  2986. current = FS.lookupNode(current, parts[i]);
  2987. current_path = PATH.join2(current_path, parts[i]);
  2988. // jump to the mount's root node if this is a mountpoint
  2989. if (FS.isMountpoint(current)) {
  2990. if (!islast || (islast && opts.follow_mount)) {
  2991. current = current.mounted.root;
  2992. }
  2993. }
  2994. // by default, lookupPath will not follow a symlink if it is the final path component.
  2995. // setting opts.follow = true will override this behavior.
  2996. if (!islast || opts.follow) {
  2997. var count = 0;
  2998. while (FS.isLink(current.mode)) {
  2999. var link = FS.readlink(current_path);
  3000. current_path = PATH_FS.resolve(PATH.dirname(current_path), link);
  3001. var lookup = FS.lookupPath(current_path, { recurse_count: opts.recurse_count });
  3002. current = lookup.node;
  3003. if (count++ > 40) { // limit max consecutive symlinks to 40 (SYMLOOP_MAX).
  3004. throw new FS.ErrnoError(32);
  3005. }
  3006. }
  3007. }
  3008. }
  3009. return { path: current_path, node: current };
  3010. },getPath:function(node) {
  3011. var path;
  3012. while (true) {
  3013. if (FS.isRoot(node)) {
  3014. var mount = node.mount.mountpoint;
  3015. if (!path) return mount;
  3016. return mount[mount.length-1] !== '/' ? mount + '/' + path : mount + path;
  3017. }
  3018. path = path ? node.name + '/' + path : node.name;
  3019. node = node.parent;
  3020. }
  3021. },hashName:function(parentid, name) {
  3022. var hash = 0;
  3023. for (var i = 0; i < name.length; i++) {
  3024. hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0;
  3025. }
  3026. return ((parentid + hash) >>> 0) % FS.nameTable.length;
  3027. },hashAddNode:function(node) {
  3028. var hash = FS.hashName(node.parent.id, node.name);
  3029. node.name_next = FS.nameTable[hash];
  3030. FS.nameTable[hash] = node;
  3031. },hashRemoveNode:function(node) {
  3032. var hash = FS.hashName(node.parent.id, node.name);
  3033. if (FS.nameTable[hash] === node) {
  3034. FS.nameTable[hash] = node.name_next;
  3035. } else {
  3036. var current = FS.nameTable[hash];
  3037. while (current) {
  3038. if (current.name_next === node) {
  3039. current.name_next = node.name_next;
  3040. break;
  3041. }
  3042. current = current.name_next;
  3043. }
  3044. }
  3045. },lookupNode:function(parent, name) {
  3046. var errCode = FS.mayLookup(parent);
  3047. if (errCode) {
  3048. throw new FS.ErrnoError(errCode, parent);
  3049. }
  3050. var hash = FS.hashName(parent.id, name);
  3051. for (var node = FS.nameTable[hash]; node; node = node.name_next) {
  3052. var nodeName = node.name;
  3053. if (node.parent.id === parent.id && nodeName === name) {
  3054. return node;
  3055. }
  3056. }
  3057. // if we failed to find it in the cache, call into the VFS
  3058. return FS.lookup(parent, name);
  3059. },createNode:function(parent, name, mode, rdev) {
  3060. var node = new FS.FSNode(parent, name, mode, rdev);
  3061. FS.hashAddNode(node);
  3062. return node;
  3063. },destroyNode:function(node) {
  3064. FS.hashRemoveNode(node);
  3065. },isRoot:function(node) {
  3066. return node === node.parent;
  3067. },isMountpoint:function(node) {
  3068. return !!node.mounted;
  3069. },isFile:function(mode) {
  3070. return (mode & 61440) === 32768;
  3071. },isDir:function(mode) {
  3072. return (mode & 61440) === 16384;
  3073. },isLink:function(mode) {
  3074. return (mode & 61440) === 40960;
  3075. },isChrdev:function(mode) {
  3076. return (mode & 61440) === 8192;
  3077. },isBlkdev:function(mode) {
  3078. return (mode & 61440) === 24576;
  3079. },isFIFO:function(mode) {
  3080. return (mode & 61440) === 4096;
  3081. },isSocket:function(mode) {
  3082. return (mode & 49152) === 49152;
  3083. },flagModes:{"r":0,"r+":2,"w":577,"w+":578,"a":1089,"a+":1090},modeStringToFlags:function(str) {
  3084. var flags = FS.flagModes[str];
  3085. if (typeof flags === 'undefined') {
  3086. throw new Error('Unknown file open mode: ' + str);
  3087. }
  3088. return flags;
  3089. },flagsToPermissionString:function(flag) {
  3090. var perms = ['r', 'w', 'rw'][flag & 3];
  3091. if ((flag & 512)) {
  3092. perms += 'w';
  3093. }
  3094. return perms;
  3095. },nodePermissions:function(node, perms) {
  3096. if (FS.ignorePermissions) {
  3097. return 0;
  3098. }
  3099. // return 0 if any user, group or owner bits are set.
  3100. if (perms.includes('r') && !(node.mode & 292)) {
  3101. return 2;
  3102. } else if (perms.includes('w') && !(node.mode & 146)) {
  3103. return 2;
  3104. } else if (perms.includes('x') && !(node.mode & 73)) {
  3105. return 2;
  3106. }
  3107. return 0;
  3108. },mayLookup:function(dir) {
  3109. var errCode = FS.nodePermissions(dir, 'x');
  3110. if (errCode) return errCode;
  3111. if (!dir.node_ops.lookup) return 2;
  3112. return 0;
  3113. },mayCreate:function(dir, name) {
  3114. try {
  3115. var node = FS.lookupNode(dir, name);
  3116. return 20;
  3117. } catch (e) {
  3118. }
  3119. return FS.nodePermissions(dir, 'wx');
  3120. },mayDelete:function(dir, name, isdir) {
  3121. var node;
  3122. try {
  3123. node = FS.lookupNode(dir, name);
  3124. } catch (e) {
  3125. return e.errno;
  3126. }
  3127. var errCode = FS.nodePermissions(dir, 'wx');
  3128. if (errCode) {
  3129. return errCode;
  3130. }
  3131. if (isdir) {
  3132. if (!FS.isDir(node.mode)) {
  3133. return 54;
  3134. }
  3135. if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) {
  3136. return 10;
  3137. }
  3138. } else {
  3139. if (FS.isDir(node.mode)) {
  3140. return 31;
  3141. }
  3142. }
  3143. return 0;
  3144. },mayOpen:function(node, flags) {
  3145. if (!node) {
  3146. return 44;
  3147. }
  3148. if (FS.isLink(node.mode)) {
  3149. return 32;
  3150. } else if (FS.isDir(node.mode)) {
  3151. if (FS.flagsToPermissionString(flags) !== 'r' || // opening for write
  3152. (flags & 512)) { // TODO: check for O_SEARCH? (== search for dir only)
  3153. return 31;
  3154. }
  3155. }
  3156. return FS.nodePermissions(node, FS.flagsToPermissionString(flags));
  3157. },MAX_OPEN_FDS:4096,nextfd:function(fd_start, fd_end) {
  3158. fd_start = fd_start || 0;
  3159. fd_end = fd_end || FS.MAX_OPEN_FDS;
  3160. for (var fd = fd_start; fd <= fd_end; fd++) {
  3161. if (!FS.streams[fd]) {
  3162. return fd;
  3163. }
  3164. }
  3165. throw new FS.ErrnoError(33);
  3166. },getStream:function(fd) {
  3167. return FS.streams[fd];
  3168. },createStream:function(stream, fd_start, fd_end) {
  3169. if (!FS.FSStream) {
  3170. FS.FSStream = /** @constructor */ function(){};
  3171. FS.FSStream.prototype = {
  3172. object: {
  3173. get: function() { return this.node; },
  3174. set: function(val) { this.node = val; }
  3175. },
  3176. isRead: {
  3177. get: function() { return (this.flags & 2097155) !== 1; }
  3178. },
  3179. isWrite: {
  3180. get: function() { return (this.flags & 2097155) !== 0; }
  3181. },
  3182. isAppend: {
  3183. get: function() { return (this.flags & 1024); }
  3184. }
  3185. };
  3186. }
  3187. // clone it, so we can return an instance of FSStream
  3188. var newStream = new FS.FSStream();
  3189. for (var p in stream) {
  3190. newStream[p] = stream[p];
  3191. }
  3192. stream = newStream;
  3193. var fd = FS.nextfd(fd_start, fd_end);
  3194. stream.fd = fd;
  3195. FS.streams[fd] = stream;
  3196. return stream;
  3197. },closeStream:function(fd) {
  3198. FS.streams[fd] = null;
  3199. },chrdev_stream_ops:{open:function(stream) {
  3200. var device = FS.getDevice(stream.node.rdev);
  3201. // override node's stream ops with the device's
  3202. stream.stream_ops = device.stream_ops;
  3203. // forward the open call
  3204. if (stream.stream_ops.open) {
  3205. stream.stream_ops.open(stream);
  3206. }
  3207. },llseek:function() {
  3208. throw new FS.ErrnoError(70);
  3209. }},major:function(dev) {
  3210. return ((dev) >> 8);
  3211. },minor:function(dev) {
  3212. return ((dev) & 0xff);
  3213. },makedev:function(ma, mi) {
  3214. return ((ma) << 8 | (mi));
  3215. },registerDevice:function(dev, ops) {
  3216. FS.devices[dev] = { stream_ops: ops };
  3217. },getDevice:function(dev) {
  3218. return FS.devices[dev];
  3219. },getMounts:function(mount) {
  3220. var mounts = [];
  3221. var check = [mount];
  3222. while (check.length) {
  3223. var m = check.pop();
  3224. mounts.push(m);
  3225. check.push.apply(check, m.mounts);
  3226. }
  3227. return mounts;
  3228. },syncfs:function(populate, callback) {
  3229. if (typeof(populate) === 'function') {
  3230. callback = populate;
  3231. populate = false;
  3232. }
  3233. FS.syncFSRequests++;
  3234. if (FS.syncFSRequests > 1) {
  3235. err('warning: ' + FS.syncFSRequests + ' FS.syncfs operations in flight at once, probably just doing extra work');
  3236. }
  3237. var mounts = FS.getMounts(FS.root.mount);
  3238. var completed = 0;
  3239. function doCallback(errCode) {
  3240. FS.syncFSRequests--;
  3241. return callback(errCode);
  3242. }
  3243. function done(errCode) {
  3244. if (errCode) {
  3245. if (!done.errored) {
  3246. done.errored = true;
  3247. return doCallback(errCode);
  3248. }
  3249. return;
  3250. }
  3251. if (++completed >= mounts.length) {
  3252. doCallback(null);
  3253. }
  3254. };
  3255. // sync all mounts
  3256. mounts.forEach(function (mount) {
  3257. if (!mount.type.syncfs) {
  3258. return done(null);
  3259. }
  3260. mount.type.syncfs(mount, populate, done);
  3261. });
  3262. },mount:function(type, opts, mountpoint) {
  3263. var root = mountpoint === '/';
  3264. var pseudo = !mountpoint;
  3265. var node;
  3266. if (root && FS.root) {
  3267. throw new FS.ErrnoError(10);
  3268. } else if (!root && !pseudo) {
  3269. var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
  3270. mountpoint = lookup.path; // use the absolute path
  3271. node = lookup.node;
  3272. if (FS.isMountpoint(node)) {
  3273. throw new FS.ErrnoError(10);
  3274. }
  3275. if (!FS.isDir(node.mode)) {
  3276. throw new FS.ErrnoError(54);
  3277. }
  3278. }
  3279. var mount = {
  3280. type: type,
  3281. opts: opts,
  3282. mountpoint: mountpoint,
  3283. mounts: []
  3284. };
  3285. // create a root node for the fs
  3286. var mountRoot = type.mount(mount);
  3287. mountRoot.mount = mount;
  3288. mount.root = mountRoot;
  3289. if (root) {
  3290. FS.root = mountRoot;
  3291. } else if (node) {
  3292. // set as a mountpoint
  3293. node.mounted = mount;
  3294. // add the new mount to the current mount's children
  3295. if (node.mount) {
  3296. node.mount.mounts.push(mount);
  3297. }
  3298. }
  3299. return mountRoot;
  3300. },unmount:function (mountpoint) {
  3301. var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
  3302. if (!FS.isMountpoint(lookup.node)) {
  3303. throw new FS.ErrnoError(28);
  3304. }
  3305. // destroy the nodes for this mount, and all its child mounts
  3306. var node = lookup.node;
  3307. var mount = node.mounted;
  3308. var mounts = FS.getMounts(mount);
  3309. Object.keys(FS.nameTable).forEach(function (hash) {
  3310. var current = FS.nameTable[hash];
  3311. while (current) {
  3312. var next = current.name_next;
  3313. if (mounts.includes(current.mount)) {
  3314. FS.destroyNode(current);
  3315. }
  3316. current = next;
  3317. }
  3318. });
  3319. // no longer a mountpoint
  3320. node.mounted = null;
  3321. // remove this mount from the child mounts
  3322. var idx = node.mount.mounts.indexOf(mount);
  3323. node.mount.mounts.splice(idx, 1);
  3324. },lookup:function(parent, name) {
  3325. return parent.node_ops.lookup(parent, name);
  3326. },mknod:function(path, mode, dev) {
  3327. var lookup = FS.lookupPath(path, { parent: true });
  3328. var parent = lookup.node;
  3329. var name = PATH.basename(path);
  3330. if (!name || name === '.' || name === '..') {
  3331. throw new FS.ErrnoError(28);
  3332. }
  3333. var errCode = FS.mayCreate(parent, name);
  3334. if (errCode) {
  3335. throw new FS.ErrnoError(errCode);
  3336. }
  3337. if (!parent.node_ops.mknod) {
  3338. throw new FS.ErrnoError(63);
  3339. }
  3340. return parent.node_ops.mknod(parent, name, mode, dev);
  3341. },create:function(path, mode) {
  3342. mode = mode !== undefined ? mode : 438 /* 0666 */;
  3343. mode &= 4095;
  3344. mode |= 32768;
  3345. return FS.mknod(path, mode, 0);
  3346. },mkdir:function(path, mode) {
  3347. mode = mode !== undefined ? mode : 511 /* 0777 */;
  3348. mode &= 511 | 512;
  3349. mode |= 16384;
  3350. return FS.mknod(path, mode, 0);
  3351. },mkdirTree:function(path, mode) {
  3352. var dirs = path.split('/');
  3353. var d = '';
  3354. for (var i = 0; i < dirs.length; ++i) {
  3355. if (!dirs[i]) continue;
  3356. d += '/' + dirs[i];
  3357. try {
  3358. FS.mkdir(d, mode);
  3359. } catch(e) {
  3360. if (e.errno != 20) throw e;
  3361. }
  3362. }
  3363. },mkdev:function(path, mode, dev) {
  3364. if (typeof(dev) === 'undefined') {
  3365. dev = mode;
  3366. mode = 438 /* 0666 */;
  3367. }
  3368. mode |= 8192;
  3369. return FS.mknod(path, mode, dev);
  3370. },symlink:function(oldpath, newpath) {
  3371. if (!PATH_FS.resolve(oldpath)) {
  3372. throw new FS.ErrnoError(44);
  3373. }
  3374. var lookup = FS.lookupPath(newpath, { parent: true });
  3375. var parent = lookup.node;
  3376. if (!parent) {
  3377. throw new FS.ErrnoError(44);
  3378. }
  3379. var newname = PATH.basename(newpath);
  3380. var errCode = FS.mayCreate(parent, newname);
  3381. if (errCode) {
  3382. throw new FS.ErrnoError(errCode);
  3383. }
  3384. if (!parent.node_ops.symlink) {
  3385. throw new FS.ErrnoError(63);
  3386. }
  3387. return parent.node_ops.symlink(parent, newname, oldpath);
  3388. },rename:function(old_path, new_path) {
  3389. var old_dirname = PATH.dirname(old_path);
  3390. var new_dirname = PATH.dirname(new_path);
  3391. var old_name = PATH.basename(old_path);
  3392. var new_name = PATH.basename(new_path);
  3393. // parents must exist
  3394. var lookup, old_dir, new_dir;
  3395. // let the errors from non existant directories percolate up
  3396. lookup = FS.lookupPath(old_path, { parent: true });
  3397. old_dir = lookup.node;
  3398. lookup = FS.lookupPath(new_path, { parent: true });
  3399. new_dir = lookup.node;
  3400. if (!old_dir || !new_dir) throw new FS.ErrnoError(44);
  3401. // need to be part of the same mount
  3402. if (old_dir.mount !== new_dir.mount) {
  3403. throw new FS.ErrnoError(75);
  3404. }
  3405. // source must exist
  3406. var old_node = FS.lookupNode(old_dir, old_name);
  3407. // old path should not be an ancestor of the new path
  3408. var relative = PATH_FS.relative(old_path, new_dirname);
  3409. if (relative.charAt(0) !== '.') {
  3410. throw new FS.ErrnoError(28);
  3411. }
  3412. // new path should not be an ancestor of the old path
  3413. relative = PATH_FS.relative(new_path, old_dirname);
  3414. if (relative.charAt(0) !== '.') {
  3415. throw new FS.ErrnoError(55);
  3416. }
  3417. // see if the new path already exists
  3418. var new_node;
  3419. try {
  3420. new_node = FS.lookupNode(new_dir, new_name);
  3421. } catch (e) {
  3422. // not fatal
  3423. }
  3424. // early out if nothing needs to change
  3425. if (old_node === new_node) {
  3426. return;
  3427. }
  3428. // we'll need to delete the old entry
  3429. var isdir = FS.isDir(old_node.mode);
  3430. var errCode = FS.mayDelete(old_dir, old_name, isdir);
  3431. if (errCode) {
  3432. throw new FS.ErrnoError(errCode);
  3433. }
  3434. // need delete permissions if we'll be overwriting.
  3435. // need create permissions if new doesn't already exist.
  3436. errCode = new_node ?
  3437. FS.mayDelete(new_dir, new_name, isdir) :
  3438. FS.mayCreate(new_dir, new_name);
  3439. if (errCode) {
  3440. throw new FS.ErrnoError(errCode);
  3441. }
  3442. if (!old_dir.node_ops.rename) {
  3443. throw new FS.ErrnoError(63);
  3444. }
  3445. if (FS.isMountpoint(old_node) || (new_node && FS.isMountpoint(new_node))) {
  3446. throw new FS.ErrnoError(10);
  3447. }
  3448. // if we are going to change the parent, check write permissions
  3449. if (new_dir !== old_dir) {
  3450. errCode = FS.nodePermissions(old_dir, 'w');
  3451. if (errCode) {
  3452. throw new FS.ErrnoError(errCode);
  3453. }
  3454. }
  3455. // remove the node from the lookup hash
  3456. FS.hashRemoveNode(old_node);
  3457. // do the underlying fs rename
  3458. try {
  3459. old_dir.node_ops.rename(old_node, new_dir, new_name);
  3460. } catch (e) {
  3461. throw e;
  3462. } finally {
  3463. // add the node back to the hash (in case node_ops.rename
  3464. // changed its name)
  3465. FS.hashAddNode(old_node);
  3466. }
  3467. },rmdir:function(path) {
  3468. var lookup = FS.lookupPath(path, { parent: true });
  3469. var parent = lookup.node;
  3470. var name = PATH.basename(path);
  3471. var node = FS.lookupNode(parent, name);
  3472. var errCode = FS.mayDelete(parent, name, true);
  3473. if (errCode) {
  3474. throw new FS.ErrnoError(errCode);
  3475. }
  3476. if (!parent.node_ops.rmdir) {
  3477. throw new FS.ErrnoError(63);
  3478. }
  3479. if (FS.isMountpoint(node)) {
  3480. throw new FS.ErrnoError(10);
  3481. }
  3482. parent.node_ops.rmdir(parent, name);
  3483. FS.destroyNode(node);
  3484. },readdir:function(path) {
  3485. var lookup = FS.lookupPath(path, { follow: true });
  3486. var node = lookup.node;
  3487. if (!node.node_ops.readdir) {
  3488. throw new FS.ErrnoError(54);
  3489. }
  3490. return node.node_ops.readdir(node);
  3491. },unlink:function(path) {
  3492. var lookup = FS.lookupPath(path, { parent: true });
  3493. var parent = lookup.node;
  3494. var name = PATH.basename(path);
  3495. var node = FS.lookupNode(parent, name);
  3496. var errCode = FS.mayDelete(parent, name, false);
  3497. if (errCode) {
  3498. // According to POSIX, we should map EISDIR to EPERM, but
  3499. // we instead do what Linux does (and we must, as we use
  3500. // the musl linux libc).
  3501. throw new FS.ErrnoError(errCode);
  3502. }
  3503. if (!parent.node_ops.unlink) {
  3504. throw new FS.ErrnoError(63);
  3505. }
  3506. if (FS.isMountpoint(node)) {
  3507. throw new FS.ErrnoError(10);
  3508. }
  3509. parent.node_ops.unlink(parent, name);
  3510. FS.destroyNode(node);
  3511. },readlink:function(path) {
  3512. var lookup = FS.lookupPath(path);
  3513. var link = lookup.node;
  3514. if (!link) {
  3515. throw new FS.ErrnoError(44);
  3516. }
  3517. if (!link.node_ops.readlink) {
  3518. throw new FS.ErrnoError(28);
  3519. }
  3520. return PATH_FS.resolve(FS.getPath(link.parent), link.node_ops.readlink(link));
  3521. },stat:function(path, dontFollow) {
  3522. var lookup = FS.lookupPath(path, { follow: !dontFollow });
  3523. var node = lookup.node;
  3524. if (!node) {
  3525. throw new FS.ErrnoError(44);
  3526. }
  3527. if (!node.node_ops.getattr) {
  3528. throw new FS.ErrnoError(63);
  3529. }
  3530. return node.node_ops.getattr(node);
  3531. },lstat:function(path) {
  3532. return FS.stat(path, true);
  3533. },chmod:function(path, mode, dontFollow) {
  3534. var node;
  3535. if (typeof path === 'string') {
  3536. var lookup = FS.lookupPath(path, { follow: !dontFollow });
  3537. node = lookup.node;
  3538. } else {
  3539. node = path;
  3540. }
  3541. if (!node.node_ops.setattr) {
  3542. throw new FS.ErrnoError(63);
  3543. }
  3544. node.node_ops.setattr(node, {
  3545. mode: (mode & 4095) | (node.mode & ~4095),
  3546. timestamp: Date.now()
  3547. });
  3548. },lchmod:function(path, mode) {
  3549. FS.chmod(path, mode, true);
  3550. },fchmod:function(fd, mode) {
  3551. var stream = FS.getStream(fd);
  3552. if (!stream) {
  3553. throw new FS.ErrnoError(8);
  3554. }
  3555. FS.chmod(stream.node, mode);
  3556. },chown:function(path, uid, gid, dontFollow) {
  3557. var node;
  3558. if (typeof path === 'string') {
  3559. var lookup = FS.lookupPath(path, { follow: !dontFollow });
  3560. node = lookup.node;
  3561. } else {
  3562. node = path;
  3563. }
  3564. if (!node.node_ops.setattr) {
  3565. throw new FS.ErrnoError(63);
  3566. }
  3567. node.node_ops.setattr(node, {
  3568. timestamp: Date.now()
  3569. // we ignore the uid / gid for now
  3570. });
  3571. },lchown:function(path, uid, gid) {
  3572. FS.chown(path, uid, gid, true);
  3573. },fchown:function(fd, uid, gid) {
  3574. var stream = FS.getStream(fd);
  3575. if (!stream) {
  3576. throw new FS.ErrnoError(8);
  3577. }
  3578. FS.chown(stream.node, uid, gid);
  3579. },truncate:function(path, len) {
  3580. if (len < 0) {
  3581. throw new FS.ErrnoError(28);
  3582. }
  3583. var node;
  3584. if (typeof path === 'string') {
  3585. var lookup = FS.lookupPath(path, { follow: true });
  3586. node = lookup.node;
  3587. } else {
  3588. node = path;
  3589. }
  3590. if (!node.node_ops.setattr) {
  3591. throw new FS.ErrnoError(63);
  3592. }
  3593. if (FS.isDir(node.mode)) {
  3594. throw new FS.ErrnoError(31);
  3595. }
  3596. if (!FS.isFile(node.mode)) {
  3597. throw new FS.ErrnoError(28);
  3598. }
  3599. var errCode = FS.nodePermissions(node, 'w');
  3600. if (errCode) {
  3601. throw new FS.ErrnoError(errCode);
  3602. }
  3603. node.node_ops.setattr(node, {
  3604. size: len,
  3605. timestamp: Date.now()
  3606. });
  3607. },ftruncate:function(fd, len) {
  3608. var stream = FS.getStream(fd);
  3609. if (!stream) {
  3610. throw new FS.ErrnoError(8);
  3611. }
  3612. if ((stream.flags & 2097155) === 0) {
  3613. throw new FS.ErrnoError(28);
  3614. }
  3615. FS.truncate(stream.node, len);
  3616. },utime:function(path, atime, mtime) {
  3617. var lookup = FS.lookupPath(path, { follow: true });
  3618. var node = lookup.node;
  3619. node.node_ops.setattr(node, {
  3620. timestamp: Math.max(atime, mtime)
  3621. });
  3622. },open:function(path, flags, mode, fd_start, fd_end) {
  3623. if (path === "") {
  3624. throw new FS.ErrnoError(44);
  3625. }
  3626. flags = typeof flags === 'string' ? FS.modeStringToFlags(flags) : flags;
  3627. mode = typeof mode === 'undefined' ? 438 /* 0666 */ : mode;
  3628. if ((flags & 64)) {
  3629. mode = (mode & 4095) | 32768;
  3630. } else {
  3631. mode = 0;
  3632. }
  3633. var node;
  3634. if (typeof path === 'object') {
  3635. node = path;
  3636. } else {
  3637. path = PATH.normalize(path);
  3638. try {
  3639. var lookup = FS.lookupPath(path, {
  3640. follow: !(flags & 131072)
  3641. });
  3642. node = lookup.node;
  3643. } catch (e) {
  3644. // ignore
  3645. }
  3646. }
  3647. // perhaps we need to create the node
  3648. var created = false;
  3649. if ((flags & 64)) {
  3650. if (node) {
  3651. // if O_CREAT and O_EXCL are set, error out if the node already exists
  3652. if ((flags & 128)) {
  3653. throw new FS.ErrnoError(20);
  3654. }
  3655. } else {
  3656. // node doesn't exist, try to create it
  3657. node = FS.mknod(path, mode, 0);
  3658. created = true;
  3659. }
  3660. }
  3661. if (!node) {
  3662. throw new FS.ErrnoError(44);
  3663. }
  3664. // can't truncate a device
  3665. if (FS.isChrdev(node.mode)) {
  3666. flags &= ~512;
  3667. }
  3668. // if asked only for a directory, then this must be one
  3669. if ((flags & 65536) && !FS.isDir(node.mode)) {
  3670. throw new FS.ErrnoError(54);
  3671. }
  3672. // check permissions, if this is not a file we just created now (it is ok to
  3673. // create and write to a file with read-only permissions; it is read-only
  3674. // for later use)
  3675. if (!created) {
  3676. var errCode = FS.mayOpen(node, flags);
  3677. if (errCode) {
  3678. throw new FS.ErrnoError(errCode);
  3679. }
  3680. }
  3681. // do truncation if necessary
  3682. if ((flags & 512)) {
  3683. FS.truncate(node, 0);
  3684. }
  3685. // we've already handled these, don't pass down to the underlying vfs
  3686. flags &= ~(128 | 512 | 131072);
  3687. // register the stream with the filesystem
  3688. var stream = FS.createStream({
  3689. node: node,
  3690. path: FS.getPath(node), // we want the absolute path to the node
  3691. flags: flags,
  3692. seekable: true,
  3693. position: 0,
  3694. stream_ops: node.stream_ops,
  3695. // used by the file family libc calls (fopen, fwrite, ferror, etc.)
  3696. ungotten: [],
  3697. error: false
  3698. }, fd_start, fd_end);
  3699. // call the new stream's open function
  3700. if (stream.stream_ops.open) {
  3701. stream.stream_ops.open(stream);
  3702. }
  3703. if (Module['logReadFiles'] && !(flags & 1)) {
  3704. if (!FS.readFiles) FS.readFiles = {};
  3705. if (!(path in FS.readFiles)) {
  3706. FS.readFiles[path] = 1;
  3707. }
  3708. }
  3709. return stream;
  3710. },close:function(stream) {
  3711. if (FS.isClosed(stream)) {
  3712. throw new FS.ErrnoError(8);
  3713. }
  3714. if (stream.getdents) stream.getdents = null; // free readdir state
  3715. try {
  3716. if (stream.stream_ops.close) {
  3717. stream.stream_ops.close(stream);
  3718. }
  3719. } catch (e) {
  3720. throw e;
  3721. } finally {
  3722. FS.closeStream(stream.fd);
  3723. }
  3724. stream.fd = null;
  3725. },isClosed:function(stream) {
  3726. return stream.fd === null;
  3727. },llseek:function(stream, offset, whence) {
  3728. if (FS.isClosed(stream)) {
  3729. throw new FS.ErrnoError(8);
  3730. }
  3731. if (!stream.seekable || !stream.stream_ops.llseek) {
  3732. throw new FS.ErrnoError(70);
  3733. }
  3734. if (whence != 0 && whence != 1 && whence != 2) {
  3735. throw new FS.ErrnoError(28);
  3736. }
  3737. stream.position = stream.stream_ops.llseek(stream, offset, whence);
  3738. stream.ungotten = [];
  3739. return stream.position;
  3740. },read:function(stream, buffer, offset, length, position) {
  3741. if (length < 0 || position < 0) {
  3742. throw new FS.ErrnoError(28);
  3743. }
  3744. if (FS.isClosed(stream)) {
  3745. throw new FS.ErrnoError(8);
  3746. }
  3747. if ((stream.flags & 2097155) === 1) {
  3748. throw new FS.ErrnoError(8);
  3749. }
  3750. if (FS.isDir(stream.node.mode)) {
  3751. throw new FS.ErrnoError(31);
  3752. }
  3753. if (!stream.stream_ops.read) {
  3754. throw new FS.ErrnoError(28);
  3755. }
  3756. var seeking = typeof position !== 'undefined';
  3757. if (!seeking) {
  3758. position = stream.position;
  3759. } else if (!stream.seekable) {
  3760. throw new FS.ErrnoError(70);
  3761. }
  3762. var bytesRead = stream.stream_ops.read(stream, buffer, offset, length, position);
  3763. if (!seeking) stream.position += bytesRead;
  3764. return bytesRead;
  3765. },write:function(stream, buffer, offset, length, position, canOwn) {
  3766. if (length < 0 || position < 0) {
  3767. throw new FS.ErrnoError(28);
  3768. }
  3769. if (FS.isClosed(stream)) {
  3770. throw new FS.ErrnoError(8);
  3771. }
  3772. if ((stream.flags & 2097155) === 0) {
  3773. throw new FS.ErrnoError(8);
  3774. }
  3775. if (FS.isDir(stream.node.mode)) {
  3776. throw new FS.ErrnoError(31);
  3777. }
  3778. if (!stream.stream_ops.write) {
  3779. throw new FS.ErrnoError(28);
  3780. }
  3781. if (stream.seekable && stream.flags & 1024) {
  3782. // seek to the end before writing in append mode
  3783. FS.llseek(stream, 0, 2);
  3784. }
  3785. var seeking = typeof position !== 'undefined';
  3786. if (!seeking) {
  3787. position = stream.position;
  3788. } else if (!stream.seekable) {
  3789. throw new FS.ErrnoError(70);
  3790. }
  3791. var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn);
  3792. if (!seeking) stream.position += bytesWritten;
  3793. return bytesWritten;
  3794. },allocate:function(stream, offset, length) {
  3795. if (FS.isClosed(stream)) {
  3796. throw new FS.ErrnoError(8);
  3797. }
  3798. if (offset < 0 || length <= 0) {
  3799. throw new FS.ErrnoError(28);
  3800. }
  3801. if ((stream.flags & 2097155) === 0) {
  3802. throw new FS.ErrnoError(8);
  3803. }
  3804. if (!FS.isFile(stream.node.mode) && !FS.isDir(stream.node.mode)) {
  3805. throw new FS.ErrnoError(43);
  3806. }
  3807. if (!stream.stream_ops.allocate) {
  3808. throw new FS.ErrnoError(138);
  3809. }
  3810. stream.stream_ops.allocate(stream, offset, length);
  3811. },mmap:function(stream, address, length, position, prot, flags) {
  3812. // User requests writing to file (prot & PROT_WRITE != 0).
  3813. // Checking if we have permissions to write to the file unless
  3814. // MAP_PRIVATE flag is set. According to POSIX spec it is possible
  3815. // to write to file opened in read-only mode with MAP_PRIVATE flag,
  3816. // as all modifications will be visible only in the memory of
  3817. // the current process.
  3818. if ((prot & 2) !== 0
  3819. && (flags & 2) === 0
  3820. && (stream.flags & 2097155) !== 2) {
  3821. throw new FS.ErrnoError(2);
  3822. }
  3823. if ((stream.flags & 2097155) === 1) {
  3824. throw new FS.ErrnoError(2);
  3825. }
  3826. if (!stream.stream_ops.mmap) {
  3827. throw new FS.ErrnoError(43);
  3828. }
  3829. return stream.stream_ops.mmap(stream, address, length, position, prot, flags);
  3830. },msync:function(stream, buffer, offset, length, mmapFlags) {
  3831. if (!stream || !stream.stream_ops.msync) {
  3832. return 0;
  3833. }
  3834. return stream.stream_ops.msync(stream, buffer, offset, length, mmapFlags);
  3835. },munmap:function(stream) {
  3836. return 0;
  3837. },ioctl:function(stream, cmd, arg) {
  3838. if (!stream.stream_ops.ioctl) {
  3839. throw new FS.ErrnoError(59);
  3840. }
  3841. return stream.stream_ops.ioctl(stream, cmd, arg);
  3842. },readFile:function(path, opts) {
  3843. opts = opts || {};
  3844. opts.flags = opts.flags || 0;
  3845. opts.encoding = opts.encoding || 'binary';
  3846. if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
  3847. throw new Error('Invalid encoding type "' + opts.encoding + '"');
  3848. }
  3849. var ret;
  3850. var stream = FS.open(path, opts.flags);
  3851. var stat = FS.stat(path);
  3852. var length = stat.size;
  3853. var buf = new Uint8Array(length);
  3854. FS.read(stream, buf, 0, length, 0);
  3855. if (opts.encoding === 'utf8') {
  3856. ret = UTF8ArrayToString(buf, 0);
  3857. } else if (opts.encoding === 'binary') {
  3858. ret = buf;
  3859. }
  3860. FS.close(stream);
  3861. return ret;
  3862. },writeFile:function(path, data, opts) {
  3863. opts = opts || {};
  3864. opts.flags = opts.flags || 577;
  3865. var stream = FS.open(path, opts.flags, opts.mode);
  3866. if (typeof data === 'string') {
  3867. var buf = new Uint8Array(lengthBytesUTF8(data)+1);
  3868. var actualNumBytes = stringToUTF8Array(data, buf, 0, buf.length);
  3869. FS.write(stream, buf, 0, actualNumBytes, undefined, opts.canOwn);
  3870. } else if (ArrayBuffer.isView(data)) {
  3871. FS.write(stream, data, 0, data.byteLength, undefined, opts.canOwn);
  3872. } else {
  3873. throw new Error('Unsupported data type');
  3874. }
  3875. FS.close(stream);
  3876. },cwd:function() {
  3877. return FS.currentPath;
  3878. },chdir:function(path) {
  3879. var lookup = FS.lookupPath(path, { follow: true });
  3880. if (lookup.node === null) {
  3881. throw new FS.ErrnoError(44);
  3882. }
  3883. if (!FS.isDir(lookup.node.mode)) {
  3884. throw new FS.ErrnoError(54);
  3885. }
  3886. var errCode = FS.nodePermissions(lookup.node, 'x');
  3887. if (errCode) {
  3888. throw new FS.ErrnoError(errCode);
  3889. }
  3890. FS.currentPath = lookup.path;
  3891. },createDefaultDirectories:function() {
  3892. FS.mkdir('/tmp');
  3893. FS.mkdir('/home');
  3894. FS.mkdir('/home/web_user');
  3895. },createDefaultDevices:function() {
  3896. // create /dev
  3897. FS.mkdir('/dev');
  3898. // setup /dev/null
  3899. FS.registerDevice(FS.makedev(1, 3), {
  3900. read: function() { return 0; },
  3901. write: function(stream, buffer, offset, length, pos) { return length; }
  3902. });
  3903. FS.mkdev('/dev/null', FS.makedev(1, 3));
  3904. // setup /dev/tty and /dev/tty1
  3905. // stderr needs to print output using err() rather than out()
  3906. // so we register a second tty just for it.
  3907. TTY.register(FS.makedev(5, 0), TTY.default_tty_ops);
  3908. TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops);
  3909. FS.mkdev('/dev/tty', FS.makedev(5, 0));
  3910. FS.mkdev('/dev/tty1', FS.makedev(6, 0));
  3911. // setup /dev/[u]random
  3912. var random_device = getRandomDevice();
  3913. FS.createDevice('/dev', 'random', random_device);
  3914. FS.createDevice('/dev', 'urandom', random_device);
  3915. // we're not going to emulate the actual shm device,
  3916. // just create the tmp dirs that reside in it commonly
  3917. FS.mkdir('/dev/shm');
  3918. FS.mkdir('/dev/shm/tmp');
  3919. },createSpecialDirectories:function() {
  3920. // create /proc/self/fd which allows /proc/self/fd/6 => readlink gives the
  3921. // name of the stream for fd 6 (see test_unistd_ttyname)
  3922. FS.mkdir('/proc');
  3923. var proc_self = FS.mkdir('/proc/self');
  3924. FS.mkdir('/proc/self/fd');
  3925. FS.mount({
  3926. mount: function() {
  3927. var node = FS.createNode(proc_self, 'fd', 16384 | 511 /* 0777 */, 73);
  3928. node.node_ops = {
  3929. lookup: function(parent, name) {
  3930. var fd = +name;
  3931. var stream = FS.getStream(fd);
  3932. if (!stream) throw new FS.ErrnoError(8);
  3933. var ret = {
  3934. parent: null,
  3935. mount: { mountpoint: 'fake' },
  3936. node_ops: { readlink: function() { return stream.path } }
  3937. };
  3938. ret.parent = ret; // make it look like a simple root node
  3939. return ret;
  3940. }
  3941. };
  3942. return node;
  3943. }
  3944. }, {}, '/proc/self/fd');
  3945. },createStandardStreams:function() {
  3946. // TODO deprecate the old functionality of a single
  3947. // input / output callback and that utilizes FS.createDevice
  3948. // and instead require a unique set of stream ops
  3949. // by default, we symlink the standard streams to the
  3950. // default tty devices. however, if the standard streams
  3951. // have been overwritten we create a unique device for
  3952. // them instead.
  3953. if (Module['stdin']) {
  3954. FS.createDevice('/dev', 'stdin', Module['stdin']);
  3955. } else {
  3956. FS.symlink('/dev/tty', '/dev/stdin');
  3957. }
  3958. if (Module['stdout']) {
  3959. FS.createDevice('/dev', 'stdout', null, Module['stdout']);
  3960. } else {
  3961. FS.symlink('/dev/tty', '/dev/stdout');
  3962. }
  3963. if (Module['stderr']) {
  3964. FS.createDevice('/dev', 'stderr', null, Module['stderr']);
  3965. } else {
  3966. FS.symlink('/dev/tty1', '/dev/stderr');
  3967. }
  3968. // open default streams for the stdin, stdout and stderr devices
  3969. var stdin = FS.open('/dev/stdin', 0);
  3970. var stdout = FS.open('/dev/stdout', 1);
  3971. var stderr = FS.open('/dev/stderr', 1);
  3972. },ensureErrnoError:function() {
  3973. if (FS.ErrnoError) return;
  3974. FS.ErrnoError = /** @this{Object} */ function ErrnoError(errno, node) {
  3975. this.node = node;
  3976. this.setErrno = /** @this{Object} */ function(errno) {
  3977. this.errno = errno;
  3978. };
  3979. this.setErrno(errno);
  3980. this.message = 'FS error';
  3981. };
  3982. FS.ErrnoError.prototype = new Error();
  3983. FS.ErrnoError.prototype.constructor = FS.ErrnoError;
  3984. // Some errors may happen quite a bit, to avoid overhead we reuse them (and suffer a lack of stack info)
  3985. [44].forEach(function(code) {
  3986. FS.genericErrors[code] = new FS.ErrnoError(code);
  3987. FS.genericErrors[code].stack = '<generic error, no stack>';
  3988. });
  3989. },staticInit:function() {
  3990. FS.ensureErrnoError();
  3991. FS.nameTable = new Array(4096);
  3992. FS.mount(MEMFS, {}, '/');
  3993. FS.createDefaultDirectories();
  3994. FS.createDefaultDevices();
  3995. FS.createSpecialDirectories();
  3996. FS.filesystems = {
  3997. 'MEMFS': MEMFS,
  3998. 'IDBFS': IDBFS,
  3999. };
  4000. },init:function(input, output, error) {
  4001. FS.init.initialized = true;
  4002. FS.ensureErrnoError();
  4003. // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here
  4004. Module['stdin'] = input || Module['stdin'];
  4005. Module['stdout'] = output || Module['stdout'];
  4006. Module['stderr'] = error || Module['stderr'];
  4007. FS.createStandardStreams();
  4008. },quit:function() {
  4009. FS.init.initialized = false;
  4010. // force-flush all streams, so we get musl std streams printed out
  4011. var fflush = Module['_fflush'];
  4012. if (fflush) fflush(0);
  4013. // close all of our streams
  4014. for (var i = 0; i < FS.streams.length; i++) {
  4015. var stream = FS.streams[i];
  4016. if (!stream) {
  4017. continue;
  4018. }
  4019. FS.close(stream);
  4020. }
  4021. },getMode:function(canRead, canWrite) {
  4022. var mode = 0;
  4023. if (canRead) mode |= 292 | 73;
  4024. if (canWrite) mode |= 146;
  4025. return mode;
  4026. },findObject:function(path, dontResolveLastLink) {
  4027. var ret = FS.analyzePath(path, dontResolveLastLink);
  4028. if (ret.exists) {
  4029. return ret.object;
  4030. } else {
  4031. return null;
  4032. }
  4033. },analyzePath:function(path, dontResolveLastLink) {
  4034. // operate from within the context of the symlink's target
  4035. try {
  4036. var lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
  4037. path = lookup.path;
  4038. } catch (e) {
  4039. }
  4040. var ret = {
  4041. isRoot: false, exists: false, error: 0, name: null, path: null, object: null,
  4042. parentExists: false, parentPath: null, parentObject: null
  4043. };
  4044. try {
  4045. var lookup = FS.lookupPath(path, { parent: true });
  4046. ret.parentExists = true;
  4047. ret.parentPath = lookup.path;
  4048. ret.parentObject = lookup.node;
  4049. ret.name = PATH.basename(path);
  4050. lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
  4051. ret.exists = true;
  4052. ret.path = lookup.path;
  4053. ret.object = lookup.node;
  4054. ret.name = lookup.node.name;
  4055. ret.isRoot = lookup.path === '/';
  4056. } catch (e) {
  4057. ret.error = e.errno;
  4058. };
  4059. return ret;
  4060. },createPath:function(parent, path, canRead, canWrite) {
  4061. parent = typeof parent === 'string' ? parent : FS.getPath(parent);
  4062. var parts = path.split('/').reverse();
  4063. while (parts.length) {
  4064. var part = parts.pop();
  4065. if (!part) continue;
  4066. var current = PATH.join2(parent, part);
  4067. try {
  4068. FS.mkdir(current);
  4069. } catch (e) {
  4070. // ignore EEXIST
  4071. }
  4072. parent = current;
  4073. }
  4074. return current;
  4075. },createFile:function(parent, name, properties, canRead, canWrite) {
  4076. var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name);
  4077. var mode = FS.getMode(canRead, canWrite);
  4078. return FS.create(path, mode);
  4079. },createDataFile:function(parent, name, data, canRead, canWrite, canOwn) {
  4080. var path = name ? PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name) : parent;
  4081. var mode = FS.getMode(canRead, canWrite);
  4082. var node = FS.create(path, mode);
  4083. if (data) {
  4084. if (typeof data === 'string') {
  4085. var arr = new Array(data.length);
  4086. for (var i = 0, len = data.length; i < len; ++i) arr[i] = data.charCodeAt(i);
  4087. data = arr;
  4088. }
  4089. // make sure we can write to the file
  4090. FS.chmod(node, mode | 146);
  4091. var stream = FS.open(node, 577);
  4092. FS.write(stream, data, 0, data.length, 0, canOwn);
  4093. FS.close(stream);
  4094. FS.chmod(node, mode);
  4095. }
  4096. return node;
  4097. },createDevice:function(parent, name, input, output) {
  4098. var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name);
  4099. var mode = FS.getMode(!!input, !!output);
  4100. if (!FS.createDevice.major) FS.createDevice.major = 64;
  4101. var dev = FS.makedev(FS.createDevice.major++, 0);
  4102. // Create a fake device that a set of stream ops to emulate
  4103. // the old behavior.
  4104. FS.registerDevice(dev, {
  4105. open: function(stream) {
  4106. stream.seekable = false;
  4107. },
  4108. close: function(stream) {
  4109. // flush any pending line data
  4110. if (output && output.buffer && output.buffer.length) {
  4111. output(10);
  4112. }
  4113. },
  4114. read: function(stream, buffer, offset, length, pos /* ignored */) {
  4115. var bytesRead = 0;
  4116. for (var i = 0; i < length; i++) {
  4117. var result;
  4118. try {
  4119. result = input();
  4120. } catch (e) {
  4121. throw new FS.ErrnoError(29);
  4122. }
  4123. if (result === undefined && bytesRead === 0) {
  4124. throw new FS.ErrnoError(6);
  4125. }
  4126. if (result === null || result === undefined) break;
  4127. bytesRead++;
  4128. buffer[offset+i] = result;
  4129. }
  4130. if (bytesRead) {
  4131. stream.node.timestamp = Date.now();
  4132. }
  4133. return bytesRead;
  4134. },
  4135. write: function(stream, buffer, offset, length, pos) {
  4136. for (var i = 0; i < length; i++) {
  4137. try {
  4138. output(buffer[offset+i]);
  4139. } catch (e) {
  4140. throw new FS.ErrnoError(29);
  4141. }
  4142. }
  4143. if (length) {
  4144. stream.node.timestamp = Date.now();
  4145. }
  4146. return i;
  4147. }
  4148. });
  4149. return FS.mkdev(path, mode, dev);
  4150. },forceLoadFile:function(obj) {
  4151. if (obj.isDevice || obj.isFolder || obj.link || obj.contents) return true;
  4152. if (typeof XMLHttpRequest !== 'undefined') {
  4153. throw new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.");
  4154. } else if (read_) {
  4155. // Command-line.
  4156. try {
  4157. // WARNING: Can't read binary files in V8's d8 or tracemonkey's js, as
  4158. // read() will try to parse UTF8.
  4159. obj.contents = intArrayFromString(read_(obj.url), true);
  4160. obj.usedBytes = obj.contents.length;
  4161. } catch (e) {
  4162. throw new FS.ErrnoError(29);
  4163. }
  4164. } else {
  4165. throw new Error('Cannot load without read() or XMLHttpRequest.');
  4166. }
  4167. },createLazyFile:function(parent, name, url, canRead, canWrite) {
  4168. // Lazy chunked Uint8Array (implements get and length from Uint8Array). Actual getting is abstracted away for eventual reuse.
  4169. /** @constructor */
  4170. function LazyUint8Array() {
  4171. this.lengthKnown = false;
  4172. this.chunks = []; // Loaded chunks. Index is the chunk number
  4173. }
  4174. LazyUint8Array.prototype.get = /** @this{Object} */ function LazyUint8Array_get(idx) {
  4175. if (idx > this.length-1 || idx < 0) {
  4176. return undefined;
  4177. }
  4178. var chunkOffset = idx % this.chunkSize;
  4179. var chunkNum = (idx / this.chunkSize)|0;
  4180. return this.getter(chunkNum)[chunkOffset];
  4181. };
  4182. LazyUint8Array.prototype.setDataGetter = function LazyUint8Array_setDataGetter(getter) {
  4183. this.getter = getter;
  4184. };
  4185. LazyUint8Array.prototype.cacheLength = function LazyUint8Array_cacheLength() {
  4186. // Find length
  4187. var xhr = new XMLHttpRequest();
  4188. xhr.open('HEAD', url, false);
  4189. xhr.send(null);
  4190. if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status);
  4191. var datalength = Number(xhr.getResponseHeader("Content-length"));
  4192. var header;
  4193. var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes";
  4194. var usesGzip = (header = xhr.getResponseHeader("Content-Encoding")) && header === "gzip";
  4195. var chunkSize = 1024*1024; // Chunk size in bytes
  4196. if (!hasByteServing) chunkSize = datalength;
  4197. // Function to get a range from the remote URL.
  4198. var doXHR = (function(from, to) {
  4199. if (from > to) throw new Error("invalid range (" + from + ", " + to + ") or no bytes requested!");
  4200. if (to > datalength-1) throw new Error("only " + datalength + " bytes available! programmer error!");
  4201. // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available.
  4202. var xhr = new XMLHttpRequest();
  4203. xhr.open('GET', url, false);
  4204. if (datalength !== chunkSize) xhr.setRequestHeader("Range", "bytes=" + from + "-" + to);
  4205. // Some hints to the browser that we want binary data.
  4206. if (typeof Uint8Array != 'undefined') xhr.responseType = 'arraybuffer';
  4207. if (xhr.overrideMimeType) {
  4208. xhr.overrideMimeType('text/plain; charset=x-user-defined');
  4209. }
  4210. xhr.send(null);
  4211. if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status);
  4212. if (xhr.response !== undefined) {
  4213. return new Uint8Array(/** @type{Array<number>} */(xhr.response || []));
  4214. } else {
  4215. return intArrayFromString(xhr.responseText || '', true);
  4216. }
  4217. });
  4218. var lazyArray = this;
  4219. lazyArray.setDataGetter(function(chunkNum) {
  4220. var start = chunkNum * chunkSize;
  4221. var end = (chunkNum+1) * chunkSize - 1; // including this byte
  4222. end = Math.min(end, datalength-1); // if datalength-1 is selected, this is the last block
  4223. if (typeof(lazyArray.chunks[chunkNum]) === "undefined") {
  4224. lazyArray.chunks[chunkNum] = doXHR(start, end);
  4225. }
  4226. if (typeof(lazyArray.chunks[chunkNum]) === "undefined") throw new Error("doXHR failed!");
  4227. return lazyArray.chunks[chunkNum];
  4228. });
  4229. if (usesGzip || !datalength) {
  4230. // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length
  4231. chunkSize = datalength = 1; // this will force getter(0)/doXHR do download the whole file
  4232. datalength = this.getter(0).length;
  4233. chunkSize = datalength;
  4234. out("LazyFiles on gzip forces download of the whole file when length is accessed");
  4235. }
  4236. this._length = datalength;
  4237. this._chunkSize = chunkSize;
  4238. this.lengthKnown = true;
  4239. };
  4240. if (typeof XMLHttpRequest !== 'undefined') {
  4241. if (!ENVIRONMENT_IS_WORKER) throw 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc';
  4242. var lazyArray = new LazyUint8Array();
  4243. Object.defineProperties(lazyArray, {
  4244. length: {
  4245. get: /** @this{Object} */ function() {
  4246. if (!this.lengthKnown) {
  4247. this.cacheLength();
  4248. }
  4249. return this._length;
  4250. }
  4251. },
  4252. chunkSize: {
  4253. get: /** @this{Object} */ function() {
  4254. if (!this.lengthKnown) {
  4255. this.cacheLength();
  4256. }
  4257. return this._chunkSize;
  4258. }
  4259. }
  4260. });
  4261. var properties = { isDevice: false, contents: lazyArray };
  4262. } else {
  4263. var properties = { isDevice: false, url: url };
  4264. }
  4265. var node = FS.createFile(parent, name, properties, canRead, canWrite);
  4266. // This is a total hack, but I want to get this lazy file code out of the
  4267. // core of MEMFS. If we want to keep this lazy file concept I feel it should
  4268. // be its own thin LAZYFS proxying calls to MEMFS.
  4269. if (properties.contents) {
  4270. node.contents = properties.contents;
  4271. } else if (properties.url) {
  4272. node.contents = null;
  4273. node.url = properties.url;
  4274. }
  4275. // Add a function that defers querying the file size until it is asked the first time.
  4276. Object.defineProperties(node, {
  4277. usedBytes: {
  4278. get: /** @this {FSNode} */ function() { return this.contents.length; }
  4279. }
  4280. });
  4281. // override each stream op with one that tries to force load the lazy file first
  4282. var stream_ops = {};
  4283. var keys = Object.keys(node.stream_ops);
  4284. keys.forEach(function(key) {
  4285. var fn = node.stream_ops[key];
  4286. stream_ops[key] = function forceLoadLazyFile() {
  4287. FS.forceLoadFile(node);
  4288. return fn.apply(null, arguments);
  4289. };
  4290. });
  4291. // use a custom read function
  4292. stream_ops.read = function stream_ops_read(stream, buffer, offset, length, position) {
  4293. FS.forceLoadFile(node);
  4294. var contents = stream.node.contents;
  4295. if (position >= contents.length)
  4296. return 0;
  4297. var size = Math.min(contents.length - position, length);
  4298. if (contents.slice) { // normal array
  4299. for (var i = 0; i < size; i++) {
  4300. buffer[offset + i] = contents[position + i];
  4301. }
  4302. } else {
  4303. for (var i = 0; i < size; i++) { // LazyUint8Array from sync binary XHR
  4304. buffer[offset + i] = contents.get(position + i);
  4305. }
  4306. }
  4307. return size;
  4308. };
  4309. node.stream_ops = stream_ops;
  4310. return node;
  4311. },createPreloadedFile:function(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) {
  4312. Browser.init(); // XXX perhaps this method should move onto Browser?
  4313. // TODO we should allow people to just pass in a complete filename instead
  4314. // of parent and name being that we just join them anyways
  4315. var fullname = name ? PATH_FS.resolve(PATH.join2(parent, name)) : parent;
  4316. var dep = getUniqueRunDependency('cp ' + fullname); // might have several active requests for the same fullname
  4317. function processData(byteArray) {
  4318. function finish(byteArray) {
  4319. if (preFinish) preFinish();
  4320. if (!dontCreateFile) {
  4321. FS.createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
  4322. }
  4323. if (onload) onload();
  4324. removeRunDependency(dep);
  4325. }
  4326. var handled = false;
  4327. Module['preloadPlugins'].forEach(function(plugin) {
  4328. if (handled) return;
  4329. if (plugin['canHandle'](fullname)) {
  4330. plugin['handle'](byteArray, fullname, finish, function() {
  4331. if (onerror) onerror();
  4332. removeRunDependency(dep);
  4333. });
  4334. handled = true;
  4335. }
  4336. });
  4337. if (!handled) finish(byteArray);
  4338. }
  4339. addRunDependency(dep);
  4340. if (typeof url == 'string') {
  4341. asyncLoad(url, function(byteArray) {
  4342. processData(byteArray);
  4343. }, onerror);
  4344. } else {
  4345. processData(url);
  4346. }
  4347. },indexedDB:function() {
  4348. return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
  4349. },DB_NAME:function() {
  4350. return 'EM_FS_' + window.location.pathname;
  4351. },DB_VERSION:20,DB_STORE_NAME:"FILE_DATA",saveFilesToDB:function(paths, onload, onerror) {
  4352. onload = onload || function(){};
  4353. onerror = onerror || function(){};
  4354. var indexedDB = FS.indexedDB();
  4355. try {
  4356. var openRequest = indexedDB.open(FS.DB_NAME(), FS.DB_VERSION);
  4357. } catch (e) {
  4358. return onerror(e);
  4359. }
  4360. openRequest.onupgradeneeded = function openRequest_onupgradeneeded() {
  4361. out('creating db');
  4362. var db = openRequest.result;
  4363. db.createObjectStore(FS.DB_STORE_NAME);
  4364. };
  4365. openRequest.onsuccess = function openRequest_onsuccess() {
  4366. var db = openRequest.result;
  4367. var transaction = db.transaction([FS.DB_STORE_NAME], 'readwrite');
  4368. var files = transaction.objectStore(FS.DB_STORE_NAME);
  4369. var ok = 0, fail = 0, total = paths.length;
  4370. function finish() {
  4371. if (fail == 0) onload(); else onerror();
  4372. }
  4373. paths.forEach(function(path) {
  4374. var putRequest = files.put(FS.analyzePath(path).object.contents, path);
  4375. putRequest.onsuccess = function putRequest_onsuccess() { ok++; if (ok + fail == total) finish() };
  4376. putRequest.onerror = function putRequest_onerror() { fail++; if (ok + fail == total) finish() };
  4377. });
  4378. transaction.onerror = onerror;
  4379. };
  4380. openRequest.onerror = onerror;
  4381. },loadFilesFromDB:function(paths, onload, onerror) {
  4382. onload = onload || function(){};
  4383. onerror = onerror || function(){};
  4384. var indexedDB = FS.indexedDB();
  4385. try {
  4386. var openRequest = indexedDB.open(FS.DB_NAME(), FS.DB_VERSION);
  4387. } catch (e) {
  4388. return onerror(e);
  4389. }
  4390. openRequest.onupgradeneeded = onerror; // no database to load from
  4391. openRequest.onsuccess = function openRequest_onsuccess() {
  4392. var db = openRequest.result;
  4393. try {
  4394. var transaction = db.transaction([FS.DB_STORE_NAME], 'readonly');
  4395. } catch(e) {
  4396. onerror(e);
  4397. return;
  4398. }
  4399. var files = transaction.objectStore(FS.DB_STORE_NAME);
  4400. var ok = 0, fail = 0, total = paths.length;
  4401. function finish() {
  4402. if (fail == 0) onload(); else onerror();
  4403. }
  4404. paths.forEach(function(path) {
  4405. var getRequest = files.get(path);
  4406. getRequest.onsuccess = function getRequest_onsuccess() {
  4407. if (FS.analyzePath(path).exists) {
  4408. FS.unlink(path);
  4409. }
  4410. FS.createDataFile(PATH.dirname(path), PATH.basename(path), getRequest.result, true, true, true);
  4411. ok++;
  4412. if (ok + fail == total) finish();
  4413. };
  4414. getRequest.onerror = function getRequest_onerror() { fail++; if (ok + fail == total) finish() };
  4415. });
  4416. transaction.onerror = onerror;
  4417. };
  4418. openRequest.onerror = onerror;
  4419. }};
  4420. var SYSCALLS = {mappings:{},DEFAULT_POLLMASK:5,umask:511,calculateAt:function(dirfd, path, allowEmpty) {
  4421. if (path[0] === '/') {
  4422. return path;
  4423. }
  4424. // relative path
  4425. var dir;
  4426. if (dirfd === -100) {
  4427. dir = FS.cwd();
  4428. } else {
  4429. var dirstream = FS.getStream(dirfd);
  4430. if (!dirstream) throw new FS.ErrnoError(8);
  4431. dir = dirstream.path;
  4432. }
  4433. if (path.length == 0) {
  4434. if (!allowEmpty) {
  4435. throw new FS.ErrnoError(44);;
  4436. }
  4437. return dir;
  4438. }
  4439. return PATH.join2(dir, path);
  4440. },doStat:function(func, path, buf) {
  4441. try {
  4442. var stat = func(path);
  4443. } catch (e) {
  4444. if (e && e.node && PATH.normalize(path) !== PATH.normalize(FS.getPath(e.node))) {
  4445. // an error occurred while trying to look up the path; we should just report ENOTDIR
  4446. return -54;
  4447. }
  4448. throw e;
  4449. }
  4450. HEAP32[((buf)>>2)] = stat.dev;
  4451. HEAP32[(((buf)+(4))>>2)] = 0;
  4452. HEAP32[(((buf)+(8))>>2)] = stat.ino;
  4453. HEAP32[(((buf)+(12))>>2)] = stat.mode;
  4454. HEAP32[(((buf)+(16))>>2)] = stat.nlink;
  4455. HEAP32[(((buf)+(20))>>2)] = stat.uid;
  4456. HEAP32[(((buf)+(24))>>2)] = stat.gid;
  4457. HEAP32[(((buf)+(28))>>2)] = stat.rdev;
  4458. HEAP32[(((buf)+(32))>>2)] = 0;
  4459. (tempI64 = [stat.size>>>0,(tempDouble=stat.size,(+(Math.abs(tempDouble))) >= 1.0 ? (tempDouble > 0.0 ? ((Math.min((+(Math.floor((tempDouble)/4294967296.0))), 4294967295.0))|0)>>>0 : (~~((+(Math.ceil((tempDouble - +(((~~(tempDouble)))>>>0))/4294967296.0)))))>>>0) : 0)],HEAP32[(((buf)+(40))>>2)] = tempI64[0],HEAP32[(((buf)+(44))>>2)] = tempI64[1]);
  4460. HEAP32[(((buf)+(48))>>2)] = 4096;
  4461. HEAP32[(((buf)+(52))>>2)] = stat.blocks;
  4462. HEAP32[(((buf)+(56))>>2)] = (stat.atime.getTime() / 1000)|0;
  4463. HEAP32[(((buf)+(60))>>2)] = 0;
  4464. HEAP32[(((buf)+(64))>>2)] = (stat.mtime.getTime() / 1000)|0;
  4465. HEAP32[(((buf)+(68))>>2)] = 0;
  4466. HEAP32[(((buf)+(72))>>2)] = (stat.ctime.getTime() / 1000)|0;
  4467. HEAP32[(((buf)+(76))>>2)] = 0;
  4468. (tempI64 = [stat.ino>>>0,(tempDouble=stat.ino,(+(Math.abs(tempDouble))) >= 1.0 ? (tempDouble > 0.0 ? ((Math.min((+(Math.floor((tempDouble)/4294967296.0))), 4294967295.0))|0)>>>0 : (~~((+(Math.ceil((tempDouble - +(((~~(tempDouble)))>>>0))/4294967296.0)))))>>>0) : 0)],HEAP32[(((buf)+(80))>>2)] = tempI64[0],HEAP32[(((buf)+(84))>>2)] = tempI64[1]);
  4469. return 0;
  4470. },doMsync:function(addr, stream, len, flags, offset) {
  4471. var buffer = HEAPU8.slice(addr, addr + len);
  4472. FS.msync(stream, buffer, offset, len, flags);
  4473. },doMkdir:function(path, mode) {
  4474. // remove a trailing slash, if one - /a/b/ has basename of '', but
  4475. // we want to create b in the context of this function
  4476. path = PATH.normalize(path);
  4477. if (path[path.length-1] === '/') path = path.substr(0, path.length-1);
  4478. FS.mkdir(path, mode, 0);
  4479. return 0;
  4480. },doMknod:function(path, mode, dev) {
  4481. // we don't want this in the JS API as it uses mknod to create all nodes.
  4482. switch (mode & 61440) {
  4483. case 32768:
  4484. case 8192:
  4485. case 24576:
  4486. case 4096:
  4487. case 49152:
  4488. break;
  4489. default: return -28;
  4490. }
  4491. FS.mknod(path, mode, dev);
  4492. return 0;
  4493. },doReadlink:function(path, buf, bufsize) {
  4494. if (bufsize <= 0) return -28;
  4495. var ret = FS.readlink(path);
  4496. var len = Math.min(bufsize, lengthBytesUTF8(ret));
  4497. var endChar = HEAP8[buf+len];
  4498. stringToUTF8(ret, buf, bufsize+1);
  4499. // readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!)
  4500. // stringToUTF8() always appends a null byte, so restore the character under the null byte after the write.
  4501. HEAP8[buf+len] = endChar;
  4502. return len;
  4503. },doAccess:function(path, amode) {
  4504. if (amode & ~7) {
  4505. // need a valid mode
  4506. return -28;
  4507. }
  4508. var node;
  4509. var lookup = FS.lookupPath(path, { follow: true });
  4510. node = lookup.node;
  4511. if (!node) {
  4512. return -44;
  4513. }
  4514. var perms = '';
  4515. if (amode & 4) perms += 'r';
  4516. if (amode & 2) perms += 'w';
  4517. if (amode & 1) perms += 'x';
  4518. if (perms /* otherwise, they've just passed F_OK */ && FS.nodePermissions(node, perms)) {
  4519. return -2;
  4520. }
  4521. return 0;
  4522. },doDup:function(path, flags, suggestFD) {
  4523. var suggest = FS.getStream(suggestFD);
  4524. if (suggest) FS.close(suggest);
  4525. return FS.open(path, flags, 0, suggestFD, suggestFD).fd;
  4526. },doReadv:function(stream, iov, iovcnt, offset) {
  4527. var ret = 0;
  4528. for (var i = 0; i < iovcnt; i++) {
  4529. var ptr = HEAP32[(((iov)+(i*8))>>2)];
  4530. var len = HEAP32[(((iov)+(i*8 + 4))>>2)];
  4531. var curr = FS.read(stream, HEAP8,ptr, len, offset);
  4532. if (curr < 0) return -1;
  4533. ret += curr;
  4534. if (curr < len) break; // nothing more to read
  4535. }
  4536. return ret;
  4537. },doWritev:function(stream, iov, iovcnt, offset) {
  4538. var ret = 0;
  4539. for (var i = 0; i < iovcnt; i++) {
  4540. var ptr = HEAP32[(((iov)+(i*8))>>2)];
  4541. var len = HEAP32[(((iov)+(i*8 + 4))>>2)];
  4542. var curr = FS.write(stream, HEAP8,ptr, len, offset);
  4543. if (curr < 0) return -1;
  4544. ret += curr;
  4545. }
  4546. return ret;
  4547. },varargs:undefined,get:function() {
  4548. SYSCALLS.varargs += 4;
  4549. var ret = HEAP32[(((SYSCALLS.varargs)-(4))>>2)];
  4550. return ret;
  4551. },getStr:function(ptr) {
  4552. var ret = UTF8ToString(ptr);
  4553. return ret;
  4554. },getStreamFromFD:function(fd) {
  4555. var stream = FS.getStream(fd);
  4556. if (!stream) throw new FS.ErrnoError(8);
  4557. return stream;
  4558. },get64:function(low, high) {
  4559. return low;
  4560. }};
  4561. function _fd_write(fd, iov, iovcnt, pnum) {try {
  4562. var stream = SYSCALLS.getStreamFromFD(fd);
  4563. var num = SYSCALLS.doWritev(stream, iov, iovcnt);
  4564. HEAP32[((pnum)>>2)] = num
  4565. return 0;
  4566. } catch (e) {
  4567. if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);
  4568. return e.errno;
  4569. }
  4570. }
  4571. function _setTempRet0(val) {
  4572. setTempRet0(val);
  4573. }
  4574. embind_init_charCodes();
  4575. BindingError = Module['BindingError'] = extendError(Error, 'BindingError');;
  4576. InternalError = Module['InternalError'] = extendError(Error, 'InternalError');;
  4577. init_emval();;
  4578. UnboundTypeError = Module['UnboundTypeError'] = extendError(Error, 'UnboundTypeError');;
  4579. var FSNode = /** @constructor */ function(parent, name, mode, rdev) {
  4580. if (!parent) {
  4581. parent = this; // root node sets parent to itself
  4582. }
  4583. this.parent = parent;
  4584. this.mount = parent.mount;
  4585. this.mounted = null;
  4586. this.id = FS.nextInode++;
  4587. this.name = name;
  4588. this.mode = mode;
  4589. this.node_ops = {};
  4590. this.stream_ops = {};
  4591. this.rdev = rdev;
  4592. };
  4593. var readMode = 292/*292*/ | 73/*73*/;
  4594. var writeMode = 146/*146*/;
  4595. Object.defineProperties(FSNode.prototype, {
  4596. read: {
  4597. get: /** @this{FSNode} */function() {
  4598. return (this.mode & readMode) === readMode;
  4599. },
  4600. set: /** @this{FSNode} */function(val) {
  4601. val ? this.mode |= readMode : this.mode &= ~readMode;
  4602. }
  4603. },
  4604. write: {
  4605. get: /** @this{FSNode} */function() {
  4606. return (this.mode & writeMode) === writeMode;
  4607. },
  4608. set: /** @this{FSNode} */function(val) {
  4609. val ? this.mode |= writeMode : this.mode &= ~writeMode;
  4610. }
  4611. },
  4612. isFolder: {
  4613. get: /** @this{FSNode} */function() {
  4614. return FS.isDir(this.mode);
  4615. }
  4616. },
  4617. isDevice: {
  4618. get: /** @this{FSNode} */function() {
  4619. return FS.isChrdev(this.mode);
  4620. }
  4621. }
  4622. });
  4623. FS.FSNode = FSNode;
  4624. FS.staticInit();Module["FS_createPath"] = FS.createPath;Module["FS_createDataFile"] = FS.createDataFile;Module["FS_createPreloadedFile"] = FS.createPreloadedFile;Module["FS_createLazyFile"] = FS.createLazyFile;Module["FS_createDevice"] = FS.createDevice;Module["FS_unlink"] = FS.unlink;;
  4625. var ASSERTIONS = false;
  4626. /** @type {function(string, boolean=, number=)} */
  4627. function intArrayFromString(stringy, dontAddNull, length) {
  4628. var len = length > 0 ? length : lengthBytesUTF8(stringy)+1;
  4629. var u8array = new Array(len);
  4630. var numBytesWritten = stringToUTF8Array(stringy, u8array, 0, u8array.length);
  4631. if (dontAddNull) u8array.length = numBytesWritten;
  4632. return u8array;
  4633. }
  4634. function intArrayToString(array) {
  4635. var ret = [];
  4636. for (var i = 0; i < array.length; i++) {
  4637. var chr = array[i];
  4638. if (chr > 0xFF) {
  4639. if (ASSERTIONS) {
  4640. assert(false, 'Character code ' + chr + ' (' + String.fromCharCode(chr) + ') at offset ' + i + ' not in 0x00-0xFF.');
  4641. }
  4642. chr &= 0xFF;
  4643. }
  4644. ret.push(String.fromCharCode(chr));
  4645. }
  4646. return ret.join('');
  4647. }
  4648. // Copied from https://github.com/strophe/strophejs/blob/e06d027/src/polyfills.js#L149
  4649. // This code was written by Tyler Akins and has been placed in the
  4650. // public domain. It would be nice if you left this header intact.
  4651. // Base64 code from Tyler Akins -- http://rumkin.com
  4652. /**
  4653. * Decodes a base64 string.
  4654. * @param {string} input The string to decode.
  4655. */
  4656. var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
  4657. var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  4658. var output = '';
  4659. var chr1, chr2, chr3;
  4660. var enc1, enc2, enc3, enc4;
  4661. var i = 0;
  4662. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  4663. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
  4664. do {
  4665. enc1 = keyStr.indexOf(input.charAt(i++));
  4666. enc2 = keyStr.indexOf(input.charAt(i++));
  4667. enc3 = keyStr.indexOf(input.charAt(i++));
  4668. enc4 = keyStr.indexOf(input.charAt(i++));
  4669. chr1 = (enc1 << 2) | (enc2 >> 4);
  4670. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  4671. chr3 = ((enc3 & 3) << 6) | enc4;
  4672. output = output + String.fromCharCode(chr1);
  4673. if (enc3 !== 64) {
  4674. output = output + String.fromCharCode(chr2);
  4675. }
  4676. if (enc4 !== 64) {
  4677. output = output + String.fromCharCode(chr3);
  4678. }
  4679. } while (i < input.length);
  4680. return output;
  4681. };
  4682. // Converts a string of base64 into a byte array.
  4683. // Throws error on invalid input.
  4684. function intArrayFromBase64(s) {
  4685. if (typeof ENVIRONMENT_IS_NODE === 'boolean' && ENVIRONMENT_IS_NODE) {
  4686. var buf = Buffer.from(s, 'base64');
  4687. return new Uint8Array(buf['buffer'], buf['byteOffset'], buf['byteLength']);
  4688. }
  4689. try {
  4690. var decoded = decodeBase64(s);
  4691. var bytes = new Uint8Array(decoded.length);
  4692. for (var i = 0 ; i < decoded.length ; ++i) {
  4693. bytes[i] = decoded.charCodeAt(i);
  4694. }
  4695. return bytes;
  4696. } catch (_) {
  4697. throw new Error('Converting base64 string to bytes failed.');
  4698. }
  4699. }
  4700. // If filename is a base64 data URI, parses and returns data (Buffer on node,
  4701. // Uint8Array otherwise). If filename is not a base64 data URI, returns undefined.
  4702. function tryParseAsDataURI(filename) {
  4703. if (!isDataURI(filename)) {
  4704. return;
  4705. }
  4706. return intArrayFromBase64(filename.slice(dataURIPrefix.length));
  4707. }
  4708. var asmLibraryArg = {
  4709. "__assert_fail": ___assert_fail,
  4710. "__cxa_allocate_exception": ___cxa_allocate_exception,
  4711. "__cxa_atexit": ___cxa_atexit,
  4712. "__cxa_throw": ___cxa_throw,
  4713. "_embind_register_bigint": __embind_register_bigint,
  4714. "_embind_register_bool": __embind_register_bool,
  4715. "_embind_register_emval": __embind_register_emval,
  4716. "_embind_register_float": __embind_register_float,
  4717. "_embind_register_function": __embind_register_function,
  4718. "_embind_register_integer": __embind_register_integer,
  4719. "_embind_register_memory_view": __embind_register_memory_view,
  4720. "_embind_register_std_string": __embind_register_std_string,
  4721. "_embind_register_std_wstring": __embind_register_std_wstring,
  4722. "_embind_register_void": __embind_register_void,
  4723. "abort": _abort,
  4724. "emscripten_memcpy_big": _emscripten_memcpy_big,
  4725. "emscripten_resize_heap": _emscripten_resize_heap,
  4726. "fd_write": _fd_write,
  4727. "setTempRet0": _setTempRet0
  4728. };
  4729. var asm = createWasm();
  4730. /** @type {function(...*):?} */
  4731. var ___wasm_call_ctors = Module["___wasm_call_ctors"] = function() {
  4732. return (___wasm_call_ctors = Module["___wasm_call_ctors"] = Module["asm"]["__wasm_call_ctors"]).apply(null, arguments);
  4733. };
  4734. /** @type {function(...*):?} */
  4735. var _openDecoder = Module["_openDecoder"] = function() {
  4736. return (_openDecoder = Module["_openDecoder"] = Module["asm"]["openDecoder"]).apply(null, arguments);
  4737. };
  4738. /** @type {function(...*):?} */
  4739. var _decodeData = Module["_decodeData"] = function() {
  4740. return (_decodeData = Module["_decodeData"] = Module["asm"]["decodeData"]).apply(null, arguments);
  4741. };
  4742. /** @type {function(...*):?} */
  4743. var _malloc = Module["_malloc"] = function() {
  4744. return (_malloc = Module["_malloc"] = Module["asm"]["malloc"]).apply(null, arguments);
  4745. };
  4746. /** @type {function(...*):?} */
  4747. var ___getTypeName = Module["___getTypeName"] = function() {
  4748. return (___getTypeName = Module["___getTypeName"] = Module["asm"]["__getTypeName"]).apply(null, arguments);
  4749. };
  4750. /** @type {function(...*):?} */
  4751. var ___embind_register_native_and_builtin_types = Module["___embind_register_native_and_builtin_types"] = function() {
  4752. return (___embind_register_native_and_builtin_types = Module["___embind_register_native_and_builtin_types"] = Module["asm"]["__embind_register_native_and_builtin_types"]).apply(null, arguments);
  4753. };
  4754. /** @type {function(...*):?} */
  4755. var ___errno_location = Module["___errno_location"] = function() {
  4756. return (___errno_location = Module["___errno_location"] = Module["asm"]["__errno_location"]).apply(null, arguments);
  4757. };
  4758. /** @type {function(...*):?} */
  4759. var stackSave = Module["stackSave"] = function() {
  4760. return (stackSave = Module["stackSave"] = Module["asm"]["stackSave"]).apply(null, arguments);
  4761. };
  4762. /** @type {function(...*):?} */
  4763. var stackRestore = Module["stackRestore"] = function() {
  4764. return (stackRestore = Module["stackRestore"] = Module["asm"]["stackRestore"]).apply(null, arguments);
  4765. };
  4766. /** @type {function(...*):?} */
  4767. var stackAlloc = Module["stackAlloc"] = function() {
  4768. return (stackAlloc = Module["stackAlloc"] = Module["asm"]["stackAlloc"]).apply(null, arguments);
  4769. };
  4770. /** @type {function(...*):?} */
  4771. var _free = Module["_free"] = function() {
  4772. return (_free = Module["_free"] = Module["asm"]["free"]).apply(null, arguments);
  4773. };
  4774. /** @type {function(...*):?} */
  4775. var dynCall_jiji = Module["dynCall_jiji"] = function() {
  4776. return (dynCall_jiji = Module["dynCall_jiji"] = Module["asm"]["dynCall_jiji"]).apply(null, arguments);
  4777. };
  4778. // === Auto-generated postamble setup entry stuff ===
  4779. Module["setValue"] = setValue;
  4780. Module["getValue"] = getValue;
  4781. Module["addRunDependency"] = addRunDependency;
  4782. Module["removeRunDependency"] = removeRunDependency;
  4783. Module["FS_createPath"] = FS.createPath;
  4784. Module["FS_createDataFile"] = FS.createDataFile;
  4785. Module["FS_createPreloadedFile"] = FS.createPreloadedFile;
  4786. Module["FS_createLazyFile"] = FS.createLazyFile;
  4787. Module["FS_createDevice"] = FS.createDevice;
  4788. Module["FS_unlink"] = FS.unlink;
  4789. Module["addFunction"] = addFunction;
  4790. var calledRun;
  4791. /**
  4792. * @constructor
  4793. * @this {ExitStatus}
  4794. */
  4795. function ExitStatus(status) {
  4796. this.name = "ExitStatus";
  4797. this.message = "Program terminated with exit(" + status + ")";
  4798. this.status = status;
  4799. }
  4800. var calledMain = false;
  4801. dependenciesFulfilled = function runCaller() {
  4802. // If run has never been called, and we should call run (INVOKE_RUN is true, and Module.noInitialRun is not false)
  4803. if (!calledRun) run();
  4804. if (!calledRun) dependenciesFulfilled = runCaller; // try this again later, after new deps are fulfilled
  4805. };
  4806. /** @type {function(Array=)} */
  4807. function run(args) {
  4808. args = args || arguments_;
  4809. if (runDependencies > 0) {
  4810. return;
  4811. }
  4812. preRun();
  4813. // a preRun added a dependency, run will be called later
  4814. if (runDependencies > 0) {
  4815. return;
  4816. }
  4817. function doRun() {
  4818. // run may have just been called through dependencies being fulfilled just in this very frame,
  4819. // or while the async setStatus time below was happening
  4820. if (calledRun) return;
  4821. calledRun = true;
  4822. Module['calledRun'] = true;
  4823. if (ABORT) return;
  4824. initRuntime();
  4825. if (Module['onRuntimeInitialized']) Module['onRuntimeInitialized']();
  4826. postRun();
  4827. }
  4828. if (Module['setStatus']) {
  4829. Module['setStatus']('Running...');
  4830. setTimeout(function() {
  4831. setTimeout(function() {
  4832. Module['setStatus']('');
  4833. }, 1);
  4834. doRun();
  4835. }, 1);
  4836. } else
  4837. {
  4838. doRun();
  4839. }
  4840. }
  4841. Module['run'] = run;
  4842. /** @param {boolean|number=} implicit */
  4843. function exit(status, implicit) {
  4844. EXITSTATUS = status;
  4845. if (keepRuntimeAlive()) {
  4846. } else {
  4847. exitRuntime();
  4848. }
  4849. procExit(status);
  4850. }
  4851. function procExit(code) {
  4852. EXITSTATUS = code;
  4853. if (!keepRuntimeAlive()) {
  4854. if (Module['onExit']) Module['onExit'](code);
  4855. ABORT = true;
  4856. }
  4857. quit_(code, new ExitStatus(code));
  4858. }
  4859. if (Module['preInit']) {
  4860. if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
  4861. while (Module['preInit'].length > 0) {
  4862. Module['preInit'].pop()();
  4863. }
  4864. }
  4865. run();