This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathcompiler-worker.ts
52 lines (49 loc) · 1.77 KB
/
compiler-worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict'
import solc from 'solc/wrapper'
import { CompilerInput, MessageToWorker } from './types'
var compileJSON: ((input: CompilerInput) => string) | null = (input) => { return '' }
var missingInputs: string[] = []
// 'DedicatedWorkerGlobalScope' object (the Worker global scope) is accessible through the self keyword
// 'dom' and 'webworker' library files can't be included together https://github.com/microsoft/TypeScript/issues/20595
export default (self) => {
self.addEventListener('message', (e) => {
const data: MessageToWorker = e.data
switch (data.cmd) {
case 'loadVersion':
delete self.Module
// NOTE: workaround some browsers?
self.Module = undefined
compileJSON = null
//importScripts() method of synchronously imports one or more scripts into the worker's scope
self.importScripts(data.data)
let compiler: solc = solc(self.Module)
compileJSON = (input) => {
try {
let missingInputsCallback = (path) => {
missingInputs.push(path)
return { 'error': 'Deferred import' }
}
return compiler.compile(input, { import: missingInputsCallback })
} catch (exception) {
return JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception })
}
}
self.postMessage({
cmd: 'versionLoaded',
data: compiler.version()
})
break
case 'compile':
missingInputs.length = 0
if(data.input && compileJSON) {
self.postMessage({
cmd: 'compiled',
job: data.job,
data: compileJSON(data.input),
missingInputs: missingInputs
})
}
break
}
}, false)
}