From 51692a582543f0182dc335df3fe89a5a08b293e6 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 21 May 2018 23:00:36 -0400 Subject: Organize modules: timers, os --- Makefile | 3 +- handlers.go | 130 ------------------------------------------------------------ main.go | 4 +- os.go | 99 +++++++++++++++++++++++++++++++++++++++++++++ timers.go | 38 ++++++++++++++++++ 5 files changed, 142 insertions(+), 132 deletions(-) delete mode 100644 handlers.go create mode 100644 os.go create mode 100644 timers.go diff --git a/Makefile b/Makefile index 86a579d66..613930781 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,11 @@ GO_FILES = \ assets.go \ deno_dir.go \ dispatch.go \ - handlers.go \ main.go \ main_test.go \ msg.pb.go \ + os.go \ + timers.go \ util.go deno: $(GO_FILES) diff --git a/handlers.go b/handlers.go deleted file mode 100644 index 237aa822e..000000000 --- a/handlers.go +++ /dev/null @@ -1,130 +0,0 @@ -package main - -import ( - "github.com/golang/protobuf/proto" - "io/ioutil" - "os" - "strings" - "time" -) - -const assetPrefix string = "/$asset$/" - -func InitHandlers() { - Sub("os", func(buf []byte) []byte { - msg := &Msg{} - check(proto.Unmarshal(buf, msg)) - switch msg.Payload.(type) { - case *Msg_Exit: - payload := msg.GetExit() - os.Exit(int(payload.Code)) - case *Msg_SourceCodeFetch: - payload := msg.GetSourceCodeFetch() - return HandleSourceCodeFetch(payload.ModuleSpecifier, payload.ContainingFile) - case *Msg_SourceCodeCache: - payload := msg.GetSourceCodeCache() - return HandleSourceCodeCache(payload.Filename, payload.SourceCode, - payload.OutputCode) - default: - panic("[os] Unexpected message " + string(buf)) - } - return nil - }) - - Sub("timers", func(buf []byte) []byte { - msg := &Msg{} - check(proto.Unmarshal(buf, msg)) - switch msg.Payload.(type) { - case *Msg_TimerStart: - payload := msg.GetTimerStart() - return HandleTimerStart(payload.Id, payload.Interval, payload.Duration) - default: - panic("[timers] Unexpected message " + string(buf)) - } - }) -} - -func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) { - assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty") - res := &Msg{} - var sourceCodeBuf []byte - var err error - - defer func() { - if err != nil { - res.Error = err.Error() - } - out, err = proto.Marshal(res) - check(err) - }() - - moduleName, filename, err := ResolveModule(moduleSpecifier, containingFile) - if err != nil { - return - } - - //println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier, - // "containingFile", containingFile, "filename", filename) - - if isRemote(moduleName) { - sourceCodeBuf, err = FetchRemoteSource(moduleName, filename) - } else if strings.HasPrefix(moduleName, assetPrefix) { - f := strings.TrimPrefix(moduleName, assetPrefix) - sourceCodeBuf, err = Asset("dist/" + f) - } else { - assert(moduleName == filename, - "if a module isn't remote, it should have the same filename") - sourceCodeBuf, err = ioutil.ReadFile(moduleName) - } - if err != nil { - return - } - - outputCode, err := LoadOutputCodeCache(filename, sourceCodeBuf) - if err != nil { - return - } - - res.Payload = &Msg_SourceCodeFetchRes{ - SourceCodeFetchRes: &SourceCodeFetchResMsg{ - ModuleName: moduleName, - Filename: filename, - SourceCode: string(sourceCodeBuf), - OutputCode: outputCode, - }, - } - return -} - -func HandleSourceCodeCache(filename string, sourceCode string, - outputCode string) []byte { - - fn := CacheFileName(filename, []byte(sourceCode)) - outputCodeBuf := []byte(outputCode) - err := ioutil.WriteFile(fn, outputCodeBuf, 0600) - res := &Msg{} - if err != nil { - res.Error = err.Error() - } - out, err := proto.Marshal(res) - check(err) - return out -} - -func HandleTimerStart(id int32, interval bool, duration int32) []byte { - wg.Add(1) - go func() { - defer wg.Done() - time.Sleep(time.Duration(duration) * time.Millisecond) - payload, err := proto.Marshal(&Msg{ - Payload: &Msg_TimerReady{ - TimerReady: &TimerReadyMsg{ - Id: id, - }, - }, - }) - check(err) - Pub("timers", payload) - }() - return nil -} diff --git a/main.go b/main.go index 409a7940d..ebeaf87c1 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,9 @@ func main() { createDirs() createWorker() - InitHandlers() + + InitOS() + InitTimers() main_js := stringAsset("main.js") check(worker.Load("/main.js", main_js)) diff --git a/os.go b/os.go new file mode 100644 index 000000000..214492eea --- /dev/null +++ b/os.go @@ -0,0 +1,99 @@ +package main + +import ( + "github.com/golang/protobuf/proto" + "io/ioutil" + "os" + "strings" +) + +const assetPrefix string = "/$asset$/" + +func InitOS() { + Sub("os", func(buf []byte) []byte { + msg := &Msg{} + check(proto.Unmarshal(buf, msg)) + switch msg.Payload.(type) { + case *Msg_Exit: + payload := msg.GetExit() + os.Exit(int(payload.Code)) + case *Msg_SourceCodeFetch: + payload := msg.GetSourceCodeFetch() + return HandleSourceCodeFetch(payload.ModuleSpecifier, payload.ContainingFile) + case *Msg_SourceCodeCache: + payload := msg.GetSourceCodeCache() + return HandleSourceCodeCache(payload.Filename, payload.SourceCode, + payload.OutputCode) + default: + panic("[os] Unexpected message " + string(buf)) + } + return nil + }) +} + +func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) { + assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty") + res := &Msg{} + var sourceCodeBuf []byte + var err error + + defer func() { + if err != nil { + res.Error = err.Error() + } + out, err = proto.Marshal(res) + check(err) + }() + + moduleName, filename, err := ResolveModule(moduleSpecifier, containingFile) + if err != nil { + return + } + + //println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier, + // "containingFile", containingFile, "filename", filename) + + if isRemote(moduleName) { + sourceCodeBuf, err = FetchRemoteSource(moduleName, filename) + } else if strings.HasPrefix(moduleName, assetPrefix) { + f := strings.TrimPrefix(moduleName, assetPrefix) + sourceCodeBuf, err = Asset("dist/" + f) + } else { + assert(moduleName == filename, + "if a module isn't remote, it should have the same filename") + sourceCodeBuf, err = ioutil.ReadFile(moduleName) + } + if err != nil { + return + } + + outputCode, err := LoadOutputCodeCache(filename, sourceCodeBuf) + if err != nil { + return + } + + res.Payload = &Msg_SourceCodeFetchRes{ + SourceCodeFetchRes: &SourceCodeFetchResMsg{ + ModuleName: moduleName, + Filename: filename, + SourceCode: string(sourceCodeBuf), + OutputCode: outputCode, + }, + } + return +} + +func HandleSourceCodeCache(filename string, sourceCode string, + outputCode string) []byte { + + fn := CacheFileName(filename, []byte(sourceCode)) + outputCodeBuf := []byte(outputCode) + err := ioutil.WriteFile(fn, outputCodeBuf, 0600) + res := &Msg{} + if err != nil { + res.Error = err.Error() + } + out, err := proto.Marshal(res) + check(err) + return out +} diff --git a/timers.go b/timers.go new file mode 100644 index 000000000..3a1efa103 --- /dev/null +++ b/timers.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/golang/protobuf/proto" + "time" +) + +func InitTimers() { + Sub("timers", func(buf []byte) []byte { + msg := &Msg{} + check(proto.Unmarshal(buf, msg)) + switch msg.Payload.(type) { + case *Msg_TimerStart: + payload := msg.GetTimerStart() + return HandleTimerStart(payload.Id, payload.Interval, payload.Duration) + default: + panic("[timers] Unexpected message " + string(buf)) + } + }) +} + +func HandleTimerStart(id int32, interval bool, duration int32) []byte { + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(time.Duration(duration) * time.Millisecond) + payload, err := proto.Marshal(&Msg{ + Payload: &Msg_TimerReady{ + TimerReady: &TimerReadyMsg{ + Id: id, + }, + }, + }) + check(err) + Pub("timers", payload) + }() + return nil +} -- cgit v1.2.3