1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

Use proto3 again.

This commit is contained in:
Ryan Dahl 2018-05-25 15:30:09 -04:00
parent d765300de5
commit 5b858632ac
5 changed files with 56 additions and 58 deletions

View file

@ -27,9 +27,9 @@ func recv(buf []byte) (response []byte) {
msg := &BaseMsg{} msg := &BaseMsg{}
check(proto.Unmarshal(buf, msg)) check(proto.Unmarshal(buf, msg))
assert(len(msg.Payload) > 0, "BaseMsg has empty payload.") assert(len(msg.Payload) > 0, "BaseMsg has empty payload.")
subscribers, ok := channels[*msg.Channel] subscribers, ok := channels[msg.Channel]
if !ok { if !ok {
panic("No subscribers for channel " + *msg.Channel) panic("No subscribers for channel " + msg.Channel)
} }
for i := 0; i < len(subscribers); i++ { for i := 0; i < len(subscribers); i++ {
s := subscribers[i] s := subscribers[i]
@ -52,7 +52,7 @@ func Sub(channel string, cb Subscriber) {
func Pub(channel string, payload []byte) { func Pub(channel string, payload []byte) {
resChan <- &BaseMsg{ resChan <- &BaseMsg{
Channel: &channel, Channel: channel,
Payload: payload, Payload: payload,
} }
} }

10
main.go
View file

@ -60,12 +60,12 @@ func main() {
var command = Msg_START // TODO use proto3 var command = Msg_START // TODO use proto3
PubMsg("start", &Msg{ PubMsg("start", &Msg{
Command: &command, Command: command,
StartCwd: &cwd, StartCwd: cwd,
StartArgv: args, StartArgv: args,
StartDebugFlag: flagDebug, StartDebugFlag: *flagDebug,
StartMainJs: &main_js, StartMainJs: main_js,
StartMainMap: &main_map, StartMainMap: main_map,
}) })
DispatchLoop() DispatchLoop()

View file

@ -1,21 +1,21 @@
syntax = "proto2"; syntax = "proto3";
package main; package main;
message BaseMsg { message BaseMsg {
optional string channel = 1; string channel = 1;
optional bytes payload = 2; bytes payload = 2;
} }
message Msg { message Msg {
optional string error = 1; string error = 1;
enum Command { enum Command {
ERROR = 1; ERROR = 0;
START = 2; START = 1;
SOURCE_CODE_FETCH_RES = 3; SOURCE_CODE_FETCH_RES = 2;
ONEOF = 100; ONEOF = 100;
} }
optional Command command = 2 [ default = ONEOF ]; Command command = 2;
// We avoid creating a message for each command (and use oneof or any types) // We avoid creating a message for each command (and use oneof or any types)
// In order to reduce code in the size of the generated javascript // In order to reduce code in the size of the generated javascript
@ -24,21 +24,20 @@ message Msg {
// arguments by their name. // arguments by their name.
// Start // Start
optional string start_cwd = 10; string start_cwd = 10;
repeated string start_argv = 11; repeated string start_argv = 11;
optional bool start_debug_flag = 12; bool start_debug_flag = 12;
optional string start_main_js = 13; // The contents of dist/main.js string start_main_js = 13; // The contents of dist/main.js
optional string start_main_map = 14; // The contents of dist/main.map string start_main_map = 14; // The contents of dist/main.map
// SOURCE_CODE_FETCH_RES // SOURCE_CODE_FETCH_RES
// If it's a non-http module, moduleName and filename will be the same. // If it's a non-http module, moduleName and filename will be the same.
// For http modules, moduleName is its resolved http URL, and filename // For http modules, moduleName is its resolved http URL, and filename
// is the location of the locally downloaded source code. // is the location of the locally downloaded source code.
optional string source_code_fetch_res_module_name = 30; string source_code_fetch_res_module_name = 30;
optional string source_code_fetch_res_filename = 31; string source_code_fetch_res_filename = 31;
optional string source_code_fetch_res_source_code = 32; string source_code_fetch_res_source_code = 32;
optional string source_code_fetch_res_output_code = string source_code_fetch_res_output_code = 33; // Non-empty only if cached.
33; // Non-empty only if cached.
oneof payload { oneof payload {
SourceCodeFetchMsg source_code_fetch = 100; SourceCodeFetchMsg source_code_fetch = 100;
@ -51,27 +50,27 @@ message Msg {
} }
message SourceCodeFetchMsg { message SourceCodeFetchMsg {
optional string module_specifier = 1; string module_specifier = 1;
optional string containing_file = 2; string containing_file = 2;
} }
message SourceCodeCacheMsg { message SourceCodeCacheMsg {
optional string filename = 1; string filename = 1;
optional string source_code = 2; string source_code = 2;
optional string output_code = 3; string output_code = 3;
} }
message ExitMsg { optional int32 code = 1; } message ExitMsg { int32 code = 1; }
message TimerStartMsg { message TimerStartMsg {
optional int32 id = 1; int32 id = 1;
optional bool interval = 2; bool interval = 2;
optional int32 duration = 3; // In milliseconds. int32 duration = 3; // In milliseconds.
} }
message TimerReadyMsg { message TimerReadyMsg {
optional int32 id = 1; int32 id = 1;
optional bool done = 2; bool done = 2;
} }
message TimerClearMsg { optional int32 id = 1; } message TimerClearMsg { int32 id = 1; }

25
os.go
View file

@ -18,14 +18,15 @@ func InitOS() {
switch msg.Payload.(type) { switch msg.Payload.(type) {
case *Msg_Exit: case *Msg_Exit:
payload := msg.GetExit() payload := msg.GetExit()
os.Exit(int(*payload.Code)) os.Exit(int(payload.Code))
case *Msg_SourceCodeFetch: case *Msg_SourceCodeFetch:
payload := msg.GetSourceCodeFetch() payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(*payload.ModuleSpecifier, *payload.ContainingFile) return HandleSourceCodeFetch(payload.ModuleSpecifier,
payload.ContainingFile)
case *Msg_SourceCodeCache: case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache() payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(*payload.Filename, *payload.SourceCode, return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
*payload.OutputCode) payload.OutputCode)
default: default:
panic("[os] Unexpected message " + string(buf)) panic("[os] Unexpected message " + string(buf))
} }
@ -64,8 +65,7 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
defer func() { defer func() {
if err != nil { if err != nil {
var errStr = err.Error() res.Error = err.Error()
res.Error = &errStr
} }
out, err = proto.Marshal(res) out, err = proto.Marshal(res)
check(err) check(err)
@ -101,11 +101,11 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
var sourceCode = string(sourceCodeBuf) var sourceCode = string(sourceCodeBuf)
var command = Msg_SOURCE_CODE_FETCH_RES var command = Msg_SOURCE_CODE_FETCH_RES
res = &Msg{ res = &Msg{
Command: &command, Command: command,
SourceCodeFetchResModuleName: &moduleName, SourceCodeFetchResModuleName: moduleName,
SourceCodeFetchResFilename: &filename, SourceCodeFetchResFilename: filename,
SourceCodeFetchResSourceCode: &sourceCode, SourceCodeFetchResSourceCode: sourceCode,
SourceCodeFetchResOutputCode: &outputCode, SourceCodeFetchResOutputCode: outputCode,
} }
return return
} }
@ -118,8 +118,7 @@ func HandleSourceCodeCache(filename string, sourceCode string,
err := ioutil.WriteFile(fn, outputCodeBuf, 0600) err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{} res := &Msg{}
if err != nil { if err != nil {
var errStr = err.Error() res.Error = err.Error()
res.Error = &errStr
} }
out, err := proto.Marshal(res) out, err := proto.Marshal(res)
check(err) check(err)

View file

@ -22,19 +22,19 @@ func InitTimers() {
switch msg.Payload.(type) { switch msg.Payload.(type) {
case *Msg_TimerStart: case *Msg_TimerStart:
payload := msg.GetTimerStart() payload := msg.GetTimerStart()
timers[*payload.Id] = &Timer{ timers[payload.Id] = &Timer{
Id: *payload.Id, Id: payload.Id,
Done: false, Done: false,
Interval: *payload.Interval, Interval: payload.Interval,
Duration: *payload.Duration, Duration: payload.Duration,
Cleared: false, Cleared: false,
} }
timers[*payload.Id].StartTimer() timers[payload.Id].StartTimer()
return nil return nil
case *Msg_TimerClear: case *Msg_TimerClear:
payload := msg.GetTimerClear() payload := msg.GetTimerClear()
// TODO maybe need mutex here. // TODO maybe need mutex here.
timer := timers[*payload.Id] timer := timers[payload.Id]
timer.Clear() timer.Clear()
return nil return nil
default: default:
@ -64,8 +64,8 @@ func (t *Timer) StartTimer() {
PubMsg("timers", &Msg{ PubMsg("timers", &Msg{
Payload: &Msg_TimerReady{ Payload: &Msg_TimerReady{
TimerReady: &TimerReadyMsg{ TimerReady: &TimerReadyMsg{
Id: &t.Id, Id: t.Id,
Done: &t.Done, Done: t.Done,
}, },
}, },
}) })