From 7a272ee0f0cef3b5a07273f0b40bf9406d106343 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Tue, 8 Oct 2024 22:52:59 -0700 Subject: [PATCH] feat(compartment-mapper): Thread native flag to opt-in for native XS runtime modules --- packages/compartment-mapper/NEWS.md | 9 +++++++++ packages/compartment-mapper/README.md | 14 ++++++++++++++ .../compartment-mapper/src/import-archive-lite.js | 4 ++++ packages/compartment-mapper/src/import-lite.js | 3 +++ packages/compartment-mapper/src/link.js | 2 ++ packages/compartment-mapper/src/types/external.ts | 2 ++ packages/compartment-mapper/src/types/internal.ts | 1 + 7 files changed, 35 insertions(+) diff --git a/packages/compartment-mapper/NEWS.md b/packages/compartment-mapper/NEWS.md index a94bcd6d46..ed45043cfd 100644 --- a/packages/compartment-mapper/NEWS.md +++ b/packages/compartment-mapper/NEWS.md @@ -51,6 +51,15 @@ Experimental: enabled by default. See the signature of `loadFromMap()` in `import-lite.js` for details. +Experimental: + +- Adds a `__native__: true` option to all paths to import, that indicates that + the application will fall through to the native implementation of + Compartment, currently only available on XS, which lacks support for + precompiled module sources (as exist in many archived applications, + particularly Agoric smart contract bundles) and instead supports loading + modules from original sources (which is not possible at runtime on XS). + # v1.2.0 (2024-07-30) - Fixes incompatible behavior with Node.js package conditional exports #2276. diff --git a/packages/compartment-mapper/README.md b/packages/compartment-mapper/README.md index 6cfbdfb6b9..7a7fd0ad19 100644 --- a/packages/compartment-mapper/README.md +++ b/packages/compartment-mapper/README.md @@ -328,6 +328,20 @@ These will be appended to each module from the archive, for debugging purposes. The `@endo/bundle-source` and `@endo/import-bundle` tools integrate source maps for an end-to-end debugging experience. +# XS (experimental) + +The Compartment Mapper can use native XS `Compartment` and `ModuleSource` under +certain conditions: + +1. The application must be an XS script that was compiled with the `xs` + package condition. + This causes `ses`, `@endo/module-source`, and `@endo/import-bundle` to + provide slightly different implementations that can fall through to native + behavior. +2. The application must opt-in with the `__native__: true` option on any + of the compartment mapper methods that import modules like `importLocation` + and `importArchive`. + # Design Each of the workflows the compartment mapper executes a portion of one sequence diff --git a/packages/compartment-mapper/src/import-archive-lite.js b/packages/compartment-mapper/src/import-archive-lite.js index 5c50c41358..a0728c6dcc 100644 --- a/packages/compartment-mapper/src/import-archive-lite.js +++ b/packages/compartment-mapper/src/import-archive-lite.js @@ -257,6 +257,7 @@ export const parseArchive = async ( modules = undefined, importHook: exitModuleImportHook = undefined, parserForLanguage: parserForLanguageOption = {}, + __native__ = false, } = options; const parserForLanguage = freeze( @@ -343,6 +344,7 @@ export const parseArchive = async ( }), ), Compartment: CompartmentParseOption, + __native__, }); await pendingJobsPromise; @@ -362,6 +364,7 @@ export const parseArchive = async ( transforms, __shimTransforms__, Compartment: CompartmentOption = CompartmentParseOption, + __native__, importHook: exitModuleImportHook, } = options || {}; @@ -388,6 +391,7 @@ export const parseArchive = async ( transforms, __shimTransforms__, Compartment: CompartmentOption, + __native__, }); await pendingJobsPromise; diff --git a/packages/compartment-mapper/src/import-lite.js b/packages/compartment-mapper/src/import-lite.js index 71d748f992..51c4a0e06b 100644 --- a/packages/compartment-mapper/src/import-lite.js +++ b/packages/compartment-mapper/src/import-lite.js @@ -152,6 +152,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { transforms, __shimTransforms__, Compartment: CompartmentOption = LoadCompartmentOption, + __native__, importHook: exitModuleImportHook, } = options; const compartmentExitModuleImportHook = exitModuleImportHookMaker({ @@ -201,6 +202,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { syncModuleTransforms, __shimTransforms__, Compartment: CompartmentOption, + __native__, })); } else { // sync module transforms are allowed, because they are "compatible" @@ -215,6 +217,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { syncModuleTransforms, __shimTransforms__, Compartment: CompartmentOption, + __native__, })); } diff --git a/packages/compartment-mapper/src/link.js b/packages/compartment-mapper/src/link.js index b2b1414f5d..b2f70f9652 100644 --- a/packages/compartment-mapper/src/link.js +++ b/packages/compartment-mapper/src/link.js @@ -258,6 +258,7 @@ export const link = ( moduleTransforms, syncModuleTransforms, __shimTransforms__ = [], + __native__ = false, archiveOnly = false, Compartment = defaultCompartment, } = options; @@ -362,6 +363,7 @@ export const link = ( transforms, __shimTransforms__, __options__: true, + __native__, }); if (!archiveOnly) { diff --git a/packages/compartment-mapper/src/types/external.ts b/packages/compartment-mapper/src/types/external.ts index d0de4e9916..6a6cd95d81 100644 --- a/packages/compartment-mapper/src/types/external.ts +++ b/packages/compartment-mapper/src/types/external.ts @@ -29,6 +29,7 @@ export type ExecuteOptions = Partial<{ __shimTransforms__: Array; attenuations: Record; Compartment: typeof Compartment; + __native__: boolean; }> & ModulesOption & ExitModuleImportHookOption; @@ -38,6 +39,7 @@ export type ParseArchiveOptions = Partial<{ computeSha512: HashFn; computeSourceLocation: ComputeSourceLocationHook; computeSourceMapLocation: ComputeSourceMapLocationHook; + __native__: boolean; }> & ModulesOption & CompartmentOption & diff --git a/packages/compartment-mapper/src/types/internal.ts b/packages/compartment-mapper/src/types/internal.ts index 8cce0cd5f4..bb6c6c26f3 100644 --- a/packages/compartment-mapper/src/types/internal.ts +++ b/packages/compartment-mapper/src/types/internal.ts @@ -44,6 +44,7 @@ export type LinkOptions = { moduleTransforms?: ModuleTransforms; syncModuleTransforms?: SyncModuleTransforms; archiveOnly?: boolean; + __native__?: boolean; } & ExecuteOptions; export type LinkResult = {