Skip to content

Commit

Permalink
refactor(compartment-mapper): Adopt Compartment usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jul 27, 2024
1 parent abe3db9 commit a299074
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
28 changes: 1 addition & 27 deletions packages/compartment-mapper/src/import-archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,6 @@ const makeArchiveImportHookMaker = (
return makeImportHook;
};

/**
* Creates a fake module namespace object that passes a brand check.
*
* @param {typeof Compartment} Compartment
* @returns {ModuleExportsNamespace}
*/
const makeFauxModuleExportsNamespace = Compartment => {
const compartment = new Compartment(
{},
{},
{
resolveHook() {
return '.';
},
async importHook() {
return {
imports: [],
execute() {},
exports: [],
};
},
},
);
return compartment.module('.');
};

// Have to give it a name to capture the external meaning of Compartment
// Otherwise @param {typeof Compartment} takes the Compartment to mean
// the const variable defined within the function.
Expand Down Expand Up @@ -377,7 +351,7 @@ export const parseArchive = async (
languageForExtension,
modules: Object.fromEntries(
Object.keys(modules || {}).map(specifier => {
return [specifier, makeFauxModuleExportsNamespace(Compartment)];
return [specifier, { namespace: {} }];
}),
),
Compartment,
Expand Down
5 changes: 3 additions & 2 deletions packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,14 @@ export const link = (
scopes,
);

const compartment = new Compartment(create(null), undefined, {
const compartment = new Compartment({
name: location,
resolveHook,
importHook,
moduleMapHook,
transforms,
__shimTransforms__,
name: location,
__options__: true,
});

if (!archiveOnly) {
Expand Down
10 changes: 8 additions & 2 deletions packages/compartment-mapper/test/bundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ test('bundles work', async t => {
const print = entry => {
log.push(entry);
};
const compartment = new Compartment({ print });
const compartment = new Compartment({
globals: { print },
__options__: true,
});
compartment.evaluate(bundle);
t.deepEqual(log, expectedLog);
});
Expand Down Expand Up @@ -93,7 +96,10 @@ test.failing('bundle cjs-compat', async t => {
const print = entry => {
log.push(entry);
};
const compartment = new Compartment({ print });
const compartment = new Compartment({
globals: { print },
__options__: true,
});
compartment.evaluate(bundle);
t.deepEqual(log, expectedLog);
});
Expand Down
13 changes: 8 additions & 5 deletions packages/compartment-mapper/test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ test('makeBundle / importArchive', async t => {

const archiverBundle = await makeBundle(readPowers.read, archiverLocation);
const archiverCompartment = new Compartment({
TextEncoder,
TextDecoder,
URL,
// See https://github.com/Agoric/agoric-sdk/issues/9515
assert: globalThis.assert,
globals: {
TextEncoder,
TextDecoder,
URL,
// See https://github.com/Agoric/agoric-sdk/issues/9515
assert: globalThis.assert,
},
__options__: true,
});
const evasiveArchiverBundle = archiverBundle
.replace(/(?<!\.)\bimport\b(?![:"'])/g, 'IMPORT')
Expand Down

0 comments on commit a299074

Please sign in to comment.