Skip to content

Commit

Permalink
readFileSync is working
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 14, 2018
1 parent 1a80bcb commit aba6a1d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions testdata/hello_world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");
1 change: 1 addition & 0 deletions testdata/hello_world.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"removeComments": true,
"preserveConstEnums": true,
"declaration": true,
"target": "es5",
"lib": ["es2015"],
"target": "es2017",
"lib": ["es2017"],
"noEmit": true,
"noUnusedLocals": true,
"noImplicitReturns": true,
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit aba6a1d

Please sign in to comment.