4afb7f60创建于 2024年9月30日历史提交
/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

use napi::bindgen_prelude::*;
use napi::{Env, JsObject};
use napi_derive::napi;

#[napi]
pub fn get_words() -> Vec<&'static str> {
  vec!["foo", "bar"]
}

#[napi]
fn get_nums() -> Vec<u32> {
  vec![1, 1, 2, 3, 5, 8]
}

#[napi]
fn sum_nums(nums: Vec<u32>) -> u32 {
  nums.iter().sum()
}

#[napi]
fn to_js_obj(env: Env) -> napi::Result<JsObject> {
  let mut arr = env.create_array(0)?;
  arr.insert("a string")?;
  arr.insert(42)?;
  arr.coerce_to_object()
}

#[napi]
fn get_num_arr() -> [u32; 2] {
  [1, 2]
}

#[napi]
fn get_nested_num_arr() -> [[[u32; 1]; 1]; 2] {
  [[[1]], [[1]]]
}

#[napi]
fn change_arr_len(mut arr: Array) -> Result<u32> {
  arr.set(0, 100)?;
  for i in 6..11 {
    arr.insert(i)?;
  }
  Ok(arr.len())
}

#[napi]
fn tuple_array(dat: (i32, i32, i32)) -> i32 {
  dat.0 + dat.1 + dat.2
}