File indexing completed on 2026-09-16 09:15:23
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_VALUE_H_
0006 #define INCLUDE_V8_VALUE_H_
0007
0008 #include "v8-data.h" // NOLINT(build/include_directory)
0009 #include "v8-internal.h" // NOLINT(build/include_directory)
0010 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0011 #include "v8-maybe.h" // NOLINT(build/include_directory)
0012 #include "v8config.h" // NOLINT(build/include_directory)
0013
0014
0015
0016
0017 namespace v8 {
0018
0019 class Primitive;
0020 class Numeric;
0021 class BigInt;
0022 class Int32;
0023 class Integer;
0024 class Number;
0025 class Object;
0026 class String;
0027 class Uint32;
0028
0029
0030
0031
0032 class V8_EXPORT Value : public Data {
0033 public:
0034
0035
0036
0037
0038
0039
0040 V8_INLINE bool IsUndefined() const;
0041
0042
0043
0044
0045
0046
0047
0048 V8_INLINE bool IsNull() const;
0049
0050
0051
0052
0053
0054
0055
0056
0057 V8_INLINE bool IsNullOrUndefined() const;
0058
0059
0060
0061
0062
0063
0064
0065
0066 V8_INLINE bool IsTrue() const;
0067
0068
0069
0070
0071
0072
0073
0074
0075 V8_INLINE bool IsFalse() const;
0076
0077
0078
0079
0080
0081
0082
0083 bool IsName() const;
0084
0085
0086
0087
0088
0089
0090
0091 V8_INLINE bool IsString() const;
0092
0093
0094
0095
0096
0097
0098 bool IsSymbol() const;
0099
0100
0101
0102
0103
0104
0105 bool IsFunction() const;
0106
0107
0108
0109
0110
0111 bool IsArray() const;
0112
0113
0114
0115
0116 bool IsObject() const;
0117
0118
0119
0120
0121
0122
0123 bool IsBigInt() const;
0124
0125
0126
0127
0128
0129
0130 bool IsBoolean() const;
0131
0132
0133
0134
0135
0136
0137 bool IsNumber() const;
0138
0139
0140
0141
0142 bool IsExternal() const;
0143
0144
0145
0146
0147 bool IsInt32() const;
0148
0149
0150
0151
0152 bool IsUint32() const;
0153
0154
0155
0156
0157 bool IsDate() const;
0158
0159
0160
0161
0162 bool IsArgumentsObject() const;
0163
0164
0165
0166
0167 bool IsBigIntObject() const;
0168
0169
0170
0171
0172 bool IsBooleanObject() const;
0173
0174
0175
0176
0177 bool IsNumberObject() const;
0178
0179
0180
0181
0182 bool IsStringObject() const;
0183
0184
0185
0186
0187 bool IsSymbolObject() const;
0188
0189
0190
0191
0192 bool IsNativeError() const;
0193
0194
0195
0196
0197 bool IsRegExp() const;
0198
0199
0200
0201
0202 bool IsAsyncFunction() const;
0203
0204
0205
0206
0207 bool IsGeneratorFunction() const;
0208
0209
0210
0211
0212 bool IsGeneratorObject() const;
0213
0214
0215
0216
0217 bool IsPromise() const;
0218
0219
0220
0221
0222 bool IsMap() const;
0223
0224
0225
0226
0227 bool IsSet() const;
0228
0229
0230
0231
0232 bool IsMapIterator() const;
0233
0234
0235
0236
0237 bool IsSetIterator() const;
0238
0239
0240
0241
0242 bool IsWeakMap() const;
0243
0244
0245
0246
0247 bool IsWeakSet() const;
0248
0249
0250
0251
0252 bool IsWeakRef() const;
0253
0254
0255
0256
0257 bool IsArrayBuffer() const;
0258
0259
0260
0261
0262 bool IsArrayBufferView() const;
0263
0264
0265
0266
0267 bool IsTypedArray() const;
0268
0269
0270
0271
0272 bool IsUint8Array() const;
0273
0274
0275
0276
0277 bool IsUint8ClampedArray() const;
0278
0279
0280
0281
0282 bool IsInt8Array() const;
0283
0284
0285
0286
0287 bool IsUint16Array() const;
0288
0289
0290
0291
0292 bool IsInt16Array() const;
0293
0294
0295
0296
0297 bool IsUint32Array() const;
0298
0299
0300
0301
0302 bool IsInt32Array() const;
0303
0304
0305
0306
0307 bool IsFloat16Array() const;
0308
0309
0310
0311
0312 bool IsFloat32Array() const;
0313
0314
0315
0316
0317 bool IsFloat64Array() const;
0318
0319
0320
0321
0322 bool IsBigInt64Array() const;
0323
0324
0325
0326
0327 bool IsBigUint64Array() const;
0328
0329
0330
0331
0332 bool IsDataView() const;
0333
0334
0335
0336
0337 bool IsSharedArrayBuffer() const;
0338
0339
0340
0341
0342 bool IsProxy() const;
0343
0344
0345
0346
0347 bool IsWasmMemoryObject() const;
0348
0349
0350
0351
0352 bool IsWasmMemoryMapDescriptor() const;
0353
0354
0355
0356
0357 bool IsWasmModuleObject() const;
0358
0359
0360
0361
0362 bool IsWasmNull() const;
0363
0364
0365
0366
0367 bool IsModuleNamespaceObject() const;
0368
0369
0370
0371
0372 bool IsPrimitive() const;
0373
0374
0375
0376
0377
0378 V8_WARN_UNUSED_RESULT MaybeLocal<Primitive> ToPrimitive(
0379 Local<Context> context) const;
0380
0381
0382
0383
0384 V8_WARN_UNUSED_RESULT MaybeLocal<Numeric> ToNumeric(
0385 Local<Context> context) const;
0386
0387
0388
0389 V8_WARN_UNUSED_RESULT MaybeLocal<BigInt> ToBigInt(
0390 Local<Context> context) const;
0391
0392
0393
0394 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
0395 Local<Context> context) const;
0396
0397
0398
0399 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
0400 Local<Context> context) const;
0401
0402
0403
0404
0405
0406 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
0407 Local<Context> context) const;
0408
0409
0410
0411 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
0412 Local<Context> context) const;
0413
0414
0415
0416
0417
0418 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
0419 Local<Context> context) const;
0420
0421
0422
0423
0424
0425 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
0426 Local<Context> context) const;
0427
0428
0429
0430
0431
0432 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
0433
0434
0435
0436
0437 Local<Boolean> ToBoolean(Isolate* isolate) const;
0438
0439
0440
0441
0442
0443 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
0444 Local<Context> context) const;
0445
0446
0447 bool BooleanValue(Isolate* isolate) const;
0448
0449
0450 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
0451
0452 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
0453 Local<Context> context) const;
0454
0455 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
0456 Local<Context> context) const;
0457
0458 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
0459
0460
0461 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
0462 Local<Value> that) const;
0463 bool StrictEquals(Local<Value> that) const;
0464 bool SameValue(Local<Value> that) const;
0465
0466 template <class T>
0467 V8_INLINE static Value* Cast(T* value) {
0468 return static_cast<Value*>(value);
0469 }
0470
0471 Local<String> TypeOf(Isolate*);
0472
0473 Maybe<bool> InstanceOf(Local<Context> context, Local<Object> object);
0474
0475
0476
0477
0478
0479
0480
0481 uint32_t GetHash();
0482
0483 private:
0484 V8_INLINE bool QuickIsUndefined() const;
0485 V8_INLINE bool QuickIsNull() const;
0486 V8_INLINE bool QuickIsNullOrUndefined() const;
0487 #if V8_STATIC_ROOTS_BOOL
0488 V8_INLINE bool QuickIsTrue() const;
0489 V8_INLINE bool QuickIsFalse() const;
0490 #endif
0491 V8_INLINE bool QuickIsString() const;
0492 bool FullIsUndefined() const;
0493 bool FullIsNull() const;
0494 bool FullIsTrue() const;
0495 bool FullIsFalse() const;
0496 bool FullIsString() const;
0497
0498 static void CheckCast(Data* that);
0499 };
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 class V8_EXPORT TypecheckWitness {
0516 public:
0517 explicit TypecheckWitness(Isolate* isolate);
0518
0519
0520
0521
0522
0523
0524
0525 V8_INLINE bool Matches(Local<Value> candidate) const;
0526
0527
0528
0529
0530 void Update(Local<Value> baseline);
0531
0532 private:
0533 Local<Data> cached_map_;
0534 };
0535
0536 template <>
0537 V8_INLINE Value* Value::Cast(Data* value) {
0538 #ifdef V8_ENABLE_CHECKS
0539 CheckCast(value);
0540 #endif
0541 return static_cast<Value*>(value);
0542 }
0543
0544 bool Value::IsUndefined() const {
0545 #ifdef V8_ENABLE_CHECKS
0546 return FullIsUndefined();
0547 #else
0548 return QuickIsUndefined();
0549 #endif
0550 }
0551
0552 bool Value::QuickIsUndefined() const {
0553 using A = internal::Address;
0554 using I = internal::Internals;
0555 A obj = internal::ValueHelper::ValueAsAddress(this);
0556 #if V8_STATIC_ROOTS_BOOL
0557 return I::is_identical(obj, I::StaticReadOnlyRoot::kUndefinedValue);
0558 #else
0559 if (!I::HasHeapObjectTag(obj)) return false;
0560 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0561 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
0562 #endif
0563 }
0564
0565 bool Value::IsNull() const {
0566 #ifdef V8_ENABLE_CHECKS
0567 return FullIsNull();
0568 #else
0569 return QuickIsNull();
0570 #endif
0571 }
0572
0573 bool Value::QuickIsNull() const {
0574 using A = internal::Address;
0575 using I = internal::Internals;
0576 A obj = internal::ValueHelper::ValueAsAddress(this);
0577 #if V8_STATIC_ROOTS_BOOL
0578 return I::is_identical(obj, I::StaticReadOnlyRoot::kNullValue);
0579 #else
0580 if (!I::HasHeapObjectTag(obj)) return false;
0581 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0582 return (I::GetOddballKind(obj) == I::kNullOddballKind);
0583 #endif
0584 }
0585
0586 bool Value::IsNullOrUndefined() const {
0587 #ifdef V8_ENABLE_CHECKS
0588 return FullIsNull() || FullIsUndefined();
0589 #else
0590 return QuickIsNullOrUndefined();
0591 #endif
0592 }
0593
0594 bool Value::QuickIsNullOrUndefined() const {
0595 #if V8_STATIC_ROOTS_BOOL
0596 return QuickIsNull() || QuickIsUndefined();
0597 #else
0598 using A = internal::Address;
0599 using I = internal::Internals;
0600 A obj = internal::ValueHelper::ValueAsAddress(this);
0601 if (!I::HasHeapObjectTag(obj)) return false;
0602 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0603 int kind = I::GetOddballKind(obj);
0604 return kind == I::kNullOddballKind || kind == I::kUndefinedOddballKind;
0605 #endif
0606 }
0607
0608 bool Value::IsTrue() const {
0609 #if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
0610 return QuickIsTrue();
0611 #else
0612 return FullIsTrue();
0613 #endif
0614 }
0615
0616 #if V8_STATIC_ROOTS_BOOL
0617 bool Value::QuickIsTrue() const {
0618 using A = internal::Address;
0619 using I = internal::Internals;
0620 A obj = internal::ValueHelper::ValueAsAddress(this);
0621 return I::is_identical(obj, I::StaticReadOnlyRoot::kTrueValue);
0622 }
0623 #endif
0624
0625 bool Value::IsFalse() const {
0626 #if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
0627 return QuickIsFalse();
0628 #else
0629 return FullIsFalse();
0630 #endif
0631 }
0632
0633 #if V8_STATIC_ROOTS_BOOL
0634 bool Value::QuickIsFalse() const {
0635 using A = internal::Address;
0636 using I = internal::Internals;
0637 A obj = internal::ValueHelper::ValueAsAddress(this);
0638 return I::is_identical(obj, I::StaticReadOnlyRoot::kFalseValue);
0639 }
0640 #endif
0641
0642 bool Value::IsString() const {
0643 #ifdef V8_ENABLE_CHECKS
0644 return FullIsString();
0645 #else
0646 return QuickIsString();
0647 #endif
0648 }
0649
0650 bool Value::QuickIsString() const {
0651 using A = internal::Address;
0652 using I = internal::Internals;
0653 A obj = internal::ValueHelper::ValueAsAddress(this);
0654 if (!I::HasHeapObjectTag(obj)) return false;
0655 #if V8_STATIC_ROOTS_BOOL && !V8_MAP_PACKING
0656 return I::CheckInstanceMapRange(obj,
0657 I::StaticReadOnlyRoot::kStringMapLowerBound,
0658 I::StaticReadOnlyRoot::kStringMapUpperBound);
0659 #else
0660 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
0661 #endif
0662 }
0663
0664 bool TypecheckWitness::Matches(Local<Value> candidate) const {
0665 internal::Address obj = internal::ValueHelper::ValueAsAddress(*candidate);
0666 internal::Address obj_map = internal::Internals::LoadMap(obj);
0667 internal::Address cached =
0668 internal::ValueHelper::ValueAsAddress(*cached_map_);
0669 return obj_map == cached;
0670 }
0671
0672 }
0673
0674 #endif