2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
use std::env;
|
2020-12-14 12:42:40 -05:00
|
|
|
use std::path::Path;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
// This is a shim that allows to generate documentation on docs.rs
|
|
|
|
#[cfg(not(feature = "docsrs"))]
|
|
|
|
mod not_docs {
|
|
|
|
use super::*;
|
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this module contains a lot of duplicated
|
|
|
|
// logic with `cli/build.rs`, factor out to `deno_core`.
|
|
|
|
fn create_snapshot(
|
|
|
|
mut js_runtime: JsRuntime,
|
|
|
|
snapshot_path: &Path,
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
) {
|
|
|
|
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
|
|
|
|
// workspace root.
|
|
|
|
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
|
|
|
for file in files {
|
|
|
|
println!("cargo:rerun-if-changed={}", file.display());
|
|
|
|
let display_path = file.strip_prefix(display_root).unwrap();
|
|
|
|
let display_path_str = display_path.display().to_string();
|
|
|
|
js_runtime
|
|
|
|
.execute_script(
|
|
|
|
&("deno:".to_string() + &display_path_str.replace('\\', "/")),
|
|
|
|
&std::fs::read_to_string(&file).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let snapshot = js_runtime.snapshot();
|
|
|
|
let snapshot_slice: &[u8] = &*snapshot;
|
|
|
|
println!("Snapshot size: {}", snapshot_slice.len());
|
|
|
|
std::fs::write(&snapshot_path, snapshot_slice).unwrap();
|
|
|
|
println!("Snapshot written to: {} ", snapshot_path.display());
|
2020-12-14 12:42:40 -05:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:56:24 -04:00
|
|
|
struct Permissions;
|
|
|
|
|
|
|
|
impl deno_fetch::FetchPermissions for Permissions {
|
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_websocket::WebSocketPermissions for Permissions {
|
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_timers::TimersPermission for Permissions {
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_unstable(
|
|
|
|
&self,
|
|
|
|
_state: &deno_core::OpState,
|
|
|
|
_api_name: &'static str,
|
|
|
|
) {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ffi::FfiPermissions for Permissions {
|
2021-10-13 13:04:44 -04:00
|
|
|
fn check(
|
|
|
|
&mut self,
|
2021-12-15 09:41:49 -05:00
|
|
|
_path: Option<&Path>,
|
2021-10-13 13:04:44 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
2021-10-04 16:56:24 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_net::NetPermissions for Permissions {
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
_host: &(T, Option<u16>),
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
|
|
|
|
let extensions: Vec<Extension> = vec![
|
|
|
|
deno_webidl::init(),
|
|
|
|
deno_console::init(),
|
|
|
|
deno_url::init(),
|
|
|
|
deno_tls::init(),
|
|
|
|
deno_web::init(deno_web::BlobStore::default(), Default::default()),
|
2021-11-28 13:07:03 -05:00
|
|
|
deno_fetch::init::<Permissions>(Default::default()),
|
2021-10-04 16:56:24 -04:00
|
|
|
deno_websocket::init::<Permissions>("".to_owned(), None, None),
|
2021-08-11 12:01:30 -04:00
|
|
|
deno_webstorage::init(None),
|
|
|
|
deno_crypto::init(None),
|
|
|
|
deno_webgpu::init(false),
|
2021-10-04 16:56:24 -04:00
|
|
|
deno_timers::init::<Permissions>(),
|
2021-08-11 12:01:30 -04:00
|
|
|
deno_broadcast_channel::init(
|
|
|
|
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
|
|
|
|
false, // No --unstable.
|
|
|
|
),
|
2021-10-04 16:56:24 -04:00
|
|
|
deno_ffi::init::<Permissions>(false),
|
|
|
|
deno_net::init::<Permissions>(
|
2021-08-11 12:01:30 -04:00
|
|
|
None, false, // No --unstable.
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
deno_http::init(),
|
|
|
|
];
|
2020-12-14 12:42:40 -05:00
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
let js_runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
will_snapshot: true,
|
|
|
|
extensions,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
create_snapshot(js_runtime, snapshot_path, files);
|
|
|
|
}
|
2021-04-28 12:41:50 -04:00
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
fn get_js_files(d: &str) -> Vec<PathBuf> {
|
|
|
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let mut js_files = std::fs::read_dir(d)
|
|
|
|
.unwrap()
|
|
|
|
.map(|dir_entry| {
|
|
|
|
let file = dir_entry.unwrap();
|
|
|
|
manifest_dir.join(file.path())
|
|
|
|
})
|
|
|
|
.filter(|path| path.extension().unwrap_or_default() == "js")
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
js_files.sort();
|
|
|
|
js_files
|
|
|
|
}
|
2020-12-14 12:42:40 -05:00
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
pub fn build_snapshot(runtime_snapshot_path: PathBuf) {
|
|
|
|
let js_files = get_js_files("js");
|
|
|
|
create_runtime_snapshot(&runtime_snapshot_path, js_files);
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
2021-08-11 12:01:30 -04:00
|
|
|
}
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
fn main() {
|
2020-12-13 13:45:53 -05:00
|
|
|
// To debug snapshot issues uncomment:
|
|
|
|
// op_fetch_asset::trace_serializer();
|
|
|
|
|
|
|
|
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
|
|
|
|
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Main snapshot
|
|
|
|
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
|
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
// If we're building on docs.rs we just create
|
|
|
|
// and empty snapshot file and return, because `rusty_v8`
|
|
|
|
// doesn't actually compile on docs.rs
|
|
|
|
if env::var_os("DOCS_RS").is_some() {
|
|
|
|
let snapshot_slice = &[];
|
|
|
|
std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
#[cfg(not(feature = "docsrs"))]
|
|
|
|
not_docs::build_snapshot(runtime_snapshot_path)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|