summaryrefslogtreecommitdiff
path: root/core/runtime.rs
diff options
context:
space:
mode:
Diffstat (limited to 'core/runtime.rs')
-rw-r--r--core/runtime.rs71
1 files changed, 43 insertions, 28 deletions
diff --git a/core/runtime.rs b/core/runtime.rs
index 9ead489af..d8355ae6d 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -44,7 +44,6 @@ use smallvec::SmallVec;
use std::any::Any;
use std::cell::RefCell;
use std::collections::HashMap;
-use std::collections::VecDeque;
use std::ffi::c_void;
use std::option::Option;
use std::pin::Pin;
@@ -176,7 +175,7 @@ pub struct JsRuntimeState {
/// instead of any other exceptions.
// TODO(nayeemrmn): This is polled in `exception_to_err_result()` which is
// flimsy. Try to poll it similarly to `pending_promise_rejections`.
- pub(crate) dispatched_exceptions: VecDeque<v8::Global<v8::Value>>,
+ pub(crate) dispatched_exception: Option<v8::Global<v8::Value>>,
pub(crate) inspector: Option<Rc<RefCell<JsRuntimeInspector>>>,
waker: AtomicWaker,
}
@@ -349,7 +348,7 @@ impl JsRuntime {
op_state: op_state.clone(),
waker: AtomicWaker::new(),
have_unpolled_ops: false,
- dispatched_exceptions: Default::default(),
+ dispatched_exception: None,
// Some fields are initialized later after isolate is created
inspector: None,
global_realm: None,
@@ -946,6 +945,27 @@ impl JsRuntime {
)
}
+ /// Call a function. If it returns a promise, run the event loop until that
+ /// promise is settled. If the promise rejects or there is an uncaught error
+ /// in the event loop, return `Err(error)`. Or return `Ok(<await returned>)`.
+ pub async fn call_and_await(
+ &mut self,
+ function: &v8::Global<v8::Function>,
+ ) -> Result<v8::Global<v8::Value>, Error> {
+ let promise = {
+ let scope = &mut self.handle_scope();
+ let cb = function.open(scope);
+ let this = v8::undefined(scope).into();
+ let promise = cb.call(scope, this, &[]);
+ if promise.is_none() || scope.is_execution_terminating() {
+ let undefined = v8::undefined(scope).into();
+ return exception_to_err_result(scope, undefined, false);
+ }
+ v8::Global::new(scope, promise.unwrap())
+ };
+ self.resolve_value(promise).await
+ }
+
/// Takes a snapshot. The isolate should have been created with will_snapshot
/// set to true.
///
@@ -1195,7 +1215,7 @@ impl JsRuntime {
if has_inspector {
// We poll the inspector first.
- let _ = self.inspector().borrow_mut().poll_unpin(cx);
+ let _ = self.inspector().borrow().poll_sessions(Some(cx)).unwrap();
}
self.pump_v8_message_loop()?;
@@ -1518,19 +1538,14 @@ pub(crate) fn exception_to_err_result<T>(
// to use the exception that was passed to it rather than the exception that
// was passed to this function.
let state = state_rc.borrow();
- exception = state
- .dispatched_exceptions
- .back()
- .map(|exception| v8::Local::new(scope, exception.clone()))
- .unwrap_or_else(|| {
- // Maybe make a new exception object.
- if was_terminating_execution && exception.is_null_or_undefined() {
- let message = v8::String::new(scope, "execution terminated").unwrap();
- v8::Exception::error(scope, message)
- } else {
- exception
- }
- });
+ exception = if let Some(exception) = &state.dispatched_exception {
+ v8::Local::new(scope, exception.clone())
+ } else if was_terminating_execution && exception.is_null_or_undefined() {
+ let message = v8::String::new(scope, "execution terminated").unwrap();
+ v8::Exception::error(scope, message)
+ } else {
+ exception
+ };
}
let mut js_error = JsError::from_v8_exception(scope, exception);
@@ -1738,7 +1753,7 @@ impl JsRuntime {
status = module.get_status();
let has_dispatched_exception =
- !state_rc.borrow_mut().dispatched_exceptions.is_empty();
+ state_rc.borrow_mut().dispatched_exception.is_some();
if has_dispatched_exception {
// This will be overrided in `exception_to_err_result()`.
let exception = v8::undefined(tc_scope).into();
@@ -2659,7 +2674,7 @@ pub mod tests {
.execute_script_static(
"filename.js",
r#"
-
+
var promiseIdSymbol = Symbol.for("Deno.core.internalPromiseId");
var p1 = Deno.core.opAsync("op_test", 42);
var p2 = Deno.core.opAsync("op_test", 42);
@@ -2715,7 +2730,7 @@ pub mod tests {
"filename.js",
r#"
let control = 42;
-
+
Deno.core.opAsync("op_test", control);
async function main() {
Deno.core.opAsync("op_test", control);
@@ -2734,7 +2749,7 @@ pub mod tests {
.execute_script_static(
"filename.js",
r#"
-
+
const p = Deno.core.opAsync("op_test", 42);
if (p[Symbol.for("Deno.core.internalPromiseId")] == undefined) {
throw new Error("missing id on returned promise");
@@ -2751,7 +2766,7 @@ pub mod tests {
.execute_script_static(
"filename.js",
r#"
-
+
Deno.core.opAsync("op_test");
"#,
)
@@ -2766,7 +2781,7 @@ pub mod tests {
.execute_script_static(
"filename.js",
r#"
-
+
let zero_copy_a = new Uint8Array([0]);
Deno.core.opAsync2("op_test", null, zero_copy_a);
"#,
@@ -3928,7 +3943,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
.execute_script_static(
"macrotasks_and_nextticks.js",
r#"
-
+
(async function () {
const results = [];
Deno.core.setMacrotaskCallback(() => {
@@ -4166,7 +4181,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
"",
format!(
r#"
-
+
globalThis.rejectValue = undefined;
Deno.core.setPromiseRejectCallback((_type, _promise, reason) => {{
globalThis.rejectValue = `{realm_name}/${{reason}}`;
@@ -4604,7 +4619,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
runtime.v8_isolate(),
"",
r#"
-
+
(async function () {
const buf = await Deno.core.opAsync("op_test", false);
let err;
@@ -4657,7 +4672,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
runtime.v8_isolate(),
"",
r#"
-
+
var promise = Deno.core.opAsync("op_pending");
"#,
)
@@ -4667,7 +4682,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
runtime.v8_isolate(),
"",
r#"
-
+
var promise = Deno.core.opAsync("op_pending");
"#,
)