Skip to content

Commit

Permalink
Runtime compiler API.
Browse files Browse the repository at this point in the history
Also restructures the compiler TypeScript files to make them easier to
manage and eventually integrate deno_typescript fully.

Fixes denoland#2927
  • Loading branch information
kitsonk committed Jan 6, 2020
1 parent 8bf3837 commit 54b6526
Show file tree
Hide file tree
Showing 25 changed files with 2,570 additions and 870 deletions.
6 changes: 6 additions & 0 deletions cli/compilers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::ErrBox;
use futures::Future;
use serde_json::Value;

mod js;
mod json;
Expand All @@ -9,9 +10,14 @@ mod wasm;

pub use js::JsCompiler;
pub use json::JsonCompiler;
pub use ts::runtime_compile_async;
pub use ts::runtime_transpile_async;
pub use ts::TsCompiler;
pub use wasm::WasmCompiler;

pub type CompilationResultFuture =
dyn Future<Output = Result<Value, ErrBox>> + Send;

#[derive(Debug, Clone)]
pub struct CompiledModule {
pub code: String,
Expand Down
70 changes: 69 additions & 1 deletion cli/compilers/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
use crate::global_state::ThreadSafeGlobalState;
use crate::msg;
use crate::serde_json::json;
use crate::source_maps::SourceMapGetter;
use crate::startup_data;
use crate::state::*;
Expand All @@ -18,8 +19,10 @@ use deno_core::ModuleSpecifier;
use futures::future::FutureExt;
use futures::Future;
use regex::Regex;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::hash::BuildHasher;
use std::io;
use std::path::PathBuf;
use std::pin::Pin;
Expand Down Expand Up @@ -156,19 +159,22 @@ fn req(
root_names: Vec<String>,
compiler_config: CompilerConfig,
out_file: Option<String>,
bundle: bool,
) -> Buf {
let j = match (compiler_config.path, compiler_config.content) {
(Some(config_path), Some(config_data)) => json!({
"type": request_type as i32,
"rootNames": root_names,
"outFile": out_file,
"bundle": bundle,
"configPath": config_path,
"config": str::from_utf8(&config_data).unwrap(),
}),
_ => json!({
"type": request_type as i32,
"rootNames": root_names,
"outFile": out_file,
"bundle": bundle,
}),
};

Expand Down Expand Up @@ -258,10 +264,11 @@ impl TsCompiler {

let root_names = vec![module_name];
let req_msg = req(
msg::CompilerRequestType::Bundle,
msg::CompilerRequestType::Compile,
root_names,
self.config.clone(),
out_file,
true,
);

let worker = TsCompiler::setup_worker(global_state);
Expand Down Expand Up @@ -356,6 +363,7 @@ impl TsCompiler {
root_names,
self.config.clone(),
None,
false,
);

let worker = TsCompiler::setup_worker(global_state.clone());
Expand Down Expand Up @@ -599,6 +607,66 @@ impl TsCompiler {
}
}

pub fn runtime_compile_async<S: BuildHasher>(
global_state: ThreadSafeGlobalState,
root_name: &str,
sources: &Option<HashMap<String, String, S>>,
bundle: bool,
options: &Option<String>,
) -> Pin<Box<CompilationResultFuture>> {
let req_msg = json!({
"type": msg::CompilerRequestType::RuntimeCompile as i32,
"rootName": root_name,
"sources": sources,
"options": options,
"bundle": bundle,
})
.to_string()
.into_boxed_str()
.into_boxed_bytes();

let worker = TsCompiler::setup_worker(global_state.clone());
let worker_ = worker.clone();

async move {
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let msg = (worker_.get_message().await?).unwrap();
let json_str = std::str::from_utf8(&msg).unwrap();
Ok(json!(json_str))
}
.boxed()
}

pub fn runtime_transpile_async<S: BuildHasher>(
global_state: ThreadSafeGlobalState,
sources: &HashMap<String, String, S>,
options: &Option<String>,
) -> Pin<Box<CompilationResultFuture>> {
let req_msg = json!({
"type": msg::CompilerRequestType::RuntimeTranspile as i32,
"sources": sources,
"options": options,
})
.to_string()
.into_boxed_str()
.into_boxed_bytes();

let worker = TsCompiler::setup_worker(global_state.clone());
let worker_ = worker.clone();

async move {
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let msg = (worker_.get_message().await?).unwrap();
let json_str = std::str::from_utf8(&msg).unwrap();
Ok(json!(json_str))
}
.boxed()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 54b6526

Please sign in to comment.