-
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.
fix(static-module-record): babelPlugin visitor to not skip declaratio…
…ns in exports + benchmark setup (#1188) * babelPlugin fix proposal and benchmarking setup * remove debris Co-authored-by: Michael FIG <mfig@agoric.com> * more cleanup
- Loading branch information
Showing
10 changed files
with
897 additions
and
83 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
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,39 @@ | ||
import * as babelParser from '@babel/parser'; | ||
import babelGenerate from '@agoric/babel-generator'; | ||
import babelTraverse from '@babel/traverse'; | ||
import babelTypes from '@babel/types'; | ||
|
||
const parseBabel = babelParser.default | ||
? babelParser.default.parse | ||
: babelParser.parse || babelParser; | ||
|
||
const visitorFromPlugin = plugin => plugin({ types: babelTypes }).visitor; | ||
|
||
const traverseBabel = babelTraverse.default || babelTraverse; | ||
const generateBabel = babelGenerate.default || babelGenerate; | ||
|
||
export const makeTransformSource = (makeModulePlugins, babel = null) => { | ||
if (babel !== null) { | ||
throw new Error( | ||
`transform-analyze.js no longer allows injecting babel; use \`null\``, | ||
); | ||
} | ||
|
||
const transformSource = (code, sourceOptions = {}) => { | ||
const { analyzePlugin, transformPlugin } = makeModulePlugins(sourceOptions); | ||
|
||
const ast = parseBabel(code, { sourceType: sourceOptions.sourceType }); | ||
|
||
traverseBabel(ast, visitorFromPlugin(analyzePlugin)); | ||
traverseBabel(ast, visitorFromPlugin(transformPlugin)); | ||
|
||
const { code: transformedCode } = generateBabel(ast, { | ||
retainLines: true, | ||
compact: true, | ||
verbatim: true, | ||
}); | ||
return transformedCode; | ||
}; | ||
|
||
return transformSource; | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/static-module-record/test/benchmark-babel-plugin.js
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,53 @@ | ||
import Benchmark from 'benchmark'; | ||
import fs from 'fs'; | ||
import url from 'url'; | ||
import { makeTransformSource } from '../src/transformSource.js'; | ||
import makeModulePlugins from '../src/babelPlugin.js'; | ||
|
||
const suite = new Benchmark.Suite(); | ||
|
||
const resolveLocal = path => url.fileURLToPath(new URL(path, import.meta.url)); | ||
const cases = [ | ||
{ | ||
name: 'small', | ||
fixture: fs.readFileSync(resolveLocal('./fixtures/small.js'), 'utf8'), | ||
}, | ||
{ | ||
name: 'large', | ||
fixture: fs.readFileSync(resolveLocal('./fixtures/large.js'), 'utf8'), | ||
}, | ||
{ | ||
name: 'exportheavy', | ||
fixture: fs.readFileSync(resolveLocal('./fixtures/exportheavy.js'), 'utf8'), | ||
}, | ||
]; | ||
|
||
const transformSource = makeTransformSource(makeModulePlugins); | ||
const freshOptions = () => { | ||
return { | ||
sourceType: 'module', | ||
fixedExportMap: Object.create(null), | ||
imports: Object.create(null), | ||
exportAlls: [], | ||
liveExportMap: Object.create(null), | ||
hoistedDecls: [], | ||
importSources: Object.create(null), | ||
importDecls: [], | ||
importMeta: { uttered: false }, | ||
}; | ||
}; | ||
|
||
cases.map(testCase => | ||
suite.add(testCase.name, () => { | ||
transformSource(testCase.fixture, freshOptions()); | ||
}), | ||
); | ||
|
||
suite | ||
.on('cycle', event => { | ||
console.log(String(event.target)); | ||
}) | ||
.on('error', event => { | ||
console.log(String(event.target), event.target.error); | ||
}) | ||
.run(); |
55 changes: 55 additions & 0 deletions
55
packages/static-module-record/test/fixtures/exportheavy.js
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,55 @@ | ||
/* eslint-disable */ | ||
console.error("This is a code sample for trying out babel transforms, it's not meant to be run"); | ||
|
||
export { mapIterable, filterIterable } from './src/helpers/iter-helpers.js'; | ||
export { | ||
PASS_STYLE, | ||
isObject, | ||
assertChecker, | ||
getTag, | ||
hasOwnPropertyOf, | ||
} from './src/helpers/passStyle-helpers.js'; | ||
|
||
export { getErrorConstructor, toPassableError } from './src/helpers/error.js'; | ||
export { getInterfaceOf } from './src/helpers/remotable.js'; | ||
|
||
export { | ||
nameForPassableSymbol, | ||
passableSymbolForName, | ||
} from './src/helpers/symbol.js'; | ||
|
||
export { passStyleOf, assertPassable } from './src/passStyleOf.js'; | ||
|
||
export { deeplyFulfilled } from './src/deeplyFulfilled.js'; | ||
|
||
export { makeTagged } from './src/makeTagged.js'; | ||
export { Remotable, Far, ToFarFunction } from './src/make-far.js'; | ||
|
||
export { QCLASS, makeMarshal } from './src/marshal.js'; | ||
export { stringify, parse } from './src/marshal-stringify.js'; | ||
// Works, but not yet used | ||
// export { decodeToJustin } from './src/marshal-justin.js'; | ||
|
||
export { | ||
assertRecord, | ||
assertCopyArray, | ||
assertRemotable, | ||
isRemotable, | ||
isRecord, | ||
isCopyArray, | ||
} from './src/typeGuards.js'; | ||
|
||
// eslint-disable-next-line import/export | ||
export * from './src/types.js'; | ||
|
||
|
||
const { details: X } = assert; | ||
|
||
// This is a pathological minimum, but exercised by the unit test. | ||
export const MIN_DATA_BUFFER_LENGTH = 1; | ||
|
||
// Calculate how big the transfer buffer needs to be. | ||
export const TRANSFER_OVERHEAD_LENGTH = | ||
BigUint64Array.BYTES_PER_ELEMENT + Int32Array.BYTES_PER_ELEMENT; | ||
export const MIN_TRANSFER_BUFFER_LENGTH = | ||
MIN_DATA_BUFFER_LENGTH + TRANSFER_OVERHEAD_LENGTH; |
Oops, something went wrong.