1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(core): change SourcePair to ExtensionFileSource (#17686)

This commit is contained in:
Bartek Iwańczuk 2023-02-08 00:21:43 +01:00 committed by GitHub
parent 49af1ab18d
commit 161a4fea47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 48 deletions

View file

@ -6,12 +6,13 @@ use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;
use deno_bench_util::BenchOptions;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
fn setup() -> Vec<Extension> {
vec![Extension::builder("bench_setup")
.js(vec![(
"setup.js",
r#"
.js(vec![ExtensionFileSource {
specifier: "setup.js".to_string(),
code: r#"
const hello = "hello world\n";
const hello1k = hello.repeat(1e3);
const hello1m = hello.repeat(1e6);
@ -19,7 +20,7 @@ fn setup() -> Vec<Extension> {
const hello1kEncoded = Deno.core.encode(hello1k);
const hello1mEncoded = Deno.core.encode(hello1m);
"#,
)])
}])
.build()]
}

View file

@ -6,7 +6,10 @@ use std::rc::Rc;
use std::task::Context;
use v8::fast_api::FastFunction;
pub type SourcePair = (String, &'static str);
pub struct ExtensionFileSource {
pub specifier: String,
pub code: &'static str,
}
pub type OpFnRef = v8::FunctionCallback;
pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl;
pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>;
@ -37,8 +40,8 @@ impl OpDecl {
#[derive(Default)]
pub struct Extension {
js_files: Option<Vec<SourcePair>>,
esm_files: Option<Vec<SourcePair>>,
js_files: Option<Vec<ExtensionFileSource>>,
esm_files: Option<Vec<ExtensionFileSource>>,
ops: Option<Vec<OpDecl>>,
opstate_fn: Option<Box<OpStateFn>>,
middleware_fn: Option<Box<OpMiddlewareFn>>,
@ -82,14 +85,14 @@ impl Extension {
/// returns JS source code to be loaded into the isolate (either at snapshotting,
/// or at startup). as a vector of a tuple of the file name, and the source code.
pub fn get_js_sources(&self) -> &[SourcePair] {
pub fn get_js_sources(&self) -> &[ExtensionFileSource] {
match &self.js_files {
Some(files) => files,
None => &[],
}
}
pub fn get_esm_sources(&self) -> &[SourcePair] {
pub fn get_esm_sources(&self) -> &[ExtensionFileSource] {
match &self.esm_files {
Some(files) => files,
None => &[],
@ -152,8 +155,8 @@ impl Extension {
// Provides a convenient builder pattern to declare Extensions
#[derive(Default)]
pub struct ExtensionBuilder {
js: Vec<SourcePair>,
esm: Vec<SourcePair>,
js: Vec<ExtensionFileSource>,
esm: Vec<ExtensionFileSource>,
ops: Vec<OpDecl>,
state: Option<Box<OpStateFn>>,
middleware: Option<Box<OpMiddlewareFn>>,
@ -168,26 +171,27 @@ impl ExtensionBuilder {
self
}
pub fn js(
&mut self,
js_files: Vec<(&'static str, &'static str)>,
) -> &mut Self {
let js_files = js_files.into_iter().map(|source_pair| {
let name = format!("internal:{}/{}", self.name, source_pair.0);
(name, source_pair.1)
});
pub fn js(&mut self, js_files: Vec<ExtensionFileSource>) -> &mut Self {
let js_files =
js_files.into_iter().map(|file_source| ExtensionFileSource {
specifier: format!("internal:{}/{}", self.name, file_source.specifier),
code: file_source.code,
});
self.js.extend(js_files);
self
}
pub fn esm(
&mut self,
esm_files: Vec<(&'static str, &'static str)>,
) -> &mut Self {
let esm_files = esm_files.into_iter().map(|source_pair| {
let name = format!("internal:{}/{}", self.name, source_pair.0);
(name, source_pair.1)
});
pub fn esm(&mut self, esm_files: Vec<ExtensionFileSource>) -> &mut Self {
let esm_files =
esm_files
.into_iter()
.map(|file_source| ExtensionFileSource {
specifier: format!(
"internal:{}/{}",
self.name, file_source.specifier
),
code: file_source.code,
});
self.esm.extend(esm_files);
self
}
@ -254,10 +258,10 @@ impl ExtensionBuilder {
macro_rules! include_js_files {
($($file:literal,)+) => {
vec![
$((
$file,
include_str!($file),
),)+
$($crate::ExtensionFileSource {
specifier: $file.to_string(),
code: include_str!($file),
},)+
]
};
}

View file

@ -54,6 +54,7 @@ pub use crate::async_cell::RcLike;
pub use crate::async_cell::RcRef;
pub use crate::extensions::Extension;
pub use crate::extensions::ExtensionBuilder;
pub use crate::extensions::ExtensionFileSource;
pub use crate::extensions::OpDecl;
pub use crate::extensions::OpMiddlewareFn;
pub use crate::flags::v8_set_flags;

View file

@ -815,27 +815,33 @@ impl JsRuntime {
for ext in &extensions {
{
let js_files = ext.get_esm_sources();
for (filename, source) in js_files {
for file_source in js_files {
futures::executor::block_on(async {
let id = self
.load_side_module(
&ModuleSpecifier::parse(filename)?,
Some(source.to_string()),
&ModuleSpecifier::parse(&file_source.specifier)?,
Some(file_source.code.to_string()),
)
.await?;
let receiver = self.mod_evaluate(id);
self.run_event_loop(false).await?;
receiver.await?
})
.with_context(|| format!("Couldn't execute '{filename}'"))?;
.with_context(|| {
format!("Couldn't execute '{}'", file_source.specifier)
})?;
}
}
{
let js_files = ext.get_js_sources();
for (filename, source) in js_files {
for file_source in js_files {
// TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
realm.execute_script(self.v8_isolate(), filename, source)?;
realm.execute_script(
self.v8_isolate(),
&file_source.specifier,
file_source.code,
)?;
}
}
}

View file

@ -6,18 +6,19 @@ use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
fn setup() -> Vec<Extension> {
vec![
deno_webidl::init(),
deno_url::init(),
Extension::builder("bench_setup")
.esm(vec![(
"internal:setup",
r#"import { URL } from "internal:deno_url/00_url.js";
.esm(vec![ExtensionFileSource {
specifier: "internal:setup".to_string(),
code: r#"import { URL } from "internal:deno_url/00_url.js";
globalThis.URL = URL;
"#,
)])
}])
.build(),
]
}

View file

@ -5,6 +5,7 @@ use deno_bench_util::bench_or_profile;
use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
use deno_web::BlobStore;
struct Permissions;
@ -29,14 +30,14 @@ fn setup() -> Vec<Extension> {
deno_console::init(),
deno_web::init::<Permissions>(BlobStore::default(), None),
Extension::builder("bench_setup")
.esm(vec![(
"internal:setup",
r#"
.esm(vec![ExtensionFileSource {
specifier: "internal:setup".to_string(),
code: r#"
import { TextDecoder } from "internal:deno_web/08_text_encoding.js";
globalThis.TextDecoder = TextDecoder;
globalThis.hello12k = Deno.core.encode("hello world\n".repeat(1e3));
"#,
)])
}])
.state(|state| {
state.put(Permissions {});
Ok(())

View file

@ -5,6 +5,7 @@ use deno_bench_util::bench_or_profile;
use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
use deno_web::BlobStore;
struct Permissions;
@ -29,11 +30,14 @@ fn setup() -> Vec<Extension> {
deno_web::init::<Permissions>(BlobStore::default(), None),
Extension::builder("bench_setup")
.esm(vec![
("internal:setup", r#"
ExtensionFileSource {
specifier: "internal:setup".to_string(),
code: r#"
import { setTimeout, handleTimerMacrotask } from "internal:deno_web/02_timers.js";
globalThis.setTimeout = setTimeout;
Deno.core.setMacrotaskCallback(handleTimerMacrotask);
"#),
"#
},
])
.state(|state| {
state.put(Permissions{});

View file

@ -6,12 +6,16 @@ use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
fn setup() -> Vec<Extension> {
vec![
deno_webidl::init(),
Extension::builder("deno_webidl_bench")
.esm(vec![("internal:setup", include_str!("dict.js"))])
.esm(vec![ExtensionFileSource {
specifier: "internal:setup".to_string(),
code: include_str!("dict.js"),
}])
.build(),
]
}