0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-03-10 05:56:52 -04:00

Make error constructors in Exception less repetitive (#394)

This commit is contained in:
Bert Belder 2020-05-31 20:41:43 +02:00
parent e4c260b8d2
commit 74d806cbe3
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461

View file

@ -260,55 +260,47 @@ impl Exception {
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
message: Local<String>, message: Local<String>,
) -> Local<'sc, Value> { ) -> Local<'sc, Value> {
let isolate = scope.isolate(); Self::new_error_with(scope, message, v8__Exception__Error)
isolate.enter();
let e = unsafe { v8__Exception__Error(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
} }
pub fn range_error<'sc>( pub fn range_error<'sc>(
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
message: Local<String>, message: Local<String>,
) -> Local<'sc, Value> { ) -> Local<'sc, Value> {
let isolate = scope.isolate(); Self::new_error_with(scope, message, v8__Exception__RangeError)
isolate.enter();
let e = unsafe { v8__Exception__RangeError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
} }
pub fn reference_error<'sc>( pub fn reference_error<'sc>(
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
message: Local<String>, message: Local<String>,
) -> Local<'sc, Value> { ) -> Local<'sc, Value> {
let isolate = scope.isolate(); Self::new_error_with(scope, message, v8__Exception__ReferenceError)
isolate.enter();
let e = unsafe { v8__Exception__ReferenceError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
} }
pub fn syntax_error<'sc>( pub fn syntax_error<'sc>(
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
message: Local<String>, message: Local<String>,
) -> Local<'sc, Value> { ) -> Local<'sc, Value> {
let isolate = scope.isolate(); Self::new_error_with(scope, message, v8__Exception__SyntaxError)
isolate.enter();
let e = unsafe { v8__Exception__SyntaxError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
} }
pub fn type_error<'sc>( pub fn type_error<'sc>(
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
message: Local<String>, message: Local<String>,
) -> Local<'sc, Value> { ) -> Local<'sc, Value> {
let isolate = scope.isolate(); Self::new_error_with(scope, message, v8__Exception__TypeError)
isolate.enter(); }
let e = unsafe { v8__Exception__TypeError(&*message) };
isolate.exit(); /// Internal helper to make the above error constructors less repetitive.
unsafe { scope.to_local(e) }.unwrap() fn new_error_with<'sc>(
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
contructor: unsafe extern "C" fn(*const String) -> *const Value,
) -> Local<'sc, Value> {
scope.isolate().enter();
let error = unsafe { scope.to_local((contructor)(&*message)) }.unwrap();
scope.isolate().exit();
error
} }
/// Creates an error message for the given exception. /// Creates an error message for the given exception.