diff --git a/main.go b/main.go index 3ee79fb6bd..4612849db0 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,37 @@ package main import ( "github.com/golang/protobuf/proto" "github.com/ry/v8worker2" + "io/ioutil" "os" ) -func recv(msg []byte) []byte { - println("recv cb", string(msg)) - return nil +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) + if err != nil { + panic(err) + } + return out +} + +func recv(buf []byte) []byte { + msg := &Msg{} + err := proto.Unmarshal(buf, msg) + if err != nil { + panic(err) + } + switch msg.Kind { + case Msg_READ_FILE_SYNC: + return ReadFileSync(msg.Path) + default: + panic("Unexpected message") + } } func loadAsset(w *v8worker2.Worker, path string) { diff --git a/main.ts b/main.ts index 153414a195..3a6a043aba 100644 --- a/main.ts +++ b/main.ts @@ -1,10 +1,29 @@ //import * as ts from "typescript"; import { main as pb } from "./msg.pb" import "./util"; +import { TextDecoder } from "text-encoding"; +function readFileSync(filename: string): string { + const msg = pb.Msg.fromObject({ + kind: pb.Msg.MsgKind.READ_FILE_SYNC, + path: filename + }); + const ui8 = pb.Msg.encode(msg).finish(); + const ab = ui8.buffer.slice(ui8.byteOffset, ui8.byteOffset + ui8.byteLength); + const resBuf = V8Worker2.send(ab as ArrayBuffer); + const res = pb.Msg.decode(new Uint8Array(resBuf)); + if (res.error != null && res.error.length > 0) { + throw Error(res.error); + } + const decoder = new TextDecoder("utf8"); + return decoder.decode(res.data) +} function load(argv: string[]): void { console.log("Load argv", argv); + const inputFn = argv[1]; + const source = readFileSync(inputFn); + console.log("source", source) } V8Worker2.recv((ab: ArrayBuffer) => { diff --git a/msg.proto b/msg.proto index d76875e130..00598ea903 100644 --- a/msg.proto +++ b/msg.proto @@ -5,9 +5,18 @@ package main; message Msg { enum MsgKind { LOAD = 0; + READ_FILE_SYNC = 1; + DATA_RESPONSE = 2; } MsgKind kind = 10; // LOAD repeated string argv = 11; + + // READ_FILE_SYNC + string path = 12; + + // DATA_RESPONSE + bytes data = 13; + string error = 14; } diff --git a/package.json b/package.json index 6b011d247a..a4f93d7740 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "name": "deno", "devDependencies": { + "@types/text-encoding": "^0.0.32", "parcel-bundler": "^1.8.1", "protobufjs": "^6.8.6", + "text-encoding": "^0.6.4", "typescript": "^2.8.3" } } diff --git a/testdata/hello_world.js b/testdata/hello_world.js new file mode 100644 index 0000000000..accefceba6 --- /dev/null +++ b/testdata/hello_world.js @@ -0,0 +1 @@ +console.log("Hello World"); diff --git a/testdata/hello_world.out b/testdata/hello_world.out new file mode 100644 index 0000000000..557db03de9 --- /dev/null +++ b/testdata/hello_world.out @@ -0,0 +1 @@ +Hello World diff --git a/tsconfig.json b/tsconfig.json index d0810f8301..71fb930b73 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,8 @@ "removeComments": true, "preserveConstEnums": true, "declaration": true, - "target": "es5", - "lib": ["es2015"], + "target": "es2017", + "lib": ["es2017"], "noEmit": true, "noUnusedLocals": true, "noImplicitReturns": true, diff --git a/yarn.lock b/yarn.lock index c0027a0785..d6e4f1bfa0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -53,6 +53,10 @@ version "8.10.14" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.14.tgz#a24767cfa22023f1bf7e751c0ead56a14c07ed45" +"@types/text-encoding@^0.0.32": + version "0.0.32" + resolved "https://registry.yarnpkg.com/@types/text-encoding/-/text-encoding-0.0.32.tgz#52289b320a406850b14f08f48b475ca021218048" + abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" @@ -3388,6 +3392,10 @@ tar@^4: safe-buffer "^5.1.2" yallist "^3.0.2" +text-encoding@^0.6.4: + version "0.6.4" + resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19" + through2@^2.0.0, through2@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"