Convert between JS Promises and PyPromises

This commit is contained in:
coolreader18 committed 2019-02-26 20:37:59 -06:00
1 parent ba1c5d78aa
commit 1d193e7fc7
2 files changed
+18 -5

No files matched your search

+4 -4
View File
@@ -167,7 +167,7 @@ impl PyPromise {
}
}
fn get_value(obj: &PyObjectRef) -> Promise {
pub fn get_promise_value(obj: &PyObjectRef) -> Promise {
if let PyObjectPayload::AnyRustValue { value } = &obj.payload {
if let Some(promise) = value.downcast_ref::<PyPromise>() {
return promise.value.clone();
@@ -176,7 +176,7 @@ fn get_value(obj: &PyObjectRef) -> Promise {
panic!("Inner error getting promise")
}
fn import_promise_type(vm: &mut VirtualMachine) -> PyResult {
pub fn import_promise_type(vm: &mut VirtualMachine) -> PyResult {
import(
vm,
PathBuf::default(),
@@ -202,7 +202,7 @@ fn promise_then(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let acc_vm = AccessibleVM::from_vm(vm);
let promise = get_value(zelf);
let promise = get_promise_value(zelf);
let ret_future = JsFuture::from(promise).then(move |res| {
let vm = &mut acc_vm
@@ -246,7 +246,7 @@ fn promise_catch(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let acc_vm = AccessibleVM::from_vm(vm);
let promise = get_value(zelf);
let promise = get_promise_value(zelf);
let ret_future = JsFuture::from(promise).then(move |res| match res {
Ok(val) => Ok(val),
+14 -1
View File
@@ -1,5 +1,6 @@
use crate::browser_module;
use crate::vm_class::{AccessibleVM, WASMVirtualMachine};
use js_sys::{Array, ArrayBuffer, Object, Reflect, Uint8Array};
use js_sys::{Array, ArrayBuffer, Object, Promise, Reflect, Uint8Array};
use rustpython_vm::obj::{objbytes, objtype};
use rustpython_vm::pyobject::{self, PyFuncArgs, PyObjectRef, PyResult};
use rustpython_vm::VirtualMachine;
@@ -61,6 +62,12 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
return func;
}
// the browser module might not be injected
if let Ok(promise_type) = browser_module::import_promise_type(vm) {
if objtype::isinstance(&py_obj, &promise_type) {
return browser_module::get_promise_value(&py_obj).into();
}
}
}
if objtype::isinstance(&py_obj, &vm.ctx.bytes_type())
|| objtype::isinstance(&py_obj, &vm.ctx.bytearray_type())
@@ -108,6 +115,12 @@ pub fn pyresult_to_jsresult(vm: &mut VirtualMachine, result: PyResult) -> Result
pub fn js_to_py(vm: &mut 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 let Ok(promise_type) = browser_module::import_promise_type(vm) {
return browser_module::PyPromise::new(promise_type, promise.clone());
}
}
if Array::is_array(&js_val) {
let js_arr: Array = js_val.into();
let elems = js_arr