Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
d9c84eb9
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,发现更多精彩内容 >>
未验证
提交
d9c84eb9
编写于
2月 06, 2020
作者:
R
Ryan Dahl
提交者:
GitHub
2月 06, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename ThreadSafeGlobalState to GlobalState (#3907)
simplify
上级
522e8563
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
59 addition
and
79 deletion
+59
-79
cli/compilers/ts.rs
cli/compilers/ts.rs
+11
-13
cli/compilers/wasm.rs
cli/compilers/wasm.rs
+3
-3
cli/global_state.rs
cli/global_state.rs
+31
-28
cli/lib.rs
cli/lib.rs
+5
-20
cli/state.rs
cli/state.rs
+5
-5
cli/worker.rs
cli/worker.rs
+4
-10
未找到文件。
cli/compilers/ts.rs
浏览文件 @
d9c84eb9
...
...
@@ -6,7 +6,7 @@ use crate::diagnostics::Diagnostic;
use
crate
::
disk_cache
::
DiskCache
;
use
crate
::
file_fetcher
::
SourceFile
;
use
crate
::
file_fetcher
::
SourceFileFetcher
;
use
crate
::
global_state
::
ThreadSafe
GlobalState
;
use
crate
::
global_state
::
GlobalState
;
use
crate
::
msg
;
use
crate
::
ops
::
JsonResult
;
use
crate
::
source_maps
::
SourceMapGetter
;
...
...
@@ -246,7 +246,7 @@ impl TsCompiler {
/// Create a new V8 worker with snapshot of TS compiler and setup compiler's
/// runtime.
fn
setup_worker
(
global_state
:
ThreadSafe
GlobalState
)
->
CompilerWorker
{
fn
setup_worker
(
global_state
:
GlobalState
)
->
CompilerWorker
{
let
entry_point
=
ModuleSpecifier
::
resolve_url_or_path
(
"./__$deno$ts_compiler.ts"
)
.unwrap
();
let
worker_state
=
...
...
@@ -270,7 +270,7 @@ impl TsCompiler {
pub
async
fn
bundle_async
(
&
self
,
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
module_name
:
String
,
out_file
:
Option
<
String
>
,
)
->
Result
<
(),
ErrBox
>
{
...
...
@@ -325,7 +325,7 @@ impl TsCompiler {
/// compiler.
pub
async
fn
compile_async
(
&
self
,
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
source_file
:
&
SourceFile
,
target
:
TargetLib
,
)
->
Result
<
CompiledModule
,
ErrBox
>
{
...
...
@@ -604,7 +604,7 @@ impl TsCompiler {
}
async
fn
execute_in_thread
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
req
:
Buf
,
)
->
Result
<
Option
<
Buf
>
,
ErrBox
>
{
let
(
load_sender
,
load_receiver
)
=
...
...
@@ -638,7 +638,7 @@ async fn execute_in_thread(
async
fn
execute_in_thread_json
(
req_msg
:
Buf
,
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
)
->
JsonResult
{
let
maybe_msg
=
execute_in_thread
(
global_state
,
req_msg
)
.await
?
;
let
msg
=
maybe_msg
.unwrap
();
...
...
@@ -647,7 +647,7 @@ async fn execute_in_thread_json(
}
pub
fn
runtime_compile_async
<
S
:
BuildHasher
>
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
root_name
:
&
str
,
sources
:
&
Option
<
HashMap
<
String
,
String
,
S
>>
,
bundle
:
bool
,
...
...
@@ -669,7 +669,7 @@ pub fn runtime_compile_async<S: BuildHasher>(
}
pub
fn
runtime_transpile_async
<
S
:
BuildHasher
>
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
sources
:
&
HashMap
<
String
,
String
,
S
>
,
options
:
&
Option
<
String
>
,
)
->
Pin
<
Box
<
CompilationResultFuture
>>
{
...
...
@@ -708,10 +708,8 @@ mod tests {
source_code
:
include_bytes!
(
"../tests/002_hello.ts"
)
.to_vec
(),
types_url
:
None
,
};
let
mock_state
=
ThreadSafeGlobalState
::
mock
(
vec!
[
String
::
from
(
"deno"
),
String
::
from
(
"hello.js"
),
]);
let
mock_state
=
GlobalState
::
mock
(
vec!
[
String
::
from
(
"deno"
),
String
::
from
(
"hello.js"
)]);
let
result
=
mock_state
.ts_compiler
.compile_async
(
mock_state
.clone
(),
&
out
,
TargetLib
::
Main
)
...
...
@@ -735,7 +733,7 @@ mod tests {
.unwrap
()
.to_string
();
let
state
=
ThreadSafe
GlobalState
::
mock
(
vec!
[
let
state
=
GlobalState
::
mock
(
vec!
[
String
::
from
(
"deno"
),
p
.to_string_lossy
()
.into
(),
String
::
from
(
"$deno$/bundle.js"
),
...
...
cli/compilers/wasm.rs
浏览文件 @
d9c84eb9
...
...
@@ -2,7 +2,7 @@
use
super
::
compiler_worker
::
CompilerWorker
;
use
crate
::
compilers
::
CompiledModule
;
use
crate
::
file_fetcher
::
SourceFile
;
use
crate
::
global_state
::
ThreadSafe
GlobalState
;
use
crate
::
global_state
::
GlobalState
;
use
crate
::
startup_data
;
use
crate
::
state
::
*
;
use
deno_core
::
ErrBox
;
...
...
@@ -48,7 +48,7 @@ pub struct WasmCompiler {
impl
WasmCompiler
{
/// Create a new V8 worker with snapshot of WASM compiler and setup compiler's runtime.
fn
setup_worker
(
global_state
:
ThreadSafe
GlobalState
)
->
CompilerWorker
{
fn
setup_worker
(
global_state
:
GlobalState
)
->
CompilerWorker
{
let
entry_point
=
ModuleSpecifier
::
resolve_url_or_path
(
"./__$deno$wasm_compiler.ts"
)
.unwrap
();
...
...
@@ -73,7 +73,7 @@ impl WasmCompiler {
pub
async
fn
compile_async
(
&
self
,
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
source_file
:
&
SourceFile
,
)
->
Result
<
CompiledModule
,
ErrBox
>
{
let
cache
=
self
.cache
.clone
();
...
...
cli/global_state.rs
浏览文件 @
d9c84eb9
...
...
@@ -14,6 +14,7 @@ use crate::metrics::Metrics;
use
crate
::
msg
;
use
crate
::
permissions
::
DenoPermissions
;
use
crate
::
progress
::
Progress
;
use
crate
::
shell
::
Shell
;
use
deno_core
::
ErrBox
;
use
deno_core
::
ModuleSpecifier
;
use
std
;
...
...
@@ -27,13 +28,12 @@ use tokio::sync::Mutex as AsyncMutex;
/// Holds state of the program and can be accessed by V8 isolate.
#[derive(Clone)]
pub
struct
ThreadSafeGlobalState
(
Arc
<
GlobalState
>
);
pub
struct
GlobalState
(
Arc
<
GlobalStateInner
>
);
/// This structure represents state of single "deno" program.
///
/// It is shared by all created workers (thus V8 isolates).
#[cfg_attr(feature
=
"cargo-clippy"
,
allow(stutter))]
pub
struct
GlobalState
{
pub
struct
GlobalStateInner
{
/// Flags parsed from `argv` contents.
pub
flags
:
flags
::
DenoFlags
,
/// Permissions parsed from `flags`.
...
...
@@ -50,21 +50,30 @@ pub struct GlobalState {
compile_lock
:
AsyncMutex
<
()
>
,
}
impl
Deref
for
ThreadSafe
GlobalState
{
type
Target
=
Arc
<
GlobalState
>
;
impl
Deref
for
GlobalState
{
type
Target
=
GlobalStateInner
;
fn
deref
(
&
self
)
->
&
Self
::
Target
{
&
self
.0
}
}
impl
ThreadSafeGlobalState
{
pub
fn
new
(
flags
:
flags
::
DenoFlags
,
progress
:
Progress
,
)
->
Result
<
Self
,
ErrBox
>
{
impl
GlobalState
{
pub
fn
new
(
flags
:
flags
::
DenoFlags
)
->
Result
<
Self
,
ErrBox
>
{
let
custom_root
=
env
::
var
(
"DENO_DIR"
)
.map
(
String
::
into
)
.ok
();
let
dir
=
deno_dir
::
DenoDir
::
new
(
custom_root
)
?
;
// TODO(ry) Shell is a useless abstraction and should be removed at
// some point.
let
shell
=
Arc
::
new
(
Mutex
::
new
(
Shell
::
new
()));
let
progress
=
Progress
::
new
();
progress
.set_callback
(
move
|
_
done
,
_
completed
,
_
total
,
status
,
msg
|
{
if
!
status
.is_empty
()
{
let
mut
s
=
shell
.lock
()
.unwrap
();
s
.status
(
status
,
msg
)
.expect
(
"shell problem"
);
}
});
let
file_fetcher
=
SourceFileFetcher
::
new
(
dir
.deps_cache
.clone
(),
progress
.clone
(),
...
...
@@ -88,7 +97,7 @@ impl ThreadSafeGlobalState {
None
};
let
state
=
GlobalState
{
let
inner
=
GlobalStateInner
{
dir
,
permissions
:
DenoPermissions
::
from_flags
(
&
flags
),
flags
,
...
...
@@ -103,7 +112,7 @@ impl ThreadSafeGlobalState {
compile_lock
:
AsyncMutex
::
new
(()),
};
Ok
(
ThreadSafeGlobalState
(
Arc
::
new
(
state
)))
Ok
(
GlobalState
(
Arc
::
new
(
inner
)))
}
pub
async
fn
fetch_compiled_module
(
...
...
@@ -231,14 +240,11 @@ impl ThreadSafeGlobalState {
}
#[cfg(test)]
pub
fn
mock
(
argv
:
Vec
<
String
>
)
->
ThreadSafeGlobalState
{
ThreadSafeGlobalState
::
new
(
flags
::
DenoFlags
{
argv
,
..
flags
::
DenoFlags
::
default
()
},
Progress
::
new
(),
)
pub
fn
mock
(
argv
:
Vec
<
String
>
)
->
GlobalState
{
GlobalState
::
new
(
flags
::
DenoFlags
{
argv
,
..
flags
::
DenoFlags
::
default
()
})
.unwrap
()
}
}
...
...
@@ -246,7 +252,7 @@ impl ThreadSafeGlobalState {
#[test]
fn
thread_safe
()
{
fn
f
<
S
:
Send
+
Sync
>
(
_
:
S
)
{}
f
(
ThreadSafe
GlobalState
::
mock
(
vec!
[
f
(
GlobalState
::
mock
(
vec!
[
String
::
from
(
"./deno"
),
String
::
from
(
"hello.js"
),
]));
...
...
@@ -254,11 +260,8 @@ fn thread_safe() {
#[test]
fn
import_map_given_for_repl
()
{
let
_
result
=
ThreadSafeGlobalState
::
new
(
flags
::
DenoFlags
{
import_map_path
:
Some
(
"import_map.json"
.to_string
()),
..
flags
::
DenoFlags
::
default
()
},
Progress
::
new
(),
);
let
_
result
=
GlobalState
::
new
(
flags
::
DenoFlags
{
import_map_path
:
Some
(
"import_map.json"
.to_string
()),
..
flags
::
DenoFlags
::
default
()
});
}
cli/lib.rs
浏览文件 @
d9c84eb9
...
...
@@ -59,9 +59,8 @@ pub mod worker;
use
crate
::
compilers
::
TargetLib
;
use
crate
::
deno_error
::
js_check
;
use
crate
::
deno_error
::{
print_err_and_exit
,
print_msg_and_exit
};
use
crate
::
global_state
::
ThreadSafe
GlobalState
;
use
crate
::
global_state
::
GlobalState
;
use
crate
::
ops
::
io
::
get_stdio
;
use
crate
::
progress
::
Progress
;
use
crate
::
state
::
ThreadSafeState
;
use
crate
::
worker
::
MainWorker
;
use
deno_core
::
v8_set_flags
;
...
...
@@ -98,28 +97,14 @@ impl log::Log for Logger {
fn
flush
(
&
self
)
{}
}
fn
create_global_state
(
flags
:
DenoFlags
)
->
ThreadSafeGlobalState
{
use
crate
::
shell
::
Shell
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Mutex
;
let
shell
=
Arc
::
new
(
Mutex
::
new
(
Shell
::
new
()));
let
progress
=
Progress
::
new
();
progress
.set_callback
(
move
|
_
done
,
_
completed
,
_
total
,
status
,
msg
|
{
if
!
status
.is_empty
()
{
let
mut
s
=
shell
.lock
()
.unwrap
();
s
.status
(
status
,
msg
)
.expect
(
"shell problem"
);
}
});
ThreadSafeGlobalState
::
new
(
flags
,
progress
)
fn
create_global_state
(
flags
:
DenoFlags
)
->
GlobalState
{
GlobalState
::
new
(
flags
)
.map_err
(
deno_error
::
print_err_and_exit
)
.unwrap
()
}
fn
create_main_worker
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
main_module
:
ModuleSpecifier
,
)
->
MainWorker
{
let
state
=
ThreadSafeState
::
new
(
global_state
,
None
,
main_module
)
...
...
@@ -147,7 +132,7 @@ fn types_command() {
);
}
fn
print_cache_info
(
state
:
&
ThreadSafe
GlobalState
)
{
fn
print_cache_info
(
state
:
&
GlobalState
)
{
println!
(
"{} {:?}"
,
colors
::
bold
(
"DENO_DIR location:"
.to_string
()),
...
...
cli/state.rs
浏览文件 @
d9c84eb9
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use
crate
::
compilers
::
TargetLib
;
use
crate
::
deno_error
::
permission_denied
;
use
crate
::
global_state
::
ThreadSafe
GlobalState
;
use
crate
::
global_state
::
GlobalState
;
use
crate
::
global_timer
::
GlobalTimer
;
use
crate
::
import_map
::
ImportMap
;
use
crate
::
metrics
::
Metrics
;
...
...
@@ -45,7 +45,7 @@ pub struct ThreadSafeState(Arc<State>);
#[cfg_attr(feature
=
"cargo-clippy"
,
allow(stutter))]
pub
struct
State
{
pub
global_state
:
ThreadSafe
GlobalState
,
pub
global_state
:
GlobalState
,
pub
permissions
:
Arc
<
Mutex
<
DenoPermissions
>>
,
pub
main_module
:
ModuleSpecifier
,
/// When flags contains a `.import_map_path` option, the content of the
...
...
@@ -223,7 +223,7 @@ impl Loader for ThreadSafeState {
impl
ThreadSafeState
{
/// If `shared_permission` is None then permissions from globa state are used.
pub
fn
new
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
shared_permissions
:
Option
<
Arc
<
Mutex
<
DenoPermissions
>>>
,
main_module
:
ModuleSpecifier
,
)
->
Result
<
Self
,
ErrBox
>
{
...
...
@@ -267,7 +267,7 @@ impl ThreadSafeState {
/// If `shared_permission` is None then permissions from globa state are used.
pub
fn
new_for_worker
(
global_state
:
ThreadSafe
GlobalState
,
global_state
:
GlobalState
,
shared_permissions
:
Option
<
Arc
<
Mutex
<
DenoPermissions
>>>
,
main_module
:
ModuleSpecifier
,
)
->
Result
<
Self
,
ErrBox
>
{
...
...
@@ -375,7 +375,7 @@ impl ThreadSafeState {
let
module_specifier
=
ModuleSpecifier
::
resolve_url_or_path
(
main_module
)
.expect
(
"Invalid entry module"
);
ThreadSafeState
::
new
(
ThreadSafe
GlobalState
::
mock
(
vec!
[
"deno"
.to_string
()]),
GlobalState
::
mock
(
vec!
[
"deno"
.to_string
()]),
None
,
module_specifier
,
)
...
...
cli/worker.rs
浏览文件 @
d9c84eb9
...
...
@@ -255,8 +255,7 @@ impl DerefMut for MainWorker {
mod
tests
{
use
super
::
*
;
use
crate
::
flags
;
use
crate
::
global_state
::
ThreadSafeGlobalState
;
use
crate
::
progress
::
Progress
;
use
crate
::
global_state
::
GlobalState
;
use
crate
::
startup_data
;
use
crate
::
state
::
ThreadSafeState
;
use
crate
::
tokio_util
;
...
...
@@ -279,9 +278,7 @@ mod tests {
.join
(
"cli/tests/esm_imports_a.js"
);
let
module_specifier
=
ModuleSpecifier
::
resolve_url_or_path
(
&
p
.to_string_lossy
())
.unwrap
();
let
global_state
=
ThreadSafeGlobalState
::
new
(
flags
::
DenoFlags
::
default
(),
Progress
::
new
())
.unwrap
();
let
global_state
=
GlobalState
::
new
(
flags
::
DenoFlags
::
default
())
.unwrap
();
let
state
=
ThreadSafeState
::
new
(
global_state
,
None
,
module_specifier
.clone
())
.unwrap
();
...
...
@@ -314,9 +311,7 @@ mod tests {
.join
(
"tests/circular1.ts"
);
let
module_specifier
=
ModuleSpecifier
::
resolve_url_or_path
(
&
p
.to_string_lossy
())
.unwrap
();
let
global_state
=
ThreadSafeGlobalState
::
new
(
flags
::
DenoFlags
::
default
(),
Progress
::
new
())
.unwrap
();
let
global_state
=
GlobalState
::
new
(
flags
::
DenoFlags
::
default
())
.unwrap
();
let
state
=
ThreadSafeState
::
new
(
global_state
,
None
,
module_specifier
.clone
())
.unwrap
();
...
...
@@ -357,8 +352,7 @@ mod tests {
reload
:
true
,
..
flags
::
DenoFlags
::
default
()
};
let
global_state
=
ThreadSafeGlobalState
::
new
(
flags
,
Progress
::
new
())
.unwrap
();
let
global_state
=
GlobalState
::
new
(
flags
)
.unwrap
();
let
state
=
ThreadSafeState
::
new
(
global_state
.clone
(),
None
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录