Skip to content

Commit

Permalink
Merge pull request #48 from caub/fix-engine262-output
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand authored Sep 7, 2020
2 parents 5c3fffb + ead88b6 commit f36ac62
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/plugins/js-eval/__tests__/jsEvalPlugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
176 changes: 92 additions & 84 deletions src/plugins/js-eval/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`,
Expand Down

0 comments on commit f36ac62

Please sign in to comment.