Skip to content

Commit

Permalink
feat(module-source): Introduce a shim that composes with lockdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 20, 2024
1 parent 013dfb8 commit e8839a4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/module-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"main": "./index.js",
"exports": {
".": "./index.js",
"./shim.js": "./shim.js",
"./package.json": "./package.json"
},
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions packages/module-source/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* global globalThis */

import { ModuleSource } from './index.js';

Object.defineProperty(globalThis, 'ModuleSource', {
value: ModuleSource,
enumerable: false,
writable: true,
configurable: true,
});
15 changes: 15 additions & 0 deletions packages/module-source/src/module-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ const analyzeModule = makeModuleAnalyzer();
* @property {SourceMapHook} [sourceMapHook]
*/

// https://github.com/tc39/proposal-source-phase-imports?tab=readme-ov-file#js-module-source
function AbstractModuleSource() {
// no-op, safe to super()
}

// WebAssembly and ModuleSource are both in motion.
// The Source Phase Imports proposal implies an additional AbstractModuleSource
// layer above the existing WebAssembly.Module that would be shared by
// the JavaScript ModuleSource prototype chain.
// At time of writing, no version of WebAssembly provides the shared base class,
// and the shimmed ModuleSource gains nothing from sharing that prototype when
// it comes into being.

// XXX implements import('ses').PrecompiledModuleSource but adding
// `@implements` errors that this isn't a class and `@returns` errors that
// there's no value returned.
Expand Down Expand Up @@ -100,3 +113,5 @@ export function ModuleSource(source, opts = {}) {
this.__needsImportMeta__ = needsImportMeta;
freeze(this);
}

Object.setPrototypeOf(ModuleSource.prototype, AbstractModuleSource.prototype);
8 changes: 7 additions & 1 deletion packages/ses/test/module-source.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference types="ses">

import test from 'ava';
import '../index.js';
import { ModuleSource } from '@endo/module-source';
import '@endo/module-source/shim.js';

lockdown();

Expand Down Expand Up @@ -42,3 +44,7 @@ test('module source constructor', t => {
'ModuleSource imports should be frozen',
);
});

test('ModuleSource is a shared intrinsic', t => {
t.truthy(ModuleSource === new Compartment().globalThis.ModuleSource);
});

0 comments on commit e8839a4

Please sign in to comment.