2018-05-13 23:32:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-17 09:47:09 -04:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2018-05-14 00:31:48 -04:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-05-13 23:32:01 -04:00
|
|
|
"github.com/ry/v8worker2"
|
2018-05-14 02:50:55 -04:00
|
|
|
"io/ioutil"
|
2018-05-14 00:31:48 -04:00
|
|
|
"os"
|
2018-05-15 04:39:03 -04:00
|
|
|
"path"
|
|
|
|
"runtime"
|
2018-05-18 11:49:28 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-05-13 23:32:01 -04:00
|
|
|
)
|
|
|
|
|
2018-05-18 11:49:28 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var resChan chan *Msg
|
|
|
|
|
2018-05-17 09:47:09 -04:00
|
|
|
func SourceCodeHash(filename string, sourceCodeBuf []byte) string {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(filename))
|
|
|
|
h.Write(sourceCodeBuf)
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:53:39 -04:00
|
|
|
func CacheFileName(filename string, sourceCodeBuf []byte) string {
|
|
|
|
cacheKey := SourceCodeHash(filename, sourceCodeBuf)
|
|
|
|
return path.Join(CompileDir, cacheKey+".js")
|
|
|
|
}
|
|
|
|
|
2018-05-17 09:47:09 -04:00
|
|
|
func HandleSourceCodeFetch(filename string) []byte {
|
2018-05-17 21:02:06 -04:00
|
|
|
res := &Msg{}
|
2018-05-17 09:47:09 -04:00
|
|
|
sourceCodeBuf, err := Asset("dist/" + filename)
|
|
|
|
if err != nil {
|
|
|
|
sourceCodeBuf, err = ioutil.ReadFile(filename)
|
2018-05-15 04:39:03 -04:00
|
|
|
}
|
2018-05-17 09:47:09 -04:00
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
} else {
|
2018-05-17 20:53:39 -04:00
|
|
|
cacheFn := CacheFileName(filename, sourceCodeBuf)
|
|
|
|
outputCodeBuf, err := ioutil.ReadFile(cacheFn)
|
|
|
|
var outputCode string
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
outputCode = ""
|
|
|
|
} else if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
outputCode = string(outputCodeBuf)
|
|
|
|
}
|
|
|
|
|
2018-05-17 09:47:09 -04:00
|
|
|
res.Payload = &Msg_SourceCodeFetchRes{
|
|
|
|
SourceCodeFetchRes: &SourceCodeFetchResMsg{
|
|
|
|
SourceCode: string(sourceCodeBuf),
|
2018-05-17 20:53:39 -04:00
|
|
|
OutputCode: outputCode,
|
2018-05-17 09:47:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(res)
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-17 09:47:09 -04:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:53:39 -04:00
|
|
|
func HandleSourceCodeCache(filename string, sourceCode string,
|
|
|
|
outputCode string) []byte {
|
|
|
|
|
|
|
|
fn := CacheFileName(filename, []byte(sourceCode))
|
|
|
|
outputCodeBuf := []byte(outputCode)
|
|
|
|
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
|
2018-05-17 21:02:06 -04:00
|
|
|
res := &Msg{}
|
2018-05-17 20:53:39 -04:00
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(res)
|
|
|
|
check(err)
|
|
|
|
return out
|
2018-05-15 04:39:03 -04:00
|
|
|
}
|
|
|
|
|
2018-05-18 11:49:28 -04:00
|
|
|
func HandleTimerStart(id int32, interval bool, duration int32) []byte {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(time.Duration(duration) * time.Millisecond)
|
|
|
|
resChan <- &Msg{
|
|
|
|
Payload: &Msg_TimerReady{
|
|
|
|
TimerReady: &TimerReadyMsg{
|
|
|
|
Id: id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:39:03 -04:00
|
|
|
func UserHomeDir() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
|
|
|
if home == "" {
|
|
|
|
home = os.Getenv("USERPROFILE")
|
|
|
|
}
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAsset(w *v8worker2.Worker, path string) {
|
|
|
|
data, err := Asset(path)
|
|
|
|
check(err)
|
|
|
|
err = w.Load(path, string(data))
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var DenoDir string
|
|
|
|
var CompileDir string
|
|
|
|
var SrcDir string
|
|
|
|
|
|
|
|
func createDirs() {
|
|
|
|
DenoDir = path.Join(UserHomeDir(), ".deno")
|
|
|
|
CompileDir = path.Join(DenoDir, "compile")
|
|
|
|
err := os.MkdirAll(CompileDir, 0700)
|
|
|
|
check(err)
|
|
|
|
SrcDir = path.Join(DenoDir, "src")
|
|
|
|
err = os.MkdirAll(SrcDir, 0700)
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 02:50:55 -04:00
|
|
|
func recv(buf []byte) []byte {
|
|
|
|
msg := &Msg{}
|
|
|
|
err := proto.Unmarshal(buf, msg)
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-17 21:02:06 -04:00
|
|
|
switch msg.Payload.(type) {
|
|
|
|
case *Msg_Exit:
|
|
|
|
payload := msg.GetExit()
|
|
|
|
os.Exit(int(payload.Code))
|
|
|
|
case *Msg_SourceCodeFetch:
|
2018-05-17 09:47:09 -04:00
|
|
|
payload := msg.GetSourceCodeFetch()
|
|
|
|
return HandleSourceCodeFetch(payload.Filename)
|
2018-05-17 21:02:06 -04:00
|
|
|
case *Msg_SourceCodeCache:
|
2018-05-17 09:47:09 -04:00
|
|
|
payload := msg.GetSourceCodeCache()
|
2018-05-18 11:49:28 -04:00
|
|
|
return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
|
|
|
|
payload.OutputCode)
|
|
|
|
case *Msg_TimerStart:
|
|
|
|
payload := msg.GetTimerStart()
|
|
|
|
return HandleTimerStart(payload.Id, payload.Interval, payload.Duration)
|
2018-05-14 02:50:55 -04:00
|
|
|
default:
|
|
|
|
panic("Unexpected message")
|
|
|
|
}
|
2018-05-14 17:27:34 -04:00
|
|
|
|
|
|
|
return nil
|
2018-05-13 23:32:01 -04:00
|
|
|
}
|
|
|
|
|
2018-05-14 00:31:48 -04:00
|
|
|
func main() {
|
2018-05-14 20:57:49 -04:00
|
|
|
args := v8worker2.SetFlags(os.Args)
|
2018-05-15 04:39:03 -04:00
|
|
|
createDirs()
|
2018-05-13 23:32:01 -04:00
|
|
|
worker := v8worker2.New(recv)
|
2018-05-14 00:31:48 -04:00
|
|
|
loadAsset(worker, "dist/main.js")
|
2018-05-14 13:02:47 -04:00
|
|
|
cwd, err := os.Getwd()
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
|
|
|
|
2018-05-18 11:49:28 -04:00
|
|
|
resChan = make(chan *Msg)
|
|
|
|
doneChan := make(chan bool)
|
|
|
|
|
2018-05-14 13:02:47 -04:00
|
|
|
out, err := proto.Marshal(&Msg{
|
2018-05-15 04:39:03 -04:00
|
|
|
Payload: &Msg_Start{
|
|
|
|
Start: &StartMsg{
|
|
|
|
Cwd: cwd,
|
|
|
|
Argv: args,
|
|
|
|
},
|
|
|
|
},
|
2018-05-14 13:02:47 -04:00
|
|
|
})
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-14 00:31:48 -04:00
|
|
|
err = worker.SendBytes(out)
|
|
|
|
if err != nil {
|
2018-05-14 03:12:36 -04:00
|
|
|
os.Stderr.WriteString(err.Error())
|
|
|
|
os.Exit(1)
|
2018-05-14 00:31:48 -04:00
|
|
|
}
|
2018-05-18 11:49:28 -04:00
|
|
|
|
|
|
|
// In a goroutine, we wait on for all goroutines to complete (for example
|
|
|
|
// timers). We use this to signal to the main thread to exit.
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
doneChan <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-resChan:
|
|
|
|
out, err := proto.Marshal(msg)
|
|
|
|
err = worker.SendBytes(out)
|
|
|
|
check(err)
|
|
|
|
case <-doneChan:
|
|
|
|
// All goroutines have completed. Now we can exit main().
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 23:32:01 -04:00
|
|
|
}
|