Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ShadowRealm testing for ErrorEvent and queueMicrotask #49325

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// META: title=Microtask checkpoint after ShadowRealm onerror events
// META: global=shadowrealm

// Adapted from first part of ./resources/checkpoint-after-error-event.js.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not verified that the expected behavior is correct, but I assume it is if it matches the existing test


setup({allow_uncaught_exception: true});

var log = [];

addEventListener('error', () => {
log.push('handler 1');
Promise.resolve().then(() => log.push('handler 1 promise'));
});
addEventListener('error', () => {
log.push('handler 2');
Promise.resolve().then(() => log.push('handler 2 promise'));
});

async_test(t => {
t.step_timeout(() => {
assert_array_equals(log, [
'handler 1',
'handler 2',
'handler 1 promise',
'handler 2 promise'
]);
t.done();
},
0);
}, "Promise resolved during #report-the-error");

queueMicrotask(() => thisFunctionDoesNotExist());
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=dedicatedworker-module,sharedworker-module
// META: global=dedicatedworker-module,sharedworker-module,shadowrealm-in-window,shadowrealm-in-shadowrealm,shadowrealm-in-dedicatedworker,shadowrealm-in-sharedworker
// META: script=./resources/evaluation-order-setup.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the improved error reporting in #49387 I realized these didn't run in shadowrealm-in-serviceworker and shadowrealm-in-audioworklet because the fake dynamic import isn't actually module code 😖 I'm not sure how important that is though.


import './resources/evaluation-order-1-nothrow-setup.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=dedicatedworker-module,sharedworker-module
// META: global=dedicatedworker-module,sharedworker-module,shadowrealm-in-window,shadowrealm-in-shadowrealm,shadowrealm-in-dedicatedworker,shadowrealm-in-sharedworker
// META: script=./resources/evaluation-order-setup.js

import './resources/evaluation-order-1-throw-setup.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=dedicatedworker-module,sharedworker-module
// META: global=dedicatedworker-module,sharedworker-module,shadowrealm-in-window,shadowrealm-in-shadowrealm,shadowrealm-in-dedicatedworker,shadowrealm-in-sharedworker
// META: script=./resources/evaluation-order-setup.js

import './resources/evaluation-order-2-setup.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=dedicatedworker-module,sharedworker-module
// META: global=dedicatedworker-module,sharedworker-module,shadowrealm-in-window,shadowrealm-in-shadowrealm,shadowrealm-in-dedicatedworker,shadowrealm-in-sharedworker
// META: script=./resources/evaluation-order-setup.js

import './resources/evaluation-order-3-setup.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=window,worker
// META: global=window,worker,shadowrealm
"use strict";

setup({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// META: title=Errors thrown by wrapped microtask in ShadowRealm

setup({ allow_uncaught_exception: true });

const realm = new ShadowRealm();

const onerrorCalls = [];
window.onerror = e => {
onerrorCalls.push("Window");
};
realm.evaluate(`(push) => {
onerror = function (e) {
const assertResult = e instanceof TypeError;
push(assertResult);
};
}`)(assertResult => {
onerrorCalls.push("ShadowRealm");
assert_true(assertResult,
"exception should be converted to a fresh ShadowRealm TypeError")
});

async_test(t => {
window.onload = t.step_func(() => {
const task = () => { throw new Error("will be converted to TypeError"); };
realm.evaluate(`queueMicrotask`)(task);

t.step_timeout(() => {
assert_array_equals(onerrorCalls, ["ShadowRealm"],
"exception should only be reported in ShadowRealm's onerror handler");
t.done();
}, 4);
});
});
2 changes: 1 addition & 1 deletion html/webappapis/microtask-queuing/queue-microtask.any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=window,worker
// META: global=window,worker,shadowrealm
"use strict";

test(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// META: global=worker,shadowrealm

"use strict";
importScripts("/resources/testharness.js");

setup({ allow_uncaught_exception: true });

Expand All @@ -14,10 +15,10 @@ promise_test(t => {
assert_equals(e.defaultPrevented, true);
});

setTimeout(() => thisFunctionDoesNotExist(), 0);
queueMicrotask(() => thisFunctionDoesNotExist());

return promise;
}, "error event is weird (return true cancels; many args) on WorkerGlobalScope, with a runtime error");
}, "error event is weird (return true cancels; many args) on non-Window global scope, with a runtime error");
ptomato marked this conversation as resolved.
Show resolved Hide resolved

promise_test(t => {
self.onerror = t.step_func(function (message, filename, lineno, colno, error) {
Expand All @@ -31,10 +32,8 @@ promise_test(t => {
return true;
});

setTimeout(() => thisFunctionDoesNotExist(), 0);
queueMicrotask(() => thisFunctionDoesNotExist());

const eventWatcher = new EventWatcher(t, self, "error");
return eventWatcher.wait_for("error");
}, "error event has the right 5 args on WorkerGlobalScope, with a runtime error");

done();
}, "error event has the right 5 args on non-Window global scope, with a runtime error");
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// META: global=worker,shadowrealm

"use strict";
importScripts("/resources/testharness.js");

setup({ allow_uncaught_exception: true });

Expand All @@ -17,7 +18,7 @@ promise_test(t => {
self.dispatchEvent(new ErrorEvent("error", { cancelable: true }));

return promise;
}, "error event is weird (return true cancels; many args) on WorkerGlobalScope, with a synthetic ErrorEvent");
}, "error event is weird (return true cancels; many args) on non-Window global scope, with a synthetic ErrorEvent");

promise_test(t => {
const theError = { the: "error object" };
Expand All @@ -44,6 +45,4 @@ promise_test(t => {
}));

return promise;
}, "error event has the right 5 args on WorkerGlobalScope, with a synthetic ErrorEvent");

done();
}, "error event has the right 5 args on non-Window global scope, with a synthetic ErrorEvent");
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// META: global=worker,shadowrealm

"use strict";
importScripts("/resources/testharness.js");

setup({ allow_uncaught_exception: true });

Expand All @@ -17,6 +18,4 @@ promise_test(t => {
self.dispatchEvent(new Event("error", { cancelable: true }));

return promise;
}, "error event is normal (return true does not cancel; one arg) on WorkerGlobalScope, with a synthetic Event");

done();
}, "error event is normal (return true does not cancel; one arg) on non-Window global scope, with a synthetic Event");
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// META: title=globalThis.onerror: runtime script errors in ShadowRealm

// https://html.spec.whatwg.org/multipage/#runtime-script-errors says what to do
// for uncaught runtime script errors, and just below describes what to do when
// onerror is a Function.

async_test(t => {
onerror = t.unreached_func("Window onerror should not be triggered");

const realm = new ShadowRealm();

realm.evaluate("var errorCount = 0;");
realm.evaluate(`(doAsserts) => {
globalThis.onerror = function(msg, url, lineno, colno, thrownValue) {
++errorCount;
doAsserts(url, lineno, colno, typeof thrownValue, String(thrownValue));
};
}`)(t.step_func((url, lineno, typeofThrownValue, stringifiedThrownValue) => {
assert_equals(url, "eval code", "correct url passed to onerror");
assert_equals(lineno, 8, "correct line number passed to onerror");
assert_equals(typeofThrownValue, "string", "thrown string passed directly to onerror");
assert_equals(stringifiedThrownValue, "bar", "correct thrown value passed to onerror");
}));

assert_throws_js(TypeError, () => realm.evaluate(`
try {
// This error is caught, so it should NOT trigger onerror.
throw "foo";
} catch (ex) {
}
// This error is NOT caught, so it should trigger onerror.
throw "bar";
`), "thrown error is wrapped in a TypeError object from the surrounding realm");

t.step_timeout(() => {
assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once");
}, 1000);
}, "onerror triggered by uncaught thrown exception in realm.evaluate");

async_test(t => {
onerror = t.unreached_func("Window onerror should not be triggered");

const realm = new ShadowRealm();

realm.evaluate("var errorCount = 0;");
realm.evaluate(`(doAsserts) => {
globalThis.onerror = function(msg, url, lineno, colno, thrownValue) {
++errorCount;
doAsserts(url, lineno, typeof thrownValue, thrownValue instanceof TypeError);
};
}`)(t.step_func((url, lineno, typeofThrownValue, isTypeError) => {
assert_equals(url, "eval code", "correct url passed to onerror");
assert_equals(lineno, 8, "correct line number passed to onerror");
assert_equals(typeofThrownValue, "object", "thrown error instance passed to onerror");
assert_true(isShadowRealmTypeError, "correct thrown value passed to onerror");
}));

assert_throws_js(TypeError, () => realm.evaluate(`
try {
// This error is caught, so it should NOT trigger onerror.
window.nonexistentproperty.oops();
} catch (ex) {
}
// This error is NOT caught, so it should trigger onerror.
window.nonexistentproperty.oops();
`), "thrown error is wrapped in a TypeError object from the surrounding realm");

t.step_timeout(() => {
assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once");
}, 1000);
}, "onerror triggered by uncaught runtime error in realm.evaluate");
4 changes: 3 additions & 1 deletion html/webappapis/scripting/reporterror.any.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// META: global=window,dedicatedworker,shadowrealm

setup({ allow_uncaught_exception:true });

[
Expand All @@ -9,7 +11,7 @@ setup({ allow_uncaught_exception:true });
let happened = false;
self.addEventListener("error", t.step_func(e => {
assert_true(e.message !== "");
assert_equals(e.filename, new URL("reporterror.any.js", location.href).href);
assert_equals(e.filename, location ? new URL("reporterror.any.js", location.href).href : "");
assert_greater_than(e.lineno, 0);
assert_greater_than(e.colno, 0);
assert_equals(e.error, throwable);
Expand Down
Loading