Skip to content

Commit

Permalink
Document the implemented neon_runtime::napi functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Apr 3, 2020
1 parent ff8a4f2 commit 55700eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/neon-runtime/src/napi/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use nodejs_sys as napi;

use raw::{Env, Local};

/// Mutates the `out` argument to refer to a `napi_value` containing a newly created JavaScript Object.
pub unsafe extern "C" fn new(out: &mut Local, env: Env) {
napi::napi_create_object(env, out as *mut _);
}

/// Mutates the `out` argument to refer to a `napi_value` containing the own property names of the
/// `object` as a JavaScript Array.
pub unsafe extern "C" fn get_own_property_names(out: &mut Local, env: Env, object: Local) -> bool {
let status = napi::napi_get_property_names(env, object, out as *mut _);

Expand All @@ -19,6 +22,7 @@ pub unsafe extern "C" fn get_isolate(_obj: Local) -> Env {
unimplemented!()
}

/// Mutate the `out` argument to refer to the value at `index` in the given `object`. Returns `false` if the value couldn't be retrieved.
pub unsafe extern "C" fn get_index(out: &mut Local, env: Env, object: Local, index: u32) -> bool {
let status = napi::napi_get_element(env, object, index, out as *mut _);

Expand All @@ -39,6 +43,7 @@ pub unsafe extern "C" fn set_index(out: &mut bool, env: Env, object: Local, inde
*out
}

/// Mutate the `out` argument to refer to the value at a named `key` in the given `object`. Returns `false` if the value couldn't be retrieved.
pub unsafe extern "C" fn get_string(env: Env, out: &mut Local, object: Local, key: *const u8, len: i32) -> bool {
let mut key_val = MaybeUninit::uninit();

Expand Down Expand Up @@ -88,6 +93,8 @@ pub unsafe extern "C" fn set_string(env: Env, out: &mut bool, object: Local, key
true
}

/// Mutates `out` to refer to the value of the property of `object` named by the `key` value.
/// Returns false if the value couldn't be retrieved.
pub unsafe extern "C" fn get(out: &mut Local, env: Env, object: Local, key: Local) -> bool {
let status = napi::napi_get_property(env, object, key, out as *mut _);

Expand Down
4 changes: 4 additions & 0 deletions crates/neon-runtime/src/napi/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use raw::{Env, Local};

use nodejs_sys as napi;

/// Return true if an `napi_value` `val` has the expected value type.
unsafe fn is_type(env: Env, val: Local, expect: napi::napi_valuetype) -> bool {
let mut actual = napi::napi_valuetype::napi_undefined;
if napi::napi_typeof(env, val, &mut actual as *mut _) == napi::napi_status::napi_ok {
Expand All @@ -15,14 +16,17 @@ pub unsafe extern "C" fn is_undefined(_env: Env, _val: Local) -> bool { unimplem

pub unsafe extern "C" fn is_null(_env: Env, _val: Local) -> bool { unimplemented!() }

/// Is `val` a JavaScript number?
pub unsafe extern "C" fn is_number(env: Env, val: Local) -> bool {
is_type(env, val, napi::napi_valuetype::napi_number)
}

/// Is `val` a JavaScript boolean?
pub unsafe extern "C" fn is_boolean(env: Env, val: Local) -> bool {
is_type(env, val, napi::napi_valuetype::napi_boolean)
}

/// Is `val` a JavaScript string?
pub unsafe extern "C" fn is_string(env: Env, val: Local) -> bool {
is_type(env, val, napi::napi_valuetype::napi_string)
}
Expand Down

0 comments on commit 55700eb

Please sign in to comment.