napi-rs/test_module/src/napi6/bigint.rs

62 lines
1.8 KiB
Rust
Raw Normal View History

2020-08-26 01:07:27 +09:00
use napi::{CallContext, JsBigint, JsNumber, JsObject, Result};
2020-07-10 20:14:32 +09:00
use std::convert::TryFrom;
2020-09-02 11:59:11 +09:00
#[js_function]
2020-07-10 20:14:32 +09:00
pub fn test_create_bigint_from_i64(ctx: CallContext) -> Result<JsBigint> {
ctx.env.create_bigint_from_i64(i64::max_value())
}
2020-09-02 11:59:11 +09:00
#[js_function]
2020-07-10 20:14:32 +09:00
pub fn test_create_bigint_from_u64(ctx: CallContext) -> Result<JsBigint> {
ctx.env.create_bigint_from_u64(u64::max_value())
}
2020-09-02 11:59:11 +09:00
#[js_function]
pub fn test_create_bigint_from_i128(ctx: CallContext) -> Result<JsBigint> {
ctx.env.create_bigint_from_i128(i128::max_value())
}
#[js_function]
pub fn test_create_bigint_from_u128(ctx: CallContext) -> Result<JsBigint> {
ctx.env.create_bigint_from_u128(u128::max_value())
}
#[js_function]
2020-07-10 20:14:32 +09:00
pub fn test_create_bigint_from_words(ctx: CallContext) -> Result<JsBigint> {
2020-08-26 01:07:27 +09:00
ctx
.env
.create_bigint_from_words(true, vec![u64::max_value(), u64::max_value()])
2020-07-10 20:14:32 +09:00
}
#[js_function(1)]
pub fn test_get_bigint_i64(ctx: CallContext) -> Result<JsNumber> {
let js_bigint = ctx.get::<JsBigint>(0)?;
let val = i64::try_from(js_bigint)?;
ctx.env.create_int32(val as i32)
}
#[js_function(1)]
pub fn test_get_bigint_u64(ctx: CallContext) -> Result<JsNumber> {
let js_bigint = ctx.get::<JsBigint>(0)?;
let val = u64::try_from(js_bigint)?;
ctx.env.create_int32(val as i32)
}
#[js_function(0)]
pub fn test_get_bigint_words(ctx: CallContext) -> Result<JsObject> {
2020-08-26 01:07:27 +09:00
let mut js_bigint = ctx
.env
.create_bigint_from_words(true, vec![i64::max_value() as u64, i64::max_value() as u64])?;
2020-07-10 20:14:32 +09:00
let mut js_arr = ctx.env.create_array_with_length(2)?;
2020-08-26 01:07:27 +09:00
let (_signed, words) = js_bigint.get_words()?;
2020-07-10 20:14:32 +09:00
js_arr.set_number_indexed_property(
ctx.env.create_int64(0)?,
2020-08-26 01:07:27 +09:00
ctx.env.create_bigint_from_u64(words[0])?,
2020-07-10 20:14:32 +09:00
)?;
js_arr.set_number_indexed_property(
ctx.env.create_int64(1)?,
2020-08-26 01:07:27 +09:00
ctx.env.create_bigint_from_u64(words[1])?,
2020-07-10 20:14:32 +09:00
)?;
Ok(js_arr)
}