1#[macro_use]
2extern crate quote;
3
4use std::collections::HashMap;
5use std::sync::LazyLock;
6
7use proc_macro2::TokenStream;
8
9#[macro_use]
10pub mod error;
11pub mod ast;
12pub mod codegen;
13#[cfg(feature = "type-def")]
14pub mod typegen;
15mod util;
16
17pub use ast::*;
18pub use codegen::*;
19pub use error::{BindgenResult, Diagnostic};
20#[cfg(feature = "type-def")]
21pub use semver;
22#[cfg(feature = "type-def")]
23pub use typegen::*;
24pub use util::to_case;
25
26#[derive(Debug)]
27pub struct Napi {
28 pub item: NapiItem,
29}
30
31macro_rules! napi_ast_impl {
32 ( $( ($v:ident, $ast:ident), )* ) => {
33 #[derive(Debug)]
34 #[allow(clippy::large_enum_variant)]
35 pub enum NapiItem {
36 $($v($ast)),*
37 }
38
39 impl TryToTokens for Napi {
40 fn try_to_tokens(&self, tokens: &mut TokenStream) -> BindgenResult<()> {
41 match self.item {
42 $( NapiItem::$v(ref ast) => ast.try_to_tokens(tokens) ),*
43 }
44 }
45 }
46
47 #[cfg(feature = "type-def")]
48 impl ToTypeDef for Napi {
49 fn to_type_def(&self) -> Option<TypeDef> {
50 match self.item {
51 $( NapiItem::$v(ref ast) => ast.to_type_def() ),*
52 }
53 }
54 }
55
56 impl Napi {
57 pub fn register_name(&self) -> String {
58 match self.item {
59 $( NapiItem::$v(ref ast) => ast.register_name.to_string() ),*
60 }
61 }
62 }
63 };
64}
65
66napi_ast_impl! {
67 (Fn, NapiFn),
68 (Struct, NapiStruct),
69 (Impl, NapiImpl),
70 (Enum, NapiEnum),
71 (Const, NapiConst),
72 (Type, NapiType),
73}
74
75pub(crate) static PRIMITIVE_TYPES: &[(&str, (&str, bool, bool))] = &[
76 ("JsUndefined", ("undefined", false, false)),
77 ("()", ("undefined", false, false)),
78 ("Undefined", ("undefined", false, false)),
79 ("JsNumber", ("number", false, false)),
80 ("i8", ("number", false, false)),
81 ("i16", ("number", false, false)),
82 ("i32", ("number", false, false)),
83 ("i64", ("number", false, false)),
84 ("f32", ("number", false, false)),
85 ("f64", ("number", false, false)),
86 ("u8", ("number", false, false)),
87 ("u16", ("number", false, false)),
88 ("u32", ("number", false, false)),
89 ("Number", ("number", false, false)),
91 ("u64", ("bigint", false, false)),
92 ("i64n", ("bigint", false, false)),
93 ("u128", ("bigint", false, false)),
94 ("i128", ("bigint", false, false)),
95 ("usize", ("bigint", false, false)),
96 ("isize", ("bigint", false, false)),
97 ("JsBigInt", ("bigint", false, false)),
98 ("BigInt", ("bigint", false, false)),
99 ("JsBoolean", ("boolean", false, false)),
100 ("bool", ("boolean", false, false)),
101 ("JsString", ("string", false, false)),
102 ("String", ("string", false, false)),
103 ("RawCString", ("string", false, false)),
104 ("str", ("string", false, false)),
105 ("Latin1String", ("string", false, false)),
106 ("Utf16String", ("string", false, false)),
107 ("JsStringUtf8", ("string", false, false)),
108 ("JsStringUtf16", ("string", false, false)),
109 ("JsStringLatin1", ("string", false, false)),
110 ("char", ("string", false, false)),
111 ("Null", ("null", false, false)),
112 ("JsNull", ("null", false, false)),
113 ("null", ("null", false, false)),
114 ("Symbol", ("symbol", false, false)),
115 ("JsSymbol", ("symbol", false, false)),
116 ("SymbolRef", ("symbol", false, false)),
117 ("JsFunction", ("(...args: any[]) => any", true, false)),
118];
119
120pub(crate) static TYPEDARRAY_SLICE_TYPES: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
121 HashMap::from([
122 ("u8", "Uint8Array"),
123 ("i8", "Int8Array"),
124 ("u16", "Uint16Array"),
125 ("i16", "Int16Array"),
126 ("u32", "Uint32Array"),
127 ("i32", "Int32Array"),
128 ("f32", "Float32Array"),
129 ("f64", "Float64Array"),
130 ("u64", "BigUint64Array"),
131 ("i64", "BigInt64Array"),
132 ])
133});