OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / bindings / js / ScriptValue.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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. 
16  *
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.
27  */
28
29 #include "config.h"
30 #include "ScriptValue.h"
31
32 #include "InspectorValues.h"
33 #include "SerializedScriptValue.h"
34
35 #include <JavaScriptCore/APICast.h>
36 #include <JavaScriptCore/JSValueRef.h>
37
38 #include <runtime/JSLock.h>
39 #include <runtime/Protect.h>
40 #include <runtime/UString.h>
41
42 using namespace JSC;
43
44 namespace WebCore {
45
46 bool ScriptValue::getString(ScriptState* scriptState, String& result) const
47 {
48     if (!m_value)
49         return false;
50     JSLock lock(SilenceAssertionsOnly);
51     UString ustring;
52     if (!m_value.get().getString(scriptState, ustring))
53         return false;
54     result = ustringToString(ustring);
55     return true;
56 }
57
58 bool ScriptValue::isEqual(ScriptState* scriptState, const ScriptValue& anotherValue) const
59 {
60     if (hasNoValue())
61         return anotherValue.hasNoValue();
62
63     return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), 0);
64 }
65
66 bool ScriptValue::isNull() const
67 {
68     if (!m_value)
69         return false;
70     return m_value.get().isNull();
71 }
72
73 bool ScriptValue::isUndefined() const
74 {
75     if (!m_value)
76         return false;
77     return m_value.get().isUndefined();
78 }
79
80 bool ScriptValue::isObject() const
81 {
82     if (!m_value)
83         return false;
84     return m_value.get().isObject();
85 }
86
87 bool ScriptValue::isFunction() const
88 {
89     CallData callData;
90     return getCallData(m_value, callData) != CallTypeNone;
91 }
92
93 PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState)
94 {
95     return SerializedScriptValue::create(scriptState, jsValue());
96 }
97
98 ScriptValue ScriptValue::deserialize(ScriptState* scriptState, SerializedScriptValue* value)
99 {
100     return ScriptValue(value->deserialize(scriptState, scriptState->lexicalGlobalObject()));
101 }
102
103 #if ENABLE(INSPECTOR)
104 static PassRefPtr<InspectorValue> jsToInspectorValue(ScriptState* scriptState, JSValue value)
105 {
106     if (!value) {
107         ASSERT_NOT_REACHED();
108         return 0;
109     }
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()));
119     }
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);
128                 if (!elementValue) {
129                     ASSERT_NOT_REACHED();
130                     elementValue = InspectorValue::null();
131                 }
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->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();
147             }
148             inspectorObject->setValue(String(name.characters(), name.length()), inspectorValue);
149         }
150         return inspectorObject;
151     }
152     ASSERT_NOT_REACHED();
153     return 0;
154 }
155
156 PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ScriptState* scriptState) const
157 {
158     return jsToInspectorValue(scriptState, m_value.get());
159 }
160 #endif // ENABLE(INSPECTOR)
161
162 } // namespace WebCore