11cb0ef41Sopenharmony_ci// TODO: it would be nice to support `idl_array.add_objects`
21cb0ef41Sopenharmony_cifunction fetch_text(url) {
31cb0ef41Sopenharmony_ci    return fetch(url).then(function (r) {
41cb0ef41Sopenharmony_ci        if (!r.ok) {
51cb0ef41Sopenharmony_ci            throw new Error("Error fetching " + url + ".");
61cb0ef41Sopenharmony_ci        }
71cb0ef41Sopenharmony_ci        return r.text();
81cb0ef41Sopenharmony_ci    });
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci/**
121cb0ef41Sopenharmony_ci * idl_test_shadowrealm is a promise_test wrapper that handles the fetching of the IDL, and
131cb0ef41Sopenharmony_ci * running the code in a `ShadowRealm`, avoiding repetitive boilerplate.
141cb0ef41Sopenharmony_ci *
151cb0ef41Sopenharmony_ci * @see https://github.com/tc39/proposal-shadowrealm
161cb0ef41Sopenharmony_ci * @param {String[]} srcs Spec name(s) for source idl files (fetched from
171cb0ef41Sopenharmony_ci *      /interfaces/{name}.idl).
181cb0ef41Sopenharmony_ci * @param {String[]} deps Spec name(s) for dependency idl files (fetched
191cb0ef41Sopenharmony_ci *      from /interfaces/{name}.idl). Order is important - dependencies from
201cb0ef41Sopenharmony_ci *      each source will only be included if they're already know to be a
211cb0ef41Sopenharmony_ci *      dependency (i.e. have already been seen).
221cb0ef41Sopenharmony_ci */
231cb0ef41Sopenharmony_cifunction idl_test_shadowrealm(srcs, deps) {
241cb0ef41Sopenharmony_ci    const script_urls = [
251cb0ef41Sopenharmony_ci        "/resources/testharness.js",
261cb0ef41Sopenharmony_ci        "/resources/WebIDLParser.js",
271cb0ef41Sopenharmony_ci        "/resources/idlharness.js",
281cb0ef41Sopenharmony_ci    ];
291cb0ef41Sopenharmony_ci    promise_setup(async t => {
301cb0ef41Sopenharmony_ci        const realm = new ShadowRealm();
311cb0ef41Sopenharmony_ci        // https://github.com/web-platform-tests/wpt/issues/31996
321cb0ef41Sopenharmony_ci        realm.evaluate("globalThis.self = globalThis; undefined;");
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci        realm.evaluate(`
351cb0ef41Sopenharmony_ci            globalThis.self.GLOBAL = {
361cb0ef41Sopenharmony_ci                isWindow: function() { return false; },
371cb0ef41Sopenharmony_ci                isWorker: function() { return false; },
381cb0ef41Sopenharmony_ci                isShadowRealm: function() { return true; },
391cb0ef41Sopenharmony_ci            }; undefined;
401cb0ef41Sopenharmony_ci        `);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci        const ss = await Promise.all(script_urls.map(url => fetch_text(url)));
431cb0ef41Sopenharmony_ci        for (const s of ss) {
441cb0ef41Sopenharmony_ci            realm.evaluate(s);
451cb0ef41Sopenharmony_ci        }
461cb0ef41Sopenharmony_ci        const specs = await Promise.all(srcs.concat(deps).map(spec => {
471cb0ef41Sopenharmony_ci            return fetch_text("/interfaces/" + spec + ".idl");
481cb0ef41Sopenharmony_ci        }));
491cb0ef41Sopenharmony_ci        const idls = JSON.stringify(specs);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci        const results = JSON.parse(await new Promise(
521cb0ef41Sopenharmony_ci          realm.evaluate(`(resolve,reject) => {
531cb0ef41Sopenharmony_ci              const idls = ${idls};
541cb0ef41Sopenharmony_ci              add_completion_callback(function (tests, harness_status, asserts_run) {
551cb0ef41Sopenharmony_ci                resolve(JSON.stringify(tests));
561cb0ef41Sopenharmony_ci              });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci              // Without the wrapping test, testharness.js will think it's done after it has run
591cb0ef41Sopenharmony_ci              // the first idlharness test.
601cb0ef41Sopenharmony_ci              test(() => {
611cb0ef41Sopenharmony_ci                  const idl_array = new IdlArray();
621cb0ef41Sopenharmony_ci                  for (let i = 0; i < ${srcs.length}; i++) {
631cb0ef41Sopenharmony_ci                      idl_array.add_idls(idls[i]);
641cb0ef41Sopenharmony_ci                  }
651cb0ef41Sopenharmony_ci                  for (let i = ${srcs.length}; i < ${srcs.length + deps.length}; i++) {
661cb0ef41Sopenharmony_ci                      idl_array.add_dependency_idls(idls[i]);
671cb0ef41Sopenharmony_ci                  }
681cb0ef41Sopenharmony_ci                  idl_array.test();
691cb0ef41Sopenharmony_ci              }, "setup");
701cb0ef41Sopenharmony_ci          }`)
711cb0ef41Sopenharmony_ci        ));
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci        // We ran the tests in the ShadowRealm and gathered the results. Now treat them as if
741cb0ef41Sopenharmony_ci        // we'd run them directly here, so we can see them.
751cb0ef41Sopenharmony_ci        for (const {name, status, message} of results) {
761cb0ef41Sopenharmony_ci            // TODO: make this an API in testharness.js - needs RFC?
771cb0ef41Sopenharmony_ci            promise_test(t => {t.set_status(status, message); t.phase = t.phases.HAS_RESULT; t.done()}, name);
781cb0ef41Sopenharmony_ci        }
791cb0ef41Sopenharmony_ci    }, "outer setup");
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci// vim: set expandtab shiftwidth=4 tabstop=4 foldmarker=@{,@} foldmethod=marker:
82