mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 04:52:26 -05:00
Use typescript
This commit is contained in:
parent
2da9893da3
commit
50105d7855
10 changed files with 177 additions and 34 deletions
2
Makefile
2
Makefile
|
@ -13,7 +13,7 @@ msg.pb.js: msg.proto node_modules
|
|||
msg.pb.d.ts: msg.pb.js node_modules
|
||||
./node_modules/.bin/pbts -o msg.pb.d.ts msg.pb.js
|
||||
|
||||
dist/main.js: main.ts msg.pb.js msg.pb.d.ts node_modules
|
||||
dist/main.js: main.ts compiler.ts fs.ts util.ts msg.pb.js msg.pb.d.ts node_modules
|
||||
./node_modules/.bin/tsc --noEmit # Only for type checking.
|
||||
./node_modules/.bin/parcel build --out-dir=dist/ --no-minify main.ts
|
||||
|
||||
|
|
128
compiler.ts
Normal file
128
compiler.ts
Normal file
|
@ -0,0 +1,128 @@
|
|||
import * as ts from "typescript";
|
||||
import { assert, globalEval } from "./util";
|
||||
import { readFileSync } from "./fs";
|
||||
|
||||
export function compile(cwd: string, inputFn: string): void {
|
||||
const options: ts.CompilerOptions = {
|
||||
"allowJs": true,
|
||||
"outFile": "out.js",
|
||||
};
|
||||
const host = new CompilerHost(cwd);
|
||||
|
||||
let program = ts.createProgram([inputFn], options, host);
|
||||
//let sourceFiles = program.getSourceFiles();
|
||||
//console.log("rootFileNames", program.getRootFileNames());
|
||||
|
||||
let emitResult = program.emit();
|
||||
assert(!emitResult.emitSkipped);
|
||||
//console.log("emitResult", emitResult);
|
||||
}
|
||||
|
||||
export class CompilerHost {
|
||||
constructor(public cwd: string) {}
|
||||
|
||||
getSourceFile(
|
||||
fileName: string,
|
||||
languageVersion: ts.ScriptTarget,
|
||||
onError?: (message: string) => void,
|
||||
shouldCreateNewSourceFile?: boolean
|
||||
): ts.SourceFile | undefined {
|
||||
//console.log("getSourceFile", fileName);
|
||||
let sourceText: string;
|
||||
if (fileName === "lib.d.ts") {
|
||||
// TODO this should be compiled into the bindata.
|
||||
sourceText = readFileSync("node_modules/typescript/lib/lib.d.ts");
|
||||
} else {
|
||||
sourceText = readFileSync(fileName);
|
||||
}
|
||||
if (sourceText) {
|
||||
return ts.createSourceFile(fileName, sourceText, languageVersion);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
getSourceFileByPath?(
|
||||
fileName: string,
|
||||
path: ts.Path,
|
||||
languageVersion: ts.ScriptTarget,
|
||||
onError?: (message: string) => void,
|
||||
shouldCreateNewSourceFile?: boolean
|
||||
): ts.SourceFile | undefined {
|
||||
console.log("getSourceFileByPath", fileName);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// getCancellationToken?(): CancellationToken;
|
||||
getDefaultLibFileName(options: ts.CompilerOptions): string {
|
||||
return ts.getDefaultLibFileName(options);
|
||||
}
|
||||
|
||||
getDefaultLibLocation(): string {
|
||||
return "/blah/";
|
||||
}
|
||||
|
||||
outFileSource: string;
|
||||
writeFile(
|
||||
fileName: string,
|
||||
data: string,
|
||||
writeByteOrderMark: boolean,
|
||||
onError: ((message: string) => void) | undefined,
|
||||
sourceFiles: ReadonlyArray<ts.SourceFile>
|
||||
): void {
|
||||
//console.log("writeFile", fileName);
|
||||
//console.log("writeFile source", data);
|
||||
globalEval(data);
|
||||
//this.outFileSource = data;
|
||||
}
|
||||
|
||||
getCurrentDirectory(): string {
|
||||
return this.cwd;
|
||||
}
|
||||
|
||||
getDirectories(path: string): string[] {
|
||||
console.log("getDirectories", path);
|
||||
return [];
|
||||
}
|
||||
|
||||
getCanonicalFileName(fileName: string): string {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
useCaseSensitiveFileNames(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
getNewLine(): string {
|
||||
return "\n";
|
||||
}
|
||||
|
||||
resolveModuleNames(
|
||||
moduleNames: string[],
|
||||
containingFile: string,
|
||||
reusedNames?: string[]
|
||||
): (ts.ResolvedModule | undefined)[] {
|
||||
console.log("resolveModuleNames", moduleNames);
|
||||
return [];
|
||||
}
|
||||
|
||||
fileExists(fileName: string): boolean {
|
||||
console.log("fileExists", fileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
readFile(fileName: string): string | undefined {
|
||||
console.log("readFile", fileName);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is a companion for 'resolveModuleNames' and is used to resolve
|
||||
* 'types' references to actual type declaration files
|
||||
*/
|
||||
// resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[],
|
||||
// containingFile: string): (ResolvedTypeReferenceDirective | undefined)[];
|
||||
|
||||
// getEnvironmentVariable?(name: string): string
|
||||
// createHash?(data: string): string;
|
||||
}
|
18
fs.ts
Normal file
18
fs.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { main as pb } from "./msg.pb";
|
||||
import { TextDecoder } from "text-encoding";
|
||||
|
||||
export 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);
|
||||
}
|
12
main.go
12
main.go
|
@ -50,11 +50,15 @@ func loadAsset(w *v8worker2.Worker, path string) {
|
|||
func main() {
|
||||
worker := v8worker2.New(recv)
|
||||
loadAsset(worker, "dist/main.js")
|
||||
loadMsg := &Msg{
|
||||
Kind: Msg_LOAD,
|
||||
Argv: os.Args,
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
out, err := proto.Marshal(loadMsg)
|
||||
out, err := proto.Marshal(&Msg{
|
||||
Kind: Msg_START,
|
||||
Cwd: cwd,
|
||||
Argv: os.Args,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
28
main.ts
28
main.ts
|
@ -1,35 +1,19 @@
|
|||
//import * as ts from "typescript";
|
||||
import { main as pb } from "./msg.pb";
|
||||
import "./util";
|
||||
import { TextDecoder } from "text-encoding";
|
||||
import { compile } from "./compiler";
|
||||
|
||||
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 {
|
||||
function start(cwd: string, argv: string[]): void {
|
||||
// TODO parse arguments.
|
||||
const inputFn = argv[1];
|
||||
const source = readFileSync(inputFn);
|
||||
console.log("source", source);
|
||||
compile(cwd, inputFn);
|
||||
}
|
||||
|
||||
V8Worker2.recv((ab: ArrayBuffer) => {
|
||||
const msg = pb.Msg.decode(new Uint8Array(ab));
|
||||
switch (msg.kind) {
|
||||
case pb.Msg.MsgKind.LOAD:
|
||||
load(msg.argv);
|
||||
case pb.Msg.MsgKind.START:
|
||||
start(msg.cwd, msg.argv);
|
||||
break;
|
||||
default:
|
||||
console.log("Unknown message", msg);
|
||||
|
|
13
msg.proto
13
msg.proto
|
@ -4,19 +4,20 @@ package main;
|
|||
|
||||
message Msg {
|
||||
enum MsgKind {
|
||||
LOAD = 0;
|
||||
START = 0;
|
||||
READ_FILE_SYNC = 1;
|
||||
DATA_RESPONSE = 2;
|
||||
}
|
||||
MsgKind kind = 10;
|
||||
|
||||
// LOAD
|
||||
repeated string argv = 11;
|
||||
// START
|
||||
string cwd = 11;
|
||||
repeated string argv = 12;
|
||||
|
||||
// READ_FILE_SYNC
|
||||
string path = 12;
|
||||
string path = 20;
|
||||
|
||||
// DATA_RESPONSE
|
||||
bytes data = 13;
|
||||
string error = 14;
|
||||
bytes data = 30;
|
||||
string error = 31;
|
||||
}
|
||||
|
|
1
testdata/hello_world.ts
vendored
Normal file
1
testdata/hello_world.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
console.log("Hello World");
|
1
testdata/hello_world.ts.out
vendored
Normal file
1
testdata/hello_world.ts.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
Hello World
|
8
util.ts
8
util.ts
|
@ -3,7 +3,7 @@
|
|||
// the local scope. This means, for instance, that function declarations create
|
||||
// global functions, and that the code being evaluated doesn't have access to
|
||||
// local variables within the scope where it's being called.
|
||||
const globalEval = eval;
|
||||
export const globalEval = eval;
|
||||
|
||||
// A reference to the global object.
|
||||
const _global = globalEval("this");
|
||||
|
@ -24,3 +24,9 @@ _global["console"] = {
|
|||
print(out.join(" "));
|
||||
}
|
||||
};
|
||||
|
||||
export function assert(cond: boolean, msg = "") {
|
||||
if (!cond) {
|
||||
throw Error("Assertion failed. " + msg);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue