feat(compartment-mapper): Thread global lexicals
MODIFY
packages/compartment-mapper/NEWS.md
packages/compartment-mapper/NEWS.md
@@ -5,7 +5,8 @@ User-visible changes to the compartment mapper:
* *BREAKING*: All `import` methods now take an options bag that may contain
`globals` and `modules` options if present, instead of these as positional
arguments.
-* Threads transforms through to underlying compartments.
+* The `import` options bag now also accepts `globalLexicals` and `transforms`,
+ passing these without alteration to each `Compartment`.
## Release 0.1.0 (2020-09-21)

MODIFY
packages/compartment-mapper/src/assemble.js
packages/compartment-mapper/src/assemble.js
@@ -130,6 +130,7 @@ export const assemble = (
{
makeImportHook,
globals = {},
+ globalLexicals = {},
transforms = [],
modules: exitModules = {},
Compartment = defaultCompartment
@@ -170,6 +171,7 @@ export const assemble = (
importHook,
moduleMapHook,
transforms,
+ globalLexicals,
name: location
});

MODIFY
packages/compartment-mapper/src/import-archive.js
packages/compartment-mapper/src/import-archive.js
@@ -39,7 +39,7 @@ export const parseArchive = async (archiveBytes, archiveLocation) => {
const compartmentMap = json.parse(compartmentMapText, "compartment-map.json");
const execute = options => {
- const { globals, modules, transforms } = options;
+ const { globals, globalLexicals, modules, transforms } = options;
const {
compartments,
entry: { module: moduleSpecifier }
@@ -48,6 +48,7 @@ export const parseArchive = async (archiveBytes, archiveLocation) => {
const compartment = assemble(compartmentMap, {
makeImportHook,
globals,
+ globalLexicals,
modules,
transforms
});

MODIFY
packages/compartment-mapper/src/import.js
packages/compartment-mapper/src/import.js
@@ -26,11 +26,12 @@ export const loadLocation = async (read, moduleLocation) => {
);
const execute = async (options = {}) => {
- const { globals, modules, transforms } = options;
+ const { globals, globalLexicals, modules, transforms } = options;
const makeImportHook = makeImportHookMaker(read, packageLocation);
const compartment = assemble(compartmentMap, {
makeImportHook,
globals,
+ globalLexicals,
modules,
transforms
});

MODIFY
packages/compartment-mapper/test/main.test.js
packages/compartment-mapper/test/main.test.js
@@ -19,7 +19,12 @@ const fixture = new URL("node_modules/app/main.js", import.meta.url).toString();
const read = async location => fs.promises.readFile(new URL(location).pathname);
const globals = {
- endowment: 42
+ globalProperty: 42,
+ globalLexical: "global" // should be overshadowed
+};
+
+const globalLexicals = {
+ globalLexical: "globalLexical"
};
const assertFixture = (t, namespace) => {
@@ -29,7 +34,8 @@ const assertFixture = (t, namespace) => {
clarke,
danny,
builtin,
- endowed,
+ receivedGlobalProperty,
+ receivedGlobalLexical,
typecommon,
typemodule,
typehybrid,
@@ -43,7 +49,12 @@ const assertFixture = (t, namespace) => {
t.equal(builtin, "builtin", "exports builtin");
- t.equal(endowed, globals.endowment, "exports endowment");
+ t.equal(receivedGlobalProperty, globals.globalProperty, "exports global");
+ t.equal(
+ receivedGlobalLexical,
+ globalLexicals.globalLexical,
+ "exports global lexical"
+ );
t.deepEqual(
typecommon,
[42, 42, 42, 42],
@@ -62,7 +73,7 @@ const assertFixture = (t, namespace) => {
t.equal(typehybrid, 42, "type=module and module= package carries exports");
};
-const fixtureAssertionCount = 10;
+const fixtureAssertionCount = 11;
// The "create builtin" test prepares a builtin module namespace object that
// gets threaded into all subsequent tests to satisfy the "builtin" module
@@ -89,7 +100,11 @@ test("loadLocation", async t => {
t.plan(fixtureAssertionCount);
const application = await loadLocation(read, fixture);
- const { namespace } = await application.import({ globals, modules });
+ const { namespace } = await application.import({
+ globals,
+ globalLexicals,
+ modules
+ });
assertFixture(t, namespace);
});
@@ -98,6 +113,7 @@ test("importLocation", async t => {
const { namespace } = await importLocation(read, fixture, {
globals,
+ globalLexicals,
modules
});
assertFixture(t, namespace);
@@ -108,7 +124,11 @@ test("makeArchive / parseArchive", async t => {
const archive = await makeArchive(read, fixture);
const application = await parseArchive(archive);
- const { namespace } = await application.import({ globals, modules });
+ const { namespace } = await application.import({
+ globals,
+ globalLexicals,
+ modules
+ });
assertFixture(t, namespace);
});
@@ -128,7 +148,11 @@ test("writeArchive / loadArchive", async t => {
await writeArchive(fakeWrite, read, "app.agar", fixture);
const application = await loadArchive(fakeRead, "app.agar");
- const { namespace } = await application.import({ globals, modules });
+ const { namespace } = await application.import({
+ globals,
+ globalLexicals,
+ modules
+ });
assertFixture(t, namespace);
});
@@ -149,6 +173,7 @@ test("writeArchive / importArchive", async t => {
await writeArchive(fakeWrite, read, "app.agar", fixture);
const { namespace } = await importArchive(fakeRead, "app.agar", {
globals,
+ globalLexicals,
modules
});
assertFixture(t, namespace);

MODIFY
packages/compartment-mapper/test/node_modules/app/main.js
packages/compartment-mapper/test/node_modules/app/main.js
-/* global endowment */
+/* global globalProperty, globalLexical */
import typemodule from 'typemodule';
import typecommon from 'typecommon';
@@ -13,4 +13,5 @@ export { clarke } from 'clarke';
export { danny } from 'danny';
export { builtin } from 'builtin';
-export const endowed = endowment;
+export const receivedGlobalProperty = globalProperty;
+export const receivedGlobalLexical = globalLexical;

feat(compartment-mapper): Thread global lexicals