Merge pull request #354 from ethereum/move-imports

Move import processing from the compiler
pull/1/head
Alex Beregszaszi 8 years ago committed by GitHub
commit b3831e8bc4
  1. 29
      src/app.js
  2. 79
      src/app/compiler.js

@ -424,8 +424,35 @@ var run = function () {
cb(err || 'Unknown transport error') cb(err || 'Unknown transport error')
}) })
} }
// FIXME: at some point we should invalidate the cache
var cachedRemoteFiles = {}
function handleImportCall (url, cb) {
var githubMatch
if (editor.hasFile(url)) {
cb(null, editor.getFile(url))
} else if (url in cachedRemoteFiles) {
cb(null, cachedRemoteFiles[url])
} else if ((githubMatch = /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/.exec(url))) {
handleGithubCall(githubMatch[3], githubMatch[4], function (err, content) {
if (err) {
cb('Unable to import "' + url + '": ' + err)
return
}
cachedRemoteFiles[url] = content
cb(null, content)
})
} else if (/^[^:]*:\/\//.exec(url)) {
cb('Unable to import "' + url + '": Unsupported URL')
} else {
cb('Unable to import "' + url + '": File not found')
}
}
var executionContext = new ExecutionContext() var executionContext = new ExecutionContext()
var compiler = new Compiler(editor, handleGithubCall) var compiler = new Compiler(editor, handleImportCall)
var formalVerification = new FormalVerification($('#verificationView'), compiler.event) var formalVerification = new FormalVerification($('#verificationView'), compiler.event)
var offsetToLineColumnConverter = new OffsetToLineColumnConverter(compiler.event) var offsetToLineColumnConverter = new OffsetToLineColumnConverter(compiler.event)

@ -11,14 +11,13 @@ var EventManager = require('../lib/eventManager')
/* /*
trigger compilationFinished, compilerLoaded, compilationStarted trigger compilationFinished, compilerLoaded, compilationStarted
*/ */
function Compiler (editor, handleGithubCall) { function Compiler (editor, handleImportCall) {
var self = this var self = this
this.event = new EventManager() this.event = new EventManager()
var compileJSON var compileJSON
var compilerAcceptsMultipleFiles var compilerAcceptsMultipleFiles
var cachedRemoteFiles = {}
var worker = null var worker = null
var currentVersion var currentVersion
@ -219,61 +218,45 @@ function Compiler (editor, handleGithubCall) {
cb(null, files[editor.getCacheFile()]) cb(null, files[editor.getCacheFile()])
return return
} }
// FIXME: This will only match imports if the file begins with one. // FIXME: This will only match imports if the file begins with one.
// It should tokenize by lines and check each. // It should tokenize by lines and check each.
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
var importRegex = /^\s*import\s*[\'\"]([^\'\"]+)[\'\"];/g var importRegex = /^\s*import\s*[\'\"]([^\'\"]+)[\'\"];/g
var reloop = false
var githubMatch
do {
reloop = false
for (var fileName in files) {
var match
while ((match = importRegex.exec(files[fileName]))) {
var importFilePath = match[1]
if (importFilePath.startsWith('./')) {
importFilePath = importFilePath.slice(2)
}
// FIXME: should be using includes or sets, but there's also browser compatibility.. for (var fileName in files) {
if (importHints.indexOf(importFilePath) === -1) { var match
importHints.push(importFilePath) while ((match = importRegex.exec(files[fileName]))) {
} var importFilePath = match[1]
if (importFilePath.startsWith('./')) {
importFilePath = importFilePath.slice(2)
} }
}
while (importHints.length > 0) { // FIXME: should be using includes or sets, but there's also browser compatibility..
var m = importHints.pop() if (importHints.indexOf(importFilePath) === -1) {
if (m in files) { importHints.push(importFilePath)
continue
} }
if (editor.hasFile(m)) { }
files[m] = editor.getFile(m) }
reloop = true
} else if (m in cachedRemoteFiles) { while (importHints.length > 0) {
files[m] = cachedRemoteFiles[m] var m = importHints.pop()
reloop = true if (m in files) {
} else if ((githubMatch = /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/.exec(m))) { continue
handleGithubCall(githubMatch[3], githubMatch[4], function (err, content) { }
if (err) {
cb('Unable to import "' + m + '": ' + err) handleImportCall(m, function (err, content) {
return if (err) {
} cb(err)
cachedRemoteFiles[m] = content
files[m] = content
gatherImports(files, importHints, cb)
})
return
} else if (/^[^:]*:\/\//.exec(m)) {
cb('Unable to import "' + m + '": Unsupported URL')
return
} else { } else {
cb('Unable to import "' + m + '": File not found') files[m] = content
return gatherImports(files, importHints, cb)
} }
} })
} while (reloop)
return
}
cb(null, { 'sources': files }) cb(null, { 'sources': files })
} }

Loading…
Cancel
Save