]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/ScriptValue.cpp
JavaScriptCore-7600.1.4.11.8.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 #if ENABLE(INSPECTOR)
101 static PassRefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)
102 {
103 if (!value) {
104 ASSERT_NOT_REACHED();
105 return nullptr;
106 }
107
108 if (!maxDepth)
109 return nullptr;
110
111 maxDepth--;
112
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));
121
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);
130 if (!elementValue)
131 return nullptr;
132 inspectorArray->pushValue(elementValue);
133 }
134 return inspectorArray;
135 }
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);
144 if (!inspectorValue)
145 return nullptr;
146 inspectorObject->setValue(name.string(), inspectorValue);
147 }
148 return inspectorObject;
149 }
150
151 ASSERT_NOT_REACHED();
152 return nullptr;
153 }
154
155 PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
156 {
157 JSLockHolder holder(scriptState);
158 return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth);
159 }
160 #endif // ENABLE(INSPECTOR)
161
162 } // namespace Deprecated