Skip to content

Commit

Permalink
wip: import.meta implementation according to #291
Browse files Browse the repository at this point in the history
  • Loading branch information
naugtur committed Apr 25, 2022
1 parent 4f2b3de commit d667716
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
15 changes: 6 additions & 9 deletions packages/ses/src/module-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
mapGet,
weakmapGet,
reflectHas,
assign,
} from './commons.js';
import { compartmentEvaluate } from './compartment-evaluate.js';

Expand Down Expand Up @@ -128,6 +129,7 @@ export const makeModuleInstance = (
__syncModuleProgram__: functorSource,
__fixedExportMap__: fixedExportMap = {},
__liveExportMap__: liveExportMap = {},
__usesImportMeta: usesImportMeta = false,
} = staticModuleRecord;

const compartmentFields = weakmapGet(privateFields, compartment);
Expand Down Expand Up @@ -159,20 +161,15 @@ export const makeModuleInstance = (
const liveVar = create(null);

const metaVar = create(null);
try {
metaVar.url = new URL(
moduleSpecifier,
compartmentFields.name || '.',
).toString();
} catch (e) {
metaVar.url = moduleSpecifier;
console.error('Wont URL:', moduleSpecifier, compartmentFields.name);
if(usesImportMeta){
assign(metaVar, importMetaHook())
}


// {_localName_: [{get, set, notify}]} used to merge all the export updaters.
const localGetNotify = create(null);

// {[importName: string]: notify(update(newValue))} Used by code that imports
// {[importName: string]: notify(update(newVametaVarlue))} Used by code that imports
// one of this module's exports, so that their update function will
// be notified when this binding is initialized or updated.
const notifiers = create(null);
Expand Down
9 changes: 8 additions & 1 deletion packages/ses/src/module-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const loadRecord = async (
moduleLoads,
errors,
) => {
const { resolveHook, moduleRecords } = weakmapGet(
const { resolveHook, moduleRecords, importMetaHook } = weakmapGet(
compartmentPrivateFields,
compartment,
);
Expand All @@ -82,6 +82,13 @@ const loadRecord = async (
resolvedImports,
});

let importMeta;

if (staticModuleRecord.__usesImportMeta && importMetaHook) {
importMeta = await importMetaHook()
} else {
// provide import.meta.url anyway in case we're getting someone else's implementation of StaticModuleRecord?
}
// Enqueue jobs to load this module's shallow dependencies.
for (const fullSpecifier of values(resolvedImports)) {
// Behold: recursion.
Expand Down
20 changes: 15 additions & 5 deletions packages/static-module-record/src/babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function makeModulePlugins(options) {
importDecls,
importSources,
liveExportMap,
importMetaProperties,
} = options;

if (sourceType !== 'module') {
Expand Down Expand Up @@ -308,13 +309,22 @@ function makeModulePlugins(options) {
},
};

const moduleVisitor = (doAnalyze, doTransform) => ({
MetaProperty(path){
if(path.node.meta.name==='import' && doTransform){
path.replaceWithMultiple([replace(path.node, t.identifier(h.HIDDEN_META))]);
const moduleVisitor = (doAnalyze, doTransform) => ({
MetaProperty(path) {
if (path.node.meta.name === 'import') {
if (doAnalyze && path.parentPath.isMemberExpression()) {
importMetaProperties.add(path.parentPath.node.property.name);
}
if (doTransform) {
// I saw meta added to import somewhere, but it was dead code. Leaving this here in case there's a reason to switch to meta as a field on import.
// path.replaceWith(replace(path.node, t.metaProperty(hiddenIdentifier(h.HIDDEN_IMPORT),t.identifier('meta'))));
path.replaceWithMultiple([
replace(path.node, hiddenIdentifier(h.HIDDEN_META)),
]);
}
}
},
// We handle all the import and export productions.
// We handle all the import and export productions.
ImportDeclaration(path) {
if (doAnalyze) {
const specs = path.node.specifiers;
Expand Down
2 changes: 1 addition & 1 deletion packages/static-module-record/src/hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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}meta`;
export const HIDDEN_META = `${HIDDEN_PREFIX}import_meta`;
export const HIDDEN_LIVE = `${HIDDEN_PREFIX}live`;
export const HIDDEN_IDENTIFIERS = [
HIDDEN_A,
Expand Down
5 changes: 5 additions & 0 deletions packages/static-module-record/src/static-module-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function StaticModuleRecord(source, url) {
liveExportMap,
fixedExportMap,
exportAlls,
importMetaProperties,
} = analyzeModule({ string: source, url });
this.imports = freeze([...keys(imports)].sort());
this.exports = freeze(
Expand All @@ -60,5 +61,9 @@ export function StaticModuleRecord(source, url) {
this.__syncModuleProgram__ = functorSource;
this.__liveExportMap__ = liveExportMap;
this.__fixedExportMap__ = fixedExportMap;
// this.__usesImport
this.__usesImportMeta = importMetaProperties.length > 0;
// useful for knowing if resolve or any other expensive features are used
this.__usesImportMetaProperties = importMetaProperties;
freeze(this);
}
10 changes: 7 additions & 3 deletions packages/static-module-record/src/transform-analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const makeTransformSource = (babel = null) => {
compact: true,
verbatim: true,
});
// console.log(`transformed`, transformedCode);
// console.error(`transformed`, transformedCode);
return transformedCode;
};

Expand All @@ -65,6 +65,8 @@ const makeCreateStaticRecord = transformSource =>
hoistedDecls: [],
importSources: Object.create(null),
importDecls: [],
// enables passing import.meta usage hints up.
importMetaProperties: new Set(),
};
if (moduleSource.startsWith('#!')) {
// Comment out the shebang lines.
Expand Down Expand Up @@ -123,12 +125,14 @@ const makeCreateStaticRecord = transformSource =>
if (url) {
functorSource += `//# sourceURL=${url}\n`;
}

const moduleAnalysis = freeze({
exportAlls: freeze(sourceOptions.exportAlls),
imports: freeze(sourceOptions.imports),
liveExportMap: freeze(sourceOptions.liveExportMap),
fixedExportMap: freeze(sourceOptions.fixedExportMap),
importMetaProperties: freeze(
Array.from(sourceOptions.importMetaProperties),
),
functorSource,
});
return moduleAnalysis;
Expand Down Expand Up @@ -198,7 +202,7 @@ export const makeModuleTransformer = (babel, importer) => {
? maybeSource.slice(0, -1)
: maybeSource;

// console.log(ss.isExpr, `generated`, src, `from`, ast);
console.log(ss.isExpr, `generated`, src, `from`, ast);
return { ...ss, endowments, src: actualSource };
},
};
Expand Down

0 comments on commit d667716

Please sign in to comment.