summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-15 04:39:03 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-15 04:50:45 -0400
commit5117a8f8a289101723bc74d4e9c45581d5b99172 (patch)
tree99e63aede656212711c40475a797419d639b7acd /main.go
parent03af2c711818d616f8de82a5bc731a8ec2dbc723 (diff)
Compile cache and relative imports working.
Diffstat (limited to 'main.go')
-rw-r--r--main.go98
1 files changed, 73 insertions, 25 deletions
diff --git a/main.go b/main.go
index 9fe6b92dd..daadadb90 100644
--- a/main.go
+++ b/main.go
@@ -5,8 +5,28 @@ import (
"github.com/ry/v8worker2"
"io/ioutil"
"os"
+ "path"
+ "path/filepath"
+ "runtime"
+ "strings"
)
+func HandleCompileOutput(source string, filename string) []byte {
+ // println("compile output from golang", filename)
+ // Remove any ".." elements. This prevents hacking by trying to move up.
+ filename, err := filepath.Rel("/", filename)
+ check(err)
+ if strings.Contains(filename, "..") {
+ panic("Assertion error.")
+ }
+ filename = path.Join(CompileDir, filename)
+ err = os.MkdirAll(path.Dir(filename), 0700)
+ check(err)
+ err = ioutil.WriteFile(filename, []byte(source), 0600)
+ check(err)
+ return nil
+}
+
func ReadFileSync(filename string) []byte {
buf, err := ioutil.ReadFile(filename)
msg := &Msg{Kind: Msg_DATA_RESPONSE}
@@ -16,23 +36,60 @@ func ReadFileSync(filename string) []byte {
msg.Data = buf
}
out, err := proto.Marshal(msg)
- if err != nil {
- panic(err)
- }
+ check(err)
return out
}
+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)
+ }
+}
+
func recv(buf []byte) []byte {
msg := &Msg{}
err := proto.Unmarshal(buf, msg)
- if err != nil {
- panic(err)
- }
+ check(err)
switch msg.Kind {
case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.Path)
case Msg_EXIT:
os.Exit(int(msg.Code))
+ case Msg_COMPILE_OUTPUT:
+ payload := msg.GetCompileOutput()
+ return HandleCompileOutput(payload.Source, payload.Filename)
default:
panic("Unexpected message")
}
@@ -40,33 +97,24 @@ func recv(buf []byte) []byte {
return nil
}
-func loadAsset(w *v8worker2.Worker, path string) {
- data, err := Asset(path)
- if err != nil {
- panic("asset not found")
- }
- err = w.Load(path, string(data))
- if err != nil {
- panic(err)
- }
-}
-
func main() {
args := v8worker2.SetFlags(os.Args)
+ createDirs()
worker := v8worker2.New(recv)
loadAsset(worker, "dist/main.js")
cwd, err := os.Getwd()
- if err != nil {
- panic(err)
- }
+ check(err)
+
out, err := proto.Marshal(&Msg{
Kind: Msg_START,
- Cwd: cwd,
- Argv: args,
+ Payload: &Msg_Start{
+ Start: &StartMsg{
+ Cwd: cwd,
+ Argv: args,
+ },
+ },
})
- if err != nil {
- panic(err)
- }
+ check(err)
err = worker.SendBytes(out)
if err != nil {
os.Stderr.WriteString(err.Error())