X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/2d39b0e377c0896910ee49ae70082ba665faf986..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/inspector/InspectorValues.h diff --git a/inspector/InspectorValues.h b/inspector/InspectorValues.h index 3b86166..db05036 100644 --- a/inspector/InspectorValues.h +++ b/inspector/InspectorValues.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2009 Google Inc. All rights reserved. + * Copyright (C) 2014 University of Washington. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -31,7 +32,7 @@ #ifndef InspectorValues_h #define InspectorValues_h -#include +#include #include #include #include @@ -41,50 +42,52 @@ namespace Inspector { class InspectorArray; +class InspectorArrayBase; class InspectorObject; +class InspectorObjectBase; class JS_EXPORT_PRIVATE InspectorValue : public RefCounted { public: static const int maxDepth = 1000; - InspectorValue() : m_type(TypeNull) { } + InspectorValue() + : m_type(Type::Null) { } virtual ~InspectorValue() { } - static PassRefPtr null(); + static Ref null(); - typedef enum { - TypeNull = 0, - TypeBoolean, - TypeNumber, - TypeString, - TypeObject, - TypeArray - } Type; + enum class Type { + Null = 0, + Boolean, + Double, + Integer, + String, + Object, + Array + }; Type type() const { return m_type; } - bool isNull() const { return m_type == TypeNull; } - - virtual bool asBoolean(bool* output) const; - virtual bool asNumber(double* output) const; - virtual bool asNumber(float* output) const; - virtual bool asNumber(int* output) const; - virtual bool asNumber(unsigned* output) const; - virtual bool asNumber(long* output) const; - virtual bool asNumber(long long* output) const; - virtual bool asNumber(unsigned long* output) const; - virtual bool asNumber(unsigned long long* output) const; - virtual bool asString(String* output) const; - virtual bool asValue(RefPtr* output); - virtual bool asObject(RefPtr* output); - virtual bool asArray(RefPtr* output); - virtual PassRefPtr asObject(); - virtual PassRefPtr asArray(); - - static PassRefPtr parseJSON(const String& json); + bool isNull() const { return m_type == Type::Null; } + + virtual bool asBoolean(bool&) const; + virtual bool asInteger(int&) const; + virtual bool asInteger(unsigned&) const; + virtual bool asInteger(long&) const; + virtual bool asInteger(long long&) const; + virtual bool asInteger(unsigned long&) const; + virtual bool asInteger(unsigned long long&) const; + virtual bool asDouble(double&) const; + virtual bool asDouble(float&) const; + virtual bool asString(String&) const; + virtual bool asValue(RefPtr&); + virtual bool asObject(RefPtr&); + virtual bool asArray(RefPtr&); + + static bool parseJSON(const String& jsonInput, RefPtr& output); String toJSONString() const; - virtual void writeJSON(StringBuilder* output) const; + virtual void writeJSON(StringBuilder& output) const; protected: explicit InspectorValue(Type type) : m_type(type) { } @@ -96,45 +99,60 @@ private: class JS_EXPORT_PRIVATE InspectorBasicValue : public InspectorValue { public: - static PassRefPtr create(bool); - static PassRefPtr create(int); - static PassRefPtr create(double); + static Ref create(bool); + static Ref create(int); + static Ref create(double); - virtual bool asBoolean(bool* output) const override; - virtual bool asNumber(double* output) const override; - virtual bool asNumber(float* output) const override; - virtual bool asNumber(int* output) const override; - virtual bool asNumber(unsigned* output) const override; - virtual bool asNumber(long* output) const override; - virtual bool asNumber(long long* output) const override; - virtual bool asNumber(unsigned long* output) const override; - virtual bool asNumber(unsigned long long* output) const override; + virtual bool asBoolean(bool&) const override; + // Numbers from the frontend are always parsed as doubles, so we allow + // clients to convert to integral values with this function. + virtual bool asInteger(int&) const override; + virtual bool asInteger(unsigned&) const override; + virtual bool asInteger(long&) const override; + virtual bool asInteger(long long&) const override; + virtual bool asInteger(unsigned long&) const override; + virtual bool asInteger(unsigned long long&) const override; + virtual bool asDouble(double&) const override; + virtual bool asDouble(float&) const override; - virtual void writeJSON(StringBuilder* output) const override; + virtual void writeJSON(StringBuilder& output) const override; private: - explicit InspectorBasicValue(bool value) : InspectorValue(TypeBoolean), m_boolValue(value) { } - explicit InspectorBasicValue(int value) : InspectorValue(TypeNumber), m_doubleValue((double)value) { } - explicit InspectorBasicValue(double value) : InspectorValue(TypeNumber), m_doubleValue(value) { } + explicit InspectorBasicValue(bool value) + : InspectorValue(Type::Boolean) + , m_booleanValue(value) { } + + explicit InspectorBasicValue(int value) + : InspectorValue(Type::Integer) + , m_doubleValue(static_cast(value)) { } + + explicit InspectorBasicValue(double value) + : InspectorValue(Type::Double) + , m_doubleValue(value) { } union { - bool m_boolValue; + bool m_booleanValue; double m_doubleValue; }; }; class JS_EXPORT_PRIVATE InspectorString : public InspectorValue { public: - static PassRefPtr create(const String&); - static PassRefPtr create(const char*); + static Ref create(const String&); + static Ref create(const char*); - virtual bool asString(String* output) const override; + virtual bool asString(String& output) const override; - virtual void writeJSON(StringBuilder* output) const override; + virtual void writeJSON(StringBuilder& output) const override; private: - explicit InspectorString(const String& value) : InspectorValue(TypeString), m_stringValue(value) { } - explicit InspectorString(const char* value) : InspectorValue(TypeString), m_stringValue(value) { } + explicit InspectorString(const String& value) + : InspectorValue(Type::String) + , m_stringValue(value) { } + + explicit InspectorString(const char* value) + : InspectorValue(Type::String) + , m_stringValue(value) { } String m_stringValue; }; @@ -147,39 +165,52 @@ public: typedef Dictionary::iterator iterator; typedef Dictionary::const_iterator const_iterator; - virtual PassRefPtr asObject() override; InspectorObject* openAccessors(); protected: virtual ~InspectorObjectBase(); - virtual bool asObject(RefPtr* output) override; + virtual bool asObject(RefPtr& output) override; + // FIXME: use templates to reduce the amount of duplicated set*() methods. void setBoolean(const String& name, bool); - void setNumber(const String& name, double); + void setInteger(const String& name, int); + void setDouble(const String& name, double); void setString(const String& name, const String&); - void setValue(const String& name, PassRefPtr); - void setObject(const String& name, PassRefPtr); - void setArray(const String& name, PassRefPtr); + void setValue(const String& name, RefPtr&&); + void setObject(const String& name, RefPtr&&); + void setArray(const String& name, RefPtr&&); iterator find(const String& name); const_iterator find(const String& name) const; - bool getBoolean(const String& name, bool* output) const; - template bool getNumber(const String& name, T* output) const + + // FIXME: use templates to reduce the amount of duplicated get*() methods. + bool getBoolean(const String& name, bool& output) const; + template bool getDouble(const String& name, T& output) const + { + RefPtr value; + if (!getValue(name, value)) + return false; + + return value->asDouble(output); + } + template bool getInteger(const String& name, T& output) const { - RefPtr value = get(name); - if (!value) + RefPtr value; + if (!getValue(name, value)) return false; - return value->asNumber(output); + + return value->asInteger(output); } - bool getString(const String& name, String* output) const; - PassRefPtr getObject(const String& name) const; - PassRefPtr getArray(const String& name) const; - PassRefPtr get(const String& name) const; + + bool getString(const String& name, String& output) const; + bool getObject(const String& name, RefPtr&) const; + bool getArray(const String& name, RefPtr&) const; + bool getValue(const String& name, RefPtr&) const; void remove(const String& name); - virtual void writeJSON(StringBuilder* output) const override; + virtual void writeJSON(StringBuilder& output) const override; iterator begin() { return m_data.begin(); } iterator end() { return m_data.end(); } @@ -198,12 +229,13 @@ private: class InspectorObject : public InspectorObjectBase { public: - static JS_EXPORT_PRIVATE PassRefPtr create(); + static JS_EXPORT_PRIVATE Ref create(); using InspectorObjectBase::asObject; using InspectorObjectBase::setBoolean; - using InspectorObjectBase::setNumber; + using InspectorObjectBase::setInteger; + using InspectorObjectBase::setDouble; using InspectorObjectBase::setString; using InspectorObjectBase::setValue; using InspectorObjectBase::setObject; @@ -211,11 +243,12 @@ public: using InspectorObjectBase::find; using InspectorObjectBase::getBoolean; - using InspectorObjectBase::getNumber; + using InspectorObjectBase::getInteger; + using InspectorObjectBase::getDouble; using InspectorObjectBase::getString; using InspectorObjectBase::getObject; using InspectorObjectBase::getArray; - using InspectorObjectBase::get; + using InspectorObjectBase::getValue; using InspectorObjectBase::remove; @@ -231,26 +264,24 @@ public: typedef Vector>::iterator iterator; typedef Vector>::const_iterator const_iterator; - virtual PassRefPtr asArray() override; - - unsigned length() const { return m_data.size(); } + unsigned length() const { return static_cast(m_data.size()); } protected: virtual ~InspectorArrayBase(); - virtual bool asArray(RefPtr* output) override; + virtual bool asArray(RefPtr&) override; void pushBoolean(bool); - void pushInt(int); - void pushNumber(double); + void pushInteger(int); + void pushDouble(double); void pushString(const String&); - void pushValue(PassRefPtr); - void pushObject(PassRefPtr); - void pushArray(PassRefPtr); + void pushValue(RefPtr&&); + void pushObject(RefPtr&&); + void pushArray(RefPtr&&); - PassRefPtr get(size_t index); + RefPtr get(size_t index) const; - virtual void writeJSON(StringBuilder* output) const override; + virtual void writeJSON(StringBuilder& output) const override; iterator begin() { return m_data.begin(); } iterator end() { return m_data.end(); } @@ -266,13 +297,13 @@ private: class InspectorArray : public InspectorArrayBase { public: - static JS_EXPORT_PRIVATE PassRefPtr create(); + static JS_EXPORT_PRIVATE Ref create(); using InspectorArrayBase::asArray; using InspectorArrayBase::pushBoolean; - using InspectorArrayBase::pushInt; - using InspectorArrayBase::pushNumber; + using InspectorArrayBase::pushInteger; + using InspectorArrayBase::pushDouble; using InspectorArrayBase::pushString; using InspectorArrayBase::pushValue; using InspectorArrayBase::pushObject; @@ -300,7 +331,12 @@ inline void InspectorObjectBase::setBoolean(const String& name, bool value) setValue(name, InspectorBasicValue::create(value)); } -inline void InspectorObjectBase::setNumber(const String& name, double value) +inline void InspectorObjectBase::setInteger(const String& name, int value) +{ + setValue(name, InspectorBasicValue::create(value)); +} + +inline void InspectorObjectBase::setDouble(const String& name, double value) { setValue(name, InspectorBasicValue::create(value)); } @@ -310,24 +346,24 @@ inline void InspectorObjectBase::setString(const String& name, const String& val setValue(name, InspectorString::create(value)); } -inline void InspectorObjectBase::setValue(const String& name, PassRefPtr value) +inline void InspectorObjectBase::setValue(const String& name, RefPtr&& value) { ASSERT(value); - if (m_data.set(name, value).isNewEntry) + if (m_data.set(name, WTF::move(value)).isNewEntry) m_order.append(name); } -inline void InspectorObjectBase::setObject(const String& name, PassRefPtr value) +inline void InspectorObjectBase::setObject(const String& name, RefPtr&& value) { ASSERT(value); - if (m_data.set(name, value).isNewEntry) + if (m_data.set(name, WTF::move(value)).isNewEntry) m_order.append(name); } -inline void InspectorObjectBase::setArray(const String& name, PassRefPtr value) +inline void InspectorObjectBase::setArray(const String& name, RefPtr&& value) { ASSERT(value); - if (m_data.set(name, value).isNewEntry) + if (m_data.set(name, WTF::move(value)).isNewEntry) m_order.append(name); } @@ -336,12 +372,12 @@ inline void InspectorArrayBase::pushBoolean(bool value) m_data.append(InspectorBasicValue::create(value)); } -inline void InspectorArrayBase::pushInt(int value) +inline void InspectorArrayBase::pushInteger(int value) { m_data.append(InspectorBasicValue::create(value)); } -inline void InspectorArrayBase::pushNumber(double value) +inline void InspectorArrayBase::pushDouble(double value) { m_data.append(InspectorBasicValue::create(value)); } @@ -351,22 +387,22 @@ inline void InspectorArrayBase::pushString(const String& value) m_data.append(InspectorString::create(value)); } -inline void InspectorArrayBase::pushValue(PassRefPtr value) +inline void InspectorArrayBase::pushValue(RefPtr&& value) { ASSERT(value); - m_data.append(value); + m_data.append(WTF::move(value)); } -inline void InspectorArrayBase::pushObject(PassRefPtr value) +inline void InspectorArrayBase::pushObject(RefPtr&& value) { ASSERT(value); - m_data.append(value); + m_data.append(WTF::move(value)); } -inline void InspectorArrayBase::pushArray(PassRefPtr value) +inline void InspectorArrayBase::pushArray(RefPtr&& value) { ASSERT(value); - m_data.append(value); + m_data.append(WTF::move(value)); } } // namespace Inspector