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

10
main.go
View file

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

View file

@ -1,21 +1,21 @@
syntax = "proto2";
syntax = "proto3";
package main;
message BaseMsg {
optional string channel = 1;
optional bytes payload = 2;
string channel = 1;
bytes payload = 2;
}
message Msg {
optional string error = 1;
string error = 1;
enum Command {
ERROR = 1;
START = 2;
SOURCE_CODE_FETCH_RES = 3;
ERROR = 0;
START = 1;
SOURCE_CODE_FETCH_RES = 2;
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)
// In order to reduce code in the size of the generated javascript
@ -24,21 +24,20 @@ message Msg {
// arguments by their name.
// Start
optional string start_cwd = 10;
string start_cwd = 10;
repeated string start_argv = 11;
optional bool start_debug_flag = 12;
optional string start_main_js = 13; // The contents of dist/main.js
optional string start_main_map = 14; // The contents of dist/main.map
bool start_debug_flag = 12;
string start_main_js = 13; // The contents of dist/main.js
string start_main_map = 14; // The contents of dist/main.map
// SOURCE_CODE_FETCH_RES
// 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
// is the location of the locally downloaded source code.
optional string source_code_fetch_res_module_name = 30;
optional string source_code_fetch_res_filename = 31;
optional string source_code_fetch_res_source_code = 32;
optional string source_code_fetch_res_output_code =
33; // Non-empty only if cached.
string source_code_fetch_res_module_name = 30;
string source_code_fetch_res_filename = 31;
string source_code_fetch_res_source_code = 32;
string source_code_fetch_res_output_code = 33; // Non-empty only if cached.
oneof payload {
SourceCodeFetchMsg source_code_fetch = 100;
@ -51,27 +50,27 @@ message Msg {
}
message SourceCodeFetchMsg {
optional string module_specifier = 1;
optional string containing_file = 2;
string module_specifier = 1;
string containing_file = 2;
}
message SourceCodeCacheMsg {
optional string filename = 1;
optional string source_code = 2;
optional string output_code = 3;
string filename = 1;
string source_code = 2;
string output_code = 3;
}
message ExitMsg { optional int32 code = 1; }
message ExitMsg { int32 code = 1; }
message TimerStartMsg {
optional int32 id = 1;
optional bool interval = 2;
optional int32 duration = 3; // In milliseconds.
int32 id = 1;
bool interval = 2;
int32 duration = 3; // In milliseconds.
}
message TimerReadyMsg {
optional int32 id = 1;
optional bool done = 2;
int32 id = 1;
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) {
case *Msg_Exit:
payload := msg.GetExit()
os.Exit(int(*payload.Code))
os.Exit(int(payload.Code))
case *Msg_SourceCodeFetch:
payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(*payload.ModuleSpecifier, *payload.ContainingFile)
return HandleSourceCodeFetch(payload.ModuleSpecifier,
payload.ContainingFile)
case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(*payload.Filename, *payload.SourceCode,
*payload.OutputCode)
return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
payload.OutputCode)
default:
panic("[os] Unexpected message " + string(buf))
}
@ -64,8 +65,7 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
defer func() {
if err != nil {
var errStr = err.Error()
res.Error = &errStr
res.Error = err.Error()
}
out, err = proto.Marshal(res)
check(err)
@ -101,11 +101,11 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
var sourceCode = string(sourceCodeBuf)
var command = Msg_SOURCE_CODE_FETCH_RES
res = &Msg{
Command: &command,
SourceCodeFetchResModuleName: &moduleName,
SourceCodeFetchResFilename: &filename,
SourceCodeFetchResSourceCode: &sourceCode,
SourceCodeFetchResOutputCode: &outputCode,
Command: command,
SourceCodeFetchResModuleName: moduleName,
SourceCodeFetchResFilename: filename,
SourceCodeFetchResSourceCode: sourceCode,
SourceCodeFetchResOutputCode: outputCode,
}
return
}
@ -118,8 +118,7 @@ func HandleSourceCodeCache(filename string, sourceCode string,
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{}
if err != nil {
var errStr = err.Error()
res.Error = &errStr
res.Error = err.Error()
}
out, err := proto.Marshal(res)
check(err)

View file

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