-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
class.rs
397 lines (341 loc) · 7.44 KB
/
class.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use napi::{
bindgen_prelude::{Buffer, ClassInstance, ObjectFinalize, This, Uint8Array, Unknown},
Env, Result,
};
use crate::r#enum::Kind;
/// `constructor` option for `struct` requires all fields to be public,
/// otherwise tag impl fn as constructor
/// #[napi(constructor)]
#[napi]
pub struct Animal {
#[napi(readonly)]
/// Kind of animal
pub kind: Kind,
name: String,
}
#[napi]
impl Animal {
/// This is the constructor
#[napi(constructor)]
pub fn new(kind: Kind, name: String) -> Self {
Animal { kind, name }
}
/// This is a factory method
#[napi(factory)]
pub fn with_kind(kind: Kind) -> Self {
Animal {
kind,
name: "Default".to_owned(),
}
}
#[napi(getter)]
pub fn get_name(&self) -> &str {
self.name.as_str()
}
#[napi(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[napi(getter, js_name = "type")]
pub fn kind(&self) -> Kind {
self.kind
}
#[napi(setter, js_name = "type")]
pub fn set_kind(&mut self, kind: Kind) {
self.kind = kind;
}
/// This is a
/// multi-line comment
/// with an emoji 🚀
#[napi]
pub fn whoami(&self) -> String {
match self.kind {
Kind::Dog => {
format!("Dog: {}", self.name)
}
Kind::Cat => format!("Cat: {}", self.name),
Kind::Duck => format!("Duck: {}", self.name),
}
}
#[napi]
/// This is static...
pub fn get_dog_kind() -> Kind {
Kind::Dog
}
#[napi]
/// Here are some characters and character sequences
/// that should be escaped correctly:
/// \[]{}/\:""
pub fn return_other_class(&self) -> Dog {
Dog {
name: "Doge".to_owned(),
}
}
#[napi]
pub fn return_other_class_with_custom_constructor(&self) -> Bird {
Bird::new("parrot".to_owned())
}
#[napi]
pub fn override_individual_arg_on_method(
&self,
normal_ty: String,
#[napi(ts_arg_type = "{n: string}")] overridden_ty: napi::JsObject,
) -> Bird {
let obj = overridden_ty.coerce_to_object().unwrap();
let the_n: Option<String> = obj.get("n").unwrap();
Bird::new(format!("{}-{}", normal_ty, the_n.unwrap()))
}
}
#[napi(constructor)]
pub struct Dog {
pub name: String,
}
#[napi]
pub struct Bird {
pub name: String,
}
#[napi]
impl Bird {
#[napi(constructor)]
pub fn new(name: String) -> Self {
Bird { name }
}
#[napi]
pub fn get_count(&self) -> u32 {
1234
}
}
/// Smoking test for type generation
#[napi]
#[repr(transparent)]
pub struct Blake2bHasher(u32);
#[napi]
impl Blake2bHasher {
#[napi(factory)]
pub fn with_key(key: &Blake2bKey) -> Self {
Blake2bHasher(key.get_inner())
}
}
#[napi]
impl Blake2bHasher {
#[napi]
pub fn update(&mut self, data: Buffer) {
self.0 += data.len() as u32;
}
}
#[napi]
pub struct Blake2bKey(u32);
impl Blake2bKey {
fn get_inner(&self) -> u32 {
self.0
}
}
#[napi]
pub struct Context {
data: String,
pub maybe_need: Option<bool>,
pub buffer: Uint8Array,
}
// Test for return `napi::Result` and `Result`
#[napi]
impl Context {
#[napi(constructor)]
pub fn new() -> napi::Result<Self> {
Ok(Self {
data: "not empty".into(),
maybe_need: None,
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
})
}
#[napi(factory)]
pub fn with_data(data: String) -> Result<Self> {
Ok(Self {
data,
maybe_need: Some(true),
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
})
}
#[napi(factory)]
pub fn with_buffer(buf: Uint8Array) -> Self {
Self {
data: "not empty".into(),
maybe_need: None,
buffer: buf,
}
}
#[napi]
pub fn method(&self) -> String {
self.data.clone()
}
}
#[napi(constructor)]
pub struct AnimalWithDefaultConstructor {
pub name: String,
pub kind: u32,
}
// Test for skip_typescript
#[napi]
pub struct NinjaTurtle {
pub name: String,
#[napi(skip_typescript)]
pub mask_color: String,
}
#[napi]
impl NinjaTurtle {
#[napi]
pub fn is_instance_of(env: Env, value: Unknown) -> Result<bool> {
Self::instance_of(env, value)
}
/// Create your ninja turtle! 🐢
#[napi(factory)]
pub fn new_raph() -> Self {
Self {
name: "Raphael".to_owned(),
mask_color: "Red".to_owned(),
}
}
/// We are not going to expose this character, so we just skip it...
#[napi(factory, skip_typescript)]
pub fn new_leo() -> Self {
Self {
name: "Leonardo".to_owned(),
mask_color: "Blue".to_owned(),
}
}
#[napi]
pub fn get_mask_color(&self) -> &str {
self.mask_color.as_str()
}
#[napi]
pub fn get_name(&self) -> &str {
self.name.as_str()
}
#[napi]
pub fn return_this(&self, this: This) -> This {
this
}
}
#[napi(js_name = "Assets")]
pub struct JsAssets {}
#[napi]
impl JsAssets {
#[napi(constructor)]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
JsAssets {}
}
#[napi]
pub fn get(&mut self, _id: u32) -> Option<JsAsset> {
Some(JsAsset {})
}
}
#[napi(js_name = "Asset")]
pub struct JsAsset {}
#[napi]
impl JsAsset {
#[napi(constructor)]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {}
}
#[napi(getter)]
pub fn get_file_path(&self) -> u32 {
1
}
}
#[napi]
pub struct Optional {}
#[napi]
impl Optional {
#[napi]
pub fn option_end(required: String, optional: Option<String>) -> String {
match optional {
None => required,
Some(optional) => format!("{} {}", required, optional),
}
}
#[napi]
pub fn option_start(optional: Option<String>, required: String) -> String {
match optional {
None => required,
Some(optional) => format!("{} {}", optional, required),
}
}
#[napi]
pub fn option_start_end(
optional1: Option<String>,
required: String,
optional2: Option<String>,
) -> String {
match (optional1, optional2) {
(None, None) => required,
(None, Some(optional2)) => format!("{} {}", required, optional2),
(Some(optional1), None) => format!("{} {}", optional1, required),
(Some(optional1), Some(optional2)) => format!("{} {} {}", optional1, required, optional2),
}
}
#[napi]
pub fn option_only(optional: Option<String>) -> String {
match optional {
None => "".to_string(),
Some(optional) => optional,
}
}
}
#[napi(object)]
pub struct ObjectFieldClassInstance {
pub bird: ClassInstance<Bird>,
}
#[napi]
pub fn create_object_with_class_field(env: Env) -> Result<ObjectFieldClassInstance> {
Ok(ObjectFieldClassInstance {
bird: Bird {
name: "Carolyn".to_owned(),
}
.into_instance(env)?,
})
}
#[napi]
pub fn receive_object_with_class_field(
object: ObjectFieldClassInstance,
) -> Result<ClassInstance<Bird>> {
Ok(object.bird)
}
#[napi(constructor)]
pub struct NotWritableClass {
#[napi(writable = false)]
pub name: String,
}
#[napi]
impl NotWritableClass {
#[napi(writable = false)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
}
#[napi(custom_finalize)]
pub struct CustomFinalize {
width: u32,
height: u32,
inner: Vec<u8>,
}
#[napi]
impl CustomFinalize {
#[napi(constructor)]
pub fn new(mut env: Env, width: u32, height: u32) -> Result<Self> {
let inner = vec![0; (width * height * 4) as usize];
let inner_size = inner.len();
env.adjust_external_memory(inner_size as i64)?;
Ok(Self {
width,
height,
inner,
})
}
}
impl ObjectFinalize for CustomFinalize {
fn finalize(self, mut env: Env) -> Result<()> {
env.adjust_external_memory(-(self.inner.len() as i64))?;
Ok(())
}
}