neon/sys/
tag.rs

1use super::{
2    bindings as napi,
3    raw::{Env, Local},
4};
5
6/// Return true if an `napi_value` `val` has the expected value type.
7unsafe fn is_type(env: Env, val: Local, expect: napi::ValueType) -> bool {
8    let mut actual = napi::ValueType::Undefined;
9    napi::typeof_value(env, val, &mut actual as *mut _).unwrap();
10    actual == expect
11}
12
13pub unsafe fn is_undefined(env: Env, val: Local) -> bool {
14    is_type(env, val, napi::ValueType::Undefined)
15}
16
17pub unsafe fn is_null(env: Env, val: Local) -> bool {
18    is_type(env, val, napi::ValueType::Null)
19}
20
21/// Is `val` a JavaScript number?
22pub unsafe fn is_number(env: Env, val: Local) -> bool {
23    is_type(env, val, napi::ValueType::Number)
24}
25
26/// Is `val` a JavaScript boolean?
27pub unsafe fn is_boolean(env: Env, val: Local) -> bool {
28    is_type(env, val, napi::ValueType::Boolean)
29}
30
31/// Is `val` a JavaScript string?
32pub unsafe fn is_string(env: Env, val: Local) -> bool {
33    is_type(env, val, napi::ValueType::String)
34}
35
36pub unsafe fn is_object(env: Env, val: Local) -> bool {
37    is_type(env, val, napi::ValueType::Object)
38}
39
40pub unsafe fn is_array(env: Env, val: Local) -> bool {
41    let mut result = false;
42    napi::is_array(env, val, &mut result as *mut _).unwrap();
43    result
44}
45
46pub unsafe fn is_function(env: Env, val: Local) -> bool {
47    is_type(env, val, napi::ValueType::Function)
48}
49
50pub unsafe fn is_error(env: Env, val: Local) -> bool {
51    let mut result = false;
52    napi::is_error(env, val, &mut result as *mut _).unwrap();
53    result
54}
55
56/// Is `val` a Node.js Buffer instance?
57pub unsafe fn is_buffer(env: Env, val: Local) -> bool {
58    let mut result = false;
59    napi::is_buffer(env, val, &mut result as *mut _).unwrap();
60    result
61}
62
63/// Is `val` an ArrayBuffer instance?
64pub unsafe fn is_arraybuffer(env: Env, val: Local) -> bool {
65    let mut result = false;
66    napi::is_arraybuffer(env, val, &mut result as *mut _).unwrap();
67    result
68}
69
70/// Is `val` a TypedArray instance?
71pub unsafe fn is_typedarray(env: Env, val: Local) -> bool {
72    let mut result = false;
73    napi::is_typedarray(env, val, &mut result as *mut _).unwrap();
74    result
75}
76
77#[cfg(feature = "napi-5")]
78pub unsafe fn is_date(env: Env, val: Local) -> bool {
79    let mut result = false;
80    napi::is_date(env, val, &mut result as *mut _).unwrap();
81    result
82}
83
84/// Is `val` a Promise?
85///
86/// # Safety
87/// * `env` is a valid `napi_env` for the current thread
88pub unsafe fn is_promise(env: Env, val: Local) -> bool {
89    let mut result = false;
90    napi::is_promise(env, val, &mut result as *mut _).unwrap();
91    result
92}
93
94#[cfg(feature = "napi-8")]
95pub unsafe fn type_tag_object(env: Env, object: Local, tag: &super::TypeTag) {
96    napi::type_tag_object(env, object, tag as *const _).unwrap();
97}
98
99#[cfg(feature = "napi-8")]
100pub unsafe fn check_object_type_tag(env: Env, object: Local, tag: &super::TypeTag) -> bool {
101    let mut result = false;
102
103    napi::check_object_type_tag(env, object, tag as *const _, &mut result as *mut _).unwrap();
104    result
105}
106
107#[cfg(feature = "napi-6")]
108pub unsafe fn is_bigint(env: Env, val: Local) -> bool {
109    is_type(env, val, napi::ValueType::BigInt)
110}