Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
3d03f5b0
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3d03f5b0
编写于
11月 20, 2018
作者:
K
Kitson Kelly
提交者:
Ryan Dahl
11月 20, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Split Runner from Compiler
上级
3597d685
变更
14
展开全部
隐藏空白更改
内联
并排
Showing
14 changed file
with
525 addition
and
540 deletion
+525
-540
BUILD.gn
BUILD.gn
+1
-0
js/compiler.ts
js/compiler.ts
+116
-302
js/compiler_test.ts
js/compiler_test.ts
+55
-218
js/deno.ts
js/deno.ts
+3
-0
js/main.ts
js/main.ts
+6
-5
js/runner.ts
js/runner.ts
+187
-0
js/runner_test.ts
js/runner_test.ts
+141
-0
js/unit_tests.ts
js/unit_tests.ts
+1
-0
tests/async_error.ts.out
tests/async_error.ts.out
+3
-3
tests/error_001.ts.out
tests/error_001.ts.out
+3
-3
tests/error_002.ts.out
tests/error_002.ts.out
+3
-3
tests/error_004_missing_module.ts.out
tests/error_004_missing_module.ts.out
+2
-2
tests/error_005_missing_dynamic_import.ts.out
tests/error_005_missing_dynamic_import.ts.out
+2
-2
tests/error_006_import_ext_failure.ts.out
tests/error_006_import_ext_failure.ts.out
+2
-2
未找到文件。
BUILD.gn
浏览文件 @
3d03f5b0
...
...
@@ -118,6 +118,7 @@ ts_sources = [
"js/rename.ts",
"js/repl.ts",
"js/resources.ts",
"js/runner.ts",
"js/stat.ts",
"js/symlink.ts",
"js/text_encoding.ts",
...
...
js/compiler.ts
浏览文件 @
3d03f5b0
此差异已折叠。
点击以展开。
js/compiler_test.ts
浏览文件 @
3d03f5b0
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import
{
test
,
assert
,
assertEqual
}
from
"
./test_util
.ts
"
;
import
{
test
,
assert
,
assertEqual
}
from
"
./test_util
"
;
import
*
as
deno
from
"
deno
"
;
import
*
as
ts
from
"
typescript
"
;
// We use a silly amount of `any` in these tests...
// tslint:disable:no-any
const
{
Deno
Compiler
,
jsonAmdTemplate
}
=
(
deno
as
any
).
_compiler
;
const
{
Compiler
,
jsonAmdTemplate
}
=
(
deno
as
any
).
_compiler
;
interface
ModuleInfo
{
moduleName
:
string
|
undefined
;
...
...
@@ -17,16 +17,14 @@ interface ModuleInfo {
sourceMap
:
string
|
undefined
;
}
const
compilerInstance
=
Deno
Compiler
.
instance
();
const
compilerInstance
=
Compiler
.
instance
();
// References to original items we are going to mock
const
originals
=
{
_globalEval
:
(
compilerInstance
as
any
).
_globalEval
,
_log
:
(
compilerInstance
as
any
).
_log
,
_os
:
(
compilerInstance
as
any
).
_os
,
_ts
:
(
compilerInstance
as
any
).
_ts
,
_service
:
(
compilerInstance
as
any
).
_service
,
_window
:
(
compilerInstance
as
any
).
_window
_service
:
(
compilerInstance
as
any
).
_service
};
enum
MediaType
{
...
...
@@ -244,7 +242,6 @@ const emittedFiles = {
"
/root/project/foo/qat.ts
"
:
"
console.log('foo');
"
};
let
globalEvalStack
:
string
[]
=
[];
let
getEmitOutputStack
:
string
[]
=
[];
let
logStack
:
any
[][]
=
[];
let
codeCacheStack
:
Array
<
{
...
...
@@ -261,12 +258,6 @@ let codeFetchStack: Array<{
let
mockDepsStack
:
string
[][]
=
[];
let
mockFactoryStack
:
any
[]
=
[];
function
globalEvalMock
(
x
:
string
):
void
{
globalEvalStack
.
push
(
x
);
if
(
windowMock
.
define
&&
mockDepsStack
.
length
&&
mockFactoryStack
.
length
)
{
windowMock
.
define
(
mockDepsStack
.
pop
(),
mockFactoryStack
.
pop
());
}
}
function
logMock
(...
args
:
any
[]):
void
{
logStack
.
push
(
args
);
}
...
...
@@ -306,6 +297,7 @@ const osMock = {
throw
new
Error
(
`Unexpected call to os.exit(
${
code
}
)`
);
}
};
const
tsMock
=
{
createLanguageService
(
host
:
ts
.
LanguageServiceHost
):
ts
.
LanguageService
{
return
{}
as
ts
.
LanguageService
;
...
...
@@ -355,14 +347,11 @@ const serviceMock = {
);
}
};
const
windowMock
:
{
define
?:
any
}
=
{};
const
mocks
=
{
_globalEval
:
globalEvalMock
,
_log
:
logMock
,
_os
:
osMock
,
_ts
:
tsMock
,
_service
:
serviceMock
,
_window
:
windowMock
_service
:
serviceMock
};
/**
...
...
@@ -386,7 +375,6 @@ function teardown() {
codeCacheStack
=
[];
logStack
=
[];
getEmitOutputStack
=
[];
globalEvalStack
=
[];
assertEqual
(
mockDepsStack
.
length
,
0
);
assertEqual
(
mockFactoryStack
.
length
,
0
);
...
...
@@ -405,7 +393,10 @@ test(function testJsonAmdTemplate() {
factory
=
f
;
}
const
code
=
jsonAmdTemplate
(
`{ "hello": "world", "foo": "bar" }`
);
const
code
=
jsonAmdTemplate
(
`{ "hello": "world", "foo": "bar" }`
,
"
example.json
"
);
const
result
=
eval
(
code
);
assert
(
result
==
null
);
assertEqual
(
deps
&&
deps
.
length
,
0
);
...
...
@@ -415,156 +406,51 @@ test(function testJsonAmdTemplate() {
});
test
(
function
compilerInstance
()
{
assert
(
Deno
Compiler
!=
null
);
assert
(
Deno
Compiler
.
instance
()
!=
null
);
assert
(
Compiler
!=
null
);
assert
(
Compiler
.
instance
()
!=
null
);
});
// Testing the internal APIs
test
(
function
compilerRun
()
{
// equal to `deno foo/bar.ts`
test
(
function
testGetFilename
()
{
setup
();
let
factoryRun
=
false
;
mockDepsStack
.
push
([
"
require
"
,
"
exports
"
,
"
deno
"
]);
mockFactoryStack
.
push
((
_require
,
_exports
,
_deno
)
=>
{
factoryRun
=
true
;
assertEqual
(
typeof
_require
,
"
function
"
);
assertEqual
(
typeof
_exports
,
"
object
"
);
assert
(
_deno
===
deno
);
_exports
.
foo
=
"
bar
"
;
});
const
moduleMetaData
=
compilerInstance
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assert
(
factoryRun
);
assert
(
moduleMetaData
.
hasRun
);
assertEqual
(
moduleMetaData
.
sourceCode
,
fooBarTsSource
);
assertEqual
(
moduleMetaData
.
outputCode
,
fooBarTsOutput
);
assertEqual
(
JSON
.
stringify
(
moduleMetaData
.
sourceMap
),
fooBarTsSourcemap
);
assertEqual
(
moduleMetaData
.
exports
,
{
foo
:
"
bar
"
});
assertEqual
(
codeFetchStack
.
length
,
1
,
"
Module should have only been fetched once.
"
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
),
"
/root/project/foo/bar.ts
"
);
assertEqual
(
codeCacheStack
.
length
,
1
,
"
Compiled code should have only been cached once.
"
);
const
[
codeCacheCall
]
=
codeCacheStack
;
assertEqual
(
codeCacheCall
.
fileName
,
"
/root/project/foo/bar.ts
"
);
assertEqual
(
codeCacheCall
.
sourceCode
,
fooBarTsSource
);
assertEqual
(
codeCacheCall
.
outputCode
,
fooBarTsOutput
);
assertEqual
(
codeCacheCall
.
sourceMap
,
fooBarTsSourcemap
);
teardown
();
});
test
(
function
compilerRunMultiModule
()
{
// equal to `deno foo/baz.ts`
test
(
function
testGetOutput
()
{
setup
();
const
factoryStack
:
string
[]
=
[];
const
bazDeps
=
[
"
require
"
,
"
exports
"
,
"
./bar.ts
"
];
const
bazFactory
=
(
_require
,
_exports
,
_bar
)
=>
{
factoryStack
.
push
(
"
baz
"
);
assertEqual
(
_bar
.
foo
,
"
bar
"
);
};
const
barDeps
=
[
"
require
"
,
"
exports
"
,
"
deno
"
];
const
barFactory
=
(
_require
,
_exports
,
_deno
)
=>
{
factoryStack
.
push
(
"
bar
"
);
_exports
.
foo
=
"
bar
"
;
};
mockDepsStack
.
push
(
barDeps
);
mockFactoryStack
.
push
(
barFactory
);
mockDepsStack
.
push
(
bazDeps
);
mockFactoryStack
.
push
(
bazFactory
);
compilerInstance
.
run
(
"
foo/baz.ts
"
,
"
/root/project
"
);
assertEqual
(
factoryStack
,
[
"
bar
"
,
"
baz
"
]);
assertEqual
(
codeFetchStack
.
length
,
2
,
"
Modules should have only been fetched once.
"
);
assertEqual
(
codeCacheStack
.
length
,
0
,
"
No code should have been cached.
"
);
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assertEqual
(
compilerInstance
.
getOutput
(
filename
),
fooBarTsOutput
);
teardown
();
});
test
(
function
compilerRunCircularDependency
()
{
test
(
function
testGetOutputJson
()
{
setup
();
const
factoryStack
:
string
[]
=
[];
const
modADeps
=
[
"
require
"
,
"
exports
"
,
"
./modB.ts
"
];
const
modAFactory
=
(
_require
,
_exports
,
_modB
)
=>
{
assertEqual
(
_modB
.
foo
,
"
bar
"
);
factoryStack
.
push
(
"
modA
"
);
_exports
.
bar
=
"
baz
"
;
_modB
.
assertModA
();
};
const
modBDeps
=
[
"
require
"
,
"
exports
"
,
"
./modA.ts
"
];
const
modBFactory
=
(
_require
,
_exports
,
_modA
)
=>
{
assertEqual
(
_modA
,
{});
factoryStack
.
push
(
"
modB
"
);
_exports
.
foo
=
"
bar
"
;
_exports
.
assertModA
=
()
=>
{
assertEqual
(
_modA
,
{
bar
:
"
baz
"
});
};
};
mockDepsStack
.
push
(
modBDeps
);
mockFactoryStack
.
push
(
modBFactory
);
mockDepsStack
.
push
(
modADeps
);
mockFactoryStack
.
push
(
modAFactory
);
compilerInstance
.
run
(
"
modA.ts
"
,
"
/root/project
"
);
assertEqual
(
factoryStack
,
[
"
modB
"
,
"
modA
"
]);
teardown
();
const
filename
=
compilerInstance
.
getFilename
(
"
./config.json
"
,
"
/root/project/loadConfig.ts
"
);
assertEqual
(
compilerInstance
.
getOutput
(
filename
),
jsonAmdTemplate
(
configJsonSource
,
filename
)
);
});
test
(
function
compilerLoadJsonModul
e
()
{
test
(
function
testGetSourc
e
()
{
setup
();
const
factoryStack
:
string
[]
=
[];
const
configJsonDeps
:
string
[]
=
[];
const
configJsonFactory
=
()
=>
{
factoryStack
.
push
(
"
configJson
"
);
return
JSON
.
parse
(
configJsonSource
);
};
const
loadConfigDeps
=
[
"
require
"
,
"
exports
"
,
"
./config.json
"
];
const
loadConfigFactory
=
(
_require
,
_exports
,
_config
)
=>
{
factoryStack
.
push
(
"
loadConfig
"
);
assertEqual
(
_config
,
JSON
.
parse
(
configJsonSource
));
};
mockDepsStack
.
push
(
configJsonDeps
);
mockFactoryStack
.
push
(
configJsonFactory
);
mockDepsStack
.
push
(
loadConfigDeps
);
mockFactoryStack
.
push
(
loadConfigFactory
);
compilerInstance
.
run
(
"
loadConfig.ts
"
,
"
/root/project
"
);
assertEqual
(
factoryStack
,
[
"
configJson
"
,
"
loadConfig
"
]);
teardown
();
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assertEqual
(
compilerInstance
.
getSource
(
filename
),
fooBarTsSource
);
});
test
(
function
compilerResolveModule
()
{
setup
();
const
moduleMetaData
=
compilerInstance
.
resolveModule
(
"
foo/baz.ts
"
,
"
/root/project
"
);
assertEqual
(
moduleMetaData
.
sourceCode
,
fooBazTsSource
);
assertEqual
(
moduleMetaData
.
outputCode
,
fooBazTsOutput
);
assertEqual
(
JSON
.
stringify
(
moduleMetaData
.
sourceMap
),
fooBazTsSourcemap
);
assert
(
!
moduleMetaData
.
hasRun
);
assert
(
!
moduleMetaData
.
deps
);
assertEqual
(
moduleMetaData
.
exports
,
{});
assertEqual
(
moduleMetaData
.
scriptVersion
,
"
1
"
);
assertEqual
(
codeFetchStack
.
length
,
1
,
"
Only initial module is resolved.
"
);
teardown
();
});
test
(
function
compilerResolveModuleUnknownMediaType
()
{
test
(
function
testGetOutputUnknownMediaType
()
{
setup
();
let
didThrow
=
false
;
try
{
compilerInstance
.
resolveModul
e
(
"
some.txt
"
,
"
/root/project
"
);
compilerInstance
.
getFilenam
e
(
"
some.txt
"
,
"
/root/project
"
);
}
catch
(
e
)
{
assert
(
e
instanceof
Error
);
assertEqual
(
...
...
@@ -577,28 +463,6 @@ test(function compilerResolveModuleUnknownMediaType() {
teardown
();
});
test
(
function
compilerGetModuleDependencies
()
{
setup
();
const
bazDeps
=
[
"
require
"
,
"
exports
"
,
"
./bar.ts
"
];
const
bazFactory
=
()
=>
{
throw
new
Error
(
"
Unexpected factory call
"
);
};
const
barDeps
=
[
"
require
"
,
"
exports
"
,
"
deno
"
];
const
barFactory
=
()
=>
{
throw
new
Error
(
"
Unexpected factory call
"
);
};
mockDepsStack
.
push
(
barDeps
);
mockFactoryStack
.
push
(
barFactory
);
mockDepsStack
.
push
(
bazDeps
);
mockFactoryStack
.
push
(
bazFactory
);
const
deps
=
compilerInstance
.
getModuleDependencies
(
"
foo/baz.ts
"
,
"
/root/project
"
);
assertEqual
(
deps
,
[
"
/root/project/foo/bar.ts
"
,
"
/root/project/foo/baz.ts
"
]);
teardown
();
});
// TypeScript LanguageServiceHost APIs
test
(
function
compilerGetCompilationSettings
()
{
...
...
@@ -626,7 +490,8 @@ test(function compilerGetNewLine() {
test
(
function
compilerGetScriptFileNames
()
{
setup
();
compilerInstance
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
getOutput
(
filename
);
const
result
=
compilerInstance
.
getScriptFileNames
();
assertEqual
(
result
.
length
,
1
,
"
Expected only a single filename.
"
);
assertEqual
(
result
[
0
],
"
/root/project/foo/bar.ts
"
);
...
...
@@ -635,21 +500,22 @@ test(function compilerGetScriptFileNames() {
test
(
function
compilerRecompileFlag
()
{
setup
();
compilerInstance
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
getOutput
(
filename
);
assertEqual
(
getEmitOutputStack
.
length
,
1
,
"
Expected only a single emitted file.
"
);
// running compiler against same file should use cached code
compilerInstance
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
getOutput
(
filename
);
assertEqual
(
getEmitOutputStack
.
length
,
1
,
"
Expected only a single emitted file.
"
);
compilerInstance
.
recompile
=
true
;
compilerInstance
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
getOutput
(
filename
);
assertEqual
(
getEmitOutputStack
.
length
,
2
,
"
Expected two emitted file.
"
);
assert
(
getEmitOutputStack
[
0
]
===
getEmitOutputStack
[
1
],
...
...
@@ -660,43 +526,25 @@ test(function compilerRecompileFlag() {
test
(
function
compilerGetScriptKind
()
{
setup
();
compilerInstance
.
resolveModule
(
"
foo.ts
"
,
"
/moduleKinds
"
);
compilerInstance
.
resolveModule
(
"
foo.d.ts
"
,
"
/moduleKinds
"
);
compilerInstance
.
resolveModule
(
"
foo.js
"
,
"
/moduleKinds
"
);
compilerInstance
.
resolveModule
(
"
foo.json
"
,
"
/moduleKinds
"
);
compilerInstance
.
resolveModule
(
"
foo.txt
"
,
"
/moduleKinds
"
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.ts
"
),
ts
.
ScriptKind
.
TS
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.d.ts
"
),
ts
.
ScriptKind
.
TS
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.js
"
),
ts
.
ScriptKind
.
JS
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.json
"
),
ts
.
ScriptKind
.
JSON
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.txt
"
),
ts
.
ScriptKind
.
JS
);
compilerInstance
.
getFilename
(
"
foo.ts
"
,
"
/moduleKinds
"
);
compilerInstance
.
getFilename
(
"
foo.d.ts
"
,
"
/moduleKinds
"
);
compilerInstance
.
getFilename
(
"
foo.js
"
,
"
/moduleKinds
"
);
compilerInstance
.
getFilename
(
"
foo.json
"
,
"
/moduleKinds
"
);
compilerInstance
.
getFilename
(
"
foo.txt
"
,
"
/moduleKinds
"
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.ts
"
),
3
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.d.ts
"
),
3
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.js
"
),
1
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.json
"
),
6
);
assertEqual
(
compilerInstance
.
getScriptKind
(
"
/moduleKinds/foo.txt
"
),
1
);
teardown
();
});
test
(
function
compilerGetScriptVersion
()
{
setup
();
const
moduleMetaData
=
compilerInstance
.
resolveModule
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
compile
(
moduleMetaData
);
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
compilerInstance
.
getOutput
(
filename
);
assertEqual
(
compilerInstance
.
getScriptVersion
(
moduleMetaData
.
fileN
ame
),
compilerInstance
.
getScriptVersion
(
filen
ame
),
"
1
"
,
"
Expected known module to have script version of 1
"
);
...
...
@@ -713,11 +561,8 @@ test(function compilerGetScriptVersionUnknown() {
test
(
function
compilerGetScriptSnapshot
()
{
setup
();
const
moduleMetaData
=
compilerInstance
.
resolveModule
(
"
foo/bar.ts
"
,
"
/root/project
"
);
const
result
=
compilerInstance
.
getScriptSnapshot
(
moduleMetaData
.
fileName
);
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
const
result
=
compilerInstance
.
getScriptSnapshot
(
filename
);
assert
(
result
!=
null
,
"
Expected snapshot to be defined.
"
);
assertEqual
(
result
.
getLength
(),
fooBarTsSource
.
length
);
assertEqual
(
...
...
@@ -729,11 +574,6 @@ test(function compilerGetScriptSnapshot() {
// This is and optional part of the `IScriptSnapshot` API which we don't
// define, os checking for the lack of this property.
assert
(
!
(
"
dispose
"
in
result
));
assert
(
result
===
moduleMetaData
,
"
result should strictly equal moduleMetaData
"
);
teardown
();
});
...
...
@@ -767,11 +607,8 @@ test(function compilerReadFile() {
test
(
function
compilerFileExists
()
{
setup
();
const
moduleMetaData
=
compilerInstance
.
resolveModule
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assert
(
compilerInstance
.
fileExists
(
moduleMetaData
.
fileName
));
const
filename
=
compilerInstance
.
getFilename
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assert
(
compilerInstance
.
fileExists
(
filename
));
assert
(
compilerInstance
.
fileExists
(
"
$asset$/lib.deno_runtime.d.ts
"
));
assertEqual
(
compilerInstance
.
fileExists
(
"
/root/project/unknown-module.ts
"
),
...
...
js/deno.ts
浏览文件 @
3d03f5b0
...
...
@@ -48,3 +48,6 @@ export const args: string[] = [];
import
*
as
compiler
from
"
./compiler
"
;
// @internal
export
const
_compiler
=
compiler
;
import
*
as
runner
from
"
./runner
"
;
// @internal
export
const
_runner
=
runner
;
js/main.ts
浏览文件 @
3d03f5b0
...
...
@@ -6,7 +6,8 @@ import * as flatbuffers from "./flatbuffers";
import
*
as
msg
from
"
gen/msg_generated
"
;
import
{
assert
,
log
,
setLogDebug
}
from
"
./util
"
;
import
*
as
os
from
"
./os
"
;
import
{
DenoCompiler
}
from
"
./compiler
"
;
import
{
Compiler
}
from
"
./compiler
"
;
import
{
Runner
}
from
"
./runner
"
;
import
{
libdeno
}
from
"
./libdeno
"
;
import
{
args
}
from
"
./deno
"
;
import
{
sendSync
,
handleAsyncMsgFromRust
}
from
"
./dispatch
"
;
...
...
@@ -17,7 +18,7 @@ import { version } from "typescript";
// Install the source maps handler and do some pre-calculations so all of it is
// available in the snapshot
const
compiler
=
Deno
Compiler
.
instance
();
const
compiler
=
Compiler
.
instance
();
sourceMaps
.
install
({
installPrepareStackTrace
:
true
,
getGeneratedContents
:
compiler
.
getGeneratedContents
...
...
@@ -70,8 +71,7 @@ export default function denoMain() {
// handle `--types`
if
(
startResMsg
.
typesFlag
())
{
const
defaultLibFileName
=
compiler
.
getDefaultLibFileName
();
const
defaultLibModule
=
compiler
.
resolveModule
(
defaultLibFileName
,
""
);
console
.
log
(
defaultLibModule
.
sourceCode
);
console
.
log
(
compiler
.
getSource
(
defaultLibFileName
));
os
.
exit
(
0
);
}
...
...
@@ -94,9 +94,10 @@ export default function denoMain() {
const
inputFn
=
args
[
0
];
compiler
.
recompile
=
startResMsg
.
recompileFlag
();
const
runner
=
new
Runner
(
compiler
);
if
(
inputFn
)
{
compil
er
.
run
(
inputFn
,
`
${
cwd
}
/`
);
runn
er
.
run
(
inputFn
,
`
${
cwd
}
/`
);
}
else
{
replLoop
();
}
...
...
js/runner.ts
0 → 100644
浏览文件 @
3d03f5b0
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// tslint:disable-next-line:no-circular-imports
import
*
as
deno
from
"
./deno
"
;
import
{
globalEval
}
from
"
./global_eval
"
;
import
{
assert
,
log
}
from
"
./util
"
;
// tslint:disable:no-any
type
AmdCallback
=
(...
args
:
any
[])
=>
void
;
type
AmdDefine
=
(
deps
:
ModuleSpecifier
[],
factory
:
AmdFactory
)
=>
void
;
type
AmdErrback
=
(
err
:
any
)
=>
void
;
type
AmdFactory
=
(...
args
:
any
[])
=>
object
|
void
;
type
AmdRequire
=
(
deps
:
ModuleSpecifier
[],
callback
:
AmdCallback
,
errback
:
AmdErrback
)
=>
void
;
// tslint:enable:no-any
// tslint:disable-next-line:no-any
type
BuiltinMap
=
{
[
moduleSpecifier
:
string
]:
any
};
// Type aliases to make the code more readable
type
ContainingFile
=
string
;
type
Filename
=
string
;
type
ModuleSpecifier
=
string
;
type
OutputCode
=
string
;
/** Internal representation of a module being loaded */
class
Module
{
deps
?:
Filename
[];
factory
?:
AmdFactory
;
// tslint:disable-next-line:no-any
constructor
(
public
filename
:
Filename
,
public
exports
:
any
)
{}
}
/** External APIs which the runner depends upon to be able to retrieve
* transpiled modules.
*/
export
interface
CodeProvider
{
/** Given a module specifier and a containing file, return the filename. */
getFilename
(
moduleSpecifier
:
ModuleSpecifier
,
containingFile
:
ContainingFile
):
Filename
;
/** Given a filename, return the transpiled output code. */
getOutput
(
filename
:
Filename
):
OutputCode
;
}
const
window
=
globalEval
(
"
this
"
);
/** A class which can load and run modules into the current environment. */
export
class
Runner
{
private
_globalEval
=
globalEval
;
/** A map of modules indexed by filename. */
private
_modules
=
new
Map
<
Filename
,
Module
>
();
private
_provider
:
CodeProvider
;
/** Modules are placed in here to have their factories run after all the
* the dependencies have been collected.
*/
private
_runQueue
:
Module
[]
=
[];
private
_drainRunQueue
():
void
{
log
(
"
runner._drainRunQueue
"
,
this
.
_runQueue
.
length
);
let
module
:
Module
|
undefined
;
while
((
module
=
this
.
_runQueue
.
shift
()))
{
assert
(
module
.
factory
!=
null
,
"
Cannot run module without factory.
"
);
// TypeScript always imports `exports` and mutates it directly, but the
// AMD specification allows values to be returned from the factory and
// is the case with JSON modules and potentially other future features.
const
exports
=
module
.
factory
!
(...
this
.
_getFactoryArguments
(
module
));
if
(
exports
!=
null
)
{
module
.
exports
=
exports
;
}
}
}
private
_gatherDependencies
(
filename
:
Filename
):
void
{
log
(
"
runner._gatherDependencies
"
,
filename
);
if
(
this
.
_modules
.
has
(
filename
))
{
log
(
"
Module already exists:
"
,
filename
);
return
;
}
const
module
=
new
Module
(
filename
,
{});
this
.
_modules
.
set
(
filename
,
module
);
window
.
define
=
this
.
_makeDefine
(
module
);
this
.
_globalEval
(
this
.
_provider
.
getOutput
(
filename
));
window
.
define
=
undefined
;
}
// tslint:disable-next-line:no-any
private
_getFactoryArguments
(
module
:
Module
):
any
[]
{
log
(
"
runner._getFactoryArguments
"
,
module
.
filename
);
assert
(
module
.
deps
!=
null
,
"
Missing dependencies for module.
"
);
return
module
.
deps
!
.
map
(
dep
=>
{
if
(
dep
===
"
require
"
)
{
return
this
.
_makeLocalRequire
(
module
);
}
if
(
dep
===
"
exports
"
)
{
return
module
.
exports
;
}
if
(
dep
in
Runner
.
_builtins
)
{
return
Runner
.
_builtins
[
dep
];
}
const
depModule
=
this
.
_modules
.
get
(
dep
)
!
;
assert
(
dep
!=
null
,
`Missing dependency "
${
dep
}
"`
);
return
depModule
.
exports
;
});
}
private
_makeDefine
(
module
:
Module
):
AmdDefine
{
log
(
"
runner._makeDefine
"
,
module
.
filename
);
return
(
deps
:
ModuleSpecifier
[],
factory
:
AmdFactory
):
void
=>
{
module
.
factory
=
factory
;
module
.
deps
=
deps
.
map
(
dep
=>
{
if
(
dep
===
"
require
"
||
dep
===
"
exports
"
||
dep
in
Runner
.
_builtins
)
{
return
dep
;
}
const
depFilename
=
this
.
_provider
.
getFilename
(
dep
,
module
.
filename
);
if
(
!
this
.
_modules
.
get
(
depFilename
))
{
this
.
_gatherDependencies
(
depFilename
);
}
return
depFilename
;
});
if
(
!
this
.
_runQueue
.
includes
(
module
))
{
this
.
_runQueue
.
push
(
module
);
}
};
}
private
_makeLocalRequire
(
module
:
Module
):
AmdRequire
{
log
(
"
runner._makeLocalRequire
"
,
module
.
filename
);
return
(
deps
:
ModuleSpecifier
[],
callback
:
AmdCallback
,
errback
:
AmdErrback
):
void
=>
{
log
(
"
runner._makeLocalRequire
"
,
deps
);
assert
(
deps
.
length
===
1
,
"
Local require supports exactly one dependency.
"
);
const
[
moduleSpecifier
]
=
deps
;
try
{
this
.
run
(
moduleSpecifier
,
module
.
filename
);
const
requiredFilename
=
this
.
_provider
.
getFilename
(
moduleSpecifier
,
module
.
filename
);
const
requiredModule
=
this
.
_modules
.
get
(
requiredFilename
)
!
;
assert
(
requiredModule
!=
null
);
callback
(
requiredModule
.
exports
);
}
catch
(
e
)
{
errback
(
e
);
}
};
}
constructor
(
provider
:
CodeProvider
)
{
this
.
_provider
=
provider
;
}
/** Given a module specifier and the containing file, resolve the module and
* ensure that it is in the runtime environment, returning the exports of the
* module.
*/
// tslint:disable-next-line:no-any
run
(
moduleSpecifier
:
ModuleSpecifier
,
containingFile
:
ContainingFile
):
any
{
log
(
"
runner.run
"
,
moduleSpecifier
,
containingFile
);
const
filename
=
this
.
_provider
.
getFilename
(
moduleSpecifier
,
containingFile
);
if
(
!
this
.
_modules
.
has
(
filename
))
{
this
.
_gatherDependencies
(
filename
);
this
.
_drainRunQueue
();
}
return
this
.
_modules
.
get
(
filename
)
!
.
exports
;
}
/** Builtin modules which can be loaded by user modules. */
private
static
_builtins
:
BuiltinMap
=
{
deno
};
}
js/runner_test.ts
0 → 100644
浏览文件 @
3d03f5b0
import
{
test
,
assert
,
assertEqual
}
from
"
./test_util
"
;
import
*
as
deno
from
"
deno
"
;
// tslint:disable-next-line:no-any
const
Runner
=
(
deno
as
any
).
_runner
.
Runner
;
let
mockGetOutputStack
:
string
[]
=
[];
let
mockGetFilenameStack
:
Array
<
[
string
,
string
]
>
=
[];
// tslint:disable:max-line-length
const
mockOutput
=
{
"
/root/project/foo/bar.ts
"
:
`define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = "bar";
});
//# sourceMappingURL=bar.js.map
//# sourceURL=/root/project/foo/bar.ts`
,
"
/root/project/foo/baz.ts
"
:
`define(["require", "exports", "./qat.ts"], function (require, exports, qat) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = qat.foo;
});
//# sourceMappingURL=baz.js.map
//# sourceURL=/root/project/foo/baz.ts`
,
"
/root/project/foo/qat.ts
"
:
`define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = "qat";
});
//# sourceMappingURL=qat.js.map
//# sourceURL=/root/project/foo/qat.ts`
,
"
/root/project/foo/config.json
"
:
`define([], function () {
return JSON.parse('{"foo":{"bar": true,"baz": ["qat", 1]}}');
});
//# sourceURL=/root/project/foo/config.json`
,
"
/circular/modA.ts
"
:
`define(["require", "exports", "./modB.ts"], function (require, exports, modB) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = modB.foo;
});
//# sourceMappingURL=modA.js.map
//# sourceURL=/circular/modA.ts`
,
"
/circular/modB.ts
"
:
`define(["require", "exports", "./modA.ts"], function (require, exports, modA) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
if (modA && typeof modA === "object") {
modA;
} else {
throw new Error("Mod A is empty!");
}
exports.foo = "bar";
});
//# sourceMappingURL=modB.js.map
//# sourceURL=/circular/modB.ts`
};
// tslint:enable
const
mockFilenames
=
{
"
/root/project
"
:
{
"
foo/bar.ts
"
:
"
/root/project/foo/bar.ts
"
,
"
foo/baz.ts
"
:
"
/root/project/foo/baz.ts
"
,
"
foo/config.json
"
:
"
/root/project/foo/config.json
"
},
"
/root/project/foo/baz.ts
"
:
{
"
./qat.ts
"
:
"
/root/project/foo/qat.ts
"
},
"
/circular
"
:
{
"
modA.ts
"
:
"
/circular/modA.ts
"
},
"
/circular/modA.ts
"
:
{
"
./modB.ts
"
:
"
/circular/modB.ts
"
},
"
/circular/modB.ts
"
:
{
"
./modA.ts
"
:
"
/circular/modA.ts
"
}
};
const
mockCodeProvider
=
{
getOutput
(
filename
:
string
)
{
mockGetOutputStack
.
push
(
filename
);
if
(
filename
in
mockOutput
)
{
return
mockOutput
[
filename
];
}
throw
new
Error
(
"
Module not found.
"
);
},
getFilename
(
moduleSpecifier
:
string
,
containingFile
:
string
)
{
mockGetFilenameStack
.
push
([
moduleSpecifier
,
containingFile
]);
if
(
containingFile
in
mockFilenames
&&
moduleSpecifier
in
mockFilenames
[
containingFile
]
)
{
return
mockFilenames
[
containingFile
][
moduleSpecifier
];
}
}
};
function
setup
()
{
mockGetOutputStack
=
[];
mockGetFilenameStack
=
[];
return
new
Runner
(
mockCodeProvider
);
}
test
(
function
runnerConstruction
()
{
const
runner
=
setup
();
assert
(
runner
);
});
test
(
function
runnerRun
()
{
const
runner
=
setup
();
const
result
=
runner
.
run
(
"
foo/bar.ts
"
,
"
/root/project
"
);
assertEqual
(
result
,
{
foo
:
"
bar
"
});
assertEqual
(
mockGetFilenameStack
,
[[
"
foo/bar.ts
"
,
"
/root/project
"
]]);
assertEqual
(
mockGetOutputStack
,
[
"
/root/project/foo/bar.ts
"
]);
});
test
(
function
runnerRunImports
()
{
const
runner
=
setup
();
const
result
=
runner
.
run
(
"
foo/baz.ts
"
,
"
/root/project
"
);
assertEqual
(
result
,
{
foo
:
"
qat
"
});
assertEqual
(
mockGetFilenameStack
,
[
[
"
foo/baz.ts
"
,
"
/root/project
"
],
[
"
./qat.ts
"
,
"
/root/project/foo/baz.ts
"
]
]);
assertEqual
(
mockGetOutputStack
,
[
"
/root/project/foo/baz.ts
"
,
"
/root/project/foo/qat.ts
"
]);
});
test
(
function
runnerRunExportReturn
()
{
const
runner
=
setup
();
const
result
=
runner
.
run
(
"
foo/config.json
"
,
"
/root/project
"
);
assertEqual
(
result
,
{
foo
:
{
bar
:
true
,
baz
:
[
"
qat
"
,
1
]
}
});
});
test
(
function
runnerCircularReference
()
{
const
runner
=
setup
();
const
result
=
runner
.
run
(
"
modA.ts
"
,
"
/circular
"
);
assertEqual
(
result
,
{
foo
:
"
bar
"
});
});
js/unit_tests.ts
浏览文件 @
3d03f5b0
...
...
@@ -28,6 +28,7 @@ import "./read_file_test.ts";
import
"
./read_link_test.ts
"
;
import
"
./rename_test.ts
"
;
import
"
./resources_test.ts
"
;
import
"
./runner_test.ts
"
;
import
"
./stat_test.ts
"
;
import
"
./symlink_test.ts
"
;
import
"
./text_encoding_test.ts
"
;
...
...
tests/async_error.ts.out
浏览文件 @
3d03f5b0
...
...
@@ -4,8 +4,8 @@ world
Error: error
at foo ([WILDCARD]tests/async_error.ts:4:9)
at eval ([WILDCARD]tests/async_error.ts:7:1)
at
DenoCompil
er.eval [as _globalEval] (<anonymous>)
at
DenoCompiler._gatherDependencies ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at
DenoCompiler.run ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at
Runn
er.eval [as _globalEval] (<anonymous>)
at
Runner._gatherDependencies ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at
Runner.run ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
at deno_main.js:1:1
tests/error_001.ts.out
浏览文件 @
3d03f5b0
...
...
@@ -2,8 +2,8 @@ Error: bad
at foo (file://[WILDCARD]tests/error_001.ts:2:9)
at bar (file://[WILDCARD]tests/error_001.ts:6:3)
at eval (file://[WILDCARD]tests/error_001.ts:9:1)
at
DenoCompil
er.eval [as _globalEval] (<anonymous>)
at
DenoCompiler._gatherDependencies ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at
DenoCompiler.run ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at
Runn
er.eval [as _globalEval] (<anonymous>)
at
Runner._gatherDependencies ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at
Runner.run ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
at deno_main.js:1:1
tests/error_002.ts.out
浏览文件 @
3d03f5b0
Error: exception from mod1
at Object.throwsError (file://[WILDCARD]/tests/subdir/mod1.ts:16:9)
at foo (file://[WILDCARD]/tests/error_002.ts:4:3)
at Module
MetaData
.eval [as factory ] (file://[WILDCARD]/tests/error_002.ts:7:1)
at
DenoCompiler._drainRunQueue ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at
DenoCompiler.run ([WILDCARD]/js/compil
er.ts:[WILDCARD])
at Module.eval [as factory ] (file://[WILDCARD]/tests/error_002.ts:7:1)
at
Runner._drainRunQueue ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at
Runner.run ([WILDCARD]/js/runn
er.ts:[WILDCARD])
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
at deno_main.js:1:1
tests/error_004_missing_module.ts.out
浏览文件 @
3d03f5b0
...
...
@@ -3,9 +3,9 @@ NotFound: Cannot resolve module "bad-module.ts" from "[WILDCARD]/tests/error_004
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
at Object.codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
at
DenoCompiler.
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at
Compiler._
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Array.map (<anonymous>)
at
Deno
Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Object.compilerHost.resolveModuleNames (<anonymous>)
at resolveModuleNamesWorker (<anonymous>)
tests/error_005_missing_dynamic_import.ts.out
浏览文件 @
3d03f5b0
...
...
@@ -3,9 +3,9 @@ NotFound: Cannot resolve module "bad-module.ts" from "[WILDCARD]/tests/error_005
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
at Object.codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
at
DenoCompiler.
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at
Compiler._
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Array.map (<anonymous>)
at
Deno
Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Object.compilerHost.resolveModuleNames (<anonymous>)
at resolveModuleNamesWorker (<anonymous>)
tests/error_006_import_ext_failure.ts.out
浏览文件 @
3d03f5b0
...
...
@@ -3,9 +3,9 @@ NotFound: Cannot resolve module "./non-existent" from "[WILDCARD]/tests/error_00
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
at Object.codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
at
DenoCompiler.
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at
Compiler._
resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Array.map (<anonymous>)
at
Deno
Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Compiler.resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
at Object.compilerHost.resolveModuleNames (<anonymous>)
at resolveModuleNamesWorker (<anonymous>)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录