#pragma once
#include "Concurrency.h"
#include "ECMAMode.h"
#include "JSExportMacros.h"
#include "PureNaN.h"
#include <functional>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <wtf/Assertions.h>
#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/HashTraits.h>
#include <wtf/MathExtras.h>
#include <wtf/MediaTime.h>
#include <wtf/Nonmovable.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TriState.h>
namespace JSC {
class AssemblyHelpers;
class DeletePropertySlot;
class JSBigInt;
class CallFrame;
class JSCell;
class JSValueSource;
class VM;
class JSGlobalObject;
class JSObject;
class JSString;
class Identifier;
class PropertyName;
class PropertySlot;
class PutPropertySlot;
class Structure;
#if ENABLE(DFG_JIT)
namespace DFG {
class JITCompiler;
class OSRExitCompiler;
class SpeculativeJIT;
}
#endif
#if ENABLE(C_LOOP)
namespace LLInt {
class CLoop;
}
#endif
struct ClassInfo;
struct DumpContext;
struct Instruction;
struct MethodTable;
enum class Unknown { };
template <class T, typename Traits> class WriteBarrierBase;
template<class T>
using WriteBarrierTraitsSelect = typename std::conditional<std::is_same<T, Unknown>::value,
RawValueTraits<T>, RawPtrTraits<T>
>::type;
enum PreferredPrimitiveType : uint8_t { NoPreference, PreferNumber, PreferString };
struct CallData;
typedef int64_t EncodedJSValue;
union EncodedValueDescriptor {
int64_t asInt64;
#if USE(JSVALUE32_64)
double asDouble;
#elif USE(JSVALUE64)
JSCell* ptr;
#endif
#if CPU(BIG_ENDIAN)
struct {
int32_t tag;
int32_t payload;
} asBits;
#else
struct {
int32_t payload;
int32_t tag;
} asBits;
#endif
};
#define TagOffset (offsetof(EncodedValueDescriptor, asBits.tag))
#define PayloadOffset (offsetof(EncodedValueDescriptor, asBits.payload))
#if USE(JSVALUE64)
#define CellPayloadOffset 0
#else
#define CellPayloadOffset PayloadOffset
#endif
enum WhichValueWord {
TagWord,
PayloadWord
};
int64_t tryConvertToInt52(double);
bool isInt52(double);
enum class SourceCodeRepresentation : uint8_t {
Other,
Integer,
Double,
LinkTimeConstant,
};
class JSValue {
friend struct EncodedJSValueHashTraits;
friend struct EncodedJSValueWithRepresentationHashTraits;
friend class AssemblyHelpers;
friend class JIT;
friend class JITSlowPathCall;
friend class JITStubs;
friend class JITStubCall;
friend class JSInterfaceJIT;
friend class JSValueSource;
friend class SpecializedThunkJIT;
#if ENABLE(DFG_JIT)
friend class DFG::JITCompiler;
friend class DFG::OSRExitCompiler;
friend class DFG::SpeculativeJIT;
#endif
#if ENABLE(C_LOOP)
friend class LLInt::CLoop;
#endif
public:
#if USE(JSVALUE32_64)
enum { Int32Tag = 0xffffffff };
enum { BooleanTag = 0xfffffffe };
enum { NullTag = 0xfffffffd };
enum { UndefinedTag = 0xfffffffc };
enum { CellTag = 0xfffffffb };
enum { EmptyValueTag = 0xfffffffa };
enum { DeletedValueTag = 0xfffffff9 };
enum { LowestTag = DeletedValueTag };
#endif
static EncodedJSValue encode(JSValue);
static JSValue decode(EncodedJSValue);
enum JSNullTag { JSNull };
enum JSUndefinedTag { JSUndefined };
enum JSTrueTag { JSTrue };
enum JSFalseTag { JSFalse };
enum JSCellTag { JSCellType };
#if USE(BIGINT32)
enum EncodeAsBigInt32Tag { EncodeAsBigInt32 };
#endif
enum EncodeAsDoubleTag { EncodeAsDouble };
JSValue();
JSValue(JSNullTag);
JSValue(JSUndefinedTag);
JSValue(JSTrueTag);
JSValue(JSFalseTag);
JSValue(JSCell* ptr);
JSValue(const JSCell* ptr);
#if USE(BIGINT32)
JSValue(EncodeAsBigInt32Tag, int32_t);
#endif
JSValue(EncodeAsDoubleTag, double);
explicit JSValue(double);
explicit JSValue(char);
explicit JSValue(unsigned char);
explicit JSValue(short);
explicit JSValue(unsigned short);
explicit JSValue(int);
explicit JSValue(unsigned);
explicit JSValue(long);
explicit JSValue(unsigned long);
explicit JSValue(long long);
explicit JSValue(unsigned long long);
explicit operator bool() const;
bool operator==(const JSValue& other) const;
bool operator!=(const JSValue& other) const;
bool isInt32() const;
bool isUInt32() const;
bool isDouble() const;
bool isTrue() const;
bool isFalse() const;
int32_t asInt32() const;
uint32_t asUInt32() const;
Optional<uint32_t> tryGetAsUint32Index();
Optional<int32_t> tryGetAsInt32();
int64_t asAnyInt() const;
uint32_t asUInt32AsAnyInt() const;
int32_t asInt32AsAnyInt() const;
double asDouble() const;
bool asBoolean() const;
double asNumber() const;
#if USE(BIGINT32)
int32_t bigInt32AsInt32() const; #endif
int32_t asInt32ForArithmetic() const;
bool isEmpty() const;
bool isCallable(VM&) const;
template<Concurrency> TriState isCallableWithConcurrency(VM&) const;
bool isConstructor(VM&) const;
template<Concurrency> TriState isConstructorWithConcurrency(VM&) const;
bool isUndefined() const;
bool isNull() const;
bool isUndefinedOrNull() const;
bool isBoolean() const;
bool isAnyInt() const;
bool isUInt32AsAnyInt() const;
bool isInt32AsAnyInt() const;
bool isNumber() const;
bool isString() const;
bool isBigInt() const;
bool isHeapBigInt() const;
bool isBigInt32() const;
bool isSymbol() const;
bool isPrimitive() const;
bool isGetterSetter() const;
bool isCustomGetterSetter() const;
bool isObject() const;
bool inherits(VM&, const ClassInfo*) const;
template<typename Target> bool inherits(VM&) const;
const ClassInfo* classInfoOrNull(VM&) const;
bool getString(JSGlobalObject*, WTF::String&) const;
WTF::String getString(JSGlobalObject*) const; JSObject* getObject() const;
bool getUInt32(uint32_t&) const;
JSValue toPrimitive(JSGlobalObject*, PreferredPrimitiveType = NoPreference) const;
bool toBoolean(JSGlobalObject*) const;
TriState pureToBoolean() const;
double toNumber(JSGlobalObject*) const;
JSValue toNumeric(JSGlobalObject*) const;
JSValue toBigIntOrInt32(JSGlobalObject*) const;
JSBigInt* asHeapBigInt() const;
Optional<double> toNumberFromPrimitive() const;
JSString* toString(JSGlobalObject*) const; JSString* toStringOrNull(JSGlobalObject*) const; Identifier toPropertyKey(JSGlobalObject*) const;
JSValue toPropertyKeyValue(JSGlobalObject*) const;
WTF::String toWTFString(JSGlobalObject*) const;
JSObject* toObject(JSGlobalObject*) const;
JS_EXPORT_PRIVATE double toInteger(JSGlobalObject*) const;
JS_EXPORT_PRIVATE double toIntegerPreserveNaN(JSGlobalObject*) const;
double toIntegerOrInfinity(JSGlobalObject*) const;
int32_t toInt32(JSGlobalObject*) const;
uint32_t toUInt32(JSGlobalObject*) const;
uint32_t toIndex(JSGlobalObject*, const char* errorName) const;
double toLength(JSGlobalObject*) const;
JS_EXPORT_PRIVATE JSValue toBigInt(JSGlobalObject*) const;
int64_t toBigInt64(JSGlobalObject*) const;
uint64_t toBigUInt64(JSGlobalObject*) const;
Optional<uint32_t> toUInt32AfterToNumeric(JSGlobalObject*) const;
float toFloat(JSGlobalObject* globalObject) const { return static_cast<float>(toNumber(globalObject)); }
JSValue get(JSGlobalObject*, PropertyName) const;
JSValue get(JSGlobalObject*, PropertyName, PropertySlot&) const;
JSValue get(JSGlobalObject*, unsigned propertyName) const;
JSValue get(JSGlobalObject*, unsigned propertyName, PropertySlot&) const;
JSValue get(JSGlobalObject*, uint64_t propertyName) const;
bool getPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&) const;
template<typename CallbackWhenNoException> typename std::result_of<CallbackWhenNoException(bool, PropertySlot&)>::type getPropertySlot(JSGlobalObject*, PropertyName, CallbackWhenNoException) const;
template<typename CallbackWhenNoException> typename std::result_of<CallbackWhenNoException(bool, PropertySlot&)>::type getPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&, CallbackWhenNoException) const;
bool getOwnPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&) const;
bool put(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&);
bool putInline(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&);
JS_EXPORT_PRIVATE bool putToPrimitive(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&);
JS_EXPORT_PRIVATE bool putToPrimitiveByIndex(JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow);
bool putByIndex(JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow);
JSValue getPrototype(JSGlobalObject*) const;
JSValue toThis(JSGlobalObject*, ECMAMode) const;
static bool equal(JSGlobalObject*, JSValue v1, JSValue v2);
static bool equalSlowCase(JSGlobalObject*, JSValue v1, JSValue v2);
static bool equalSlowCaseInline(JSGlobalObject*, JSValue v1, JSValue v2);
static bool strictEqual(JSGlobalObject*, JSValue v1, JSValue v2);
static bool strictEqualForCells(JSGlobalObject*, JSCell* v1, JSCell* v2);
static TriState pureStrictEqual(JSValue v1, JSValue v2);
bool isCell() const;
JSCell* asCell() const;
JS_EXPORT_PRIVATE bool isValidCallee();
Structure* structureOrNull(VM&) const;
JS_EXPORT_PRIVATE void dump(PrintStream&) const;
void dumpInContext(PrintStream&, DumpContext*) const;
void dumpInContextAssumingStructure(PrintStream&, DumpContext*, Structure*) const;
void dumpForBacktrace(PrintStream&) const;
JS_EXPORT_PRIVATE JSObject* synthesizePrototype(JSGlobalObject*) const;
bool requireObjectCoercible(JSGlobalObject*) const;
static constexpr const unsigned numberOfInt52Bits = 52;
static constexpr const int64_t notInt52 = static_cast<int64_t>(1) << numberOfInt52Bits;
static constexpr const unsigned int52ShiftAmount = 12;
static ptrdiff_t offsetOfPayload() { return OBJECT_OFFSETOF(JSValue, u.asBits.payload); }
static ptrdiff_t offsetOfTag() { return OBJECT_OFFSETOF(JSValue, u.asBits.tag); }
#if USE(JSVALUE32_64)
uint32_t tag() const;
int32_t payload() const;
explicit JSValue(int32_t tag, int32_t payload);
#elif USE(JSVALUE64)
static constexpr size_t DoubleEncodeOffsetBit = 49;
static constexpr int64_t DoubleEncodeOffset = 1ll << DoubleEncodeOffsetBit;
static constexpr int64_t NumberTag = 0xfffe000000000000ll;
static constexpr int64_t LowestOfHighBits = 1ULL << 49;
static_assert(LowestOfHighBits & NumberTag);
static_assert(!((LowestOfHighBits>>1) & NumberTag));
static constexpr int32_t OtherTag = 0x2;
static constexpr int32_t BoolTag = 0x4;
static constexpr int32_t UndefinedTag = 0x8;
#if USE(BIGINT32)
static constexpr int32_t BigInt32Tag = 0x12;
static constexpr int64_t BigInt32Mask = NumberTag | BigInt32Tag;
#endif
static constexpr int32_t ValueFalse = OtherTag | BoolTag | false;
static constexpr int32_t ValueTrue = OtherTag | BoolTag | true;
static constexpr int32_t ValueUndefined = OtherTag | UndefinedTag;
static constexpr int32_t ValueNull = OtherTag;
static constexpr int64_t MiscTag = OtherTag | BoolTag | UndefinedTag;
static constexpr int64_t NotCellMask = NumberTag | OtherTag;
static constexpr int32_t ValueEmpty = 0x0;
static constexpr int32_t ValueDeleted = 0x4;
static constexpr int64_t WasmTag = OtherTag | 0x1;
static constexpr int64_t WasmMask = NumberTag | 0x7;
#endif
private:
template <class T> JSValue(WriteBarrierBase<T, WriteBarrierTraitsSelect<T>>);
enum HashTableDeletedValueTag { HashTableDeletedValue };
JSValue(HashTableDeletedValueTag);
inline const JSValue asValue() const { return *this; }
JS_EXPORT_PRIVATE double toNumberSlowCase(JSGlobalObject*) const;
JS_EXPORT_PRIVATE JSString* toStringSlowCase(JSGlobalObject*, bool returnEmptyStringOnError) const;
JS_EXPORT_PRIVATE WTF::String toWTFStringSlowCase(JSGlobalObject*) const;
JS_EXPORT_PRIVATE JSObject* toObjectSlowCase(JSGlobalObject*) const;
JS_EXPORT_PRIVATE JSValue toThisSlowCase(JSGlobalObject*, ECMAMode) const;
EncodedValueDescriptor u;
};
typedef IntHash<EncodedJSValue> EncodedJSValueHash;
#if USE(JSVALUE32_64)
struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> {
static constexpr bool emptyValueIsZero = false;
static EncodedJSValue emptyValue() { return JSValue::encode(JSValue()); }
static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
};
#else
struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> {
static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
};
#endif
typedef std::pair<EncodedJSValue, SourceCodeRepresentation> EncodedJSValueWithRepresentation;
struct EncodedJSValueWithRepresentationHashTraits : HashTraits<EncodedJSValueWithRepresentation> {
static constexpr bool emptyValueIsZero = false;
static EncodedJSValueWithRepresentation emptyValue() { return std::make_pair(JSValue::encode(JSValue()), SourceCodeRepresentation::Other); }
static void constructDeletedValue(EncodedJSValueWithRepresentation& slot) { slot = std::make_pair(JSValue::encode(JSValue(JSValue::HashTableDeletedValue)), SourceCodeRepresentation::Other); }
static bool isDeletedValue(EncodedJSValueWithRepresentation value) { return value == std::make_pair(JSValue::encode(JSValue(JSValue::HashTableDeletedValue)), SourceCodeRepresentation::Other); }
};
struct EncodedJSValueWithRepresentationHash {
static unsigned hash(const EncodedJSValueWithRepresentation& value)
{
return WTF::pairIntHash(EncodedJSValueHash::hash(value.first), IntHash<SourceCodeRepresentation>::hash(value.second));
}
static bool equal(const EncodedJSValueWithRepresentation& a, const EncodedJSValueWithRepresentation& b)
{
return a == b;
}
static constexpr bool safeToCompareToEmptyOrDeleted = true;
};
inline JSValue jsNull()
{
return JSValue(JSValue::JSNull);
}
inline JSValue jsUndefined()
{
return JSValue(JSValue::JSUndefined);
}
inline JSValue jsTDZValue()
{
return JSValue();
}
inline JSValue jsBoolean(bool b)
{
return b ? JSValue(JSValue::JSTrue) : JSValue(JSValue::JSFalse);
}
#if USE(BIGINT32)
ALWAYS_INLINE JSValue jsBigInt32(int32_t intValue)
{
return JSValue(JSValue::EncodeAsBigInt32, intValue);
}
#endif
ALWAYS_INLINE JSValue jsDoubleNumber(double d)
{
ASSERT(JSValue(JSValue::EncodeAsDouble, d).isNumber());
return JSValue(JSValue::EncodeAsDouble, d);
}
ALWAYS_INLINE JSValue jsNumber(double d)
{
ASSERT(JSValue(d).isNumber());
ASSERT(!isImpureNaN(d));
return JSValue(d);
}
ALWAYS_INLINE JSValue jsNumber(const MediaTime& t)
{
return jsNumber(t.toDouble());
}
ALWAYS_INLINE JSValue jsNumber(char i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned char i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(short i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned short i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(int i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(long long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned long long i)
{
return JSValue(i);
}
ALWAYS_INLINE EncodedJSValue encodedJSUndefined()
{
return JSValue::encode(jsUndefined());
}
ALWAYS_INLINE EncodedJSValue encodedJSValue()
{
return JSValue::encode(JSValue());
}
inline bool operator==(const JSValue a, const JSCell* b) { return a == JSValue(b); }
inline bool operator==(const JSCell* a, const JSValue b) { return JSValue(a) == b; }
inline bool operator!=(const JSValue a, const JSCell* b) { return a != JSValue(b); }
inline bool operator!=(const JSCell* a, const JSValue b) { return JSValue(a) != b; }
bool isThisValueAltered(const PutPropertySlot&, JSObject* baseObject);
bool sameValue(JSGlobalObject*, JSValue a, JSValue b);
#if COMPILER(GCC_COMPATIBLE)
ALWAYS_INLINE void ensureStillAliveHere(JSValue value)
{
#if USE(JSVALUE64)
asm volatile ("" : : "g"(bitwise_cast<uint64_t>(value)) : "memory");
#else
asm volatile ("" : : "g"(value.payload()) : "memory");
#endif
}
#else
JS_EXPORT_PRIVATE void ensureStillAliveHere(JSValue);
#endif
class EnsureStillAliveScope {
WTF_FORBID_HEAP_ALLOCATION;
WTF_MAKE_NONCOPYABLE(EnsureStillAliveScope);
WTF_MAKE_NONMOVABLE(EnsureStillAliveScope);
public:
EnsureStillAliveScope(JSValue value)
: m_value(value)
{
}
~EnsureStillAliveScope()
{
ensureStillAliveHere(m_value);
}
JSValue value() const { return m_value; }
private:
JSValue m_value;
};
}