File indexing completed on 2025-02-21 10:05:27
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 IsWasmModuleObject() const;
0353
0354
0355
0356
0357 bool IsWasmNull() const;
0358
0359
0360
0361
0362 bool IsModuleNamespaceObject() const;
0363
0364
0365
0366
0367
0368 V8_WARN_UNUSED_RESULT MaybeLocal<Primitive> ToPrimitive(
0369 Local<Context> context) const;
0370
0371
0372
0373
0374 V8_WARN_UNUSED_RESULT MaybeLocal<Numeric> ToNumeric(
0375 Local<Context> context) const;
0376
0377
0378
0379 V8_WARN_UNUSED_RESULT MaybeLocal<BigInt> ToBigInt(
0380 Local<Context> context) const;
0381
0382
0383
0384 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
0385 Local<Context> context) const;
0386
0387
0388
0389 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
0390 Local<Context> context) const;
0391
0392
0393
0394
0395
0396 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
0397 Local<Context> context) const;
0398
0399
0400
0401 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
0402 Local<Context> context) const;
0403
0404
0405
0406
0407
0408 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
0409 Local<Context> context) const;
0410
0411
0412
0413
0414
0415 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
0416 Local<Context> context) const;
0417
0418
0419
0420
0421
0422 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
0423
0424
0425
0426
0427 Local<Boolean> ToBoolean(Isolate* isolate) const;
0428
0429
0430
0431
0432
0433 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
0434 Local<Context> context) const;
0435
0436
0437 bool BooleanValue(Isolate* isolate) const;
0438
0439
0440 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
0441
0442 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
0443 Local<Context> context) const;
0444
0445 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
0446 Local<Context> context) const;
0447
0448 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
0449
0450
0451 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
0452 Local<Value> that) const;
0453 bool StrictEquals(Local<Value> that) const;
0454 bool SameValue(Local<Value> that) const;
0455
0456 template <class T>
0457 V8_INLINE static Value* Cast(T* value) {
0458 return static_cast<Value*>(value);
0459 }
0460
0461 Local<String> TypeOf(Isolate*);
0462
0463 Maybe<bool> InstanceOf(Local<Context> context, Local<Object> object);
0464
0465 private:
0466 V8_INLINE bool QuickIsUndefined() const;
0467 V8_INLINE bool QuickIsNull() const;
0468 V8_INLINE bool QuickIsNullOrUndefined() const;
0469 #if V8_STATIC_ROOTS_BOOL
0470 V8_INLINE bool QuickIsTrue() const;
0471 V8_INLINE bool QuickIsFalse() const;
0472 #endif
0473 V8_INLINE bool QuickIsString() const;
0474 bool FullIsUndefined() const;
0475 bool FullIsNull() const;
0476 bool FullIsTrue() const;
0477 bool FullIsFalse() const;
0478 bool FullIsString() const;
0479
0480 static void CheckCast(Data* that);
0481 };
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497 class V8_EXPORT TypecheckWitness {
0498 public:
0499 explicit TypecheckWitness(Isolate* isolate);
0500
0501
0502
0503
0504
0505
0506
0507 V8_INLINE bool Matches(Local<Value> candidate) const;
0508
0509
0510
0511
0512 void Update(Local<Value> baseline);
0513
0514 private:
0515 Local<Data> cached_map_;
0516 };
0517
0518 template <>
0519 V8_INLINE Value* Value::Cast(Data* value) {
0520 #ifdef V8_ENABLE_CHECKS
0521 CheckCast(value);
0522 #endif
0523 return static_cast<Value*>(value);
0524 }
0525
0526 bool Value::IsUndefined() const {
0527 #ifdef V8_ENABLE_CHECKS
0528 return FullIsUndefined();
0529 #else
0530 return QuickIsUndefined();
0531 #endif
0532 }
0533
0534 bool Value::QuickIsUndefined() const {
0535 using A = internal::Address;
0536 using I = internal::Internals;
0537 A obj = internal::ValueHelper::ValueAsAddress(this);
0538 #if V8_STATIC_ROOTS_BOOL
0539 return I::is_identical(obj, I::StaticReadOnlyRoot::kUndefinedValue);
0540 #else
0541 if (!I::HasHeapObjectTag(obj)) return false;
0542 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0543 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
0544 #endif
0545 }
0546
0547 bool Value::IsNull() const {
0548 #ifdef V8_ENABLE_CHECKS
0549 return FullIsNull();
0550 #else
0551 return QuickIsNull();
0552 #endif
0553 }
0554
0555 bool Value::QuickIsNull() const {
0556 using A = internal::Address;
0557 using I = internal::Internals;
0558 A obj = internal::ValueHelper::ValueAsAddress(this);
0559 #if V8_STATIC_ROOTS_BOOL
0560 return I::is_identical(obj, I::StaticReadOnlyRoot::kNullValue);
0561 #else
0562 if (!I::HasHeapObjectTag(obj)) return false;
0563 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0564 return (I::GetOddballKind(obj) == I::kNullOddballKind);
0565 #endif
0566 }
0567
0568 bool Value::IsNullOrUndefined() const {
0569 #ifdef V8_ENABLE_CHECKS
0570 return FullIsNull() || FullIsUndefined();
0571 #else
0572 return QuickIsNullOrUndefined();
0573 #endif
0574 }
0575
0576 bool Value::QuickIsNullOrUndefined() const {
0577 #if V8_STATIC_ROOTS_BOOL
0578 return QuickIsNull() || QuickIsUndefined();
0579 #else
0580 using A = internal::Address;
0581 using I = internal::Internals;
0582 A obj = internal::ValueHelper::ValueAsAddress(this);
0583 if (!I::HasHeapObjectTag(obj)) return false;
0584 if (I::GetInstanceType(obj) != I::kOddballType) return false;
0585 int kind = I::GetOddballKind(obj);
0586 return kind == I::kNullOddballKind || kind == I::kUndefinedOddballKind;
0587 #endif
0588 }
0589
0590 bool Value::IsTrue() const {
0591 #if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
0592 return QuickIsTrue();
0593 #else
0594 return FullIsTrue();
0595 #endif
0596 }
0597
0598 #if V8_STATIC_ROOTS_BOOL
0599 bool Value::QuickIsTrue() const {
0600 using A = internal::Address;
0601 using I = internal::Internals;
0602 A obj = internal::ValueHelper::ValueAsAddress(this);
0603 return I::is_identical(obj, I::StaticReadOnlyRoot::kTrueValue);
0604 }
0605 #endif
0606
0607 bool Value::IsFalse() const {
0608 #if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
0609 return QuickIsFalse();
0610 #else
0611 return FullIsFalse();
0612 #endif
0613 }
0614
0615 #if V8_STATIC_ROOTS_BOOL
0616 bool Value::QuickIsFalse() const {
0617 using A = internal::Address;
0618 using I = internal::Internals;
0619 A obj = internal::ValueHelper::ValueAsAddress(this);
0620 return I::is_identical(obj, I::StaticReadOnlyRoot::kFalseValue);
0621 }
0622 #endif
0623
0624 bool Value::IsString() const {
0625 #ifdef V8_ENABLE_CHECKS
0626 return FullIsString();
0627 #else
0628 return QuickIsString();
0629 #endif
0630 }
0631
0632 bool Value::QuickIsString() const {
0633 using A = internal::Address;
0634 using I = internal::Internals;
0635 A obj = internal::ValueHelper::ValueAsAddress(this);
0636 if (!I::HasHeapObjectTag(obj)) return false;
0637 #if V8_STATIC_ROOTS_BOOL && !V8_MAP_PACKING
0638 return I::CheckInstanceMapRange(obj, I::StaticReadOnlyRoot::kFirstStringMap,
0639 I::StaticReadOnlyRoot::kLastStringMap);
0640 #else
0641 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
0642 #endif
0643 }
0644
0645 bool TypecheckWitness::Matches(Local<Value> candidate) const {
0646 internal::Address obj = internal::ValueHelper::ValueAsAddress(*candidate);
0647 internal::Address obj_map = internal::Internals::LoadMap(obj);
0648 internal::Address cached =
0649 internal::ValueHelper::ValueAsAddress(*cached_map_);
0650 return obj_map == cached;
0651 }
0652
0653 }
0654
0655 #endif