| 1 | // Copyright 2023 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use crate::error::{Error, Result}; |
| 16 | use neon::prelude::*; |
| 17 | |
| 18 | // extends neon's [JsObject] with helper functions to extract properties |
| 19 | pub trait JsObjectExt { |
| 20 | fn get_opt_u32(&self, cx: &mut FunctionContext, key: &str) -> Result<Option<u32>>; |
| 21 | fn get_usize(&self, cx: &mut FunctionContext, key: &str) -> Result<usize>; |
| 22 | #[allow(dead_code)] |
| 23 | fn get_opt_usize(&self, cx: &mut FunctionContext, key: &str) -> Result<Option<usize>>; |
| 24 | } |
| 25 | |
| 26 | impl JsObjectExt for JsObject { |
| 27 | fn get_opt_u32(&self, cx: &mut FunctionContext, key: &str) -> Result<Option<u32>> { |
| 28 | let val_opt = self |
| 29 | .get_opt::<JsNumber, _, _>(cx, key)? |
| 30 | .map(|s| f64_to_u32_safe(s.value(cx), key)); |
| 31 | val_opt.transpose() |
| 32 | } |
| 33 | |
| 34 | fn get_usize(&self, cx: &mut FunctionContext, key: &str) -> Result<usize> { |
| 35 | let val = self.get::<JsNumber, _, _>(cx, key)?.value(cx); |
| 36 | f64_to_usize_safe(val, key) |
| 37 | } |
| 38 | |
| 39 | fn get_opt_usize(&self, cx: &mut FunctionContext, key: &str) -> Result<Option<usize>> { |
| 40 | let val_opt = self |
| 41 | .get_opt::<JsNumber, _, _>(cx, key)? |
| 42 | .map(|s| f64_to_usize_safe(s.value(cx), key)); |
| 43 | val_opt.transpose() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn f64_to_u32_safe(n: f64, key: &str) -> Result<u32> { |
| 48 | use conv::*; |
| 49 | |
| 50 | n.approx_as::<u32>().map_err(|e| match e { |
| 51 | FloatError::NegOverflow(_) => Error::OutOfRange { |
| 52 | name: key.into(), |
| 53 | message: "must be > 0".to_string(), |
| 54 | }, |
| 55 | FloatError::PosOverflow(_) => Error::OutOfRange { |
| 56 | name: key.into(), |
| 57 | message: format!("must be < {}", u32::MAX), |
| 58 | }, |
| 59 | FloatError::NotANumber(_) => Error::OutOfRange { |
| 60 | name: key.into(), |
| 61 | message: "not a valid number".to_string(), |
| 62 | }, |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | fn f64_to_usize_safe(n: f64, key: &str) -> Result<usize> { |
| 67 | use conv::*; |
| 68 | |
| 69 | n.approx_as::<usize>().map_err(|e| match e { |
| 70 | FloatError::NegOverflow(_) => Error::OutOfRange { |
| 71 | name: key.into(), |
| 72 | message: "must be > 0".to_string(), |
| 73 | }, |
| 74 | FloatError::PosOverflow(_) => Error::OutOfRange { |
| 75 | name: key.into(), |
| 76 | message: format!("must be < {}", usize::MAX), |
| 77 | }, |
| 78 | FloatError::NotANumber(_) => Error::OutOfRange { |
| 79 | name: key.into(), |
| 80 | message: "not a valid number".to_string(), |
| 81 | }, |
| 82 | }) |
| 83 | } |