WebCore/bindings/js/ScriptObject.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 
       
    44 using namespace JSC;
       
    45 
       
    46 namespace WebCore {
       
    47 
       
    48 ScriptObject::ScriptObject(ScriptState* scriptState, JSObject* object)
       
    49     : ScriptValue(object)
       
    50     , m_scriptState(scriptState)
       
    51 {
       
    52 }
       
    53 
       
    54 static bool handleException(ScriptState* scriptState)
       
    55 {
       
    56     if (!scriptState->hadException())
       
    57         return true;
       
    58 
       
    59     reportException(scriptState, scriptState->exception());
       
    60     return false;
       
    61 }
       
    62 
       
    63 bool ScriptObject::set(const String& name, const String& value)
       
    64 {
       
    65     JSLock lock(SilenceAssertionsOnly);
       
    66     PutPropertySlot slot;
       
    67     jsObject()->put(m_scriptState, Identifier(m_scriptState, stringToUString(name)), jsString(m_scriptState, stringToUString(value)), slot);
       
    68     return handleException(m_scriptState);
       
    69 }
       
    70 
       
    71 bool 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 
       
    83 bool 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 
       
    91 bool 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 
       
    99 bool 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 
       
   107 bool 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 
       
   115 bool 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 
       
   123 bool 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 
       
   131 bool 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 
       
   139 bool 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 
       
   147 ScriptObject ScriptObject::createNew(ScriptState* scriptState)
       
   148 {
       
   149     JSLock lock(SilenceAssertionsOnly);
       
   150     return ScriptObject(scriptState, constructEmptyObject(scriptState));
       
   151 }
       
   152 
       
   153 bool 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)
       
   161 bool 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 
       
   169 bool 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 
       
   177 bool 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 
       
   186 bool 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 
       
   200 bool 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