Skip to content

Commit

Permalink
Port internal TS code to JS (denoland#6793)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Dahl <[email protected]>
  • Loading branch information
bartlomieju and ry committed Jul 19, 2020
1 parent 53adde8 commit fa61956
Show file tree
Hide file tree
Showing 204 changed files with 16,611 additions and 19,384 deletions.
152 changes: 82 additions & 70 deletions cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,111 +1,123 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::include_crate_modules;

use deno_core::js_check;
use deno_core::CoreIsolate;
use deno_core::StartupData;
use std::collections::HashMap;
use std::env;
use std::path::Path;
use std::path::PathBuf;

#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "windows")]
extern crate winres;

fn main() {
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
if env::var_os("RUSTDOCFLAGS").is_some() {
return;
fn create_snapshot(
mut isolate: CoreIsolate,
snapshot_path: &Path,
files: Vec<String>,
) {
for file in files {
println!("cargo:rerun-if-changed={}", file);
js_check(isolate.execute(&file, &std::fs::read_to_string(&file).unwrap()));
}

// To debug snapshot issues uncomment:
// deno_typescript::trace_serializer();

println!(
"cargo:rustc-env=TS_VERSION={}",
deno_typescript::ts_version()
);

println!(
"cargo:rustc-env=TARGET={}",
std::env::var("TARGET").unwrap()
);

let extern_crate_modules = include_crate_modules![deno_core];

let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());

// Main snapshot
let root_names = vec![c.join("js/main.ts")];
let bundle_path = o.join("CLI_SNAPSHOT.js");
let snapshot_path = o.join("CLI_SNAPSHOT.bin");

let main_module_name = deno_typescript::compile_bundle(
&bundle_path,
root_names,
Some(extern_crate_modules.clone()),
)
.expect("Bundle compilation failed");
assert!(bundle_path.exists());

let mut runtime_isolate = CoreIsolate::new(StartupData::None, true);
let snapshot = isolate.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());
}

deno_typescript::mksnapshot_bundle(
&mut runtime_isolate,
&snapshot_path,
&bundle_path,
&main_module_name,
)
.expect("Failed to create snapshot");

// Compiler snapshot
let root_names = vec![c.join("js/compiler.ts")];
let bundle_path = o.join("COMPILER_SNAPSHOT.js");
let snapshot_path = o.join("COMPILER_SNAPSHOT.bin");

let main_module_name = deno_typescript::compile_bundle(
&bundle_path,
root_names,
Some(extern_crate_modules),
)
.expect("Bundle compilation failed");
assert!(bundle_path.exists());
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<String>) {
let runtime_isolate = CoreIsolate::new(StartupData::None, true);
create_snapshot(runtime_isolate, snapshot_path, files);
}

fn create_compiler_snapshot(
snapshot_path: &Path,
files: Vec<String>,
cwd: &Path,
) {
let mut runtime_isolate = CoreIsolate::new(StartupData::None, true);

let mut custom_libs: HashMap<String, PathBuf> = HashMap::new();
custom_libs.insert(
"lib.deno.window.d.ts".to_string(),
c.join("js/lib.deno.window.d.ts"),
cwd.join("js2/lib.deno.window.d.ts"),
);
custom_libs.insert(
"lib.deno.worker.d.ts".to_string(),
c.join("js/lib.deno.worker.d.ts"),
cwd.join("js2/lib.deno.worker.d.ts"),
);
custom_libs.insert(
"lib.deno.shared_globals.d.ts".to_string(),
c.join("js/lib.deno.shared_globals.d.ts"),
cwd.join("js2/lib.deno.shared_globals.d.ts"),
);
custom_libs.insert(
"lib.deno.ns.d.ts".to_string(),
c.join("js/lib.deno.ns.d.ts"),
cwd.join("js2/lib.deno.ns.d.ts"),
);
custom_libs.insert(
"lib.deno.unstable.d.ts".to_string(),
c.join("js/lib.deno.unstable.d.ts"),
cwd.join("js2/lib.deno.unstable.d.ts"),
);
runtime_isolate.register_op(
"op_fetch_asset",
deno_typescript::op_fetch_asset(custom_libs),
);

deno_typescript::mksnapshot_bundle_ts(
&mut runtime_isolate,
&snapshot_path,
&bundle_path,
&main_module_name,
)
.expect("Failed to create snapshot");
js_check(
runtime_isolate.execute("typescript.js", deno_typescript::TYPESCRIPT_CODE),
);

create_snapshot(runtime_isolate, snapshot_path, files);
}

fn main() {
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
if env::var_os("RUSTDOCFLAGS").is_some() {
return;
}

// To debug snapshot issues uncomment:
// deno_typescript::trace_serializer();

println!(
"cargo:rustc-env=TS_VERSION={}",
deno_typescript::ts_version()
);

println!(
"cargo:rustc-env=TARGET={}",
std::env::var("TARGET").unwrap()
);

let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());

// Main snapshot
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");

let mut js_files = std::fs::read_dir("js2/")
.unwrap()
.map(|dir_entry| {
let file = dir_entry.unwrap();
file.path().to_string_lossy().to_string()
})
.filter(|filename| filename.ends_with(".js"))
.collect::<Vec<String>>();

js_files.sort();

let runtime_files = js_files
.clone()
.into_iter()
.filter(|filepath| !filepath.ends_with("compiler.js"))
.collect::<Vec<String>>();
create_runtime_snapshot(&runtime_snapshot_path, runtime_files);
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);

set_binary_metadata();
}
Expand Down
8 changes: 4 additions & 4 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,8 +1461,8 @@ mod tests {
.await;
assert!(r.is_err());

let p =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts");
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("js2/99_main.js");
let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
let r = fetcher
Expand All @@ -1484,8 +1484,8 @@ mod tests {
.await;
assert!(r.is_err());

let p =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts");
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("js2/99_main.js");
let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
let r = fetcher
Expand Down
14 changes: 4 additions & 10 deletions cli/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ pub const TS_VERSION: &str = env!("TS_VERSION");

pub static CLI_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin"));
pub static CLI_SNAPSHOT_MAP: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.js.map"));

pub static COMPILER_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
pub static COMPILER_SNAPSHOT_MAP: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.js.map"));

pub static DENO_NS_LIB: &str = include_str!("js/lib.deno.ns.d.ts");
pub static DENO_NS_LIB: &str = include_str!("js2/lib.deno.ns.d.ts");
pub static SHARED_GLOBALS_LIB: &str =
include_str!("js/lib.deno.shared_globals.d.ts");
pub static WINDOW_LIB: &str = include_str!("js/lib.deno.window.d.ts");
pub static UNSTABLE_NS_LIB: &str = include_str!("js/lib.deno.unstable.d.ts");
include_str!("js2/lib.deno.shared_globals.d.ts");
pub static WINDOW_LIB: &str = include_str!("js2/lib.deno.window.d.ts");
pub static UNSTABLE_NS_LIB: &str = include_str!("js2/lib.deno.unstable.d.ts");

#[test]
fn cli_snapshot() {
Expand Down
Loading

0 comments on commit fa61956

Please sign in to comment.