Skip to content

Commit

Permalink
Implicitly enter Isolate in v8::error (denoland#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and piscisaureus committed Jan 5, 2020
1 parent a76339a commit bddefbc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
31 changes: 25 additions & 6 deletions src/exception.rs
Expand Up @@ -292,34 +292,53 @@ pub fn range_error<'sc>(
scope: &mut impl ToLocal<'sc>,
mut message: Local<String>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__Exception__RangeError(&mut *message)) }.unwrap()
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__RangeError(&mut *message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
}

pub fn reference_error<'sc>(
scope: &mut impl ToLocal<'sc>,
mut message: Local<String>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__Exception__ReferenceError(&mut *message)) }
.unwrap()
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__ReferenceError(&mut *message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
}

pub fn syntax_error<'sc>(
scope: &mut impl ToLocal<'sc>,
mut message: Local<String>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__Exception__SyntaxError(&mut *message)) }.unwrap()
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__SyntaxError(&mut *message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
}

pub fn type_error<'sc>(
scope: &mut impl ToLocal<'sc>,
mut message: Local<String>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__Exception__TypeError(&mut *message)) }.unwrap()
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__TypeError(&mut *message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
}

pub fn error<'sc>(
scope: &mut impl ToLocal<'sc>,
mut message: Local<String>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__Exception__Error(&mut *message)) }.unwrap()
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__Error(&mut *message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
}
47 changes: 21 additions & 26 deletions tests/test_api.rs
Expand Up @@ -754,33 +754,28 @@ fn exception() {
setup();
let mut params = v8::Isolate::create_params();
params.set_array_buffer_allocator(v8::new_default_allocator());
let mut isolate = v8::Isolate::new(params);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
isolate.enter();
{
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter();
let mut context = v8::Context::new(scope);
context.enter();
let reference = "This is a test error";
let local = v8::String::new(scope, reference).unwrap();
v8::range_error(scope, local);
v8::reference_error(scope, local);
v8::syntax_error(scope, local);
v8::type_error(scope, local);
let exception = v8::error(scope, local);
let msg = v8::create_message(scope, exception);
let msg_string = msg.get(scope);
let rust_msg_string = msg_string.to_rust_string_lossy(scope);
assert_eq!(
"Uncaught Error: This is a test error".to_string(),
rust_msg_string
);
assert!(v8::get_stack_trace(scope, exception).is_none());
context.exit();
}
drop(locker);
isolate.exit();
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter();
let mut context = v8::Context::new(scope);
context.enter();
let reference = "This is a test error";
let local = v8::String::new(scope, reference).unwrap();
v8::range_error(scope, local);
v8::reference_error(scope, local);
v8::syntax_error(scope, local);
v8::type_error(scope, local);
let exception = v8::error(scope, local);
let msg = v8::create_message(scope, exception);
let msg_string = msg.get(scope);
let rust_msg_string = msg_string.to_rust_string_lossy(scope);
assert_eq!(
"Uncaught Error: This is a test error".to_string(),
rust_msg_string
);
assert!(v8::get_stack_trace(scope, exception).is_none());
context.exit();
}

#[test]
Expand Down

0 comments on commit bddefbc

Please sign in to comment.