WebCore/bindings/js/ScriptFunctionCall.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 "ScriptFunctionCall.h"
       
    33 
       
    34 #include "JSDOMBinding.h"
       
    35 #include "JSMainThreadExecState.h"
       
    36 #include "ScriptString.h"
       
    37 #include "ScriptValue.h"
       
    38 
       
    39 #include <runtime/JSLock.h>
       
    40 #include <runtime/UString.h>
       
    41 
       
    42 using namespace JSC;
       
    43 
       
    44 namespace WebCore {
       
    45 
       
    46 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
       
    47     : m_exec(thisObject.scriptState())
       
    48     , m_thisObject(thisObject)
       
    49     , m_name(name)
       
    50 {
       
    51 }
       
    52 
       
    53 void ScriptFunctionCall::appendArgument(const ScriptObject& argument)
       
    54 {
       
    55     if (argument.scriptState() != m_exec) {
       
    56         ASSERT_NOT_REACHED();
       
    57         return;
       
    58     }
       
    59     m_arguments.append(argument.jsObject());
       
    60 }
       
    61 
       
    62 void ScriptFunctionCall::appendArgument(const ScriptString& argument)
       
    63 {
       
    64     m_arguments.append(jsString(m_exec, argument.ustring()));
       
    65 }
       
    66 
       
    67 void ScriptFunctionCall::appendArgument(const ScriptValue& argument)
       
    68 {
       
    69     m_arguments.append(argument.jsValue());
       
    70 }
       
    71 
       
    72 void ScriptFunctionCall::appendArgument(const String& argument)
       
    73 {
       
    74     JSLock lock(SilenceAssertionsOnly);
       
    75     m_arguments.append(jsString(m_exec, argument));
       
    76 }
       
    77 
       
    78 void ScriptFunctionCall::appendArgument(const JSC::UString& argument)
       
    79 {
       
    80     JSLock lock(SilenceAssertionsOnly);
       
    81     m_arguments.append(jsString(m_exec, argument));
       
    82 }
       
    83 
       
    84 void ScriptFunctionCall::appendArgument(const char* argument)
       
    85 {
       
    86     JSLock lock(SilenceAssertionsOnly);
       
    87     m_arguments.append(jsString(m_exec, UString(argument)));
       
    88 }
       
    89 
       
    90 void ScriptFunctionCall::appendArgument(JSC::JSValue argument)
       
    91 {
       
    92     m_arguments.append(argument);
       
    93 }
       
    94 
       
    95 void ScriptFunctionCall::appendArgument(long argument)
       
    96 {
       
    97     JSLock lock(SilenceAssertionsOnly);
       
    98     m_arguments.append(jsNumber(m_exec, argument));
       
    99 }
       
   100 
       
   101 void ScriptFunctionCall::appendArgument(long long argument)
       
   102 {
       
   103     JSLock lock(SilenceAssertionsOnly);
       
   104     m_arguments.append(jsNumber(m_exec, argument));
       
   105 }
       
   106 
       
   107 void ScriptFunctionCall::appendArgument(unsigned int argument)
       
   108 {
       
   109     JSLock lock(SilenceAssertionsOnly);
       
   110     m_arguments.append(jsNumber(m_exec, argument));
       
   111 }
       
   112 
       
   113 void ScriptFunctionCall::appendArgument(unsigned long argument)
       
   114 {
       
   115     JSLock lock(SilenceAssertionsOnly);
       
   116     m_arguments.append(jsNumber(m_exec, argument));
       
   117 }
       
   118 
       
   119 void ScriptFunctionCall::appendArgument(int argument)
       
   120 {
       
   121     JSLock lock(SilenceAssertionsOnly);
       
   122     m_arguments.append(jsNumber(m_exec, argument));
       
   123 }
       
   124 
       
   125 void ScriptFunctionCall::appendArgument(bool argument)
       
   126 {
       
   127     m_arguments.append(jsBoolean(argument));
       
   128 }
       
   129 
       
   130 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
       
   131 {
       
   132     JSObject* thisObject = m_thisObject.jsObject();
       
   133 
       
   134     JSLock lock(SilenceAssertionsOnly);
       
   135 
       
   136     JSValue function = thisObject->get(m_exec, Identifier(m_exec, stringToUString(m_name)));
       
   137     if (m_exec->hadException()) {
       
   138         if (reportExceptions)
       
   139             reportException(m_exec, m_exec->exception());
       
   140 
       
   141         hadException = true;
       
   142         return ScriptValue();
       
   143     }
       
   144 
       
   145     CallData callData;
       
   146     CallType callType = getCallData(function, callData);
       
   147     if (callType == CallTypeNone)
       
   148         return ScriptValue();
       
   149 
       
   150     JSValue result = JSMainThreadExecState::call(m_exec, function, callType, callData, thisObject, m_arguments);
       
   151     if (m_exec->hadException()) {
       
   152         if (reportExceptions)
       
   153             reportException(m_exec, m_exec->exception());
       
   154 
       
   155         hadException = true;
       
   156         return ScriptValue();
       
   157     }
       
   158 
       
   159     return ScriptValue(result);
       
   160 }
       
   161 
       
   162 ScriptValue ScriptFunctionCall::call()
       
   163 {
       
   164     bool hadException = false;
       
   165     return call(hadException);
       
   166 }
       
   167 
       
   168 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
       
   169 {
       
   170     JSObject* thisObject = m_thisObject.jsObject();
       
   171 
       
   172     JSLock lock(SilenceAssertionsOnly);
       
   173 
       
   174     JSObject* constructor = asObject(thisObject->get(m_exec, Identifier(m_exec, stringToUString(m_name))));
       
   175     if (m_exec->hadException()) {
       
   176         if (reportExceptions)
       
   177             reportException(m_exec, m_exec->exception());
       
   178 
       
   179         hadException = true;
       
   180         return ScriptObject();
       
   181     }
       
   182 
       
   183     ConstructData constructData;
       
   184     ConstructType constructType = constructor->getConstructData(constructData);
       
   185     if (constructType == ConstructTypeNone)
       
   186         return ScriptObject();
       
   187 
       
   188     JSValue result = JSC::construct(m_exec, constructor, constructType, constructData, m_arguments);
       
   189     if (m_exec->hadException()) {
       
   190         if (reportExceptions)
       
   191             reportException(m_exec, m_exec->exception());
       
   192 
       
   193         hadException = true;
       
   194         return ScriptObject();
       
   195     }
       
   196 
       
   197     return ScriptObject(m_exec, asObject(result));
       
   198 }
       
   199 
       
   200 } // namespace WebCore