From 0fe6ab91df7158cacd5c2a632d36066251a292c4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 25 May 2018 15:36:13 -0400 Subject: [PATCH] Finish de-oneof-ing --- msg.proto | 68 +++++++++++++++++++++++++------------------------------ os.go | 24 ++++++++++---------- os.ts | 16 +++++++++---- timers.go | 33 ++++++++++++--------------- timers.ts | 26 +++++++++++---------- 5 files changed, 84 insertions(+), 83 deletions(-) diff --git a/msg.proto b/msg.proto index db86f8ae19..28ad77e567 100644 --- a/msg.proto +++ b/msg.proto @@ -12,8 +12,13 @@ message Msg { enum Command { ERROR = 0; START = 1; - SOURCE_CODE_FETCH_RES = 2; - ONEOF = 100; + SOURCE_CODE_FETCH = 2; + SOURCE_CODE_FETCH_RES = 3; + SOURCE_CODE_CACHE = 4; + EXIT = 5; + TIMER_START = 6; + TIMER_READY = 7; + TIMER_CLEAR = 8; } Command command = 2; @@ -23,13 +28,17 @@ message Msg { // potentially add many hundreds of commands. Therefore we just prefix command // arguments by their name. - // Start + // START string start_cwd = 10; repeated string start_argv = 11; 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 + string source_code_fetch_module_specifier = 20; + string source_code_fetch_containing_file = 21; + // 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 @@ -39,38 +48,23 @@ message Msg { 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; - SourceCodeCacheMsg source_code_cache = 102; - ExitMsg exit = 103; - TimerStartMsg timer_start = 104; - TimerReadyMsg timer_ready = 105; - TimerClearMsg timer_clear = 106; - } + // SOURCE_CODE_CACHE + string source_code_cache_filename = 41; + string source_code_cache_source_code = 42; + string source_code_cache_output_code = 43; + + // EXIT + int32 exit_code = 50; + + // TIMER_START + int32 timer_start_id = 60; + bool timer_start_interval = 61; + int32 timer_start_duration = 62; // In milliseconds. + + // TIMER_READY + int32 timer_ready_id = 70; + bool timer_ready_done = 71; + + // TIMER_CLEAR + int32 timer_clear_id = 80; } - -message SourceCodeFetchMsg { - string module_specifier = 1; - string containing_file = 2; -} - -message SourceCodeCacheMsg { - string filename = 1; - string source_code = 2; - string output_code = 3; -} - -message ExitMsg { int32 code = 1; } - -message TimerStartMsg { - int32 id = 1; - bool interval = 2; - int32 duration = 3; // In milliseconds. -} - -message TimerReadyMsg { - int32 id = 1; - bool done = 2; -} - -message TimerClearMsg { int32 id = 1; } diff --git a/os.go b/os.go index 9a677ed6ec..5af05e32c1 100644 --- a/os.go +++ b/os.go @@ -15,18 +15,18 @@ 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) + switch msg.Command { + case Msg_SOURCE_CODE_FETCH: + return HandleSourceCodeFetch( + msg.SourceCodeFetchModuleSpecifier, + msg.SourceCodeFetchContainingFile) + case Msg_SOURCE_CODE_CACHE: + return HandleSourceCodeCache( + msg.SourceCodeCacheFilename, + msg.SourceCodeCacheSourceCode, + msg.SourceCodeCacheOutputCode) + case Msg_EXIT: + os.Exit(int(msg.ExitCode)) default: panic("[os] Unexpected message " + string(buf)) } diff --git a/os.ts b/os.ts index de4bdf4369..258524afc6 100644 --- a/os.ts +++ b/os.ts @@ -3,8 +3,11 @@ import { sendMsg } from "./dispatch"; import { main as pb } from "./msg.pb"; import { assert } from "./util"; -export function exit(code = 0): void { - sendMsg("os", { exit: { code } }); +export function exit(exitCode = 0): void { + sendMsg("os", { + command: pb.Msg.Command.EXIT, + exitCode + }); } export function sourceCodeFetch( @@ -12,7 +15,9 @@ export function sourceCodeFetch( containingFile: string ): ModuleInfo { const res = sendMsg("os", { - sourceCodeFetch: { moduleSpecifier, containingFile } + command: pb.Msg.Command.SOURCE_CODE_FETCH, + sourceCodeFetchModuleSpecifier: moduleSpecifier, + sourceCodeFetchContainingFile: containingFile }); assert(res.command === pb.Msg.Command.SOURCE_CODE_FETCH_RES); return { @@ -29,6 +34,9 @@ export function sourceCodeCache( outputCode: string ): void { sendMsg("os", { - sourceCodeCache: { filename, sourceCode, outputCode } + command: pb.Msg.Command.SOURCE_CODE_CACHE, + sourceCodeCacheFilename: filename, + sourceCodeCacheSourceCode: sourceCode, + sourceCodeCacheOutputCode: outputCode }); } diff --git a/timers.go b/timers.go index cfe0f05442..96ad72997c 100644 --- a/timers.go +++ b/timers.go @@ -19,27 +19,27 @@ 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() - timers[payload.Id] = &Timer{ - Id: payload.Id, + switch msg.Command { + case Msg_TIMER_START: + id := msg.TimerStartId + t := &Timer{ + Id: id, Done: false, - Interval: payload.Interval, - Duration: payload.Duration, + Interval: msg.TimerStartInterval, + Duration: msg.TimerStartDuration, Cleared: false, } - timers[payload.Id].StartTimer() + t.StartTimer() + timers[id] = t return nil - case *Msg_TimerClear: - payload := msg.GetTimerClear() + case Msg_TIMER_CLEAR: // TODO maybe need mutex here. - timer := timers[payload.Id] + timer := timers[msg.TimerClearId] timer.Clear() - return nil default: panic("[timers] Unexpected message " + string(buf)) } + return nil }) } @@ -62,12 +62,9 @@ func (t *Timer) StartTimer() { t.Done = true } PubMsg("timers", &Msg{ - Payload: &Msg_TimerReady{ - TimerReady: &TimerReadyMsg{ - Id: t.Id, - Done: t.Done, - }, - }, + Command: Msg_TIMER_READY, + TimerReadyId: t.Id, + TimerReadyDone: t.Done, }) if t.Done { return diff --git a/timers.ts b/timers.ts index 5407c63b00..d0d4b7d020 100644 --- a/timers.ts +++ b/timers.ts @@ -1,5 +1,6 @@ import { main as pb } from "./msg.pb"; import * as dispatch from "./dispatch"; +import { assert } from "./util"; let nextTimerId = 1; @@ -23,7 +24,9 @@ export function initTimers() { function onMessage(payload: Uint8Array) { const msg = pb.Msg.decode(payload); - const { id, done } = msg.timerReady; + assert(msg.command === pb.Msg.Command.TIMER_READY); + const id = msg.timerReadyId; + const done = msg.timerReadyDone; const timer = timers.get(id); if (!timer) { return; @@ -49,11 +52,10 @@ export function setTimeout( }; timers.set(timer.id, timer); dispatch.sendMsg("timers", { - timerStart: { - id: timer.id, - interval: false, - duration - } + command: pb.Msg.Command.TIMER_START, + timerStartId: timer.id, + timerStartInterval: false, + timerStartDuration: duration }); return timer.id; } @@ -74,17 +76,17 @@ export function setInterval( }; timers.set(timer.id, timer); dispatch.sendMsg("timers", { - timerStart: { - id: timer.id, - interval: true, - duration: repeat - } + command: pb.Msg.Command.TIMER_START, + timerStartId: timer.id, + timerStartInterval: true, + timerStartDuration: repeat }); return timer.id; } export function clearTimer(id: number) { dispatch.sendMsg("timers", { - timerClear: { id } + command: pb.Msg.Command.TIMER_CLEAR, + timerClearId: id }); }