Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4function.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
"qv4function_p.h"
5
6
#
include
<
private
/
qqmlpropertycachecreator_p
.
h
>
7
#
include
<
private
/
qqmltype_p_p
.
h
>
8
9
#
include
<
private
/
qv4engine_p
.
h
>
10
#
include
<
private
/
qv4functiontable_p
.
h
>
11
#
include
<
private
/
qv4identifiertable_p
.
h
>
12
#
include
<
private
/
qv4jscall_p
.
h
>
13
#
include
<
private
/
qv4vme_moth_p
.
h
>
14
15
#
include
<
assembler
/
MacroAssemblerCodeRef
.
h
>
16
17
QT_BEGIN_NAMESPACE
18
19
namespace
QV4
{
20
21
bool
Function
::
call
(
QObject
*
thisObject
,
void
**
a
,
const
QMetaType
*
types
,
int
argc
,
22
ExecutionContext
*
context
)
23
{
24
if
(
kind
!=
AotCompiled
) {
25
return
QV4
::
convertAndCall
(
26
context
->
engine
(),
thisObject
,
a
,
types
,
argc
,
27
[
this
,
context
](
const
Value
*
thisObject
,
const
Value
*
argv
,
int
argc
) {
28
return
call
(
thisObject
,
argv
,
argc
,
context
);
29
});
30
}
31
32
ExecutionEngine
*
engine
=
context
->
engine
();
33
MetaTypesStackFrame
frame
;
34
frame
.
init
(
this
,
thisObject
,
context
,
a
,
types
,
argc
);
35
frame
.
push
(
engine
);
36
Moth
::
VME
::
exec
(&
frame
,
engine
);
37
frame
.
pop
(
engine
);
38
return
!
frame
.
isReturnValueUndefined
();
39
}
40
41
static
ReturnedValue
doCall
(
42
QV4
::
Function
*
self
,
const
QV4
::
Value
*
thisObject
,
const
QV4
::
Value
*
argv
,
int
argc
,
43
QV4
::
ExecutionContext
*
context
)
44
{
45
ExecutionEngine
*
engine
=
context
->
engine
();
46
JSTypesStackFrame
frame
;
47
frame
.
init
(
self
,
argv
,
argc
);
48
frame
.
setupJSFrame
(
engine
->
jsStackTop
,
Value
::
undefinedValue
(),
context
->
d
(),
49
thisObject
? *
thisObject
:
Value
::
undefinedValue
());
50
engine
->
jsStackTop
+=
frame
.
requiredJSStackFrameSize
();
51
frame
.
push
(
engine
);
52
ReturnedValue
result
=
Moth
::
VME
::
exec
(&
frame
,
engine
);
53
frame
.
pop
(
engine
);
54
return
result
;
55
}
56
57
ReturnedValue
Function
::
call
(
58
const
Value
*
thisObject
,
const
Value
*
argv
,
int
argc
,
ExecutionContext
*
context
) {
59
switch
(
kind
) {
60
case
AotCompiled
:
61
return
QV4
::
convertAndCall
(
62
context
->
engine
(), &
aotCompiledFunction
,
thisObject
,
argv
,
argc
,
63
[
this
,
context
](
64
QObject
*
thisObject
,
void
**
a
,
const
QMetaType
*
types
,
int
argc
) {
65
call
(
thisObject
,
a
,
types
,
argc
,
context
);
66
});
67
case
JsTyped
:
68
return
QV4
::
coerceAndCall
(
69
context
->
engine
(), &
jsTypedFunction
,
compiledFunction
,
argv
,
argc
,
70
[
this
,
context
,
thisObject
](
const
Value
*
argv
,
int
argc
) {
71
return
doCall
(
this
,
thisObject
,
argv
,
argc
,
context
);
72
});
73
default
:
74
break
;
75
}
76
77
return
doCall
(
this
,
thisObject
,
argv
,
argc
,
context
);
78
}
79
80
Function
*
Function
::
create
(
ExecutionEngine
*
engine
,
ExecutableCompilationUnit
*
unit
,
81
const
CompiledData
::
Function
*
function
,
82
const
QQmlPrivate
::
AOTCompiledFunction
*
aotFunction
)
83
{
84
return
new
Function
(
engine
,
unit
,
function
,
aotFunction
);
85
}
86
87
void
Function
::
destroy
()
88
{
89
delete
this
;
90
}
91
92
void
Function
::
mark
(
MarkStack
*
ms
)
93
{
94
if
(
internalClass
)
95
internalClass
->
mark
(
ms
);
96
}
97
98
static
bool
isSpecificType
(
const
CompiledData::ParameterType &type)
99
{
100
return
type.typeNameIndexOrCommonType()
101
!= (type.indexIsCommonType() ? quint32(CompiledData::CommonType::Invalid) : 0);
102
}
103
104
Function
::
Function
(
ExecutionEngine
*
engine
,
ExecutableCompilationUnit
*
unit
,
105
const
CompiledData
::
Function
*
function
,
106
const
QQmlPrivate
::
AOTCompiledFunction
*
aotFunction
)
107
:
FunctionData
(
engine
,
unit
)
108
,
compiledFunction
(
function
)
109
,
codeData
(
function
->
code
())
110
{
111
Scope
scope
(
engine
);
112
Scoped
<
InternalClass
>
ic
(
scope
,
engine
->
internalClasses
(
EngineBase
::
Class_CallContext
));
113
114
// first locals
115
const
quint32_le
*
localsIndices
=
compiledFunction
->
localsTable
();
116
for
(
quint32
i
= 0;
i
<
compiledFunction
->
nLocals
; ++
i
)
117
ic
=
ic
->
addMember
(
engine
->
identifierTable
->
asPropertyKey
(
compilationUnit
->
runtimeStrings
[
localsIndices
[
i
]]),
Attr_NotConfigurable
);
118
119
const
CompiledData
::
Parameter
*
formalsIndices
=
compiledFunction
->
formalsTable
();
120
bool
enforceJsTypes
= !
unit
->
ignoresFunctionSignature
();
121
122
for
(
quint32
i
= 0;
i
<
compiledFunction
->
nFormals
; ++
i
) {
123
ic
=
ic
->
addMember
(
engine
->
identifierTable
->
asPropertyKey
(
compilationUnit
->
runtimeStrings
[
formalsIndices
[
i
].
nameIndex
]),
Attr_NotConfigurable
);
124
if
(
enforceJsTypes
&& !
isSpecificType
(
formalsIndices
[
i
].
type
))
125
enforceJsTypes
=
false
;
126
}
127
internalClass
.
set
(
engine
,
ic
->
d
());
128
129
nFormals
=
compiledFunction
->
nFormals
;
130
131
if
(!
enforceJsTypes
)
132
return
;
133
134
if
(
aotFunction
) {
135
aotCompiledCode
=
aotFunction
->
functionPtr
;
136
new
(&
aotCompiledFunction
)
AOTCompiledFunction
;
137
kind
=
AotCompiled
;
138
aotCompiledFunction
.
types
.
resize
(
aotFunction
->
numArguments
+ 1);
139
aotFunction
->
signature
(
unit
,
aotCompiledFunction
.
types
.
data
());
140
return
;
141
}
142
143
// If a function has any typed arguments, but an untyped return value, the return value is void.
144
// If it doesn't have any arguments at all and the return value is untyped, the function is
145
// untyped. Users can specifically set the return type to "void" to have it enforced.
146
if
(
nFormals
== 0 && !
isSpecificType
(
compiledFunction
->
returnType
))
147
return
;
148
149
QQmlTypeLoader
*
typeLoader
=
engine
->
typeLoader
();
150
151
auto
findQmlType
= [&](
const
CompiledData
::
ParameterType
&
param
) {
152
const
quint32
type
=
param
.
typeNameIndexOrCommonType
();
153
if
(
param
.
indexIsCommonType
()) {
154
return
QQmlMetaType
::
qmlType
(
QQmlPropertyCacheCreatorBase
::
metaTypeForPropertyType
(
155
QV4
::
CompiledData
::
CommonType
(
type
)));
156
}
157
158
if
(
type
== 0 || !
typeLoader
)
159
return
QQmlType
();
160
161
const
QQmlType
qmltype
=
QQmlTypePrivate
::
visibleQmlTypeByName
(
unit
,
type
,
typeLoader
);
162
return
qmltype
.
typeId
().
isValid
() ?
qmltype
:
QQmlType
();
163
};
164
165
new
(&
jsTypedFunction
)
JSTypedFunction
;
166
kind
=
JsTyped
;
167
jsTypedFunction
.
types
.
reserve
(
nFormals
+ 1);
168
jsTypedFunction
.
types
.
append
(
findQmlType
(
compiledFunction
->
returnType
));
169
for
(
quint16
i
= 0;
i
<
nFormals
; ++
i
)
170
jsTypedFunction
.
types
.
append
(
findQmlType
(
formalsIndices
[
i
].
type
));
171
}
172
173
Function
::~
Function
()
174
{
175
if
(
codeRef
) {
176
destroyFunctionTable
(
this
,
codeRef
);
177
delete
codeRef
;
178
}
179
180
switch
(
kind
) {
181
case
JsTyped
:
182
jsTypedFunction
.~
JSTypedFunction
();
183
break
;
184
case
AotCompiled
:
185
aotCompiledFunction
.~
AOTCompiledFunction
();
186
break
;
187
case
JsUntyped
:
188
case
Eval
:
189
break
;
190
}
191
}
192
193
void
Function
::
updateInternalClass
(
ExecutionEngine
*
engine
,
const
QList
<
QByteArray
> &
parameters
)
194
{
195
QStringList
parameterNames
;
196
197
// Resolve duplicate parameter names:
198
for
(
int
i
= 0,
ei
=
parameters
.
size
();
i
!=
ei
; ++
i
) {
199
const
QByteArray
&
param
=
parameters
.
at
(
i
);
200
int
duplicate
= -1;
201
202
for
(
int
j
=
i
- 1;
j
>= 0; --
j
) {
203
const
QByteArray
&
prevParam
=
parameters
.
at
(
j
);
204
if
(
param
==
prevParam
) {
205
duplicate
=
j
;
206
break
;
207
}
208
}
209
210
if
(
duplicate
== -1) {
211
parameterNames
.
append
(
QString
::
fromUtf8
(
param
));
212
}
else
{
213
const
QString
dup
=
parameterNames
[
duplicate
];
214
parameterNames
.
append
(
dup
);
215
parameterNames
[
duplicate
] =
216
QString
(
QChar
(0xfffe)) +
QString
::
number
(
duplicate
) +
dup
;
217
}
218
219
}
220
221
Scope
scope
(
engine
);
222
Scoped
<
InternalClass
>
ic
(
scope
,
engine
->
internalClasses
(
EngineBase
::
Class_CallContext
));
223
224
// first locals
225
const
quint32_le
*
localsIndices
=
compiledFunction
->
localsTable
();
226
for
(
quint32
i
= 0;
i
<
compiledFunction
->
nLocals
; ++
i
) {
227
ic
=
ic
->
addMember
(
228
engine
->
identifierTable
->
asPropertyKey
(
compilationUnit
->
runtimeStrings
[
localsIndices
[
i
]]),
229
Attr_NotConfigurable
);
230
}
231
232
ScopedString
arg
(
scope
);
233
for
(
const
QString
&
parameterName
:
parameterNames
) {
234
arg
=
engine
->
newIdentifier
(
parameterName
);
235
ic
=
ic
->
addMember
(
arg
->
propertyKey
(),
Attr_NotConfigurable
);
236
}
237
internalClass
.
set
(
engine
,
ic
->
d
());
238
nFormals
=
parameters
.
size
();
239
}
240
241
QString
Function
::
prettyName
(
const
Function
*
function
,
const
void
*
code
)
242
{
243
QString
prettyName
=
function
?
function
->
name
()->
toQString
() :
QString
();
244
if
(
prettyName
.
isEmpty
()) {
245
prettyName
=
QString
::
number
(
reinterpret_cast
<
quintptr
>(
code
), 16);
246
prettyName
.
prepend
(
QLatin1String
(
"QV4::Function(0x"
));
247
prettyName
.
append
(
QLatin1Char
(
')'
));
248
}
249
return
prettyName
;
250
}
251
252
QQmlSourceLocation
Function
::
sourceLocation
()
const
253
{
254
return
QQmlSourceLocation
(
255
sourceFile
(),
compiledFunction
->
location
.
line
(),
compiledFunction
->
location
.
column
());
256
}
257
258
FunctionData
::
FunctionData
(
EngineBase
*
engine
,
ExecutableCompilationUnit
*
compilationUnit_
)
259
{
260
compilationUnit
.
set
(
engine
,
compilationUnit_
);
261
}
262
263
}
// namespace QV4
264
265
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qstandardpaths_haiku.cpp:21
QV4
Definition
qjsvalue.h:23
QV4::doCall
static ReturnedValue doCall(QV4::Function *self, const QV4::Value *thisObject, const QV4::Value *argv, int argc, QV4::ExecutionContext *context)
Definition
qv4function.cpp:41
QV4::isSpecificType
static bool isSpecificType(const CompiledData::ParameterType &type)
Definition
qv4function.cpp:98
qtdeclarative
src
qml
jsruntime
qv4function.cpp
Generated on Mon Mar 10 2025 00:48:23 for Qt by
1.13.2