Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
d82c1991
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,发现更多精彩内容 >>
提交
d82c1991
编写于
6月 11, 2019
作者:
M
Matt Harrison
提交者:
Ryan Dahl
6月 11, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add --seed for setting RNG seed (#2483)
上级
cb581620
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
112 addition
and
2 deletion
+112
-2
cli/flags.rs
cli/flags.rs
+68
-0
cli/ops.rs
cli/ops.rs
+9
-2
cli/state.rs
cli/state.rs
+9
-0
tests/seed_random.js
tests/seed_random.js
+11
-0
tests/seed_random.js.out
tests/seed_random.js.out
+12
-0
tests/seed_random.test
tests/seed_random.test
+2
-0
website/manual.md
website/manual.md
+1
-0
未找到文件。
cli/flags.rs
浏览文件 @
d82c1991
...
...
@@ -29,6 +29,7 @@ pub struct DenoFlags {
pub
allow_hrtime
:
bool
,
pub
no_prompts
:
bool
,
pub
no_fetch
:
bool
,
pub
seed
:
Option
<
u64
>
,
pub
v8_flags
:
Option
<
Vec
<
String
>>
,
pub
xeval_replvar
:
Option
<
String
>
,
pub
xeval_delim
:
Option
<
String
>
,
...
...
@@ -145,6 +146,19 @@ To get help on the another subcommands (run in this case):
.help
(
"Load compiler configuration file"
)
.takes_value
(
true
)
.global
(
true
),
)
.arg
(
Arg
::
with_name
(
"seed"
)
.long
(
"seed"
)
.value_name
(
"NUMBER"
)
.help
(
"Seed Math.random()"
)
.takes_value
(
true
)
.validator
(|
val
:
String
|
{
match
val
.parse
::
<
u64
>
()
{
Ok
(
_
)
=>
Ok
(()),
Err
(
_
)
=>
Err
(
"Seed should be a number"
.to_string
())
}
})
.global
(
true
),
)
.arg
(
Arg
::
with_name
(
"v8-options"
)
.long
(
"v8-options"
)
...
...
@@ -379,6 +393,22 @@ pub fn parse_flags(matches: &ArgMatches) -> DenoFlags {
v8_flags
.insert
(
0
,
"deno"
.to_string
());
flags
.v8_flags
=
Some
(
v8_flags
);
}
if
matches
.is_present
(
"seed"
)
{
let
seed_string
=
matches
.value_of
(
"seed"
)
.unwrap
();
let
seed
=
seed_string
.parse
::
<
u64
>
()
.unwrap
();
flags
.seed
=
Some
(
seed
);
let
v8_seed_flag
=
format!
(
"--random-seed={}"
,
seed
);
match
flags
.v8_flags
{
Some
(
ref
mut
v8_flags
)
=>
{
v8_flags
.push
(
v8_seed_flag
);
}
None
=>
{
flags
.v8_flags
=
Some
(
svec!
[
"deno"
,
v8_seed_flag
]);
}
}
}
flags
=
parse_run_args
(
flags
,
matches
);
// flags specific to "run" subcommand
...
...
@@ -1112,4 +1142,42 @@ mod tests {
assert_eq!
(
subcommand
,
DenoSubcommand
::
Run
);
assert_eq!
(
argv
,
svec!
[
"deno"
,
"script.ts"
]);
}
#[test]
fn
test_flags_from_vec_28
()
{
let
(
flags
,
subcommand
,
argv
)
=
flags_from_vec
(
svec!
[
"deno"
,
"--seed"
,
"250"
,
"run"
,
"script.ts"
]);
assert_eq!
(
flags
,
DenoFlags
{
seed
:
Some
(
250
as
u64
),
v8_flags
:
Some
(
svec!
[
"deno"
,
"--random-seed=250"
]),
..
DenoFlags
::
default
()
}
);
assert_eq!
(
subcommand
,
DenoSubcommand
::
Run
);
assert_eq!
(
argv
,
svec!
[
"deno"
,
"script.ts"
])
}
#[test]
fn
test_flags_from_vec_29
()
{
let
(
flags
,
subcommand
,
argv
)
=
flags_from_vec
(
svec!
[
"deno"
,
"--seed"
,
"250"
,
"--v8-flags=--expose-gc"
,
"run"
,
"script.ts"
]);
assert_eq!
(
flags
,
DenoFlags
{
seed
:
Some
(
250
as
u64
),
v8_flags
:
Some
(
svec!
[
"deno"
,
"--expose-gc"
,
"--random-seed=250"
]),
..
DenoFlags
::
default
()
}
);
assert_eq!
(
subcommand
,
DenoSubcommand
::
Run
);
assert_eq!
(
argv
,
svec!
[
"deno"
,
"script.ts"
])
}
}
cli/ops.rs
浏览文件 @
d82c1991
...
...
@@ -2211,10 +2211,17 @@ fn op_host_post_message(
}
fn
op_get_random_values
(
_
state
:
&
ThreadSafeState
,
state
:
&
ThreadSafeState
,
_
base
:
&
msg
::
Base
<
'_
>
,
data
:
Option
<
PinnedBuf
>
,
)
->
Box
<
OpWithError
>
{
thread_rng
()
.fill
(
&
mut
data
.unwrap
()[
..
]);
if
let
Some
(
ref
seeded_rng
)
=
state
.seeded_rng
{
let
mut
rng
=
seeded_rng
.lock
()
.unwrap
();
rng
.fill
(
&
mut
data
.unwrap
()[
..
]);
}
else
{
let
mut
rng
=
thread_rng
();
rng
.fill
(
&
mut
data
.unwrap
()[
..
]);
}
Box
::
new
(
ok_future
(
empty_buf
()))
}
cli/state.rs
浏览文件 @
d82c1991
...
...
@@ -23,6 +23,8 @@ use deno::PinnedBuf;
use
futures
::
future
::
Either
;
use
futures
::
future
::
Shared
;
use
futures
::
Future
;
use
rand
::
rngs
::
StdRng
;
use
rand
::
SeedableRng
;
use
std
;
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashSet
;
...
...
@@ -82,6 +84,7 @@ pub struct State {
pub
dispatch_selector
:
ops
::
OpSelector
,
/// Reference to global progress bar.
pub
progress
:
Progress
,
pub
seeded_rng
:
Option
<
Mutex
<
StdRng
>>
,
/// Set of all URLs that have been compiled. This is a hacky way to work
/// around the fact that --reload will force multiple compilations of the same
...
...
@@ -295,6 +298,11 @@ impl ThreadSafeState {
}
}
let
mut
seeded_rng
=
None
;
if
let
Some
(
seed
)
=
flags
.seed
{
seeded_rng
=
Some
(
Mutex
::
new
(
StdRng
::
seed_from_u64
(
seed
)));
};
ThreadSafeState
(
Arc
::
new
(
State
{
main_module
,
dir
,
...
...
@@ -312,6 +320,7 @@ impl ThreadSafeState {
resource
,
dispatch_selector
,
progress
,
seeded_rng
,
compiled
:
Mutex
::
new
(
HashSet
::
new
()),
}))
}
...
...
tests/seed_random.js
0 → 100644
浏览文件 @
d82c1991
for
(
let
i
=
0
;
i
<
10
;
++
i
)
{
console
.
log
(
Math
.
random
());
}
const
arr
=
new
Uint8Array
(
32
);
crypto
.
getRandomValues
(
arr
);
console
.
log
(
arr
);
crypto
.
getRandomValues
(
arr
);
console
.
log
(
arr
);
tests/seed_random.js.out
0 → 100644
浏览文件 @
d82c1991
0.858562739044346
0.8973397944553141
0.15335012655691727
0.36867387434349963
0.3591039342838782
0.7044499748617652
0.7461423057751548
0.3824611207183364
0.5950178237266042
0.22440633214343908
Uint8Array [ 31, 147, 233, 143, 64, 159, 189, 114, 137, 153, 196, 156, 133, 210, 78, 4, 125, 255, 147, 234, 169, 149, 228, 46, 166, 246, 137, 49, 50, 182, 106, 219 ]
Uint8Array [ 220, 209, 104, 94, 239, 165, 8, 254, 123, 163, 160, 177, 229, 105, 171, 232, 236, 71, 107, 28, 132, 143, 113, 44, 86, 251, 159, 102, 20, 119, 174, 230 ]
tests/seed_random.test
0 → 100644
浏览文件 @
d82c1991
args
:
run
--
seed
=
100
tests
/
seed_random
.
js
output
:
tests
/
seed_random
.
js
.
out
website/manual.md
浏览文件 @
d82c1991
...
...
@@ -635,6 +635,7 @@ OPTIONS:
--allow-write=<allow-write> Allow file system write access
-c, --config <FILE> Load compiler configuration file
--importmap <FILE> Load import map file
--seed <NUMBER> Seed Math.random() and crypto.getRandomValues()
--v8-flags=<v8-flags> Set V8 command line options
SUBCOMMANDS:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录