mirror of https://github.com/embarklabs/embark.git
Merge pull request #744 from embark-framework/bugfix/buffer-contract
Isolate the code runner
This commit is contained in:
commit
052ffb72f9
|
@ -44,6 +44,10 @@ class REPL {
|
||||||
writer: this.enhancedWriter.bind(this)
|
writer: this.enhancedWriter.bind(this)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.events.request("runcode:getContext", (context) => {
|
||||||
|
this.replServer.context = context;
|
||||||
|
});
|
||||||
|
|
||||||
this.replServer.on("exit", () => {
|
this.replServer.on("exit", () => {
|
||||||
process.exit();
|
process.exit();
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,13 +8,12 @@ class CodeRunner {
|
||||||
this.events = options.events;
|
this.events = options.events;
|
||||||
this.ipc = options.ipc;
|
this.ipc = options.ipc;
|
||||||
this.commands = [];
|
this.commands = [];
|
||||||
|
this.runCode = new RunCode();
|
||||||
let self = this;
|
let self = this;
|
||||||
// necessary to init the context
|
|
||||||
RunCode.initContext();
|
|
||||||
|
|
||||||
if (this.ipc.isServer()) {
|
if (this.ipc.isServer()) {
|
||||||
this.ipc.on('runcode:getCommands', (_err, callback) => {
|
this.ipc.on('runcode:getCommands', (_err, callback) => {
|
||||||
let result = {web3Config: RunCode.getWeb3Config(), commands: self.commands};
|
let result = {web3Config: self.runCode.getWeb3Config(), commands: self.commands};
|
||||||
callback(null, result);
|
callback(null, result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -34,7 +33,11 @@ class CodeRunner {
|
||||||
self.commands.push({varName, code});
|
self.commands.push({varName, code});
|
||||||
self.ipc.broadcast("runcode:newCommand", {varName, code});
|
self.ipc.broadcast("runcode:newCommand", {varName, code});
|
||||||
}
|
}
|
||||||
RunCode.registerVar(varName, code);
|
self.runCode.registerVar(varName, code);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.events.setCommandHandler('runcode:getContext', (cb) => {
|
||||||
|
cb(self.runCode.context);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.events.setCommandHandler('runcode:eval', (code, cb, forConsoleOnly = false) => {
|
this.events.setCommandHandler('runcode:eval', (code, cb, forConsoleOnly = false) => {
|
||||||
|
@ -53,14 +56,17 @@ class CodeRunner {
|
||||||
code = `(async function() {${code}})();`;
|
code = `(async function() {${code}})();`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let result = RunCode.doEval(code);
|
let result = self.runCode.doEval(code);
|
||||||
if (result instanceof Promise) {
|
|
||||||
return result.then((value) => cb(null, value)).catch(cb);
|
|
||||||
}
|
|
||||||
if (forConsoleOnly && self.ipc.isServer()) {
|
if (forConsoleOnly && self.ipc.isServer()) {
|
||||||
self.commands.push({code});
|
self.commands.push({code});
|
||||||
self.ipc.broadcast("runcode:newCommand", {code});
|
self.ipc.broadcast("runcode:newCommand", {code});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
return result.then((value) => cb(null, value)).catch(cb);
|
||||||
|
}
|
||||||
|
|
||||||
cb(null, result);
|
cb(null, result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,27 @@
|
||||||
/*eslint no-unused-vars: off*/
|
const vm = require('vm');
|
||||||
let __mainContext = this;
|
|
||||||
|
|
||||||
function initContext() {
|
class RunCode {
|
||||||
doEval("__mainContext = this");
|
constructor() {
|
||||||
|
this.context = Object.assign({}, global.this);
|
||||||
|
}
|
||||||
|
|
||||||
|
doEval(code) {
|
||||||
|
return vm.runInNewContext(code, this.context);
|
||||||
|
}
|
||||||
|
|
||||||
|
registerVar(varName, code) {
|
||||||
|
// TODO: Update all the code being dependent of web3
|
||||||
|
// To identify, look at the top of the file for something like:
|
||||||
|
// /*global web3*/
|
||||||
|
if (varName === 'web3') {
|
||||||
|
global.web3 = code;
|
||||||
|
}
|
||||||
|
this.context[varName] = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
getWeb3Config() {
|
||||||
|
return {defaultAccount: this.context.web3.eth.defaultAccount, provider: this.context.web3.currentProvider};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================
|
module.exports = RunCode;
|
||||||
// the eval is used for evaluating some of the contact calls for different purposes
|
|
||||||
// this should be at least moved to a different process and scope
|
|
||||||
// for now it is defined here
|
|
||||||
// ======================
|
|
||||||
function doEval(code) {
|
|
||||||
// TODO: add trace log here
|
|
||||||
return eval(code);
|
|
||||||
}
|
|
||||||
|
|
||||||
function registerVar(varName, code) {
|
|
||||||
__mainContext[varName] = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getWeb3Config() {
|
|
||||||
return {defaultAccount:__mainContext.web3.eth.defaultAccount, provider: __mainContext.web3.currentProvider};
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
doEval,
|
|
||||||
registerVar,
|
|
||||||
initContext,
|
|
||||||
getWeb3Config
|
|
||||||
};
|
|
||||||
|
|
|
@ -18,9 +18,7 @@ config({
|
||||||
"onDeploy": [
|
"onDeploy": [
|
||||||
`ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send().then(() => {
|
`ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send().then(() => {
|
||||||
ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send();
|
ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send();
|
||||||
Resolver.methods.setAddr('${rootNode}', '${address}').send().then(() => {
|
Resolver.methods.setAddr('${rootNode}', '${address}').send();
|
||||||
global.ensTestReady = true;
|
|
||||||
});
|
|
||||||
});`
|
});`
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -28,21 +26,18 @@ config({
|
||||||
});
|
});
|
||||||
|
|
||||||
contract("ENS", function () {
|
contract("ENS", function () {
|
||||||
this.timeout(1000);
|
it("should have registered embark.eth", function () {
|
||||||
|
let maxRetry = 20;
|
||||||
|
let domainAddress;
|
||||||
|
|
||||||
before(function (done) {
|
const wait = setInterval(async () => {
|
||||||
// Wait for onDeploy to finish
|
domainAddress = await Resolver.methods.addr(rootNode).call();
|
||||||
const wait = setInterval(() => {
|
if (domainAddress || maxRetry === 0) {
|
||||||
if (!global.ensTestReady) {
|
clearInterval(wait);
|
||||||
|
assert.strictEqual(domainAddress, address);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
clearInterval(wait);
|
maxRetry--;
|
||||||
done();
|
|
||||||
}, 50);
|
}, 50);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should have registered embark.eth", async function () {
|
|
||||||
const domainAddress = await Resolver.methods.addr(rootNode).call();
|
|
||||||
assert.strictEqual(domainAddress, address);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue