0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -05:00
denoland-deno/main.go

67 lines
1.1 KiB
Go
Raw Normal View History

2018-05-13 23:32:01 -04:00
package main
import (
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-13 23:32:01 -04:00
)
2018-05-14 02:50:55 -04:00
func ReadFileSync(filename string) []byte {
buf, err := ioutil.ReadFile(filename)
msg := &Msg{Kind: Msg_DATA_RESPONSE}
if err != nil {
msg.Error = err.Error()
} else {
msg.Data = buf
}
out, err := proto.Marshal(msg)
if err != nil {
panic(err)
}
return out
}
func recv(buf []byte) []byte {
msg := &Msg{}
err := proto.Unmarshal(buf, msg)
if err != nil {
panic(err)
}
switch msg.Kind {
case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.Path)
default:
panic("Unexpected message")
}
2018-05-13 23:32:01 -04:00
}
2018-05-14 00:31:48 -04:00
func loadAsset(w *v8worker2.Worker, path string) {
data, err := Asset(path)
2018-05-13 23:32:01 -04:00
if err != nil {
panic("asset not found")
}
2018-05-14 00:31:48 -04:00
err = w.Load(path, string(data))
if err != nil {
panic(err)
}
}
2018-05-13 23:32:01 -04:00
2018-05-14 00:31:48 -04:00
func main() {
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")
loadMsg := &Msg{
Kind: Msg_LOAD,
Argv: os.Args,
}
out, err := proto.Marshal(loadMsg)
2018-05-13 23:32:01 -04:00
if err != nil {
panic(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-13 23:32:01 -04:00
}