From 289ef7315716b862f4bac82abd29f104b569dff2 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 16 Aug 2017 18:25:35 +0200 Subject: [PATCH] inject web3 to the terminal --- package.json | 1 + src/app.js | 32 +++++++++++++++++++++----------- src/app/panels/editor-panel.js | 8 +++++++- src/app/panels/terminal.js | 20 +++++++++++++++++--- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index ccc91dea0d..7cd736f239 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "swarmgw": "^0.2.0", "tape": "^4.5.1", "uglify-js": "^2.8.16", + "vm-browserify": "0.0.4", "watchify": "^3.9.0", "web3": "^0.18.0", "webworkify": "^1.2.1", diff --git a/src/app.js b/src/app.js index 7cd3b2ea3c..3e6ef7cd97 100644 --- a/src/app.js +++ b/src/app.js @@ -103,13 +103,6 @@ class App { } } } - // ----------------- editor ---------------------------- - self._components.editor = new Editor({}) // @TODO: put into editorpanel - // ----------------- editor panel ---------------------- - self._components.editorpanel = new EditorPanel({ - api: { editor: self._components.editor, config: self._api.config } - }) - self._components.editorpanel.event.register('resize', direction => self._adjustLayout(direction)) } _adjustLayout (direction, delta) { var self = this @@ -147,7 +140,7 @@ class App { ` self._view.centerpanel = yo`
- ${self._components.editorpanel.render()} + ${''}
` self._view.rightpanel = yo` @@ -173,6 +166,26 @@ module.exports = App function run () { var self = this + // ------------------------------------------------------------ + var executionContext = new ExecutionContext() + + // ----------------- editor ---------------------------- + this._components.editor = new Editor({}) // @TODO: put into editorpanel + // ----------------- editor panel ---------------------- + this._components.editorpanel = new EditorPanel({ + api: { + editor: self._components.editor, + config: self._api.config, + web3: () => { + return executionContext.web3() + }, + context: () => { + return executionContext.getProvider() + }} + }) + this._components.editorpanel.event.register('resize', direction => self._adjustLayout(direction)) + + this._view.centerpanel.appendChild(this._components.editorpanel.render()) var queryParams = new QueryParams() var gistHandler = new GistHandler() @@ -501,9 +514,6 @@ function run () { } var renderer = new Renderer(rendererAPI) - // ------------------------------------------------------------ - var executionContext = new ExecutionContext() - // ----------------- UniversalDApp ----------------- var transactionContextAPI = { getAddress: (cb) => { diff --git a/src/app/panels/editor-panel.js b/src/app/panels/editor-panel.js index 59764cb2c6..a8bf6f5fdf 100644 --- a/src/app/panels/editor-panel.js +++ b/src/app/panels/editor-panel.js @@ -125,7 +125,7 @@ var css = csjs` class EditorPanel { constructor (opts = {}) { var self = this - self._api = { config: opts.api.config } + self._api = opts.api self.event = new EventManager() self.data = { _FILE_SCROLL_DELTA: 200, @@ -148,6 +148,12 @@ class EditorPanel { var newpos = (event.pageY < limitUp) ? limitUp : event.pageY newpos = (newpos < height - limitDown) ? newpos : height - limitDown return newpos + }, + web3 () { + return self._api.web3() + }, + context () { + return self._api.context() } } }) diff --git a/src/app/panels/terminal.js b/src/app/panels/terminal.js index 41e6bfc745..f19fb227bc 100644 --- a/src/app/panels/terminal.js +++ b/src/app/panels/terminal.js @@ -4,7 +4,9 @@ var csjs = require('csjs-inject') var javascriptserialize = require('javascript-serialize') var jsbeautify = require('js-beautify') var type = require('component-type') +var vm = require('vm') var EventManager = require('ethereum-remix').lib.EventManager +var Web3 = require('web3') var css = csjs` .panel { @@ -196,6 +198,8 @@ class Terminal { self._templates.default = self._blocksRenderer('default') self._templates.error = self._blocksRenderer('error') self._templates.info = self._blocksRenderer('info') + self._jsSandboxContext = {} + self._jsSandbox = vm.createContext(self._jsSandboxContext) if (opts.shell) self._shell = opts.shell register(self) } @@ -395,10 +399,10 @@ class Terminal { }) } _shell (input, done) { // default shell - // @TODO: add environment and proxy console.log to self.log - // make `web3` object available in the console vm try { - var result = eval(input) // eslint-disable-line + var context = vm.createContext(Object.assign(this._jsSandboxContext, domTerminalFeatures(this))) + var result = vm.runInContext(input, context) + this._jsSandboxContext = Object.assign({}, context) done(null, result) } catch (error) { done(error.message) @@ -406,4 +410,14 @@ class Terminal { } } +// @TODO add all the `console` functions +function domTerminalFeatures (self) { + return { + web3: self._api.context() !== 'vm' ? new Web3(self._api.web3().currentProvider) : null, + console: { + log: function () { self.log.apply(self, arguments) } + } + } +} + module.exports = Terminal