deno.land / x / wasm@wasmer-sdk-v0.6.0 / src / js_runtime.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::{ 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)]#[repr(transparent)]#[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>,}
impl JsRuntime { pub fn new(rt: Arc<Runtime>) -> Self { JsRuntime { rt } }
pub fn into_inner(self) -> Arc<Runtime> { self.rt }}
#[wasm_bindgen(js_class = "Runtime")]impl JsRuntime { #[wasm_bindgen(constructor)] pub fn js_new(options: Option<RuntimeOptions>) -> Result<JsRuntime, Error> { let pool = ThreadPool::new();
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())?; }
if let Some(gateway) = options.as_ref().and_then(|opts| opts.network_gateway()) { rt.set_network_gateway(gateway); }
Ok(JsRuntime::new(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::new(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 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; /** * Enable networking (i.e. TCP and UDP) via a gateway server. */ networkGateway?: string;};"#;
#[wasm_bindgen]extern "C" { #[wasm_bindgen(typescript_type = "RuntimeOptions")] pub type RuntimeOptions;
#[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(method, getter, js_name = "networkGateway")] fn network_gateway(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!() } }}
wasm

Version Info

Tagged at
4 months ago