]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/ScriptValue.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bindings / ScriptValue.cpp
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
38 using namespace JSC;
39 using namespace Inspector;
40
41 namespace Deprecated {
42
43 ScriptValue::~ScriptValue()
44 {
45 }
46
47 bool 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
57 String 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
66 bool 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
73 bool ScriptValue::isNull() const
74 {
75 if (!m_value)
76 return false;
77 return m_value.get().isNull();
78 }
79
80 bool ScriptValue::isUndefined() const
81 {
82 if (!m_value)
83 return false;
84 return m_value.get().isUndefined();
85 }
86
87 bool ScriptValue::isObject() const
88 {
89 if (!m_value)
90 return false;
91 return m_value.get().isObject();
92 }
93
94 bool ScriptValue::isFunction() const
95 {
96 CallData callData;
97 return getCallData(m_value.get(), callData) != CallTypeNone;
98 }
99
100 static RefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)
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());
116 if (value.isNumber() && value.isDouble())
117 return InspectorBasicValue::create(value.asNumber());
118 if (value.isNumber() && value.isMachineInt())
119 return InspectorBasicValue::create(static_cast<int>(value.asMachineInt()));
120 if (value.isString())
121 return InspectorString::create(value.getString(scriptState));
122
123 if (value.isObject()) {
124 if (isJSArray(value)) {
125 Ref<InspectorArray> inspectorArray = InspectorArray::create();
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;
133 inspectorArray->pushValue(WTF::move(elementValue));
134 }
135 return WTF::move(inspectorArray);
136 }
137 Ref<InspectorObject> inspectorObject = InspectorObject::create();
138 JSObject* object = value.getObject();
139 PropertyNameArray propertyNames(scriptState);
140 object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, EnumerationMode());
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;
147 inspectorObject->setValue(name.string(), WTF::move(inspectorValue));
148 }
149 return WTF::move(inspectorObject);
150 }
151
152 ASSERT_NOT_REACHED();
153 return nullptr;
154 }
155
156 RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
157 {
158 JSLockHolder holder(scriptState);
159 return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth);
160 }
161
162 } // namespace Deprecated