1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qv4function_p.h"
41#include "qv4functionobject_p.h"
42#include "qv4managed_p.h"
43#include "qv4string_p.h"
44#include "qv4value_p.h"
45#include "qv4engine_p.h"
46#include "qv4lookup_p.h"
47#include <private/qv4mm_p.h>
48#include <private/qv4identifiertable_p.h>
49#include <private/qv4functiontable_p.h>
50#include <assembler/MacroAssemblerCodeRef.h>
51#include <private/qv4vme_moth_p.h>
52#include <private/qqmlglobal_p.h>
53
54QT_BEGIN_NAMESPACE
55
56using namespace QV4;
57
58ReturnedValue Function::call(const Value *thisObject, const Value *argv, int argc, const ExecutionContext *context) {
59 ExecutionEngine *engine = context->engine();
60 CppStackFrame frame;
61 frame.init(engine, v4Function: this, argv, argc);
62 frame.setupJSFrame(stackSpace: engine->jsStackTop, function: Value::undefinedValue(), scope: context->d(),
63 thisObject: thisObject ? *thisObject : Value::undefinedValue(),
64 newTarget: Value::undefinedValue());
65
66 frame.push();
67 engine->jsStackTop += frame.requiredJSStackFrameSize();
68
69 ReturnedValue result = Moth::VME::exec(frame: &frame, engine);
70
71 frame.pop();
72
73 return result;
74}
75
76Function *Function::create(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
77 const CompiledData::Function *function)
78{
79 return new Function(engine, unit, function);
80}
81
82void Function::destroy()
83{
84 delete this;
85}
86
87Function::Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
88 const CompiledData::Function *function)
89 : FunctionData(unit)
90 , compiledFunction(function)
91 , codeData(function->code())
92 , jittedCode(nullptr)
93 , codeRef(nullptr)
94{
95 Scope scope(engine);
96 Scoped<InternalClass> ic(scope, engine->internalClasses(icType: EngineBase::Class_CallContext));
97
98 // first locals
99 const quint32_le *localsIndices = compiledFunction->localsTable();
100 for (quint32 i = 0; i < compiledFunction->nLocals; ++i)
101 ic = ic->addMember(identifier: engine->identifierTable->asPropertyKey(str: compilationUnit->runtimeStrings[localsIndices[i]]), data: Attr_NotConfigurable);
102
103 const CompiledData::Parameter *formalsIndices = compiledFunction->formalsTable();
104 for (quint32 i = 0; i < compiledFunction->nFormals; ++i)
105 ic = ic->addMember(identifier: engine->identifierTable->asPropertyKey(str: compilationUnit->runtimeStrings[formalsIndices[i].nameIndex]), data: Attr_NotConfigurable);
106 internalClass = ic->d();
107
108 nFormals = compiledFunction->nFormals;
109}
110
111Function::~Function()
112{
113 if (codeRef) {
114 destroyFunctionTable(function: this, codeRef);
115 delete codeRef;
116 }
117}
118
119void Function::updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> &parameters)
120{
121 QStringList parameterNames;
122
123 // Resolve duplicate parameter names:
124 for (int i = 0, ei = parameters.count(); i != ei; ++i) {
125 const QByteArray &param = parameters.at(i);
126 int duplicate = -1;
127
128 for (int j = i - 1; j >= 0; --j) {
129 const QByteArray &prevParam = parameters.at(i: j);
130 if (param == prevParam) {
131 duplicate = j;
132 break;
133 }
134 }
135
136 if (duplicate == -1) {
137 parameterNames.append(t: QString::fromUtf8(str: param));
138 } else {
139 const QString &dup = parameterNames[duplicate];
140 parameterNames.append(t: dup);
141 parameterNames[duplicate] =
142 QString(0xfffe) + QString::number(duplicate) + dup;
143 }
144
145 }
146
147 internalClass = engine->internalClasses(icType: EngineBase::Class_CallContext);
148
149 // first locals
150 const quint32_le *localsIndices = compiledFunction->localsTable();
151 for (quint32 i = 0; i < compiledFunction->nLocals; ++i) {
152 internalClass = internalClass->addMember(
153 identifier: engine->identifierTable->asPropertyKey(str: compilationUnit->runtimeStrings[localsIndices[i]]),
154 data: Attr_NotConfigurable);
155 }
156
157 Scope scope(engine);
158 ScopedString arg(scope);
159 for (const QString &parameterName : parameterNames) {
160 arg = engine->newIdentifier(text: parameterName);
161 internalClass = internalClass->addMember(identifier: arg->propertyKey(), data: Attr_NotConfigurable);
162 }
163 nFormals = parameters.size();
164}
165
166QString Function::prettyName(const Function *function, const void *code)
167{
168 QString prettyName = function ? function->name()->toQString() : QString();
169 if (prettyName.isEmpty()) {
170 prettyName = QString::number(reinterpret_cast<quintptr>(code), base: 16);
171 prettyName.prepend(s: QLatin1String("QV4::Function(0x"));
172 prettyName.append(c: QLatin1Char(')'));
173 }
174 return prettyName;
175}
176
177QQmlSourceLocation Function::sourceLocation() const
178{
179 return QQmlSourceLocation(sourceFile(), compiledFunction->location.line, compiledFunction->location.column);
180}
181
182QT_END_NAMESPACE
183

source code of qtdeclarative/src/qml/jsruntime/qv4function.cpp