Skip to content

Commit

Permalink
[WIP] Runtime Compiler API
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Dec 5, 2019
1 parent 214b3eb commit c491f49
Show file tree
Hide file tree
Showing 13 changed files with 1,571 additions and 332 deletions.
5 changes: 5 additions & 0 deletions cli/compilers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use deno::ErrBox;
use futures::Future;
use serde_json::Value;

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

pub use js::JsCompiler;
pub use json::JsonCompiler;
pub use ts::runtime_compile_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
35 changes: 35 additions & 0 deletions cli/compilers/ts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::compilers::CompilationResultFuture;
use crate::compilers::CompiledModule;
use crate::compilers::CompiledModuleFuture;
use crate::diagnostics::Diagnostic;
Expand All @@ -7,6 +8,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,6 +20,7 @@ use deno::ModuleSpecifier;
use futures::future::FutureExt;
use futures::Future;
use regex::Regex;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::io;
Expand Down Expand Up @@ -604,6 +607,38 @@ impl TsCompiler {
}
}

pub fn runtime_compile_async(
global_state: ThreadSafeGlobalState,
root_name: &str,
sources: &Option<HashMap<String, String>>,
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()
}

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

0 comments on commit c491f49

Please sign in to comment.