这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ function parseFrontmatter(src) {
if (!phase || !type) return null;
negative = {phase: phase, type: type};
}
return {includes: includes, flags: flags, negative: negative};

return {includes: includes, flags: flags, negative: negative, isDynamic: /dynamic-import/.test(frontmatter)}; // lol, do better
}

var errSigil = {};
Expand All @@ -224,13 +225,15 @@ arg looks like this:
isModule: boolean,
isAsync: boolean,
needsAPI: boolean,
path: string, // only present for modules
path: string,
}

*/
function runSources(arg, done) {
var iframe = iframes.pop();

var path = ['SYNTHETIC'].concat(arg.path);

var listener = function() {
iframe.removeEventListener('load', listener);
var err = errSigil;
Expand Down Expand Up @@ -266,14 +269,11 @@ function runSources(arg, done) {
script.text = arg.setup;
w.document.body.appendChild(script);


if (arg.isModule) {
w.navigator.serviceWorker.addEventListener('message', messageListener);
}
w.navigator.serviceWorker.addEventListener('message', messageListener);

script = w.document.createElement('script');
if (arg.isModule) {
script.src = arg.path;
script.src = path[path.length - 1];
script.type = 'module';
} else {
script.text = arg.source;
Expand All @@ -296,7 +296,7 @@ function runSources(arg, done) {

iframe.addEventListener('load', listener);

iframe.src = arg.isModule ? 'blank.html' : iframeSrc; // Our service worker can't intercept requests when src = '', sadly.
iframe.src = (arg.isDynamic || arg.isModule) ? path.slice(0, path.length - 1).concat(['blank.html']).join('/') : iframeSrc; // Our service worker can't intercept requests when src = '', sadly.
}

function checkErrorType(errorEvent, global, kind) {
Expand Down Expand Up @@ -390,22 +390,22 @@ function runTest262Test(src, path, pass, fail, skip) {
}

if (meta.flags.module) {
runSources({ setup: setup, source: src, isModule: true, isAsync: isAsync, needsAPI: needsAPI, path: path.join('/') }, checkErr(meta.negative, pass, fail));
runSources({ setup: setup, source: src, isModule: true, isAsync: isAsync, needsAPI: needsAPI, path: path, isDynamic: meta.isDynamic }, checkErr(meta.negative, pass, fail));
return;
}
if (meta.flags.raw) {
// Note: we cannot assert phase for these, so false positives are possible.
runSources({ setup: setup, source: src, isModule: false, isAsync: isAsync, needsAPI: needsAPI }, checkErr(meta.negative, pass, fail));
runSources({ setup: setup, source: src, isModule: false, isAsync: isAsync, needsAPI: needsAPI, path: path, isDynamic: meta.isDynamic }, checkErr(meta.negative, pass, fail));
return;
}
if (meta.flags.strict) {
runSources({ setup: setup, source: meta.flags.strict === 'always' ? strict(src) : src, isModule: false, isAsync: isAsync, needsAPI: needsAPI }, checkErr(meta.negative, pass, fail));
runSources({ setup: setup, source: meta.flags.strict === 'always' ? strict(src) : src, isModule: false, isAsync: isAsync, needsAPI: needsAPI, path: path, isDynamic: meta.isDynamic }, checkErr(meta.negative, pass, fail));
return;
}

// run in both strict and non-strict
runSources({ setup: setup, source: strict(src), isAsync: isAsync, needsAPI: needsAPI }, checkErr(meta.negative, function() {
runSources({ setup: setup, source: src, isModule: false, isAsync: isAsync, needsAPI: needsAPI }, checkErr(meta.negative, pass, fail));
runSources({ setup: setup, source: strict(src), isAsync: isAsync, needsAPI: needsAPI, path: path, isDynamic: meta.isDynamic }, checkErr(meta.negative, function() {
runSources({ setup: setup, source: src, isModule: false, isAsync: isAsync, needsAPI: needsAPI, path: path, isDynamic: meta.isDynamic }, checkErr(meta.negative, pass, fail));
}, fail));
}

Expand Down Expand Up @@ -754,6 +754,7 @@ function messageListener(e) {
port.postMessage({ success: false });
});
}
navigator.serviceWorker.addEventListener('message', messageListener);

// onload

Expand Down Expand Up @@ -866,7 +867,7 @@ window.addEventListener('load', function() {
}

// Check if the environment reports errors from iframes with src = ''.
runSources({ setup: '', source: 'throw new Error;', isModule: false, isAsync: false, needsAPI: false }, function(e) {
runSources({ setup: '', source: 'throw new Error;', isModule: false, isAsync: false, needsAPI: false, path: ['test', 'path', 'test.js'] }, function(e) {
if (e.message.match(/Script error\./i)) {
iframeSrc = 'blank.html';
}
Expand Down
21 changes: 16 additions & 5 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
self.addEventListener('fetch', function(event) {
// TODO also intercept blank.html to serve the right thing without hitting the network
const isInitial = /blank.html$/.test(event.request.referrer);
const isSubsequent = /\/test\/.*\.js$/.test(event.request.referrer);
if (isInitial || isSubsequent) {
// 'blank.html' resolves for all paths.
// We use this to run scripts containing a dynamic import on a synthetic page which has the right URL to serve its depedencies.
if (/blank\.html$/.test(event.request.url)) {
event.respondWith(Promise.resolve(new Response(
`<!doctype html><meta charset=utf-8><title>realm</title>`,
{
headers: {
'Content-Type': 'text/html',
}
}
)));
return;
}

if (/\/SYNTHETIC\//.test(event.request.url)) {
event.respondWith(async function() {
const client = await clients.get(event.clientId);
const chan = new MessageChannel();
Expand All @@ -19,7 +30,7 @@ self.addEventListener('fetch', function(event) {
return;
}
let text = e.data.data;
if (isInitial) {
if (/blank.html$/.test(event.request.referrer)) {
text += '\n;$$testFinished();'; // This is such a hack...
}
resolve(new Response(text, {
Expand Down