Skip to content

Commit

Permalink
chore(compartment-mapper): rename exitModuleImportHook to importHook …
Browse files Browse the repository at this point in the history
…in compartment-mapper public API
  • Loading branch information
naugtur committed Jul 13, 2023
1 parent f5fb141 commit b057700
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/compartment-mapper/demo/policy/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const options = {
process,
setTimeout,
},
exitModuleImportHook: async specifier => {
importHook: async specifier => {
const ns = await import(specifier);
return Object.freeze({
imports: [],
Expand Down Expand Up @@ -125,7 +125,7 @@ console.log('\n\n________________________________________________ Archive\n');
const archive = await makeArchive(readPower, entrypointPath, {
modules: options.modules,
policy: options.policy,
exitModuleImportHook: options.exitModuleImportHook,
importHook: options.importHook,
});
console.log('>----------makeArchive -> parseArchive');
const application = await parseArchive(archive, '<unknown>', {
Expand All @@ -135,7 +135,7 @@ console.log('\n\n________________________________________________ Archive\n');
const { namespace } = await application.import({
globals: options.globals,
modules: options.modules,
exitModuleImportHook: options.exitModuleImportHook,
importHook: options.importHook,
});
console.log('>----------import -> end');
console.log(2, namespace.poem);
Expand Down
6 changes: 3 additions & 3 deletions packages/compartment-mapper/src/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
captureSourceLocation = undefined,
searchSuffixes = undefined,
commonDependencies = undefined,
exitModuleImportHook = undefined,
importHook: exitModuleImportHook = undefined,
policy = undefined,
} = options || {};
const { read, computeSha512 } = unpackReadPowers(powers);
Expand Down Expand Up @@ -336,7 +336,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
/** @type {Sources} */
const sources = Object.create(null);

const internalExitModuleImportHook = exitModuleImportHookMaker({
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules: exitModules,
exitModuleImportHook,
});
Expand All @@ -346,7 +346,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
baseLocation: packageLocation,
sources,
compartmentDescriptors: compartments,
exitModuleImportHook: internalExitModuleImportHook,
exitModuleImportHook: compartmentExitModuleImportHook,
archiveOnly: true,
computeSha512,
searchSuffixes,
Expand Down
24 changes: 8 additions & 16 deletions packages/compartment-mapper/src/import-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ const postponeErrorToExecute = errorMessage => {
return record;
};

// /**
// * @callback ArchiveImportHookMaker
// * @param {string} packageLocation
// * @param {string} packageName
// * @param {DeferredAttenuatorsProvider} attenuators
// * @returns {ImportHook}
// */

/**
* @param {(path: string) => Uint8Array} get
* @param {Record<string, CompartmentDescriptor>} compartments
Expand Down Expand Up @@ -234,7 +226,7 @@ const makeFeauxModuleExportsNamespace = Compartment => {
};

// Have to give it a name to capture the external meaning of Compartment
// Otherwise @param {typeof COmpartment} takes the Compartment to mean
// Otherwise @param {typeof Compartment} takes the Compartment to mean
// the const variable defined within the function.
/** @typedef {typeof Compartment} CompartmentConstructor */

Expand All @@ -245,7 +237,7 @@ const makeFeauxModuleExportsNamespace = Compartment => {
* @param {string} [options.expectedSha512]
* @param {HashFn} [options.computeSha512]
* @param {Record<string, unknown>} [options.modules]
* @param {ExitModuleImportHook} [options.exitModuleImportHook]
* @param {ExitModuleImportHook} [options.importHook]
* @param {CompartmentConstructor} [options.Compartment]
* @param {ComputeSourceLocationHook} [options.computeSourceLocation]
* @returns {Promise<Application>}
Expand All @@ -261,10 +253,10 @@ export const parseArchive = async (
computeSourceLocation = undefined,
Compartment = DefaultCompartment,
modules = undefined,
exitModuleImportHook = undefined,
importHook: exitModuleImportHook = undefined,
} = options;

const internalExitModuleImportHook = exitModuleImportHookMaker({
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
});
Expand Down Expand Up @@ -326,7 +318,7 @@ export const parseArchive = async (
archiveLocation,
computeSha512,
computeSourceLocation,
internalExitModuleImportHook,
compartmentExitModuleImportHook,
);
// A weakness of the current Compartment design is that the `modules` map
// must be given a module namespace object that passes a brand check.
Expand Down Expand Up @@ -360,10 +352,10 @@ export const parseArchive = async (
transforms,
__shimTransforms__,
Compartment,
exitModuleImportHook,
importHook: exitModuleImportHook,
} = options || {};

const internalExitModuleImportHook = exitModuleImportHookMaker({
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
});
Expand All @@ -373,7 +365,7 @@ export const parseArchive = async (
archiveLocation,
computeSha512,
computeSourceLocation,
internalExitModuleImportHook,
compartmentExitModuleImportHook,
);
const { compartment, pendingJobsPromise } = link(compartmentMap, {
makeImportHook,
Expand Down
6 changes: 3 additions & 3 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
transforms,
__shimTransforms__,
Compartment,
exitModuleImportHook,
importHook: exitModuleImportHook,
} = options;
const internalExitModuleImportHook = exitModuleImportHookMaker({
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
});
const makeImportHook = makeImportHookMaker({
readPowers,
baseLocation: packageLocation,
compartmentDescriptors: compartmentMap.compartments,
exitModuleImportHook: internalExitModuleImportHook,
exitModuleImportHook: compartmentExitModuleImportHook,
archiveOnly: false,
searchSuffixes,
});
Expand Down
7 changes: 1 addition & 6 deletions packages/compartment-mapper/src/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,7 @@ async function attenuateModule({
exports: originalModuleRecord.exports,
execute: (moduleExports, compartment, resolvedImports) => {
const ns = {};
originalModuleRecord.execute(ns, compartment, resolvedImports); // TODO: fix typing

// TODO: attenuator being async forces us to call original execute out of order before returning the record.
// one solution is to make the attenuator itself synchronous.
// we could make attenuator accept and return a record, so that it can remain asynchronous while execute could be called in time.

originalModuleRecord.execute(ns, compartment, resolvedImports);
const attenuated = attenuate(ns);
moduleExports.default = attenuated;
assign(moduleExports, attenuated);
Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export {};
* @property {Array<Transform>} [transforms]
* @property {Array<Transform>} [__shimTransforms__]
* @property {Record<string, object>} [modules]
* @property {ExitModuleImportHook} [exitModuleImportHook]
* @property {ExitModuleImportHook} [importHook]
* @property {Record<string, object>} [attenuations]
* @property {typeof Compartment} [Compartment]
*/
Expand Down Expand Up @@ -332,7 +332,7 @@ export {};
* @property {object} [policy]
* @property {Set<string>} [tags]
* @property {CaptureSourceLocationHook} [captureSourceLocation]
* @property {ExitModuleImportHook} [exitModuleImportHook]
* @property {ExitModuleImportHook} [importHook]
* @property {Array<string>} [searchSuffixes]
* @property {Record<string, string>} [commonDependencies]
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/test/test-exit-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fixture = new URL(
).toString();

scaffold(
'exitModuleImportHook - import actual builtin',
'exitModule importHook - import actual builtin',
test,
fixture,
(t, { namespace }) => {
Expand All @@ -18,7 +18,7 @@ scaffold(
1, // expected number of assertions
{
additionalOptions: {
exitModuleImportHook: async specifier => {
importHook: async specifier => {
const ns = await import(specifier);
return Object.freeze({
imports: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/compartment-mapper/test/test-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ scaffold(
policy,
additionalOptions: {
modules: {},
exitModuleImportHook: async specifier => {
importHook: async specifier => {
const ns = {
a: 1,
b: 2,
Expand Down

0 comments on commit b057700

Please sign in to comment.