Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created November 29, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/046f03be832d00f40d940ffc790a44bb to your computer and use it in GitHub Desktop.
Save devsnek/046f03be832d00f40d940ffc790a44bb to your computer and use it in GitHub Desktop.
'use strict';
const {
initializeAgent,
inspect,
Realm,
Value,
Descriptor,
Abstract,
Completion,
AbruptCompletion,
} = require('.');
function X(val) {
if (val instanceof AbruptCompletion) {
throw new Error('should not be abrupt');
}
if (val instanceof Completion) {
return val.Value;
}
return val;
}
module.exports = (Agent) => {
class Engine262Agent extends Agent {
constructor(options) {
super(options);
this.name = 'engine262';
initializeAgent();
this.stdout = '';
this.realm = undefined;
}
async initialize() {
const realm = new Realm();
this.realm = realm;
X(Abstract.DefinePropertyOrThrow(realm.global, new Value(realm, 'print'), Descriptor({
Value: new Value(realm, ([value]) => {
this.stdout += `${inspect(value, realm)}\n`;
return new Value(realm, undefined);
}),
})));
return this;
}
async evalScript(source) {
try {
const result = this.realm.evaluateScript(source);
if (result instanceof AbruptCompletion) {
return {
stdout: '',
stderr: inspect(result, this.realm),
error: {
name: inspect(Abstract.Get(result.Value, new Value(this.realm, 'name'))),
message: inspect(Abstract.Get(result.Value, new Value(this.realm, 'message'))),
},
};
} else {
return { stdout: this.stdout, stderr: '', error: null };
}
} catch (error) {
return { stdout: '', stderr: '', error };
}
}
stop() {
return super.stop();
}
destroy() {
this.realm = undefined;
this.stdout = '';
return super.destroy();
}
}
return Engine262Agent;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment