openworkers-v8 146.5.0

Rust bindings to V8 (fork with Locker/UnenteredIsolate support for isolate pooling)
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
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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
use crate::AccessorConfiguration;
use crate::AccessorNameGetterCallback;
use crate::AccessorNameSetterCallback;
use crate::Array;
use crate::Context;
use crate::Data;
use crate::GetPropertyNamesArgs;
use crate::IndexFilter;
use crate::KeyCollectionMode;
use crate::KeyConversionMode;
use crate::Local;
use crate::Map;
use crate::Name;
use crate::Object;
use crate::Private;
use crate::PropertyAttribute;
use crate::PropertyDescriptor;
use crate::PropertyFilter;
use crate::Set;
use crate::String;
use crate::Value;
use crate::binding::RustObj;
use crate::cppgc::GarbageCollected;
use crate::cppgc::GetRustObj;
use crate::cppgc::UnsafePtr;
use crate::isolate::Isolate;
use crate::isolate::RealIsolate;
use crate::scope::PinScope;
use crate::support::MapFnTo;
use crate::support::Maybe;
use crate::support::MaybeBool;
use crate::support::int;
use std::convert::TryFrom;
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::num::NonZeroI32;
use std::ptr::null;

unsafe extern "C" {
  fn v8__Object__New(isolate: *mut RealIsolate) -> *const Object;
  fn v8__Object__New__with_prototype_and_properties(
    isolate: *mut RealIsolate,
    prototype_or_null: *const Value,
    names: *const *const Name,
    values: *const *const Value,
    length: usize,
  ) -> *const Object;
  fn v8__Object__SetAccessor(
    this: *const Object,
    context: *const Context,
    key: *const Name,
    getter: AccessorNameGetterCallback,
    setter: Option<AccessorNameSetterCallback>,
    data_or_null: *const Value,
    attr: PropertyAttribute,
  ) -> MaybeBool;
  fn v8__Object__Get(
    this: *const Object,
    context: *const Context,
    key: *const Value,
  ) -> *const Value;
  fn v8__Object__GetWithReceiver(
    this: *const Object,
    context: *const Context,
    key: *const Value,
    receiver: *const Object,
  ) -> *const Value;
  fn v8__Object__GetIndex(
    this: *const Object,
    context: *const Context,
    index: u32,
  ) -> *const Value;
  fn v8__Object__GetPrototype(this: *const Object) -> *const Value;
  fn v8__Object__Set(
    this: *const Object,
    context: *const Context,
    key: *const Value,
    value: *const Value,
  ) -> MaybeBool;
  fn v8__Object__SetWithReceiver(
    this: *const Object,
    context: *const Context,
    key: *const Value,
    value: *const Value,
    receiver: *const Object,
  ) -> MaybeBool;
  fn v8__Object__SetIndex(
    this: *const Object,
    context: *const Context,
    index: u32,
    value: *const Value,
  ) -> MaybeBool;
  fn v8__Object__SetPrototype(
    this: *const Object,
    context: *const Context,
    prototype: *const Value,
  ) -> MaybeBool;
  fn v8__Object__GetConstructorName(this: *const Object) -> *const String;
  fn v8__Object__CreateDataProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
    value: *const Value,
  ) -> MaybeBool;
  fn v8__Object__DefineOwnProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
    value: *const Value,
    attr: PropertyAttribute,
  ) -> MaybeBool;
  fn v8__Object__DefineProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
    desc: *const PropertyDescriptor,
  ) -> MaybeBool;
  fn v8__Object__GetIdentityHash(this: *const Object) -> int;
  fn v8__Object__GetCreationContext(this: *const Object) -> *const Context;
  fn v8__Object__GetOwnPropertyNames(
    this: *const Object,
    context: *const Context,
    filter: PropertyFilter,
    key_conversion: KeyConversionMode,
  ) -> *const Array;
  fn v8__Object__GetPropertyNames(
    this: *const Object,
    context: *const Context,
    mode: KeyCollectionMode,
    property_filter: PropertyFilter,
    index_filter: IndexFilter,
    key_conversion: KeyConversionMode,
  ) -> *const Array;
  fn v8__Object__Has(
    this: *const Object,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Object__HasIndex(
    this: *const Object,
    context: *const Context,
    index: u32,
  ) -> MaybeBool;
  fn v8__Object__HasOwnProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
  ) -> MaybeBool;
  fn v8__Object__Delete(
    this: *const Object,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Object__DeleteIndex(
    this: *const Object,
    context: *const Context,
    index: u32,
  ) -> MaybeBool;
  fn v8__Object__InternalFieldCount(this: *const Object) -> int;
  fn v8__Object__GetInternalField(
    this: *const Object,
    index: int,
  ) -> *const Data;
  fn v8__Object__GetAlignedPointerFromInternalField(
    this: *const Object,
    index: int,
    tag: u16,
  ) -> *const c_void;
  fn v8__Object__SetAlignedPointerInInternalField(
    this: *const Object,
    index: int,
    value: *const c_void,
    tag: u16,
  );
  fn v8__Object__SetIntegrityLevel(
    this: *const Object,
    context: *const Context,
    level: IntegrityLevel,
  ) -> MaybeBool;
  fn v8__Object__SetInternalField(
    this: *const Object,
    index: int,
    data: *const Data,
  );
  fn v8__Object__GetPrivate(
    this: *const Object,
    context: *const Context,
    key: *const Private,
  ) -> *const Value;
  fn v8__Object__SetPrivate(
    this: *const Object,
    context: *const Context,
    key: *const Private,
    value: *const Value,
  ) -> MaybeBool;
  fn v8__Object__DeletePrivate(
    this: *const Object,
    context: *const Context,
    key: *const Private,
  ) -> MaybeBool;
  fn v8__Object__HasPrivate(
    this: *const Object,
    context: *const Context,
    key: *const Private,
  ) -> MaybeBool;
  fn v8__Object__GetPropertyAttributes(
    this: *const Object,
    context: *const Context,
    key: *const Value,
    out: *mut Maybe<PropertyAttribute>,
  );
  fn v8__Object__GetOwnPropertyDescriptor(
    this: *const Object,
    context: *const Context,
    key: *const Name,
  ) -> *const Value;
  fn v8__Object__PreviewEntries(
    this: *const Object,
    is_key_value: *mut bool,
  ) -> *const Array;
  fn v8__Object__GetRealNamedProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
  ) -> *const Value;
  fn v8__Object__HasRealNamedProperty(
    this: *const Object,
    context: *const Context,
    key: *const Name,
  ) -> MaybeBool;
  fn v8__Object__GetRealNamedPropertyAttributes(
    this: *const Object,
    context: *const Context,
    key: *const Name,
    out: *mut Maybe<PropertyAttribute>,
  );
  fn v8__Object__Wrap(
    isolate: *const RealIsolate,
    wrapper: *const Object,
    value: *const RustObj,
    tag: u16,
  );
  fn v8__Object__Unwrap(
    isolate: *const RealIsolate,
    wrapper: *const Object,
    tag: u16,
  ) -> *mut RustObj;
  fn v8__Object__IsApiWrapper(this: *const Object) -> bool;

  fn v8__Array__New(isolate: *mut RealIsolate, length: int) -> *const Array;
  fn v8__Array__New_with_elements(
    isolate: *mut RealIsolate,
    elements: *const *const Value,
    length: usize,
  ) -> *const Array;
  fn v8__Array__Length(array: *const Array) -> u32;
  fn v8__Map__New(isolate: *mut RealIsolate) -> *const Map;
  fn v8__Map__Clear(this: *const Map);
  fn v8__Map__Get(
    this: *const Map,
    context: *const Context,
    key: *const Value,
  ) -> *const Value;
  fn v8__Map__Set(
    this: *const Map,
    context: *const Context,
    key: *const Value,
    value: *const Value,
  ) -> *const Map;
  fn v8__Map__Has(
    this: *const Map,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Map__Delete(
    this: *const Map,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Map__Size(map: *const Map) -> usize;
  fn v8__Map__As__Array(this: *const Map) -> *const Array;
  fn v8__Set__New(isolate: *mut RealIsolate) -> *const Set;
  fn v8__Set__Clear(this: *const Set);
  fn v8__Set__Add(
    this: *const Set,
    context: *const Context,
    key: *const Value,
  ) -> *const Set;
  fn v8__Set__Has(
    this: *const Set,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Set__Delete(
    this: *const Set,
    context: *const Context,
    key: *const Value,
  ) -> MaybeBool;
  fn v8__Set__Size(map: *const Set) -> usize;
  fn v8__Set__As__Array(this: *const Set) -> *const Array;
}

const LAST_TAG: u16 = 0x7fff;

impl Object {
  /// Creates an empty object.
  #[inline(always)]
  pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Object> {
    unsafe { scope.cast_local(|sd| v8__Object__New(sd.get_isolate_ptr())) }
      .unwrap()
  }

  /// Creates a JavaScript object with the given properties, and the given
  /// prototype_or_null (which can be any JavaScript value, and if it's null,
  /// the newly created object won't have a prototype at all). This is similar
  /// to Object.create(). All properties will be created as enumerable,
  /// configurable and writable properties.
  #[inline(always)]
  pub fn with_prototype_and_properties<'s>(
    scope: &PinScope<'s, '_>,
    prototype_or_null: Local<'s, Value>,
    names: &[Local<Name>],
    values: &[Local<Value>],
  ) -> Local<'s, Object> {
    assert_eq!(names.len(), values.len());
    let names = Local::slice_into_raw(names);
    let values = Local::slice_into_raw(values);
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__New__with_prototype_and_properties(
          sd.get_isolate_ptr(),
          &*prototype_or_null,
          names.as_ptr(),
          values.as_ptr(),
          names.len(),
        )
      })
    }
    .unwrap()
  }

  /// Set only return Just(true) or Empty(), so if it should never fail, use
  /// result.Check().
  #[inline(always)]
  pub fn set(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
    value: Local<Value>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__Set(self, &*scope.get_current_context(), &*key, &*value)
    }
    .into()
  }

  /// SetWithReceiver only return Just(true) or Empty(), so if it should never fail, use
  /// result.Check().
  #[inline(always)]
  pub fn set_with_receiver(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
    value: Local<Value>,
    receiver: Local<Object>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetWithReceiver(
        self,
        &*scope.get_current_context(),
        &*key,
        &*value,
        &*receiver,
      )
    }
    .into()
  }

  /// Set only return Just(true) or Empty(), so if it should never fail, use
  /// result.Check().
  #[inline(always)]
  pub fn set_index(
    &self,
    scope: &PinScope<'_, '_>,
    index: u32,
    value: Local<Value>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetIndex(self, &*scope.get_current_context(), index, &*value)
    }
    .into()
  }

  /// Set the prototype object. This does not skip objects marked to be
  /// skipped by proto and it does not consult the security handler.
  #[inline(always)]
  pub fn set_prototype(
    &self,
    scope: &PinScope<'_, '_>,
    prototype: Local<Value>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetPrototype(self, &*scope.get_current_context(), &*prototype)
    }
    .into()
  }

  /// Returns the name of the function invoked as a constructor for this object.
  #[inline(always)]
  pub fn get_constructor_name(&self) -> Local<'_, String> {
    unsafe { Local::from_raw(v8__Object__GetConstructorName(self)) }.unwrap()
  }

  /// Implements CreateDataProperty (ECMA-262, 7.3.4).
  ///
  /// Defines a configurable, writable, enumerable property with the given value
  /// on the object unless the property already exists and is not configurable
  /// or the object is not extensible.
  ///
  /// Returns true on success.
  #[inline(always)]
  pub fn create_data_property(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
    value: Local<Value>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__CreateDataProperty(
        self,
        &*scope.get_current_context(),
        &*key,
        &*value,
      )
    }
    .into()
  }

  /// Implements DefineOwnProperty.
  ///
  /// In general, CreateDataProperty will be faster, however, does not allow
  /// for specifying attributes.
  ///
  /// Returns true on success.
  #[inline(always)]
  pub fn define_own_property(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
    value: Local<Value>,
    attr: PropertyAttribute,
  ) -> Option<bool> {
    unsafe {
      v8__Object__DefineOwnProperty(
        self,
        &*scope.get_current_context(),
        &*key,
        &*value,
        attr,
      )
    }
    .into()
  }

  #[inline(always)]
  pub fn define_property(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
    descriptor: &PropertyDescriptor,
  ) -> Option<bool> {
    unsafe {
      v8__Object__DefineProperty(
        self,
        &*scope.get_current_context(),
        &*key,
        descriptor,
      )
      .into()
    }
  }

  #[inline(always)]
  pub fn get<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Value>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope
        .cast_local(|sd| v8__Object__Get(self, sd.get_current_context(), &*key))
    }
  }

  #[inline(always)]
  pub fn get_with_receiver<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Value>,
    receiver: Local<Object>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetWithReceiver(
          self,
          sd.get_current_context(),
          &*key,
          &*receiver,
        )
      })
    }
  }

  #[inline(always)]
  pub fn get_index<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    index: u32,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetIndex(self, sd.get_current_context(), index)
      })
    }
  }

  /// Get the prototype object. This does not skip objects marked to be
  /// skipped by proto and it does not consult the security handler.
  #[inline(always)]
  pub fn get_prototype<'s>(
    &self,
    scope: &PinScope<'s, '_>,
  ) -> Option<Local<'s, Value>> {
    unsafe { scope.cast_local(|_| v8__Object__GetPrototype(self)) }
  }

  /// Note: SideEffectType affects the getter only, not the setter.
  #[inline(always)]
  pub fn set_accessor(
    &self,
    scope: &PinScope<'_, '_>,
    name: Local<Name>,
    getter: impl MapFnTo<AccessorNameGetterCallback>,
  ) -> Option<bool> {
    self.set_accessor_with_configuration(
      scope,
      name,
      AccessorConfiguration::new(getter),
    )
  }

  #[inline(always)]
  pub fn set_accessor_with_setter(
    &self,
    scope: &PinScope<'_, '_>,
    name: Local<Name>,
    getter: impl MapFnTo<AccessorNameGetterCallback>,
    setter: impl MapFnTo<AccessorNameSetterCallback>,
  ) -> Option<bool> {
    self.set_accessor_with_configuration(
      scope,
      name,
      AccessorConfiguration::new(getter).setter(setter),
    )
  }
  #[inline(always)]
  pub fn set_accessor_with_configuration(
    &self,
    scope: &PinScope<'_, '_>,
    name: Local<Name>,
    configuration: AccessorConfiguration,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetAccessor(
        self,
        &*scope.get_current_context(),
        &*name,
        configuration.getter,
        configuration.setter,
        configuration.data.map_or_else(null, |p| &*p),
        configuration.property_attribute,
      )
    }
    .into()
  }

  /// Returns the V8 hash value for this value. The current implementation
  /// uses a hidden property to store the identity hash.
  ///
  /// The return value will never be 0. Also, it is not guaranteed to be
  /// unique.
  #[inline(always)]
  pub fn get_identity_hash(&self) -> NonZeroI32 {
    unsafe { NonZeroI32::new_unchecked(v8__Object__GetIdentityHash(self)) }
  }

  /// Returns the context in which the object was created.
  #[inline(always)]
  pub fn get_creation_context<'s>(
    &self,
    scope: &PinScope<'s, '_>,
  ) -> Option<Local<'s, Context>> {
    unsafe { scope.cast_local(|_| v8__Object__GetCreationContext(self)) }
  }

  /// This function has the same functionality as GetPropertyNames but the
  /// returned array doesn't contain the names of properties from prototype
  /// objects.
  #[inline(always)]
  pub fn get_own_property_names<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    args: GetPropertyNamesArgs,
  ) -> Option<Local<'s, Array>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetOwnPropertyNames(
          self,
          sd.get_current_context(),
          args.property_filter,
          args.key_conversion,
        )
      })
    }
  }

  /// Returns an array containing the names of the filtered properties of this
  /// object, including properties from prototype objects. The array returned by
  /// this method contains the same values as would be enumerated by a for-in
  /// statement over this object.
  #[inline(always)]
  pub fn get_property_names<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    args: GetPropertyNamesArgs,
  ) -> Option<Local<'s, Array>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetPropertyNames(
          self,
          sd.get_current_context(),
          args.mode,
          args.property_filter,
          args.index_filter,
          args.key_conversion,
        )
      })
    }
  }

  // Calls the abstract operation HasProperty(O, P) described in ECMA-262,
  // 7.3.10. Returns true, if the object has the property, either own or on the
  // prototype chain. Interceptors, i.e., PropertyQueryCallbacks, are called if
  // present.
  //
  // This function has the same side effects as JavaScript's variable in object.
  // For example, calling this on a revoked proxy will throw an exception.
  //
  // Note: This function converts the key to a name, which possibly calls back
  // into JavaScript.
  #[inline(always)]
  pub fn has(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Object__Has(self, &*scope.get_current_context(), &*key) }
      .into()
  }

  #[inline(always)]
  pub fn has_index(
    &self,
    scope: &PinScope<'_, '_>,
    index: u32,
  ) -> Option<bool> {
    unsafe { v8__Object__HasIndex(self, &*scope.get_current_context(), index) }
      .into()
  }

  /// HasOwnProperty() is like JavaScript's Object.prototype.hasOwnProperty().
  #[inline(always)]
  pub fn has_own_property(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key)
    }
    .into()
  }

  #[inline(always)]
  pub fn delete(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Object__Delete(self, &*scope.get_current_context(), &*key) }
      .into()
  }

  #[inline]
  pub fn delete_index(
    &self,
    scope: &PinScope<'_, '_>,
    index: u32,
  ) -> Option<bool> {
    unsafe {
      v8__Object__DeleteIndex(self, &*scope.get_current_context(), index)
    }
    .into()
  }

  /// Gets the number of internal fields for this Object.
  #[inline(always)]
  pub fn internal_field_count(&self) -> usize {
    let count = unsafe { v8__Object__InternalFieldCount(self) };
    usize::try_from(count).expect("bad internal field count") // Can't happen.
  }

  /// Gets the data from an internal field.
  #[inline(always)]
  pub fn get_internal_field<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    index: usize,
  ) -> Option<Local<'s, Data>> {
    // Trying to access out-of-bounds internal fields makes V8 abort
    // in debug mode and access out-of-bounds memory in release mode.
    // The C++ API takes an i32 but doesn't check for indexes < 0, which
    // results in an out-of-bounds access in both debug and release mode.
    if index < self.internal_field_count()
      && let Ok(index) = int::try_from(index)
    {
      return unsafe {
        scope.cast_local(|_| v8__Object__GetInternalField(self, index))
      };
    }
    None
  }

  /// Gets a 2-byte-aligned native pointer from an internal field.
  ///
  /// # Safety
  /// This field must have been set by SetAlignedPointerInInternalField, everything else leads to undefined behavior.
  #[inline(always)]
  pub unsafe fn get_aligned_pointer_from_internal_field(
    &self,
    index: i32,
    tag: u16,
  ) -> *const c_void {
    unsafe { v8__Object__GetAlignedPointerFromInternalField(self, index, tag) }
  }

  /// Sets a 2-byte-aligned native pointer in an internal field.
  /// To retrieve such a field, GetAlignedPointerFromInternalField must be used.
  #[allow(clippy::not_unsafe_ptr_arg_deref)]
  #[inline(always)]
  pub fn set_aligned_pointer_in_internal_field(
    &self,
    index: i32,
    value: *const c_void,
    tag: u16,
  ) {
    unsafe {
      v8__Object__SetAlignedPointerInInternalField(self, index, value, tag)
    }
  }

  /// Wraps a JS wrapper with a C++ instance.
  ///
  /// # Safety
  ///
  /// The `TAG` must be unique to the caller within the heap.
  #[inline(always)]
  pub unsafe fn wrap<const TAG: u16, T: GarbageCollected>(
    isolate: &mut Isolate,
    wrapper: Local<Object>,
    value: &impl GetRustObj<T>,
  ) {
    const {
      assert!(TAG < LAST_TAG);
    }
    let ptr = value.get_rust_obj();
    unsafe { v8__Object__Wrap(isolate.as_real_ptr(), &*wrapper, ptr, TAG) }
  }

  /// Unwraps a JS wrapper object.
  ///
  /// # Safety
  ///
  /// The caller must ensure that the returned pointer is always stored on
  /// the stack, or is safely moved into one of the other cppgc pointer types.
  #[inline(always)]
  pub unsafe fn unwrap<const TAG: u16, T: GarbageCollected>(
    isolate: &mut Isolate,
    wrapper: Local<Object>,
  ) -> Option<UnsafePtr<T>> {
    const {
      assert!(TAG < LAST_TAG);
    }
    let ptr =
      unsafe { v8__Object__Unwrap(isolate.as_real_ptr(), &*wrapper, TAG) };
    unsafe { UnsafePtr::new(&ptr) }
  }

  /// Returns true if this object can be generally used to wrap object objects.
  /// This means that the object either follows the convention of using embedder
  /// fields to denote type/instance pointers or is using the Wrap()/Unwrap()
  /// APIs for the same purpose. Returns false otherwise.
  ///
  /// Note that there may be other objects that use embedder fields but are not
  /// used as API wrapper objects. E.g., v8::Promise may in certain configuration
  /// use embedder fields but promises are not generally supported as API
  /// wrappers. The method will return false in those cases.
  #[inline(always)]
  pub fn is_api_wrapper(&self) -> bool {
    unsafe { v8__Object__IsApiWrapper(self) }
  }

  /// Sets the integrity level of the object.
  #[inline(always)]
  pub fn set_integrity_level(
    &self,
    scope: &PinScope<'_, '_>,
    level: IntegrityLevel,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetIntegrityLevel(self, &*scope.get_current_context(), level)
    }
    .into()
  }

  /// Sets the data in an internal field. Returns false when the index
  /// is out of bounds, true otherwise.
  #[inline(always)]
  pub fn set_internal_field(&self, index: usize, data: Local<Data>) -> bool {
    // Trying to access out-of-bounds internal fields makes V8 abort
    // in debug mode and access out-of-bounds memory in release mode.
    // The C++ API takes an i32 but doesn't check for indexes < 0, which
    // results in an out-of-bounds access in both debug and release mode.
    if index < self.internal_field_count()
      && let Ok(index) = int::try_from(index)
    {
      unsafe { v8__Object__SetInternalField(self, index, &*data) };
      return true;
    }
    false
  }

  /// Functionality for private properties.
  /// This is an experimental feature, use at your own risk.
  /// Note: Private properties are not inherited. Do not rely on this, since it
  /// may change.
  #[inline(always)]
  pub fn get_private<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Private>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetPrivate(self, sd.get_current_context(), &*key)
      })
    }
  }

  /// Functionality for private properties.
  /// This is an experimental feature, use at your own risk.
  /// Note: Private properties are not inherited. Do not rely on this, since it
  /// may change.
  #[inline(always)]
  pub fn set_private(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Private>,
    value: Local<Value>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__SetPrivate(
        self,
        &*scope.get_current_context(),
        &*key,
        &*value,
      )
    }
    .into()
  }

  /// Functionality for private properties.
  /// This is an experimental feature, use at your own risk.
  /// Note: Private properties are not inherited. Do not rely on this, since it
  /// may change.
  #[inline(always)]
  pub fn delete_private(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Private>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__DeletePrivate(self, &*scope.get_current_context(), &*key)
    }
    .into()
  }

  /// Functionality for private properties.
  /// This is an experimental feature, use at your own risk.
  /// Note: Private properties are not inherited. Do not rely on this, since it
  /// may change.
  #[inline(always)]
  pub fn has_private(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Private>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__HasPrivate(self, &*scope.get_current_context(), &*key)
    }
    .into()
  }

  /// Gets the property attributes of a property which can be
  /// [PropertyAttribute::NONE] or any combination of
  /// [PropertyAttribute::READ_ONLY], [PropertyAttribute::DONT_ENUM] and
  /// [PropertyAttribute::DONT_DELETE].
  /// Returns [PropertyAttribute::NONE] when the property doesn't exist.
  pub fn get_property_attributes(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<PropertyAttribute> {
    let mut out = Maybe::<PropertyAttribute>::default();
    unsafe {
      v8__Object__GetPropertyAttributes(
        self,
        &*scope.get_current_context(),
        &*key,
        &mut out,
      );
    };
    out.into()
  }

  /// Implements Object.getOwnPropertyDescriptor(O, P), see
  /// https://tc39.es/ecma262/#sec-object.getownpropertydescriptor.
  #[inline]
  pub fn get_own_property_descriptor<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Name>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetOwnPropertyDescriptor(
          self,
          sd.get_current_context(),
          &*key,
        )
      })
    }
  }

  /// If this object is a Set, Map, WeakSet or WeakMap, this returns a
  /// representation of the elements of this object as an array.
  /// If this object is a SetIterator or MapIterator, this returns all elements
  /// of the underlying collection, starting at the iterator's current position.
  ///
  /// Also returns a boolean, indicating whether the returned array contains
  /// key & values (for example when the value is Set.entries()).
  #[inline]
  pub fn preview_entries<'s>(
    &self,
    scope: &PinScope<'s, '_>,
  ) -> (Option<Local<'s, Array>>, bool) {
    let mut is_key_value = MaybeUninit::uninit();
    unsafe {
      let val = scope.cast_local(|_| {
        v8__Object__PreviewEntries(self, is_key_value.as_mut_ptr())
      });
      let is_key_value = is_key_value.assume_init();

      (val, is_key_value)
    }
  }

  /// If result.IsEmpty() no real property was located on the object or
  /// in the prototype chain.
  /// This means interceptors in the prototype chain are not called.
  #[inline(always)]
  pub fn get_real_named_property<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Name>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Object__GetRealNamedProperty(self, sd.get_current_context(), &*key)
      })
    }
  }

  #[inline(always)]
  pub fn has_real_named_property(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
  ) -> Option<bool> {
    unsafe {
      v8__Object__HasRealNamedProperty(
        self,
        &*scope.get_current_context(),
        &*key,
      )
    }
    .into()
  }

  /// Gets the property attributes of a real property which can be
  /// None or any combination of ReadOnly, DontEnum and DontDelete.
  /// Interceptors in the prototype chain are not called.
  #[inline(always)]
  pub fn get_real_named_property_attributes(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Name>,
  ) -> Option<PropertyAttribute> {
    let mut out = Maybe::<PropertyAttribute>::default();
    unsafe {
      v8__Object__GetRealNamedPropertyAttributes(
        self,
        &*scope.get_current_context(),
        &*key,
        &mut out,
      );
    }
    out.into()
  }
}

/// Object integrity levels can be used to restrict what can be done to an
/// object's properties.
#[derive(Debug)]
#[repr(C)]
pub enum IntegrityLevel {
  /// Frozen objects are like Sealed objects, except all existing properties are
  /// also made non-writable.
  Frozen,
  /// Sealed objects prevent addition of any new property on the object, makes
  /// all existing properties non-configurable, meaning they cannot be deleted,
  /// have their enumerability, configurability, or writability changed.
  Sealed,
}

impl Array {
  /// Creates a JavaScript array with the given length. If the length
  /// is negative the returned array will have length 0.
  #[inline(always)]
  pub fn new<'s>(scope: &PinScope<'s, '_>, length: i32) -> Local<'s, Array> {
    unsafe {
      scope.cast_local(|sd| v8__Array__New(sd.get_isolate_ptr(), length))
    }
    .unwrap()
  }

  /// Creates a JavaScript array out of a Local<Value> array with a known
  /// length.
  #[inline(always)]
  pub fn new_with_elements<'s>(
    scope: &PinScope<'s, '_>,
    elements: &[Local<Value>],
  ) -> Local<'s, Array> {
    if elements.is_empty() {
      return Self::new(scope, 0);
    }
    let elements = Local::slice_into_raw(elements);
    unsafe {
      scope.cast_local(|sd| {
        v8__Array__New_with_elements(
          sd.get_isolate_ptr(),
          elements.as_ptr(),
          elements.len(),
        )
      })
    }
    .unwrap()
  }

  #[inline(always)]
  pub fn length(&self) -> u32 {
    unsafe { v8__Array__Length(self) }
  }
}

impl Map {
  #[inline(always)]
  pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Map> {
    unsafe { scope.cast_local(|sd| v8__Map__New(sd.get_isolate_ptr())) }
      .unwrap()
  }

  #[inline(always)]
  pub fn size(&self) -> usize {
    unsafe { v8__Map__Size(self) }
  }

  #[inline(always)]
  pub fn clear(&self) {
    unsafe { v8__Map__Clear(self) }
  }

  #[inline(always)]
  pub fn get<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Value>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| v8__Map__Get(self, sd.get_current_context(), &*key))
    }
  }

  #[inline(always)]
  pub fn set<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Value>,
    value: Local<Value>,
  ) -> Option<Local<'s, Map>> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Map__Set(self, sd.get_current_context(), &*key, &*value)
      })
    }
  }

  #[inline(always)]
  pub fn has(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Map__Has(self, &*scope.get_current_context(), &*key) }.into()
  }

  #[inline(always)]
  pub fn delete(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Map__Delete(self, &*scope.get_current_context(), &*key) }
      .into()
  }

  /// Returns an array of length size() * 2, where index N is the Nth key and
  /// index N + 1 is the Nth value.
  #[inline(always)]
  pub fn as_array<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Array> {
    unsafe { scope.cast_local(|_| v8__Map__As__Array(self)) }.unwrap()
  }
}

impl Set {
  #[inline(always)]
  pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Set> {
    unsafe { scope.cast_local(|sd| v8__Set__New(sd.get_isolate_ptr())) }
      .unwrap()
  }

  #[inline(always)]
  pub fn size(&self) -> usize {
    unsafe { v8__Set__Size(self) }
  }

  #[inline(always)]
  pub fn clear(&self) {
    unsafe { v8__Set__Clear(self) }
  }

  #[inline(always)]
  pub fn add<'s>(
    &self,
    scope: &PinScope<'s, '_>,
    key: Local<Value>,
  ) -> Option<Local<'s, Set>> {
    unsafe {
      scope.cast_local(|sd| v8__Set__Add(self, sd.get_current_context(), &*key))
    }
  }

  #[inline(always)]
  pub fn has(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Set__Has(self, &*scope.get_current_context(), &*key) }.into()
  }

  #[inline(always)]
  pub fn delete(
    &self,
    scope: &PinScope<'_, '_>,
    key: Local<Value>,
  ) -> Option<bool> {
    unsafe { v8__Set__Delete(self, &*scope.get_current_context(), &*key) }
      .into()
  }

  /// Returns an array of length size() * 2, where index N is the Nth key and
  /// index N + 1 is the Nth value.
  #[inline(always)]
  pub fn as_array<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Array> {
    unsafe { scope.cast_local(|_| v8__Set__As__Array(self)) }.unwrap()
  }
}