2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "ScriptValue.h"
32 #include "InspectorValues.h"
33 #include "SerializedScriptValue.h"
35 #include <JavaScriptCore/APICast.h>
36 #include <JavaScriptCore/JSValueRef.h>
38 #include <runtime/JSLock.h>
39 #include <runtime/Protect.h>
40 #include <runtime/UString.h>
46 bool ScriptValue::getString(ScriptState* scriptState, String& result) const
50 JSLock lock(SilenceAssertionsOnly);
52 if (!m_value.get().getString(scriptState, ustring))
54 result = ustringToString(ustring);
58 bool ScriptValue::isEqual(ScriptState* scriptState, const ScriptValue& anotherValue) const
61 return anotherValue.hasNoValue();
63 return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), 0);
66 bool ScriptValue::isNull() const
70 return m_value.get().isNull();
73 bool ScriptValue::isUndefined() const
77 return m_value.get().isUndefined();
80 bool ScriptValue::isObject() const
84 return m_value.get().isObject();
87 bool ScriptValue::isFunction() const
90 return getCallData(m_value, callData) != CallTypeNone;
93 PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState)
95 return SerializedScriptValue::create(scriptState, jsValue());
98 ScriptValue ScriptValue::deserialize(ScriptState* scriptState, SerializedScriptValue* value)
100 return ScriptValue(value->deserialize(scriptState, scriptState->lexicalGlobalObject()));
103 #if ENABLE(INSPECTOR)
104 static PassRefPtr<InspectorValue> jsToInspectorValue(ScriptState* scriptState, JSValue value)
107 ASSERT_NOT_REACHED();
110 if (value.isNull() || value.isUndefined())
111 return InspectorValue::null();
112 if (value.isBoolean())
113 return InspectorBasicValue::create(value.getBoolean());
114 if (value.isNumber())
115 return InspectorBasicValue::create(value.uncheckedGetNumber());
116 if (value.isString()) {
117 UString s = value.getString(scriptState);
118 return InspectorString::create(String(s.characters(), s.length()));
120 if (value.isObject()) {
121 if (isJSArray(&scriptState->globalData(), value)) {
122 RefPtr<InspectorArray> inspectorArray = InspectorArray::create();
123 JSArray* array = asArray(value);
124 unsigned length = array->length();
125 for (unsigned i = 0; i < length; i++) {
126 JSValue element = array->getIndex(i);
127 RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element);
129 ASSERT_NOT_REACHED();
130 elementValue = InspectorValue::null();
132 inspectorArray->pushValue(elementValue);
134 return inspectorArray;
136 RefPtr<InspectorObject> inspectorObject = InspectorObject::create();
137 JSObject* object = value.getObject();
138 PropertyNameArray propertyNames(scriptState);
139 object->getOwnPropertyNames(scriptState, propertyNames);
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);
144 if (!inspectorValue) {
145 ASSERT_NOT_REACHED();
146 inspectorValue = InspectorValue::null();
148 inspectorObject->setValue(String(name.characters(), name.length()), inspectorValue);
150 return inspectorObject;
152 ASSERT_NOT_REACHED();
156 PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ScriptState* scriptState) const
158 return jsToInspectorValue(scriptState, m_value.get());
160 #endif // ENABLE(INSPECTOR)
162 } // namespace WebCore