Skip to content

Commit

Permalink
fix: excise @babel/core except from ui-components
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 13, 2021
1 parent f533f76 commit af564f1
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 58 deletions.
1 change: 0 additions & 1 deletion packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@agoric/tame-metering": "^1.3.9",
"@agoric/transform-metering": "^1.4.12",
"@agoric/xsnap": "^0.6.2",
"@babel/core": "^7.5.0",
"@babel/generator": "^7.6.4",
"@endo/base64": "^0.1.0",
"@types/tmp": "^0.2.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { performance } from 'perf_hooks';
import { spawn } from 'child_process';
import { type as osType } from 'os';
import { Worker } from 'worker_threads';
import * as babelCore from '@babel/core';
import anylogger from 'anylogger';
import { tmpName } from 'tmp';

Expand Down Expand Up @@ -213,7 +212,7 @@ export async function makeSwingsetController(
// cannot be implemented within a non-start-Compartment. We build it out
// here and pass it to the kernel, which then passes it to vats. This is
// intended to be powerless.
const mt = makeMeteringTransformer(babelCore);
const mt = makeMeteringTransformer();
function transformMetering(src, getMeter) {
// 'getMeter' provides the meter to which the transformation itself is
// billed (the COMPUTE meter is charged the length of the source string).
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/test/metering/test-metering.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { assert, details as X } from '@agoric/assert';
import bundleSource from '@agoric/bundle-source';
import { importBundle } from '@agoric/import-bundle';
import { makeMeter, makeMeteringTransformer } from '@agoric/transform-metering';
import * as babelCore from '@babel/core';
import re2 from 're2';
import test from 'ava';
import { waitUntilQuiescent } from '../../src/waitUntilQuiescent';
Expand Down Expand Up @@ -52,7 +51,7 @@ async function meteredImportBundle(bundle, endowments) {
// controller into the kernel, so the kernel can create dynamic vats. We can also
// pass it into static vats, so within-vat metering can happen.

const mt = makeMeteringTransformer(babelCore);
const mt = makeMeteringTransformer();
function transform(src) {
const ss = mt.rewrite({ src, endowments: { getMeter } });
// const newRegExp = ss.endowments.RegExp; // === re2
Expand Down
45 changes: 0 additions & 45 deletions packages/transform-metering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,9 @@ This technique is not airtight, but it is at least is a best approximation in th

## Quickstart

### SES 1.0

The following example is for legacy [SES-1.0](https://github.com/Agoric/SES#readme):

```js
import { SES1TameMeteringShim, SES1ReplaceGlobalMeter } from '@agoric/tame-metering';
import SES1 from 'ses';
import * as babelCore from '@babel/core';
import { makeMeteredEvaluator } from '@agoric/transform-metering';

// Create a new SES instance (root realm).
const sesRealm = SES1.makeSESRootRealm({
shims: [SES1TameMeteringShim],
configurableGlobals: true,
});

const replaceGlobalMeter = SES1ReplaceGlobalMeter(sesRealm);

const meteredEval = makeMeteredEvaluator({
// Needed for enabling metering of the global builtins.
replaceGlobalMeter,
// Needed for source transforms that prevent runaways.
babelCore,
// Create an object with an `evaluate(src, endowments, options)` method
makeEvaluator: opts => {
const c = sesRealm.global.Realm.makeCompartment(opts);
// FIXME: Realms bug doesn't propagate global properties.
Object.defineProperties(
c.global,
Object.getOwnPropertyDescriptors(sesRealm.global),
);
return c;
},
// Call a callback when the code inside the meteredEval is done evaluating.
quiesceCallback: cb => setTimeout(cb),
});
```

### SES 2.0

This example is for the [SES 2.0 (beta)](https://github.com/Agoric/SES-beta#readme). Note that it currently has issues that prevents it from working correctly, but YMMV.

```js
import { tameMetering } from '@agoric/tame-metering';
import { lockdown } from 'ses';
import * as babelCore from '@babel/core';
import { makeMeteredEvaluator } from '@agoric/transform-metering';

// Override all the global objects with metered versions.
Expand All @@ -63,8 +20,6 @@ lockdown();
const meteredEval = makeMeteredEvaluator({
// Needed for enabling metering of the global builtins.
replaceGlobalMeter,
// Needed for source transforms that prevent runaways.
babelCore,
// Create an object with an `evaluate(src, endowments)` method
makeEvaluator: opts => {
const c = new Compartment(undefined, undefined, opts);
Expand Down
5 changes: 4 additions & 1 deletion packages/transform-metering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"devDependencies": {
"@agoric/install-ses": "^0.5.13",
"@babel/core": "^7.5.0",
"ava": "^3.12.1",
"esm": "^3.2.5",
"nyc": "^15.1.0",
Expand All @@ -26,6 +25,10 @@
"dependencies": {
"@agoric/nat": "^4.0.0",
"@agoric/tame-metering": "^1.3.9",
"@agoric/babel-parser": "^7.6.4",
"@babel/types": "^7.6.0",
"@babel/generator": "^7.6.0",
"@babel/traverse": "^7.6.0",
"re2": "^1.10.5"
},
"keywords": [],
Expand Down
60 changes: 60 additions & 0 deletions packages/transform-metering/src/pure-babel-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as babelParser from '@agoric/babel-parser';
import traverse, { NodePath } from '@babel/traverse';
import generator from '@babel/generator';
import * as types from '@babel/types';

// Create a file structure to wire into @babel/traverse.
export function makePureFile({ code, ast }) {
const file = {
ast,
opts: {},
code,
};
file.hub = {
file,
getCode: () => file.code,
getScope: () => file.scope,
addHelper() {
throw Error(`Helpers are not supported by the fake file hub.`);
},
buildError(_node, msg, Error = TypeError) {
return new Error(msg);
},
};
file.path = NodePath.get({
hub: file.hub,
parentPath: null,
parent: file.ast,
container: file.ast,
key: 'program',
}).setContext();
file.scope = file.path.scope;
return file;
}

/**
* Create a working subset of babelCore that doesn't touch the filesystem.
*/
export function makePureBabelCore() {
return {
parseSync(source, { parserOpts = undefined } = {}) {
return babelParser.parse(source, parserOpts);
},
transformFromAstSync(ast, code = undefined, transformOptions = undefined) {
const { generatorOpts, plugins } = transformOptions || {};

const pluginArgs = [{ types }];
const visitors = plugins
.map(p => p(...pluginArgs).visitor)
.filter(v => v);
if (!visitors.length) {
// We fail here because a null transform is unnecessary.
throw Error(`No visitors found in plugins`);
}
const visitor = traverse.visitors.merge(visitors);
const file = makePureFile({ ast, code });
traverse(file.ast, visitor);
return generator(ast, generatorOpts, code);
},
};
}
4 changes: 2 additions & 2 deletions packages/transform-metering/src/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global require */
import * as c from './constants';
import { makePureBabelCore } from './pure-babel-core';

// We'd like to import this, but RE2 is cjs
const RE2 = require('re2');
Expand All @@ -8,7 +9,7 @@ const METER_GENERATED = Symbol('meter-generated');
const getMeterId = 'getMeter';

export function makeMeteringTransformer(
babelCore,
babelCore = makePureBabelCore(),
{
overrideParser = undefined,
overrideRegExp = RE2,
Expand Down Expand Up @@ -192,7 +193,6 @@ const ${reid}=RegExp(${JSON.stringify(pattern)},${JSON.stringify(flags)});`);
compact: false,
},
plugins: [meteringPlugin(regexpList)],
ast: true,
code: true,
});

Expand Down
3 changes: 1 addition & 2 deletions packages/transform-metering/test/test-transform.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* global __dirname */
/* eslint-disable no-await-in-loop */
import test from 'ava';
import * as babelCore from '@babel/core';
import fs from 'fs';

import { makeMeteringTransformer } from '../src/index';
import * as c from '../src/constants';

test('meter transform', async t => {
let getMeter;
const meteringTransform = makeMeteringTransformer(babelCore, {
const meteringTransform = makeMeteringTransformer(undefined, {
overrideMeterId: '$m',
overrideRegExpIdPrefix: '$re_',
});
Expand Down
4 changes: 1 addition & 3 deletions packages/transform-metering/test/test-zzz-eval.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* global process setTimeout */
// eslint-disable-next-line import/order
import { replaceGlobalMeter } from './install-metering';
import '@agoric/install-ses'; // calls lockdown()

// eslint-disable-next-line import/order
import test from 'ava';
import * as babelCore from '@babel/core';

import { makeMeter, makeMeteredEvaluator } from '../src/index';

Expand Down Expand Up @@ -42,7 +41,6 @@ test.skip('metering evaluator', async t => {
refiller();
}
},
babelCore,
makeEvaluator,
quiesceCallback: cb => setTimeout(cb),
});
Expand Down

0 comments on commit af564f1

Please sign in to comment.