mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
readFileSync is working
This commit is contained in:
parent
1a80bcb250
commit
aba6a1dc87
8 changed files with 70 additions and 5 deletions
31
main.go
31
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) {
|
||||
|
|
19
main.ts
19
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) => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
testdata/hello_world.js
vendored
Normal file
1
testdata/hello_world.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
console.log("Hello World");
|
1
testdata/hello_world.out
vendored
Normal file
1
testdata/hello_world.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
Hello World
|
|
@ -7,8 +7,8 @@
|
|||
"removeComments": true,
|
||||
"preserveConstEnums": true,
|
||||
"declaration": true,
|
||||
"target": "es5",
|
||||
"lib": ["es2015"],
|
||||
"target": "es2017",
|
||||
"lib": ["es2017"],
|
||||
"noEmit": true,
|
||||
"noUnusedLocals": true,
|
||||
"noImplicitReturns": true,
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue