1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -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 go build -o deno
assets.go: dist/main.js 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/ cp deno.d.ts dist/
go-bindata -pkg main -o assets.go 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 return
} }
//println("HandleCodeFetch", "moduleSpecifier", moduleSpecifier, logDebug("HandleCodeFetch moduleSpecifier %s containingFile %s filename %s",
// "containingFile", containingFile, "filename", filename) moduleSpecifier, containingFile, filename)
if isRemote(moduleName) { if isRemote(moduleName) {
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename) sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
} else if strings.HasPrefix(moduleName, assetPrefix) { } else if strings.HasPrefix(moduleName, assetPrefix) {
f := strings.TrimPrefix(moduleName, assetPrefix) f := strings.TrimPrefix(moduleName, assetPrefix)
sourceCodeBuf, err = Asset("dist/" + f) sourceCodeBuf, err = Asset("dist/" + f)
if err != nil {
logDebug("%s Asset doesn't exist. Return without error", moduleName)
err = nil
}
} else { } else {
assert(moduleName == filename, assert(moduleName == filename,
"if a module isn't remote, it should have the same filename") "if a module isn't remote, it should have the same filename")

View file

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