test: finish splitting out make-importer
MODIFY
src/index.js
src/index.js
@@ -59,10 +59,12 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
// The functor captures the SES `arguments`, which is definitely
// less bad than the functor's arguments (which we are trying to
// hide.
+ //
// It must also be strict to enforce strictness of modules.
+ // We use destructuring parameters, so 'use strict' is not allowed
+ // but the function actually is strict.
const functorSource = `\
(({${h.HIDDEN_IMPORT}, ${h.HIDDEN_IMPORTS}, ${h.HIDDEN_ONCE}, ${h.HIDDEN_LIVE}}) => { \
- 'use strict'; \
${preamble} \
${scriptSource}
})`;
@@ -77,17 +79,25 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
return moduleStaticRecord;
}
+ return {
+ rewrite(ss) {
+ // Transform the source into evaluable form.
+ const { allowHidden, evaluateProgram, endowments, src: source, url } = ss;
+
// Make an importer that uses our transform for its submodules.
function curryImporter(srcSpec) {
- return makeImporter(
- srcSpec,
- source => transformSource(source, { sourceType: 'module' }),
- createStaticRecord,
- );
+ const evaluate = (src, postEndowments = {}) => {
+ const endow = Object.create(null, {
+ ...Object.getOwnPropertyDescriptors(endowments),
+ ...Object.getOwnPropertyDescriptors(postEndowments),
+ });
+ return evaluateProgram(src, endow, { allowHidden: true });
+ };
+ return makeImporter(srcSpec, createStaticRecord, evaluate);
}
// Create an import expression for the given URL.
- function makeImportExpr(url) {
+ function makeImportExpr() {
// TODO: Provide a way to allow hardening of the import expression.
const importExpr = spec => curryImporter({ url, spec });
importExpr.meta = Object.create(null);
@@ -95,14 +105,9 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
return importExpr;
}
- return {
- rewrite(ss) {
- // Transform the source into evaluable form.
- const { allowHidden, endowments, src: source, url } = ss;
-
// Add the $h_import hidden endowment for import expressions.
Object.assign(endowments, {
- [h.HIDDEN_IMPORT]: makeImportExpr(url),
+ [h.HIDDEN_IMPORT]: makeImportExpr(),
});
if (ss.sourceType === 'module') {

MODIFY
test/test-export-default.js
test/test-export-default.js
@@ -4,10 +4,39 @@ import { makeEvaluators } from '@agoric/evaluate';
import * as babelCore from '@babel/core';
import makeModuleTransformer from '../src/index';
+import * as h from '../src/hidden';
test('export default', async t => {
try {
- const transforms = [makeModuleTransformer(babelCore)];
+ const makeImporter = (srcSpec, createStaticRecord, evaluateProgram) => {
+ const { spec, source } = srcSpec;
+ let actualSource;
+ const doImport = async () => {
+ const staticRecord = createStaticRecord(actualSource);
+ const exportNS = {};
+ const functorArg = {
+ [h.HIDDEN_ONCE]: {
+ default(val) {
+ exportNS.default = val;
+ },
+ },
+ [h.HIDDEN_IMPORTS](_imports) {},
+ };
+ // console.log(staticRecord.functorSource);
+ evaluateProgram(staticRecord.functorSource)(functorArg);
+ return exportNS;
+ };
+
+ if (spec === undefined && source !== undefined) {
+ actualSource = source;
+ return doImport;
+ }
+
+ actualSource = `export default ${JSON.stringify(spec)};`;
+ return doImport();
+ };
+
+ const transforms = [makeModuleTransformer(babelCore, makeImporter)];
const { evaluateExpr, evaluateProgram, evaluateModule } = makeEvaluators({
transforms,
});
@@ -17,18 +46,7 @@ test('export default', async t => {
})) {
t.deepEqual(
// eslint-disable-next-line no-await-in-loop
- await myEval(
- `import('foo')`,
- {},
- {
- loader(spec) {
- return Promise.resolve([
- '.',
- `export default ${JSON.stringify(spec)};`,
- ]);
- },
- },
- ),
+ await myEval(`import('foo')`, {}),
{ default: 'foo' },
`${name} import expression works`,
);

MODIFY
test/test-export-named.js
test/test-export-named.js
@@ -4,10 +4,54 @@ import { makeEvaluators } from '@agoric/evaluate';
import * as babelCore from '@babel/core';
import makeModuleTransformer from '../src/index';
+import * as h from '../src/hidden';
test(`export named`, async t => {
try {
- const transforms = [makeModuleTransformer(babelCore)];
+ const makeImporter = (srcSpec, createStaticRecord, evaluateProgram) => {
+ const { spec, source } = srcSpec;
+ let actualSource;
+ const doImport = async () => {
+ const staticRecord = createStaticRecord(actualSource);
+ const exportNS = {};
+ const onceProxy = new Proxy(
+ {},
+ {
+ get(_target, prop) {
+ return value => (exportNS[prop] = value);
+ },
+ },
+ );
+ const endow = Object.create(null, {
+ def: {
+ get() {
+ return exportNS.def;
+ },
+ set(value) {
+ exportNS.def = value;
+ },
+ },
+ });
+ const functorArg = {
+ [h.HIDDEN_ONCE]: onceProxy,
+ [h.HIDDEN_LIVE]: onceProxy,
+ [h.HIDDEN_IMPORTS](_imports) {},
+ };
+ // console.log(staticRecord.functorSource);
+ evaluateProgram(staticRecord.functorSource, endow)(functorArg);
+ return exportNS;
+ };
+
+ if (spec === undefined && source !== undefined) {
+ actualSource = source;
+ return doImport;
+ }
+
+ throw Error(`No import expression`);
+ // return doImport();
+ };
+
+ const transforms = [makeModuleTransformer(babelCore, makeImporter)];
const { evaluateModule } = makeEvaluators({
transforms,
});
@@ -48,7 +92,55 @@ export const ghi = 789;
test(`export hoisting`, async t => {
try {
- const transforms = [makeModuleTransformer(babelCore)];
+ const makeImporter = (srcSpec, createStaticRecord, evaluateProgram) => {
+ const { spec, source } = srcSpec;
+ let actualSource;
+ const doImport = async () => {
+ const staticRecord = createStaticRecord(actualSource);
+ const exportNS = {};
+ const onceProxy = new Proxy(
+ {},
+ {
+ get(_target, prop) {
+ return value => (exportNS[prop] = value);
+ },
+ },
+ );
+ const makeLive = vname => ({
+ get() {
+ if (vname in exportNS) {
+ return exportNS[vname];
+ }
+ throw ReferenceError(`${vname} is not defined`);
+ },
+ set(value) {
+ return (exportNS[vname] = value);
+ },
+ });
+ const endow = Object.create(null, {
+ abc: makeLive('abc'),
+ fn: makeLive('fn'),
+ });
+ const functorArg = {
+ [h.HIDDEN_ONCE]: onceProxy,
+ [h.HIDDEN_LIVE]: onceProxy,
+ [h.HIDDEN_IMPORTS](_imports) {},
+ };
+ console.log(staticRecord.functorSource);
+ evaluateProgram(staticRecord.functorSource, endow)(functorArg);
+ return exportNS;
+ };
+
+ if (spec === undefined && source !== undefined) {
+ actualSource = source;
+ return doImport;
+ }
+
+ throw Error(`No import expression`);
+ // return doImport();
+ };
+
+ const transforms = [makeModuleTransformer(babelCore, makeImporter)];
const { evaluateModule } = makeEvaluators({
transforms,
});

test: finish splitting out make-importer