diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-01-05 22:10:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-05 22:10:50 +0100 |
commit | 46c0cab763380a1ecbdf93e309257de4e37a7341 (patch) | |
tree | 232992fc9a79bb312144e2426759c41b0afd15c6 /core/runtime.rs | |
parent | 0d41e21b0e9b46cc179eff42c215439fb672964b (diff) |
refactor(core): simplify Deno.core initialisation, remove stale TODO (#8847)
This commit rewrites initialisation of the "shared queue" and
in effect prevents from double execution of "core/core.js" and
"core/error.js".
Previously both of these files were executed every time a "JsRuntime"
was created. That lead to a situation where one copy of each script
was included in the snapshot and then another copy would be
executed after loading the snapshot.
Effectively "JsRuntime::shared_init" was removed; instead execution
of those scripts and actual initialisation of shared queue
was split into two helper functions: "JsRuntime::js_init" and
"JsRuntime::share_queue_init".
Additionally stale TODO comments were removed.
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index d1fb41dd0..7e8ac48fa 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -81,7 +81,6 @@ pub struct JsRuntime { v8_isolate: Option<v8::OwnedIsolate>, snapshot_creator: Option<v8::SnapshotCreator>, has_snapshotted: bool, - needs_init: bool, allocations: IsolateAllocations, } @@ -203,6 +202,8 @@ impl JsRuntime { unsafe { v8_init() }; }); + let has_startup_snapshot = options.startup_snapshot.is_some(); + let global_context; let (mut isolate, maybe_snapshot_creator) = if options.will_snapshot { // TODO(ry) Support loading snapshots before snapshotting. @@ -258,7 +259,7 @@ impl JsRuntime { let js_error_create_fn = options .js_error_create_fn .unwrap_or_else(|| Rc::new(JsError::create)); - let mut op_state = OpState::default(); + let mut op_state = OpState::new(); if let Some(get_error_class_fn) = options.get_error_class_fn { op_state.get_error_class_fn = get_error_class_fn; @@ -286,13 +287,22 @@ impl JsRuntime { waker: AtomicWaker::new(), }))); - Self { + let mut js_runtime = Self { v8_isolate: Some(isolate), snapshot_creator: maybe_snapshot_creator, has_snapshotted: false, - needs_init: true, allocations: IsolateAllocations::default(), + }; + + if !has_startup_snapshot { + js_runtime.js_init(); } + + if !options.will_snapshot { + js_runtime.shared_queue_init(); + } + + js_runtime } pub fn global_context(&mut self) -> v8::Global<v8::Context> { @@ -322,17 +332,29 @@ impl JsRuntime { s.clone() } - /// Executes a bit of built-in JavaScript to provide Deno.sharedQueue. - fn shared_init(&mut self) { - if self.needs_init { - self.needs_init = false; - self - .execute("deno:core/core.js", include_str!("core.js")) - .unwrap(); - self - .execute("deno:core/error.js", include_str!("error.js")) - .unwrap(); - } + /// Executes a JavaScript code to provide Deno.core and error reporting. + /// + /// This function can be called during snapshotting. + fn js_init(&mut self) { + self + .execute("deno:core/core.js", include_str!("core.js")) + .unwrap(); + self + .execute("deno:core/error.js", include_str!("error.js")) + .unwrap(); + } + + /// Executes a JavaScript code to initialize shared queue binding + /// between Rust and JS. + /// + /// This function mustn't be called during snapshotting. + fn shared_queue_init(&mut self) { + self + .execute( + "deno:core/shared_queue_init.js", + "Deno.core.sharedQueueInit()", + ) + .unwrap(); } /// Returns the runtime's op state, which can be used to maintain ops @@ -356,8 +378,6 @@ impl JsRuntime { js_filename: &str, js_source: &str, ) -> Result<(), AnyError> { - self.shared_init(); - let context = self.global_context(); let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context); @@ -482,8 +502,6 @@ impl JsRuntime { &mut self, cx: &mut Context, ) -> Poll<Result<(), AnyError>> { - self.shared_init(); - let state_rc = Self::state(self.v8_isolate()); { let state = state_rc.borrow(); @@ -740,8 +758,6 @@ impl JsRuntime { load_id: ModuleLoadId, id: ModuleId, ) -> Result<(), AnyError> { - self.shared_init(); - let state_rc = Self::state(self.v8_isolate()); let context = self.global_context(); let context1 = self.global_context(); @@ -827,8 +843,6 @@ impl JsRuntime { &mut self, id: ModuleId, ) -> mpsc::Receiver<Result<(), AnyError>> { - self.shared_init(); - let state_rc = Self::state(self.v8_isolate()); let context = self.global_context(); @@ -1276,7 +1290,6 @@ impl JsRuntime { specifier: &ModuleSpecifier, code: Option<String>, ) -> Result<ModuleId, AnyError> { - self.shared_init(); let loader = { let state_rc = Self::state(self.v8_isolate()); let state = state_rc.borrow(); |