]> git.saurik.com Git - apple/javascriptcore.git/blame - bindings/ScriptValue.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bindings / ScriptValue.cpp
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
3 * Copyright (c) 2011 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
17 *
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.
28 */
29
30#include "config.h"
31#include "ScriptValue.h"
32
33#include "APICast.h"
34#include "InspectorValues.h"
35#include "JSLock.h"
36#include "StructureInlines.h"
37
38using namespace JSC;
39using namespace Inspector;
40
41namespace Deprecated {
42
43ScriptValue::~ScriptValue()
44{
45}
46
47bool ScriptValue::getString(ExecState* scriptState, String& result) const
48{
49 if (!m_value)
50 return false;
51 JSLockHolder lock(scriptState);
52 if (!m_value.get().getString(scriptState, result))
53 return false;
54 return true;
55}
56
57String ScriptValue::toString(ExecState* scriptState) const
58{
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();
63 return result;
64}
65
66bool ScriptValue::isEqual(ExecState* scriptState, const ScriptValue& anotherValue) const
67{
68 if (hasNoValue())
69 return anotherValue.hasNoValue();
70 return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), nullptr);
71}
72
73bool ScriptValue::isNull() const
74{
75 if (!m_value)
76 return false;
77 return m_value.get().isNull();
78}
79
80bool ScriptValue::isUndefined() const
81{
82 if (!m_value)
83 return false;
84 return m_value.get().isUndefined();
85}
86
87bool ScriptValue::isObject() const
88{
89 if (!m_value)
90 return false;
91 return m_value.get().isObject();
92}
93
94bool ScriptValue::isFunction() const
95{
96 CallData callData;
97 return getCallData(m_value.get(), callData) != CallTypeNone;
98}
99
ed1e77d3 100static RefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)
81345200
A
101{
102 if (!value) {
103 ASSERT_NOT_REACHED();
104 return nullptr;
105 }
106
107 if (!maxDepth)
108 return nullptr;
109
110 maxDepth--;
111
112 if (value.isNull() || value.isUndefined())
113 return InspectorValue::null();
114 if (value.isBoolean())
115 return InspectorBasicValue::create(value.asBoolean());
ed1e77d3 116 if (value.isNumber() && value.isDouble())
81345200 117 return InspectorBasicValue::create(value.asNumber());
ed1e77d3
A
118 if (value.isNumber() && value.isMachineInt())
119 return InspectorBasicValue::create(static_cast<int>(value.asMachineInt()));
81345200
A
120 if (value.isString())
121 return InspectorString::create(value.getString(scriptState));
122
123 if (value.isObject()) {
124 if (isJSArray(value)) {
ed1e77d3 125 Ref<InspectorArray> inspectorArray = InspectorArray::create();
81345200
A
126 JSArray* array = asArray(value);
127 unsigned length = array->length();
128 for (unsigned i = 0; i < length; i++) {
129 JSValue element = array->getIndex(scriptState, i);
130 RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth);
131 if (!elementValue)
132 return nullptr;
ed1e77d3 133 inspectorArray->pushValue(WTF::move(elementValue));
81345200 134 }
ed1e77d3 135 return WTF::move(inspectorArray);
81345200 136 }
ed1e77d3 137 Ref<InspectorObject> inspectorObject = InspectorObject::create();
81345200
A
138 JSObject* object = value.getObject();
139 PropertyNameArray propertyNames(scriptState);
ed1e77d3 140 object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, EnumerationMode());
81345200
A
141 for (size_t i = 0; i < propertyNames.size(); i++) {
142 const Identifier& name = propertyNames[i];
143 JSValue propertyValue = object->get(scriptState, name);
144 RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth);
145 if (!inspectorValue)
146 return nullptr;
ed1e77d3 147 inspectorObject->setValue(name.string(), WTF::move(inspectorValue));
81345200 148 }
ed1e77d3 149 return WTF::move(inspectorObject);
81345200
A
150 }
151
152 ASSERT_NOT_REACHED();
153 return nullptr;
154}
155
ed1e77d3 156RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
81345200
A
157{
158 JSLockHolder holder(scriptState);
159 return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth);
160}
81345200
A
161
162} // namespace Deprecated