From 986513c080b97372ae4dfe23256089b0f2c11033 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 12 Apr 2021 12:15:04 +0200 Subject: core: let embedders plug in their own V8 platform Fixes #9912. --- core/runtime.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'core/runtime.rs') diff --git a/core/runtime.rs b/core/runtime.rs index 3583ad89e..4174e0598 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -142,11 +142,19 @@ impl Drop for JsRuntime { } } -#[allow(clippy::missing_safety_doc)] -pub unsafe fn v8_init() { - let platform = v8::new_default_platform().unwrap(); - v8::V8::initialize_platform(platform); +fn v8_init(v8_platform: Option>) { + // Include 10MB ICU data file. + #[repr(C, align(16))] + struct IcuData([u8; 10413584]); + static ICU_DATA: IcuData = IcuData(*include_bytes!("icudtl.dat")); + v8::icu::set_common_data(&ICU_DATA.0).unwrap(); + + let v8_platform = v8_platform + .unwrap_or_else(v8::new_default_platform) + .unwrap(); + v8::V8::initialize_platform(v8_platform); v8::V8::initialize(); + let argv = vec![ "".to_string(), "--wasm-test-streaming".to_string(), @@ -192,20 +200,19 @@ pub struct RuntimeOptions { /// Isolate creation parameters. pub create_params: Option, + + /// V8 platform instance to use. Used when Deno initializes V8 + /// (which it only does once), otherwise it's silenty dropped. + pub v8_platform: Option>, } impl JsRuntime { /// Only constructor, configuration is done through `options`. pub fn new(mut options: RuntimeOptions) -> Self { + let v8_platform = options.v8_platform.take(); + static DENO_INIT: Once = Once::new(); - DENO_INIT.call_once(|| { - // Include 10MB ICU data file. - #[repr(C, align(16))] - struct IcuData([u8; 10413584]); - static ICU_DATA: IcuData = IcuData(*include_bytes!("icudtl.dat")); - v8::icu::set_common_data(&ICU_DATA.0).unwrap(); - unsafe { v8_init() }; - }); + DENO_INIT.call_once(move || v8_init(v8_platform)); let has_startup_snapshot = options.startup_snapshot.is_some(); @@ -2381,4 +2388,14 @@ main(); // Test that the script specifier is a URL: `deno:`. assert!(error_string.contains("deno:core/core.js")); } + + #[test] + fn test_v8_platform() { + let options = RuntimeOptions { + v8_platform: Some(v8::new_default_platform()), + ..Default::default() + }; + let mut runtime = JsRuntime::new(options); + runtime.execute("", "").unwrap(); + } } -- cgit v1.2.3