summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--main.go31
-rw-r--r--main.ts4
-rw-r--r--msg.proto33
-rw-r--r--os.ts15
5 files changed, 21 insertions, 66 deletions
diff --git a/Makefile b/Makefile
index fca1c2f8f..a7ee97eca 100644
--- a/Makefile
+++ b/Makefile
@@ -45,7 +45,7 @@ fmt: node_modules
go fmt
clang-format msg.proto -i
-test:
+test: deno
node test.js
-.PHONY: lint clean distclean
+.PHONY: test lint clean distclean
diff --git a/main.go b/main.go
index a277f24d6..be7ad541f 100644
--- a/main.go
+++ b/main.go
@@ -24,7 +24,7 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string {
}
func HandleSourceCodeFetch(filename string) []byte {
- res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES}
+ res := &Msg{}
sourceCodeBuf, err := Asset("dist/" + filename)
if err != nil {
sourceCodeBuf, err = ioutil.ReadFile(filename)
@@ -61,7 +61,7 @@ func HandleSourceCodeCache(filename string, sourceCode string,
fn := CacheFileName(filename, []byte(sourceCode))
outputCodeBuf := []byte(outputCode)
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
- res := &Msg{Kind: Msg_DATA_RESPONSE}
+ res := &Msg{}
if err != nil {
res.Error = err.Error()
}
@@ -70,19 +70,6 @@ func HandleSourceCodeCache(filename string, sourceCode string,
return out
}
-func ReadFileSync(filename string) []byte {
- buf, err := ioutil.ReadFile(filename)
- msg := &Msg{Kind: Msg_DATA_RESPONSE}
- if err != nil {
- msg.Error = err.Error()
- } else {
- msg.Data = buf
- }
- out, err := proto.Marshal(msg)
- check(err)
- return out
-}
-
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
@@ -125,15 +112,14 @@ func recv(buf []byte) []byte {
msg := &Msg{}
err := proto.Unmarshal(buf, msg)
check(err)
- switch msg.Kind {
- case Msg_READ_FILE_SYNC:
- return ReadFileSync(msg.Path)
- case Msg_EXIT:
- os.Exit(int(msg.Code))
- case Msg_SOURCE_CODE_FETCH:
+ switch msg.Payload.(type) {
+ case *Msg_Exit:
+ payload := msg.GetExit()
+ os.Exit(int(payload.Code))
+ case *Msg_SourceCodeFetch:
payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(payload.Filename)
- case Msg_SOURCE_CODE_CACHE:
+ case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(payload.Filename, payload.SourceCode, payload.OutputCode)
default:
@@ -152,7 +138,6 @@ func main() {
check(err)
out, err := proto.Marshal(&Msg{
- Kind: Msg_START,
Payload: &Msg_Start{
Start: &StartMsg{
Cwd: cwd,
diff --git a/main.ts b/main.ts
index 4b64650f9..e840e5b5a 100644
--- a/main.ts
+++ b/main.ts
@@ -13,8 +13,8 @@ function start(cwd: string, argv: string[]): void {
V8Worker2.recv((ab: ArrayBuffer) => {
const msg = pb.Msg.decode(new Uint8Array(ab));
- switch (msg.kind) {
- case pb.Msg.MsgKind.START:
+ switch (msg.payload) {
+ case "start":
start(msg.start.cwd, msg.start.argv);
break;
default:
diff --git a/msg.proto b/msg.proto
index fbedf9752..181dcc551 100644
--- a/msg.proto
+++ b/msg.proto
@@ -2,34 +2,15 @@ syntax = "proto3";
package main;
message Msg {
- enum MsgKind {
- START = 0;
- READ_FILE_SYNC = 1;
- DATA_RESPONSE = 2;
- EXIT = 3;
-
- SOURCE_CODE_FETCH = 4;
- SOURCE_CODE_FETCH_RES = 5;
- SOURCE_CODE_CACHE = 6;
- }
- MsgKind kind = 10;
+ string error = 1;
oneof payload {
- StartMsg start = 90;
- SourceCodeFetchMsg source_code_fetch = 91;
- SourceCodeFetchResMsg source_code_fetch_res = 92;
- SourceCodeCacheMsg source_code_cache = 93;
+ StartMsg start = 10;
+ SourceCodeFetchMsg source_code_fetch = 11;
+ SourceCodeFetchResMsg source_code_fetch_res = 12;
+ SourceCodeCacheMsg source_code_cache = 13;
+ ExitMsg exit = 14;
}
-
- // READ_FILE_SYNC and MKDIRP
- string path = 20;
-
- // DATA_RESPONSE
- bytes data = 30;
- string error = 31;
-
- // EXIT
- int32 code = 40;
}
// START
@@ -50,3 +31,5 @@ message SourceCodeCacheMsg {
string source_code = 2;
string output_code = 3;
}
+
+message ExitMsg { int32 code = 1; }
diff --git a/os.ts b/os.ts
index 4952609f2..d09026c6c 100644
--- a/os.ts
+++ b/os.ts
@@ -1,13 +1,11 @@
import { main as pb } from "./msg.pb";
-import { TextDecoder } from "text-encoding";
// TODO move this to types.ts
type TypedArray = Uint8Array | Float32Array | Int32Array;
export function exit(code = 0): void {
sendMsgFromObject({
- kind: pb.Msg.MsgKind.EXIT,
- code
+ exit: { code }
});
}
@@ -15,7 +13,6 @@ export function sourceCodeFetch(
filename: string
): { sourceCode: string; outputCode: string } {
const res = sendMsgFromObject({
- kind: pb.Msg.MsgKind.SOURCE_CODE_FETCH,
sourceCodeFetch: { filename }
});
const { sourceCode, outputCode } = res.sourceCodeFetchRes;
@@ -28,21 +25,11 @@ export function sourceCodeCache(
outputCode: string
): void {
const res = sendMsgFromObject({
- kind: pb.Msg.MsgKind.SOURCE_CODE_CACHE,
sourceCodeCache: { filename, sourceCode, outputCode }
});
throwOnError(res);
}
-export function readFileSync(filename: string): string {
- const res = sendMsgFromObject({
- kind: pb.Msg.MsgKind.READ_FILE_SYNC,
- path: filename
- });
- const decoder = new TextDecoder("utf8");
- return decoder.decode(res.data);
-}
-
function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;