Promises, promises

This commit is contained in:
John Doty 2023-06-29 10:10:40 -07:00
parent 17fdee51e6
commit a2dafeea12
6 changed files with 303 additions and 102 deletions

View file

@ -279,23 +279,8 @@ impl ContextRef {
}
/// Construct a new promise.
pub fn new_promise(&self) -> Result<Promise> {
unsafe {
let mut resolving_funcs: [sys::JSValue; 2] =
[sys::JS_MakeUndefined(), sys::JS_MakeUndefined()];
let val =
sys::JS_NewPromiseCapability(self.ctx, &mut resolving_funcs as *mut sys::JSValue);
if sys::JS_ValueGetTag(val) == sys::JS_TAG_EXCEPTION {
Err(self.exception_error())
} else {
Ok(Promise::new(
Value::from_raw(val, self),
Value::from_raw(resolving_funcs[0], self),
Value::from_raw(resolving_funcs[1], self),
))
}
}
pub fn new_promise(&self) -> Result<(Value, Promise)> {
self.get_runtime().new_promise(self)
}
/// Construct a new exception object, suitable for throwing.