src/script/bridge/qscriptfunction.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtScript module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "config.h"
       
    43 #include "qscriptfunction_p.h"
       
    44 
       
    45 #include "private/qscriptengine_p.h"
       
    46 #include "qscriptcontext.h"
       
    47 #include "private/qscriptcontext_p.h"
       
    48 #include "private/qscriptvalue_p.h"
       
    49 #include "qscriptactivationobject_p.h"
       
    50 #include "qscriptobject_p.h"
       
    51 
       
    52 #include "JSGlobalObject.h"
       
    53 #include "DebuggerCallFrame.h"
       
    54 #include "Debugger.h"
       
    55 
       
    56 namespace JSC
       
    57 {
       
    58 ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWrapper));
       
    59 ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWithArgWrapper));
       
    60 }
       
    61 
       
    62 QT_BEGIN_NAMESPACE
       
    63 
       
    64 namespace QScript
       
    65 {
       
    66 
       
    67 const JSC::ClassInfo FunctionWrapper::info = { "QtNativeFunctionWrapper", &PrototypeFunction::info, 0, 0 };
       
    68 const JSC::ClassInfo FunctionWithArgWrapper::info = { "QtNativeFunctionWithArgWrapper", &PrototypeFunction::info, 0, 0 };
       
    69 
       
    70 FunctionWrapper::FunctionWrapper(JSC::ExecState *exec, int length, const JSC::Identifier &name,
       
    71                                  QScriptEngine::FunctionSignature function)
       
    72     : JSC::PrototypeFunction(exec, length, name, proxyCall),
       
    73       data(new Data())
       
    74 {
       
    75     data->function = function;
       
    76 }
       
    77 
       
    78 FunctionWrapper::~FunctionWrapper()
       
    79 {
       
    80     delete data;
       
    81 }
       
    82 
       
    83 JSC::ConstructType FunctionWrapper::getConstructData(JSC::ConstructData& consData)
       
    84 {
       
    85     consData.native.function = proxyConstruct;
       
    86     consData.native.function.doNotCallDebuggerFunctionExit();
       
    87     return JSC::ConstructTypeHost;
       
    88 }
       
    89 
       
    90 JSC::JSValue FunctionWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
       
    91                                         JSC::JSValue thisObject, const JSC::ArgList &args)
       
    92 {
       
    93     FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
       
    94     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
    95 
       
    96     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
    97     eng_p->pushContext(exec, thisObject, args, callee);
       
    98     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
    99 
       
   100     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
       
   101     if (!result.isValid())
       
   102         result = QScriptValue(QScriptValue::UndefinedValue);
       
   103 
       
   104     eng_p->popContext();
       
   105     eng_p->currentFrame = oldFrame;
       
   106 
       
   107     return eng_p->scriptValueToJSCValue(result);
       
   108 }
       
   109 
       
   110 JSC::JSObject* FunctionWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
       
   111                                                const JSC::ArgList &args)
       
   112 {
       
   113     FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
       
   114     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
   115 
       
   116     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
   117     eng_p->pushContext(exec, JSC::JSValue(), args, callee, true);
       
   118     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
   119 
       
   120     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
       
   121 
       
   122     if (JSC::Debugger* debugger = eng_p->originalGlobalObject()->debugger())
       
   123         debugger->functionExit(QScriptValuePrivate::get(result)->jscValue, -1);
       
   124 
       
   125     if (!result.isObject())
       
   126         result = ctx->thisObject();
       
   127 
       
   128     eng_p->popContext();
       
   129     eng_p->currentFrame = oldFrame;
       
   130 
       
   131     return JSC::asObject(eng_p->scriptValueToJSCValue(result));
       
   132 }
       
   133 
       
   134 FunctionWithArgWrapper::FunctionWithArgWrapper(JSC::ExecState *exec, int length, const JSC::Identifier &name,
       
   135                                                QScriptEngine::FunctionWithArgSignature function, void *arg)
       
   136     : JSC::PrototypeFunction(exec, length, name, proxyCall),
       
   137       data(new Data())
       
   138 {
       
   139     data->function = function;
       
   140     data->arg = arg;
       
   141 }
       
   142 
       
   143 FunctionWithArgWrapper::~FunctionWithArgWrapper()
       
   144 {
       
   145     delete data;
       
   146 }
       
   147 
       
   148 JSC::ConstructType FunctionWithArgWrapper::getConstructData(JSC::ConstructData& consData)
       
   149 {
       
   150     consData.native.function = proxyConstruct;
       
   151     return JSC::ConstructTypeHost;
       
   152 }
       
   153 
       
   154 JSC::JSValue FunctionWithArgWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
       
   155                                                JSC::JSValue thisObject, const JSC::ArgList &args)
       
   156 {
       
   157     FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
       
   158     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
   159 
       
   160     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
   161     eng_p->pushContext(exec, thisObject, args, callee);
       
   162     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
   163 
       
   164     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p), self->data->arg);
       
   165 
       
   166     eng_p->popContext();
       
   167     eng_p->currentFrame = oldFrame;
       
   168 
       
   169     return eng_p->scriptValueToJSCValue(result);
       
   170 }
       
   171 
       
   172 JSC::JSObject* FunctionWithArgWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
       
   173                                                       const JSC::ArgList &args)
       
   174 {
       
   175     FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
       
   176     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
   177 
       
   178     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
   179     eng_p->pushContext(exec, JSC::JSValue(), args, callee, true);
       
   180     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
   181 
       
   182     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p) , self->data->arg);
       
   183     if (!result.isObject())
       
   184         result = ctx->thisObject();
       
   185 
       
   186     eng_p->popContext();
       
   187     eng_p->currentFrame = oldFrame;
       
   188 
       
   189     return JSC::asObject(eng_p->scriptValueToJSCValue(result));
       
   190 }
       
   191 
       
   192 } // namespace QScript
       
   193 
       
   194 QT_END_NAMESPACE