mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat(core): deterministic snapshots (#14037)
This commit is contained in:
parent
d76acfdc17
commit
68bf43fca7
2 changed files with 39 additions and 3 deletions
|
@ -20,6 +20,30 @@ fn eval_p() {
|
|||
assert_eq!("3", stdout_str);
|
||||
}
|
||||
|
||||
// Make sure that snapshot flags don't affect runtime.
|
||||
#[test]
|
||||
fn eval_randomness() {
|
||||
let mut numbers = Vec::with_capacity(10);
|
||||
for _ in 0..10 {
|
||||
let output = util::deno_cmd()
|
||||
.arg("eval")
|
||||
.arg("-p")
|
||||
.arg("Math.random()")
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap()
|
||||
.wait_with_output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
let stdout_str = util::strip_ansi_codes(
|
||||
std::str::from_utf8(&output.stdout).unwrap().trim(),
|
||||
);
|
||||
numbers.push(stdout_str.to_string());
|
||||
}
|
||||
numbers.dedup();
|
||||
assert!(numbers.len() > 1);
|
||||
}
|
||||
|
||||
itest!(_029_eval {
|
||||
args: "eval console.log(\"hello\")",
|
||||
output: "029_eval.out",
|
||||
|
|
|
@ -204,7 +204,10 @@ impl Drop for JsRuntime {
|
|||
}
|
||||
}
|
||||
|
||||
fn v8_init(v8_platform: Option<v8::SharedRef<v8::Platform>>) {
|
||||
fn v8_init(
|
||||
v8_platform: Option<v8::SharedRef<v8::Platform>>,
|
||||
predictable: bool,
|
||||
) {
|
||||
// Include 10MB ICU data file.
|
||||
#[repr(C, align(16))]
|
||||
struct IcuData([u8; 10284336]);
|
||||
|
@ -222,7 +225,15 @@ fn v8_init(v8_platform: Option<v8::SharedRef<v8::Platform>>) {
|
|||
" --harmony-import-assertions",
|
||||
" --no-validate-asm",
|
||||
);
|
||||
v8::V8::set_flags_from_string(flags);
|
||||
|
||||
if predictable {
|
||||
v8::V8::set_flags_from_string(&format!(
|
||||
"{}{}",
|
||||
flags, " --predictable --random-seed=42"
|
||||
));
|
||||
} else {
|
||||
v8::V8::set_flags_from_string(flags);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -251,6 +262,7 @@ pub struct RuntimeOptions {
|
|||
pub startup_snapshot: Option<Snapshot>,
|
||||
|
||||
/// Prepare runtime to take snapshot of loaded code.
|
||||
/// The snapshot is determinstic and uses predictable random numbers.
|
||||
///
|
||||
/// Currently can't be used with `startup_snapshot`.
|
||||
pub will_snapshot: bool,
|
||||
|
@ -284,7 +296,7 @@ impl JsRuntime {
|
|||
let v8_platform = options.v8_platform.take();
|
||||
|
||||
static DENO_INIT: Once = Once::new();
|
||||
DENO_INIT.call_once(move || v8_init(v8_platform));
|
||||
DENO_INIT.call_once(move || v8_init(v8_platform, options.will_snapshot));
|
||||
|
||||
let has_startup_snapshot = options.startup_snapshot.is_some();
|
||||
|
||||
|
|
Loading…
Reference in a new issue