Use async at places, use &self instead of self: &Self (#3594)

This commit is contained in:
Gurwinder Singh 2020-01-04 15:50:52 +05:30 committed by Ry Dahl
parent 70b1be6ff4
commit 9f6bab6010
14 changed files with 160 additions and 197 deletions

View file

@ -10,7 +10,7 @@ pub struct JsCompiler {}
impl JsCompiler {
pub fn compile_async(
self: &Self,
&self,
source_file: &SourceFile,
) -> Pin<Box<CompiledModuleFuture>> {
let module = CompiledModule {

View file

@ -15,7 +15,7 @@ pub struct JsonCompiler {}
impl JsonCompiler {
pub fn compile_async(
self: &Self,
&self,
source_file: &SourceFile,
) -> Pin<Box<CompiledModuleFuture>> {
let maybe_json_value: serde_json::Result<serde_json::Value> =

View file

@ -142,7 +142,7 @@ impl CompiledFileMetadata {
None
}
pub fn to_json_string(self: &Self) -> Result<String, serde_json::Error> {
pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
let mut value_map = serde_json::map::Map::new();
value_map.insert(SOURCE_PATH.to_owned(), json!(&self.source_path));
@ -246,7 +246,7 @@ impl TsCompiler {
}
pub fn bundle_async(
self: &Self,
&self,
global_state: ThreadSafeGlobalState,
module_name: String,
out_file: Option<String>,
@ -305,7 +305,7 @@ impl TsCompiler {
///
/// If compilation is required then new V8 worker is spawned with fresh TS compiler.
pub fn compile_async(
self: &Self,
&self,
global_state: ThreadSafeGlobalState,
source_file: &SourceFile,
) -> Pin<Box<CompiledModuleFuture>> {
@ -389,7 +389,7 @@ impl TsCompiler {
}
/// Get associated `CompiledFileMetadata` for given module if it exists.
pub fn get_metadata(self: &Self, url: &Url) -> Option<CompiledFileMetadata> {
pub fn get_metadata(&self, url: &Url) -> Option<CompiledFileMetadata> {
// Try to load cached version:
// 1. check if there's 'meta' file
let cache_key = self
@ -409,7 +409,7 @@ impl TsCompiler {
}
pub fn get_compiled_module(
self: &Self,
&self,
module_url: &Url,
) -> Result<CompiledModule, ErrBox> {
let compiled_source_file = self.get_compiled_source_file(module_url)?;
@ -428,7 +428,7 @@ impl TsCompiler {
// TODO: ideally we shouldn't construct SourceFile by hand, but it should be delegated to
// SourceFileFetcher
pub fn get_compiled_source_file(
self: &Self,
&self,
module_url: &Url,
) -> Result<SourceFile, ErrBox> {
let cache_key = self
@ -453,7 +453,7 @@ impl TsCompiler {
/// Along compiled file a special metadata file is saved as well containing
/// hash that can be validated to avoid unnecessary recompilation.
fn cache_compiled_file(
self: &Self,
&self,
module_specifier: &ModuleSpecifier,
contents: &str,
) -> std::io::Result<()> {
@ -495,7 +495,7 @@ impl TsCompiler {
// TODO: ideally we shouldn't construct SourceFile by hand, but it should be delegated to
// SourceFileFetcher
pub fn get_source_map_file(
self: &Self,
&self,
module_specifier: &ModuleSpecifier,
) -> Result<SourceFile, ErrBox> {
let cache_key = self
@ -517,7 +517,7 @@ impl TsCompiler {
/// Save source map file for given TS module to on-disk cache.
fn cache_source_map(
self: &Self,
&self,
module_specifier: &ModuleSpecifier,
contents: &str,
) -> std::io::Result<()> {
@ -529,7 +529,7 @@ impl TsCompiler {
/// This method is called by TS compiler via an "op".
pub fn cache_compiler_output(
self: &Self,
&self,
module_specifier: &ModuleSpecifier,
extension: &str,
contents: &str,
@ -564,7 +564,7 @@ impl SourceMapGetter for TsCompiler {
// `SourceMapGetter` related methods
impl TsCompiler {
fn try_to_resolve(self: &Self, script_name: &str) -> Option<ModuleSpecifier> {
fn try_to_resolve(&self, script_name: &str) -> Option<ModuleSpecifier> {
// if `script_name` can't be resolved to ModuleSpecifier it's probably internal
// script (like `gen/cli/bundle/compiler.js`) so we won't be
// able to get source for it anyway

View file

@ -6,9 +6,7 @@ use crate::global_state::ThreadSafeGlobalState;
use crate::startup_data;
use crate::state::*;
use crate::worker::Worker;
use deno::Buf;
use futures::FutureExt;
use futures::TryFutureExt;
use serde_derive::Deserialize;
use serde_json;
use std::collections::HashMap;
@ -69,7 +67,7 @@ impl WasmCompiler {
}
pub fn compile_async(
self: &Self,
&self,
global_state: ThreadSafeGlobalState,
source_file: &SourceFile,
) -> Pin<Box<CompiledModuleFuture>> {
@ -86,47 +84,45 @@ impl WasmCompiler {
let worker_ = worker.clone();
let url = source_file.url.clone();
let fut = worker
.post_message(
serde_json::to_string(&base64_data)
.unwrap()
.into_boxed_str()
.into_boxed_bytes(),
)
.then(|_| worker)
.then(move |result| {
if let Err(err) = result {
// TODO(ry) Need to forward the error instead of exiting.
eprintln!("{}", err.to_string());
std::process::exit(1);
}
debug!("Sent message to worker");
worker_.get_message()
})
.map_err(|_| panic!("not handled"))
.and_then(move |maybe_msg: Option<Buf>| {
debug!("Received message from worker");
let json_msg = maybe_msg.unwrap();
let module_info: WasmModuleInfo =
serde_json::from_slice(&json_msg).unwrap();
debug!("WASM module info: {:#?}", &module_info);
let code = wrap_wasm_code(
&base64_data,
&module_info.import_list,
&module_info.export_list,
);
debug!("Generated code: {}", &code);
let module = CompiledModule {
code,
name: url.to_string(),
};
{
cache_.lock().unwrap().insert(url.clone(), module.clone());
}
debug!("<<<<< wasm_compile_async END");
futures::future::ok(module)
});
fut.boxed()
Box::pin(async move {
let _ = worker
.post_message(
serde_json::to_string(&base64_data)
.unwrap()
.into_boxed_str()
.into_boxed_bytes(),
)
.await;
if let Err(err) = worker.await {
// TODO(ry) Need to forward the error instead of exiting.
eprintln!("{}", err.to_string());
std::process::exit(1);
}
debug!("Sent message to worker");
let maybe_msg = worker_.get_message().await.expect("not handled");
debug!("Received message from worker");
let json_msg = maybe_msg.unwrap();
let module_info: WasmModuleInfo =
serde_json::from_slice(&json_msg).unwrap();
debug!("WASM module info: {:#?}", &module_info);
let code = wrap_wasm_code(
&base64_data,
&module_info.import_list,
&module_info.export_list,
);
debug!("Generated code: {}", &code);
let module = CompiledModule {
code,
name: url.to_string(),
};
{
cache_.lock().unwrap().insert(url.clone(), module.clone());
}
debug!("<<<<< wasm_compile_async END");
Ok(module)
})
}
}