From ead88b6e0d03e18c4bb76d16bc18b9e22e6e6065 Mon Sep 17 00:00:00 2001 From: caub Date: Sun, 6 Sep 2020 22:04:18 +0200 Subject: [PATCH] fix engine262 output (inspected twice) --- .../js-eval/__tests__/jsEvalPlugin-test.js | 4 +- src/plugins/js-eval/run.js | 176 +++++++++--------- 2 files changed, 94 insertions(+), 86 deletions(-) diff --git a/src/plugins/js-eval/__tests__/jsEvalPlugin-test.js b/src/plugins/js-eval/__tests__/jsEvalPlugin-test.js index ef3ddbc..4263db9 100644 --- a/src/plugins/js-eval/__tests__/jsEvalPlugin-test.js +++ b/src/plugins/js-eval/__tests__/jsEvalPlugin-test.js @@ -130,12 +130,12 @@ describe('jsEvalPlugin', () => { describe('engine262', () => { it('works', async () => { const output = await testEval('e> ({foo: 1})?.foo ?? 2'); - expect(output).toEqual(`(okay) '1'`); + expect(output).toEqual(`(okay) 1`); }); it(`adds print() global util, because there's no console`, async () => { const output = await testEval('e> print(0b1); print(2n); Math.PI|0'); - expect(output).toEqual(`(okay) 1\n2n\n'3'`); + expect(output).toEqual(`(okay) 1\n2n\n3`); }); it(`errors when it should`, async () => { diff --git a/src/plugins/js-eval/run.js b/src/plugins/js-eval/run.js index 1344112..f0a1058 100644 --- a/src/plugins/js-eval/run.js +++ b/src/plugins/js-eval/run.js @@ -21,97 +21,105 @@ const inspect = (val) => { }; const run = async (code, environment, timeout) => { - if (environment === 'node-cjs') { - const script = new Script(code); - global.module = module; - global.require = require; - global.exports = exports; - global.__dirname = __dirname; - global.__filename = __filename; - for (const name of builtinModules) { - const setReal = (val) => { - delete global[name]; - global[name] = val; - }; - Object.defineProperty(global, name, { - get: () => { - const lib = require(name); // eslint-disable-line + switch (environment) { + case 'node-cjs': { + const script = new Script(code); + global.module = module; + global.require = require; + global.exports = exports; + global.__dirname = __dirname; + global.__filename = __filename; + for (const name of builtinModules) { + const setReal = (val) => { delete global[name]; - Object.defineProperty(global, name, { - get: () => lib, - set: setReal, - configurable: true, - enumerable: false, - }); - return lib; - }, - set: setReal, - configurable: true, - enumerable: false, + global[name] = val; + }; + Object.defineProperty(global, name, { + get: () => { + const lib = require(name); // eslint-disable-line + delete global[name]; + Object.defineProperty(global, name, { + get: () => lib, + set: setReal, + configurable: true, + enumerable: false, + }); + return lib; + }, + set: setReal, + configurable: true, + enumerable: false, + }); + } + const result = await script.runInThisContext({ + timeout, + displayErrors: true, }); + return inspect(result); } - return script.runInThisContext({ - timeout, - displayErrors: true, - }); - } - if (environment === 'module') { - const module = new SourceTextModule(code, { - context: createContext(Object.create(null)), - }); - await module.link(async () => { - throw new Error('Unable to resolve import'); - }); - module.instantiate(); - const { result } = await module.evaluate({ timeout }); - return result; - } - if (environment === 'script') { - const script = new Script(code, { - displayErrors: true, - }); - return script.runInContext(createContext(Object.create(null)), { - timeout, - displayErrors: true, - }); - } - if (environment === 'engine262') { - const { - Agent, - ManagedRealm, - Value, - CreateDataProperty, - FEATURES, - setSurroundingAgent, - inspect: _inspect, - } = require('engine262'); - const agent = new Agent({ - features: FEATURES.map((o) => o.name), - }); - setSurroundingAgent(agent); + case 'module': { + const module = new SourceTextModule(code, { + context: createContext(Object.create(null)), + }); + await module.link(async () => { + throw new Error('Unable to resolve import'); + }); + module.instantiate(); + const { result } = await module.evaluate({ timeout }); + return inspect(result); + } - const realm = new ManagedRealm(); + case 'script': { + const script = new Script(code, { + displayErrors: true, + }); + const result = await script.runInContext(createContext(Object.create(null)), { + timeout, + displayErrors: true, + }); + return inspect(result); + } - return new Promise((resolve, reject) => { - realm.scope(() => { - const print = new Value((args) => { - console.log(...args.map((tmp) => _inspect(tmp))); - return Value.undefined; - }); - CreateDataProperty(realm.GlobalObject, new Value('print'), print); + case 'engine262': { + const { + Agent, + ManagedRealm, + Value, + CreateDataProperty, + FEATURES, + setSurroundingAgent, + inspect: _inspect, + } = require('engine262'); - const completion = realm.evaluateScript(code); - if (completion.Type === 'throw') { - reject(_inspect(completion.Value)); - } else { - resolve(_inspect(completion.Value)); - } + const agent = new Agent({ + features: FEATURES.map((o) => o.name), }); - }); - } + setSurroundingAgent(agent); + + const realm = new ManagedRealm(); - throw new RangeError(`Invalid environment: ${environment}`); + return new Promise((resolve, reject) => { + realm.scope(() => { + const print = new Value((args) => { + console.log(...args.map((tmp) => _inspect(tmp))); + return Value.undefined; + }); + CreateDataProperty(realm.GlobalObject, new Value('print'), print); + + const completion = realm.evaluateScript(code); + if (completion.Type === 'throw') { + reject(_inspect(completion.Value)); + } else { + resolve(_inspect(completion.Value)); + } + }); + }); + } + + default: + throw new RangeError(`Invalid environment: ${environment}`); + } }; if (!module.parent) { @@ -125,13 +133,13 @@ if (!module.parent) { } } try { - const result = await run( + const output = await run( code, process.env.JSEVAL_ENV, Number.parseInt(process.env.JSEVAL_TIMEOUT, 10) || undefined, ); process.stdout.write('⬊ '); - process.stdout.write(inspect(result)); + process.stdout.write(output); } catch (error) { process.stdout.write( error instanceof Error ? `${error.name}: ${error.message}` : `${error}`,