Skip to content

Commit

Permalink
Refactor error tracking and scope juggling in deno_core (denoland#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jan 25, 2020
1 parent c21e000 commit 37a7b01
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 317 deletions.
1 change: 0 additions & 1 deletion cli/lib.rs
Expand Up @@ -354,7 +354,6 @@ fn run_repl(flags: DenoFlags) {
let result = worker.clone().await;
if let Err(err) = result {
eprintln!("{}", err.to_string());
worker.clear_exception();
}
}
};
Expand Down
8 changes: 0 additions & 8 deletions cli/ops/worker_host.rs
Expand Up @@ -202,7 +202,6 @@ fn op_host_poll_worker(
) -> Result<JsonOp, ErrBox> {
let args: WorkerArgs = serde_json::from_value(args)?;
let id = args.id as u32;
let state_ = state.clone();

let future = WorkerPollFuture {
state: state.clone(),
Expand All @@ -211,13 +210,6 @@ fn op_host_poll_worker(

let op = async move {
let result = future.await;

if result.is_err() {
let mut workers_table = state_.workers.lock().unwrap();
let worker = workers_table.get_mut(&id).unwrap();
worker.clear_exception();
}

Ok(serialize_worker_result(result))
};
Ok(JsonOp::Async(op.boxed()))
Expand Down
5 changes: 0 additions & 5 deletions cli/worker.rs
Expand Up @@ -139,11 +139,6 @@ impl Worker {
}
.boxed()
}

pub fn clear_exception(&mut self) {
let mut isolate = self.isolate.try_lock().unwrap();
isolate.clear_exception();
}
}

impl Future for Worker {
Expand Down
5 changes: 4 additions & 1 deletion core/any_error.rs
@@ -1,4 +1,7 @@
use std::any::{Any, TypeId};
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use std::any::Any;
use std::any::TypeId;
use std::convert::From;
use std::error::Error;
use std::fmt;
Expand Down
32 changes: 15 additions & 17 deletions core/bindings.rs
Expand Up @@ -270,31 +270,28 @@ pub extern "C" fn message_callback(
message: v8::Local<v8::Message>,
_exception: v8::Local<v8::Value>,
) {
let mut scope = v8::CallbackScope::new(message);
let mut scope = v8::HandleScope::new(scope.enter());
let scope = scope.enter();
let mut cbs = v8::CallbackScope::new(message);
let mut hs = v8::HandleScope::new(cbs.enter());
let scope = hs.enter();

let deno_isolate: &mut Isolate =
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };

assert!(!deno_isolate.global_context.is_empty());
let context = deno_isolate.global_context.get(scope).unwrap();

// TerminateExecution was called
if scope.isolate().is_execution_terminating() {
let u = v8::undefined(scope);
deno_isolate.handle_exception(scope, context, u.into());
let undefined = v8::undefined(scope).into();
deno_isolate.handle_exception(scope, undefined);
return;
}

let json_str = deno_isolate.encode_message_as_json(scope, context, message);
let json_str = deno_isolate.encode_message_as_json(scope, message);
deno_isolate.last_exception = Some(json_str);
}

pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
let mut scope = v8::CallbackScope::new(&message);
let mut scope = v8::HandleScope::new(scope.enter());
let scope = scope.enter();
let mut cbs = v8::CallbackScope::new(&message);
let mut hs = v8::HandleScope::new(cbs.enter());
let scope = hs.enter();

let deno_isolate: &mut Isolate =
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };
Expand All @@ -312,12 +309,12 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
let mut error_global = v8::Global::<v8::Value>::new();
error_global.set(scope, error);
deno_isolate
.pending_promise_map
.pending_promise_exceptions
.insert(promise_id, error_global);
}
v8::PromiseRejectEvent::PromiseHandlerAddedAfterReject => {
if let Some(mut handle) =
deno_isolate.pending_promise_map.remove(&promise_id)
deno_isolate.pending_promise_exceptions.remove(&promise_id)
{
handle.reset(scope);
}
Expand Down Expand Up @@ -411,7 +408,8 @@ fn send(
.ok();

// If response is empty then it's either async op or exception was thrown
let maybe_response = deno_isolate.dispatch_op(op_id, control, zero_copy);
let maybe_response =
deno_isolate.dispatch_op(scope, op_id, control, zero_copy);

if let Some(response) = maybe_response {
// Synchronous response.
Expand Down Expand Up @@ -566,7 +564,7 @@ fn error_to_json(
let context = deno_isolate.global_context.get(scope).unwrap();

let message = v8::Exception::create_message(scope, args.get(0));
let json_obj = encode_message_as_object(scope, context, message);
let json_obj = encode_message_as_object(scope, message);
let json_string = v8::json::stringify(context, json_obj.into()).unwrap();

rv.set(json_string.into());
Expand Down Expand Up @@ -661,9 +659,9 @@ pub fn module_resolve_callback<'s>(

pub fn encode_message_as_object<'a>(
s: &mut impl v8::ToLocal<'a>,
context: v8::Local<v8::Context>,
message: v8::Local<v8::Message>,
) -> v8::Local<'a, v8::Object> {
let context = s.isolate().get_current_context();
let json_obj = v8::Object::new(s);

let exception_str = message.get(s);
Expand Down

0 comments on commit 37a7b01

Please sign in to comment.