nodex-api 0.2.3

rust binding to node_api.h
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::{api, prelude::*};
use std::mem::MaybeUninit;

#[derive(Clone, Debug)]
#[repr(C)]
pub struct NapiPropertyDescriptor(napi_property_descriptor);

impl AsRef<napi_property_descriptor> for NapiPropertyDescriptor {
    fn as_ref(&self) -> &napi_property_descriptor {
        &self.0
    }
}

impl std::ops::Deref for NapiPropertyDescriptor {
    type Target = napi_property_descriptor;
    fn deref(&self) -> &napi_property_descriptor {
        &self.0
    }
}

impl NapiPropertyDescriptor {
    pub fn raw(&self) -> &napi_property_descriptor {
        &self.0
    }
}

/// The DescriptorBuild for value.
pub struct DescriptorValueBuilder {
    pub utf8name: Option<String>,
    pub name: napi_value,
    pub value: napi_value,
    pub attributes: NapiPropertyAttributes,
}

/// The DescriptorBuild for method.
/// NB: there seems no way to reclaim the napi_property_descriptor.data, so it is leaked.
#[allow(clippy::type_complexity)]
pub struct DescriptorMethodBuilder<T: FromJsArgs, R: NapiValueT> {
    pub utf8name: Option<String>,
    pub name: napi_value,
    pub method: Option<Box<dyn FnMut(JsObject, T) -> NapiResult<R> + 'static>>,
    pub attributes: NapiPropertyAttributes,
}

/// The DescriptorBuild for accessor.
/// NB: there seems no way to reclaim the napi_property_descriptor.data, so it is leaked.
#[allow(clippy::type_complexity)]
pub struct DescriptorAccessorBuilder<T: NapiValueT, R: NapiValueT> {
    pub utf8name: Option<String>,
    pub name: napi_value,
    pub getter: Option<Box<dyn FnMut(JsObject) -> NapiResult<R> + 'static>>,
    pub setter: Option<Box<dyn FnMut(JsObject, T) -> NapiResult<()> + 'static>>,
    pub attributes: NapiPropertyAttributes,
}

impl DescriptorValueBuilder {
    pub fn new() -> DescriptorValueBuilder {
        DescriptorValueBuilder {
            utf8name: None,
            name: std::ptr::null_mut(),
            value: std::ptr::null_mut(),
            attributes: NapiPropertyAttributes::Default,
        }
    }

    /// Optional string describing the key for the property, encoded as UTF8. One of utf8name or
    /// name must be provided for the property.
    pub fn with_utf8name(mut self, name: impl Into<String>) -> Self {
        self.utf8name.replace(name.into());
        self
    }

    /// Optional napi_value that points to a JavaScript string or symbol to be used as the key for
    /// the property. One of utf8name or name must be provided for the property.
    pub fn with_name(mut self, name: impl NapiValueT) -> Self {
        let name = name.value();
        if let (Ok(name_string), Ok(name_symbol)) = (
            unsafe { name.cast::<JsString>() }.check(),
            unsafe { name.cast::<JsSymbol>() }.check(),
        ) {
            if name_string || name_symbol {
                self.name = name.raw();
            }
        }
        self
    }

    /// The value that's retrieved by a get access of the property if the property is a data
    /// property. If this is passed in, set getter, setter, method and data to NULL (since these
    /// members won't be used).
    pub fn with_value(mut self, value: impl NapiValueT) -> Self {
        self.value = value.raw();
        self
    }

    /// The attributes associated with the particular property. See napi_property_attributes.
    pub fn with_attribute(mut self, attribute: NapiPropertyAttributes) -> Self {
        self.attributes |= attribute;
        self
    }

    /// build finale `NapiPropertyDescriptor`
    pub fn build(mut self) -> NapiResult<NapiPropertyDescriptor> {
        let utf8name = if let Some(name) = self.utf8name {
            std::ffi::CString::new(name)
                .map_err(|_| NapiStatus::StringExpected)?
                .into_raw()
        } else {
            std::ptr::null()
        };

        let name = self.name;

        // NB: panic if utf8name and name is both null
        if (utf8name.is_null() && name.is_null()) {
            return Err(NapiStatus::InvalidArg);
        }

        let method = None;
        let getter = None;
        let setter = None;

        let value = self.value;
        let attributes = self.attributes.bits();

        Ok(NapiPropertyDescriptor(napi_property_descriptor {
            utf8name,
            name,
            method,
            getter,
            setter,
            value,
            attributes,
            data: std::ptr::null_mut(),
        }))
    }
}

impl<T: FromJsArgs, R: NapiValueT> DescriptorMethodBuilder<T, R> {
    pub fn new() -> Self {
        Self {
            utf8name: None,
            name: std::ptr::null_mut(),
            method: None,
            attributes: NapiPropertyAttributes::Default,
        }
    }

    /// Optional string describing the key for the property, encoded as UTF8. One of utf8name or
    /// name must be provided for the property.
    pub fn with_utf8name(mut self, name: impl Into<String>) -> Self {
        self.utf8name.replace(name.into());
        self
    }

    /// Optional napi_value that points to a JavaScript string or symbol to be used as the key for
    /// the property. One of utf8name or name must be provided for the property.
    pub fn with_name(mut self, name: impl NapiValueT) -> Self {
        let name = name.value();
        if let (Ok(name_string), Ok(name_symbol)) = (
            unsafe { name.cast::<JsString>() }.check(),
            unsafe { name.cast::<JsSymbol>() }.check(),
        ) {
            if name_string || name_symbol {
                self.name = name.raw();
            }
        }
        self
    }

    /// Set this to make the property descriptor object's value property to be a JavaScript
    /// function represented by method. If this is passed in, set value, getter and setter to NULL
    /// (since these members won't be used). napi_callback provides more details.
    pub fn with_method(
        mut self,
        method: impl FnMut(JsObject, T) -> NapiResult<R> + 'static,
    ) -> Self {
        self.method = Some(Box::new(method));
        self
    }

    /// The attributes associated with the particular property. See napi_property_attributes.
    pub fn with_attribute(mut self, attribute: NapiPropertyAttributes) -> Self {
        self.attributes |= attribute;
        self
    }

    /// build finale `NapiPropertyDescriptor`
    #[allow(clippy::type_complexity)]
    pub fn build(mut self) -> NapiResult<NapiPropertyDescriptor> {
        let utf8name = if let Some(name) = self.utf8name {
            std::ffi::CString::new(name)
                .map_err(|_| NapiStatus::StringExpected)?
                .into_raw()
        } else {
            std::ptr::null()
        };

        let name = self.name;

        // NB: panic if utf8name and name is both null
        if (utf8name.is_null() && name.is_null()) {
            return Err(NapiStatus::InvalidArg);
        }

        extern "C" fn method_trampoline<T: FromJsArgs, R: NapiValueT>(
            env: NapiEnv,
            info: napi_callback_info,
        ) -> napi_value {
            let mut data = MaybeUninit::uninit();
            let mut this = MaybeUninit::uninit();

            let (argc, argv, this, mut func) = unsafe {
                let mut argc = T::len();
                let mut argv = vec![std::ptr::null_mut(); T::len()];

                let status = api::napi_get_cb_info(
                    env,
                    info,
                    &mut argc,
                    argv.as_mut_ptr(),
                    this.as_mut_ptr(),
                    data.as_mut_ptr(),
                );

                let func: &mut Box<dyn FnMut(JsObject, T) -> NapiResult<R>> =
                    std::mem::transmute(data);

                (argc, argv, this.assume_init(), func)
            };

            let args = argv
                .into_iter()
                .map(|arg| JsValue::from_raw(env, arg))
                .collect();
            let this = JsObject::from_raw(env, this);

            if let Ok(args) = T::from_js_args(JsArgs(args)) {
                napi_r!(env, =func(this, args))
            } else {
                env.throw_error("wrong argument type!").unwrap();
                env.undefined().unwrap().raw()
            }
        }

        let method = Some(method_trampoline::<T, R> as _);
        let data = if let Some(method) = self.method.take() {
            Box::into_raw(Box::new(method)) as _
        } else {
            return Err(NapiStatus::InvalidArg);
        };

        let getter = None;
        let setter = None;
        let value = std::ptr::null_mut();

        let attributes = self.attributes.bits();

        Ok(NapiPropertyDescriptor(napi_property_descriptor {
            utf8name,
            name,
            method,
            getter,
            setter,
            value,
            attributes,
            data,
        }))
    }
}

impl<T: NapiValueT, R: NapiValueT> DescriptorAccessorBuilder<T, R> {
    pub fn new() -> Self {
        Self {
            utf8name: None,
            name: std::ptr::null_mut(),
            getter: None,
            setter: None,
            attributes: NapiPropertyAttributes::Default,
        }
    }

    /// Optional string describing the key for the property, encoded as UTF8. One of utf8name or
    /// name must be provided for the property.
    pub fn with_utf8name(mut self, name: impl Into<String>) -> Self {
        self.utf8name.replace(name.into());
        self
    }

    /// Optional napi_value that points to a JavaScript string or symbol to be used as the key for
    /// the property. One of utf8name or name must be provided for the property.
    pub fn with_name(mut self, name: impl NapiValueT) -> Self {
        let name = name.value();
        if let (Ok(name_string), Ok(name_symbol)) = (
            unsafe { name.cast::<JsString>() }.check(),
            unsafe { name.cast::<JsSymbol>() }.check(),
        ) {
            if name_string || name_symbol {
                self.name = name.raw();
            }
        }
        self
    }

    ///  A function to call when a get access of the property is performed. If this is passed in,
    ///  set value and method to NULL (since these members won't be used). The given function is
    ///  called implicitly by the runtime when the property is accessed from JavaScript code (or if
    ///  a get on the property is performed using a Node-API call). napi_callback provides more
    ///  details.
    pub fn with_getter(mut self, getter: impl FnMut(JsObject) -> NapiResult<R> + 'static) -> Self {
        self.getter = Some(Box::new(getter));
        self
    }

    /// A function to call when a set access of the property is performed. If this is passed in,
    /// set value and method to NULL (since these members won't be used). The given function is
    /// called implicitly by the runtime when the property is set from JavaScript code (or if a set
    /// on the property is performed using a Node-API call). napi_callback provides more details.
    pub fn with_setter(
        mut self,
        setter: impl FnMut(JsObject, T) -> NapiResult<()> + 'static,
    ) -> Self {
        self.setter = Some(Box::new(setter));
        self
    }

    /// The attributes associated with the particular property. See napi_property_attributes.
    pub fn with_attribute(mut self, attribute: NapiPropertyAttributes) -> Self {
        self.attributes |= attribute;
        self
    }

    /// build finale `NapiPropertyDescriptor`
    #[allow(clippy::type_complexity)]
    pub fn build(mut self) -> NapiResult<NapiPropertyDescriptor> {
        let utf8name = if let Some(name) = self.utf8name {
            std::ffi::CString::new(name)
                .map_err(|_| NapiStatus::StringExpected)?
                .into_raw()
        } else {
            std::ptr::null()
        };

        let name = self.name;

        // NB: panic if utf8name and name is both null
        if (utf8name.is_null() && name.is_null()) {
            return Err(NapiStatus::InvalidArg);
        }

        extern "C" fn getter_trampoline<T: NapiValueT, R: NapiValueT>(
            env: NapiEnv,
            info: napi_callback_info,
        ) -> napi_value {
            let mut argc = 0;
            let mut argv = [std::ptr::null_mut(); 0];
            let mut data = MaybeUninit::uninit();
            let mut this = MaybeUninit::uninit();

            let (argc, argv, this, mut func) = unsafe {
                let status = api::napi_get_cb_info(
                    env,
                    info,
                    &mut argc,
                    argv.as_mut_ptr(),
                    this.as_mut_ptr(),
                    data.as_mut_ptr(),
                );

                // NB: the Function maybe called multiple times, so we can shoud leak the
                // closure memory here.
                //
                // With napi >= 5, we can add a finalizer to this function.
                let func: &mut (
                    Option<Box<dyn FnMut(JsObject) -> NapiResult<R>>>,
                    Option<Box<dyn FnMut(JsObject, T) -> NapiResult<()>>>,
                ) = std::mem::transmute(data);

                (argc, argv, this.assume_init(), func)
            };

            let this = JsObject::from_raw(env, this);

            napi_r!(env, =func.0.as_mut().unwrap()(this))
        }

        let mut data = (None, None);

        let getter = if let Some(getter) = self.getter {
            data.0 = Some(getter);
            Some(getter_trampoline::<T, R> as _)
        } else {
            None
        };

        extern "C" fn setter_trampoline<T: NapiValueT, R: NapiValueT>(
            env: NapiEnv,
            info: napi_callback_info,
        ) -> napi_value {
            let mut argc = 1;
            let mut argv = [std::ptr::null_mut(); 1];
            let mut data = MaybeUninit::uninit();
            let mut this = MaybeUninit::uninit();

            let (argc, argv, this, mut func) = unsafe {
                let status = api::napi_get_cb_info(
                    env,
                    info,
                    &mut argc,
                    argv.as_mut_ptr(),
                    this.as_mut_ptr(),
                    data.as_mut_ptr(),
                );

                let func: &mut (
                    Option<Box<dyn FnMut(JsObject) -> NapiResult<R>>>,
                    Option<Box<dyn FnMut(JsObject, T) -> NapiResult<()>>>,
                ) = std::mem::transmute(data);

                (argc, argv, this.assume_init(), func)
            };

            let value = T::from_raw(env, argv[0]);
            let this = JsObject::from_raw(env, this);

            napi_r!(env, func.1.as_mut().unwrap()(this, value))
        }

        let setter = if let Some(setter) = self.setter {
            data.1 = Some(setter);
            Some(setter_trampoline::<T, R> as _)
        } else {
            None
        };

        let attributes = self.attributes.bits();

        let method = None;
        let value = std::ptr::null_mut();

        Ok(NapiPropertyDescriptor(napi_property_descriptor {
            utf8name,
            name,
            method,
            getter,
            setter,
            value,
            attributes,
            data: Box::into_raw(Box::new(data)) as _,
        }))
    }
}

impl Default for DescriptorValueBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: FromJsArgs, R: NapiValueT> Default for DescriptorMethodBuilder<T, R> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: NapiValueT, R: NapiValueT> Default for DescriptorAccessorBuilder<T, R> {
    fn default() -> Self {
        Self::new()
    }
}