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
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
use std::ptr;
use std::ffi::CString;
use std::collections::VecDeque;

use libc::size_t;
use log::error;
use log::debug;
use log::trace;

use crate::sys::napi_env;
use crate::sys::napi_value;
use crate::sys::napi_callback_info;
use crate::sys::napi_callback_raw;
use crate::sys::napi_finalize_raw;
use crate::sys::napi_valuetype;
use crate::sys::napi_get_property;
use crate::sys::napi_has_property;
use crate::sys::napi_ref;
use crate::sys::napi_deferred;
use crate::sys::napi_threadsafe_function_call_js;

use crate::napi_call_result;
use crate::napi_call_assert;
use crate::PropertiesBuilder;
use crate::NjError;
use crate::JSObjectWrapper;
use crate::JSValue;
use crate::TryIntoJs;

fn napi_value_type_to_string(js_type: napi_valuetype) -> &'static str {
    match js_type {
        crate::sys::napi_valuetype_napi_bigint => "big_int",
        crate::sys::napi_valuetype_napi_boolean => "bool",
        crate::sys::napi_valuetype_napi_number => "number",
        crate::sys::napi_valuetype_napi_string => "string",
        crate::sys::napi_valuetype_napi_symbol => "symbol",
        crate::sys::napi_valuetype_napi_function => "function",
        crate::sys::napi_valuetype_napi_null => "null",
        crate::sys::napi_valuetype_napi_external => "external",
        crate::sys::napi_valuetype_napi_undefined => "undefined",
        _ => "other",
    }
}

#[derive(Clone)]
pub struct JsNapiValue(napi_value);

unsafe impl Send for JsNapiValue {}

impl From<napi_value> for JsNapiValue {
    fn from(value: napi_value) -> Self {
        Self(value)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct JsEnv(napi_env);

impl From<napi_env> for JsEnv {
    fn from(env: napi_env) -> Self {
        Self(env)
    }
}

unsafe impl Send for JsEnv {}
unsafe impl Sync for JsEnv {}

impl JsEnv {
    pub fn new(env: napi_env) -> Self {
        Self(env)
    }

    pub fn inner(&self) -> napi_env {
        self.0
    }

    pub fn create_string_utf8(&self, r_string: &str) -> Result<napi_value, NjError> {
        trace!("create utf8 string: {}", r_string);
        use nj_sys::napi_create_string_utf8;

        let mut js_value = ptr::null_mut();
        napi_call_result!(napi_create_string_utf8(
            self.0,
            r_string.as_ptr() as *const i8,
            r_string.len(),
            &mut js_value
        ))?;
        Ok(js_value)
    }

    pub fn create_string_utf8_from_bytes(&self, r_string: &[u8]) -> Result<napi_value, NjError> {
        use nj_sys::napi_create_string_utf8;

        let mut js_value = ptr::null_mut();
        napi_call_result!(napi_create_string_utf8(
            self.0,
            r_string.as_ptr() as *const i8,
            r_string.len(),
            &mut js_value
        ))?;
        Ok(js_value)
    }

    pub fn create_double(&self, value: f64) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_create_double(self.0, value, &mut result))?;
        Ok(result)
    }

    pub fn create_int64(&self, value: i64) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_create_int64(self.0, value, &mut result))?;
        Ok(result)
    }

    pub fn create_int32(&self, value: i32) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_create_int32(self.0, value, &mut result))?;
        Ok(result)
    }

    pub fn create_bigint_uint64(&self, value: u64) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_create_bigint_uint64(
            self.0,
            value,
            &mut result
        ))?;
        Ok(result)
    }

    pub fn create_object(&self) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();

        napi_call_result!(crate::sys::napi_create_object(self.0, &mut result))?;
        Ok(result)
    }

    pub fn create_boolean(&self, value: bool) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_get_boolean(self.0, value, &mut result,))?;
        Ok(result)
    }

    pub fn create_array_with_len(&self, len: usize) -> Result<napi_value, NjError> {
        let mut array = ptr::null_mut();

        napi_call_result!(crate::sys::napi_create_array_with_length(
            self.0, len, &mut array
        ))?;
        Ok(array)
    }

    pub fn set_element(
        &self,
        object: napi_value,
        element: napi_value,
        index: usize,
    ) -> Result<(), NjError> {
        napi_call_result!(crate::sys::napi_set_element(
            self.0,
            object,
            index as u32,
            element
        ))?;
        Ok(())
    }

    pub fn get_element(&self, array: napi_value, index: u32) -> Result<napi_value, NjError> {
        let mut element = ptr::null_mut();

        napi_call_result!(crate::sys::napi_get_element(
            self.0,
            array,
            index,
            &mut element
        ))?;
        Ok(element)
    }

    /// check if napi value is array
    pub fn is_array(&self, array: napi_value) -> Result<bool, NjError> {
        let mut result: bool = false;

        napi_call_result!(crate::sys::napi_is_array(self.0, array, &mut result))?;

        Ok(result)
    }

    /// check if napi value is array buffer
    pub fn is_array_buffer(&self, array: napi_value) -> Result<bool, NjError> {
        let mut result: bool = false;

        napi_call_result!(crate::sys::napi_is_arraybuffer(self.0, array, &mut result))?;

        Ok(result)
    }

    pub fn is_buffer(&self, n_value: napi_value) -> Result<bool, NjError> {
        let mut result: bool = false;

        napi_call_result!(crate::sys::napi_is_buffer(self.0, n_value, &mut result))?;

        Ok(result)
    }

    pub fn is_date(&self, n_value: napi_value) -> Result<bool, NjError> {
        let mut result: bool = false;

        napi_call_result!(crate::sys::napi_is_date(self.0, n_value, &mut result))?;

        Ok(result)
    }

    pub fn is_error(&self, n_value: napi_value) -> Result<bool, NjError> {
        let mut result: bool = false;

        napi_call_result!(crate::sys::napi_is_error(self.0, n_value, &mut result))?;

        Ok(result)
    }

    pub fn get_global(&self) -> Result<napi_value, NjError> {
        use nj_sys::napi_get_global;

        let mut js_global = ptr::null_mut();
        napi_call_result!(napi_get_global(self.0, &mut js_global))?;
        Ok(js_global)
    }

    pub fn call_function(
        &self,
        recv: napi_value,
        func: napi_value,
        mut argv: Vec<napi_value>,
    ) -> Result<napi_value, NjError> {
        use nj_sys::napi_call_function;

        let mut result = ptr::null_mut();

        napi_call_result!(napi_call_function(
            self.0,
            recv,
            func,
            argv.len(),
            argv.as_mut_ptr(),
            &mut result
        ))?;

        Ok(result)
    }

    /// get callback information
    pub fn get_cb_info(
        &self,
        info: napi_callback_info,
        max_count: usize,
    ) -> Result<JsCallback, NjError> {
        use nj_sys::napi_get_cb_info;

        let mut this = ptr::null_mut();

        let mut argc: size_t = max_count as size_t;
        let mut args = vec![ptr::null_mut(); max_count];
        napi_call_result!(napi_get_cb_info(
            self.0,
            info,
            &mut argc,
            args.as_mut_ptr(),
            &mut this,
            ptr::null_mut()
        ))?;

        // truncate arg to actual received count
        args.resize(argc, ptr::null_mut());

        Ok(JsCallback::new(JsEnv::new(self.0), this, args))
    }

    /// define classes
    pub fn define_class(
        &self,
        name: &str,
        constructor: napi_callback_raw,
        properties: PropertiesBuilder,
    ) -> Result<napi_value, NjError> {
        let mut js_constructor = ptr::null_mut();
        let mut raw_properties = properties.as_raw_properties();

        debug!(
            "defining class: {} with {} properties",
            name,
            raw_properties.len()
        );
        napi_call_result!(crate::sys::napi_define_class(
            self.0,
            name.as_ptr() as *const i8,
            name.len(),
            Some(constructor),
            ptr::null_mut(),
            raw_properties.len(),
            raw_properties.as_mut_ptr(),
            &mut js_constructor
        ))?;

        Ok(js_constructor)
    }

    pub fn create_reference(&self, cons: napi_value, count: u32) -> Result<napi_ref, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_create_reference(
            self.0,
            cons,
            count,
            &mut result
        ))?;

        Ok(result)
    }

    pub fn delete_reference(&self, ref_: napi_ref) -> Result<(), NjError> {
        Ok(napi_call_result!(crate::sys::napi_delete_reference(
            self.0, ref_
        ))?)
    }

    pub fn get_new_target(&self, info: napi_callback_info) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_get_new_target(self.0, info, &mut result))?;

        Ok(result)
    }

    pub fn wrap(
        &self,
        js_object: napi_value,
        rust_obj: *mut u8,
        finalize: napi_finalize_raw,
    ) -> Result<napi_ref, NjError> {
        let mut result = ptr::null_mut();

        napi_call_result!(crate::sys::napi_wrap(
            self.0,
            js_object,
            rust_obj as *mut core::ffi::c_void,
            Some(finalize),
            ptr::null_mut(),
            &mut result
        ))?;

        Ok(result)
    }

    pub fn unwrap<T>(&self, js_this: napi_value) -> Result<&'static T, NjError> {
        let mut result: *mut ::std::os::raw::c_void = ptr::null_mut();
        napi_call_result!(crate::sys::napi_unwrap(self.0, js_this, &mut result))?;

        Ok(unsafe {
            let rust_ref: &T = &mut *(result as *mut T);
            rust_ref
        })
    }

    pub fn unwrap_mut<T>(&self, js_this: napi_value) -> Result<&'static mut T, NjError> {
        let mut result: *mut ::std::os::raw::c_void = ptr::null_mut();
        napi_call_result!(crate::sys::napi_unwrap(self.0, js_this, &mut result))?;

        Ok(unsafe {
            let rust_ref: &mut T = &mut *(result as *mut T);
            rust_ref
        })
    }

    pub fn new_instance(
        &self,
        constructor: napi_value,
        mut args: Vec<napi_value>,
    ) -> Result<napi_value, NjError> {
        trace!("napi new instance: {}", args.len());
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_new_instance(
            self.0,
            constructor,
            args.len(),
            args.as_mut_ptr(),
            &mut result
        ))?;

        Ok(result)
    }

    pub fn get_reference_value(&self, obj_ref: napi_ref) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();
        napi_call_result!(crate::sys::napi_get_reference_value(
            self.0,
            obj_ref,
            &mut result
        ))?;

        Ok(result)
    }

    /// create promise and deferred
    pub fn create_promise(&self) -> Result<(napi_value, napi_deferred), NjError> {
        let mut deferred = ptr::null_mut();
        let mut promise = ptr::null_mut();

        napi_call_result!(crate::sys::napi_create_promise(
            self.0,
            &mut deferred,
            &mut promise
        ))?;

        Ok((promise, deferred))
    }

    pub fn resolve_deferred(
        &self,
        deferred: napi_deferred,
        resolution: napi_value,
    ) -> Result<(), NjError> {
        napi_call_result!(crate::sys::napi_resolve_deferred(
            self.0, deferred, resolution
        ))
    }

    pub fn reject_deferred(
        &self,
        deferred: napi_deferred,
        rejection: napi_value,
    ) -> Result<(), NjError> {
        napi_call_result!(crate::sys::napi_reject_deferred(
            self.0, deferred, rejection
        ))
    }

    pub fn create_thread_safe_function(
        &self,
        name: &str,
        js_func: Option<napi_value>,
        call_js_cb: napi_threadsafe_function_call_js,
    ) -> Result<crate::ThreadSafeFunction, NjError> {
        use crate::sys::napi_create_threadsafe_function;

        let work_name = self.create_string_utf8(name)?;

        let mut tsfn = ptr::null_mut();

        trace!("trying to create threadsafe fn: {}", name);

        napi_call_result!(napi_create_threadsafe_function(
            self.inner(),
            js_func.unwrap_or(ptr::null_mut()),
            ptr::null_mut(),
            work_name,
            0,
            1,
            ptr::null_mut(),
            None,
            ptr::null_mut(),
            call_js_cb,
            &mut tsfn
        ))?;

        trace!("created threadsafe fn: {}", name);

        Ok(crate::ThreadSafeFunction::new(self.0, tsfn))
    }

    pub fn is_exception_pending(&self) -> bool {
        let mut pending = false;
        napi_call_assert!(crate::sys::napi_is_exception_pending(
            self.inner(),
            &mut pending
        ));
        pending
    }

    pub fn throw_type_error(&self, message: &str) {
        debug!("throwing type error: {}", message);
        // check if there is exception pending, if so log and not do anything
        if self.is_exception_pending() {
            error!(
                "there is exception pending when trying to throw {}, ignoring for now",
                message
            );
            return;
        }

        let c_error_msg = CString::new(message).expect("message should not contain null");
        unsafe {
            crate::sys::napi_throw_type_error(self.inner(), ptr::null_mut(), c_error_msg.as_ptr())
        };
    }

    pub fn create_error(&self, message: &str) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();

        let err_message = self.create_string_utf8(message)?;

        napi_call_result!(crate::sys::napi_create_error(
            self.0,
            ptr::null_mut(),
            err_message,
            &mut result
        ))?;

        Ok(result)
    }

    /// get value type
    pub fn value_type(&self, napi_value: napi_value) -> Result<napi_valuetype, NjError> {
        use crate::sys::napi_typeof;

        let mut valuetype: napi_valuetype = 0;

        napi_call_result!(napi_typeof(self.inner(), napi_value, &mut valuetype))?;

        Ok(valuetype)
    }

    /// get string representation of value type
    pub fn value_type_string(&self, napi_value: napi_value) -> Result<&'static str, NjError> {
        Ok(napi_value_type_to_string(self.value_type(napi_value)?))
    }

    /// assert that napi value is certain type, otherwise raise exception
    pub fn assert_type(
        &self,
        napi_value: napi_value,
        should_be_type: napi_valuetype,
    ) -> Result<(), NjError> {
        let valuetype = self.value_type(napi_value)?;

        if valuetype != should_be_type {
            debug!(
                "value type is: {}-{} but should be: {}-{}",
                napi_value_type_to_string(valuetype),
                valuetype,
                napi_value_type_to_string(should_be_type),
                should_be_type
            );
            Err(NjError::InvalidType(
                napi_value_type_to_string(should_be_type).to_owned(),
                napi_value_type_to_string(valuetype).to_owned(),
            ))
        } else {
            Ok(())
        }
    }

    /// convert napi value to rust value
    pub fn convert_to_rust<'a, T>(&'a self, napi_value: napi_value) -> Result<T, NjError>
    where
        T: JSValue<'a>,
    {
        T::convert_to_rust(&self, napi_value)
    }

    pub fn get_undefined(&self) -> Result<napi_value, NjError> {
        let mut result = ptr::null_mut();

        napi_call_result!(crate::sys::napi_get_undefined(self.0, &mut result))?;

        Ok(result)
    }

    /// get buffer info
    pub fn get_buffer_info(&self, napi_value: napi_value) -> Result<&[u8], NjError> {
        use std::slice;
        use crate::sys::napi_get_buffer_info;

        let mut len: size_t = 0;
        let mut data = ptr::null_mut();

        //  napi_status napi_get_buffer_info(napi_env env,
        //      napi_value value,
        //      void** data,
        //      size_t* length)

        napi_call_result!(napi_get_buffer_info(
            self.inner(),
            napi_value,
            &mut data,
            &mut len
        ))?;

        let array: &[u8] = unsafe { slice::from_raw_parts(data as *const u8, len as usize) };

        Ok(array)
    }

    /// Detach ArrayBuffer
    pub fn detach_arraybuffer(&self, napi_value: napi_value) -> Result<(), NjError> {
        napi_call_result!(crate::sys::napi_detach_arraybuffer(
            self.inner(),
            napi_value
        ))?;
        Ok(())
    }

    /// Is this ArrayBuffer Detached?
    pub fn is_detached_arraybuffer(&self, napi_value: napi_value) -> Result<bool, NjError> {
        let mut is_detached = false;
        napi_call_result!(crate::sys::napi_is_detached_arraybuffer(
            self.inner(),
            napi_value,
            &mut is_detached,
        ))?;
        Ok(is_detached)
    }

    #[allow(unused_unsafe)]
    #[allow(clippy::missing_safety_doc)]
    pub unsafe fn add_env_clean_up_hook(
        &self,
        init_func: Option<unsafe extern "C" fn(arg: *mut ::std::os::raw::c_void)>,
        arg: *mut ::std::os::raw::c_void,
    ) -> Result<(), NjError> {
        use crate::sys::napi_add_env_cleanup_hook;

        napi_call_result!(napi_add_env_cleanup_hook(self.inner(), init_func, arg))?;

        Ok(())
    }
}

#[derive(Clone)]
pub struct JsCallback {
    env: JsEnv,
    this: napi_value,
    args: VecDeque<napi_value>,
}

unsafe impl Send for JsCallback {}
unsafe impl Sync for JsCallback {}

impl JsCallback {
    pub fn new(env: JsEnv, this: napi_value, args: Vec<napi_value>) -> Self {
        Self {
            env,
            this,
            args: args.into(),
        }
    }

    pub fn env(&self) -> &JsEnv {
        &self.env
    }

    pub fn args(&self, index: usize) -> napi_value {
        self.args[index]
    }

    pub fn this(&self) -> napi_value {
        self.this
    }

    pub fn this_owned(self) -> napi_value {
        self.this
    }

    pub fn remove_napi(&mut self) -> Option<napi_value> {
        self.args.remove(0)
    }

    /// consume next napi value and remove them from arg list
    pub fn get_value<'a, T>(&'a mut self) -> Result<T, NjError>
    where
        T: ExtractFromJs<'a>,
    {
        trace!("trying extract value out of {} args", self.args.len());

        T::extract(self)
    }

    /// convert value to rust
    pub fn get_value_at<'a, T>(&'a self, index: usize) -> Result<T, NjError>
    where
        T: ExtractArgFromJs<'a>,
    {
        trace!("trying extract value at: {}", index);
        T::convert_arg_at(self, index)
    }

    /// create thread safe function
    pub fn create_thread_safe_function(
        &mut self,
        name: &str,
        call_js_cb: napi_threadsafe_function_call_js,
    ) -> Result<crate::ThreadSafeFunction, NjError> {
        if let Some(n_value) = self.remove_napi() {
            self.env
                .create_thread_safe_function(name, Some(n_value), call_js_cb)
        } else {
            Err(NjError::Other("expected js callback".to_owned()))
        }
    }

    /// create thread safe function at
    pub fn create_thread_safe_function_at(
        &self,
        name: &str,
        index: usize,
        call_js_cb: napi_threadsafe_function_call_js,
    ) -> Result<crate::ThreadSafeFunction, NjError> {
        if index < self.args.len() {
            self.env
                .create_thread_safe_function(name, Some(self.args[index]), call_js_cb)
        } else {
            Err(NjError::Other(format!(
                "expected js callback at: {}",
                index
            )))
        }
    }

    pub fn unwrap_mut<T>(&self) -> Result<&'static mut T, NjError> {
        Ok(self
            .env
            .unwrap_mut::<JSObjectWrapper<T>>(self.this())?
            .mut_inner())
    }

    pub fn unwrap<T>(&self) -> Result<&'static T, NjError> {
        Ok(self.env.unwrap::<JSObjectWrapper<T>>(self.this())?.inner())
    }
}

/// #[deprecated(since = "4.1.0","No longer used"]
pub trait ExtractFromJs<'a>: Sized {
    fn label() -> &'static str {
        std::any::type_name::<Self>()
    }

    /// extract from js callback
    fn extract(js_cb: &'a mut JsCallback) -> Result<Self, NjError>;
}

impl<'a, T: ?Sized> ExtractFromJs<'a> for T
where
    T: JSValue<'a>,
{
    fn label() -> &'static str {
        T::label()
    }

    fn extract(js_cb: &'a mut JsCallback) -> Result<Self, NjError> {
        if let Some(n_value) = js_cb.remove_napi() {
            T::convert_to_rust(js_cb.env(), n_value)
        } else {
            Err(NjError::Other(format!(
                "expected argument of type: {}",
                Self::label()
            )))
        }
    }
}

/// for optional argument
impl<'a, T: Sized> ExtractFromJs<'a> for Option<T>
where
    T: JSValue<'a>,
{
    fn label() -> &'static str {
        T::label()
    }

    fn extract(js_cb: &'a mut JsCallback) -> Result<Self, NjError> {
        if let Some(n_value) = js_cb.remove_napi() {
            Ok(Some(T::convert_to_rust(js_cb.env(), n_value)?))
        } else {
            Ok(None)
        }
    }
}

impl ExtractFromJs<'_> for JsEnv {
    fn extract(js_cb: &mut JsCallback) -> Result<Self, NjError> {
        Ok(*js_cb.env())
    }
}

pub trait ExtractArgFromJs<'a>: Sized {
    fn label() -> &'static str {
        std::any::type_name::<Self>()
    }

    /// convert js callback argument at index
    fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result<Self, NjError>;
}

impl<'a, T: ?Sized> ExtractArgFromJs<'a> for T
where
    T: JSValue<'a>,
{
    fn label() -> &'static str {
        T::label()
    }

    fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result<Self, NjError> {
        if index < js_cb.args.len() {
            T::convert_to_rust(js_cb.env(), js_cb.args[index])
        } else {
            Err(NjError::Other(format!(
                "expected argument of type: {} at: {}",
                Self::label(),
                index
            )))
        }
    }
}

impl<'a, T: Sized> ExtractArgFromJs<'a> for Option<T>
where
    T: JSValue<'a>,
{
    fn label() -> &'static str {
        T::label()
    }

    fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result<Self, NjError> {
        if index < js_cb.args.len() {
            Ok(Some(T::convert_to_rust(js_cb.env(), js_cb.args[index])?))
        } else {
            Ok(None)
        }
    }
}

impl ExtractArgFromJs<'_> for JsEnv {
    fn convert_arg_at(js_cb: &JsCallback, _index: usize) -> Result<Self, NjError> {
        Ok(*js_cb.env())
    }
}

pub struct JsExports {
    inner: napi_value,
    env: JsEnv,
}

impl JsExports {
    pub fn new(env: napi_env, exports: napi_value) -> Self {
        Self {
            inner: exports,
            env: JsEnv::new(env),
        }
    }

    pub fn env(&self) -> &JsEnv {
        &self.env
    }

    pub fn prop_builder(&self) -> PropertiesBuilder {
        PropertiesBuilder::new()
    }

    pub fn define_property(&self, properties: PropertiesBuilder) -> Result<(), NjError> {
        // it is important not to release properties until this call is executed
        // since it is source of name string
        let mut raw_properties = properties.as_raw_properties();

        napi_call_result!(crate::sys::napi_define_properties(
            self.env.inner(),
            self.inner,
            raw_properties.len(),
            raw_properties.as_mut_ptr()
        ))
    }

    pub fn set_name_property(&self, name: &str, js_class: napi_value) -> Result<(), NjError> {
        let c_name = CString::new(name).expect("should work");

        napi_call_result!(crate::sys::napi_set_named_property(
            self.env.inner(),
            self.inner,
            c_name.as_ptr(),
            js_class
        ))
    }
}

/// Js callback function
pub struct JsCallbackFunction {
    ctx: napi_value,
    js_func: napi_value,
    env: JsEnv,
}

unsafe impl Send for JsCallbackFunction {}
unsafe impl Sync for JsCallbackFunction {}

impl JSValue<'_> for JsCallbackFunction {
    fn label() -> &'static str {
        "callback"
    }

    fn convert_to_rust(env: &JsEnv, js_value: napi_value) -> Result<Self, NjError> {
        env.assert_type(js_value, crate::sys::napi_valuetype_napi_function)?;

        let ctx = env.get_global()?;
        Ok(Self {
            ctx,
            js_func: js_value,
            env: *env,
        })
    }
}

impl JsCallbackFunction {
    /// invoke
    pub fn call<T>(&self, rust_argv: Vec<T>) -> Result<napi_value, NjError>
    where
        T: TryIntoJs,
    {
        trace!("invoking normal js callback");

        let env = &self.env;
        let mut argv: Vec<napi_value> = vec![];
        for rust_arg in rust_argv {
            match rust_arg.try_to_js(env) {
                Ok(js_value) => argv.push(js_value),
                Err(err) => return Err(err),
            }
        }

        self.env.call_function(self.ctx, self.js_func, argv)
    }
}

/// represent arbitrary js object
pub struct JsObject {
    env: JsEnv,
    napi_value: napi_value,
}

unsafe impl Send for JsObject {}

impl JsObject {
    /// create js object from js object
    pub fn new(env: JsEnv, napi_value: napi_value) -> Self {
        Self { env, napi_value }
    }

    /// create new js object from env
    pub fn create(env: &JsEnv) -> Result<Self, NjError> {
        Ok(Self::new(*env, env.create_object()?))
    }

    pub fn env(&self) -> &JsEnv {
        &self.env
    }

    pub fn napi_value(&self) -> napi_value {
        self.napi_value
    }

    /// get property
    pub fn get_property(&self, key: &str) -> Result<Option<Self>, NjError> {
        let property_key = self.env.create_string_utf8(key)?;

        let mut exist: bool = false;
        napi_call_result!(napi_has_property(
            self.env.inner(),
            self.napi_value,
            property_key,
            &mut exist,
        ))?;

        if !exist {
            return Ok(None);
        }

        let mut property_value = ptr::null_mut();

        napi_call_result!(napi_get_property(
            self.env.inner(),
            self.napi_value,
            property_key,
            &mut property_value,
        ))?;

        Ok(Some(Self {
            env: self.env,
            napi_value: property_value,
        }))
    }

    pub fn set_property(&mut self, key: &str, property_value: napi_value) -> Result<(), NjError> {
        use crate::sys::napi_set_property;

        let property_key = self.env.create_string_utf8(key)?;

        napi_call_result!(napi_set_property(
            self.env.inner(),
            self.napi_value,
            property_key,
            property_value,
        ))?;

        Ok(())
    }

    /// convert to equivalent rust object
    pub fn as_value<'a, T>(&'a self) -> Result<T, NjError>
    where
        T: JSValue<'a>,
    {
        self.env.convert_to_rust(self.napi_value)
    }
}

impl JSValue<'_> for JsObject {
    fn convert_to_rust(env: &JsEnv, js_value: napi_value) -> Result<Self, NjError> {
        env.assert_type(js_value, crate::sys::napi_valuetype_napi_object)?;
        Ok(Self::new(*env, js_value))
    }
}

impl TryIntoJs for JsObject {
    fn try_to_js(self, _js_env: &JsEnv) -> Result<napi_value, NjError> {
        Ok(self.napi_value)
    }
}