util/src/script/bridge/qscriptfunction.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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-ONLY$
       
    10 ** GNU Lesser General Public License Usage
       
    11 ** This file may be used under the terms of the GNU Lesser
       
    12 ** General Public License version 2.1 as published by the Free Software
       
    13 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    14 ** packaging of this file.  Please review the following information to
       
    15 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    17 **
       
    18 ** If you have questions regarding the use of this file, please contact
       
    19 ** Nokia at qt-info@nokia.com.
       
    20 ** $QT_END_LICENSE$
       
    21 **
       
    22 ****************************************************************************/
       
    23 
       
    24 #include "config.h"
       
    25 #include "qscriptfunction_p.h"
       
    26 
       
    27 #include "private/qscriptengine_p.h"
       
    28 #include "qscriptcontext.h"
       
    29 #include "private/qscriptcontext_p.h"
       
    30 #include "private/qscriptvalue_p.h"
       
    31 #include "qscriptactivationobject_p.h"
       
    32 #include "qscriptobject_p.h"
       
    33 
       
    34 #include "JSGlobalObject.h"
       
    35 #include "DebuggerCallFrame.h"
       
    36 #include "Debugger.h"
       
    37 
       
    38 namespace JSC
       
    39 {
       
    40 ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWrapper));
       
    41 ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWithArgWrapper));
       
    42 }
       
    43 
       
    44 QT_BEGIN_NAMESPACE
       
    45 
       
    46 namespace QScript
       
    47 {
       
    48 
       
    49 const JSC::ClassInfo FunctionWrapper::info = { "QtNativeFunctionWrapper", &PrototypeFunction::info, 0, 0 };
       
    50 const JSC::ClassInfo FunctionWithArgWrapper::info = { "QtNativeFunctionWithArgWrapper", &PrototypeFunction::info, 0, 0 };
       
    51 
       
    52 FunctionWrapper::FunctionWrapper(JSC::ExecState *exec, int length, const JSC::Identifier &name,
       
    53                                  QScriptEngine::FunctionSignature function)
       
    54     : JSC::PrototypeFunction(exec, length, name, proxyCall),
       
    55       data(new Data())
       
    56 {
       
    57     data->function = function;
       
    58 }
       
    59 
       
    60 FunctionWrapper::~FunctionWrapper()
       
    61 {
       
    62     delete data;
       
    63 }
       
    64 
       
    65 JSC::ConstructType FunctionWrapper::getConstructData(JSC::ConstructData& consData)
       
    66 {
       
    67     consData.native.function = proxyConstruct;
       
    68     consData.native.function.doNotCallDebuggerFunctionExit();
       
    69     return JSC::ConstructTypeHost;
       
    70 }
       
    71 
       
    72 JSC::JSValue FunctionWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
       
    73                                         JSC::JSValue thisObject, const JSC::ArgList &args)
       
    74 {
       
    75     FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
       
    76     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
    77 
       
    78     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
    79     eng_p->pushContext(exec, thisObject, args, callee);
       
    80     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
    81 
       
    82     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
       
    83     if (!result.isValid())
       
    84         result = QScriptValue(QScriptValue::UndefinedValue);
       
    85 
       
    86     eng_p->popContext();
       
    87     eng_p->currentFrame = oldFrame;
       
    88 
       
    89     return eng_p->scriptValueToJSCValue(result);
       
    90 }
       
    91 
       
    92 JSC::JSObject* FunctionWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
       
    93                                                const JSC::ArgList &args)
       
    94 {
       
    95     FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
       
    96     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
    97 
       
    98     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
    99     eng_p->pushContext(exec, JSC::JSValue(), args, callee, true);
       
   100     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
   101 
       
   102     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
       
   103 
       
   104     if (JSC::Debugger* debugger = eng_p->originalGlobalObject()->debugger())
       
   105         debugger->functionExit(QScriptValuePrivate::get(result)->jscValue, -1);
       
   106 
       
   107     if (!result.isObject())
       
   108         result = ctx->thisObject();
       
   109 
       
   110     eng_p->popContext();
       
   111     eng_p->currentFrame = oldFrame;
       
   112 
       
   113     return JSC::asObject(eng_p->scriptValueToJSCValue(result));
       
   114 }
       
   115 
       
   116 FunctionWithArgWrapper::FunctionWithArgWrapper(JSC::ExecState *exec, int length, const JSC::Identifier &name,
       
   117                                                QScriptEngine::FunctionWithArgSignature function, void *arg)
       
   118     : JSC::PrototypeFunction(exec, length, name, proxyCall),
       
   119       data(new Data())
       
   120 {
       
   121     data->function = function;
       
   122     data->arg = arg;
       
   123 }
       
   124 
       
   125 FunctionWithArgWrapper::~FunctionWithArgWrapper()
       
   126 {
       
   127     delete data;
       
   128 }
       
   129 
       
   130 JSC::ConstructType FunctionWithArgWrapper::getConstructData(JSC::ConstructData& consData)
       
   131 {
       
   132     consData.native.function = proxyConstruct;
       
   133     return JSC::ConstructTypeHost;
       
   134 }
       
   135 
       
   136 JSC::JSValue FunctionWithArgWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
       
   137                                                JSC::JSValue thisObject, const JSC::ArgList &args)
       
   138 {
       
   139     FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
       
   140     QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
       
   141 
       
   142     JSC::ExecState *oldFrame = eng_p->currentFrame;
       
   143     eng_p->pushContext(exec, thisObject, args, callee);
       
   144     QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
       
   145 
       
   146     QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p), self->data->arg);
       
   147 
       
   148     eng_p->popContext();
       
   149     eng_p->currentFrame = oldFrame;
       
   150 
       
   151     return eng_p->scriptValueToJSCValue(result);
       
   152 }
       
   153 
       
   154 JSC::JSObject* FunctionWithArgWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
       
   155                                                       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, JSC::JSValue(), args, callee, true);
       
   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     if (!result.isObject())
       
   166         result = ctx->thisObject();
       
   167 
       
   168     eng_p->popContext();
       
   169     eng_p->currentFrame = oldFrame;
       
   170 
       
   171     return JSC::asObject(eng_p->scriptValueToJSCValue(result));
       
   172 }
       
   173 
       
   174 } // namespace QScript
       
   175 
       
   176 QT_END_NAMESPACE