|
| 1 | +use crate::browser_module; |
1 | 2 | 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}; |
3 | 4 | use rustpython_vm::obj::{objbytes, objtype}; |
4 | 5 | use rustpython_vm::pyobject::{self, PyFuncArgs, PyObjectRef, PyResult}; |
5 | 6 | use rustpython_vm::VirtualMachine; |
@@ -61,6 +62,12 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue { |
61 | 62 |
|
62 | 63 | return func; |
63 | 64 | } |
| 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 | + } |
64 | 71 | } |
65 | 72 | if objtype::isinstance(&py_obj, &vm.ctx.bytes_type()) |
66 | 73 | || objtype::isinstance(&py_obj, &vm.ctx.bytearray_type()) |
@@ -108,6 +115,12 @@ pub fn pyresult_to_jsresult(vm: &mut VirtualMachine, result: PyResult) -> Result |
108 | 115 |
|
109 | 116 | pub fn js_to_py(vm: &mut VirtualMachine, js_val: JsValue) -> PyObjectRef { |
110 | 117 | 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 | + } |
111 | 124 | if Array::is_array(&js_val) { |
112 | 125 | let js_arr: Array = js_val.into(); |
113 | 126 | let elems = js_arr |
|
0 commit comments