Skip to content

Commit

Permalink
feat(daemon): web-page endowments include all compatible window prope…
Browse files Browse the repository at this point in the history
…rties
  • Loading branch information
kumavis committed Dec 2, 2023
1 parent 6050df8 commit c43d35b
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions packages/daemon/src/web-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ 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) {
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 (...args) {
if (new.target) {
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 +53,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

0 comments on commit c43d35b

Please sign in to comment.