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

feat(daemon): web-page endowments include all compatible window properties #1874

Merged
merged 1 commit into from
Dec 2, 2023
Merged
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
58 changes: 55 additions & 3 deletions packages/daemon/src/web-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,51 @@ import { makeCapTP } from '@endo/captp';
import { E, Far } from '@endo/far';
import { importBundle } from '@endo/import-bundle';

const getPrototypeChain = obj => {
const chain = [];
while (obj) {
chain.push(obj);
obj = Object.getPrototypeOf(obj);
}
return chain;
};
const collectPropsAndBind = target => {
const container = {};
for (const obj of getPrototypeChain(target)) {
for (const [name, propDesc] of Object.entries(
Object.getOwnPropertyDescriptors(obj),
)) {
if (name in container) {
// eslint-disable-next-line no-continue
continue;
}
let value = propDesc.value;
if (propDesc.get) {
value = propDesc.get.call(target);
}
if (typeof value === 'function') {
// This wrapper is a bind that works for constructors as well.
const wrapper = function bindWrapperFn(...args) {
if (new.target) {
// eslint-disable-next-line new-cap
return new value(...args);
} else {
return value.call(target, ...args);
}
};
Object.defineProperties(
wrapper,
Object.getOwnPropertyDescriptors(value),
);
container[name] = wrapper;
} else {
container[name] = value;
}
}
}
return container;
};

const hardenedEndowments = harden({
assert,
E,
Expand All @@ -15,11 +60,18 @@ const hardenedEndowments = harden({
URL,
});

const globalProps = collectPropsAndBind(window);
// These properties conflict with Compartment globals.
delete globalProps.undefined;
delete globalProps.NaN;
delete globalProps.Infinity;

const endowments = Object.freeze({
...hardenedEndowments,
window,
document,
console,
...globalProps,
process: {
env: {},
},
});

const url = new URL('/', `${window.location}`);
Expand Down