Skip to content

Commit 8f8dd82

Browse files
nayeemrmnmmastrac
authored andcommitted
refactor(core): don't use extension macro for core js (#19616)
Seems like too much of a special case because `init_cbs()` needs to be called right after them. Removes the `Extension::is_core` stuff.
1 parent 79ed162 commit 8f8dd82

File tree

5 files changed

+61
-74
lines changed

5 files changed

+61
-74
lines changed

core/extensions.rs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ macro_rules! ops {
174174
/// * bounds: a comma-separated list of additional type bounds, eg: `bounds = [ P::MyAssociatedType: MyTrait ]`
175175
/// * ops: a comma-separated list of [`OpDecl`]s to provide, eg: `ops = [ op_foo, op_bar ]`
176176
/// * esm: a comma-separated list of ESM module filenames (see [`include_js_files`]), eg: `esm = [ dir "dir", "my_file.js" ]`
177-
/// * esm_setup_script: see [`ExtensionBuilder::esm_setup_script`]
178177
/// * js: a comma-separated list of JS filenames (see [`include_js_files`]), eg: `js = [ dir "dir", "my_file.js" ]`
179178
/// * config: a structure-like definition for configuration parameters which will be required when initializing this extension, eg: `config = { my_param: Option<usize> }`
180179
/// * middleware: an [`OpDecl`] middleware function with the signature `fn (OpDecl) -> OpDecl`
@@ -191,7 +190,6 @@ macro_rules! extension {
191190
$(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )? ),+ $(,)? ] )?
192191
$(, esm_entry_point = $esm_entry_point:literal )?
193192
$(, esm = [ $( dir $dir_esm:literal , )? $( $esm:literal ),* $(,)? ] )?
194-
$(, esm_setup_script = $esm_setup_script:expr )?
195193
$(, js = [ $( dir $dir_js:literal , )? $( $js:literal ),* $(,)? ] )?
196194
$(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )?
197195
$(, middleware = $middleware_fn:expr )?
@@ -220,12 +218,6 @@ macro_rules! extension {
220218
$( ext.esm(
221219
$crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm , )* )
222220
); )?
223-
$(
224-
ext.esm(vec![ExtensionFileSource {
225-
specifier: "ext:setup",
226-
code: ExtensionFileSourceCode::IncludedInBinary($esm_setup_script),
227-
}]);
228-
)?
229221
$(
230222
ext.esm_entry_point($esm_entry_point);
231223
)?
@@ -355,8 +347,8 @@ macro_rules! extension {
355347
#[derive(Default)]
356348
pub struct Extension {
357349
pub(crate) name: &'static str,
358-
js_files: Option<Vec<ExtensionFileSource>>,
359-
esm_files: Option<Vec<ExtensionFileSource>>,
350+
js_files: Vec<ExtensionFileSource>,
351+
esm_files: Vec<ExtensionFileSource>,
360352
esm_entry_point: Option<&'static str>,
361353
ops: Option<Vec<OpDecl>>,
362354
opstate_fn: Option<Box<OpStateFn>>,
@@ -365,7 +357,6 @@ pub struct Extension {
365357
initialized: bool,
366358
enabled: bool,
367359
deps: Option<&'static [&'static str]>,
368-
pub(crate) is_core: bool,
369360
}
370361

371362
// Note: this used to be a trait, but we "downgraded" it to a single concrete type
@@ -412,12 +403,12 @@ impl Extension {
412403

413404
/// returns JS source code to be loaded into the isolate (either at snapshotting,
414405
/// or at startup). as a vector of a tuple of the file name, and the source code.
415-
pub fn get_js_sources(&self) -> Option<&Vec<ExtensionFileSource>> {
416-
self.js_files.as_ref()
406+
pub fn get_js_sources(&self) -> &Vec<ExtensionFileSource> {
407+
&self.js_files
417408
}
418409

419-
pub fn get_esm_sources(&self) -> Option<&Vec<ExtensionFileSource>> {
420-
self.esm_files.as_ref()
410+
pub fn get_esm_sources(&self) -> &Vec<ExtensionFileSource> {
411+
&self.esm_files
421412
}
422413

423414
pub fn get_esm_entry_point(&self) -> Option<&'static str> {
@@ -488,7 +479,6 @@ pub struct ExtensionBuilder {
488479
event_loop_middleware: Option<Box<OpEventLoopFn>>,
489480
name: &'static str,
490481
deps: &'static [&'static str],
491-
is_core: bool,
492482
}
493483

494484
impl ExtensionBuilder {
@@ -538,13 +528,11 @@ impl ExtensionBuilder {
538528

539529
/// Consume the [`ExtensionBuilder`] and return an [`Extension`].
540530
pub fn take(self) -> Extension {
541-
let js_files = Some(self.js);
542-
let esm_files = Some(self.esm);
543531
let ops = Some(self.ops);
544532
let deps = Some(self.deps);
545533
Extension {
546-
js_files,
547-
esm_files,
534+
js_files: self.js,
535+
esm_files: self.esm,
548536
esm_entry_point: self.esm_entry_point,
549537
ops,
550538
opstate_fn: self.state,
@@ -554,18 +542,15 @@ impl ExtensionBuilder {
554542
enabled: true,
555543
name: self.name,
556544
deps,
557-
is_core: self.is_core,
558545
}
559546
}
560547

561548
pub fn build(&mut self) -> Extension {
562-
let js_files = Some(std::mem::take(&mut self.js));
563-
let esm_files = Some(std::mem::take(&mut self.esm));
564549
let ops = Some(std::mem::take(&mut self.ops));
565550
let deps = Some(std::mem::take(&mut self.deps));
566551
Extension {
567-
js_files,
568-
esm_files,
552+
js_files: std::mem::take(&mut self.js),
553+
esm_files: std::mem::take(&mut self.esm),
569554
esm_entry_point: self.esm_entry_point.take(),
570555
ops,
571556
opstate_fn: self.state.take(),
@@ -575,15 +560,8 @@ impl ExtensionBuilder {
575560
enabled: true,
576561
name: self.name,
577562
deps,
578-
is_core: self.is_core,
579563
}
580564
}
581-
582-
#[doc(hidden)]
583-
pub(crate) fn deno_core(&mut self) -> &mut Self {
584-
self.is_core = true;
585-
self
586-
}
587565
}
588566

589567
/// Helps embed JS files in an extension. Returns a vector of

core/modules/loaders.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ impl ExtModuleLoader {
119119
extensions
120120
.iter()
121121
.flat_map(|e| e.get_esm_sources())
122-
.flatten()
123122
.map(|s| (s.specifier.to_string(), s.clone())),
124123
);
125124
ExtModuleLoader {

core/ops_builtin.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ crate::extension!(
7979
ops_builtin_v8::op_has_pending_promise_rejection,
8080
ops_builtin_v8::op_arraybuffer_was_detached,
8181
],
82-
js = ["00_primordials.js", "01_core.js", "02_error.js"],
83-
customizer = |ext: &mut crate::ExtensionBuilder| {
84-
ext.deno_core();
85-
}
8682
);
8783

8884
/// Return map of resources with id as key

core/runtime/jsruntime.rs

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::error::GetErrorClassFn;
1010
use crate::error::JsError;
1111
use crate::extensions::OpDecl;
1212
use crate::extensions::OpEventLoopFn;
13+
use crate::include_js_files;
1314
use crate::inspector::JsRuntimeInspector;
1415
use crate::module_specifier::ModuleSpecifier;
1516
use crate::modules::AssertedModuleType;
@@ -27,6 +28,7 @@ use crate::runtime::JsRealm;
2728
use crate::source_map::SourceMapCache;
2829
use crate::source_map::SourceMapGetter;
2930
use crate::Extension;
31+
use crate::ExtensionFileSource;
3032
use crate::NoopModuleLoader;
3133
use crate::OpMiddlewareFn;
3234
use crate::OpResult;
@@ -38,6 +40,7 @@ use anyhow::Error;
3840
use futures::channel::oneshot;
3941
use futures::future::poll_fn;
4042
use futures::stream::StreamExt;
43+
use once_cell::sync::Lazy;
4144
use smallvec::SmallVec;
4245
use std::any::Any;
4346
use std::cell::RefCell;
@@ -196,6 +199,16 @@ impl InitMode {
196199
}
197200
}
198201

202+
pub(crate) static BUILTIN_SOURCES: Lazy<Vec<ExtensionFileSource>> =
203+
Lazy::new(|| {
204+
include_js_files!(
205+
core
206+
"00_primordials.js",
207+
"01_core.js",
208+
"02_error.js",
209+
)
210+
});
211+
199212
/// A single execution context of JavaScript. Corresponds roughly to the "Web
200213
/// Worker" concept in the DOM.
201214
////
@@ -504,7 +517,7 @@ impl JsRuntime {
504517
maybe_load_callback: Option<ExtModuleLoaderCb>,
505518
) -> JsRuntime {
506519
let init_mode = InitMode::from_options(&options);
507-
let (op_state, ops) = Self::create_opstate(&mut options, init_mode);
520+
let (op_state, ops) = Self::create_opstate(&mut options);
508521
let op_state = Rc::new(RefCell::new(op_state));
509522

510523
// Collect event-loop middleware
@@ -840,36 +853,39 @@ impl JsRuntime {
840853
let mut esm_entrypoints = vec![];
841854

842855
futures::executor::block_on(async {
856+
if self.init_mode == InitMode::New {
857+
for file_source in &*BUILTIN_SOURCES {
858+
realm.execute_script(
859+
self.v8_isolate(),
860+
file_source.specifier,
861+
file_source.load()?,
862+
)?;
863+
}
864+
}
865+
self.init_cbs(realm);
866+
843867
for extension in &extensions {
844868
let maybe_esm_entry_point = extension.get_esm_entry_point();
845869

846-
if let Some(esm_files) = extension.get_esm_sources() {
847-
for file_source in esm_files {
848-
self
849-
.load_side_module(
850-
&ModuleSpecifier::parse(file_source.specifier)?,
851-
None,
852-
)
853-
.await?;
854-
}
870+
for file_source in extension.get_esm_sources() {
871+
self
872+
.load_side_module(
873+
&ModuleSpecifier::parse(file_source.specifier)?,
874+
None,
875+
)
876+
.await?;
855877
}
856878

857879
if let Some(entry_point) = maybe_esm_entry_point {
858880
esm_entrypoints.push(entry_point);
859881
}
860882

861-
if let Some(js_files) = extension.get_js_sources() {
862-
for file_source in js_files {
863-
realm.execute_script(
864-
self.v8_isolate(),
865-
file_source.specifier,
866-
file_source.load()?,
867-
)?;
868-
}
869-
}
870-
871-
if extension.is_core {
872-
self.init_cbs(realm);
883+
for file_source in extension.get_js_sources() {
884+
realm.execute_script(
885+
self.v8_isolate(),
886+
file_source.specifier,
887+
file_source.load()?,
888+
)?;
873889
}
874890
}
875891

@@ -966,20 +982,11 @@ impl JsRuntime {
966982
}
967983

968984
/// Initializes ops of provided Extensions
969-
fn create_opstate(
970-
options: &mut RuntimeOptions,
971-
init_mode: InitMode,
972-
) -> (OpState, Vec<OpDecl>) {
985+
fn create_opstate(options: &mut RuntimeOptions) -> (OpState, Vec<OpDecl>) {
973986
// Add built-in extension
974-
if init_mode == InitMode::FromSnapshot {
975-
options
976-
.extensions
977-
.insert(0, crate::ops_builtin::core::init_ops());
978-
} else {
979-
options
980-
.extensions
981-
.insert(0, crate::ops_builtin::core::init_ops_and_esm());
982-
}
987+
options
988+
.extensions
989+
.insert(0, crate::ops_builtin::core::init_ops());
983990

984991
let ops = Self::collect_ops(&mut options.extensions);
985992

core/runtime/snapshot_util.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use std::path::Path;
44
use std::path::PathBuf;
55
use std::time::Instant;
66

7+
use crate::runtime::jsruntime::BUILTIN_SOURCES;
78
use crate::runtime::RuntimeSnapshotOptions;
89
use crate::ExtModuleLoaderCb;
910
use crate::Extension;
11+
use crate::ExtensionFileSourceCode;
1012
use crate::JsRuntimeForSnapshot;
1113
use crate::RuntimeOptions;
1214
use crate::Snapshot;
@@ -52,14 +54,19 @@ pub fn create_snapshot(
5254
mark = Instant::now();
5355

5456
let mut files_loaded_during_snapshot = vec![];
57+
for source in &*BUILTIN_SOURCES {
58+
if let ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) =
59+
&source.code
60+
{
61+
files_loaded_during_snapshot.push(path.clone());
62+
}
63+
}
5564
for source in js_runtime
5665
.extensions()
5766
.iter()
5867
.flat_map(|e| vec![e.get_esm_sources(), e.get_js_sources()])
5968
.flatten()
60-
.flatten()
6169
{
62-
use crate::ExtensionFileSourceCode;
6370
if let ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) =
6471
&source.code
6572
{

0 commit comments

Comments
 (0)