Skip to content

Commit

Permalink
Trying to make the pool work by reusing "Working WASIX API" code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Aug 4, 2023
1 parent 1f36836 commit ec1e147
Show file tree
Hide file tree
Showing 15 changed files with 1,261 additions and 1,100 deletions.
23 changes: 0 additions & 23 deletions lib/wasi-web/src/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,6 @@ pub fn start(encoded_args: String) -> Result<(), JsValue> {
terminal.on_data(callback.as_ref().unchecked_ref());
callback.forget();

/*
{
let addon = FitAddon::new();
terminal.load_addon(addon.clone().dyn_into::<FitAddon>()?.into());
addon.fit();
}
*/

/*
{
let addon = WebLinksAddon::new();
terminal.load_addon(addon.clone().dyn_into::<WebLinksAddon>()?.into());
addon.fit();
}
*/

/*
{
let addon = WebglAddon::new(None);
terminal.load_addon(addon.clone().dyn_into::<WebglAddon>()?.into());
}
*/

{
let front_buffer: HtmlCanvasElement = front_buffer.clone().dyn_into().unwrap();
let terminal: Terminal = terminal.clone().dyn_into().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions lib/wasi-web/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ impl Drop for LeakyInterval {
}
}
}


2 changes: 2 additions & 0 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ js-sys = { version = "0.3", optional = true }
web-sys = { version = "0.3", optional = true, features = [
"BinaryType",
"Blob",
"BlobPropertyBag",
"DedicatedWorkerGlobalScope",
"FileReader",
"Headers",
Expand All @@ -82,6 +83,7 @@ web-sys = { version = "0.3", optional = true, features = [
"RequestInit",
"RequestMode",
"Response",
"Url",
"WebSocket",
"Window",
"Worker",
Expand Down
13 changes: 13 additions & 0 deletions lib/wasix/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ impl From<http::Request<Vec<u8>>> for HttpRequest {
}
}

impl From<http::Request<&str>> for HttpRequest {
fn from(value: http::Request<&str>) -> Self {
value.map(|body| body.to_string()).into()
}
}

impl From<http::Request<String>> for HttpRequest {
fn from(value: http::Request<String>) -> Self {
let (parts, body) = value.into_parts();
HttpRequest::from_http_parts(parts, body.into_bytes())
}
}

impl From<http::Request<()>> for HttpRequest {
fn from(value: http::Request<()>) -> Self {
let (parts, _) = value.into_parts();
Expand Down
4 changes: 2 additions & 2 deletions lib/wasix/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod client;
pub mod client_impl;

#[cfg(feature = "js")]
pub mod web;
pub mod web_http_client;

#[cfg(feature = "host-reqwest")]
pub mod reqwest;
Expand All @@ -17,7 +17,7 @@ pub fn default_http_client() -> Option<impl HttpClient + Send + Sync + 'static>
if #[cfg(feature = "host-reqwest")] {
Some(self::reqwest::ReqwestHttpClient::default())
} else if #[cfg(feature = "js")] {
Some(self::web::WebHttpClient::default())
Some(self::web_http_client::WebHttpClient::default())
} else {
// Note: We need something to use with turbofish otherwise returning
// a plain None will complain about not being able to infer the "T"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,79 @@ async fn fetch(request: HttpRequest) -> Result<HttpResponse, Error> {
.with_context(|| format!("Could not fetch '{url}'"))?
}
};
assert!(resp_value.is_instance_of::<web_sys::Response>());
let resp: web_sys::Response = resp_value.dyn_into().unwrap();

todo!()
let response = resp_value.dyn_ref().unwrap();
read_response(response).await
}

async fn read_response(response: &web_sys::Response) -> Result<HttpResponse, anyhow::Error> {
let status = http::StatusCode::from_u16(response.status())?;
let headers = headers(response.headers()).context("Unable to read the headers")?;
let body = get_response_data(response).await?;

Ok(HttpResponse {
body: Some(body),
redirected: response.redirected(),
status,
headers,
})
}

fn headers(headers: web_sys::Headers) -> Result<http::HeaderMap, anyhow::Error> {
let iter = js_sys::try_iter(&headers)
.map_err(js_error)?
.context("Not an iterator")?;
let mut header_map = http::HeaderMap::new();

for pair in iter {
let pair = pair.map_err(js_error)?;
let [key, value]: [js_sys::JsString; 2] =
js_array(&pair).context("Unable to unpack the header's key-value pairs")?;

let key = String::from(key);
let key: http::HeaderName = key.parse()?;
let value = String::from(value);
let value = http::HeaderValue::from_str(&value)
.with_context(|| format!("Invalid header value: {value}"))?;

header_map.insert(key, value);
}

Ok(header_map)
}

fn js_array<T, const N: usize>(value: &JsValue) -> Result<[T; N], anyhow::Error>
where
T: JsCast,
{
let array: &js_sys::Array = value.dyn_ref().context("Not an array")?;

let mut items = Vec::new();

for value in array.iter() {
let item = value
.dyn_into()
.map_err(|_| anyhow::anyhow!("Unable to cast to a {}", std::any::type_name::<T>()))?;
items.push(item);
}

<[T; N]>::try_from(items).map_err(|original| {
anyhow::anyhow!(
"Unable to turn a list of {} items into an array of {N} items",
original.len()
)
})
}

pub async fn get_response_data(resp: &web_sys::Response) -> Result<Vec<u8>, anyhow::Error> {
let buffer = JsFuture::from(resp.array_buffer().unwrap())
.await
.map_err(js_error)
.with_context(|| "Could not retrieve response body".to_string())?;

let buffer = js_sys::Uint8Array::new(&buffer);

Ok(buffer.to_vec())
}

fn call_fetch(request: &web_sys::Request) -> JsFuture {
Expand Down
7 changes: 3 additions & 4 deletions lib/wasix/src/runtime/module_cache/web_worker_module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wasmer::{Engine, Module};

use crate::runtime::{
module_cache::{CacheError, ModuleCache, ModuleHash, ThreadLocalCache},
task_manager::web::WebRunCommand,
task_manager::web_thread_pool::WebRunCommand,
};

/// A thin wrapper around [`ThreadLocalCache`] that will automatically share
Expand All @@ -19,7 +19,6 @@ pub struct WebWorkerModuleCache {
impl WebWorkerModuleCache {
fn cache_in_main(&self, key: ModuleHash, module: &Module, deterministic_id: &str) {
let deterministic_id = deterministic_id.to_string();
let inner = self.inner.clone();
let task = Box::new(WebRunCommand::ExecModule {
run: Box::new(move |module| {
let key = (key, deterministic_id);
Expand All @@ -32,7 +31,7 @@ impl WebWorkerModuleCache {
let module = JsValue::from(module.clone())
.dyn_into::<js_sys::WebAssembly::Module>()
.unwrap();
crate::runtime::task_manager::web::schedule_task(
crate::runtime::task_manager::web_thread_pool::schedule_task(
JsValue::from(task as u32),
module,
JsValue::NULL,
Expand Down Expand Up @@ -90,7 +89,7 @@ impl WebWorkerModuleCache {
// Annotation is here to prevent spurious IDE warnings.
#[allow(unused_unsafe)]
unsafe {
let entries = JsValue::from(cache).dyn_into::<js_sys::Array>().unwrap();
let entries = cache.dyn_into::<js_sys::Array>().unwrap();

for i in 0..entries.length() {
let entry = entries.get(i);
Expand Down
1 change: 1 addition & 0 deletions lib/wasix/src/runtime/resolver/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) fn file_path_from_url(url: &Url) -> Result<PathBuf, Error> {

#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;

#[test]
Expand Down
7 changes: 6 additions & 1 deletion lib/wasix/src/runtime/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
pub mod tokio;

#[cfg(feature = "js")]
pub mod web;
pub(crate) mod web;
#[cfg(feature = "js")]
pub(crate) mod web_thread_pool;

#[cfg(feature = "js")]
pub use self::{web::WebTaskManager, web_thread_pool::WebThreadPool};

use std::ops::Deref;
use std::task::{Context, Poll};
Expand Down
Loading

0 comments on commit ec1e147

Please sign in to comment.