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

Finish de-oneof-ing

This commit is contained in:
Ryan Dahl 2018-05-25 15:36:13 -04:00
parent 5b858632ac
commit 0fe6ab91df
5 changed files with 84 additions and 83 deletions

View file

@ -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; }

24
os.go
View file

@ -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))
}

16
os.ts
View file

@ -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
});
}

View file

@ -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

View file

@ -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
});
}