]>
git.saurik.com Git - apple/javascriptcore.git/blob - bindings/ScriptValue.cpp
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 static RefPtr
<InspectorValue
> jsToInspectorValue(ExecState
* scriptState
, JSValue value
, int maxDepth
)
103 ASSERT_NOT_REACHED();
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
));
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
);
133 inspectorArray
->pushValue(WTF::move(elementValue
));
135 return WTF::move(inspectorArray
);
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
);
147 inspectorObject
->setValue(name
.string(), WTF::move(inspectorValue
));
149 return WTF::move(inspectorObject
);
152 ASSERT_NOT_REACHED();
156 RefPtr
<InspectorValue
> ScriptValue::toInspectorValue(ExecState
* scriptState
) const
158 JSLockHolder
holder(scriptState
);
159 return jsToInspectorValue(scriptState
, m_value
.get(), InspectorValue::maxDepth
);
162 } // namespace Deprecated