2 * Copyright (C) 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
3 * Copyright (c) 2011 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "ScriptValue.h"
34 #include "InspectorValues.h"
36 #include "StructureInlines.h"
39 using namespace Inspector
;
41 namespace Deprecated
{
43 ScriptValue::~ScriptValue()
47 bool ScriptValue::getString(ExecState
* scriptState
, String
& result
) const
51 JSLockHolder
lock(scriptState
);
52 if (!m_value
.get().getString(scriptState
, result
))
57 String
ScriptValue::toString(ExecState
* scriptState
) const
59 String result
= m_value
.get().toString(scriptState
)->value(scriptState
);
60 // Handle the case where an exception is thrown as part of invoking toString on the object.
61 if (scriptState
->hadException())
62 scriptState
->clearException();
66 bool ScriptValue::isEqual(ExecState
* scriptState
, const ScriptValue
& anotherValue
) const
69 return anotherValue
.hasNoValue();
70 return JSValueIsEqual(toRef(scriptState
), toRef(scriptState
, jsValue()), toRef(scriptState
, anotherValue
.jsValue()), nullptr);
73 bool ScriptValue::isNull() const
77 return m_value
.get().isNull();
80 bool ScriptValue::isUndefined() const
84 return m_value
.get().isUndefined();
87 bool ScriptValue::isObject() const
91 return m_value
.get().isObject();
94 bool ScriptValue::isFunction() const
97 return getCallData(m_value
.get(), callData
) != CallTypeNone
;
100 #if ENABLE(INSPECTOR)
101 static PassRefPtr
<InspectorValue
> jsToInspectorValue(ExecState
* scriptState
, JSValue value
, int maxDepth
)
104 ASSERT_NOT_REACHED();
113 if (value
.isNull() || value
.isUndefined())
114 return InspectorValue::null();
115 if (value
.isBoolean())
116 return InspectorBasicValue::create(value
.asBoolean());
117 if (value
.isNumber())
118 return InspectorBasicValue::create(value
.asNumber());
119 if (value
.isString())
120 return InspectorString::create(value
.getString(scriptState
));
122 if (value
.isObject()) {
123 if (isJSArray(value
)) {
124 RefPtr
<InspectorArray
> inspectorArray
= InspectorArray::create();
125 JSArray
* array
= asArray(value
);
126 unsigned length
= array
->length();
127 for (unsigned i
= 0; i
< length
; i
++) {
128 JSValue element
= array
->getIndex(scriptState
, i
);
129 RefPtr
<InspectorValue
> elementValue
= jsToInspectorValue(scriptState
, element
, maxDepth
);
132 inspectorArray
->pushValue(elementValue
);
134 return inspectorArray
;
136 RefPtr
<InspectorObject
> inspectorObject
= InspectorObject::create();
137 JSObject
* object
= value
.getObject();
138 PropertyNameArray
propertyNames(scriptState
);
139 object
->methodTable()->getOwnPropertyNames(object
, scriptState
, propertyNames
, ExcludeDontEnumProperties
);
140 for (size_t i
= 0; i
< propertyNames
.size(); i
++) {
141 const Identifier
& name
= propertyNames
[i
];
142 JSValue propertyValue
= object
->get(scriptState
, name
);
143 RefPtr
<InspectorValue
> inspectorValue
= jsToInspectorValue(scriptState
, propertyValue
, maxDepth
);
146 inspectorObject
->setValue(name
.string(), inspectorValue
);
148 return inspectorObject
;
151 ASSERT_NOT_REACHED();
155 PassRefPtr
<InspectorValue
> ScriptValue::toInspectorValue(ExecState
* scriptState
) const
157 JSLockHolder
holder(scriptState
);
158 return jsToInspectorValue(scriptState
, m_value
.get(), InspectorValue::maxDepth
);
160 #endif // ENABLE(INSPECTOR)
162 } // namespace Deprecated