forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_module.rs
More file actions
263 lines (224 loc) · 8.29 KB
/
browser_module.rs
File metadata and controls
263 lines (224 loc) · 8.29 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use rustpython_vm::VirtualMachine;
pub(crate) use _browser::make_module;
#[pymodule]
mod _browser {
use crate::{convert, js_module::PyPromise, vm_class::weak_vm, wasm_builtins::window};
use js_sys::Promise;
use rustpython_vm::{
builtins::{PyDictRef, PyStrRef},
class::PyClassImpl,
convert::ToPyObject,
function::{ArgCallable, OptionalArg},
import::import_file,
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::JsFuture;
enum FetchResponseFormat {
Json,
Text,
ArrayBuffer,
}
impl FetchResponseFormat {
fn from_str(vm: &VirtualMachine, s: &str) -> PyResult<Self> {
match s {
"json" => Ok(FetchResponseFormat::Json),
"text" => Ok(FetchResponseFormat::Text),
"array_buffer" => Ok(FetchResponseFormat::ArrayBuffer),
_ => Err(vm.new_type_error("Unkown fetch response_format".into())),
}
}
fn get_response(&self, response: &web_sys::Response) -> Result<Promise, JsValue> {
match self {
FetchResponseFormat::Json => response.json(),
FetchResponseFormat::Text => response.text(),
FetchResponseFormat::ArrayBuffer => response.array_buffer(),
}
}
}
#[derive(FromArgs)]
struct FetchArgs {
#[pyarg(named, default)]
response_format: Option<PyStrRef>,
#[pyarg(named, default)]
method: Option<PyStrRef>,
#[pyarg(named, default)]
headers: Option<PyDictRef>,
#[pyarg(named, default)]
body: Option<PyObjectRef>,
#[pyarg(named, default)]
content_type: Option<PyStrRef>,
}
#[pyfunction]
fn fetch(url: PyStrRef, args: FetchArgs, vm: &VirtualMachine) -> PyResult {
let FetchArgs {
response_format,
method,
headers,
body,
content_type,
} = args;
let response_format = match response_format {
Some(s) => FetchResponseFormat::from_str(vm, s.as_str())?,
None => FetchResponseFormat::Text,
};
let mut opts = web_sys::RequestInit::new();
match method {
Some(s) => opts.method(s.as_str()),
None => opts.method("GET"),
};
if let Some(body) = body {
opts.body(Some(&convert::py_to_js(vm, body)));
}
let request = web_sys::Request::new_with_str_and_init(url.as_str(), &opts)
.map_err(|err| convert::js_py_typeerror(vm, err))?;
if let Some(headers) = headers {
let h = request.headers();
for (key, value) in headers {
let key = key.str(vm)?;
let value = value.str(vm)?;
h.set(key.as_str(), value.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))?;
}
}
if let Some(content_type) = content_type {
request
.headers()
.set("Content-Type", content_type.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))?;
}
let window = window();
let request_prom = window.fetch_with_request(&request);
let future = async move {
let val = JsFuture::from(request_prom).await?;
let response = val
.dyn_into::<web_sys::Response>()
.expect("val to be of type Response");
JsFuture::from(response_format.get_response(&response)?).await
};
Ok(PyPromise::from_future(future).into_pyobject(vm))
}
#[pyfunction]
fn request_animation_frame(func: ArgCallable, vm: &VirtualMachine) -> PyResult {
use std::{cell::RefCell, rc::Rc};
// this basic setup for request_animation_frame taken from:
// https://rustwasm.github.io/wasm-bindgen/examples/request-animation-frame.html
let f = Rc::new(RefCell::new(None));
let g = f.clone();
let weak_vm = weak_vm(vm);
*g.borrow_mut() = Some(Closure::wrap(Box::new(move |time: f64| {
let stored_vm = weak_vm
.upgrade()
.expect("that the vm is valid from inside of request_animation_frame");
stored_vm.interp.enter(|vm| {
let func = func.clone();
let args = vec![vm.ctx.new_float(time).into()];
let _ = vm.invoke(&func, args);
let closure = f.borrow_mut().take();
drop(closure);
})
}) as Box<dyn Fn(f64)>));
let id = window()
.request_animation_frame(&js_sys::Function::from(
g.borrow().as_ref().unwrap().as_ref().clone(),
))
.map_err(|err| convert::js_py_typeerror(vm, err))?;
Ok(vm.ctx.new_int(id).into())
}
#[pyfunction]
fn cancel_animation_frame(id: i32, vm: &VirtualMachine) -> PyResult<()> {
window()
.cancel_animation_frame(id)
.map_err(|err| convert::js_py_typeerror(vm, err))?;
Ok(())
}
#[pyattr]
#[pyclass(module = "browser", name)]
#[derive(Debug, PyPayload)]
struct Document {
doc: web_sys::Document,
}
#[pyclass]
impl Document {
#[pymethod]
fn query(&self, query: PyStrRef, vm: &VirtualMachine) -> PyResult {
let elem = self
.doc
.query_selector(query.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))?
.map(|elem| Element { elem })
.to_pyobject(vm);
Ok(elem)
}
}
#[pyattr]
fn document(vm: &VirtualMachine) -> PyRef<Document> {
PyRef::new_ref(
Document {
doc: window().document().expect("Document missing from window"),
},
Document::make_class(&vm.ctx),
None,
)
}
#[pyattr]
#[pyclass(module = "browser", name)]
#[derive(Debug, PyPayload)]
struct Element {
elem: web_sys::Element,
}
#[pyclass]
impl Element {
#[pymethod]
fn get_attr(
&self,
attr: PyStrRef,
default: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyObjectRef {
match self.elem.get_attribute(attr.as_str()) {
Some(s) => vm.ctx.new_str(s).into(),
None => default.unwrap_or_none(vm),
}
}
#[pymethod]
fn set_attr(&self, attr: PyStrRef, value: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
self.elem
.set_attribute(attr.as_str(), value.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))
}
}
#[pyfunction]
fn load_module(module: PyStrRef, path: PyStrRef, vm: &VirtualMachine) -> PyResult {
let weak_vm = weak_vm(vm);
let mut opts = web_sys::RequestInit::new();
opts.method("GET");
let request = web_sys::Request::new_with_str_and_init(path.as_str(), &opts)
.map_err(|err| convert::js_py_typeerror(vm, err))?;
let window = window();
let request_prom = window.fetch_with_request(&request);
let future = async move {
let val = JsFuture::from(request_prom).await?;
let response = val
.dyn_into::<web_sys::Response>()
.expect("val to be of type Response");
let text = JsFuture::from(response.text()?).await?;
let stored_vm = &weak_vm
.upgrade()
.expect("that the vm is valid when the promise resolves");
stored_vm.interp.enter(move |vm| {
let resp_text = text.as_string().unwrap();
let res = import_file(vm, module.as_str(), "WEB".to_owned(), resp_text);
match res {
Ok(_) => Ok(JsValue::null()),
Err(err) => Err(convert::py_err_to_js_err(vm, &err)),
}
})
};
Ok(PyPromise::from_future(future).into_pyobject(vm))
}
}
pub fn setup_browser_module(vm: &mut VirtualMachine) {
vm.add_native_module("_browser".to_owned(), Box::new(make_module));
vm.add_frozen(py_freeze!(dir = "Lib"));
}