12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022 |
- /*
- * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
- * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
- * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
- * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "Namespaces.h"
- #include <AK/LexicalPath.h>
- #include <AK/Queue.h>
- #include <AK/QuickSort.h>
- #include <LibIDL/Types.h>
- Vector<StringView> s_header_search_paths;
- namespace IDL {
- // FIXME: Generate this automatically somehow.
- static bool is_platform_object(Type const& type)
- {
- // NOTE: This is a hand-curated subset of platform object types that are actually relevant
- // in places where this function is used. If you add IDL code and get compile errors, you
- // might simply need to add another type here.
- static constexpr Array types = {
- "AbortSignal"sv,
- "Attr"sv,
- "AudioTrack"sv,
- "Blob"sv,
- "CanvasGradient"sv,
- "CanvasPattern"sv,
- "CanvasRenderingContext2D"sv,
- "Document"sv,
- "DocumentType"sv,
- "EventTarget"sv,
- "FileList"sv,
- "FormData"sv,
- "ImageData"sv,
- "Instance"sv,
- "IntersectionObserverEntry"sv,
- "Memory"sv,
- "Module"sv,
- "MutationRecord"sv,
- "NamedNodeMap"sv,
- "Node"sv,
- "Path2D"sv,
- "PerformanceEntry"sv,
- "PerformanceMark"sv,
- "ReadableStreamBYOBReader"sv,
- "ReadableStreamDefaultReader"sv,
- "Range"sv,
- "ReadableStream"sv,
- "Request"sv,
- "Selection"sv,
- "Table"sv,
- "Text"sv,
- "TextMetrics"sv,
- "URLSearchParams"sv,
- "VideoTrack"sv,
- "VideoTrackList"sv,
- "WebGLRenderingContext"sv,
- "Window"sv,
- "WritableStream"sv,
- };
- if (type.name().ends_with("Element"sv))
- return true;
- if (type.name().ends_with("Event"sv))
- return true;
- if (types.span().contains_slow(type.name()))
- return true;
- return false;
- }
- // FIXME: Generate this automatically somehow.
- static bool is_javascript_builtin(Type const& type)
- {
- // NOTE: This is a hand-curated subset of JavaScript built-in types that are actually relevant
- // in places where this function is used. If you add IDL code and get compile errors, you
- // might simply need to add another type here.
- static constexpr Array types = {
- "ArrayBuffer"sv,
- };
- return types.span().contains_slow(type.name());
- }
- static StringView sequence_storage_type_to_cpp_storage_type_name(SequenceStorageType sequence_storage_type)
- {
- switch (sequence_storage_type) {
- case SequenceStorageType::Vector:
- return "Vector"sv;
- case SequenceStorageType::MarkedVector:
- return "JS::MarkedVector"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface);
- static DeprecatedString union_type_to_variant(UnionType const& union_type, Interface const& interface)
- {
- StringBuilder builder;
- builder.append("Variant<"sv);
- auto flattened_types = union_type.flattened_member_types();
- for (size_t type_index = 0; type_index < flattened_types.size(); ++type_index) {
- auto& type = flattened_types.at(type_index);
- if (type_index > 0)
- builder.append(", "sv);
- auto cpp_type = idl_type_name_to_cpp_type(type, interface);
- builder.append(cpp_type.name);
- }
- if (union_type.includes_undefined())
- builder.append(", Empty"sv);
- builder.append('>');
- return builder.to_deprecated_string();
- }
- CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface)
- {
- if (is_platform_object(type))
- return { .name = DeprecatedString::formatted("JS::Handle<{}>", type.name()), .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (interface.callback_functions.contains(type.name()))
- return { .name = "JS::Handle<WebIDL::CallbackType>", .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (type.is_string()) {
- if (interface.extended_attributes.contains("UseNewAKString"))
- return { .name = "String", .sequence_storage_type = SequenceStorageType::Vector };
- return { .name = "DeprecatedString", .sequence_storage_type = SequenceStorageType::Vector };
- }
- if (type.name() == "double" && !type.is_nullable())
- return { .name = "double", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "float" && !type.is_nullable())
- return { .name = "float", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "boolean" && !type.is_nullable())
- return { .name = "bool", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "unsigned long" && !type.is_nullable())
- return { .name = "u32", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "unsigned short" && !type.is_nullable())
- return { .name = "u16", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "long long" && !type.is_nullable())
- return { .name = "i64", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "unsigned long long" && !type.is_nullable())
- return { .name = "u64", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "long" && !type.is_nullable())
- return { .name = "i32", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "any" || type.name() == "undefined")
- return { .name = "JS::Value", .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (type.name() == "object")
- return { .name = "JS::Handle<JS::Object>", .sequence_storage_type = SequenceStorageType::Vector };
- if (type.name() == "BufferSource")
- return { .name = "JS::Handle<JS::Object>", .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (type.name() == "File")
- return { .name = "JS::Handle<FileAPI::File>", .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (type.name() == "Function")
- return { .name = "JS::Handle<WebIDL::CallbackType>", .sequence_storage_type = SequenceStorageType::MarkedVector };
- if (type.name() == "sequence") {
- auto& parameterized_type = verify_cast<ParameterizedType>(type);
- auto& sequence_type = parameterized_type.parameters().first();
- auto sequence_cpp_type = idl_type_name_to_cpp_type(sequence_type, interface);
- auto storage_type_name = sequence_storage_type_to_cpp_storage_type_name(sequence_cpp_type.sequence_storage_type);
- if (sequence_cpp_type.sequence_storage_type == SequenceStorageType::MarkedVector)
- return { .name = storage_type_name, .sequence_storage_type = SequenceStorageType::Vector };
- return { .name = DeprecatedString::formatted("{}<{}>", storage_type_name, sequence_cpp_type.name), .sequence_storage_type = SequenceStorageType::Vector };
- }
- if (type.name() == "record") {
- auto& parameterized_type = verify_cast<ParameterizedType>(type);
- auto& record_key_type = parameterized_type.parameters()[0];
- auto& record_value_type = parameterized_type.parameters()[1];
- auto record_key_cpp_type = idl_type_name_to_cpp_type(record_key_type, interface);
- auto record_value_cpp_type = idl_type_name_to_cpp_type(record_value_type, interface);
- return { .name = DeprecatedString::formatted("OrderedHashMap<{}, {}>", record_key_cpp_type.name, record_value_cpp_type.name), .sequence_storage_type = SequenceStorageType::Vector };
- }
- if (is<UnionType>(type)) {
- auto& union_type = verify_cast<UnionType>(type);
- return { .name = union_type_to_variant(union_type, interface), .sequence_storage_type = SequenceStorageType::Vector };
- }
- if (!type.is_nullable()) {
- for (auto& dictionary : interface.dictionaries) {
- if (type.name() == dictionary.key)
- return { .name = type.name(), .sequence_storage_type = SequenceStorageType::Vector };
- }
- }
- dbgln("Unimplemented type for idl_type_name_to_cpp_type: {}{}", type.name(), type.is_nullable() ? "?" : "");
- TODO();
- }
- static DeprecatedString make_input_acceptable_cpp(DeprecatedString const& input)
- {
- if (input.is_one_of("class", "template", "for", "default", "char", "namespace", "delete", "inline")) {
- StringBuilder builder;
- builder.append(input);
- builder.append('_');
- return builder.to_deprecated_string();
- }
- return input.replace("-"sv, "_"sv, ReplaceMode::All);
- }
- static void generate_include_for_iterator(auto& generator, auto& iterator_path)
- {
- auto iterator_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- iterator_generator.set("iterator_class.path", iterator_path);
- // FIXME: These may or may not exist, because REASONS.
- iterator_generator.append(R"~~~(
- //#if __has_include(<LibWeb/@iterator_class.path@.h>)
- # include <LibWeb/@iterator_class.path@.h>
- //#endif
- )~~~");
- }
- static void generate_include_for(auto& generator, auto& path)
- {
- auto forked_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- auto path_string = path;
- for (auto& search_path : s_header_search_paths) {
- if (!path.starts_with(search_path))
- continue;
- auto relative_path = LexicalPath::relative_path(path, search_path);
- if (relative_path.length() < path_string.length())
- path_string = relative_path;
- }
- LexicalPath include_path { path_string };
- forked_generator.set("include.path", DeprecatedString::formatted("{}/{}.h", include_path.dirname(), include_path.title()));
- forked_generator.append(R"~~~(
- #include <@include.path@>
- )~~~");
- }
- static void emit_includes_for_all_imports(auto& interface, auto& generator, bool is_iterator = false)
- {
- Queue<RemoveCVReference<decltype(interface)> const*> interfaces;
- HashTable<DeprecatedString> paths_imported;
- interfaces.enqueue(&interface);
- while (!interfaces.is_empty()) {
- auto interface = interfaces.dequeue();
- if (paths_imported.contains(interface->module_own_path))
- continue;
- paths_imported.set(interface->module_own_path);
- for (auto& imported_interface : interface->imported_modules) {
- if (!paths_imported.contains(imported_interface.module_own_path))
- interfaces.enqueue(&imported_interface);
- }
- if (!interface->will_generate_code())
- continue;
- generate_include_for(generator, interface->module_own_path);
- }
- if (is_iterator) {
- auto iterator_path = DeprecatedString::formatted("{}Iterator", interface.fully_qualified_name.replace("::"sv, "/"sv, ReplaceMode::All));
- generate_include_for_iterator(generator, iterator_path);
- }
- }
- template<typename ParameterType>
- static void generate_to_deprecated_string(SourceGenerator& scoped_generator, ParameterType const& parameter, bool variadic, bool optional, Optional<DeprecatedString> const& optional_default_value)
- {
- if (variadic) {
- scoped_generator.append(R"~~~(
- Vector<DeprecatedString> @cpp_name@;
- if (vm.argument_count() > @js_suffix@) {
- TRY_OR_THROW_OOM(vm, @cpp_name@.try_ensure_capacity(vm.argument_count() - @js_suffix@));
- for (size_t i = @js_suffix@; i < vm.argument_count(); ++i) {
- auto to_string_result = TRY(vm.argument(i).to_deprecated_string(vm));
- @cpp_name@.unchecked_append(move(to_string_result));
- }
- }
- )~~~");
- } else if (!optional) {
- if (!parameter.type->is_nullable()) {
- scoped_generator.append(R"~~~(
- DeprecatedString @cpp_name@;
- if (@js_name@@js_suffix@.is_null() && @legacy_null_to_empty_string@) {
- @cpp_name@ = DeprecatedString::empty();
- } else {
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_deprecated_string(vm));
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- DeprecatedString @cpp_name@;
- if (!@js_name@@js_suffix@.is_nullish())
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_deprecated_string(vm));
- )~~~");
- }
- } else {
- scoped_generator.append(R"~~~(
- DeprecatedString @cpp_name@;
- if (!@js_name@@js_suffix@.is_undefined()) {
- if (@js_name@@js_suffix@.is_null() && @legacy_null_to_empty_string@)
- @cpp_name@ = DeprecatedString::empty();
- else
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_deprecated_string(vm));
- })~~~");
- if (optional_default_value.has_value() && (!parameter.type->is_nullable() || optional_default_value.value() != "null")) {
- scoped_generator.append(R"~~~( else {
- @cpp_name@ = @parameter.optional_default_value@;
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- )~~~");
- }
- }
- }
- template<typename ParameterType>
- static void generate_to_new_string(SourceGenerator& scoped_generator, ParameterType const& parameter, bool variadic, bool optional, Optional<DeprecatedString> const& optional_default_value)
- {
- if (variadic) {
- scoped_generator.append(R"~~~(
- Vector<String> @cpp_name@;
- if (vm.argument_count() > @js_suffix@) {
- TRY_OR_THROW_OOM(vm, @cpp_name@.try_ensure_capacity(vm.argument_count() - @js_suffix@));
- for (size_t i = @js_suffix@; i < vm.argument_count(); ++i) {
- auto to_string_result = TRY(vm.argument(i).to_string(vm));
- @cpp_name@.unchecked_append(move(to_string_result));
- }
- }
- )~~~");
- } else if (!optional) {
- if (!parameter.type->is_nullable()) {
- scoped_generator.append(R"~~~(
- String @cpp_name@;
- if (!@legacy_null_to_empty_string@ || !@js_name@@js_suffix@.is_null()) {
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<String> @cpp_name@;
- if (!@js_name@@js_suffix@.is_nullish())
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
- )~~~");
- }
- } else {
- scoped_generator.append(R"~~~(
- Optional<String> @cpp_name@;
- if (!@js_name@@js_suffix@.is_undefined()) {
- if (!@legacy_null_to_empty_string@ || !@js_name@@js_suffix@.is_null())
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
- })~~~");
- if (optional_default_value.has_value() && (!parameter.type->is_nullable() || optional_default_value.value() != "null")) {
- scoped_generator.append(R"~~~( else {
- @cpp_name@ = TRY_OR_THROW_OOM(vm, String::from_utf8(@parameter.optional_default_value@sv));
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- )~~~");
- }
- }
- }
- template<typename ParameterType>
- static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter, DeprecatedString const& js_name, DeprecatedString const& js_suffix, DeprecatedString const& cpp_name, IDL::Interface const& interface, bool legacy_null_to_empty_string = false, bool optional = false, Optional<DeprecatedString> optional_default_value = {}, bool variadic = false, size_t recursion_depth = 0)
- {
- auto scoped_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- auto acceptable_cpp_name = make_input_acceptable_cpp(cpp_name);
- scoped_generator.set("cpp_name", acceptable_cpp_name);
- scoped_generator.set("js_name", js_name);
- scoped_generator.set("js_suffix", js_suffix);
- scoped_generator.set("legacy_null_to_empty_string", legacy_null_to_empty_string ? "true" : "false");
- scoped_generator.set("parameter.type.name", parameter.type->name());
- if (optional_default_value.has_value())
- scoped_generator.set("parameter.optional_default_value", *optional_default_value);
- // FIXME: Add support for optional, variadic, nullable and default values to all types
- if (parameter.type->is_string()) {
- bool use_new_ak_string = interface.extended_attributes.contains("UseNewAKString");
- if (!use_new_ak_string)
- generate_to_deprecated_string(scoped_generator, parameter, variadic, optional, optional_default_value);
- else
- generate_to_new_string(scoped_generator, parameter, variadic, optional, optional_default_value);
- } else if (parameter.type->name().is_one_of("EventListener", "NodeFilter")) {
- // FIXME: Replace this with support for callback interfaces. https://webidl.spec.whatwg.org/#idl-callback-interface
- if (parameter.type->name() == "EventListener")
- scoped_generator.set("cpp_type", "IDLEventListener");
- else
- scoped_generator.set("cpp_type", parameter.type->name());
- if (parameter.type->is_nullable()) {
- scoped_generator.append(R"~~~(
- @cpp_type@* @cpp_name@ = nullptr;
- if (!@js_name@@js_suffix@.is_nullish()) {
- if (!@js_name@@js_suffix@.is_object())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- auto callback_type = vm.heap().allocate_without_realm<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_settings_object());
- @cpp_name@ = TRY(throw_dom_exception_if_needed(vm, [&] { return @cpp_type@::create(realm, *callback_type); }));
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- auto callback_type = vm.heap().allocate_without_realm<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_settings_object());
- auto @cpp_name@ = adopt_ref(*new @cpp_type@(callback_type));
- )~~~");
- }
- } else if (IDL::is_platform_object(*parameter.type)) {
- if (!parameter.type->is_nullable()) {
- if (!optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object() || !is<@parameter.type.name@>(@js_name@@js_suffix@.as_object()))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
- auto& @cpp_name@ = static_cast<@parameter.type.name@&>(@js_name@@js_suffix@.as_object());
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<JS::NonnullGCPtr<@parameter.type.name@>> @cpp_name@;
- if (!@js_name@@js_suffix@.is_undefined()) {
- if (!@js_name@@js_suffix@.is_object() || !is<@parameter.type.name@>(@js_name@@js_suffix@.as_object()))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
- @cpp_name@ = static_cast<@parameter.type.name@&>(@js_name@@js_suffix@.as_object());
- }
- )~~~");
- }
- } else {
- scoped_generator.append(R"~~~(
- @parameter.type.name@* @cpp_name@ = nullptr;
- if (!@js_name@@js_suffix@.is_nullish()) {
- if (!@js_name@@js_suffix@.is_object() || !is<@parameter.type.name@>(@js_name@@js_suffix@.as_object()))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
- @cpp_name@ = &static_cast<@parameter.type.name@&>(@js_name@@js_suffix@.as_object());
- }
- )~~~");
- }
- } else if (parameter.type->name() == "double" || parameter.type->name() == "float") {
- if (!optional) {
- scoped_generator.append(R"~~~(
- @parameter.type.name@ @cpp_name@ = TRY(@js_name@@js_suffix@.to_double(vm));
- )~~~");
- } else {
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- @parameter.type.name@ @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<@parameter.type.name@> @cpp_name@;
- )~~~");
- }
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_double(vm));
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- )~~~");
- }
- }
- } else if (parameter.type->name() == "boolean") {
- if (!optional || optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- bool @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<bool> @cpp_name@;
- )~~~");
- }
- if (optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- )~~~");
- }
- scoped_generator.append(R"~~~(
- @cpp_name@ = @js_name@@js_suffix@.to_boolean();
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@;
- )~~~");
- }
- } else if (parameter.type->name() == "unsigned long") {
- if (!optional || optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- u32 @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<u32> @cpp_name@;
- )~~~");
- }
- if (optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- )~~~");
- }
- scoped_generator.append(R"~~~(
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_u32(vm));
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@UL;
- )~~~");
- }
- } else if (parameter.type->name() == "unsigned short") {
- if (!optional || optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- u16 @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<u16> @cpp_name@;
- )~~~");
- }
- if (optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- )~~~");
- }
- scoped_generator.append(R"~~~(
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_u16(vm));
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@;
- )~~~");
- }
- } else if (parameter.type->name() == "long") {
- if (!optional || optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- i32 @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<i32> @cpp_name@;
- )~~~");
- }
- if (optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- )~~~");
- }
- scoped_generator.append(R"~~~(
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_i32(vm));
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@L;
- )~~~");
- }
- } else if (parameter.type->name() == "long long") {
- if (!optional || optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- i64 @cpp_name@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- Optional<i64> @cpp_name@;
- )~~~");
- }
- if (optional) {
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined())
- )~~~");
- }
- scoped_generator.append(R"~~~(
- @cpp_name@ = TRY(@js_name@@js_suffix@.to_bigint_int64(vm));
- )~~~");
- if (optional_default_value.has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = @parameter.optional_default_value@L;
- )~~~");
- }
- } else if (parameter.type->name() == "Promise") {
- // NOTE: It's not clear to me where the implicit wrapping of non-Promise values in a resolved
- // Promise is defined in the spec; https://webidl.spec.whatwg.org/#idl-promise doesn't say
- // anything of this sort. Both Gecko and Blink do it, however, so I'm sure it's correct.
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object() || !is<JS::Promise>(@js_name@@js_suffix@.as_object())) {
- auto new_promise = JS::Promise::create(realm);
- new_promise->fulfill(@js_name@@js_suffix@);
- @js_name@@js_suffix@ = new_promise;
- }
- auto @cpp_name@ = JS::make_handle(&static_cast<JS::Promise&>(@js_name@@js_suffix@.as_object()));
- )~~~");
- } else if (parameter.type->name() == "object") {
- if (optional) {
- scoped_generator.append(R"~~~(
- Optional<JS::Handle<JS::Object>> @cpp_name@;
- if (!@js_name@@js_suffix@.is_undefined())
- @cpp_name@ = JS::make_handle(TRY(@js_name@@js_suffix@.to_object(vm)));
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- auto @cpp_name@ = JS::make_handle(TRY(@js_name@@js_suffix@.to_object(vm)));
- )~~~");
- }
- } else if (parameter.type->name() == "BufferSource") {
- if (optional) {
- scoped_generator.append(R"~~~(
- Optional<JS::Handle<JS::Object>> @cpp_name@;
- if (!@js_name@@js_suffix@.is_undefined()) {
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- JS::Handle<JS::Object> @cpp_name@;
- )~~~");
- }
- scoped_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object() || !(is<JS::TypedArrayBase>(@js_name@@js_suffix@.as_object()) || is<JS::ArrayBuffer>(@js_name@@js_suffix@.as_object()) || is<JS::DataView>(@js_name@@js_suffix@.as_object())))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
- // TODO: Should we make this a Variant?
- @cpp_name@ = JS::make_handle(&@js_name@@js_suffix@.as_object());
- )~~~");
- if (optional) {
- scoped_generator.append(R"~~~(
- }
- )~~~");
- }
- } else if (parameter.type->name() == "any") {
- if (variadic) {
- scoped_generator.append(R"~~~(
- JS::MarkedVector<JS::Value> @cpp_name@ { vm.heap() };
- if (vm.argument_count() > @js_suffix@) {
- TRY_OR_THROW_OOM(vm, @cpp_name@.try_ensure_capacity(vm.argument_count() - @js_suffix@));
- for (size_t i = @js_suffix@; i < vm.argument_count(); ++i)
- @cpp_name@.unchecked_append(vm.argument(i));
- }
- )~~~");
- } else if (!optional) {
- scoped_generator.append(R"~~~(
- auto @cpp_name@ = @js_name@@js_suffix@;
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- JS::Value @cpp_name@ = JS::js_undefined();
- if (!@js_name@@js_suffix@.is_undefined())
- @cpp_name@ = @js_name@@js_suffix@;
- )~~~");
- if (optional_default_value.has_value()) {
- if (optional_default_value == "null") {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = JS::js_null();
- )~~~");
- } else if (optional_default_value->to_int().has_value() || optional_default_value->to_uint().has_value()) {
- scoped_generator.append(R"~~~(
- else
- @cpp_name@ = JS::Value(@parameter.optional_default_value@);
- )~~~");
- } else {
- TODO();
- }
- }
- }
- } else if (interface.enumerations.contains(parameter.type->name())) {
- auto enum_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto& enumeration = interface.enumerations.find(parameter.type->name())->value;
- StringView enum_member_name;
- if (optional_default_value.has_value()) {
- VERIFY(optional_default_value->length() >= 2 && (*optional_default_value)[0] == '"' && (*optional_default_value)[optional_default_value->length() - 1] == '"');
- enum_member_name = optional_default_value->substring_view(1, optional_default_value->length() - 2);
- } else {
- enum_member_name = enumeration.first_member;
- }
- auto default_value_cpp_name = enumeration.translated_cpp_names.get(enum_member_name);
- VERIFY(default_value_cpp_name.has_value());
- enum_generator.set("enum.default.cpp_value", *default_value_cpp_name);
- enum_generator.set("js_name.as_string", DeprecatedString::formatted("{}{}_string", enum_generator.get("js_name"sv), enum_generator.get("js_suffix"sv)));
- enum_generator.append(R"~~~(
- @parameter.type.name@ @cpp_name@ { @parameter.type.name@::@enum.default.cpp_value@ };
- )~~~");
- if (optional) {
- enum_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined()) {
- )~~~");
- }
- enum_generator.append(R"~~~(
- auto @js_name.as_string@ = TRY(@js_name@@js_suffix@.to_string(vm));
- )~~~");
- auto first = true;
- VERIFY(enumeration.translated_cpp_names.size() >= 1);
- for (auto& it : enumeration.translated_cpp_names) {
- enum_generator.set("enum.alt.name", it.key);
- enum_generator.set("enum.alt.value", it.value);
- enum_generator.set("else", first ? "" : "else ");
- first = false;
- enum_generator.append(R"~~~(
- @else@if (@js_name.as_string@ == "@enum.alt.name@"sv)
- @cpp_name@ = @parameter.type.name@::@enum.alt.value@;
- )~~~");
- }
- // NOTE: Attribute setters return undefined instead of throwing when the string doesn't match an enum value.
- if constexpr (!IsSame<Attribute, RemoveConst<ParameterType>>) {
- enum_generator.append(R"~~~(
- else
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidEnumerationValue, @js_name.as_string@, "@parameter.type.name@");
- )~~~");
- } else {
- enum_generator.append(R"~~~(
- else
- return JS::js_undefined();
- )~~~");
- }
- if (optional) {
- enum_generator.append(R"~~~(
- }
- )~~~");
- }
- } else if (interface.dictionaries.contains(parameter.type->name())) {
- if (optional_default_value.has_value() && optional_default_value != "{}")
- TODO();
- auto dictionary_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- dictionary_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_nullish() && !@js_name@@js_suffix@.is_object())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
- @parameter.type.name@ @cpp_name@ {};
- )~~~");
- auto* current_dictionary = &interface.dictionaries.find(parameter.type->name())->value;
- // FIXME: This (i) is a hack to make sure we don't generate duplicate variable names.
- static auto i = 0;
- while (true) {
- for (auto& member : current_dictionary->members) {
- dictionary_generator.set("member_key", member.name);
- auto member_js_name = make_input_acceptable_cpp(member.name.to_snakecase());
- auto member_value_name = DeprecatedString::formatted("{}_value", member_js_name);
- auto member_property_value_name = DeprecatedString::formatted("{}_property_value_{}", member_js_name, i);
- dictionary_generator.set("member_name", member_js_name);
- dictionary_generator.set("member_value_name", member_value_name);
- dictionary_generator.set("member_property_value_name", member_property_value_name);
- dictionary_generator.append(R"~~~(
- auto @member_property_value_name@ = JS::js_undefined();
- if (@js_name@@js_suffix@.is_object())
- @member_property_value_name@ = TRY(@js_name@@js_suffix@.as_object().get("@member_key@"));
- )~~~");
- if (member.required) {
- dictionary_generator.append(R"~~~(
- if (@member_property_value_name@.is_undefined())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::MissingRequiredProperty, "@member_key@");
- )~~~");
- } else if (!member.default_value.has_value()) {
- // Assume struct member is Optional<T> and _don't_ assign the generated default
- // value (e.g. first enum member) when the dictionary member is optional (i.e.
- // no `required` and doesn't have a default value).
- // This is needed so that "dictionary has member" checks work as expected.
- dictionary_generator.append(R"~~~(
- if (!@member_property_value_name@.is_undefined()) {
- )~~~");
- }
- generate_to_cpp(dictionary_generator, member, member_property_value_name, "", member_value_name, interface, member.extended_attributes.contains("LegacyNullToEmptyString"), !member.required, member.default_value);
- if (member.type->is_string() && optional && interface.extended_attributes.contains("UseNewAKString")) {
- dictionary_generator.append(R"~~~(
- if (@member_value_name@.has_value())
- @cpp_name@.@member_name@ = @member_value_name@.release_value();
- )~~~");
- } else {
- dictionary_generator.append(R"~~~(
- @cpp_name@.@member_name@ = @member_value_name@;
- )~~~");
- }
- if (!member.required && !member.default_value.has_value()) {
- dictionary_generator.append(R"~~~(
- }
- )~~~");
- }
- i++;
- }
- if (current_dictionary->parent_name.is_null())
- break;
- VERIFY(interface.dictionaries.contains(current_dictionary->parent_name));
- current_dictionary = &interface.dictionaries.find(current_dictionary->parent_name)->value;
- }
- } else if (interface.callback_functions.contains(parameter.type->name())) {
- // https://webidl.spec.whatwg.org/#es-callback-function
- auto callback_function_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto& callback_function = interface.callback_functions.find(parameter.type->name())->value;
- if (callback_function.return_type->is_object() && callback_function.return_type->name() == "Promise")
- callback_function_generator.set("operation_returns_promise", "WebIDL::OperationReturnsPromise::Yes");
- else
- callback_function_generator.set("operation_returns_promise", "WebIDL::OperationReturnsPromise::No");
- // An ECMAScript value V is converted to an IDL callback function type value by running the following algorithm:
- // 1. If the result of calling IsCallable(V) is false and the conversion to an IDL value is not being performed due to V being assigned to an attribute whose type is a nullable callback function that is annotated with [LegacyTreatNonObjectAsNull], then throw a TypeError.
- if (!callback_function.is_legacy_treat_non_object_as_null) {
- callback_function_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_function())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- )~~~");
- }
- // 2. Return the IDL callback function type value that represents a reference to the same object that V represents, with the incumbent settings object as the callback context.
- if (callback_function.is_legacy_treat_non_object_as_null) {
- callback_function_generator.append(R"~~~(
- WebIDL::CallbackType* @cpp_name@ = nullptr;
- if (@js_name@@js_suffix@.is_object())
- @cpp_name@ = vm.heap().allocate_without_realm<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_settings_object(), @operation_returns_promise@);
- )~~~");
- } else {
- callback_function_generator.append(R"~~~(
- auto @cpp_name@ = vm.heap().allocate_without_realm<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_settings_object(), @operation_returns_promise@);
- )~~~");
- }
- } else if (parameter.type->name() == "sequence") {
- // https://webidl.spec.whatwg.org/#es-sequence
- auto sequence_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto& parameterized_type = verify_cast<IDL::ParameterizedType>(*parameter.type);
- sequence_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));
- // An ECMAScript value V is converted to an IDL sequence<T> value as follows:
- // 1. If Type(V) is not Object, throw a TypeError.
- // 2. Let method be ? GetMethod(V, @@iterator).
- // 3. If method is undefined, throw a TypeError.
- // 4. Return the result of creating a sequence from V and method.
- if (optional) {
- auto sequence_cpp_type = idl_type_name_to_cpp_type(parameterized_type.parameters().first(), interface);
- sequence_generator.set("sequence.type", sequence_cpp_type.name);
- sequence_generator.set("sequence.storage_type", sequence_storage_type_to_cpp_storage_type_name(sequence_cpp_type.sequence_storage_type));
- if (!optional_default_value.has_value()) {
- if (sequence_cpp_type.sequence_storage_type == IDL::SequenceStorageType::Vector) {
- sequence_generator.append(R"~~~(
- Optional<@sequence.storage_type@<@sequence.type@>> @cpp_name@;
- )~~~");
- } else {
- sequence_generator.append(R"~~~(
- Optional<@sequence.storage_type@> @cpp_name@;
- )~~~");
- }
- } else {
- if (optional_default_value != "[]")
- TODO();
- if (sequence_cpp_type.sequence_storage_type == IDL::SequenceStorageType::Vector) {
- sequence_generator.append(R"~~~(
- @sequence.storage_type@<@sequence.type@> @cpp_name@;
- )~~~");
- } else {
- sequence_generator.append(R"~~~(
- @sequence.storage_type@ @cpp_name@ { global_object.heap() };
- )~~~");
- }
- }
- sequence_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_undefined()) {
- )~~~");
- }
- sequence_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- auto iterator_method@recursion_depth@ = TRY(@js_name@@js_suffix@.get_method(vm, vm.well_known_symbol_iterator()));
- if (!iterator_method@recursion_depth@)
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- )~~~");
- parameterized_type.generate_sequence_from_iterable(sequence_generator, DeprecatedString::formatted("{}{}", acceptable_cpp_name, optional ? "_non_optional" : ""), DeprecatedString::formatted("{}{}", js_name, js_suffix), DeprecatedString::formatted("iterator_method{}", recursion_depth), interface, recursion_depth + 1);
- if (optional) {
- sequence_generator.append(R"~~~(
- @cpp_name@ = move(@cpp_name@_non_optional);
- }
- )~~~");
- }
- } else if (parameter.type->name() == "record") {
- // https://webidl.spec.whatwg.org/#es-record
- auto record_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto& parameterized_type = verify_cast<IDL::ParameterizedType>(*parameter.type);
- record_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));
- // A record can only have two types: key type and value type.
- VERIFY(parameterized_type.parameters().size() == 2);
- // A record only allows the key to be a string.
- VERIFY(parameterized_type.parameters()[0]->is_string());
- // An ECMAScript value O is converted to an IDL record<K, V> value as follows:
- // 1. If Type(O) is not Object, throw a TypeError.
- // 2. Let result be a new empty instance of record<K, V>.
- // 3. Let keys be ? O.[[OwnPropertyKeys]]().
- // 4. For each key of keys:
- // 1. Let desc be ? O.[[GetOwnProperty]](key).
- // 2. If desc is not undefined and desc.[[Enumerable]] is true:
- // 1. Let typedKey be key converted to an IDL value of type K.
- // 2. Let value be ? Get(O, key).
- // 3. Let typedValue be value converted to an IDL value of type V.
- // 4. Set result[typedKey] to typedValue.
- // 5. Return result.
- auto record_cpp_type = IDL::idl_type_name_to_cpp_type(parameterized_type, interface);
- record_generator.set("record.type", record_cpp_type.name);
- // If this is a recursive call to generate_to_cpp, assume that the caller has already handled converting the JS value to an object for us.
- // This affects record types in unions for example.
- if (recursion_depth == 0) {
- record_generator.append(R"~~~(
- if (!@js_name@@js_suffix@.is_object())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, @js_name@@js_suffix@.to_string_without_side_effects()));
- auto& @js_name@@js_suffix@_object = @js_name@@js_suffix@.as_object();
- )~~~");
- }
- record_generator.append(R"~~~(
- @record.type@ @cpp_name@;
- auto record_keys@recursion_depth@ = TRY(@js_name@@js_suffix@_object.internal_own_property_keys());
- for (auto& key@recursion_depth@ : record_keys@recursion_depth@) {
- auto property_key@recursion_depth@ = MUST(JS::PropertyKey::from_value(vm, key@recursion_depth@));
- auto descriptor@recursion_depth@ = TRY(@js_name@@js_suffix@_object.internal_get_own_property(property_key@recursion_depth@));
- if (!descriptor@recursion_depth@.has_value() || !descriptor@recursion_depth@->enumerable.has_value() || !descriptor@recursion_depth@->enumerable.value())
- continue;
- )~~~");
- IDL::Parameter key_parameter { .type = parameterized_type.parameters()[0], .name = acceptable_cpp_name, .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(record_generator, key_parameter, "key", DeprecatedString::number(recursion_depth), DeprecatedString::formatted("typed_key{}", recursion_depth), interface, false, false, {}, false, recursion_depth + 1);
- record_generator.append(R"~~~(
- auto value@recursion_depth@ = TRY(@js_name@@js_suffix@_object.get(property_key@recursion_depth@));
- )~~~");
- // FIXME: Record value types should be TypeWithExtendedAttributes, which would allow us to get [LegacyNullToEmptyString] here.
- IDL::Parameter value_parameter { .type = parameterized_type.parameters()[1], .name = acceptable_cpp_name, .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(record_generator, value_parameter, "value", DeprecatedString::number(recursion_depth), DeprecatedString::formatted("typed_value{}", recursion_depth), interface, false, false, {}, false, recursion_depth + 1);
- record_generator.append(R"~~~(
- @cpp_name@.set(typed_key@recursion_depth@, typed_value@recursion_depth@);
- }
- )~~~");
- } else if (is<IDL::UnionType>(*parameter.type)) {
- // https://webidl.spec.whatwg.org/#es-union
- auto union_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto& union_type = verify_cast<IDL::UnionType>(*parameter.type);
- union_generator.set("union_type", union_type_to_variant(union_type, interface));
- union_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));
- // NOTE: This is handled out here as we need the dictionary conversion code for the {} optional default value.
- // 3. Let types be the flattened member types of the union type.
- auto types = union_type.flattened_member_types();
- RefPtr<Type const> dictionary_type;
- for (auto& dictionary : interface.dictionaries) {
- for (auto& type : types) {
- if (type->name() == dictionary.key) {
- dictionary_type = type;
- break;
- }
- }
- if (dictionary_type)
- break;
- }
- if (dictionary_type) {
- auto dictionary_generator = union_generator.fork().release_value_but_fixme_should_propagate_errors();
- dictionary_generator.set("dictionary.type", dictionary_type->name());
- // The lambda must take the JS::Value to convert as a parameter instead of capturing it in order to support union types being variadic.
- dictionary_generator.append(R"~~~(
- auto @js_name@@js_suffix@_to_dictionary = [&vm, &realm](JS::Value @js_name@@js_suffix@) -> JS::ThrowCompletionOr<@dictionary.type@> {
- // This might be unused.
- (void)realm;
- )~~~");
- IDL::Parameter dictionary_parameter { .type = *dictionary_type, .name = acceptable_cpp_name, .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(dictionary_generator, dictionary_parameter, js_name, js_suffix, "dictionary_union_type"sv, interface, false, false, {}, false, recursion_depth + 1);
- dictionary_generator.append(R"~~~(
- return dictionary_union_type;
- };
- )~~~");
- }
- // A lambda is used because Variants without "Empty" can't easily be default initialized.
- // Plus, this would require the user of union types to always accept a Variant with an Empty type.
- // Additionally, it handles the case of unconditionally throwing a TypeError at the end if none of the types match.
- // This is because we cannot unconditionally throw in generate_to_cpp as generate_to_cpp is supposed to assign to a variable and then continue.
- // Note that all the other types only throw on a condition.
- // The lambda must take the JS::Value to convert as a parameter instead of capturing it in order to support union types being variadic.
- StringBuilder to_variant_captures;
- to_variant_captures.append("&vm, &realm"sv);
- if (dictionary_type)
- to_variant_captures.append(DeprecatedString::formatted(", &{}{}_to_dictionary", js_name, js_suffix));
- union_generator.set("to_variant_captures", to_variant_captures.to_deprecated_string());
- union_generator.append(R"~~~(
- auto @js_name@@js_suffix@_to_variant = [@to_variant_captures@](JS::Value @js_name@@js_suffix@) -> JS::ThrowCompletionOr<@union_type@> {
- // These might be unused.
- (void)vm;
- (void)realm;
- )~~~");
- // 1. If the union type includes undefined and V is undefined, then return the unique undefined value.
- if (union_type.includes_undefined()) {
- scoped_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_undefined())
- return Empty {};
- )~~~");
- }
- // FIXME: 2. If the union type includes a nullable type and V is null or undefined, then return the IDL value null.
- if (union_type.includes_nullable_type()) {
- // Implement me
- } else if (dictionary_type) {
- // 4. If V is null or undefined, then
- // 4.1 If types includes a dictionary type, then return the result of converting V to that dictionary type.
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_nullish())
- return @union_type@ { TRY(@js_name@@js_suffix@_to_dictionary(@js_name@@js_suffix@)) };
- )~~~");
- }
- bool includes_object = false;
- for (auto& type : types) {
- if (type->name() == "object") {
- includes_object = true;
- break;
- }
- }
- // FIXME: Don't generate this if the union type doesn't include any object types.
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_object()) {
- [[maybe_unused]] auto& @js_name@@js_suffix@_object = @js_name@@js_suffix@.as_object();
- )~~~");
- bool includes_platform_object = false;
- for (auto& type : types) {
- if (IDL::is_platform_object(type)) {
- includes_platform_object = true;
- break;
- }
- }
- if (includes_platform_object) {
- // 5. If V is a platform object, then:
- union_generator.append(R"~~~(
- if (is<PlatformObject>(@js_name@@js_suffix@_object)) {
- )~~~");
- // NOTE: This codegen assumes that all union types are cells or values we can create a handle for.
- // 1. If types includes an interface type that V implements, then return the IDL value that is a reference to the object V.
- for (auto& type : types) {
- if (!IDL::is_platform_object(type))
- continue;
- auto union_platform_object_type_generator = union_generator.fork().release_value_but_fixme_should_propagate_errors();
- union_platform_object_type_generator.set("platform_object_type", type->name());
- union_platform_object_type_generator.append(R"~~~(
- if (is<@platform_object_type@>(@js_name@@js_suffix@_object))
- return JS::make_handle(static_cast<@platform_object_type@&>(@js_name@@js_suffix@_object));
- )~~~");
- }
- // 2. If types includes object, then return the IDL value that is a reference to the object V.
- if (includes_object) {
- union_generator.append(R"~~~(
- return JS::make_handle(@js_name@@js_suffix@_object);
- )~~~");
- }
- union_generator.append(R"~~~(
- }
- )~~~");
- }
- // 6. If Type(V) is Object and V has an [[ArrayBufferData]] internal slot, then
- // 1. If types includes ArrayBuffer, then return the result of converting V to ArrayBuffer.
- for (auto& type : types) {
- if (type->name() == "BufferSource") {
- union_generator.append(R"~~~(
- if (is<JS::TypedArrayBase>(@js_name@@js_suffix@_object) || is<JS::ArrayBuffer>(@js_name@@js_suffix@_object) || is<JS::DataView>(@js_name@@js_suffix@_object))
- return JS::make_handle(@js_name@@js_suffix@_object);
- )~~~");
- }
- }
- // 2. If types includes object, then return the IDL value that is a reference to the object V.
- if (includes_object) {
- union_generator.append(R"~~~(
- return @js_name@@js_suffix@_object;
- )~~~");
- }
- // FIXME: 7. If Type(V) is Object and V has a [[DataView]] internal slot, then:
- // 1. If types includes DataView, then return the result of converting V to DataView.
- // 2. If types includes object, then return the IDL value that is a reference to the object V.
- // FIXME: 8. If Type(V) is Object and V has a [[TypedArrayName]] internal slot, then:
- // 1. If types includes a typed array type whose name is the value of V’s [[TypedArrayName]] internal slot, then return the result of converting V to that type.
- // 2. If types includes object, then return the IDL value that is a reference to the object V.
- // 9. If IsCallable(V) is true, then:
- // 1. If types includes a callback function type, then return the result of converting V to that callback function type.
- // 2. If types includes object, then return the IDL value that is a reference to the object V.
- bool includes_callable = false;
- for (auto const& type : types) {
- if (type->name() == "Function"sv) {
- includes_callable = true;
- break;
- }
- }
- if (includes_callable) {
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@_object.is_function())
- return JS::make_handle(vm.heap().allocate_without_realm<WebIDL::CallbackType>(@js_name@@js_suffix@.as_function(), HTML::incumbent_settings_object()));
- )~~~");
- }
- // 10. If Type(V) is Object, then:
- // 1. If types includes a sequence type, then:
- RefPtr<IDL::ParameterizedType const> sequence_type;
- for (auto& type : types) {
- if (type->name() == "sequence") {
- sequence_type = verify_cast<IDL::ParameterizedType>(*type);
- break;
- }
- }
- if (sequence_type) {
- // 1. Let method be ? GetMethod(V, @@iterator).
- union_generator.append(R"~~~(
- auto method = TRY(@js_name@@js_suffix@.get_method(vm, vm.well_known_symbol_iterator()));
- )~~~");
- // 2. If method is not undefined, return the result of creating a sequence of that type from V and method.
- union_generator.append(R"~~~(
- if (method) {
- )~~~");
- sequence_type->generate_sequence_from_iterable(union_generator, acceptable_cpp_name, DeprecatedString::formatted("{}{}", js_name, js_suffix), "method", interface, recursion_depth + 1);
- union_generator.append(R"~~~(
- return @cpp_name@;
- }
- )~~~");
- }
- // FIXME: 2. If types includes a frozen array type, then
- // 1. Let method be ? GetMethod(V, @@iterator).
- // 2. If method is not undefined, return the result of creating a frozen array of that type from V and method.
- // 3. If types includes a dictionary type, then return the result of converting V to that dictionary type.
- if (dictionary_type) {
- union_generator.append(R"~~~(
- return @union_type@ { TRY(@js_name@@js_suffix@_to_dictionary(@js_name@@js_suffix@)) };
- )~~~");
- }
- // 4. If types includes a record type, then return the result of converting V to that record type.
- RefPtr<IDL::ParameterizedType const> record_type;
- for (auto& type : types) {
- if (type->name() == "record") {
- record_type = verify_cast<IDL::ParameterizedType>(*type);
- break;
- }
- }
- if (record_type) {
- IDL::Parameter record_parameter { .type = *record_type, .name = acceptable_cpp_name, .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(union_generator, record_parameter, js_name, js_suffix, "record_union_type"sv, interface, false, false, {}, false, recursion_depth + 1);
- union_generator.append(R"~~~(
- return record_union_type;
- )~~~");
- }
- // FIXME: 5. If types includes a callback interface type, then return the result of converting V to that callback interface type.
- // 6. If types includes object, then return the IDL value that is a reference to the object V.
- if (includes_object) {
- union_generator.append(R"~~~(
- return @js_name@@js_suffix@_object;
- )~~~");
- }
- // End of is_object.
- union_generator.append(R"~~~(
- }
- )~~~");
- // 11. If Type(V) is Boolean, then:
- // 1. If types includes boolean, then return the result of converting V to boolean.
- bool includes_boolean = false;
- for (auto& type : types) {
- if (type->name() == "boolean") {
- includes_boolean = true;
- break;
- }
- }
- if (includes_boolean) {
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_boolean())
- return @union_type@ { @js_name@@js_suffix@.as_bool() };
- )~~~");
- }
- RefPtr<IDL::Type const> numeric_type;
- for (auto& type : types) {
- if (type->is_numeric()) {
- numeric_type = type;
- break;
- }
- }
- // 12. If Type(V) is Number, then:
- // 1. If types includes a numeric type, then return the result of converting V to that numeric type.
- if (numeric_type) {
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_number()) {
- )~~~");
- // NOTE: generate_to_cpp doesn't use the parameter name.
- // NOTE: generate_to_cpp will use to_{u32,etc.} which uses to_number internally and will thus use TRY, but it cannot throw as we know we are dealing with a number.
- IDL::Parameter parameter { .type = *numeric_type, .name = DeprecatedString::empty(), .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(union_generator, parameter, js_name, js_suffix, DeprecatedString::formatted("{}{}_number", js_name, js_suffix), interface, false, false, {}, false, recursion_depth + 1);
- union_generator.append(R"~~~(
- return @js_name@@js_suffix@_number;
- }
- )~~~");
- }
- // 13. If Type(V) is BigInt, then:
- // 1. If types includes bigint, then return the result of converting V to bigint
- bool includes_bigint = false;
- for (auto& type : types) {
- if (type->name() == "bigint") {
- includes_bigint = true;
- break;
- }
- }
- if (includes_bigint) {
- union_generator.append(R"~~~(
- if (@js_name@@js_suffix@.is_bigint())
- return @js_name@@js_suffix@.as_bigint();
- )~~~");
- }
- bool includes_string = false;
- for (auto& type : types) {
- if (type->is_string()) {
- includes_string = true;
- break;
- }
- }
- if (includes_string) {
- // 14. If types includes a string type, then return the result of converting V to that type.
- // NOTE: Currently all string types are converted to String.
- if (interface.extended_attributes.contains("UseNewAKString")) {
- union_generator.append(R"~~~(
- return TRY(@js_name@@js_suffix@.to_string(vm));
- )~~~");
- } else {
- union_generator.append(R"~~~(
- return TRY(@js_name@@js_suffix@.to_deprecated_string(vm));
- )~~~");
- }
- } else if (numeric_type && includes_bigint) {
- // 15. If types includes a numeric type and bigint, then return the result of converting V to either that numeric type or bigint.
- // https://webidl.spec.whatwg.org/#converted-to-a-numeric-type-or-bigint
- // NOTE: This algorithm is only used here.
- // An ECMAScript value V is converted to an IDL numeric type T or bigint value by running the following algorithm:
- // 1. Let x be ? ToNumeric(V).
- // 2. If Type(x) is BigInt, then
- // 1. Return the IDL bigint value that represents the same numeric value as x.
- // 3. Assert: Type(x) is Number.
- // 4. Return the result of converting x to T.
- auto union_numeric_type_generator = union_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto cpp_type = IDL::idl_type_name_to_cpp_type(*numeric_type, interface);
- union_numeric_type_generator.set("numeric_type", cpp_type.name);
- union_numeric_type_generator.append(R"~~~(
- auto x = TRY(@js_name@@js_suffix@.to_numeric(vm));
- if (x.is_bigint())
- return x.as_bigint();
- VERIFY(x.is_number());
- )~~~");
- // NOTE: generate_to_cpp doesn't use the parameter name.
- // NOTE: generate_to_cpp will use to_{u32,etc.} which uses to_number internally and will thus use TRY, but it cannot throw as we know we are dealing with a number.
- IDL::Parameter parameter { .type = *numeric_type, .name = DeprecatedString::empty(), .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(union_numeric_type_generator, parameter, "x", DeprecatedString::empty(), "x_number", interface, false, false, {}, false, recursion_depth + 1);
- union_numeric_type_generator.append(R"~~~(
- return x_number;
- )~~~");
- } else if (numeric_type) {
- // 16. If types includes a numeric type, then return the result of converting V to that numeric type.
- // NOTE: generate_to_cpp doesn't use the parameter name.
- // NOTE: generate_to_cpp will use to_{u32,etc.} which uses to_number internally and will thus use TRY, but it cannot throw as we know we are dealing with a number.
- IDL::Parameter parameter { .type = *numeric_type, .name = DeprecatedString::empty(), .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(union_generator, parameter, js_name, js_suffix, DeprecatedString::formatted("{}{}_number", js_name, js_suffix), interface, false, false, {}, false, recursion_depth + 1);
- union_generator.append(R"~~~(
- return @js_name@@js_suffix@_number;
- )~~~");
- } else if (includes_boolean) {
- // 17. If types includes boolean, then return the result of converting V to boolean.
- union_generator.append(R"~~~(
- return @union_type@ { @js_name@@js_suffix@.to_boolean() };
- )~~~");
- } else if (includes_bigint) {
- // 18. If types includes bigint, then return the result of converting V to bigint.
- union_generator.append(R"~~~(
- return TRY(@js_name@@js_suffix@.to_bigint(vm));
- )~~~");
- } else {
- // 19. Throw a TypeError.
- // FIXME: Replace the error message with something more descriptive.
- union_generator.append(R"~~~(
- return vm.throw_completion<JS::TypeError>("No union types matched"sv);
- )~~~");
- }
- // Close the lambda and then perform the conversion.
- union_generator.append(R"~~~(
- };
- )~~~");
- if (!variadic) {
- if (!optional) {
- union_generator.append(R"~~~(
- @union_type@ @cpp_name@ = TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- } else {
- if (!optional_default_value.has_value() || optional_default_value == "null"sv) {
- union_generator.append(R"~~~(
- Optional<@union_type@> @cpp_name@;
- if (!@js_name@@js_suffix@.is_nullish())
- @cpp_name@ = TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- } else {
- if (optional_default_value == "\"\"") {
- if (!interface.extended_attributes.contains("UseNewAKString")) {
- union_generator.append(R"~~~(
- @union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? DeprecatedString::empty() : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- } else {
- union_generator.append(R"~~~(
- @union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? String {} : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- }
- } else if (optional_default_value == "{}") {
- VERIFY(dictionary_type);
- union_generator.append(R"~~~(
- @union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? TRY(@js_name@@js_suffix@_to_dictionary(@js_name@@js_suffix@)) : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- } else if (optional_default_value->to_int().has_value() || optional_default_value->to_uint().has_value()) {
- union_generator.append(R"~~~(
- @union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? @parameter.optional_default_value@ : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
- )~~~");
- } else {
- TODO();
- }
- }
- }
- } else {
- union_generator.append(R"~~~(
- Vector<@union_type@> @cpp_name@;
- if (vm.argument_count() > @js_suffix@) {
- TRY_OR_THROW_OOM(vm, @cpp_name@.try_ensure_capacity(vm.argument_count() - @js_suffix@));
- for (size_t i = @js_suffix@; i < vm.argument_count(); ++i) {
- auto result = TRY(@js_name@@js_suffix@_to_variant(vm.argument(i)));
- @cpp_name@.unchecked_append(move(result));
- }
- }
- )~~~");
- }
- } else {
- dbgln("Unimplemented JS-to-C++ conversion: {}", parameter.type->name());
- VERIFY_NOT_REACHED();
- }
- }
- static void generate_argument_count_check(SourceGenerator& generator, DeprecatedString const& function_name, size_t argument_count)
- {
- if (argument_count == 0)
- return;
- auto argument_count_check_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- argument_count_check_generator.set("function.name", function_name);
- argument_count_check_generator.set("function.nargs", DeprecatedString::number(argument_count));
- if (argument_count == 1) {
- argument_count_check_generator.set(".bad_arg_count", "JS::ErrorType::BadArgCountOne");
- argument_count_check_generator.set(".arg_count_suffix", "");
- } else {
- argument_count_check_generator.set(".bad_arg_count", "JS::ErrorType::BadArgCountMany");
- argument_count_check_generator.set(".arg_count_suffix", DeprecatedString::formatted(", \"{}\"", argument_count));
- }
- argument_count_check_generator.append(R"~~~(
- if (vm.argument_count() < @function.nargs@)
- return vm.throw_completion<JS::TypeError>(@.bad_arg_count@, "@function.name@"@.arg_count_suffix@);
- )~~~");
- }
- static void generate_arguments(SourceGenerator& generator, Vector<IDL::Parameter> const& parameters, StringBuilder& arguments_builder, IDL::Interface const& interface)
- {
- auto arguments_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- Vector<DeprecatedString> parameter_names;
- size_t argument_index = 0;
- for (auto& parameter : parameters) {
- auto parameter_name = make_input_acceptable_cpp(parameter.name.to_snakecase());
- if (parameter.variadic) {
- // JS::MarkedVector is non-copyable, and the implementations likely want ownership of the
- // list, so we move() it into the parameter list.
- parameter_names.append(DeprecatedString::formatted("move({})", parameter_name));
- } else {
- parameter_names.append(move(parameter_name));
- arguments_generator.set("argument.index", DeprecatedString::number(argument_index));
- arguments_generator.append(R"~~~(
- auto arg@argument.index@ = vm.argument(@argument.index@);
- )~~~");
- }
- bool legacy_null_to_empty_string = parameter.extended_attributes.contains("LegacyNullToEmptyString");
- generate_to_cpp(generator, parameter, "arg", DeprecatedString::number(argument_index), parameter.name.to_snakecase(), interface, legacy_null_to_empty_string, parameter.optional, parameter.optional_default_value, parameter.variadic, 0);
- ++argument_index;
- }
- arguments_builder.join(", "sv, parameter_names);
- }
- // https://webidl.spec.whatwg.org/#create-sequence-from-iterable
- void IDL::ParameterizedType::generate_sequence_from_iterable(SourceGenerator& generator, DeprecatedString const& cpp_name, DeprecatedString const& iterable_cpp_name, DeprecatedString const& iterator_method_cpp_name, IDL::Interface const& interface, size_t recursion_depth) const
- {
- auto sequence_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- sequence_generator.set("cpp_name", cpp_name);
- sequence_generator.set("iterable_cpp_name", iterable_cpp_name);
- sequence_generator.set("iterator_method_cpp_name", iterator_method_cpp_name);
- sequence_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));
- auto sequence_cpp_type = idl_type_name_to_cpp_type(parameters().first(), interface);
- sequence_generator.set("sequence.type", sequence_cpp_type.name);
- sequence_generator.set("sequence.storage_type", sequence_storage_type_to_cpp_storage_type_name(sequence_cpp_type.sequence_storage_type));
- // To create an IDL value of type sequence<T> given an iterable iterable and an iterator getter method, perform the following steps:
- // 1. Let iter be ? GetIterator(iterable, sync, method).
- // 2. Initialize i to be 0.
- // 3. Repeat
- // 1. Let next be ? IteratorStep(iter).
- // 2. If next is false, then return an IDL sequence value of type sequence<T> of length i, where the value of the element at index j is Sj.
- // 3. Let nextItem be ? IteratorValue(next).
- // 4. Initialize Si to the result of converting nextItem to an IDL value of type T.
- // 5. Set i to i + 1.
- // FIXME: The WebIDL spec is out of date - it should be using GetIteratorFromMethod.
- sequence_generator.append(R"~~~(
- auto iterator@recursion_depth@ = TRY(JS::get_iterator_from_method(vm, @iterable_cpp_name@, *@iterator_method_cpp_name@));
- )~~~");
- if (sequence_cpp_type.sequence_storage_type == SequenceStorageType::Vector) {
- sequence_generator.append(R"~~~(
- @sequence.storage_type@<@sequence.type@> @cpp_name@;
- )~~~");
- } else {
- sequence_generator.append(R"~~~(
- @sequence.storage_type@ @cpp_name@ { vm.heap() };
- )~~~");
- }
- sequence_generator.append(R"~~~(
- for (;;) {
- auto next@recursion_depth@ = TRY(JS::iterator_step(vm, iterator@recursion_depth@));
- if (!next@recursion_depth@)
- break;
- auto next_item@recursion_depth@ = TRY(JS::iterator_value(vm, *next@recursion_depth@));
- )~~~");
- // FIXME: Sequences types should be TypeWithExtendedAttributes, which would allow us to get [LegacyNullToEmptyString] here.
- IDL::Parameter parameter { .type = parameters().first(), .name = iterable_cpp_name, .optional_default_value = {}, .extended_attributes = {} };
- generate_to_cpp(sequence_generator, parameter, "next_item", DeprecatedString::number(recursion_depth), DeprecatedString::formatted("sequence_item{}", recursion_depth), interface, false, false, {}, false, recursion_depth);
- sequence_generator.append(R"~~~(
- @cpp_name@.append(sequence_item@recursion_depth@);
- }
- )~~~");
- }
- enum class WrappingReference {
- No,
- Yes,
- };
- static void generate_wrap_statement(SourceGenerator& generator, DeprecatedString const& value, IDL::Type const& type, IDL::Interface const& interface, StringView result_expression, WrappingReference wrapping_reference = WrappingReference::No, size_t recursion_depth = 0)
- {
- auto scoped_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- scoped_generator.set("value", value);
- if (!libweb_interface_namespaces.span().contains_slow(type.name())) {
- if (is_javascript_builtin(type))
- scoped_generator.set("type", DeprecatedString::formatted("JS::{}", type.name()));
- else
- scoped_generator.set("type", type.name());
- } else {
- // e.g. Document.getSelection which returns Selection, which is in the Selection namespace.
- StringBuilder builder;
- builder.append(type.name());
- builder.append("::"sv);
- builder.append(type.name());
- scoped_generator.set("type", builder.to_deprecated_string());
- }
- scoped_generator.set("result_expression", result_expression);
- scoped_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));
- if (type.name() == "undefined") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::js_undefined();
- )~~~");
- return;
- }
- if (type.is_nullable() && !is<UnionType>(type)) {
- if (type.is_string()) {
- if (!interface.extended_attributes.contains("UseNewAKString")) {
- scoped_generator.append(R"~~~(
- if (@value@.is_null()) {
- @result_expression@ JS::js_null();
- } else {
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- if (!@value@.has_value()) {
- @result_expression@ JS::js_null();
- } else {
- )~~~");
- }
- } else if (type.name() == "sequence") {
- scoped_generator.append(R"~~~(
- if (!@value@.has_value()) {
- @result_expression@ JS::js_null();
- } else {
- )~~~");
- } else if (type.is_primitive()) {
- scoped_generator.append(R"~~~(
- if (!@value@.has_value()) {
- @result_expression@ JS::js_null();
- } else {
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- if (!@value@) {
- @result_expression@ JS::js_null();
- } else {
- )~~~");
- }
- }
- if (type.is_string()) {
- if (type.is_nullable() && interface.extended_attributes.contains("UseNewAKString")) {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::PrimitiveString::create(vm, @value@.release_value());
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::PrimitiveString::create(vm, @value@);
- )~~~");
- }
- } else if (type.name() == "sequence") {
- // https://webidl.spec.whatwg.org/#es-sequence
- auto& sequence_generic_type = verify_cast<IDL::ParameterizedType>(type);
- scoped_generator.append(R"~~~(
- auto new_array@recursion_depth@ = MUST(JS::Array::create(realm, 0));
- )~~~");
- if (!type.is_nullable()) {
- scoped_generator.append(R"~~~(
- for (size_t i@recursion_depth@ = 0; i@recursion_depth@ < @value@.size(); ++i@recursion_depth@) {
- auto& element@recursion_depth@ = @value@.at(i@recursion_depth@);
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- auto& @value@_non_optional = @value@.value();
- for (size_t i@recursion_depth@ = 0; i@recursion_depth@ < @value@_non_optional.size(); ++i@recursion_depth@) {
- auto& element@recursion_depth@ = @value@_non_optional.at(i@recursion_depth@);
- )~~~");
- }
- // If the type is a platform object we currently return a Vector<JS::Handle<T>> from the
- // C++ implementation, thus allowing us to unwrap the element (a handle) like below.
- // This might need to change if we switch to a MarkedVector.
- if (is_platform_object(sequence_generic_type.parameters().first())) {
- scoped_generator.append(R"~~~(
- auto* wrapped_element@recursion_depth@ = &(*element@recursion_depth@);
- )~~~");
- } else {
- generate_wrap_statement(scoped_generator, DeprecatedString::formatted("element{}", recursion_depth), sequence_generic_type.parameters().first(), interface, DeprecatedString::formatted("auto wrapped_element{} =", recursion_depth), WrappingReference::Yes, recursion_depth + 1);
- }
- scoped_generator.append(R"~~~(
- auto property_index@recursion_depth@ = JS::PropertyKey { i@recursion_depth@ };
- MUST(new_array@recursion_depth@->create_data_property(property_index@recursion_depth@, wrapped_element@recursion_depth@));
- }
- @result_expression@ new_array@recursion_depth@;
- )~~~");
- } else if (type.name() == "boolean" || type.name() == "double" || type.name() == "float") {
- if (type.is_nullable()) {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value(@value@.release_value());
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value(@value@);
- )~~~");
- }
- } else if (type.name() == "short" || type.name() == "long" || type.name() == "unsigned short") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value((i32)@value@);
- )~~~");
- } else if (type.name() == "unsigned long") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value((u32)@value@);
- )~~~");
- } else if (type.name() == "long long") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value((double)@value@);
- )~~~");
- } else if (type.name() == "unsigned long long") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value((double)@value@);
- )~~~");
- } else if (type.name() == "Location" || type.name() == "Promise" || type.name() == "Uint8Array" || type.name() == "Uint8ClampedArray" || type.name() == "any") {
- scoped_generator.append(R"~~~(
- @result_expression@ @value@;
- )~~~");
- } else if (is<IDL::UnionType>(type)) {
- auto& union_type = verify_cast<IDL::UnionType>(type);
- auto union_types = union_type.flattened_member_types();
- auto union_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- union_generator.append(R"~~~(
- @result_expression@ @value@.visit(
- )~~~");
- for (size_t current_union_type_index = 0; current_union_type_index < union_types.size(); ++current_union_type_index) {
- auto& current_union_type = union_types.at(current_union_type_index);
- auto cpp_type = IDL::idl_type_name_to_cpp_type(current_union_type, interface);
- union_generator.set("current_type", cpp_type.name);
- union_generator.append(R"~~~(
- [&vm, &realm]([[maybe_unused]] @current_type@ const& visited_union_value@recursion_depth@) -> JS::Value {
- // These may be unused.
- (void)vm;
- (void)realm;
- )~~~");
- // NOTE: While we are using const&, the underlying type for wrappable types in unions is (Nonnull)RefPtr, which are not references.
- generate_wrap_statement(union_generator, DeprecatedString::formatted("visited_union_value{}", recursion_depth), current_union_type, interface, "return"sv, WrappingReference::No, recursion_depth + 1);
- // End of current visit lambda.
- // The last lambda cannot have a trailing comma on the closing brace, unless the type is nullable, where an extra lambda will be generated for the Empty case.
- if (current_union_type_index != union_types.size() - 1 || type.is_nullable()) {
- union_generator.append(R"~~~(
- },
- )~~~");
- } else {
- union_generator.append(R"~~~(
- }
- )~~~");
- }
- }
- if (type.is_nullable()) {
- union_generator.append(R"~~~(
- [](Empty) -> JS::Value {
- return JS::js_null();
- }
- )~~~");
- }
- // End of visit.
- union_generator.append(R"~~~(
- );
- )~~~");
- } else if (interface.enumerations.contains(type.name())) {
- if (!interface.extended_attributes.contains("UseNewAKString")) {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::PrimitiveString::create(vm, Bindings::idl_enum_to_deprecated_string(@value@));
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, Bindings::idl_enum_to_string(@value@)));
- )~~~");
- }
- } else if (interface.callback_functions.contains(type.name())) {
- // https://webidl.spec.whatwg.org/#es-callback-function
- auto& callback_function = interface.callback_functions.find(type.name())->value;
- // The result of converting an IDL callback function type value to an ECMAScript value is a reference to the same object that the IDL callback function type value represents.
- if (callback_function.is_legacy_treat_non_object_as_null && !type.is_nullable()) {
- scoped_generator.append(R"~~~(
- if (!@value@) {
- @result_expression@ JS::js_null();
- } else {
- @result_expression@ @value@->callback;
- }
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- @result_expression@ @value@->callback;
- )~~~");
- }
- } else if (interface.dictionaries.contains(type.name())) {
- // https://webidl.spec.whatwg.org/#es-dictionary
- auto dictionary_generator = scoped_generator.fork().release_value_but_fixme_should_propagate_errors();
- dictionary_generator.append(R"~~~(
- auto dictionary_object@recursion_depth@ = JS::Object::create(realm, realm.intrinsics().object_prototype());
- )~~~");
- auto* current_dictionary = &interface.dictionaries.find(type.name())->value;
- while (true) {
- for (auto& member : current_dictionary->members) {
- dictionary_generator.set("member_key", member.name);
- auto member_key_js_name = DeprecatedString::formatted("{}{}", make_input_acceptable_cpp(member.name.to_snakecase()), recursion_depth);
- dictionary_generator.set("member_name", member_key_js_name);
- auto member_value_js_name = DeprecatedString::formatted("{}_value", member_key_js_name);
- dictionary_generator.set("member_value", member_value_js_name);
- auto wrapped_value_name = DeprecatedString::formatted("wrapped_{}", member_value_js_name);
- dictionary_generator.set("wrapped_value_name", wrapped_value_name);
- generate_wrap_statement(dictionary_generator, DeprecatedString::formatted("{}.{}", value, member.name.to_snakecase()), member.type, interface, DeprecatedString::formatted("auto {} =", wrapped_value_name), WrappingReference::No, recursion_depth + 1);
- dictionary_generator.append(R"~~~(
- MUST(dictionary_object@recursion_depth@->create_data_property("@member_key@", @wrapped_value_name@));
- )~~~");
- }
- if (current_dictionary->parent_name.is_null())
- break;
- VERIFY(interface.dictionaries.contains(current_dictionary->parent_name));
- current_dictionary = &interface.dictionaries.find(current_dictionary->parent_name)->value;
- }
- dictionary_generator.append(R"~~~(
- @result_expression@ dictionary_object@recursion_depth@;
- )~~~");
- } else if (type.name() == "object") {
- scoped_generator.append(R"~~~(
- @result_expression@ JS::Value(const_cast<JS::Object*>(@value@));
- )~~~");
- } else {
- if (wrapping_reference == WrappingReference::No) {
- scoped_generator.append(R"~~~(
- @result_expression@ &const_cast<@type@&>(*@value@);
- )~~~");
- } else {
- scoped_generator.append(R"~~~(
- @result_expression@ &const_cast<@type@&>(@value@);
- )~~~");
- }
- }
- if (type.is_nullable() && !is<UnionType>(type)) {
- scoped_generator.append(R"~~~(
- }
- )~~~");
- }
- }
- enum class StaticFunction {
- No,
- Yes,
- };
- static void generate_return_statement(SourceGenerator& generator, IDL::Type const& return_type, IDL::Interface const& interface)
- {
- return generate_wrap_statement(generator, "retval", return_type, interface, "return"sv);
- }
- static void generate_variable_statement(SourceGenerator& generator, DeprecatedString const& variable_name, IDL::Type const& value_type, DeprecatedString const& value_name, IDL::Interface const& interface)
- {
- auto variable_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- variable_generator.set("variable_name", variable_name);
- variable_generator.append(R"~~~(
- JS::Value @variable_name@;
- )~~~");
- return generate_wrap_statement(generator, value_name, value_type, interface, DeprecatedString::formatted("{} = ", variable_name));
- }
- static void generate_function(SourceGenerator& generator, IDL::Function const& function, StaticFunction is_static_function, DeprecatedString const& class_name, DeprecatedString const& interface_fully_qualified_name, IDL::Interface const& interface)
- {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("class_name", class_name);
- function_generator.set("interface_fully_qualified_name", interface_fully_qualified_name);
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(function.name.to_snakecase()));
- function_generator.set("overload_suffix", function.is_overloaded ? DeprecatedString::number(function.overload_index) : DeprecatedString::empty());
- if (function.extended_attributes.contains("ImplementedAs")) {
- auto implemented_as = function.extended_attributes.get("ImplementedAs").value();
- function_generator.set("function.cpp_name", implemented_as);
- } else {
- function_generator.set("function.cpp_name", make_input_acceptable_cpp(function.name.to_snakecase()));
- }
- function_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@@overload_suffix@)
- {
- [[maybe_unused]] auto& realm = *vm.current_realm();
- )~~~");
- if (is_static_function == StaticFunction::No) {
- function_generator.append(R"~~~(
- auto* impl = TRY(impl_from(vm));
- )~~~");
- }
- // Optimization: overloaded functions' arguments count is checked by the overload arbiter
- if (!function.is_overloaded)
- generate_argument_count_check(generator, function.name, function.shortest_length());
- StringBuilder arguments_builder;
- generate_arguments(generator, function.parameters, arguments_builder, interface);
- function_generator.set(".arguments", arguments_builder.string_view());
- if (is_static_function == StaticFunction::No) {
- // For [CEReactions]: https://html.spec.whatwg.org/multipage/custom-elements.html#cereactions
- if (function.extended_attributes.contains("CEReactions")) {
- // 1. Push a new element queue onto this object's relevant agent's custom element reactions stack.
- function_generator.append(R"~~~(
- auto& relevant_agent = HTML::relevant_agent(*impl);
- auto* custom_data = verify_cast<Bindings::WebEngineCustomData>(relevant_agent.custom_data());
- auto& reactions_stack = custom_data->custom_element_reactions_stack;
- reactions_stack.element_queue_stack.append({});
- )~~~");
- }
- if (!function.extended_attributes.contains("CEReactions")) {
- function_generator.append(R"~~~(
- [[maybe_unused]] auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->@function.cpp_name@(@.arguments@); }));
- )~~~");
- } else {
- // 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
- // 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
- // 4. Invoke custom element reactions in queue.
- // 5. If an exception exception was thrown by the original steps, rethrow exception.
- // 6. If a value value was returned from the original steps, return value.
- function_generator.append(R"~~~(
- auto retval_or_exception = throw_dom_exception_if_needed(vm, [&] { return impl->@function.cpp_name@(@.arguments@); });
- auto queue = reactions_stack.element_queue_stack.take_last();
- Bindings::invoke_custom_element_reactions(queue);
- if (retval_or_exception.is_error())
- return retval_or_exception.release_error();
- [[maybe_unused]] auto retval = retval_or_exception.release_value();
- )~~~");
- }
- } else {
- // Make sure first argument for static functions is the Realm.
- if (arguments_builder.is_empty())
- function_generator.set(".arguments", "vm");
- else
- function_generator.set(".arguments", DeprecatedString::formatted("vm, {}", arguments_builder.string_view()));
- function_generator.append(R"~~~(
- [[maybe_unused]] auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return @interface_fully_qualified_name@::@function.cpp_name@(@.arguments@); }));
- )~~~");
- }
- generate_return_statement(generator, *function.return_type, interface);
- function_generator.append(R"~~~(
- }
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#compute-the-effective-overload-set
- static EffectiveOverloadSet compute_the_effective_overload_set(auto const& overload_set)
- {
- // 1. Let S be an ordered set.
- Vector<EffectiveOverloadSet::Item> overloads;
- // 2. Let F be an ordered set with items as follows, according to the kind of effective overload set:
- // Note: This is determined by the caller of generate_overload_arbiter()
- // 3. Let maxarg be the maximum number of arguments the operations, legacy factory functions, or
- // callback functions in F are declared to take. For variadic operations and legacy factory functions,
- // the argument on which the ellipsis appears counts as a single argument.
- auto overloaded_functions = overload_set.value;
- auto maximum_arguments = 0;
- for (auto const& function : overloaded_functions)
- maximum_arguments = max(maximum_arguments, static_cast<int>(function.parameters.size()));
- // 4. Let max be max(maxarg, N).
- // NOTE: We don't do this step. `N` is a runtime value, so we just use `maxarg` here instead.
- // Later, `generate_overload_arbiter()` produces individual overload sets for each possible N.
- // 5. For each operation or extended attribute X in F:
- auto overload_id = 0;
- for (auto const& overload : overloaded_functions) {
- // 1. Let arguments be the list of arguments X is declared to take.
- auto const& arguments = overload.parameters;
- // 2. Let n be the size of arguments.
- int argument_count = (int)arguments.size();
- // 3. Let types be a type list.
- Vector<NonnullRefPtr<Type const>> types;
- // 4. Let optionalityValues be an optionality list.
- Vector<Optionality> optionality_values;
- bool overload_is_variadic = false;
- // 5. For each argument in arguments:
- for (auto const& argument : arguments) {
- // 1. Append the type of argument to types.
- types.append(argument.type);
- // 2. Append "variadic" to optionalityValues if argument is a final, variadic argument, "optional" if argument is optional, and "required" otherwise.
- if (argument.variadic) {
- optionality_values.append(Optionality::Variadic);
- overload_is_variadic = true;
- } else if (argument.optional) {
- optionality_values.append(Optionality::Optional);
- } else {
- optionality_values.append(Optionality::Required);
- }
- }
- // 6. Append the tuple (X, types, optionalityValues) to S.
- overloads.empend(overload_id, types, optionality_values);
- // 7. If X is declared to be variadic, then:
- if (overload_is_variadic) {
- // 1. For each i in the range n to max − 1, inclusive:
- for (auto i = argument_count; i < maximum_arguments; ++i) {
- // 1. Let t be a type list.
- // 2. Let o be an optionality list.
- // NOTE: We hold both of these in an Item instead.
- EffectiveOverloadSet::Item item;
- item.callable_id = overload_id;
- // 3. For each j in the range 0 to n − 1, inclusive:
- for (auto j = 0; j < argument_count; ++j) {
- // 1. Append types[j] to t.
- item.types.append(types[j]);
- // 2. Append optionalityValues[j] to o.
- item.optionality_values.append(optionality_values[j]);
- }
- // 4. For each j in the range n to i, inclusive:
- for (auto j = argument_count; j <= i; ++j) {
- // 1. Append types[n − 1] to t.
- item.types.append(types[argument_count - 1]);
- // 2. Append "variadic" to o.
- item.optionality_values.append(Optionality::Variadic);
- }
- // 5. Append the tuple (X, t, o) to S.
- overloads.append(move(item));
- }
- }
- // 8. Let i be n − 1.
- auto i = argument_count - 1;
- // 9. While i ≥ 0:
- while (i >= 0) {
- // 1. If arguments[i] is not optional (i.e., it is not marked as "optional" and is not a final, variadic argument), then break.
- if (!arguments[i].optional && !arguments[i].variadic)
- break;
- // 2. Let t be a type list.
- // 3. Let o be an optionality list.
- // NOTE: We hold both of these in an Item instead.
- EffectiveOverloadSet::Item item;
- item.callable_id = overload_id;
- // 4. For each j in the range 0 to i − 1, inclusive:
- for (auto j = 0; j < i; ++j) {
- // 1. Append types[j] to t.
- item.types.append(types[j]);
- // 2. Append optionalityValues[j] to o.
- item.optionality_values.append(optionality_values[j]);
- }
- // 5. Append the tuple (X, t, o) to S.
- overloads.append(move(item));
- // 6. Set i to i − 1.
- --i;
- }
- overload_id++;
- }
- return EffectiveOverloadSet { move(overloads) };
- }
- static DeprecatedString generate_constructor_for_idl_type(Type const& type)
- {
- auto append_type_list = [](auto& builder, auto const& type_list) {
- bool first = true;
- for (auto const& child_type : type_list) {
- if (first) {
- first = false;
- } else {
- builder.append(", "sv);
- }
- builder.append(generate_constructor_for_idl_type(child_type));
- }
- };
- switch (type.kind()) {
- case Type::Kind::Plain:
- return DeprecatedString::formatted("make_ref_counted<IDL::Type>(\"{}\", {})", type.name(), type.is_nullable());
- case Type::Kind::Parameterized: {
- auto const& parameterized_type = type.as_parameterized();
- StringBuilder builder;
- builder.appendff("make_ref_counted<IDL::ParameterizedTypeType>(\"{}\", {}, Vector<NonnullRefPtr<IDL::Type const>> {{", type.name(), type.is_nullable());
- append_type_list(builder, parameterized_type.parameters());
- builder.append("})"sv);
- return builder.to_deprecated_string();
- }
- case Type::Kind::Union: {
- auto const& union_type = type.as_union();
- StringBuilder builder;
- builder.appendff("make_ref_counted<IDL::UnionType>(\"{}\", {}, Vector<NonnullRefPtr<IDL::Type const>> {{", type.name(), type.is_nullable());
- append_type_list(builder, union_type.member_types());
- builder.append("})"sv);
- return builder.to_deprecated_string();
- }
- }
- VERIFY_NOT_REACHED();
- }
- static void generate_overload_arbiter(SourceGenerator& generator, auto const& overload_set, DeprecatedString const& class_name)
- {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("class_name", class_name);
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@)
- {
- Optional<IDL::EffectiveOverloadSet> effective_overload_set;
- )~~~");
- auto all_possible_effective_overloads = compute_the_effective_overload_set(overload_set);
- auto overloads_set = all_possible_effective_overloads.items();
- auto maximum_argument_count = 0u;
- for (auto const& overload : overloads_set)
- maximum_argument_count = max(maximum_argument_count, overload.types.size());
- function_generator.set("max_argument_count", DeprecatedString::number(maximum_argument_count));
- function_generator.appendln(" switch (min(@max_argument_count@, vm.argument_count())) {");
- // Generate the effective overload set for each argument count.
- // This skips part of the Overload Resolution Algorithm https://webidl.spec.whatwg.org/#es-overloads
- // Namely, since that discards any overloads that don't have the exact number of arguments that were given,
- // we simply only provide the overloads that do have that number of arguments.
- for (auto argument_count = 0u; argument_count <= maximum_argument_count; ++argument_count) {
- // FIXME: Calculate the distinguishing argument index now instead of at runtime.
- auto effective_overload_count = 0;
- for (auto const& overload : overloads_set) {
- if (overload.types.size() == argument_count)
- effective_overload_count++;
- }
- if (effective_overload_count == 0)
- continue;
- function_generator.set("current_argument_count", DeprecatedString::number(argument_count));
- function_generator.set("overload_count", DeprecatedString::number(effective_overload_count));
- function_generator.appendln(R"~~~(
- case @current_argument_count@: {
- Vector<IDL::EffectiveOverloadSet::Item> overloads;
- overloads.ensure_capacity(@overload_count@);
- )~~~");
- for (auto& overload : overloads_set) {
- if (overload.types.size() != argument_count)
- continue;
- StringBuilder types_builder;
- types_builder.append("Vector<NonnullRefPtr<IDL::Type const>> { "sv);
- StringBuilder optionality_builder;
- optionality_builder.append("Vector<IDL::Optionality> { "sv);
- for (auto i = 0u; i < overload.types.size(); ++i) {
- if (i > 0) {
- types_builder.append(", "sv);
- optionality_builder.append(", "sv);
- }
- types_builder.append(generate_constructor_for_idl_type(overload.types[i]));
- optionality_builder.append("IDL::Optionality::"sv);
- switch (overload.optionality_values[i]) {
- case Optionality::Required:
- optionality_builder.append("Required"sv);
- break;
- case Optionality::Optional:
- optionality_builder.append("Optional"sv);
- break;
- case Optionality::Variadic:
- optionality_builder.append("Variadic"sv);
- break;
- }
- }
- types_builder.append("}"sv);
- optionality_builder.append("}"sv);
- function_generator.set("overload.callable_id", DeprecatedString::number(overload.callable_id));
- function_generator.set("overload.types", types_builder.to_deprecated_string());
- function_generator.set("overload.optionality_values", optionality_builder.to_deprecated_string());
- function_generator.appendln(" overloads.empend(@overload.callable_id@, @overload.types@, @overload.optionality_values@);");
- }
- function_generator.append(R"~~~(
- effective_overload_set.emplace(move(overloads));
- break;
- }
- )~~~");
- }
- function_generator.append(R"~~~(
- }
- if (!effective_overload_set.has_value())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::OverloadResolutionFailed);
- auto chosen_overload = TRY(WebIDL::resolve_overload(vm, effective_overload_set.value()));
- switch (chosen_overload.callable_id) {
- )~~~");
- for (auto i = 0u; i < overload_set.value.size(); ++i) {
- function_generator.set("overload_id", DeprecatedString::number(i));
- function_generator.append(R"~~~(
- case @overload_id@:
- return @function.name:snakecase@@overload_id@(vm);
- )~~~");
- }
- function_generator.append(R"~~~(
- default:
- VERIFY_NOT_REACHED();
- }
- }
- )~~~");
- }
- static void generate_prototype_or_global_mixin_declarations(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- for (auto const& overload_set : interface.overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@);
- )~~~");
- if (overload_set.value.size() > 1) {
- for (auto i = 0u; i < overload_set.value.size(); ++i) {
- function_generator.set("overload_suffix", DeprecatedString::number(i));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@@overload_suffix@);
- )~~~");
- }
- }
- }
- if (interface.has_stringifier) {
- auto stringifier_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- stringifier_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(to_string);
- )~~~");
- }
- if (interface.pair_iterator_types.has_value()) {
- auto iterator_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- iterator_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(entries);
- JS_DECLARE_NATIVE_FUNCTION(for_each);
- JS_DECLARE_NATIVE_FUNCTION(keys);
- JS_DECLARE_NATIVE_FUNCTION(values);
- )~~~");
- }
- for (auto& attribute : interface.attributes) {
- auto attribute_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- attribute_generator.set("attribute.name:snakecase", attribute.name.to_snakecase());
- attribute_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@attribute.name:snakecase@_getter);
- )~~~");
- if (!attribute.readonly || attribute.extended_attributes.contains("Replaceable"sv) || attribute.extended_attributes.contains("PutForwards"sv)) {
- attribute_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@attribute.name:snakecase@_setter);
- )~~~");
- }
- }
- generator.append(R"~~~(
- };
- )~~~");
- for (auto& it : interface.enumerations) {
- if (!it.value.is_original_definition)
- continue;
- auto enum_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- enum_generator.set("enum.type.name", it.key);
- enum_generator.append(R"~~~(
- enum class @enum.type.name@ {
- )~~~");
- for (auto& entry : it.value.translated_cpp_names) {
- enum_generator.set("enum.entry", entry.value);
- enum_generator.append(R"~~~(
- @enum.entry@,
- )~~~");
- }
- enum_generator.append(R"~~~(
- };
- )~~~");
- if (!interface.extended_attributes.contains("UseNewAKString")) {
- enum_generator.append(R"~~~(
- inline DeprecatedString idl_enum_to_deprecated_string(@enum.type.name@ value) {
- switch(value) {
- )~~~");
- for (auto& entry : it.value.translated_cpp_names) {
- enum_generator.set("enum.entry", entry.value);
- enum_generator.set("enum.string", entry.key);
- enum_generator.append(R"~~~(
- case @enum.type.name@::@enum.entry@: return "@enum.string@";
- )~~~");
- }
- enum_generator.append(R"~~~(
- default: return "<unknown>";
- };
- }
- )~~~");
- } else {
- enum_generator.append(R"~~~(
- inline ErrorOr<String> idl_enum_to_string(@enum.type.name@ value) {
- switch(value) {
- )~~~");
- for (auto& entry : it.value.translated_cpp_names) {
- enum_generator.set("enum.entry", entry.value);
- enum_generator.set("enum.string", entry.key);
- enum_generator.append(R"~~~(
- case @enum.type.name@::@enum.entry@: return "@enum.string@"_string;
- )~~~");
- }
- enum_generator.append(R"~~~(
- default: return "<unknown>"_string;
- };
- }
- )~~~");
- }
- }
- }
- // https://webidl.spec.whatwg.org/#create-an-inheritance-stack
- static Vector<Interface const&> create_an_inheritance_stack(IDL::Interface const& start_interface)
- {
- // 1. Let stack be a new stack.
- Vector<Interface const&> inheritance_chain;
- // 2. Push I onto stack.
- inheritance_chain.append(start_interface);
- // 3. While I inherits from an interface,
- auto const* current_interface = &start_interface;
- while (current_interface && !current_interface->parent_name.is_empty()) {
- // 1. Let I be that interface.
- auto imported_interface_iterator = start_interface.imported_modules.find_if([¤t_interface](IDL::Interface const& imported_interface) {
- return imported_interface.name == current_interface->parent_name;
- });
- // Inherited interfaces must have their IDL files imported.
- VERIFY(imported_interface_iterator != start_interface.imported_modules.end());
- // 2. Push I onto stack.
- inheritance_chain.append(*imported_interface_iterator);
- }
- // 4. Return stack.
- return inheritance_chain;
- }
- // https://webidl.spec.whatwg.org/#collect-attribute-values-of-an-inheritance-stack
- static void collect_attribute_values_of_an_inheritance_stack(SourceGenerator& function_generator, Vector<Interface const&> const& inheritance_chain)
- {
- // 1. Let I be the result of popping from stack.
- // 3. If stack is not empty, then invoke collect attribute values of an inheritance stack given object, stack, and map.
- for (auto const& interface_in_chain : inheritance_chain.in_reverse()) {
- // 2. Invoke collect attribute values given object, I, and map.
- // https://webidl.spec.whatwg.org/#collect-attribute-values
- // 1. If a toJSON operation with a [Default] extended attribute is declared on I, then for each exposed regular attribute attr that is an interface member of I, in order:
- auto to_json_iterator = interface_in_chain.functions.find_if([](IDL::Function const& function) {
- return function.name == "toJSON" && function.extended_attributes.contains("Default");
- });
- if (to_json_iterator == interface_in_chain.functions.end())
- continue;
- // FIXME: Check if the attributes are exposed.
- // 1. Let id be the identifier of attr.
- // 2. Let value be the result of running the getter steps of attr with object as this.
- // 3. If value is a JSON type, then set map[id] to value.
- // Since we are flatly generating the attributes, the consequent is replaced with these steps from "default toJSON steps":
- // 5. For each key → value of map,
- // 1. Let k be key converted to an ECMAScript value.
- // 2. Let v be value converted to an ECMAScript value.
- // 3. Perform ! CreateDataProperty(result, k, v).
- // NOTE: Functions, constructors and static functions cannot be JSON types, so they're not checked here.
- for (auto& attribute : interface_in_chain.attributes) {
- if (!attribute.type->is_json(interface_in_chain))
- continue;
- auto attribute_generator = function_generator.fork().release_value_but_fixme_should_propagate_errors();
- auto return_value_name = DeprecatedString::formatted("{}_retval", attribute.name.to_snakecase());
- attribute_generator.set("attribute.name", attribute.name);
- attribute_generator.set("attribute.return_value_name", return_value_name);
- if (attribute.extended_attributes.contains("ImplementedAs")) {
- auto implemented_as = attribute.extended_attributes.get("ImplementedAs").value();
- attribute_generator.set("attribute.cpp_name", implemented_as);
- } else {
- attribute_generator.set("attribute.cpp_name", attribute.name.to_snakecase());
- }
- if (attribute.extended_attributes.contains("Reflect")) {
- auto attribute_name = attribute.extended_attributes.get("Reflect").value();
- if (attribute_name.is_null())
- attribute_name = attribute.name;
- attribute_name = make_input_acceptable_cpp(attribute_name);
- attribute_generator.set("attribute.reflect_name", attribute_name);
- } else {
- attribute_generator.set("attribute.reflect_name", attribute.name.to_snakecase());
- }
- if (attribute.extended_attributes.contains("Reflect")) {
- if (attribute.type->name() != "boolean") {
- attribute_generator.append(R"~~~(
- auto @attribute.return_value_name@ = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
- )~~~");
- } else {
- attribute_generator.append(R"~~~(
- auto @attribute.return_value_name@ = impl->has_attribute(HTML::AttributeNames::@attribute.reflect_name@);
- )~~~");
- }
- } else {
- attribute_generator.append(R"~~~(
- auto @attribute.return_value_name@ = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->@attribute.cpp_name@(); }));
- )~~~");
- }
- generate_wrap_statement(attribute_generator, return_value_name, attribute.type, interface_in_chain, DeprecatedString::formatted("auto {}_wrapped =", return_value_name));
- attribute_generator.append(R"~~~(
- MUST_OR_THROW_OOM(result->create_data_property("@attribute.name@", @attribute.return_value_name@_wrapped));
- )~~~");
- }
- for (auto& constant : interface_in_chain.constants) {
- auto constant_generator = function_generator.fork().release_value_but_fixme_should_propagate_errors();
- constant_generator.set("constant.name", constant.name);
- generate_wrap_statement(constant_generator, constant.value, constant.type, interface_in_chain, DeprecatedString::formatted("auto constant_{}_value =", constant.name));
- constant_generator.append(R"~~~(
- MUST_OR_THROW_OOM(result->create_data_property("@constant.name@", constant_@constant.name@_value));
- )~~~");
- }
- }
- }
- // https://webidl.spec.whatwg.org/#default-tojson-steps
- static void generate_default_to_json_function(SourceGenerator& generator, DeprecatedString const& class_name, IDL::Interface const& start_interface)
- {
- // NOTE: This is done heavily out of order since the spec mixes parse time and run time type information together.
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("class_name", class_name);
- // 4. Let result be OrdinaryObjectCreate(%Object.prototype%).
- function_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::to_json)
- {
- auto& realm = *vm.current_realm();
- auto* impl = TRY(impl_from(vm));
- auto result = JS::Object::create(realm, realm.intrinsics().object_prototype());
- )~~~");
- // 1. Let map be a new ordered map.
- // NOTE: Instead of making a map, we flatly generate the attributes.
- // 2. Let stack be the result of creating an inheritance stack for interface I.
- auto inheritance_chain = create_an_inheritance_stack(start_interface);
- // 3. Invoke collect attribute values of an inheritance stack given this, stack, and map.
- collect_attribute_values_of_an_inheritance_stack(function_generator, inheritance_chain);
- // NOTE: Step 5 is done as part of collect_attribute_values_of_an_inheritance_stack, due to us flatly generating the attributes.
- // 6. Return result.
- function_generator.append(R"~~~(
- return result;
- }
- )~~~");
- }
- static void generate_prototype_or_global_mixin_definitions(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- auto is_global_interface = interface.extended_attributes.contains("Global");
- auto class_name = is_global_interface ? interface.global_mixin_class : interface.prototype_class;
- generator.set("name", interface.name);
- generator.set("namespaced_name", interface.namespaced_name);
- generator.set("class_name", class_name);
- generator.set("fully_qualified_name", interface.fully_qualified_name);
- generator.set("parent_name", interface.parent_name);
- generator.set("prototype_base_class", interface.prototype_base_class);
- if (interface.pair_iterator_types.has_value()) {
- generator.set("iterator_name", DeprecatedString::formatted("{}Iterator", interface.name));
- }
- if (is_global_interface) {
- // Doing this with macros is not super nice, but simplifies codegen a lot.
- generator.append(R"~~~(
- #define define_direct_property (object.define_direct_property)
- #define define_native_accessor (object.define_native_accessor)
- #define define_native_function (object.define_native_function)
- #define set_prototype (object.set_prototype)
- void @class_name@::initialize(JS::Realm& realm, JS::Object& object)
- {
- )~~~");
- } else {
- generator.append(R"~~~(
- void @class_name@::initialize(JS::Realm& realm)
- {
- )~~~");
- }
- // FIXME: Currently almost everything gets default_attributes but it should be configurable per attribute.
- // See the spec links for details
- generator.append(R"~~~(
- [[maybe_unused]] auto& vm = realm.vm();
- [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable;
- )~~~");
- if (interface.prototype_base_class == "ObjectPrototype") {
- generator.append(R"~~~(
- set_prototype(realm.intrinsics().object_prototype());
- )~~~");
- } else {
- generator.append(R"~~~(
- set_prototype(&ensure_web_prototype<@prototype_base_class@>(realm, "@parent_name@"));
- )~~~");
- }
- if (interface.has_unscopable_member) {
- generator.append(R"~~~(
- auto unscopable_object = JS::Object::create(realm, nullptr);
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#es-attributes
- for (auto& attribute : interface.attributes) {
- auto attribute_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- attribute_generator.set("attribute.name", attribute.name);
- attribute_generator.set("attribute.getter_callback", attribute.getter_callback_name);
- if (!attribute.readonly || attribute.extended_attributes.contains("Replaceable"sv) || attribute.extended_attributes.contains("PutForwards"sv))
- attribute_generator.set("attribute.setter_callback", attribute.setter_callback_name);
- else
- attribute_generator.set("attribute.setter_callback", "nullptr");
- if (attribute.extended_attributes.contains("Unscopable")) {
- attribute_generator.append(R"~~~(
- MUST(unscopable_object->create_data_property("@attribute.name@", JS::Value(true)));
- )~~~");
- }
- attribute_generator.append(R"~~~(
- define_native_accessor(realm, "@attribute.name@", @attribute.getter_callback@, @attribute.setter_callback@, default_attributes);
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#es-constants
- for (auto& constant : interface.constants) {
- // FIXME: Do constants need to be added to the unscopable list?
- auto constant_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- constant_generator.set("constant.name", constant.name);
- generate_wrap_statement(constant_generator, constant.value, constant.type, interface, DeprecatedString::formatted("auto constant_{}_value =", constant.name));
- constant_generator.append(R"~~~(
- define_direct_property("@constant.name@", constant_@constant.name@_value, JS::Attribute::Enumerable);
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#es-operations
- for (auto const& overload_set : interface.overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name", overload_set.key);
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.set("function.length", DeprecatedString::number(get_shortest_function_length(overload_set.value)));
- // FIXME: What if only some of the overloads are Unscopable?
- if (any_of(overload_set.value, [](auto const& function) { return function.extended_attributes.contains("Unscopable"); })) {
- function_generator.append(R"~~~(
- MUST(unscopable_object->create_data_property("@function.name@", JS::Value(true)));
- )~~~");
- }
- function_generator.append(R"~~~(
- define_native_function(realm, "@function.name@", @function.name:snakecase@, @function.length@, default_attributes);
- )~~~");
- }
- if (interface.has_stringifier) {
- // FIXME: Do stringifiers need to be added to the unscopable list?
- auto stringifier_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- stringifier_generator.append(R"~~~(
- define_native_function(realm, "toString", to_string, 0, default_attributes);
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#define-the-iteration-methods
- // This applies to this if block and the following if block.
- if (interface.indexed_property_getter.has_value()) {
- auto iterator_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- iterator_generator.append(R"~~~(
- define_direct_property(vm.well_known_symbol_iterator(), realm.intrinsics().array_prototype()->get_without_side_effects(vm.names.values), JS::Attribute::Configurable | JS::Attribute::Writable);
- )~~~");
- if (interface.value_iterator_type.has_value()) {
- iterator_generator.append(R"~~~(
- define_direct_property(vm.names.entries, realm.intrinsics().array_prototype()->get_without_side_effects(vm.names.entries), default_attributes);
- define_direct_property(vm.names.keys, realm.intrinsics().array_prototype()->get_without_side_effects(vm.names.keys), default_attributes);
- define_direct_property(vm.names.values, realm.intrinsics().array_prototype()->get_without_side_effects(vm.names.values), default_attributes);
- define_direct_property(vm.names.forEach, realm.intrinsics().array_prototype()->get_without_side_effects(vm.names.forEach), default_attributes);
- )~~~");
- }
- }
- if (interface.pair_iterator_types.has_value()) {
- // FIXME: Do pair iterators need to be added to the unscopable list?
- auto iterator_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- iterator_generator.append(R"~~~(
- define_native_function(realm, vm.names.entries, entries, 0, default_attributes);
- define_native_function(realm, vm.names.forEach, for_each, 1, default_attributes);
- define_native_function(realm, vm.names.keys, keys, 0, default_attributes);
- define_native_function(realm, vm.names.values, values, 0, default_attributes);
- define_direct_property(vm.well_known_symbol_iterator(), get_without_side_effects(vm.names.entries), JS::Attribute::Configurable | JS::Attribute::Writable);
- )~~~");
- }
- if (interface.has_unscopable_member) {
- generator.append(R"~~~(
- define_direct_property(vm.well_known_symbol_unscopables(), unscopable_object, JS::Attribute::Configurable);
- )~~~");
- }
- generator.append(R"~~~(
- define_direct_property(vm.well_known_symbol_to_string_tag(), MUST(JS::PrimitiveString::create(vm, "@namespaced_name@"sv)), JS::Attribute::Configurable);
- )~~~");
- if (!is_global_interface) {
- generator.append(R"~~~(
- Base::initialize(realm);
- )~~~");
- }
- generator.append(R"~~~(
- }
- )~~~");
- if (!interface.attributes.is_empty() || !interface.functions.is_empty() || interface.has_stringifier) {
- generator.append(R"~~~(
- static JS::ThrowCompletionOr<@fully_qualified_name@*> impl_from(JS::VM& vm)
- {
- auto this_value = vm.this_value();
- JS::Object* this_object = nullptr;
- if (this_value.is_nullish())
- this_object = &vm.current_realm()->global_object();
- else
- this_object = TRY(this_value.to_object(vm));
- )~~~");
- if (interface.name.is_one_of("EventTarget", "Window")) {
- generator.append(R"~~~(
- if (is<HTML::Window>(this_object)) {
- return static_cast<HTML::Window*>(this_object);
- }
- if (is<HTML::WindowProxy>(this_object)) {
- return static_cast<HTML::WindowProxy*>(this_object)->window().ptr();
- }
- )~~~");
- }
- generator.append(R"~~~(
- if (!is<@fully_qualified_name@>(this_object))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@namespaced_name@");
- return static_cast<@fully_qualified_name@*>(this_object);
- }
- )~~~");
- }
- for (auto& attribute : interface.attributes) {
- auto attribute_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- attribute_generator.set("attribute.name", attribute.name);
- attribute_generator.set("attribute.getter_callback", attribute.getter_callback_name);
- attribute_generator.set("attribute.setter_callback", attribute.setter_callback_name);
- if (attribute.extended_attributes.contains("ImplementedAs")) {
- auto implemented_as = attribute.extended_attributes.get("ImplementedAs").value();
- attribute_generator.set("attribute.cpp_name", implemented_as);
- } else {
- attribute_generator.set("attribute.cpp_name", attribute.name.to_snakecase());
- }
- if (attribute.extended_attributes.contains("Reflect")) {
- auto attribute_name = attribute.extended_attributes.get("Reflect").value();
- if (attribute_name.is_null())
- attribute_name = attribute.name;
- attribute_name = make_input_acceptable_cpp(attribute_name);
- attribute_generator.set("attribute.reflect_name", attribute_name);
- } else {
- attribute_generator.set("attribute.reflect_name", attribute.name.to_snakecase());
- }
- // For [CEReactions]: https://html.spec.whatwg.org/multipage/custom-elements.html#cereactions
- attribute_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
- {
- [[maybe_unused]] auto& realm = *vm.current_realm();
- auto* impl = TRY(impl_from(vm));
- )~~~");
- if (attribute.extended_attributes.contains("CEReactions")) {
- // 1. Push a new element queue onto this object's relevant agent's custom element reactions stack.
- attribute_generator.append(R"~~~(
- auto& relevant_agent = HTML::relevant_agent(*impl);
- auto* custom_data = verify_cast<Bindings::WebEngineCustomData>(relevant_agent.custom_data());
- auto& reactions_stack = custom_data->custom_element_reactions_stack;
- reactions_stack.element_queue_stack.append({});
- )~~~");
- }
- if (attribute.extended_attributes.contains("Reflect")) {
- if (attribute.type->name() != "boolean") {
- attribute_generator.append(R"~~~(
- auto retval = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
- )~~~");
- } else {
- attribute_generator.append(R"~~~(
- auto retval = impl->has_attribute(HTML::AttributeNames::@attribute.reflect_name@);
- )~~~");
- }
- if (attribute.extended_attributes.contains("CEReactions")) {
- // 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
- // 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
- // 4. Invoke custom element reactions in queue.
- // 5. If an exception exception was thrown by the original steps, rethrow exception.
- // 6. If a value value was returned from the original steps, return value.
- attribute_generator.append(R"~~~(
- auto queue = reactions_stack.element_queue_stack.take_last();
- Bindings::invoke_custom_element_reactions(queue);
- )~~~");
- }
- } else {
- if (!attribute.extended_attributes.contains("CEReactions")) {
- attribute_generator.append(R"~~~(
- auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->@attribute.cpp_name@(); }));
- )~~~");
- } else {
- // 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
- // 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
- // 4. Invoke custom element reactions in queue.
- // 5. If an exception exception was thrown by the original steps, rethrow exception.
- // 6. If a value value was returned from the original steps, return value.
- attribute_generator.append(R"~~~(
- auto retval_or_exception = throw_dom_exception_if_needed(vm, [&] { return impl->@attribute.cpp_name@(); });
- auto queue = reactions_stack.element_queue_stack.take_last();
- Bindings::invoke_custom_element_reactions(queue);
- if (retval_or_exception.is_error())
- return retval_or_exception.release_error();
- auto retval = retval_or_exception.release_value();
- )~~~");
- }
- }
- generate_return_statement(generator, *attribute.type, interface);
- attribute_generator.append(R"~~~(
- }
- )~~~");
- if (!attribute.readonly) {
- // For [CEReactions]: https://html.spec.whatwg.org/multipage/custom-elements.html#cereactions
- attribute_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
- {
- [[maybe_unused]] auto& realm = *vm.current_realm();
- auto* impl = TRY(impl_from(vm));
- auto value = vm.argument(0);
- )~~~");
- if (attribute.extended_attributes.contains("CEReactions")) {
- // 1. Push a new element queue onto this object's relevant agent's custom element reactions stack.
- attribute_generator.append(R"~~~(
- auto& relevant_agent = HTML::relevant_agent(*impl);
- auto* custom_data = verify_cast<Bindings::WebEngineCustomData>(relevant_agent.custom_data());
- auto& reactions_stack = custom_data->custom_element_reactions_stack;
- reactions_stack.element_queue_stack.append({});
- )~~~");
- }
- generate_to_cpp(generator, attribute, "value", "", "cpp_value", interface, attribute.extended_attributes.contains("LegacyNullToEmptyString"));
- if (attribute.extended_attributes.contains("Reflect")) {
- if (attribute.type->name() != "boolean") {
- attribute_generator.append(R"~~~(
- MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, cpp_value));
- )~~~");
- } else {
- attribute_generator.append(R"~~~(
- if (!cpp_value)
- impl->remove_attribute(HTML::AttributeNames::@attribute.reflect_name@);
- else
- MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, DeprecatedString::empty()));
- )~~~");
- }
- if (attribute.extended_attributes.contains("CEReactions")) {
- // 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
- // 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
- // 4. Invoke custom element reactions in queue.
- // 5. If an exception exception was thrown by the original steps, rethrow exception.
- // 6. If a value value was returned from the original steps, return value.
- attribute_generator.append(R"~~~(
- auto queue = reactions_stack.element_queue_stack.take_last();
- Bindings::invoke_custom_element_reactions(queue);
- )~~~");
- }
- } else {
- if (!attribute.extended_attributes.contains("CEReactions")) {
- attribute_generator.append(R"~~~(
- TRY(throw_dom_exception_if_needed(vm, [&] { return impl->set_@attribute.cpp_name@(cpp_value); }));
- )~~~");
- } else {
- // 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
- // 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
- // 4. Invoke custom element reactions in queue.
- // 5. If an exception exception was thrown by the original steps, rethrow exception.
- // 6. If a value value was returned from the original steps, return value.
- attribute_generator.append(R"~~~(
- auto maybe_exception = throw_dom_exception_if_needed(vm, [&] { return impl->set_@attribute.cpp_name@(cpp_value); });
- auto queue = reactions_stack.element_queue_stack.take_last();
- Bindings::invoke_custom_element_reactions(queue);
- if (maybe_exception.is_error())
- return maybe_exception.release_error();
- )~~~");
- }
- }
- attribute_generator.append(R"~~~(
- return JS::js_undefined();
- }
- )~~~");
- } else if (attribute.extended_attributes.contains("Replaceable"sv)) {
- attribute_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
- {
- auto this_value = vm.this_value();
- if (!this_value.is_object() || !is<@fully_qualified_name@>(this_value.as_object()))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@namespaced_name@");
- TRY(this_value.as_object().internal_define_own_property("@attribute.name@", JS::PropertyDescriptor { .value = vm.argument(0), .writable = true }));
- return JS::js_undefined();
- }
- )~~~");
- } else if (auto put_forwards_identifier = attribute.extended_attributes.get("PutForwards"sv); put_forwards_identifier.has_value()) {
- attribute_generator.set("put_forwards_identifier"sv, *put_forwards_identifier);
- attribute_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
- {
- auto* impl = TRY(impl_from(vm));
- auto value = vm.argument(0);
- auto receiver = TRY(throw_dom_exception_if_needed(vm, [&]() { return impl->@attribute.cpp_name@(); }));
- TRY(receiver->set(JS::PropertyKey { "@put_forwards_identifier@" }, value, JS::Object::ShouldThrowExceptions::Yes));
- return JS::js_undefined();
- }
- )~~~");
- }
- }
- // Implementation: Functions
- for (auto& function : interface.functions) {
- if (function.extended_attributes.contains("Default")) {
- if (function.name == "toJSON"sv && function.return_type->name() == "object"sv) {
- generate_default_to_json_function(generator, class_name, interface);
- continue;
- }
- dbgln("Unknown default operation: {} {}()", function.return_type->name(), function.name);
- VERIFY_NOT_REACHED();
- }
- generate_function(generator, function, StaticFunction::No, class_name, interface.fully_qualified_name, interface);
- }
- for (auto const& overload_set : interface.overload_sets) {
- if (overload_set.value.size() == 1)
- continue;
- generate_overload_arbiter(generator, overload_set, class_name);
- }
- if (interface.has_stringifier) {
- auto stringifier_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- stringifier_generator.set("class_name", class_name);
- if (interface.stringifier_attribute.has_value())
- stringifier_generator.set("attribute.cpp_getter_name", interface.stringifier_attribute->to_snakecase());
- stringifier_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::to_string)
- {
- [[maybe_unused]] auto& realm = *vm.current_realm();
- auto* impl = TRY(impl_from(vm));
- )~~~");
- if (interface.stringifier_attribute.has_value()) {
- stringifier_generator.append(R"~~~(
- auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->@attribute.cpp_getter_name@(); }));
- )~~~");
- } else {
- if (!interface.extended_attributes.contains("UseNewAKString")) {
- stringifier_generator.append(R"~~~(
- auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->to_deprecated_string(); }));
- )~~~");
- } else {
- stringifier_generator.append(R"~~~(
- auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->to_string(); }));
- )~~~");
- }
- }
- stringifier_generator.append(R"~~~(
- return JS::PrimitiveString::create(vm, move(retval));
- }
- )~~~");
- }
- if (interface.pair_iterator_types.has_value()) {
- auto iterator_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- iterator_generator.append(R"~~~(
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::entries)
- {
- auto* impl = TRY(impl_from(vm));
- return TRY(throw_dom_exception_if_needed(vm, [&] { return @iterator_name@::create(*impl, Object::PropertyKind::KeyAndValue); }));
- }
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::for_each)
- {
- [[maybe_unused]] auto& realm = *vm.current_realm();
- auto* impl = TRY(impl_from(vm));
- auto callback = vm.argument(0);
- if (!callback.is_function())
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, callback.to_string_without_side_effects()));
- auto this_value = vm.this_value();
- TRY(impl->for_each([&](auto key, auto value) -> JS::ThrowCompletionOr<void> {
- )~~~");
- generate_variable_statement(iterator_generator, "wrapped_key", interface.pair_iterator_types->get<0>(), "key", interface);
- generate_variable_statement(iterator_generator, "wrapped_value", interface.pair_iterator_types->get<1>(), "value", interface);
- iterator_generator.append(R"~~~(
- TRY(call(vm, callback.as_function(), vm.argument(1), wrapped_value, wrapped_key, this_value));
- return {};
- }));
- return JS::js_undefined();
- }
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::keys)
- {
- auto* impl = TRY(impl_from(vm));
- return TRY(throw_dom_exception_if_needed(vm, [&] { return @iterator_name@::create(*impl, Object::PropertyKind::Key); }));
- }
- JS_DEFINE_NATIVE_FUNCTION(@class_name@::values)
- {
- auto* impl = TRY(impl_from(vm));
- return TRY(throw_dom_exception_if_needed(vm, [&] { return @iterator_name@::create(*impl, Object::PropertyKind::Value); }));
- }
- )~~~");
- }
- }
- void generate_namespace_header(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("namespace_class", interface.namespace_class);
- generator.append(R"~~~(
- #pragma once
- #include <LibJS/Runtime/Object.h>
- namespace Web::Bindings {
- class @namespace_class@ final : public JS::Object {
- JS_OBJECT(@namespace_class@, JS::Object);
- public:
- explicit @namespace_class@(JS::Realm&);
- virtual void initialize(JS::Realm&) override;
- virtual ~@namespace_class@() override;
- private:
- )~~~");
- if (interface.extended_attributes.contains("WithGCVistor"sv)) {
- generator.append(R"~~~(
- virtual void visit_edges(JS::Cell::Visitor&) override;
- )~~~");
- }
- for (auto const& overload_set : interface.overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@);
- )~~~");
- if (overload_set.value.size() > 1) {
- for (auto i = 0u; i < overload_set.value.size(); ++i) {
- function_generator.set("overload_suffix", DeprecatedString::number(i));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@@overload_suffix@);
- )~~~");
- }
- }
- }
- generator.append(R"~~~(
- };
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_namespace_implementation(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("name", interface.name);
- generator.set("namespace_class", interface.namespace_class);
- generator.append(R"~~~(
- #include <AK/Function.h>
- #include <LibIDL/Types.h>
- #include <LibJS/Runtime/ArrayBuffer.h>
- #include <LibJS/Runtime/DataView.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/PrimitiveString.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibJS/Runtime/Value.h>
- #include <LibWeb/Bindings/@namespace_class@.h>
- #include <LibWeb/Bindings/ExceptionOrUtils.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #include <LibWeb/HTML/Window.h>
- #include <LibWeb/HTML/WindowProxy.h>
- #include <LibWeb/WebIDL/OverloadResolution.h>
- )~~~");
- for (auto& path : interface.required_imported_paths)
- generate_include_for(generator, path);
- emit_includes_for_all_imports(interface, generator, interface.pair_iterator_types.has_value());
- generator.append(R"~~~(
- // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
- using namespace Web::CSS;
- using namespace Web::DOM;
- using namespace Web::DOMParsing;
- using namespace Web::Fetch;
- using namespace Web::FileAPI;
- using namespace Web::Geometry;
- using namespace Web::HighResolutionTime;
- using namespace Web::HTML;
- using namespace Web::Internals;
- using namespace Web::IntersectionObserver;
- using namespace Web::RequestIdleCallback;
- using namespace Web::ResizeObserver;
- using namespace Web::Selection;
- using namespace Web::Streams;
- using namespace Web::UIEvents;
- using namespace Web::URL;
- using namespace Web::XHR;
- using namespace Web::WebAssembly;
- using namespace Web::WebAudio;
- using namespace Web::WebGL;
- using namespace Web::WebIDL;
- namespace Web::Bindings {
- @namespace_class@::@namespace_class@(JS::Realm& realm)
- : Object(ConstructWithoutPrototypeTag::Tag, realm)
- {
- }
- @namespace_class@::~@namespace_class@()
- {
- }
- void @namespace_class@::initialize(JS::Realm& realm)
- {
- [[maybe_unused]] auto& vm = this->vm();
- [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;
- Base::initialize(realm);
- )~~~");
- // https://webidl.spec.whatwg.org/#es-operations
- for (auto const& overload_set : interface.overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name", overload_set.key);
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.set("function.length", DeprecatedString::number(get_shortest_function_length(overload_set.value)));
- function_generator.append(R"~~~(
- define_native_function(realm, "@function.name@", @function.name:snakecase@, @function.length@, default_attributes);
- )~~~");
- }
- generator.append(R"~~~(
- }
- )~~~");
- if (interface.extended_attributes.contains("WithGCVistor"sv)) {
- generator.append(R"~~~(
- void @namespace_class@::visit_edges(JS::Cell::Visitor& visitor)
- {
- Base::visit_edges(visitor);
- @name@::visit_edges(visitor);
- }
- )~~~");
- }
- for (auto const& function : interface.functions)
- generate_function(generator, function, StaticFunction::Yes, interface.namespace_class, interface.name, interface);
- for (auto const& overload_set : interface.overload_sets) {
- if (overload_set.value.size() == 1)
- continue;
- generate_overload_arbiter(generator, overload_set, interface.namespace_class);
- }
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_constructor_header(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("constructor_class", interface.constructor_class);
- generator.append(R"~~~(
- #pragma once
- #include <LibJS/Runtime/NativeFunction.h>
- namespace Web::Bindings {
- class @constructor_class@ : public JS::NativeFunction {
- JS_OBJECT(@constructor_class@, JS::NativeFunction);
- public:
- explicit @constructor_class@(JS::Realm&);
- virtual void initialize(JS::Realm&) override;
- virtual ~@constructor_class@() override;
- virtual JS::ThrowCompletionOr<JS::Value> call() override;
- virtual JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> construct(JS::FunctionObject& new_target) override;
- private:
- virtual bool has_constructor() const override { return true; }
- )~~~");
- for (auto const& overload_set : interface.static_overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@);
- )~~~");
- if (overload_set.value.size() > 1) {
- for (auto i = 0u; i < overload_set.value.size(); ++i) {
- function_generator.set("overload_suffix", DeprecatedString::number(i));
- function_generator.append(R"~~~(
- JS_DECLARE_NATIVE_FUNCTION(@function.name:snakecase@@overload_suffix@);
- )~~~");
- }
- }
- }
- generator.append(R"~~~(
- };
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_constructor_implementation(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("name", interface.name);
- generator.set("namespaced_name", interface.namespaced_name);
- generator.set("prototype_class", interface.prototype_class);
- generator.set("constructor_class", interface.constructor_class);
- generator.set("fully_qualified_name", interface.fully_qualified_name);
- generator.append(R"~~~(
- #include <LibJS/Heap/Heap.h>
- #include <LibJS/Runtime/ArrayBuffer.h>
- #include <LibJS/Runtime/DataView.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/Iterator.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibWeb/Bindings/@constructor_class@.h>
- #include <LibWeb/Bindings/@prototype_class@.h>
- #include <LibWeb/Bindings/ExceptionOrUtils.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #if __has_include(<LibWeb/Crypto/@name@.h>)
- # include <LibWeb/Crypto/@name@.h>
- #elif __has_include(<LibWeb/CSS/@name@.h>)
- # include <LibWeb/CSS/@name@.h>
- #elif __has_include(<LibWeb/DOM/@name@.h>)
- # include <LibWeb/DOM/@name@.h>
- #elif __has_include(<LibWeb/Encoding/@name@.h>)
- # include <LibWeb/Encoding/@name@.h>
- #elif __has_include(<LibWeb/Fetch/@name@.h>)
- # include <LibWeb/Fetch/@name@.h>
- #elif __has_include(<LibWeb/FileAPI/@name@.h>)
- # include <LibWeb/FileAPI/@name@.h>
- #elif __has_include(<LibWeb/Geometry/@name@.h>)
- # include <LibWeb/Geometry/@name@.h>
- #elif __has_include(<LibWeb/HTML/@name@.h>)
- # include <LibWeb/HTML/@name@.h>
- #elif __has_include(<LibWeb/UIEvents/@name@.h>)
- # include <LibWeb/UIEvents/@name@.h>
- #elif __has_include(<LibWeb/HighResolutionTime/@name@.h>)
- # include <LibWeb/HighResolutionTime/@name@.h>
- #elif __has_include(<LibWeb/IntersectionObserver/@name@.h>)
- # include <LibWeb/IntersectionObserver/@name@.h>
- #elif __has_include(<LibWeb/NavigationTiming/@name@.h>)
- # include <LibWeb/NavigationTiming/@name@.h>
- #elif __has_include(<LibWeb/RequestIdleCallback/@name@.h>)
- # include <LibWeb/RequestIdleCallback/@name@.h>
- #elif __has_include(<LibWeb/ResizeObserver/@name@.h>)
- # include <LibWeb/ResizeObserver/@name@.h>
- #elif __has_include(<LibWeb/SVG/@name@.h>)
- # include <LibWeb/SVG/@name@.h>
- #elif __has_include(<LibWeb/Selection/@name@.h>)
- # include <LibWeb/Selection/@name@.h>
- #elif __has_include(<LibWeb/WebSockets/@name@.h>)
- # include <LibWeb/WebSockets/@name@.h>
- #elif __has_include(<LibWeb/XHR/@name@.h>)
- # include <LibWeb/XHR/@name@.h>
- #elif __has_include(<LibWeb/URL/@name@.h>)
- # include <LibWeb/URL/@name@.h>
- #endif
- #include <LibWeb/WebIDL/CallbackType.h>
- )~~~");
- if (interface.constructors.size() == 1) {
- auto& constructor = interface.constructors[0];
- if (constructor.extended_attributes.contains("HTMLConstructor"sv)) {
- generator.append(R"~~~(
- #include <LibJS/Runtime/AbstractOperations.h>
- #include <LibWeb/DOM/ElementFactory.h>
- #include <LibWeb/HTML/CustomElements/CustomElementRegistry.h>
- #include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
- #include <LibWeb/HTML/Window.h>
- #include <LibWeb/Namespace.h>
- )~~~");
- }
- }
- for (auto& path : interface.required_imported_paths)
- generate_include_for(generator, path);
- emit_includes_for_all_imports(interface, generator, interface.pair_iterator_types.has_value());
- generator.append(R"~~~(
- // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
- using namespace Web::CSS;
- using namespace Web::DOM;
- using namespace Web::DOMParsing;
- using namespace Web::Fetch;
- using namespace Web::FileAPI;
- using namespace Web::Geometry;
- using namespace Web::HighResolutionTime;
- using namespace Web::HTML;
- using namespace Web::Internals;
- using namespace Web::IntersectionObserver;
- using namespace Web::PerformanceTimeline;
- using namespace Web::RequestIdleCallback;
- using namespace Web::ResizeObserver;
- using namespace Web::Selection;
- using namespace Web::Streams;
- using namespace Web::UIEvents;
- using namespace Web::UserTiming;
- using namespace Web::URL;
- using namespace Web::XHR;
- using namespace Web::WebAssembly;
- using namespace Web::WebAudio;
- using namespace Web::WebGL;
- using namespace Web::WebIDL;
- namespace Web::Bindings {
- @constructor_class@::@constructor_class@(JS::Realm& realm)
- : NativeFunction("@name@"sv, realm.intrinsics().function_prototype())
- {
- }
- @constructor_class@::~@constructor_class@()
- {
- }
- JS::ThrowCompletionOr<JS::Value> @constructor_class@::call()
- {
- return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "@namespaced_name@");
- }
- JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> @constructor_class@::construct([[maybe_unused]] FunctionObject& new_target)
- {
- )~~~");
- if (interface.constructors.is_empty()) {
- // No constructor
- generator.set("constructor.length", "0");
- generator.append(R"~~~(
- return vm().throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, "@namespaced_name@");
- )~~~");
- } else if (interface.constructors.size() == 1) {
- // Single constructor
- auto& constructor = interface.constructors[0];
- generator.set("constructor.length", DeprecatedString::number(constructor.shortest_length()));
- generator.append(R"~~~(
- auto& vm = this->vm();
- auto& realm = *vm.current_realm();
- )~~~");
- if (!constructor.extended_attributes.contains("HTMLConstructor"sv)) {
- if (!constructor.parameters.is_empty()) {
- generate_argument_count_check(generator, constructor.name, constructor.shortest_length());
- StringBuilder arguments_builder;
- generate_arguments(generator, constructor.parameters, arguments_builder, interface);
- generator.set(".constructor_arguments", arguments_builder.string_view());
- generator.append(R"~~~(
- auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return @fully_qualified_name@::construct_impl(realm, @.constructor_arguments@); }));
- )~~~");
- } else {
- generator.append(R"~~~(
- auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return @fully_qualified_name@::construct_impl(realm); }));
- )~~~");
- }
- generator.append(R"~~~(
- return *impl;
- )~~~");
- } else {
- // https://html.spec.whatwg.org/multipage/dom.html#html-element-constructors
- // NOTE: The active function object in this context is always going to be the current constructor that has just been called.
- generator.append(R"~~~(
- auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
- // 1. Let registry be the current global object's CustomElementRegistry object.
- auto registry = TRY(throw_dom_exception_if_needed(vm, [&] { return window.custom_elements(); }));
- // 2. If NewTarget is equal to the active function object, then throw a TypeError.
- if (&new_target == vm.active_function_object())
- return vm.throw_completion<JS::TypeError>("Cannot directly construct an HTML element, it must be inherited"sv);
- // 3. Let definition be the entry in registry with constructor equal to NewTarget. If there is no such definition, then throw a TypeError.
- auto definition = registry->get_definition_from_new_target(new_target);
- if (!definition)
- return vm.throw_completion<JS::TypeError>("There is no custom element definition assigned to the given constructor"sv);
- // 4. Let is value be null.
- Optional<String> is_value;
- // 5. If definition's local name is equal to definition's name (i.e., definition is for an autonomous custom element), then:
- if (definition->local_name() == definition->name()) {
- // 1. If the active function object is not HTMLElement, then throw a TypeError.
- )~~~");
- if (interface.name != "HTMLElement") {
- generator.append(R"~~~(
- return vm.throw_completion<JS::TypeError>("Autonomous custom elements can only inherit from HTMLElement"sv);
- )~~~");
- } else {
- generator.append(R"~~~(
- // Do nothing, as this is the HTMLElement constructor.
- )~~~");
- }
- generator.append(R"~~~(
- }
- // 6. Otherwise (i.e., if definition is for a customized built-in element):
- else {
- // 1. Let valid local names be the list of local names for elements defined in this specification or in other applicable specifications that use the active function object as their element interface.
- static auto valid_local_names = TRY_OR_THROW_OOM(vm, DOM::valid_local_names_for_given_html_element_interface("@name@"sv));
- // 2. If valid local names does not contain definition's local name, then throw a TypeError.
- if (!valid_local_names.contains_slow(definition->local_name().to_deprecated_string()))
- return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Local name '{}' of customized built-in element is not a valid local name for @name@"sv, definition->local_name())));
- // 3. Set is value to definition's name.
- is_value = definition->name();
- }
- // 7. If definition's construction stack is empty, then:
- if (definition->construction_stack().is_empty()) {
- // 1. Let element be the result of internally creating a new object implementing the interface to which the active function object corresponds, given the current Realm Record and NewTarget.
- // 2. Set element's node document to the current global object's associated Document.
- // 3. Set element's namespace to the HTML namespace.
- // 4. Set element's namespace prefix to null.
- // 5. Set element's local name to definition's local name.
- auto element = realm.heap().allocate<@fully_qualified_name@>(realm, window.associated_document(), DOM::QualifiedName { definition->local_name().to_deprecated_string(), {}, Namespace::HTML }).release_allocated_value_but_fixme_should_propagate_errors();
- // https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface
- // Important steps from "internally create a new object implementing the interface"
- // 3.2: Let prototype be ? Get(newTarget, "prototype").
- auto prototype = TRY(new_target.get(vm.names.prototype));
- // 3.3. If Type(prototype) is not Object, then:
- if (!prototype.is_object()) {
- // 1. Let targetRealm be ? GetFunctionRealm(newTarget).
- auto* target_realm = TRY(JS::get_function_realm(vm, new_target));
- // 2. Set prototype to the interface prototype object for interface in targetRealm.
- VERIFY(target_realm);
- prototype = &Bindings::ensure_web_prototype<@prototype_class@>(*target_realm, "@name@"sv);
- }
- // 7. Set instance.[[Prototype]] to prototype.
- VERIFY(prototype.is_object());
- MUST(element->internal_set_prototype_of(&prototype.as_object()));
- // 6. Set element's custom element state to "custom".
- // 7. Set element's custom element definition to definition.
- // 8. Set element's is value to is value.
- element->setup_custom_element_from_constructor(*definition, is_value);
- // 9. Return element.
- return *element;
- }
- // 8. Let prototype be ? Get(NewTarget, "prototype").
- auto prototype = TRY(new_target.get(vm.names.prototype));
- // 9. If Type(prototype) is not Object, then:
- if (!prototype.is_object()) {
- // 1. Let realm be ? GetFunctionRealm(NewTarget).
- auto* function_realm = TRY(JS::get_function_realm(vm, new_target));
- // 2. Set prototype to the interface prototype object of realm whose interface is the same as the interface of the active function object.
- VERIFY(function_realm);
- prototype = &Bindings::ensure_web_prototype<@prototype_class@>(*function_realm, "@name@"sv);
- }
- VERIFY(prototype.is_object());
- // 10. Let element be the last entry in definition's construction stack.
- auto& element = definition->construction_stack().last();
- // 11. If element is an already constructed marker, then throw an "InvalidStateError" DOMException.
- if (element.has<HTML::AlreadyConstructedCustomElementMarker>())
- return JS::throw_completion(WebIDL::InvalidStateError::create(realm, "Custom element has already been constructed"sv));
- // 12. Perform ? element.[[SetPrototypeOf]](prototype).
- auto actual_element = element.get<JS::Handle<DOM::Element>>();
- TRY(actual_element->internal_set_prototype_of(&prototype.as_object()));
- // 13. Replace the last entry in definition's construction stack with an already constructed marker.
- definition->construction_stack().last() = HTML::AlreadyConstructedCustomElementMarker {};
- // 14. Return element.
- return *actual_element;
- )~~~");
- }
- } else {
- // Multiple constructor overloads - can't do that yet.
- TODO();
- }
- generator.append(R"~~~(
- }
- void @constructor_class@::initialize(JS::Realm& realm)
- {
- auto& vm = this->vm();
- [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;
- Base::initialize(realm);
- define_direct_property(vm.names.prototype, &ensure_web_prototype<@prototype_class@>(realm, "@namespaced_name@"), 0);
- define_direct_property(vm.names.length, JS::Value(@constructor.length@), JS::Attribute::Configurable);
- )~~~");
- for (auto& constant : interface.constants) {
- auto constant_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- constant_generator.set("constant.name", constant.name);
- generate_wrap_statement(constant_generator, constant.value, constant.type, interface, DeprecatedString::formatted("auto constant_{}_value =", constant.name));
- constant_generator.append(R"~~~(
- define_direct_property("@constant.name@", constant_@constant.name@_value, JS::Attribute::Enumerable);
- )~~~");
- }
- // https://webidl.spec.whatwg.org/#es-operations
- for (auto const& overload_set : interface.static_overload_sets) {
- auto function_generator = generator.fork().release_value_but_fixme_should_propagate_errors();
- function_generator.set("function.name", overload_set.key);
- function_generator.set("function.name:snakecase", make_input_acceptable_cpp(overload_set.key.to_snakecase()));
- function_generator.set("function.length", DeprecatedString::number(get_shortest_function_length(overload_set.value)));
- function_generator.append(R"~~~(
- define_native_function(realm, "@function.name@", @function.name:snakecase@, @function.length@, default_attributes);
- )~~~");
- }
- generator.append(R"~~~(
- }
- )~~~");
- // Implementation: Static Functions
- for (auto& function : interface.static_functions)
- generate_function(generator, function, StaticFunction::Yes, interface.constructor_class, interface.fully_qualified_name, interface);
- for (auto const& overload_set : interface.static_overload_sets) {
- if (overload_set.value.size() == 1)
- continue;
- generate_overload_arbiter(generator, overload_set, interface.constructor_class);
- }
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_prototype_header(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("prototype_class", interface.prototype_class);
- generator.append(R"~~~(
- #pragma once
- #include <LibJS/Runtime/Object.h>
- namespace Web::Bindings {
- class @prototype_class@ : public JS::Object {
- JS_OBJECT(@prototype_class@, JS::Object);
- public:
- explicit @prototype_class@(JS::Realm&);
- virtual void initialize(JS::Realm&) override;
- virtual ~@prototype_class@() override;
- private:
- )~~~");
- // Generate an empty prototype object for global interfaces.
- auto is_global_interface = interface.extended_attributes.contains("Global");
- if (is_global_interface) {
- generator.append(R"~~~(
- };
- )~~~");
- } else {
- generate_prototype_or_global_mixin_declarations(interface, builder);
- }
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_prototype_implementation(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("parent_name", interface.parent_name);
- generator.set("prototype_class", interface.prototype_class);
- generator.set("prototype_base_class", interface.prototype_base_class);
- generator.append(R"~~~(
- #include <AK/Function.h>
- #include <LibIDL/Types.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/ArrayBuffer.h>
- #include <LibJS/Runtime/DataView.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/FunctionObject.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/Iterator.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibJS/Runtime/Value.h>
- #include <LibWeb/Bindings/@prototype_class@.h>
- #include <LibWeb/Bindings/ExceptionOrUtils.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #include <LibWeb/DOM/Element.h>
- #include <LibWeb/DOM/Event.h>
- #include <LibWeb/DOM/IDLEventListener.h>
- #include <LibWeb/DOM/NodeFilter.h>
- #include <LibWeb/DOM/Range.h>
- #include <LibWeb/HTML/Origin.h>
- #include <LibWeb/HTML/Scripting/Environments.h>
- #include <LibWeb/HTML/Window.h>
- #include <LibWeb/HTML/WindowProxy.h>
- #include <LibWeb/WebIDL/OverloadResolution.h>
- #if __has_include(<LibWeb/Bindings/@prototype_base_class@.h>)
- # include <LibWeb/Bindings/@prototype_base_class@.h>
- #endif
- )~~~");
- bool has_ce_reactions = false;
- for (auto const& function : interface.functions) {
- if (function.extended_attributes.contains("CEReactions")) {
- has_ce_reactions = true;
- break;
- }
- }
- if (!has_ce_reactions) {
- for (auto const& attribute : interface.attributes) {
- if (attribute.extended_attributes.contains("CEReactions")) {
- has_ce_reactions = true;
- break;
- }
- }
- }
- if (!has_ce_reactions && interface.indexed_property_setter.has_value() && interface.indexed_property_setter->extended_attributes.contains("CEReactions"))
- has_ce_reactions = true;
- if (!has_ce_reactions && interface.named_property_setter.has_value() && interface.named_property_setter->extended_attributes.contains("CEReactions"))
- has_ce_reactions = true;
- if (!has_ce_reactions && interface.named_property_deleter.has_value() && interface.named_property_deleter->extended_attributes.contains("CEReactions"))
- has_ce_reactions = true;
- if (has_ce_reactions) {
- generator.append(R"~~~(
- #include <LibWeb/Bindings/MainThreadVM.h>
- )~~~");
- }
- for (auto& path : interface.required_imported_paths)
- generate_include_for(generator, path);
- emit_includes_for_all_imports(interface, generator, interface.pair_iterator_types.has_value());
- generator.append(R"~~~(
- // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
- using namespace Web::Crypto;
- using namespace Web::CSS;
- using namespace Web::DOM;
- using namespace Web::DOMParsing;
- using namespace Web::Fetch;
- using namespace Web::FileAPI;
- using namespace Web::Geometry;
- using namespace Web::HighResolutionTime;
- using namespace Web::HTML;
- using namespace Web::Internals;
- using namespace Web::IntersectionObserver;
- using namespace Web::NavigationTiming;
- using namespace Web::PerformanceTimeline;
- using namespace Web::RequestIdleCallback;
- using namespace Web::ResizeObserver;
- using namespace Web::Selection;
- using namespace Web::Streams;
- using namespace Web::SVG;
- using namespace Web::UIEvents;
- using namespace Web::URL;
- using namespace Web::UserTiming;
- using namespace Web::WebSockets;
- using namespace Web::XHR;
- using namespace Web::WebAssembly;
- using namespace Web::WebAudio;
- using namespace Web::WebGL;
- using namespace Web::WebIDL;
- namespace Web::Bindings {
- @prototype_class@::@prototype_class@([[maybe_unused]] JS::Realm& realm))~~~");
- if (interface.name == "DOMException") {
- // https://webidl.spec.whatwg.org/#es-DOMException-specialness
- // Object.getPrototypeOf(DOMException.prototype) === Error.prototype
- generator.append(R"~~~(
- : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().error_prototype())
- )~~~");
- } else if (!interface.parent_name.is_empty()) {
- generator.append(R"~~~(
- : Object(realm, nullptr)
- )~~~");
- } else {
- generator.append(R"~~~(
- : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
- )~~~");
- }
- generator.append(R"~~~(
- {
- }
- @prototype_class@::~@prototype_class@()
- {
- }
- )~~~");
- // Generate an empty prototype object for global interfaces.
- auto is_global_interface = interface.extended_attributes.contains("Global");
- if (is_global_interface) {
- generator.append(R"~~~(
- void @prototype_class@::initialize(JS::Realm& realm)
- {
- Base::initialize(realm);
- }
- )~~~");
- } else {
- generate_prototype_or_global_mixin_definitions(interface, builder);
- }
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_iterator_prototype_header(IDL::Interface const& interface, StringBuilder& builder)
- {
- VERIFY(interface.pair_iterator_types.has_value());
- SourceGenerator generator { builder };
- generator.set("prototype_class", DeprecatedString::formatted("{}IteratorPrototype", interface.name));
- generator.append(R"~~~(
- #pragma once
- #include <LibJS/Runtime/Object.h>
- namespace Web::Bindings {
- class @prototype_class@ : public JS::Object {
- JS_OBJECT(@prototype_class@, JS::Object);
- public:
- explicit @prototype_class@(JS::Realm&);
- virtual void initialize(JS::Realm&) override;
- virtual ~@prototype_class@() override;
- private:
- JS_DECLARE_NATIVE_FUNCTION(next);
- };
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_iterator_prototype_implementation(IDL::Interface const& interface, StringBuilder& builder)
- {
- VERIFY(interface.pair_iterator_types.has_value());
- SourceGenerator generator { builder };
- generator.set("name", DeprecatedString::formatted("{}Iterator", interface.name));
- generator.set("parent_name", interface.parent_name);
- generator.set("prototype_class", DeprecatedString::formatted("{}IteratorPrototype", interface.name));
- generator.set("prototype_base_class", interface.prototype_base_class);
- generator.set("fully_qualified_name", DeprecatedString::formatted("{}Iterator", interface.fully_qualified_name));
- generator.set("possible_include_path", DeprecatedString::formatted("{}Iterator", interface.name.replace("::"sv, "/"sv, ReplaceMode::All)));
- generator.append(R"~~~(
- #include <AK/Function.h>
- #include <AK/TypeCasts.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/FunctionObject.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibWeb/Bindings/@prototype_class@.h>
- #include <LibWeb/Bindings/ExceptionOrUtils.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #if __has_include(<LibWeb/@possible_include_path@.h>)
- # include <LibWeb/@possible_include_path@.h>
- #endif
- )~~~");
- emit_includes_for_all_imports(interface, generator, true);
- generator.append(R"~~~(
- // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
- using namespace Web::CSS;
- using namespace Web::DOM;
- using namespace Web::DOMParsing;
- using namespace Web::Fetch;
- using namespace Web::FileAPI;
- using namespace Web::Geometry;
- using namespace Web::HighResolutionTime;
- using namespace Web::HTML;
- using namespace Web::Internals;
- using namespace Web::IntersectionObserver;
- using namespace Web::NavigationTiming;
- using namespace Web::PerformanceTimeline;
- using namespace Web::RequestIdleCallback;
- using namespace Web::ResizeObserver;
- using namespace Web::Selection;
- using namespace Web::XHR;
- using namespace Web::UIEvents;
- using namespace Web::URL;
- using namespace Web::UserTiming;
- using namespace Web::WebAudio;
- using namespace Web::WebGL;
- using namespace Web::WebIDL;
- namespace Web::Bindings {
- @prototype_class@::@prototype_class@(JS::Realm& realm)
- : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().iterator_prototype())
- {
- }
- @prototype_class@::~@prototype_class@()
- {
- }
- void @prototype_class@::initialize(JS::Realm& realm)
- {
- auto& vm = this->vm();
- Base::initialize(realm);
- define_native_function(realm, vm.names.next, next, 0, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
- define_direct_property(vm.well_known_symbol_to_string_tag(), MUST(JS::PrimitiveString::create(vm, "Iterator"sv)), JS::Attribute::Configurable);
- }
- static JS::ThrowCompletionOr<@fully_qualified_name@*> impl_from(JS::VM& vm)
- {
- auto this_object = TRY(vm.this_value().to_object(vm));
- if (!is<@fully_qualified_name@>(*this_object))
- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@name@");
- return static_cast<@fully_qualified_name@*>(this_object.ptr());
- }
- JS_DEFINE_NATIVE_FUNCTION(@prototype_class@::next)
- {
- auto* impl = TRY(impl_from(vm));
- return TRY(throw_dom_exception_if_needed(vm, [&] { return impl->next(); }));
- }
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_global_mixin_header(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("class_name", interface.global_mixin_class);
- generator.append(R"~~~(
- #pragma once
- #include <LibJS/Runtime/Object.h>
- namespace Web::Bindings {
- class @class_name@ {
- public:
- void initialize(JS::Realm&, JS::Object&);
- @class_name@();
- virtual ~@class_name@();
- private:
- )~~~");
- generate_prototype_or_global_mixin_declarations(interface, builder);
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- void generate_global_mixin_implementation(IDL::Interface const& interface, StringBuilder& builder)
- {
- SourceGenerator generator { builder };
- generator.set("class_name", interface.global_mixin_class);
- generator.append(R"~~~(
- #include <AK/Function.h>
- #include <LibIDL/Types.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/DataView.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/FunctionObject.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/Iterator.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibJS/Runtime/Value.h>
- #include <LibWeb/Bindings/@class_name@.h>
- #include <LibWeb/Bindings/ExceptionOrUtils.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #include <LibWeb/DOM/Element.h>
- #include <LibWeb/DOM/Event.h>
- #include <LibWeb/DOM/IDLEventListener.h>
- #include <LibWeb/DOM/NodeFilter.h>
- #include <LibWeb/DOM/Range.h>
- #include <LibWeb/HTML/Origin.h>
- #include <LibWeb/HTML/Scripting/Environments.h>
- #include <LibWeb/HTML/Window.h>
- #include <LibWeb/HTML/WindowProxy.h>
- #include <LibWeb/WebIDL/OverloadResolution.h>
- )~~~");
- for (auto& path : interface.required_imported_paths)
- generate_include_for(generator, path);
- emit_includes_for_all_imports(interface, generator, interface.pair_iterator_types.has_value());
- generator.append(R"~~~(
- // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
- using namespace Web::Crypto;
- using namespace Web::CSS;
- using namespace Web::DOM;
- using namespace Web::DOMParsing;
- using namespace Web::Fetch;
- using namespace Web::FileAPI;
- using namespace Web::Geometry;
- using namespace Web::HighResolutionTime;
- using namespace Web::HTML;
- using namespace Web::Internals;
- using namespace Web::IntersectionObserver;
- using namespace Web::NavigationTiming;
- using namespace Web::PerformanceTimeline;
- using namespace Web::RequestIdleCallback;
- using namespace Web::ResizeObserver;
- using namespace Web::Selection;
- using namespace Web::Streams;
- using namespace Web::SVG;
- using namespace Web::UIEvents;
- using namespace Web::URL;
- using namespace Web::UserTiming;
- using namespace Web::WebAudio;
- using namespace Web::WebSockets;
- using namespace Web::XHR;
- using namespace Web::WebGL;
- using namespace Web::WebIDL;
- namespace Web::Bindings {
- @class_name@::@class_name@() = default;
- @class_name@::~@class_name@() = default;
- )~~~");
- generate_prototype_or_global_mixin_definitions(interface, builder);
- generator.append(R"~~~(
- } // namespace Web::Bindings
- )~~~");
- }
- }
|