@@ -3,48 +3,28 @@ import { makeTransformSource } from './transform-source.js';
33import makeModulePlugins from './babel-plugin.js' ;
44
55import * as h from './hidden.js' ;
6+ import { createSourceOptions } from './source-options.js' ;
7+ import { buildFunctorSource , buildModuleRecord } from './functor.js' ;
68
7- const { freeze } = Object ;
8-
9- /** @import {Options} from './module-source.js' */
9+ /** @import {ModuleSourceOptions} from './types/module-source.js' */
1010
1111const makeCreateStaticRecord = transformSource =>
1212 /**
1313 *
1414 * @param {string } moduleSource
15- * @param {Options } options
15+ * @param {ModuleSourceOptions } options
1616 */
1717 function createStaticRecord (
1818 moduleSource ,
1919 { sourceUrl, sourceMapUrl, sourceMap, sourceMapHook } = { } ,
2020 ) {
21- // Transform the Module source code.
22- const sourceOptions = {
21+ const sourceOptions = createSourceOptions ( {
2322 sourceUrl,
2423 sourceMap,
2524 sourceMapUrl,
2625 sourceMapHook,
27- sourceType : 'module' ,
28- // exportNames of variables that are only initialized and used, but
29- // never assigned to.
30- fixedExportMap : Object . create ( null ) ,
31- // Record of imported module specifier names to list of importNames.
32- // The importName '*' is that module's module namespace object.
33- imports : Object . create ( null ) ,
34- // List of module specifiers that we export all from.
35- exportAlls : [ ] ,
36- // exportNames of variables that are assigned to, or reexported and
37- // therefore assumed live. A reexported variable might not have any
38- // localName.
39- reexportMap : Object . create ( null ) ,
40- liveExportMap : Object . create ( null ) ,
41- hoistedDecls : [ ] ,
42- importSources : Object . create ( null ) ,
43- importDecls : [ ] ,
44- dynamicImport : { present : false } ,
45- // enables passing import.meta usage hints up.
46- importMeta : { present : false } ,
47- } ;
26+ } ) ;
27+
4828 if ( moduleSource . startsWith ( '#!' ) ) {
4929 // Comment out the shebang lines.
5030 moduleSource = `//${ moduleSource } ` ;
@@ -62,71 +42,23 @@ const makeCreateStaticRecord = transformSource =>
6242 ) ;
6343 }
6444
65- let preamble = sourceOptions . importDecls . join ( ',' ) ;
66- if ( preamble !== '' ) {
67- preamble = `let ${ preamble } ;` ;
68- }
69- const js = JSON . stringify ;
70- const isrc = sourceOptions . importSources ;
71- preamble += `${ h . HIDDEN_IMPORTS } ([${ Object . keys ( isrc )
72- . map (
73- src =>
74- `[${ js ( src ) } , [${ Object . entries ( isrc [ src ] )
75- . map ( ( [ exp , upds ] ) => `[${ js ( exp ) } ,[${ upds . join ( ',' ) } ]]` )
76- . join ( ',' ) } ]]`,
77- )
78- . join ( ',' ) } ]);`;
79- preamble += sourceOptions . hoistedDecls
80- . map ( ( [ vname , isOnce , cvname ] ) => {
81- let src = '' ;
82- if ( cvname ) {
83- // It's a function assigned to, so set its name property.
84- src = `Object.defineProperty(${ cvname } ,'name',{value:${ js ( vname ) } });` ;
85- }
86- const hDeclId = isOnce ? h . HIDDEN_ONCE : h . HIDDEN_LIVE ;
87- src += `${ hDeclId } .${ vname } (${ cvname || '' } );` ;
88- return src ;
89- } )
90- . join ( '' ) ;
91-
92- // The outer function destructures the module calling convention's internal
93- // variables into hidden lexical variables.
94- // The inner function binds `this` to `undefined` and overshadows the
95- // evaluator's `arguments` with a completely empty `arguments` object.
96- // There is no avoiding the overshadowing of `globalThis.arguments` if it
97- // exists in this emulation of ESM since the evaluator binds `arguments` as
98- // well.
99- // Relies on the evaluator to ensure these functions are strict.
100- let functorSource = `\
101- ({imports:${ h . HIDDEN_IMPORTS } ,liveVar:${ h . HIDDEN_LIVE } ,onceVar:${ h . HIDDEN_ONCE } ,import:${ h . HIDDEN_IMPORT } ,importMeta:${ h . HIDDEN_META } })=>(function(){'use strict';\
102- ${ preamble } \
103- ${ scriptSource }
104- })()
105- ` ;
45+ const functorSource = buildFunctorSource (
46+ scriptSource ,
47+ sourceOptions ,
48+ sourceUrl ,
49+ ) ;
10650
107- if ( sourceUrl ) {
108- functorSource += `//# sourceURL=${ sourceUrl } \n` ;
109- }
110- const moduleAnalysis = freeze ( {
111- exportAlls : freeze ( sourceOptions . exportAlls ) ,
112- imports : freeze ( sourceOptions . imports ) ,
113- liveExportMap : freeze ( sourceOptions . liveExportMap ) ,
114- fixedExportMap : freeze ( sourceOptions . fixedExportMap ) ,
115- reexportMap : freeze ( sourceOptions . reexportMap ) ,
116- needsImport : sourceOptions . dynamicImport . present ,
117- needsImportMeta : sourceOptions . importMeta . present ,
118- functorSource,
119- } ) ;
120- return moduleAnalysis ;
51+ return buildModuleRecord ( sourceOptions , functorSource ) ;
12152 } ;
12253
123- export const makeModuleAnalyzer = babel => {
124- const transformSource = makeTransformSource ( makeModulePlugins , babel ) ;
54+ export const makeModuleAnalyzer = ( ) => {
55+ const transformSource = makeTransformSource ( makeModulePlugins ) ;
12556 return makeCreateStaticRecord ( transformSource ) ;
12657} ;
12758
128- export const makeModuleTransformer = ( babel , importer ) => {
129- const transformSource = makeTransformSource ( makeModulePlugins , babel ) ;
59+ // TODO: May be unused; referenced only in ses/test262
60+ export const makeModuleTransformer = ( _babel , importer ) => {
61+ const transformSource = makeTransformSource ( makeModulePlugins ) ;
13062 const createStaticRecord = makeCreateStaticRecord ( transformSource ) ;
13163 return {
13264 rewrite ( ss ) {
0 commit comments