diff options
author | Gurwinder Singh <vargwin@gmail.com> | 2020-01-04 15:50:52 +0530 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2020-01-04 05:20:52 -0500 |
commit | 9f6bab6010abb26814d6d7a163db24fa5965d09c (patch) | |
tree | d680722aeffb6535de0f49689a4dbf8763836b2c /cli/compilers/wasm.rs | |
parent | 70b1be6ff459ebd2bf57b7788ab7d66c1f375b29 (diff) |
Use async at places, use &self instead of self: &Self (#3594)
Diffstat (limited to 'cli/compilers/wasm.rs')
-rw-r--r-- | cli/compilers/wasm.rs | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/cli/compilers/wasm.rs b/cli/compilers/wasm.rs index dbd951b03..e3297283b 100644 --- a/cli/compilers/wasm.rs +++ b/cli/compilers/wasm.rs @@ -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) + }) } } |