Skip to content

Commit 65f9a43

Browse files
committed
feat(napi): implement type specialization methods for NapiAny
1 parent 6eb248c commit 65f9a43

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

napi/src/result.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ptr;
66
use env::NapiEnv;
77
use sys::{napi_create_error, napi_create_range_error, napi_create_type_error,
88
napi_status, napi_value};
9+
use value::{NapiString, NapiValue};
910

1011
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1112
pub enum NapiErrorKind {
@@ -108,13 +109,13 @@ impl Display for NapiError {
108109

109110
macro_rules! error_constructor {
110111
($name:ident => $napi_fn_name:ident) => {
111-
pub fn $name(env: &NapiEnv, message: napi_value) -> NapiError {
112+
pub fn $name(env: &NapiEnv, message: &NapiString) -> NapiError {
112113
let mut exception = ptr::null_mut();
113114
let status = unsafe {
114115
$napi_fn_name(
115116
env.as_sys_env(),
116117
ptr::null_mut(),
117-
message,
118+
message.as_sys_value(),
118119
&mut exception,
119120
)
120121
};

napi/src/value/any.rs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::ptr;
22

33
use env::NapiEnv;
4-
use result::NapiResult;
4+
use result::{NapiError, NapiResult};
55
use sys;
66

7-
use super::NapiValue;
7+
use super::{NapiArray, NapiBoolean, NapiNull, NapiNumber, NapiObject,
8+
NapiString, NapiUndefined, NapiValue, NapiValueInternal,
9+
NapiValueType};
810

911
#[derive(Clone, Copy, Debug)]
1012
pub struct NapiAny<'a> {
@@ -25,6 +27,91 @@ impl<'a> NapiAny<'a> {
2527
pub fn with_value(env: &'a NapiEnv, value: sys::napi_value) -> Self {
2628
Self { env, value }
2729
}
30+
31+
pub fn as_undefined(&self) -> NapiResult<NapiUndefined<'a>> {
32+
match self.value_type()? {
33+
NapiValueType::Undefined => {
34+
Ok(NapiUndefined::construct(self.env(), self.as_sys_value()))
35+
}
36+
_ => Err(NapiError::type_error(
37+
self.env(),
38+
&NapiString::from_str(self.env(), "undefined expected")?,
39+
)),
40+
}
41+
}
42+
43+
pub fn as_null(&self) -> NapiResult<NapiNull<'a>> {
44+
match self.value_type()? {
45+
NapiValueType::Null => {
46+
Ok(NapiNull::construct(self.env(), self.as_sys_value()))
47+
}
48+
_ => Err(NapiError::type_error(
49+
self.env(),
50+
&NapiString::from_str(self.env(), "null expected")?,
51+
)),
52+
}
53+
}
54+
55+
pub fn as_boolean(&self) -> NapiResult<NapiBoolean<'a>> {
56+
match self.value_type()? {
57+
NapiValueType::Boolean => {
58+
Ok(NapiBoolean::construct(self.env(), self.as_sys_value()))
59+
}
60+
_ => Err(NapiError::type_error(
61+
self.env(),
62+
&NapiString::from_str(self.env(), "boolean expected")?,
63+
)),
64+
}
65+
}
66+
67+
pub fn as_number(&self) -> NapiResult<NapiNumber<'a>> {
68+
match self.value_type()? {
69+
NapiValueType::Number => {
70+
Ok(NapiNumber::construct(self.env(), self.as_sys_value()))
71+
}
72+
_ => Err(NapiError::type_error(
73+
self.env(),
74+
&NapiString::from_str(self.env(), "number expected")?,
75+
)),
76+
}
77+
}
78+
79+
pub fn as_string(&self) -> NapiResult<NapiString<'a>> {
80+
match self.value_type()? {
81+
NapiValueType::String => {
82+
Ok(NapiString::construct(self.env(), self.as_sys_value()))
83+
}
84+
_ => Err(NapiError::type_error(
85+
self.env(),
86+
&NapiString::from_str(self.env(), "string expected")?,
87+
)),
88+
}
89+
}
90+
91+
pub fn as_object(&self) -> NapiResult<NapiObject<'a>> {
92+
match self.value_type()? {
93+
NapiValueType::Object |
94+
NapiValueType::String |
95+
NapiValueType::Function => {
96+
Ok(NapiObject::construct(self.env(), self.as_sys_value()))
97+
}
98+
_ => Err(NapiError::type_error(
99+
self.env(),
100+
&NapiString::from_str(self.env(), "object expected")?,
101+
)),
102+
}
103+
}
104+
105+
pub fn as_array(&self) -> NapiResult<NapiArray<'a>> {
106+
if self.is_array()? {
107+
Ok(NapiArray::construct(self.env(), self.as_sys_value()))
108+
} else {
109+
Err(NapiError::type_error(
110+
self.env(),
111+
&NapiString::from_str(self.env(), "array expected")?,
112+
))
113+
}
114+
}
28115
}
29116

30117
impl<'a> NapiValue<'a> for NapiAny<'a> {

napi/src/value/null.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use env::NapiEnv;
44
use result::NapiResult;
55
use sys;
66

7-
use super::NapiValue;
7+
use super::{NapiValue, NapiValueInternal};
88

99
#[derive(Clone, Copy, Debug)]
1010
pub struct NapiNull<'a> {
@@ -32,3 +32,9 @@ impl<'a> NapiValue<'a> for NapiNull<'a> {
3232
self.env
3333
}
3434
}
35+
36+
impl<'a> NapiValueInternal<'a> for NapiNull<'a> {
37+
fn construct(env: &'a NapiEnv, value: sys::napi_value) -> Self {
38+
Self { env, value }
39+
}
40+
}

napi/src/value/undefined.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use env::NapiEnv;
44
use result::NapiResult;
55
use sys;
66

7-
use super::NapiValue;
7+
use super::{NapiValue, NapiValueInternal};
88

99
#[derive(Clone, Copy, Debug)]
1010
pub struct NapiUndefined<'a> {
@@ -32,3 +32,9 @@ impl<'a> NapiValue<'a> for NapiUndefined<'a> {
3232
self.env
3333
}
3434
}
35+
36+
impl<'a> NapiValueInternal<'a> for NapiUndefined<'a> {
37+
fn construct(env: &'a NapiEnv, value: sys::napi_value) -> Self {
38+
Self { env, value }
39+
}
40+
}

0 commit comments

Comments
 (0)