1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtScript module of the Qt Toolkit.
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
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
43
#include "qscriptfunction_p.h"
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"
52
#include "JSGlobalObject.h"
53
#include "DebuggerCallFrame.h"
58
ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWrapper));
59
ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScript::FunctionWithArgWrapper));
67
const JSC::ClassInfo FunctionWrapper::info = { "QtNativeFunctionWrapper", &PrototypeFunction::info, 0, 0 };
68
const JSC::ClassInfo FunctionWithArgWrapper::info = { "QtNativeFunctionWithArgWrapper", &PrototypeFunction::info, 0, 0 };
70
FunctionWrapper::FunctionWrapper(JSC::ExecState *exec, int length, const JSC::Identifier &name,
71
QScriptEngine::FunctionSignature function)
72
: JSC::PrototypeFunction(exec, length, name, proxyCall),
75
data->function = function;
78
FunctionWrapper::~FunctionWrapper()
83
JSC::ConstructType FunctionWrapper::getConstructData(JSC::ConstructData& consData)
85
consData.native.function = proxyConstruct;
86
consData.native.function.doNotCallDebuggerFunctionExit();
87
return JSC::ConstructTypeHost;
90
JSC::JSValue FunctionWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
91
JSC::JSValue thisObject, const JSC::ArgList &args)
93
FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
94
QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
96
JSC::ExecState *oldFrame = eng_p->currentFrame;
97
eng_p->pushContext(exec, thisObject, args, callee);
98
QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
100
QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
101
if (!result.isValid())
102
result = QScriptValue(QScriptValue::UndefinedValue);
105
eng_p->currentFrame = oldFrame;
107
return eng_p->scriptValueToJSCValue(result);
110
JSC::JSObject* FunctionWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
111
const JSC::ArgList &args)
113
FunctionWrapper *self = static_cast<FunctionWrapper*>(callee);
114
QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
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);
120
QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p));
122
if (JSC::Debugger* debugger = eng_p->originalGlobalObject()->debugger())
123
debugger->functionExit(QScriptValuePrivate::get(result)->jscValue, -1);
125
if (!result.isObject())
126
result = ctx->thisObject();
129
eng_p->currentFrame = oldFrame;
131
return JSC::asObject(eng_p->scriptValueToJSCValue(result));
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),
139
data->function = function;
143
FunctionWithArgWrapper::~FunctionWithArgWrapper()
148
JSC::ConstructType FunctionWithArgWrapper::getConstructData(JSC::ConstructData& consData)
150
consData.native.function = proxyConstruct;
151
return JSC::ConstructTypeHost;
154
JSC::JSValue FunctionWithArgWrapper::proxyCall(JSC::ExecState *exec, JSC::JSObject *callee,
155
JSC::JSValue thisObject, const JSC::ArgList &args)
157
FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
158
QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
160
JSC::ExecState *oldFrame = eng_p->currentFrame;
161
eng_p->pushContext(exec, thisObject, args, callee);
162
QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
164
QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p), self->data->arg);
167
eng_p->currentFrame = oldFrame;
169
return eng_p->scriptValueToJSCValue(result);
172
JSC::JSObject* FunctionWithArgWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
173
const JSC::ArgList &args)
175
FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
176
QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
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);
182
QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p) , self->data->arg);
183
if (!result.isObject())
184
result = ctx->thisObject();
187
eng_p->currentFrame = oldFrame;
189
return JSC::asObject(eng_p->scriptValueToJSCValue(result));
192
} // namespace QScript