Skip to content

Commit

Permalink
chore: change 'use' and 'utter' to 'need' in import meta detection to…
Browse files Browse the repository at this point in the history
… adhere to the compartment proposal
  • Loading branch information
naugtur committed Jun 13, 2022
1 parent c176b04 commit ce2caee
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 32 deletions.
4 changes: 3 additions & 1 deletion packages/ses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ A compiled static module record has the following shape:
exports to notifier functions.
A notifier function accepts an update function and registers
to receive updates for the value exported by the other module.
- `importMeta` is a null-prototype object with keys transferred from `meta` property in the envelope returned by importHook and/or mutated by calling `importMetaHook(importMeta)`
- `importMeta` is a null-prototype object with keys transferred from `meta`
property in the envelope returned by importHook and/or mutated by
calling `importMetaHook(moduleSpecifier, importMeta)`
- `liveVar` is a record that maps names exported by this module
to a function that may be called to initialize or update
the corresponding value in another module.
Expand Down
8 changes: 4 additions & 4 deletions packages/ses/src/module-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ export const makeModuleInstance = (
compartment,
moduleSpecifier,
staticModuleRecord,
meta: moduleRecordMeta,
importMeta: moduleRecordMeta,
} = moduleRecord;
const {
reexports: exportAlls = [],
__syncModuleProgram__: functorSource,
__fixedExportMap__: fixedExportMap = {},
__liveExportMap__: liveExportMap = {},
__usesImportMeta__: usesImportMeta = false,
__needsImportMeta__: needsImportMeta = false,
} = staticModuleRecord;

const compartmentFields = weakmapGet(privateFields, compartment);
Expand Down Expand Up @@ -169,8 +169,8 @@ export const makeModuleInstance = (
if (moduleRecordMeta) {
assign(importMeta, moduleRecordMeta);
}
if (usesImportMeta && importMetaHook) {
importMetaHook(importMeta);
if (needsImportMeta && importMetaHook) {
importMetaHook(moduleSpecifier, importMeta);
}

// {_localName_: [{get, set, notify}]} used to merge all the export updaters.
Expand Down
8 changes: 4 additions & 4 deletions packages/ses/src/module-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const loadRecord = async (
pendingJobs,
moduleLoads,
errors,
meta,
importMeta,
) => {
const { resolveHook, moduleRecords } = weakmapGet(
compartmentPrivateFields,
Expand All @@ -81,7 +81,7 @@ const loadRecord = async (
staticModuleRecord,
moduleSpecifier,
resolvedImports,
meta,
importMeta,
});

// Enqueue jobs to load this module's shallow dependencies.
Expand Down Expand Up @@ -183,7 +183,7 @@ const loadWithoutErrorAnnotation = async (
compartment: aliasCompartment = compartment,
specifier: aliasSpecifier = moduleSpecifier,
record: aliasModuleRecord,
meta,
importMeta,
} = staticModuleRecord;

const aliasRecord = await loadRecord(
Expand All @@ -195,7 +195,7 @@ const loadWithoutErrorAnnotation = async (
pendingJobs,
moduleLoads,
errors,
meta,
importMeta,
);
mapSet(moduleRecords, moduleSpecifier, aliasRecord);
return aliasRecord;
Expand Down
4 changes: 2 additions & 2 deletions packages/ses/test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export const makeLocator = root => {
};
};

const wrapImporterWithMeta = (importer, meta) => async specifier => {
const wrapImporterWithMeta = (importer, importMeta) => async specifier => {
const moduleRecord = await importer(specifier);
return { record: moduleRecord, meta };
return { record: moduleRecord, importMeta };
};

// makeNodeImporter conveniently curries makeImporter with a Node.js style
Expand Down
4 changes: 2 additions & 2 deletions packages/ses/test/test-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ test('importMetaHook', async t => {
name: 'https://example.com',
resolveHook: resolveNode,
importHook: makeImportHook('https://example.com'),
importMetaHook: meta => {
importMetaHook: (_moduleSpecifier, meta) => {
meta.url = 'https://example.com/index.js';
},
},
Expand Down Expand Up @@ -632,7 +632,7 @@ test('importMetaHook and meta from record', async t => {
importHook: makeImportHook('https://example.com', {
meta: { url: 'https://example.com/index.js' },
}),
importMetaHook: meta => {
importMetaHook: (_moduleSpecifier, meta) => {
meta.url += '?foo';
meta.isStillMutableHopefully = 1;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/static-module-record/src/babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function makeModulePlugins(options) {
path.node.meta.name === 'import' &&
path.node.property.name === 'meta'
) {
importMeta.uttered = true;
importMeta.present = true;
path.replaceWithMultiple([
replace(path.node, hiddenIdentifier(h.HIDDEN_META)),
]);
Expand Down
4 changes: 3 additions & 1 deletion packages/static-module-record/src/hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const HIDDEN_IMPORT = `${HIDDEN_PREFIX}import`;
export const HIDDEN_IMPORT_SELF = `${HIDDEN_PREFIX}importSelf`;
export const HIDDEN_IMPORTS = `${HIDDEN_PREFIX}imports`;
export const HIDDEN_ONCE = `${HIDDEN_PREFIX}once`;
export const HIDDEN_META = `${HIDDEN_PREFIX}import_meta`;
// HIDDEN_META is used to replace `import.meta`. The value fits the original
// length so it doesn’t displace the column number of following text
export const HIDDEN_META = `${HIDDEN_PREFIX}___meta`;
export const HIDDEN_LIVE = `${HIDDEN_PREFIX}live`;
export const HIDDEN_IDENTIFIERS = [
HIDDEN_A,
Expand Down
4 changes: 2 additions & 2 deletions packages/static-module-record/src/static-module-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function StaticModuleRecord(source, url) {
liveExportMap,
fixedExportMap,
exportAlls,
importMetaUttered,
needsImportMeta,
} = analyzeModule({ string: source, url });
this.imports = freeze([...keys(imports)].sort());
this.exports = freeze(
Expand All @@ -61,6 +61,6 @@ export function StaticModuleRecord(source, url) {
this.__syncModuleProgram__ = functorSource;
this.__liveExportMap__ = liveExportMap;
this.__fixedExportMap__ = fixedExportMap;
this.__usesImportMeta__ = importMetaUttered;
this.__needsImportMeta__ = needsImportMeta;
freeze(this);
}
4 changes: 2 additions & 2 deletions packages/static-module-record/src/transform-analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const makeCreateStaticRecord = transformSource =>
importSources: Object.create(null),
importDecls: [],
// enables passing import.meta usage hints up.
importMeta: { uttered: false },
importMeta: { present: false },
};
if (moduleSource.startsWith('#!')) {
// Comment out the shebang lines.
Expand Down Expand Up @@ -90,7 +90,7 @@ const makeCreateStaticRecord = transformSource =>
imports: freeze(sourceOptions.imports),
liveExportMap: freeze(sourceOptions.liveExportMap),
fixedExportMap: freeze(sourceOptions.fixedExportMap),
importMetaUttered: sourceOptions.importMeta.uttered,
needsImportMeta: sourceOptions.importMeta.present,
functorSource,
});
return moduleAnalysis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const freshOptions = () => {
hoistedDecls: [],
importSources: Object.create(null),
importDecls: [],
importMeta: { uttered: false },
importMeta: { present: false },
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/static-module-record/test/fixtures/large.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function makeModulePlugins(options) {
path.node.property.name === 'meta'
) {
if (doAnalyze) {
importMeta.uttered = true;
importMeta.present = true;
}
if (doTransform) {
console.error('at least I tried')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,13 @@ test('import meta in export', t => {
});
t.is(namespace.a, 'ok file://meta.url');
});
test('import meta member uttered', t => {
test('import meta member present', t => {
const record = new StaticModuleRecord(`const a = import.meta.url`);
t.is(record.__usesImportMeta__, true);
t.is(record.__needsImportMeta__, true);
});
test('import meta uttered', t => {
test('import meta present', t => {
const record = new StaticModuleRecord(`const a = import.meta`);
t.is(record.__usesImportMeta__, true);
t.is(record.__needsImportMeta__, true);
});

test('export names', t => {
Expand Down
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9644,13 +9644,6 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-2.0.0.tgz#c819ac728059a461cab1c3889a2be3c49a004d7f"
integrity sha1-yBmscoBZpGHKscOImivjxJoATX8=
dependencies:
find-up "^2.1.0"

platform@^1.3.3:
version "1.3.6"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.6.tgz#48b4ce983164b209c2d45a107adb31f473a6e7a7"
Expand Down

0 comments on commit ce2caee

Please sign in to comment.