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::{Object, Result};
use napi::JsFunction;
use napi_derive::napi;

#[napi(
  ts_args_type = "a: { foo: number,  bar: number, baz: number}",
  ts_return_type = "string[]"
)]
fn ts_rename(a: Object) -> Result<Object> {
  a.get_property_names()
}

#[napi]
fn override_individual_arg_on_function(
  not_overridden: String,
  #[napi(ts_arg_type = "() => string")] f: JsFunction,
  not_overridden2: u32,
) -> String {
  let u = f.call_without_args(None).unwrap();
  let s = u
    .coerce_to_string()
    .unwrap()
    .into_utf8()
    .unwrap()
    .as_str()
    .unwrap()
    .to_string();

  format!("oia: {}-{}-{}", not_overridden, not_overridden2, s)
}

#[napi]
fn override_individual_arg_on_function_with_cb_arg<
  T: Fn(String, Option<String>) -> Result<Object>,
>(
  #[napi(ts_arg_type = "(town: string, name?: string | undefined | null) => string")] callback: T,
  not_overridden: u32,
) -> Result<Object> {
  callback(format!("World({})", not_overridden), None)
}