forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wasmerio#343 from wasmerio/global-runtime
Use a global `Runtime` when one isn't explicitly provided
- Loading branch information
Showing
10 changed files
with
236 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use std::{ | ||
num::NonZeroUsize, | ||
ops::{Deref, DerefMut}, | ||
sync::Arc, | ||
}; | ||
|
||
use wasm_bindgen::{prelude::wasm_bindgen, JsCast}; | ||
|
||
use crate::{runtime::Runtime, tasks::ThreadPool, utils::Error}; | ||
|
||
#[derive(Clone, Debug, wasm_bindgen_derive::TryFromJsValue)] | ||
#[wasm_bindgen(js_name = "Runtime")] | ||
pub struct JsRuntime { | ||
// Note: This is just a wrapper around the "real" runtime implementation. | ||
// We need it because most JS-facing methods will want to accept/return an | ||
// `Arc<Runtime>`, but wasm-bindgen doesn't support passing Arc around | ||
// directly. | ||
rt: Arc<Runtime>, | ||
} | ||
|
||
#[wasm_bindgen(js_class = "Runtime")] | ||
impl JsRuntime { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(options: Option<RuntimeOptions>) -> Result<JsRuntime, Error> { | ||
let pool_size = options.as_ref().and_then(|options| options.pool_size()); | ||
|
||
let pool = match pool_size { | ||
Some(size) => { | ||
let size = NonZeroUsize::new(size).unwrap_or(NonZeroUsize::MIN); | ||
ThreadPool::new(size) | ||
} | ||
None => ThreadPool::new_with_max_threads()?, | ||
}; | ||
|
||
let registry = match options.as_ref().and_then(|opts| opts.registry()) { | ||
Some(registry_url) => registry_url.resolve(), | ||
None => Some(crate::DEFAULT_REGISTRY.to_string()), | ||
}; | ||
|
||
let mut rt = Runtime::new(pool); | ||
|
||
if let Some(registry) = registry.as_deref() { | ||
let api_key = options.as_ref().and_then(|opts| opts.api_key()); | ||
rt.set_registry(registry, api_key.as_deref())?; | ||
} | ||
|
||
Ok(JsRuntime { rt: Arc::new(rt) }) | ||
} | ||
|
||
/// Get a reference to the global runtime, optionally initializing it if | ||
/// requested. | ||
pub fn global(initialize: Option<bool>) -> Result<Option<JsRuntime>, Error> { | ||
match Runtime::global() { | ||
Some(rt) => Ok(Some(JsRuntime { rt })), | ||
None if initialize == Some(true) => { | ||
let rt = Runtime::lazily_initialized()?; | ||
Ok(Some(JsRuntime { rt })) | ||
} | ||
None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
impl Deref for JsRuntime { | ||
type Target = Arc<Runtime>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.rt | ||
} | ||
} | ||
|
||
impl DerefMut for JsRuntime { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.rt | ||
} | ||
} | ||
|
||
impl From<Arc<Runtime>> for JsRuntime { | ||
fn from(value: Arc<Runtime>) -> Self { | ||
JsRuntime { rt: value } | ||
} | ||
} | ||
|
||
impl AsRef<Arc<Runtime>> for JsRuntime { | ||
fn as_ref(&self) -> &Arc<Runtime> { | ||
self | ||
} | ||
} | ||
|
||
#[wasm_bindgen(typescript_custom_section)] | ||
const RUNTIME_OPTIONS_TYPE_DECLARATION: &str = r#" | ||
/** | ||
* Options used when constructing a {@link Runtime}. | ||
*/ | ||
export type RuntimeOptions = { | ||
/** | ||
* The number of worker threads to use. | ||
*/ | ||
poolSize?: number; | ||
/** | ||
* The GraphQL endpoint for the Wasmer registry used when looking up | ||
* packages. | ||
* | ||
* Defaults to `"https://registry.wasmer.io/graphql"`. | ||
* | ||
* If `null`, no queries will be made to the registry. | ||
*/ | ||
registry?: string | null; | ||
/** | ||
* An optional API key to use when sending requests to the Wasmer registry. | ||
*/ | ||
apiKey?: string; | ||
}; | ||
"#; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(typescript_type = "RuntimeOptions")] | ||
pub type RuntimeOptions; | ||
|
||
#[wasm_bindgen(method, getter, js_name = "poolSize")] | ||
fn pool_size(this: &RuntimeOptions) -> Option<usize>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn registry(this: &RuntimeOptions) -> Option<MaybeRegistryUrl>; | ||
|
||
#[wasm_bindgen(method, getter, js_name = "apiKey")] | ||
fn api_key(this: &RuntimeOptions) -> Option<String>; | ||
|
||
#[wasm_bindgen(typescript_type = "string | null | undefined")] | ||
type MaybeRegistryUrl; | ||
} | ||
|
||
impl MaybeRegistryUrl { | ||
fn resolve(&self) -> Option<String> { | ||
if self.is_undefined() { | ||
Some(crate::DEFAULT_REGISTRY.to_string()) | ||
} else if self.is_null() { | ||
None | ||
} else if let Some(s) = self.dyn_ref::<js_sys::JsString>() { | ||
Some(s.into()) | ||
} else { | ||
unreachable!() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.