summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Cargo.toml1
-rw-r--r--core/extensions.rs49
-rw-r--r--core/modules.rs8
-rw-r--r--core/runtime.rs6
-rw-r--r--core/snapshot_util.rs6
5 files changed, 58 insertions, 12 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 2b4d40207..a246a7efe 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -16,6 +16,7 @@ path = "lib.rs"
[features]
default = ["v8_use_custom_libcxx"]
v8_use_custom_libcxx = ["v8/use_custom_libcxx"]
+include_js_files_for_snapshotting = []
[dependencies]
anyhow.workspace = true
diff --git a/core/extensions.rs b/core/extensions.rs
index ab686d868..f84ab0a91 100644
--- a/core/extensions.rs
+++ b/core/extensions.rs
@@ -1,7 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::OpState;
+use anyhow::Context as _;
use anyhow::Error;
use std::cell::RefCell;
+use std::path::PathBuf;
use std::rc::Rc;
use std::task::Context;
use v8::fast_api::FastFunction;
@@ -13,8 +15,24 @@ pub enum ExtensionFileSourceCode {
/// will result in two copies of the source code being included - one in the
/// snapshot, the other the static string in the `Extension`.
IncludedInBinary(&'static str),
- // TODO(bartlomieju): add more variants that allow to read file from the disk,
- // and not include it in the binary.
+
+ // Source code is loaded from a file on disk. It's meant to be used if the
+ // embedder is creating snapshots. Files will be loaded from the filesystem
+ // during the build time and they will only be present in the V8 snapshot.
+ LoadedFromFsDuringSnapshot(PathBuf),
+}
+
+impl ExtensionFileSourceCode {
+ pub fn load(&self) -> Result<String, Error> {
+ match self {
+ ExtensionFileSourceCode::IncludedInBinary(code) => Ok(code.to_string()),
+ ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => {
+ let msg = format!("Failed to read \"{}\"", path.display());
+ let code = std::fs::read_to_string(path).context(msg)?;
+ Ok(code)
+ }
+ }
+ }
}
#[derive(Clone, Debug)]
@@ -299,6 +317,7 @@ impl ExtensionBuilder {
/// - "internal:my_extension/js/01_hello.js"
/// - "internal:my_extension/js/02_goodbye.js"
/// ```
+#[cfg(not(feature = "include_js_files_for_snapshotting"))]
#[macro_export]
macro_rules! include_js_files {
(dir $dir:literal, $($file:literal,)+) => {
@@ -323,3 +342,29 @@ macro_rules! include_js_files {
]
};
}
+
+#[cfg(feature = "include_js_files_for_snapshotting")]
+#[macro_export]
+macro_rules! include_js_files {
+ (dir $dir:literal, $($file:literal,)+) => {
+ vec![
+ $($crate::ExtensionFileSource {
+ specifier: concat!($dir, "/", $file).to_string(),
+ code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
+ std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file)
+ ),
+ },)+
+ ]
+ };
+
+ ($($file:literal,)+) => {
+ vec![
+ $($crate::ExtensionFileSource {
+ specifier: $file.to_string(),
+ code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
+ std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($file)
+ ),
+ },)+
+ ]
+ };
+}
diff --git a/core/modules.rs b/core/modules.rs
index 43dabf411..f1d4273e8 100644
--- a/core/modules.rs
+++ b/core/modules.rs
@@ -3,7 +3,6 @@
use crate::bindings;
use crate::error::generic_error;
use crate::extensions::ExtensionFileSource;
-use crate::extensions::ExtensionFileSourceCode;
use crate::module_specifier::ModuleSpecifier;
use crate::resolve_import;
use crate::resolve_url;
@@ -403,10 +402,9 @@ impl ModuleLoader for InternalModuleLoader {
let result = if let Some(load_callback) = &self.maybe_load_callback {
load_callback(file_source)
} else {
- match file_source.code {
- ExtensionFileSourceCode::IncludedInBinary(code) => {
- Ok(code.to_string())
- }
+ match file_source.code.load() {
+ Ok(code) => Ok(code),
+ Err(err) => return futures::future::err(err).boxed_local(),
}
};
diff --git a/core/runtime.rs b/core/runtime.rs
index ae2b0489b..d098c25b1 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -21,7 +21,6 @@ use crate::source_map::SourceMapCache;
use crate::source_map::SourceMapGetter;
use crate::Extension;
use crate::ExtensionFileSource;
-use crate::ExtensionFileSourceCode;
use crate::NoopModuleLoader;
use crate::OpMiddlewareFn;
use crate::OpResult;
@@ -869,14 +868,11 @@ impl JsRuntime {
{
let js_files = ext.get_js_sources();
for file_source in js_files {
- let ExtensionFileSourceCode::IncludedInBinary(code) =
- file_source.code;
-
// TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
realm.execute_script(
self.v8_isolate(),
&file_source.specifier,
- code,
+ &file_source.code.load()?,
)?;
}
}
diff --git a/core/snapshot_util.rs b/core/snapshot_util.rs
index a4bf80227..5b0ba92a0 100644
--- a/core/snapshot_util.rs
+++ b/core/snapshot_util.rs
@@ -33,6 +33,12 @@ pub fn create_snapshot(create_snapshot_options: CreateSnapshotOptions) {
snapshot_module_load_cb: create_snapshot_options.snapshot_module_load_cb,
..Default::default()
});
+ println!(
+ "JsRuntime for snapshot prepared, took {:#?} ({})",
+ Instant::now().saturating_duration_since(mark),
+ create_snapshot_options.snapshot_path.display()
+ );
+ mark = Instant::now();
let snapshot = js_runtime.snapshot();
let snapshot_slice: &[u8] = &snapshot;