0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -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 { enum Command {
ERROR = 0; ERROR = 0;
START = 1; START = 1;
SOURCE_CODE_FETCH_RES = 2; SOURCE_CODE_FETCH = 2;
ONEOF = 100; SOURCE_CODE_FETCH_RES = 3;
SOURCE_CODE_CACHE = 4;
EXIT = 5;
TIMER_START = 6;
TIMER_READY = 7;
TIMER_CLEAR = 8;
} }
Command command = 2; Command command = 2;
@ -23,13 +28,17 @@ message Msg {
// potentially add many hundreds of commands. Therefore we just prefix command // potentially add many hundreds of commands. Therefore we just prefix command
// arguments by their name. // arguments by their name.
// Start // START
string start_cwd = 10; string start_cwd = 10;
repeated string start_argv = 11; repeated string start_argv = 11;
bool start_debug_flag = 12; bool start_debug_flag = 12;
string start_main_js = 13; // The contents of dist/main.js string start_main_js = 13; // The contents of dist/main.js
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
string source_code_fetch_module_specifier = 20;
string source_code_fetch_containing_file = 21;
// 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
@ -39,38 +48,23 @@ message Msg {
string source_code_fetch_res_source_code = 32; string source_code_fetch_res_source_code = 32;
string source_code_fetch_res_output_code = 33; // Non-empty only if cached. string source_code_fetch_res_output_code = 33; // Non-empty only if cached.
oneof payload { // SOURCE_CODE_CACHE
SourceCodeFetchMsg source_code_fetch = 100; string source_code_cache_filename = 41;
SourceCodeCacheMsg source_code_cache = 102; string source_code_cache_source_code = 42;
ExitMsg exit = 103; string source_code_cache_output_code = 43;
TimerStartMsg timer_start = 104;
TimerReadyMsg timer_ready = 105; // EXIT
TimerClearMsg timer_clear = 106; 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 { Sub("os", func(buf []byte) []byte {
msg := &Msg{} msg := &Msg{}
check(proto.Unmarshal(buf, msg)) check(proto.Unmarshal(buf, msg))
switch msg.Payload.(type) { switch msg.Command {
case *Msg_Exit: case Msg_SOURCE_CODE_FETCH:
payload := msg.GetExit() return HandleSourceCodeFetch(
os.Exit(int(payload.Code)) msg.SourceCodeFetchModuleSpecifier,
case *Msg_SourceCodeFetch: msg.SourceCodeFetchContainingFile)
payload := msg.GetSourceCodeFetch() case Msg_SOURCE_CODE_CACHE:
return HandleSourceCodeFetch(payload.ModuleSpecifier, return HandleSourceCodeCache(
payload.ContainingFile) msg.SourceCodeCacheFilename,
case *Msg_SourceCodeCache: msg.SourceCodeCacheSourceCode,
payload := msg.GetSourceCodeCache() msg.SourceCodeCacheOutputCode)
return HandleSourceCodeCache(payload.Filename, payload.SourceCode, case Msg_EXIT:
payload.OutputCode) os.Exit(int(msg.ExitCode))
default: default:
panic("[os] Unexpected message " + string(buf)) 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 { main as pb } from "./msg.pb";
import { assert } from "./util"; import { assert } from "./util";
export function exit(code = 0): void { export function exit(exitCode = 0): void {
sendMsg("os", { exit: { code } }); sendMsg("os", {
command: pb.Msg.Command.EXIT,
exitCode
});
} }
export function sourceCodeFetch( export function sourceCodeFetch(
@ -12,7 +15,9 @@ export function sourceCodeFetch(
containingFile: string containingFile: string
): ModuleInfo { ): ModuleInfo {
const res = sendMsg("os", { 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); assert(res.command === pb.Msg.Command.SOURCE_CODE_FETCH_RES);
return { return {
@ -29,6 +34,9 @@ export function sourceCodeCache(
outputCode: string outputCode: string
): void { ): void {
sendMsg("os", { 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 { Sub("timers", func(buf []byte) []byte {
msg := &Msg{} msg := &Msg{}
check(proto.Unmarshal(buf, msg)) check(proto.Unmarshal(buf, msg))
switch msg.Payload.(type) { switch msg.Command {
case *Msg_TimerStart: case Msg_TIMER_START:
payload := msg.GetTimerStart() id := msg.TimerStartId
timers[payload.Id] = &Timer{ t := &Timer{
Id: payload.Id, Id: id,
Done: false, Done: false,
Interval: payload.Interval, Interval: msg.TimerStartInterval,
Duration: payload.Duration, Duration: msg.TimerStartDuration,
Cleared: false, Cleared: false,
} }
timers[payload.Id].StartTimer() t.StartTimer()
timers[id] = t
return nil return nil
case *Msg_TimerClear: case Msg_TIMER_CLEAR:
payload := msg.GetTimerClear()
// TODO maybe need mutex here. // TODO maybe need mutex here.
timer := timers[payload.Id] timer := timers[msg.TimerClearId]
timer.Clear() timer.Clear()
return nil
default: default:
panic("[timers] Unexpected message " + string(buf)) panic("[timers] Unexpected message " + string(buf))
} }
return nil
}) })
} }
@ -62,12 +62,9 @@ func (t *Timer) StartTimer() {
t.Done = true t.Done = true
} }
PubMsg("timers", &Msg{ PubMsg("timers", &Msg{
Payload: &Msg_TimerReady{ Command: Msg_TIMER_READY,
TimerReady: &TimerReadyMsg{ TimerReadyId: t.Id,
Id: t.Id, TimerReadyDone: t.Done,
Done: t.Done,
},
},
}) })
if t.Done { if t.Done {
return return

View file

@ -1,5 +1,6 @@
import { main as pb } from "./msg.pb"; import { main as pb } from "./msg.pb";
import * as dispatch from "./dispatch"; import * as dispatch from "./dispatch";
import { assert } from "./util";
let nextTimerId = 1; let nextTimerId = 1;
@ -23,7 +24,9 @@ export function initTimers() {
function onMessage(payload: Uint8Array) { function onMessage(payload: Uint8Array) {
const msg = pb.Msg.decode(payload); 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); const timer = timers.get(id);
if (!timer) { if (!timer) {
return; return;
@ -49,11 +52,10 @@ export function setTimeout(
}; };
timers.set(timer.id, timer); timers.set(timer.id, timer);
dispatch.sendMsg("timers", { dispatch.sendMsg("timers", {
timerStart: { command: pb.Msg.Command.TIMER_START,
id: timer.id, timerStartId: timer.id,
interval: false, timerStartInterval: false,
duration timerStartDuration: duration
}
}); });
return timer.id; return timer.id;
} }
@ -74,17 +76,17 @@ export function setInterval(
}; };
timers.set(timer.id, timer); timers.set(timer.id, timer);
dispatch.sendMsg("timers", { dispatch.sendMsg("timers", {
timerStart: { command: pb.Msg.Command.TIMER_START,
id: timer.id, timerStartId: timer.id,
interval: true, timerStartInterval: true,
duration: repeat timerStartDuration: repeat
}
}); });
return timer.id; return timer.id;
} }
export function clearTimer(id: number) { export function clearTimer(id: number) {
dispatch.sendMsg("timers", { dispatch.sendMsg("timers", {
timerClear: { id } command: pb.Msg.Command.TIMER_CLEAR,
timerClearId: id
}); });
} }