1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Support async/await by including ES2017

This required adjusting the module loading system.
This commit is contained in:
Ryan Dahl 2018-05-26 15:21:15 -04:00
parent cbfb7d8720
commit 7609df9d4f
6 changed files with 53 additions and 15 deletions

View file

@ -31,7 +31,7 @@ deno: msg.pb.go $(GO_FILES)
go build -o deno
assets.go: dist/main.js
cp node_modules/typescript/lib/lib.d.ts dist/
cp node_modules/typescript/lib/lib.*d.ts dist/
cp deno.d.ts dist/
go-bindata -pkg main -o assets.go dist/

8
os.go
View file

@ -76,14 +76,18 @@ func HandleCodeFetch(moduleSpecifier string, containingFile string) (out []byte)
return
}
//println("HandleCodeFetch", "moduleSpecifier", moduleSpecifier,
// "containingFile", containingFile, "filename", filename)
logDebug("HandleCodeFetch moduleSpecifier %s containingFile %s filename %s",
moduleSpecifier, containingFile, filename)
if isRemote(moduleName) {
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
} else if strings.HasPrefix(moduleName, assetPrefix) {
f := strings.TrimPrefix(moduleName, assetPrefix)
sourceCodeBuf, err = Asset("dist/" + f)
if err != nil {
logDebug("%s Asset doesn't exist. Return without error", moduleName)
err = nil
}
} else {
assert(moduleName == filename,
"if a module isn't remote, it should have the same filename")

View file

@ -57,6 +57,10 @@ export class FileModule {
readonly sourceCode = "",
public outputCode = ""
) {
util.assert(
!FileModule.map.has(fileName),
`FileModule.map already has ${fileName}`
);
FileModule.map.set(fileName, this);
if (outputCode !== "") {
this.scriptVersion = "1";
@ -124,8 +128,8 @@ export function makeDefine(fileName: string): AmdDefine {
export function resolveModule(
moduleSpecifier: string,
containingFile: string
): FileModule {
util.log("resolveModule", { moduleSpecifier, containingFile });
): null | FileModule {
//util.log("resolveModule", { moduleSpecifier, containingFile });
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
// We ask golang to sourceCodeFetch. It will load the sourceCode and if
// there is any outputCode cached, it will return that as well.
@ -133,8 +137,16 @@ export function resolveModule(
moduleSpecifier,
containingFile
);
util.log("resolveModule", { containingFile, moduleSpecifier, filename });
if (sourceCode.length == 0) {
return null;
}
util.log("resolveModule sourceCode length ", sourceCode.length);
let m = FileModule.load(filename);
if (m != null) {
return m;
} else {
return new FileModule(filename, sourceCode, outputCode);
}
}
function resolveModuleName(
@ -160,7 +172,9 @@ class Compiler {
module: ts.ModuleKind.AMD,
outDir: "$deno$",
inlineSourceMap: true,
inlineSources: true
lib: ["es2017"],
inlineSources: true,
target: ts.ScriptTarget.ES2017
};
/*
allowJs: true,
@ -224,15 +238,21 @@ class TypeScriptHost implements ts.LanguageServiceHost {
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
util.log("getScriptSnapshot", fileName);
const m = FileModule.load(fileName);
util.assert(m != null);
const m = resolveModule(fileName, ".");
if (m == null) {
util.log("getScriptSnapshot", fileName, "NOT FOUND");
return undefined;
}
//const m = resolveModule(fileName, ".");
util.assert(m.sourceCode.length > 0);
return ts.ScriptSnapshot.fromString(m.sourceCode);
}
fileExists(fileName: string): boolean {
util.log("fileExist", fileName);
return true;
const m = resolveModule(fileName, ".");
const exists = m != null;
util.log("fileExist", fileName, exists);
return exists;
}
readFile(path: string, encoding?: string): string | undefined {
@ -255,8 +275,8 @@ class TypeScriptHost implements ts.LanguageServiceHost {
}
getDefaultLibFileName(options: ts.CompilerOptions): string {
util.log("getDefaultLibFileName");
const fn = ts.getDefaultLibFileName(options);
util.log("getDefaultLibFileName", fn);
const m = resolveModule(fn, "/$asset$/");
return m.fileName;
}
@ -266,7 +286,7 @@ class TypeScriptHost implements ts.LanguageServiceHost {
containingFile: string,
reusedNames?: string[]
): Array<ts.ResolvedModule | undefined> {
util.log("resolveModuleNames", { moduleNames, reusedNames });
//util.log("resolveModuleNames", { moduleNames, reusedNames });
return moduleNames.map((name: string) => {
let resolvedFileName;
if (name === "deno") {

11
testdata/012_async.ts vendored Normal file
View file

@ -0,0 +1,11 @@
// Check that we can use the async keyword.
async function main() {
await new Promise((resolve, reject) => {
console.log("2");
setTimeout(resolve, 100);
});
console.log("3");
}
console.log("1");
main();

3
testdata/012_async.ts.out vendored Normal file
View file

@ -0,0 +1,3 @@
1
2
3

View file

@ -9,7 +9,7 @@ import (
func logDebug(format string, v ...interface{}) {
// Unless the debug flag is specified, discard logs.
if *flagDebug {
fmt.Printf(format+"\n", v)
fmt.Printf(format+"\n", v...)
}
}