-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module-source): Specialize for XS native ModuleSource
- Loading branch information
Showing
9 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* global process */ | ||
import 'ses'; | ||
import { promises as fs } from 'fs'; | ||
// Lerna does not like dependency cycles. | ||
// With an explicit devDependency from module-source to compartment-mapper, | ||
// the build script stalls before running every package's build script. | ||
// yarn lerna run build | ||
// Omitting the dependency from package.json solves the problem and works | ||
// by dint of shared workspace node_modules. | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { makeBundle } from '@endo/compartment-mapper/bundle.js'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const read = async location => { | ||
const path = fileURLToPath(location); | ||
return fs.readFile(path); | ||
}; | ||
const write = async (location, content) => { | ||
const path = fileURLToPath(location); | ||
await fs.writeFile(path, content); | ||
}; | ||
|
||
const main = async () => { | ||
const xsPrelude = await makeBundle( | ||
read, | ||
new URL('../test/_xs.js', import.meta.url).href, | ||
{ | ||
tags: new Set(['xs']), | ||
}, | ||
); | ||
|
||
await fs.mkdir(fileURLToPath(new URL('../tmp', import.meta.url)), { | ||
recursive: true, | ||
}); | ||
await write(new URL('../tmp/test-xs.js', import.meta.url).href, xsPrelude); | ||
}; | ||
|
||
main().catch(err => { | ||
console.error('Error running main:', err); | ||
process.exitCode = 1; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// @ts-check | ||
/* global globalThis */ | ||
/* eslint-disable @endo/no-nullish-coalescing */ | ||
|
||
/** @typedef {{ import: string, as?: string, from: string } & { importAllFrom: string, as: string, from: string } & { export: string, as?: string, from?: string } & { exportAllFrom: string, as?: string } & {importFrom: string }} Binding */ | ||
|
||
/** @param {Binding[]} bindings */ | ||
function* getImports(bindings) { | ||
for (const binding of bindings) { | ||
if (binding.import !== undefined) { | ||
yield binding.from; | ||
} else if (binding.importFrom !== undefined) { | ||
yield binding.importFrom; | ||
} else if (binding.importAllFrom !== undefined) { | ||
yield binding.importAllFrom; | ||
} else if (binding.exportAllFrom !== undefined) { | ||
yield binding.exportAllFrom; | ||
} | ||
} | ||
} | ||
|
||
/** @param {Binding[]} bindings */ | ||
function* getExports(bindings) { | ||
for (const binding of bindings) { | ||
if (binding.export !== undefined) { | ||
yield binding.as ?? binding.export; | ||
} | ||
} | ||
} | ||
|
||
/** @param {Binding[]} bindings */ | ||
function* getReexports(bindings) { | ||
for (const binding of bindings) { | ||
if (binding.exportAllFrom !== undefined) { | ||
yield binding.exportAllFrom; | ||
} | ||
} | ||
} | ||
|
||
const ModuleSource = globalThis.ModuleSource; | ||
|
||
Object.defineProperties( | ||
ModuleSource.prototype, | ||
Object.getOwnPropertyDescriptors({ | ||
get imports() { | ||
return Array.from(new Set(getImports(this.bindings))); | ||
}, | ||
|
||
get exports() { | ||
return Array.from(new Set(getExports(this.bindings))); | ||
}, | ||
|
||
get reexports() { | ||
return Array.from(new Set(getReexports(this.bindings))); | ||
}, | ||
}), | ||
); | ||
|
||
export { ModuleSource }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* global globalThis */ | ||
export const NativeModuleSource = globalThis.ModuleSource; | ||
export const NativeCompartment = globalThis.Compartment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// This is a test fixture for XS validation (yarn test:xs with Moddable's xst | ||
// on the PATH) | ||
// This must be bundled with the -C xs condition to produce an artifact | ||
// (tmp/test-xs.js) suitable for running with xst. | ||
import { NativeModuleSource, NativeCompartment } from './_native.js'; | ||
// Eslint does not know about package reflexive imports (importing your own | ||
// package), which in this case is necessary to go through the conditional | ||
// export in package.json. | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import '@endo/module-source/shim.js'; | ||
import 'ses'; | ||
|
||
lockdown(); | ||
|
||
// spot checks | ||
assert(Object.isFrozen(Object)); | ||
|
||
const source = new ModuleSource(` | ||
import name from 'imported'; | ||
export * from 'reexported'; | ||
export default 42; | ||
throw new Error('unreached'); | ||
`); | ||
assert(source.imports[0] === 'imported'); | ||
assert(source.exports[0] === 'default'); | ||
assert(source.reexports[0] === 'reexported'); | ||
|
||
assert(source instanceof NativeModuleSource); | ||
|
||
const compartment = new NativeCompartment({ | ||
modules: { | ||
'.': { | ||
source: new ModuleSource(` | ||
export default 42; | ||
`), | ||
}, | ||
}, | ||
}); | ||
assert(compartment.importNow('.').default === 42); | ||
|
||
// to be continued with XS-specific adapters for Compartment in SES... |