File indexing completed on 2025-02-21 10:05:25
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_OBJECT_H_
0006 #define INCLUDE_V8_OBJECT_H_
0007
0008 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0009 #include "v8-maybe.h" // NOLINT(build/include_directory)
0010 #include "v8-persistent-handle.h" // NOLINT(build/include_directory)
0011 #include "v8-primitive.h" // NOLINT(build/include_directory)
0012 #include "v8-traced-handle.h" // NOLINT(build/include_directory)
0013 #include "v8-value.h" // NOLINT(build/include_directory)
0014 #include "v8config.h" // NOLINT(build/include_directory)
0015
0016 namespace v8 {
0017
0018 class Array;
0019 class Function;
0020 class FunctionTemplate;
0021 template <typename T>
0022 class PropertyCallbackInfo;
0023
0024
0025
0026
0027
0028
0029 class V8_EXPORT Private : public Data {
0030 public:
0031
0032
0033
0034 Local<Value> Name() const;
0035
0036
0037
0038
0039 static Local<Private> New(Isolate* isolate,
0040 Local<String> name = Local<String>());
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 static Local<Private> ForApi(Isolate* isolate, Local<String> name);
0052
0053 V8_INLINE static Private* Cast(Data* data);
0054
0055 private:
0056 Private();
0057
0058 static void CheckCast(Data* that);
0059 };
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 class V8_EXPORT PropertyDescriptor {
0090 public:
0091
0092 PropertyDescriptor();
0093
0094
0095 explicit PropertyDescriptor(Local<Value> value);
0096
0097
0098 PropertyDescriptor(Local<Value> value, bool writable);
0099
0100
0101 PropertyDescriptor(Local<Value> get, Local<Value> set);
0102
0103 ~PropertyDescriptor();
0104
0105 Local<Value> value() const;
0106 bool has_value() const;
0107
0108 Local<Value> get() const;
0109 bool has_get() const;
0110 Local<Value> set() const;
0111 bool has_set() const;
0112
0113 void set_enumerable(bool enumerable);
0114 bool enumerable() const;
0115 bool has_enumerable() const;
0116
0117 void set_configurable(bool configurable);
0118 bool configurable() const;
0119 bool has_configurable() const;
0120
0121 bool writable() const;
0122 bool has_writable() const;
0123
0124 struct PrivateData;
0125 PrivateData* get_private() const { return private_; }
0126
0127 PropertyDescriptor(const PropertyDescriptor&) = delete;
0128 void operator=(const PropertyDescriptor&) = delete;
0129
0130 private:
0131 PrivateData* private_;
0132 };
0133
0134
0135
0136
0137 enum PropertyAttribute {
0138
0139 None = 0,
0140
0141 ReadOnly = 1 << 0,
0142
0143 DontEnum = 1 << 1,
0144
0145 DontDelete = 1 << 2
0146 };
0147
0148
0149
0150
0151
0152
0153 using AccessorGetterCallback =
0154 void (*)(Local<String> property, const PropertyCallbackInfo<Value>& info);
0155 using AccessorNameGetterCallback =
0156 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
0157
0158 using AccessorSetterCallback = void (*)(Local<String> property,
0159 Local<Value> value,
0160 const PropertyCallbackInfo<void>& info);
0161 using AccessorNameSetterCallback =
0162 void (*)(Local<Name> property, Local<Value> value,
0163 const PropertyCallbackInfo<void>& info);
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 enum AccessControl {
0174 DEFAULT = 0,
0175 };
0176
0177
0178
0179
0180 enum PropertyFilter {
0181 ALL_PROPERTIES = 0,
0182 ONLY_WRITABLE = 1,
0183 ONLY_ENUMERABLE = 2,
0184 ONLY_CONFIGURABLE = 4,
0185 SKIP_STRINGS = 8,
0186 SKIP_SYMBOLS = 16
0187 };
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 enum class SideEffectType {
0200 kHasSideEffect,
0201 kHasNoSideEffect,
0202 kHasSideEffectToReceiver
0203 };
0204
0205
0206
0207
0208
0209
0210
0211
0212 enum class KeyCollectionMode { kOwnOnly, kIncludePrototypes };
0213
0214
0215
0216
0217
0218 enum class IndexFilter { kIncludeIndices, kSkipIndices };
0219
0220
0221
0222
0223
0224 enum class KeyConversionMode { kConvertToString, kKeepNumbers, kNoNumbers };
0225
0226
0227
0228
0229 enum class IntegrityLevel { kFrozen, kSealed };
0230
0231
0232
0233
0234 class V8_EXPORT Object : public Value {
0235 public:
0236
0237
0238
0239
0240 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
0241 Local<Value> key, Local<Value> value);
0242
0243 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
0244 Local<Value> value);
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
0257 Local<Name> key,
0258 Local<Value> value);
0259 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
0260 uint32_t index,
0261 Local<Value> value);
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty(
0273 Local<Context> context, Local<Name> key, Local<Value> value,
0274 PropertyAttribute attributes = None);
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292 V8_WARN_UNUSED_RESULT Maybe<bool> DefineProperty(
0293 Local<Context> context, Local<Name> key, PropertyDescriptor& descriptor);
0294
0295 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
0296 Local<Value> key);
0297
0298 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
0299 uint32_t index);
0300
0301
0302
0303
0304
0305
0306 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes(
0307 Local<Context> context, Local<Value> key);
0308
0309
0310
0311
0312
0313 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor(
0314 Local<Context> context, Local<Name> key);
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
0332 Local<Value> key);
0333
0334 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
0335 Local<Value> key);
0336
0337 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
0338
0339 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
0340 uint32_t index);
0341
0342 V8_DEPRECATE_SOON("Use SetNativeDataProperty instead")
0343 V8_WARN_UNUSED_RESULT Maybe<bool> SetAccessor(
0344 Local<Context> context, Local<Name> name,
0345 AccessorNameGetterCallback getter,
0346 AccessorNameSetterCallback setter = nullptr,
0347 MaybeLocal<Value> data = MaybeLocal<Value>(),
0348 AccessControl deprecated_settings = DEFAULT,
0349 PropertyAttribute attribute = None,
0350 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0351 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0352
0353 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
0354 Local<Function> setter = Local<Function>(),
0355 PropertyAttribute attributes = None);
0356
0357
0358
0359
0360
0361 V8_WARN_UNUSED_RESULT Maybe<bool> SetNativeDataProperty(
0362 Local<Context> context, Local<Name> name,
0363 AccessorNameGetterCallback getter,
0364 AccessorNameSetterCallback setter = nullptr,
0365 Local<Value> data = Local<Value>(), PropertyAttribute attributes = None,
0366 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0367 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377 V8_WARN_UNUSED_RESULT Maybe<bool> SetLazyDataProperty(
0378 Local<Context> context, Local<Name> name,
0379 AccessorNameGetterCallback getter, Local<Value> data = Local<Value>(),
0380 PropertyAttribute attributes = None,
0381 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0382 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0383
0384
0385
0386
0387
0388
0389
0390 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
0391 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
0392 Local<Value> value);
0393 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
0394 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
0395
0396
0397
0398
0399
0400
0401
0402 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
0403 Local<Context> context);
0404 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
0405 Local<Context> context, KeyCollectionMode mode,
0406 PropertyFilter property_filter, IndexFilter index_filter,
0407 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
0408
0409
0410
0411
0412
0413
0414 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
0415 Local<Context> context);
0416
0417
0418
0419
0420
0421
0422
0423 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
0424 Local<Context> context, PropertyFilter filter,
0425 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
0426
0427
0428
0429
0430
0431
0432 Local<Value> GetPrototype();
0433
0434
0435
0436
0437
0438
0439 V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
0440 Local<Value> prototype);
0441
0442
0443
0444
0445
0446 Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl);
0447
0448
0449
0450
0451
0452
0453 V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString(
0454 Local<Context> context);
0455
0456
0457
0458
0459 Local<String> GetConstructorName();
0460
0461
0462
0463
0464 Maybe<bool> SetIntegrityLevel(Local<Context> context, IntegrityLevel level);
0465
0466
0467 int InternalFieldCount() const;
0468
0469
0470 V8_INLINE static int InternalFieldCount(
0471 const PersistentBase<Object>& object) {
0472 return object.template value<Object>()->InternalFieldCount();
0473 }
0474
0475
0476 V8_INLINE static int InternalFieldCount(
0477 const BasicTracedReference<Object>& object) {
0478 return object.template value<Object>()->InternalFieldCount();
0479 }
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491 V8_INLINE Local<Data> GetInternalField(int index);
0492
0493
0494 void SetInternalField(int index, Local<Data> data);
0495
0496
0497
0498
0499
0500
0501 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
0502 V8_INLINE void* GetAlignedPointerFromInternalField(v8::Isolate* isolate,
0503 int index);
0504
0505
0506 V8_INLINE static void* GetAlignedPointerFromInternalField(
0507 const PersistentBase<Object>& object, int index) {
0508 return object.template value<Object>()->GetAlignedPointerFromInternalField(
0509 index);
0510 }
0511
0512
0513 V8_INLINE static void* GetAlignedPointerFromInternalField(
0514 const BasicTracedReference<Object>& object, int index) {
0515 return object.template value<Object>()->GetAlignedPointerFromInternalField(
0516 index);
0517 }
0518
0519
0520
0521
0522
0523
0524 void SetAlignedPointerInInternalField(int index, void* value);
0525 void SetAlignedPointerInInternalFields(int argc, int indices[],
0526 void* values[]);
0527
0528
0529
0530
0531
0532
0533 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
0534 Local<Name> key);
0535 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
0536 uint32_t index);
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
0551 Local<Name> key);
0552 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty(
0553 Local<Context> context, uint32_t index);
0554 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty(
0555 Local<Context> context, Local<Name> key);
0556
0557
0558
0559
0560
0561 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain(
0562 Local<Context> context, Local<Name> key);
0563
0564
0565
0566
0567
0568
0569 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute>
0570 GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context,
0571 Local<Name> key);
0572
0573
0574
0575
0576
0577
0578 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty(
0579 Local<Context> context, Local<Name> key);
0580
0581
0582
0583
0584
0585
0586 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
0587 Local<Context> context, Local<Name> key);
0588
0589
0590 bool HasNamedLookupInterceptor() const;
0591
0592
0593 bool HasIndexedLookupInterceptor() const;
0594
0595
0596
0597
0598
0599
0600
0601
0602 int GetIdentityHash();
0603
0604
0605
0606
0607
0608
0609 Local<Object> Clone();
0610
0611
0612
0613
0614 MaybeLocal<Context> GetCreationContext();
0615
0616
0617
0618
0619 Local<Context> GetCreationContextChecked();
0620
0621
0622 V8_INLINE static MaybeLocal<Context> GetCreationContext(
0623 const PersistentBase<Object>& object) {
0624 return object.template value<Object>()->GetCreationContext();
0625 }
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638 void* GetAlignedPointerFromEmbedderDataInCreationContext(int index);
0639
0640
0641
0642
0643
0644
0645 bool IsCallable() const;
0646
0647
0648
0649
0650 bool IsConstructor() const;
0651
0652
0653
0654
0655
0656
0657
0658
0659 bool IsApiWrapper() const;
0660
0661
0662
0663
0664
0665
0666 bool IsUndetectable() const;
0667
0668
0669
0670
0671
0672 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context,
0673 Local<Value> recv,
0674 int argc,
0675 Local<Value> argv[]);
0676
0677
0678
0679
0680
0681
0682 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsConstructor(
0683 Local<Context> context, int argc, Local<Value> argv[]);
0684
0685
0686
0687
0688 Isolate* GetIsolate();
0689
0690 V8_INLINE static Isolate* GetIsolate(const TracedReference<Object>& handle) {
0691 return handle.template value<Object>()->GetIsolate();
0692 }
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703 MaybeLocal<Array> PreviewEntries(bool* is_key_value);
0704
0705 static Local<Object> New(Isolate* isolate);
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 static Local<Object> New(Isolate* isolate, Local<Value> prototype_or_null,
0716 Local<Name>* names, Local<Value>* values,
0717 size_t length);
0718
0719 V8_INLINE static Object* Cast(Value* obj);
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729 bool IsCodeLike(Isolate* isolate) const;
0730
0731 private:
0732 Object();
0733 static void CheckCast(Value* obj);
0734 Local<Data> SlowGetInternalField(int index);
0735 void* SlowGetAlignedPointerFromInternalField(int index);
0736 void* SlowGetAlignedPointerFromInternalField(v8::Isolate* isolate, int index);
0737 };
0738
0739
0740
0741 Local<Data> Object::GetInternalField(int index) {
0742 #ifndef V8_ENABLE_CHECKS
0743 using A = internal::Address;
0744 using I = internal::Internals;
0745 A obj = internal::ValueHelper::ValueAsAddress(this);
0746
0747
0748 int instance_type = I::GetInstanceType(obj);
0749 if (I::CanHaveInternalField(instance_type)) {
0750 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index);
0751 A value = I::ReadRawField<A>(obj, offset);
0752 #ifdef V8_COMPRESS_POINTERS
0753
0754
0755 value = I::DecompressTaggedField(obj, static_cast<uint32_t>(value));
0756 #endif
0757
0758 auto isolate = reinterpret_cast<v8::Isolate*>(
0759 internal::IsolateFromNeverReadOnlySpaceObject(obj));
0760 return Local<Data>::New(isolate, value);
0761 }
0762 #endif
0763 return SlowGetInternalField(index);
0764 }
0765
0766 void* Object::GetAlignedPointerFromInternalField(v8::Isolate* isolate,
0767 int index) {
0768 #if !defined(V8_ENABLE_CHECKS)
0769 using A = internal::Address;
0770 using I = internal::Internals;
0771 A obj = internal::ValueHelper::ValueAsAddress(this);
0772
0773
0774 auto instance_type = I::GetInstanceType(obj);
0775 if (V8_LIKELY(I::CanHaveInternalField(instance_type))) {
0776 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index) +
0777 I::kEmbedderDataSlotExternalPointerOffset;
0778 A value =
0779 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
0780 isolate, obj, offset);
0781 return reinterpret_cast<void*>(value);
0782 }
0783 #endif
0784 return SlowGetAlignedPointerFromInternalField(isolate, index);
0785 }
0786
0787 void* Object::GetAlignedPointerFromInternalField(int index) {
0788 #if !defined(V8_ENABLE_CHECKS)
0789 using A = internal::Address;
0790 using I = internal::Internals;
0791 A obj = internal::ValueHelper::ValueAsAddress(this);
0792
0793
0794 auto instance_type = I::GetInstanceType(obj);
0795 if (V8_LIKELY(I::CanHaveInternalField(instance_type))) {
0796 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index) +
0797 I::kEmbedderDataSlotExternalPointerOffset;
0798 Isolate* isolate = I::GetIsolateForSandbox(obj);
0799 A value =
0800 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
0801 isolate, obj, offset);
0802 return reinterpret_cast<void*>(value);
0803 }
0804 #endif
0805 return SlowGetAlignedPointerFromInternalField(index);
0806 }
0807
0808 Private* Private::Cast(Data* data) {
0809 #ifdef V8_ENABLE_CHECKS
0810 CheckCast(data);
0811 #endif
0812 return reinterpret_cast<Private*>(data);
0813 }
0814
0815 Object* Object::Cast(v8::Value* value) {
0816 #ifdef V8_ENABLE_CHECKS
0817 CheckCast(value);
0818 #endif
0819 return static_cast<Object*>(value);
0820 }
0821
0822 }
0823
0824 #endif