Skip to content

Commit e5a31d9

Browse files
committed
fix: make invoking the v8 platform initialize not dependent with cli
1 parent 14e9968 commit e5a31d9

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

crates/base/src/commands.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::{
33
server::{Server, ServerCodes, WorkerEntrypoints},
44
};
55
use anyhow::Error;
6-
use deno_core::JsRuntime;
7-
use log::error;
86
use tokio::sync::mpsc::Sender;
97

108
#[allow(clippy::too_many_arguments)]
@@ -19,13 +17,6 @@ pub async fn start_server(
1917
callback_tx: Option<Sender<ServerCodes>>,
2018
entrypoints: WorkerEntrypoints,
2119
) -> Result<(), Error> {
22-
set_v8_flags();
23-
24-
// NOTE(denoland/deno/20495): Due to the new PKU (Memory Protection Keys)
25-
// feature introduced in V8 11.6, We need to initialize the V8 platform on
26-
// the main thread that spawns V8 isolates.
27-
JsRuntime::init_platform(None);
28-
2920
let mut server = Server::new(
3021
ip,
3122
port,
@@ -40,20 +31,3 @@ pub async fn start_server(
4031
.await?;
4132
server.listen().await
4233
}
43-
44-
fn set_v8_flags() {
45-
let v8_flags = std::env::var("V8_FLAGS").unwrap_or("".to_string());
46-
let mut vec = vec![""];
47-
48-
if v8_flags.is_empty() {
49-
return;
50-
}
51-
52-
vec.append(&mut v8_flags.split(' ').collect());
53-
54-
let ignored = deno_core::v8_set_flags(vec.iter().map(|v| v.to_string()).collect());
55-
56-
if *ignored.as_slice() != [""] {
57-
error!("v8 flags unrecognized {:?}", ignored);
58-
}
59-
}

crates/base/src/deno_runtime.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use deno_tls::rustls_native_certs::load_native_certs;
1313
use deno_tls::RootCertStoreProvider;
1414
use futures_util::future::poll_fn;
1515
use log::{error, trace};
16+
use once_cell::sync::OnceCell;
1617
use sb_core::conn_sync::ConnSync;
1718
use serde::de::DeserializeOwned;
1819
use std::collections::HashMap;
@@ -74,6 +75,17 @@ impl DenoRuntime {
7475
#[allow(clippy::unnecessary_literal_unwrap)]
7576
#[allow(clippy::arc_with_non_send_sync)]
7677
pub async fn new(opts: WorkerContextInitOpts) -> Result<Self, Error> {
78+
static INITIALZE_V8_PLATFORM: OnceCell<()> = OnceCell::new();
79+
80+
INITIALZE_V8_PLATFORM.get_or_init(|| {
81+
set_v8_flags();
82+
83+
// NOTE(denoland/deno/20495): Due to the new PKU (Memory Protection Keys)
84+
// feature introduced in V8 11.6, We need to initialize the V8 platform on
85+
// the main thread that spawns V8 isolates.
86+
JsRuntime::init_platform(None);
87+
});
88+
7789
let WorkerContextInitOpts {
7890
service_path,
7991
no_module_cache,
@@ -474,6 +486,23 @@ impl DenoRuntime {
474486
}
475487
}
476488

489+
fn set_v8_flags() {
490+
let v8_flags = std::env::var("V8_FLAGS").unwrap_or("".to_string());
491+
let mut vec = vec![""];
492+
493+
if v8_flags.is_empty() {
494+
return;
495+
}
496+
497+
vec.append(&mut v8_flags.split(' ').collect());
498+
499+
let ignored = deno_core::v8_set_flags(vec.iter().map(|v| v.to_string()).collect());
500+
501+
if *ignored.as_slice() != [""] {
502+
error!("v8 flags unrecognized {:?}", ignored);
503+
}
504+
}
505+
477506
#[cfg(test)]
478507
mod test {
479508
use crate::deno_runtime::DenoRuntime;

0 commit comments

Comments
 (0)