-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13a2d3a
commit fb6388e
Showing
4 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { | ||
assert, | ||
assertEquals, | ||
assertThrows, | ||
loadTestLibrary, | ||
} from "./common.js"; | ||
|
||
const theError = new Error("Some error"); | ||
|
||
const testException = (function () { | ||
let resultException; | ||
try { | ||
loadTestLibrary(); | ||
} catch (e) { | ||
resultException = e; | ||
} | ||
assertEquals(resultException.message, "Error during Init"); | ||
return resultException.binding; | ||
})(); | ||
|
||
Deno.test("napi exception", function () { | ||
const throwTheError = () => { | ||
throw theError; | ||
}; | ||
|
||
const returnedError = testException.returnException(throwTheError); | ||
assertEquals(returnedError, theError); | ||
|
||
let thrown = false; | ||
try { | ||
testException.allowException(throwTheError); | ||
} catch (e) { | ||
thrown = true; | ||
assertEquals(e, theError); | ||
} finally { | ||
assert(thrown); | ||
} | ||
|
||
const exceptionPending = testException.exceptionPending(); | ||
assertEquals(exceptionPending, true); | ||
|
||
let called = false; | ||
const cb = () => { | ||
called = true; | ||
}; | ||
const returnedError2 = testException.returnException(cb); | ||
assert(called); | ||
assertEquals(returnedError2, undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use crate::assert_napi_ok; | ||
use crate::cstr; | ||
use crate::napi_get_callback_info; | ||
use crate::napi_new_property; | ||
use napi_sys::Status::napi_pending_exception; | ||
use napi_sys::*; | ||
use std::ffi::c_char; | ||
use std::ptr; | ||
|
||
extern "C" fn return_exception( | ||
env: napi_env, | ||
info: napi_callback_info, | ||
) -> napi_value { | ||
let (args, argc, _) = napi_get_callback_info!(env, info, 1); | ||
assert_eq!(argc, 1); | ||
|
||
let mut global: napi_value = ptr::null_mut(); | ||
assert_napi_ok!(napi_get_global(env, &mut global)); | ||
|
||
let mut result: napi_value = ptr::null_mut(); | ||
let status = unsafe { | ||
napi_call_function( | ||
env, | ||
global, | ||
args[0], | ||
0, | ||
std::ptr::null_mut(), | ||
&mut result, | ||
) | ||
}; | ||
if status == napi_pending_exception { | ||
let mut ex: napi_value = ptr::null_mut(); | ||
assert_napi_ok!(napi_get_and_clear_last_exception(env, &mut ex)); | ||
return ex; | ||
} | ||
|
||
std::ptr::null_mut() | ||
} | ||
|
||
pub fn init(env: napi_env, exports: napi_value) { | ||
let properties = &[ | ||
napi_new_property!(env, "returnException", return_exception), | ||
// napi_new_property!(env, "allowException", allow_exception), | ||
// napi_new_property!( | ||
// env, | ||
// "constructReturnException", | ||
// construct_return_exception | ||
// ), | ||
// napi_new_property!( | ||
// env, | ||
// "constructAllowException", | ||
// construct_allow_exception | ||
// ), | ||
// napi_new_property!(env, "wasPending", was_pending), | ||
// napi_new_property!(env, "createExternal", create_external), | ||
]; | ||
|
||
assert_napi_ok!(napi_define_properties( | ||
env, | ||
exports, | ||
properties.len(), | ||
properties.as_ptr() | ||
)); | ||
|
||
let mut error: napi_value = ptr::null_mut(); | ||
let mut code: napi_value = ptr::null_mut(); | ||
let mut message: napi_value = ptr::null_mut(); | ||
assert_napi_ok!(napi_create_string_utf8( | ||
env, | ||
// TODO(bartlomieju): this is broken, if we pass usize::MAX then we shouldn't need | ||
// to null terminate... I think | ||
"Error during Init\0".as_ptr() as *const c_char, | ||
usize::MAX, | ||
&mut message | ||
)); | ||
assert_napi_ok!(napi_create_string_utf8( | ||
env, | ||
// TODO(bartlomieju): this is broken, if we pass usize::MAX then we shouldn't need | ||
// to null terminate... I think | ||
"\0".as_ptr() as *const c_char, | ||
usize::MAX, | ||
&mut code | ||
)); | ||
assert_napi_ok!(napi_create_error(env, code, message, &mut error)); | ||
assert_napi_ok!(napi_set_named_property( | ||
env, | ||
error, | ||
cstr!("binding"), | ||
exports | ||
)); | ||
assert_napi_ok!(napi_throw(env, error)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters