v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
v8-value.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_VALUE_H_
6#define INCLUDE_V8_VALUE_H_
7
8#include "v8-data.h" // NOLINT(build/include_directory)
9#include "v8-internal.h" // NOLINT(build/include_directory)
10#include "v8-local-handle.h" // NOLINT(build/include_directory)
11#include "v8-maybe.h" // NOLINT(build/include_directory)
12#include "v8config.h" // NOLINT(build/include_directory)
13
17namespace v8 {
18
19class Primitive;
20class Numeric;
21class BigInt;
22class Int32;
23class Integer;
24class Number;
25class Object;
26class String;
27class Uint32;
28
32class V8_EXPORT Value : public Data {
33 public:
40 V8_INLINE bool IsUndefined() const;
41
48 V8_INLINE bool IsNull() const;
49
57 V8_INLINE bool IsNullOrUndefined() const;
58
66 V8_INLINE bool IsTrue() const;
67
75 V8_INLINE bool IsFalse() const;
76
83 bool IsName() const;
84
91 V8_INLINE bool IsString() const;
92
98 bool IsSymbol() const;
99
105 bool IsFunction() const;
106
111 bool IsArray() const;
112
116 bool IsObject() const;
117
123 bool IsBigInt() const;
124
130 bool IsBoolean() const;
131
137 bool IsNumber() const;
138
142 bool IsExternal() const;
143
147 bool IsInt32() const;
148
152 bool IsUint32() const;
153
157 bool IsDate() const;
158
162 bool IsArgumentsObject() const;
163
167 bool IsBigIntObject() const;
168
172 bool IsBooleanObject() const;
173
177 bool IsNumberObject() const;
178
182 bool IsStringObject() const;
183
187 bool IsSymbolObject() const;
188
192 bool IsNativeError() const;
193
197 bool IsRegExp() const;
198
202 bool IsAsyncFunction() const;
203
207 bool IsGeneratorFunction() const;
208
212 bool IsGeneratorObject() const;
213
217 bool IsPromise() const;
218
222 bool IsMap() const;
223
227 bool IsSet() const;
228
232 bool IsMapIterator() const;
233
237 bool IsSetIterator() const;
238
242 bool IsWeakMap() const;
243
247 bool IsWeakSet() const;
248
252 bool IsWeakRef() const;
253
257 bool IsArrayBuffer() const;
258
262 bool IsArrayBufferView() const;
263
267 bool IsTypedArray() const;
268
272 bool IsUint8Array() const;
273
278
282 bool IsInt8Array() const;
283
287 bool IsUint16Array() const;
288
292 bool IsInt16Array() const;
293
297 bool IsUint32Array() const;
298
302 bool IsInt32Array() const;
303
307 bool IsFloat16Array() const;
308
312 bool IsFloat32Array() const;
313
317 bool IsFloat64Array() const;
318
322 bool IsBigInt64Array() const;
323
327 bool IsBigUint64Array() const;
328
332 bool IsDataView() const;
333
337 bool IsSharedArrayBuffer() const;
338
342 bool IsProxy() const;
343
347 bool IsWasmMemoryObject() const;
348
352 bool IsWasmMemoryMapDescriptor() const;
353
357 bool IsWasmModuleObject() const;
358
362 bool IsWasmNull() const;
363
367 bool IsModuleNamespaceObject() const;
368
372 bool IsPrimitive() const;
373
379 Local<Context> context) const;
385 Local<Context> context) const;
390 Local<Context> context) const;
395 Local<Context> context) const;
400 Local<Context> context) const;
407 Local<Context> context) const;
412 Local<Context> context) const;
419 Local<Context> context) const;
426 Local<Context> context) const;
433
437 Local<Boolean> ToBoolean(Isolate* isolate) const;
438
444 Local<Context> context) const;
445
447 bool BooleanValue(Isolate* isolate) const;
448
450 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
453 Local<Context> context) const;
456 Local<Context> context) const;
458 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
459
462 Local<Value> that) const;
463 bool StrictEquals(Local<Value> that) const;
464 bool SameValue(Local<Value> that) const;
465
466 template <class T>
467 V8_INLINE static Value* Cast(T* value) {
468 return static_cast<Value*>(value);
469 }
470
471 Local<String> TypeOf(Isolate*);
472
473 Maybe<bool> InstanceOf(Local<Context> context, Local<Object> object);
474
481 uint32_t GetHash();
482
483 private:
484 V8_INLINE bool QuickIsUndefined() const;
485 V8_INLINE bool QuickIsNull() const;
486 V8_INLINE bool QuickIsNullOrUndefined() const;
487#if V8_STATIC_ROOTS_BOOL
488 V8_INLINE bool QuickIsTrue() const;
489 V8_INLINE bool QuickIsFalse() const;
490#endif // V8_STATIC_ROOTS_BOOL
491 V8_INLINE bool QuickIsString() const;
492 bool FullIsUndefined() const;
493 bool FullIsNull() const;
494 bool FullIsTrue() const;
495 bool FullIsFalse() const;
496 bool FullIsString() const;
497
498 static void CheckCast(Data* that);
499};
500
516 public:
517 explicit TypecheckWitness(Isolate* isolate);
518
525 V8_INLINE bool Matches(Local<Value> candidate) const;
526
530 void Update(Local<Value> baseline);
531
532 private:
534};
535
536template <>
538#ifdef V8_ENABLE_CHECKS
539 CheckCast(value);
540#endif
541 return static_cast<Value*>(value);
542}
543
544bool Value::IsUndefined() const {
545#ifdef V8_ENABLE_CHECKS
546 return FullIsUndefined();
547#else
548 return QuickIsUndefined();
549#endif
550}
551
553 using A = internal::Address;
554 using I = internal::Internals;
556#if V8_STATIC_ROOTS_BOOL
557 return I::is_identical(obj, I::StaticReadOnlyRoot::kUndefinedValue);
558#else
559 if (!I::HasHeapObjectTag(obj)) return false;
560 if (I::GetInstanceType(obj) != I::kOddballType) return false;
561 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
562#endif // V8_STATIC_ROOTS_BOOL
563}
564
565bool Value::IsNull() const {
566#ifdef V8_ENABLE_CHECKS
567 return FullIsNull();
568#else
569 return QuickIsNull();
570#endif
571}
572
573bool Value::QuickIsNull() const {
574 using A = internal::Address;
575 using I = internal::Internals;
577#if V8_STATIC_ROOTS_BOOL
578 return I::is_identical(obj, I::StaticReadOnlyRoot::kNullValue);
579#else
580 if (!I::HasHeapObjectTag(obj)) return false;
581 if (I::GetInstanceType(obj) != I::kOddballType) return false;
582 return (I::GetOddballKind(obj) == I::kNullOddballKind);
583#endif // V8_STATIC_ROOTS_BOOL
584}
585
587#ifdef V8_ENABLE_CHECKS
588 return FullIsNull() || FullIsUndefined();
589#else
590 return QuickIsNullOrUndefined();
591#endif
592}
593
595#if V8_STATIC_ROOTS_BOOL
596 return QuickIsNull() || QuickIsUndefined();
597#else
598 using A = internal::Address;
599 using I = internal::Internals;
601 if (!I::HasHeapObjectTag(obj)) return false;
602 if (I::GetInstanceType(obj) != I::kOddballType) return false;
603 int kind = I::GetOddballKind(obj);
604 return kind == I::kNullOddballKind || kind == I::kUndefinedOddballKind;
605#endif // V8_STATIC_ROOTS_BOOL
606}
607
608bool Value::IsTrue() const {
609#if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
610 return QuickIsTrue();
611#else
612 return FullIsTrue();
613#endif
614}
615
616#if V8_STATIC_ROOTS_BOOL
617bool Value::QuickIsTrue() const {
618 using A = internal::Address;
619 using I = internal::Internals;
621 return I::is_identical(obj, I::StaticReadOnlyRoot::kTrueValue);
622}
623#endif // V8_STATIC_ROOTS_BOOL
624
625bool Value::IsFalse() const {
626#if V8_STATIC_ROOTS_BOOL && !defined(V8_ENABLE_CHECKS)
627 return QuickIsFalse();
628#else
629 return FullIsFalse();
630#endif
631}
632
633#if V8_STATIC_ROOTS_BOOL
634bool Value::QuickIsFalse() const {
635 using A = internal::Address;
636 using I = internal::Internals;
638 return I::is_identical(obj, I::StaticReadOnlyRoot::kFalseValue);
639}
640#endif // V8_STATIC_ROOTS_BOOL
641
642bool Value::IsString() const {
643#ifdef V8_ENABLE_CHECKS
644 return FullIsString();
645#else
646 return QuickIsString();
647#endif
648}
649
651 using A = internal::Address;
652 using I = internal::Internals;
654 if (!I::HasHeapObjectTag(obj)) return false;
655#if V8_STATIC_ROOTS_BOOL && !V8_MAP_PACKING
656 return I::CheckInstanceMapRange(obj,
657 I::StaticReadOnlyRoot::kStringMapLowerBound,
658 I::StaticReadOnlyRoot::kStringMapUpperBound);
659#else
660 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
661#endif // V8_STATIC_ROOTS_BOOL
662}
663
671
672} // namespace v8
673
674#endif // INCLUDE_V8_VALUE_H_
Builtins::Kind kind
Definition builtins.cc:40
Local< Data > cached_map_
Definition v8-value.h:533
V8_INLINE bool Matches(Local< Value > candidate) const
Definition v8-value.h:664
bool IsSet() const
bool IsSymbolObject() const
bool IsBigInt64Array() const
bool IsStringObject() const
bool IsBigIntObject() const
bool FullIsUndefined() const
Definition api.cc:3480
bool IsWeakMap() const
V8_INLINE bool IsNullOrUndefined() const
Definition v8-value.h:586
V8_INLINE bool IsTrue() const
Definition v8-value.h:608
bool IsNumberObject() const
bool IsBooleanObject() const
bool IsUint32Array() const
V8_INLINE bool QuickIsUndefined() const
Definition v8-value.h:552
bool FullIsTrue() const
Definition api.cc:3492
bool IsWeakRef() const
bool IsUint8ClampedArray() const
bool IsWeakSet() const
V8_INLINE bool IsString() const
Definition v8-value.h:642
V8_INLINE bool IsNull() const
Definition v8-value.h:565
bool IsFloat64Array() const
V8_INLINE bool IsFalse() const
Definition v8-value.h:625
bool IsMap() const
bool FullIsFalse() const
Definition api.cc:3498
bool IsBigUint64Array() const
bool IsInt16Array() const
V8_INLINE bool QuickIsString() const
Definition v8-value.h:650
bool FullIsString() const
Definition api.cc:3515
static V8_INLINE Value * Cast(T *value)
Definition v8-value.h:467
bool IsDate() const
bool IsFloat32Array() const
bool FullIsNull() const
Definition api.cc:3486
V8_INLINE bool QuickIsNull() const
Definition v8-value.h:573
V8_INLINE bool IsUndefined() const
Definition v8-value.h:544
static void CheckCast(Data *that)
Definition api.cc:3844
bool IsInt8Array() const
bool IsArgumentsObject() const
bool IsUint8Array() const
V8_INLINE bool QuickIsNullOrUndefined() const
Definition v8-value.h:594
bool IsUint16Array() const
bool IsInt32Array() const
static V8_INLINE Address LoadMap(Address obj)
static V8_INLINE Address ValueAsAddress(const T *value)
constexpr const char * ToString(DataViewOp op)
UnionOf< BigInt, NonBigIntPrimitive > Primitive
Definition index.h:546
Union< Smi, HeapNumber > Number
Definition globals.h:1181
Union< Smi, HeapNumber, BigInt > Numeric
Definition globals.h:1183
v8_inspector::String16 String
Definition string-util.h:26
#define I(name, number_of_args, result_size)
Definition runtime.cc:36
#define V8_EXPORT
Definition v8config.h:800
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
std::unique_ptr< ValueMirror > value