PageRenderTime 57ms CodeModel.GetById 7ms app.highlight 46ms RepoModel.GetById 2ms app.codeStats 0ms

/platform/external/webkit/WebCore/bindings/js/ScriptObject.cpp

https://github.com/aharish/totoro-gb-opensource-update2
C++ | 207 lines | 149 code | 29 blank | 29 comment | 5 complexity | 21df9834f69978b7ba1b0466998f51f6 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 Google 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 are
  6 * met:
  7 *
  8 *     * Redistributions of source code must retain the above copyright
  9 * notice, this list of conditions and the following disclaimer.
 10 *     * Redistributions in binary form must reproduce the above
 11 * copyright notice, this list of conditions and the following disclaimer
 12 * in the documentation and/or other materials provided with the
 13 * distribution.
 14 *     * Neither the name of Google Inc. nor the names of its
 15 * contributors may be used to endorse or promote products derived from
 16 * this software without specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#include "config.h"
 32#include "ScriptObject.h"
 33
 34#include "JSDOMBinding.h"
 35
 36#include <runtime/JSLock.h>
 37
 38#if ENABLE(INSPECTOR)
 39#include "JSInjectedScriptHost.h"
 40#include "JSInspectorBackend.h"
 41#include "JSInspectorFrontendHost.h"
 42#endif
 43
 44using namespace JSC;
 45
 46namespace WebCore {
 47
 48ScriptObject::ScriptObject(ScriptState* scriptState, JSObject* object)
 49    : ScriptValue(object)
 50    , m_scriptState(scriptState)
 51{
 52}
 53
 54static bool handleException(ScriptState* scriptState)
 55{
 56    if (!scriptState->hadException())
 57        return true;
 58
 59    reportException(scriptState, scriptState->exception());
 60    return false;
 61}
 62
 63bool ScriptObject::set(const String& name, const String& value)
 64{
 65    JSLock lock(SilenceAssertionsOnly);
 66    PutPropertySlot slot;
 67    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
 68    return handleException(m_scriptState);
 69}
 70
 71bool ScriptObject::set(const char* name, const ScriptObject& value)
 72{
 73    if (value.scriptState() != m_scriptState) {
 74        ASSERT_NOT_REACHED();
 75        return false;
 76    }
 77    JSLock lock(SilenceAssertionsOnly);
 78    PutPropertySlot slot;
 79    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), value.jsObject(), slot);
 80    return handleException(m_scriptState);
 81}
 82
 83bool ScriptObject::set(const char* name, const String& value)
 84{
 85    JSLock lock(SilenceAssertionsOnly);
 86    PutPropertySlot slot;
 87    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
 88    return handleException(m_scriptState);
 89}
 90
 91bool ScriptObject::set(const char* name, double value)
 92{
 93    JSLock lock(SilenceAssertionsOnly);
 94    PutPropertySlot slot;
 95    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
 96    return handleException(m_scriptState);
 97}
 98
 99bool ScriptObject::set(const char* name, long value)
100{
101    JSLock lock(SilenceAssertionsOnly);
102    PutPropertySlot slot;
103    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
104    return handleException(m_scriptState);
105}
106
107bool ScriptObject::set(const char* name, long long value)
108{
109    JSLock lock(SilenceAssertionsOnly);
110    PutPropertySlot slot;
111    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
112    return handleException(m_scriptState);
113}
114
115bool ScriptObject::set(const char* name, int value)
116{
117    JSLock lock(SilenceAssertionsOnly);
118    PutPropertySlot slot;
119    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
120    return handleException(m_scriptState);
121}
122
123bool ScriptObject::set(const char* name, unsigned value)
124{
125    JSLock lock(SilenceAssertionsOnly);
126    PutPropertySlot slot;
127    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
128    return handleException(m_scriptState);
129}
130
131bool ScriptObject::set(const char* name, unsigned long value)
132{
133    JSLock lock(SilenceAssertionsOnly);
134    PutPropertySlot slot;
135    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
136    return handleException(m_scriptState);
137}
138
139bool ScriptObject::set(const char* name, bool value)
140{
141    JSLock lock(SilenceAssertionsOnly);
142    PutPropertySlot slot;
143    jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsBoolean(value), slot);
144    return handleException(m_scriptState);
145}
146
147ScriptObject ScriptObject::createNew(ScriptState* scriptState)
148{
149    JSLock lock(SilenceAssertionsOnly);
150    return ScriptObject(scriptState, constructEmptyObject(scriptState));
151}
152
153bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, const ScriptObject& value)
154{
155    JSLock lock(SilenceAssertionsOnly);
156    scriptState->lexicalGlobalObject()->putDirect(Identifier(scriptState, name), value.jsObject());
157    return handleException(scriptState);
158}
159
160#if ENABLE(INSPECTOR)
161bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorBackend* value)
162{
163    JSLock lock(SilenceAssertionsOnly);
164    JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
165    globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
166    return handleException(scriptState);
167}
168
169bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorFrontendHost* value)
170{
171    JSLock lock(SilenceAssertionsOnly);
172    JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
173    globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
174    return handleException(scriptState);
175}
176
177bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InjectedScriptHost* value)
178{
179    JSLock lock(SilenceAssertionsOnly);
180    JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
181    globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
182    return handleException(scriptState);
183}
184#endif // ENABLE(INSPECTOR)
185
186bool ScriptGlobalObject::get(ScriptState* scriptState, const char* name, ScriptObject& value)
187{
188    JSLock lock(SilenceAssertionsOnly);
189    JSValue jsValue = scriptState->lexicalGlobalObject()->get(scriptState, Identifier(scriptState, name));
190    if (!jsValue)
191        return false;
192
193    if (!jsValue.isObject())
194        return false;
195
196    value = ScriptObject(scriptState, asObject(jsValue));
197    return true;
198}
199
200bool ScriptGlobalObject::remove(ScriptState* scriptState, const char* name)
201{
202    JSLock lock(SilenceAssertionsOnly);
203    scriptState->lexicalGlobalObject()->deleteProperty(scriptState, Identifier(scriptState, name));
204    return handleException(scriptState);
205}
206
207} // namespace WebCore