{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathconvert.rs
More file actions
348 lines (328 loc) · 12.8 KB
/
Copy pathconvert.rs
File metadata and controls
348 lines (328 loc) · 12.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#![allow(clippy::empty_docs)] // TODO: remove it later. false positive by wasm-bindgen generated code
use crate::js_module;
use crate::vm_class::{WASMVirtualMachine, stored_vm_from_wasm};
use js_sys::{
Array, ArrayBuffer, JsString, Map, Object, Promise, Reflect, SyntaxError, Uint8Array,
};
use rustpython_common::wtf8::{Wtf8, Wtf8Buf};
use rustpython_vm::{
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
builtins::{PyBaseException, PyBaseExceptionRef, PyDict, PyList, PyStr, PyTuple},
compiler::{CompileError, ParseError, parser::LexicalErrorType, parser::ParseErrorType},
exceptions,
function::{ArgBytesLike, FuncArgs},
py_serde,
};
use wasm_bindgen::{JsCast, closure::Closure, prelude::*};
pub(crate) fn js_string_to_wtf8(value: &JsString) -> Wtf8Buf {
Wtf8Buf::from_wide(&value.iter().collect::<Vec<_>>())
}
fn wtf8_to_js_string(value: &Wtf8) -> JsString {
const CHUNK_SIZE: usize = 8192;
if let Ok(value) = value.as_str() {
return value.into();
}
value
.encode_wide()
.collect::<Vec<_>>()
.chunks(CHUNK_SIZE)
.map(JsString::from_char_code)
.collect::<Array>()
.join("")
}
#[wasm_bindgen(inline_js = r"
export class PyError extends Error {
constructor(info) {
const msg = info.args[0];
if (typeof msg === 'string') super(msg);
else super();
this.info = info;
}
get name() { return this.info.exc_type; }
get traceback() { return this.info.traceback; }
toString() { return this.info.rendered; }
}
")]
extern "C" {
pub type PyError;
#[wasm_bindgen(constructor)]
fn new(info: JsValue) -> PyError;
}
pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &Py<PyBaseException>) -> JsValue {
let js_err = vm.try_class("_js", "JSError").ok();
let js_arg = if js_err.is_some_and(|js_err| py_err.fast_isinstance(&js_err)) {
py_err.get_arg(0)
} else {
None
};
let js_arg = js_arg
.as_ref()
.and_then(|x| x.downcast_ref::<js_module::PyJsValue>());
match js_arg {
Some(val) => val.value.clone(),
None => {
let res =
serde_wasm_bindgen::to_value(&exceptions::SerializeException::new(vm, py_err));
match res {
Ok(err_info) => PyError::new(err_info).into(),
Err(_) => {
// Fallback: create a basic JS Error with the exception type and message
let exc_type = py_err.class().name().to_string();
let msg = match py_err.as_object().str(vm) {
Ok(s) => format!("{exc_type}: {s}"),
Err(_) => exc_type,
};
js_sys::Error::new(&msg).into()
}
}
}
}
}
pub fn js_py_typeerror(vm: &VirtualMachine, js_err: JsValue) -> PyBaseExceptionRef {
let msg: String = js_err.unchecked_into::<js_sys::Error>().to_string().into();
vm.new_type_error(msg)
}
pub fn js_err_to_py_err(vm: &VirtualMachine, js_err: &JsValue) -> PyBaseExceptionRef {
match js_err.dyn_ref::<js_sys::Error>() {
Some(err) => {
let exc_type = match String::from(err.name()).as_str() {
"TypeError" => vm.ctx.exceptions.type_error,
"ReferenceError" => vm.ctx.exceptions.name_error,
"SyntaxError" => vm.ctx.exceptions.syntax_error,
_ => vm.ctx.exceptions.exception_type,
}
.to_owned();
vm.new_exception_msg(exc_type, String::from(err.message()).into())
}
None => vm.new_exception_msg(
vm.ctx.exceptions.exception_type.to_owned(),
format!("{js_err:?}").into(),
),
}
}
pub fn py_to_js(vm: &VirtualMachine, py_obj: PyObjectRef) -> JsValue {
if let Some(ref wasm_id) = vm.wasm_id
&& py_obj.fast_isinstance(vm.ctx.types.function_type)
{
let wasm_vm = WASMVirtualMachine {
id: wasm_id.clone(),
};
let weak_py_obj = wasm_vm.push_held_rc(py_obj).unwrap().unwrap();
let closure = move |args: Option<Box<[JsValue]>>,
kwargs: Option<Object>|
-> Result<JsValue, JsValue> {
let py_obj = match wasm_vm.assert_valid() {
Ok(_) => weak_py_obj
.upgrade()
.expect("weak_py_obj to be valid if VM is valid"),
Err(err) => {
return Err(err);
}
};
stored_vm_from_wasm(&wasm_vm).interp.enter(move |vm| {
let args = match args {
Some(args) => Vec::from(args)
.into_iter()
.map(|arg| js_to_py(vm, arg))
.collect::<Vec<_>>(),
None => Vec::new(),
};
let mut py_func_args = FuncArgs::from(args);
if let Some(ref kwargs) = kwargs {
for pair in object_entries(kwargs) {
let (key, val) = pair?;
py_func_args
.kwargs
.insert(js_string_to_wtf8(&key.into()), js_to_py(vm, val));
}
}
let result = py_obj.call(py_func_args, vm);
pyresult_to_js_result(vm, result)
})
};
let closure = Closure::wrap(Box::new(closure)
as Box<
dyn FnMut(Option<Box<[JsValue]>>, Option<Object>) -> Result<JsValue, JsValue>,
>);
let func = closure.as_ref().clone();
// stores pretty much nothing, it's fine to leak this because if it gets dropped
// the error message is worse
closure.forget();
return func;
}
// the browser module might not be injected
if vm.try_class("_js", "Promise").is_ok()
&& let Some(py_prom) = py_obj.downcast_ref::<js_module::PyPromise>()
{
return py_prom.as_js(vm).into();
}
if let Ok(bytes) = ArgBytesLike::try_from_borrowed_object(vm, &py_obj) {
return bytes.with_ref(|bytes| unsafe {
// `Uint8Array::view` is an `unsafe fn` because it provides
// a direct view into the WASM linear memory; if you were to allocate
// something with Rust that view would probably become invalid. It's safe
// because we then copy the array using `Uint8Array::slice`.
let view = Uint8Array::view(bytes);
view.slice(0, bytes.len() as u32).into()
});
}
py_serde_to_js(vm, &py_obj).unwrap_or(JsValue::UNDEFINED)
}
fn py_serde_to_js(
vm: &VirtualMachine,
py_obj: &PyObjectRef,
) -> Result<JsValue, serde_wasm_bindgen::Error> {
if let Some(value) = py_obj.downcast_ref::<PyStr>() {
Ok(wtf8_to_js_string(value.as_wtf8()).into())
} else if let Some(value) = py_obj.downcast_ref::<PyList>() {
let array = Array::new();
for item in value.borrow_vec().iter() {
array.push(&py_serde_to_js(vm, item)?);
}
Ok(array.into())
} else if let Some(value) = py_obj.downcast_ref::<PyTuple>() {
let array = Array::new();
for item in value {
array.push(&py_serde_to_js(vm, item)?);
}
Ok(array.into())
} else if let Some(value) = py_obj.downcast_ref::<PyDict>() {
let map = Map::new();
for (key, value) in value {
map.set(&py_serde_to_js(vm, &key)?, &py_serde_to_js(vm, &value)?);
}
Ok(map.into())
} else {
py_serde::serialize(vm, py_obj, &serde_wasm_bindgen::Serializer::new())
}
}
pub fn object_entries(obj: &Object) -> impl Iterator<Item = Result<(JsValue, JsValue), JsValue>> {
Object::entries(obj).values().into_iter().map(|pair| {
pair.map(|pair| {
let key = Reflect::get(&pair, &"0".into()).unwrap();
let val = Reflect::get(&pair, &"1".into()).unwrap();
(key, val)
})
})
}
pub fn pyresult_to_js_result(vm: &VirtualMachine, result: PyResult) -> Result<JsValue, JsValue> {
result
.map(|value| py_to_js(vm, value))
.map_err(|err| py_err_to_js_err(vm, &err))
}
pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
if js_val.is_object() {
if let Some(promise) = js_val.dyn_ref::<Promise>() {
// the browser module might not be injected
if vm.try_class("browser", "Promise").is_ok() {
return js_module::PyPromise::new(promise.clone())
.into_ref(&vm.ctx)
.into();
}
}
if Array::is_array(&js_val) {
let js_arr: Array = js_val.into();
let elems = js_arr
.values()
.into_iter()
.map(|val| js_to_py(vm, val.expect("Iteration over array failed")))
.collect();
vm.ctx.new_list(elems).into()
} else if let Some(map) = js_val.dyn_ref::<Map>() {
let dict = vm.ctx.new_dict();
for entry in map.entries() {
let entry = Array::from(&entry.expect("Iteration over map failed"));
let key = js_to_py(vm, entry.get(0));
dict.set_item(&*key, js_to_py(vm, entry.get(1)), vm)
.unwrap();
}
dict.into()
} else if ArrayBuffer::is_view(&js_val) || js_val.is_instance_of::<ArrayBuffer>() {
// unchecked_ref because if it's not an ArrayBuffer it could either be a TypedArray
// or a DataView, but they all have a `buffer` property
let u8_array = js_sys::Uint8Array::new(
&js_val
.dyn_ref::<ArrayBuffer>()
.cloned()
.unwrap_or_else(|| js_val.unchecked_ref::<Uint8Array>().buffer()),
);
let mut vec = vec![0; u8_array.length() as usize];
u8_array.copy_to(&mut vec);
vm.ctx.new_bytes(vec).into()
} else {
let dict = vm.ctx.new_dict();
for pair in object_entries(&Object::from(js_val)) {
let (key, val) = pair.expect("iteration over object to not fail");
let py_val = js_to_py(vm, val);
dict.set_item(&*js_string_to_wtf8(&key.into()), py_val, vm)
.unwrap();
}
dict.into()
}
} else if js_val.is_function() {
let func = js_sys::Function::from(js_val);
vm.new_function(
vm.ctx.intern_str(String::from(func.name())).as_str(),
move |args: FuncArgs, vm: &VirtualMachine| -> PyResult {
let this = Object::new();
for (k, v) in args.kwargs {
Reflect::set(&this, &wtf8_to_js_string(&k).into(), &py_to_js(vm, v))
.expect("property to be settable");
}
let js_args = args
.args
.into_iter()
.map(|v| py_to_js(vm, v))
.collect::<Array>();
func.apply(&this, &js_args)
.map(|val| js_to_py(vm, val))
.map_err(|err| js_err_to_py_err(vm, &err))
},
)
.into()
} else if let Some(err) = js_val.dyn_ref::<js_sys::Error>() {
js_err_to_py_err(vm, err).into()
} else if js_val.is_undefined() {
// Because `JSON.stringify(undefined)` returns undefined
vm.ctx.none()
} else if js_val.is_string() {
vm.ctx.new_str(js_string_to_wtf8(&js_val.into())).into()
} else {
py_serde::deserialize(vm, serde_wasm_bindgen::Deserializer::from(js_val))
.unwrap_or_else(|_| vm.ctx.none())
}
}
#[must_use]
pub fn syntax_err(err: CompileError) -> SyntaxError {
let js_err = SyntaxError::new(&format!("Error parsing Python code: {err}"));
let _ = Reflect::set(
&js_err,
&"row".into(),
&(err.location().unwrap().line.get()).into(),
);
let _ = Reflect::set(
&js_err,
&"col".into(),
&(err.location().unwrap().character_offset.get()).into(),
);
// | ParseErrorType::UnrecognizedToken(Token::Dedent, _)
let can_continue = matches!(
&err,
CompileError::Parse(ParseError {
error: ParseErrorType::Lexical(
LexicalErrorType::Eof | LexicalErrorType::IndentationError
),
..
})
);
let _ = Reflect::set(&js_err, &"canContinue".into(), &can_continue.into());
js_err
}
pub trait PyResultExt<T> {
fn into_js(self, vm: &VirtualMachine) -> Result<T, JsValue>;
}
impl<T> PyResultExt<T> for PyResult<T> {
fn into_js(self, vm: &VirtualMachine) -> Result<T, JsValue> {
self.map_err(|err| py_err_to_js_err(vm, &err))
}
}
You can’t perform that action at this time.
