Merge pull request #389 from napi-rs/uncheck

feat(napi): uncheck version of Object methods
This commit is contained in:
LongYinan 2020-12-28 00:05:24 +08:00 committed by GitHub
commit a17999024b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 12 deletions

View file

@ -114,7 +114,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
)),
ValueType::Object => {
let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) };
let properties = js_object.get_property_names::<JsObject>()?;
let properties = js_object.get_property_names()?;
let property_len = properties.get_array_length_unchecked()?;
if property_len != 1 {
Err(Error::new(
@ -314,7 +314,7 @@ pub(crate) struct JsObjectAccess<'env> {
#[doc(hidden)]
impl<'env> JsObjectAccess<'env> {
fn new(value: &'env JsObject) -> Result<Self> {
let properties = value.get_property_names::<JsObject>()?;
let properties = value.get_property_names()?;
let property_len = properties.get_array_length_unchecked()?;
Ok(Self {
value,

View file

@ -14,10 +14,10 @@ pub struct JsFunction(pub(crate) Value);
///
/// #[js_function(1)]
/// pub fn call_function(ctx: CallContext) -> Result<JsNull> {
/// let js_func = ctx.get::<JsFunction>(0)?;
/// let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?;
/// js_func.call(None, &[js_string])?;
/// Ok(ctx.env.get_null()?)
/// let js_func = ctx.get::<JsFunction>(0)?;
/// let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?;
/// js_func.call(None, &[js_string])?;
/// Ok(ctx.env.get_null()?)
/// }
/// ```
impl JsFunction {
@ -53,6 +53,7 @@ impl JsFunction {
}
/// https://nodejs.org/api/n-api.html#n_api_napi_new_instance
///
/// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object.
#[allow(clippy::new_ret_no_self)]
#[inline]

View file

@ -257,6 +257,19 @@ macro_rules! impl_object_methods {
unsafe { T::from_raw(self.0.env, raw_value) }
}
#[inline]
pub fn get_property_unchecked<K, T>(&self, key: &K) -> Result<T>
where
K: NapiValue,
T: NapiValue,
{
let mut raw_value = ptr::null_mut();
check_status!(unsafe {
sys::napi_get_property(self.0.env, self.0.value, key.raw(), &mut raw_value)
})?;
Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) })
}
#[inline]
pub fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>
where
@ -301,6 +314,19 @@ macro_rules! impl_object_methods {
unsafe { T::from_raw(self.0.env, raw_value) }
}
#[inline]
pub fn get_named_property_unchecked<T>(&self, name: &str) -> Result<T>
where
T: NapiValue,
{
let key = CString::new(name)?;
let mut raw_value = ptr::null_mut();
check_status!(unsafe {
sys::napi_get_named_property(self.0.env, self.0.value, key.as_ptr(), &mut raw_value)
})?;
Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) })
}
#[inline]
pub fn has_named_property(&self, name: &str) -> Result<bool> {
let mut result = false;
@ -390,15 +416,12 @@ macro_rules! impl_object_methods {
}
#[inline]
pub fn get_property_names<T>(&self) -> Result<T>
where
T: NapiValue,
{
pub fn get_property_names(&self) -> Result<JsObject> {
let mut raw_value = ptr::null_mut();
check_status!(unsafe {
sys::napi_get_property_names(self.0.env, self.0.value, &mut raw_value)
})?;
unsafe { T::from_raw(self.0.env, raw_value) }
Ok(unsafe { JsObject::from_raw_unchecked(self.0.env, raw_value) })
}
/// https://nodejs.org/api/n-api.html#n_api_napi_get_all_property_names
@ -436,6 +459,16 @@ macro_rules! impl_object_methods {
unsafe { T::from_raw(self.0.env, result) }
}
#[inline]
pub fn get_prototype_unchecked<T>(&self) -> Result<T>
where
T: NapiValue,
{
let mut result = ptr::null_mut();
check_status!(unsafe { sys::napi_get_prototype(self.0.env, self.0.value, &mut result) })?;
Ok(unsafe { T::from_raw_unchecked(self.0.env, result) })
}
#[inline]
pub fn set_element<T>(&mut self, index: u32, value: T) -> Result<()>
where
@ -476,6 +509,18 @@ macro_rules! impl_object_methods {
unsafe { T::from_raw(self.0.env, raw_value) }
}
#[inline]
pub fn get_element_unchecked<T>(&self, index: u32) -> Result<T>
where
T: NapiValue,
{
let mut raw_value = ptr::null_mut();
check_status!(unsafe {
sys::napi_get_element(self.0.env, self.0.value, index, &mut raw_value)
})?;
Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) })
}
/// This method allows the efficient definition of multiple properties on a given object.
#[inline]
pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> {

View file

@ -91,7 +91,7 @@ fn test_get_property(ctx: CallContext) -> Result<JsUnknown> {
}
#[js_function(1)]
fn test_get_property_names(ctx: CallContext) -> Result<JsUnknown> {
fn test_get_property_names(ctx: CallContext) -> Result<JsObject> {
let obj = ctx.get::<JsObject>(0)?;
obj.get_property_names()
}