Merge pull request #695 from ethereum/iframesandbox

Inject web3 to the terminal
pull/1/head
yann300 7 years ago committed by GitHub
commit fa34ec91f8
  1. 1
      package.json
  2. 32
      src/app.js
  3. 8
      src/app/panels/editor-panel.js
  4. 20
      src/app/panels/terminal.js

@ -46,6 +46,7 @@
"swarmgw": "^0.2.0", "swarmgw": "^0.2.0",
"tape": "^4.5.1", "tape": "^4.5.1",
"uglify-js": "^2.8.16", "uglify-js": "^2.8.16",
"vm-browserify": "0.0.4",
"watchify": "^3.9.0", "watchify": "^3.9.0",
"web3": "^0.18.0", "web3": "^0.18.0",
"webworkify": "^1.2.1", "webworkify": "^1.2.1",

@ -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) { _adjustLayout (direction, delta) {
var self = this var self = this
@ -147,7 +140,7 @@ class App {
` `
self._view.centerpanel = yo` self._view.centerpanel = yo`
<div id="editor-container" class=${css.centerpanel}> <div id="editor-container" class=${css.centerpanel}>
${self._components.editorpanel.render()} ${''}
</div> </div>
` `
self._view.rightpanel = yo` self._view.rightpanel = yo`
@ -173,6 +166,26 @@ module.exports = App
function run () { function run () {
var self = this 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 queryParams = new QueryParams()
var gistHandler = new GistHandler() var gistHandler = new GistHandler()
@ -501,9 +514,6 @@ function run () {
} }
var renderer = new Renderer(rendererAPI) var renderer = new Renderer(rendererAPI)
// ------------------------------------------------------------
var executionContext = new ExecutionContext()
// ----------------- UniversalDApp ----------------- // ----------------- UniversalDApp -----------------
var transactionContextAPI = { var transactionContextAPI = {
getAddress: (cb) => { getAddress: (cb) => {

@ -125,7 +125,7 @@ var css = csjs`
class EditorPanel { class EditorPanel {
constructor (opts = {}) { constructor (opts = {}) {
var self = this var self = this
self._api = { config: opts.api.config } self._api = opts.api
self.event = new EventManager() self.event = new EventManager()
self.data = { self.data = {
_FILE_SCROLL_DELTA: 200, _FILE_SCROLL_DELTA: 200,
@ -148,6 +148,12 @@ class EditorPanel {
var newpos = (event.pageY < limitUp) ? limitUp : event.pageY var newpos = (event.pageY < limitUp) ? limitUp : event.pageY
newpos = (newpos < height - limitDown) ? newpos : height - limitDown newpos = (newpos < height - limitDown) ? newpos : height - limitDown
return newpos return newpos
},
web3 () {
return self._api.web3()
},
context () {
return self._api.context()
} }
} }
}) })

@ -4,7 +4,9 @@ var csjs = require('csjs-inject')
var javascriptserialize = require('javascript-serialize') var javascriptserialize = require('javascript-serialize')
var jsbeautify = require('js-beautify') var jsbeautify = require('js-beautify')
var type = require('component-type') var type = require('component-type')
var vm = require('vm')
var EventManager = require('ethereum-remix').lib.EventManager var EventManager = require('ethereum-remix').lib.EventManager
var Web3 = require('web3')
var css = csjs` var css = csjs`
.panel { .panel {
@ -162,6 +164,8 @@ class Terminal {
self._templates.default = self._blocksRenderer('default') self._templates.default = self._blocksRenderer('default')
self._templates.error = self._blocksRenderer('error') self._templates.error = self._blocksRenderer('error')
self._templates.info = self._blocksRenderer('info') self._templates.info = self._blocksRenderer('info')
self._jsSandboxContext = {}
self._jsSandbox = vm.createContext(self._jsSandboxContext)
if (opts.shell) self._shell = opts.shell if (opts.shell) self._shell = opts.shell
register(self) register(self)
} }
@ -371,10 +375,10 @@ class Terminal {
}) })
} }
_shell (input, done) { // default shell _shell (input, done) { // default shell
// @TODO: add environment and proxy console.log to self.log
// make `web3` object available in the console vm
try { 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) done(null, result)
} catch (error) { } catch (error) {
done(error.message) done(error.message)
@ -382,4 +386,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 module.exports = Terminal

Loading…
Cancel
Save