Created
November 29, 2018 17:49
-
-
Save devsnek/046f03be832d00f40d940ffc790a44bb to your computer and use it in GitHub Desktop.
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
'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