Skip to content

Commit 1d193e7

Browse files
committed
Convert between JS Promises and PyPromises
1 parent ba1c5d7 commit 1d193e7

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

wasm/lib/src/browser_module.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl PyPromise {
167167
}
168168
}
169169

170-
fn get_value(obj: &PyObjectRef) -> Promise {
170+
pub fn get_promise_value(obj: &PyObjectRef) -> Promise {
171171
if let PyObjectPayload::AnyRustValue { value } = &obj.payload {
172172
if let Some(promise) = value.downcast_ref::<PyPromise>() {
173173
return promise.value.clone();
@@ -176,7 +176,7 @@ fn get_value(obj: &PyObjectRef) -> Promise {
176176
panic!("Inner error getting promise")
177177
}
178178

179-
fn import_promise_type(vm: &mut VirtualMachine) -> PyResult {
179+
pub fn import_promise_type(vm: &mut VirtualMachine) -> PyResult {
180180
import(
181181
vm,
182182
PathBuf::default(),
@@ -202,7 +202,7 @@ fn promise_then(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
202202

203203
let acc_vm = AccessibleVM::from_vm(vm);
204204

205-
let promise = get_value(zelf);
205+
let promise = get_promise_value(zelf);
206206

207207
let ret_future = JsFuture::from(promise).then(move |res| {
208208
let vm = &mut acc_vm
@@ -246,7 +246,7 @@ fn promise_catch(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
246246

247247
let acc_vm = AccessibleVM::from_vm(vm);
248248

249-
let promise = get_value(zelf);
249+
let promise = get_promise_value(zelf);
250250

251251
let ret_future = JsFuture::from(promise).then(move |res| match res {
252252
Ok(val) => Ok(val),

wasm/lib/src/convert.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use crate::browser_module;
12
use crate::vm_class::{AccessibleVM, WASMVirtualMachine};
2-
use js_sys::{Array, ArrayBuffer, Object, Reflect, Uint8Array};
3+
use js_sys::{Array, ArrayBuffer, Object, Promise, Reflect, Uint8Array};
34
use rustpython_vm::obj::{objbytes, objtype};
45
use rustpython_vm::pyobject::{self, PyFuncArgs, PyObjectRef, PyResult};
56
use rustpython_vm::VirtualMachine;
@@ -61,6 +62,12 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
6162

6263
return func;
6364
}
65+
// the browser module might not be injected
66+
if let Ok(promise_type) = browser_module::import_promise_type(vm) {
67+
if objtype::isinstance(&py_obj, &promise_type) {
68+
return browser_module::get_promise_value(&py_obj).into();
69+
}
70+
}
6471
}
6572
if objtype::isinstance(&py_obj, &vm.ctx.bytes_type())
6673
|| objtype::isinstance(&py_obj, &vm.ctx.bytearray_type())
@@ -108,6 +115,12 @@ pub fn pyresult_to_jsresult(vm: &mut VirtualMachine, result: PyResult) -> Result
108115

109116
pub fn js_to_py(vm: &mut VirtualMachine, js_val: JsValue) -> PyObjectRef {
110117
if js_val.is_object() {
118+
if let Some(promise) = js_val.dyn_ref::<Promise>() {
119+
// the browser module might not be injected
120+
if let Ok(promise_type) = browser_module::import_promise_type(vm) {
121+
return browser_module::PyPromise::new(promise_type, promise.clone());
122+
}
123+
}
111124
if Array::is_array(&js_val) {
112125
let js_arr: Array = js_val.into();
113126
let elems = js_arr

0 commit comments

Comments
 (0)