Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jan 13, 2023
1 parent 13a2d3a commit fb6388e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
51 changes: 51 additions & 0 deletions test_napi/exception_test.js
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);
});
94 changes: 94 additions & 0 deletions test_napi/src/exception.rs
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));
}
2 changes: 2 additions & 0 deletions test_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod r#async;
pub mod callback;
pub mod coerce;
pub mod error;
pub mod exception;
pub mod numbers;
pub mod object_wrap;
pub mod primitives;
Expand Down Expand Up @@ -135,6 +136,7 @@ unsafe extern "C" fn napi_register_module_v1(
object_wrap::init(env, exports);
callback::init(env, exports);
r#async::init(env, exports);
exception::init(env, exports);
init_cleanup_hook(env, exports);

exports
Expand Down
8 changes: 6 additions & 2 deletions test_napi/tests/napi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const BUILD_VARIANT: &str = "release";

fn build() {
let mut build_plugin_base = Command::new("cargo");
let mut build_plugin =
build_plugin_base.arg("build").arg("-p").arg("test_napi");
let mut build_plugin = build_plugin_base
.arg("build")
.arg("-p")
.arg("test_napi")
.arg("--all-targets");
if BUILD_VARIANT == "release" {
build_plugin = build_plugin.arg("--release");
}
Expand All @@ -32,6 +35,7 @@ fn napi_tests() {
.arg("--allow-ffi")
.arg("--allow-run")
.arg("--unstable")
.arg("exception_test.js")
.spawn()
.unwrap()
.wait_with_output()
Expand Down

0 comments on commit fb6388e

Please sign in to comment.