Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qv4runtime.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
// Qt-Security score:significant
4
5
#
include
"qv4runtime_p.h"
6
7
#
include
<
private
/
qqmlengine_p
.
h
>
8
#
include
<
private
/
qqmljavascriptexpression_p
.
h
>
9
#
include
<
private
/
qqmljsast_p
.
h
>
10
#
include
<
private
/
qqmltypewrapper_p
.
h
>
11
#
include
<
private
/
qqmlvaluetypewrapper_p
.
h
>
12
#
include
<
private
/
qv4argumentsobject_p
.
h
>
13
#
include
<
private
/
qv4engine_p
.
h
>
14
#
include
<
private
/
qv4function_p
.
h
>
15
#
include
<
private
/
qv4generatorobject_p
.
h
>
16
#
include
<
private
/
qv4global_p
.
h
>
17
#
include
<
private
/
qv4globalobject_p
.
h
>
18
#
include
<
private
/
qv4jscall_p
.
h
>
19
#
include
<
private
/
qv4lookup_p
.
h
>
20
#
include
<
private
/
qv4math_p
.
h
>
21
#
include
<
private
/
qv4object_p
.
h
>
22
#
include
<
private
/
qv4qmlcontext_p
.
h
>
23
#
include
<
private
/
qv4qobjectwrapper_p
.
h
>
24
#
include
<
private
/
qv4regexp_p
.
h
>
25
#
include
<
private
/
qv4regexpobject_p
.
h
>
26
#
include
<
private
/
qv4scopedvalue_p
.
h
>
27
#
include
<
private
/
qv4stackframe_p
.
h
>
28
#
include
<
private
/
qv4symbol_p
.
h
>
29
30
#
include
<
wtf
/
MathExtras
.
h
>
31
32
#
include
<
QtCore
/
private
/
qlocale_tools_p
.
h
>
33
#
include
<
QtCore
/
qdebug
.
h
>
34
35
#
ifdef
QV4_COUNT_RUNTIME_FUNCTIONS
36
#
include
<
QtCore
/
qbuffer
.
h
>
37
#
endif
// QV4_COUNT_RUNTIME_FUNCTIONS
38
39
#
include
<
cassert
>
40
#
include
<
cstdio
>
41
#
include
<
stdlib
.
h
>
42
43
QT_BEGIN_NAMESPACE
44
45
Q_STATIC_LOGGING_CATEGORY
(lcCoercingTypeAssertion,
"qt.qml.coercingTypeAssertion"
);
46
47
namespace
QV4
{
48
49
#
ifdef
QV4_COUNT_RUNTIME_FUNCTIONS
50
struct
RuntimeCounters
::
Data
{
51
enum
Type
{
52
None
= 0,
53
Undefined
= 1,
54
Null
= 2,
55
Boolean
= 3,
56
Integer
= 4,
57
Managed
= 5,
58
Double
= 7
59
};
60
61
static
const
char
*
pretty
(
Type
t
) {
62
switch
(
t
) {
63
case
None
:
return
""
;
64
case
Undefined
:
return
"Undefined"
;
65
case
Null
:
return
"Null"
;
66
case
Boolean
:
return
"Boolean"
;
67
case
Integer
:
return
"Integer"
;
68
case
Managed
:
return
"Managed"
;
69
case
Double
:
return
"Double"
;
70
default
:
return
"Unknown"
;
71
}
72
}
73
74
static
unsigned
mangle
(
unsigned
tag
) {
75
switch
(
tag
) {
76
case
Value
::
Undefined_Type
:
return
Undefined
;
77
case
Value
::
Null_Type
:
return
Null
;
78
case
Value
::
Boolean_Type
:
return
Boolean
;
79
case
Value
::
Integer_Type
:
return
Integer
;
80
case
Value
::
Managed_Type
:
return
Managed
;
81
default
:
return
Double
;
82
}
83
}
84
85
static
unsigned
mangle
(
unsigned
tag1
,
unsigned
tag2
) {
86
return
(
mangle
(
tag1
) << 3) |
mangle
(
tag2
);
87
}
88
89
static
void
unmangle
(
unsigned
signature
,
Type
&
tag1
,
Type
&
tag2
) {
90
tag1
=
Type
((
signature
>> 3) & 7);
91
tag2
=
Type
(
signature
& 7);
92
}
93
94
typedef
QList
<
quint64
>
Counters
;
95
QHash
<
const
char
*,
Counters
>
counters
;
96
97
inline
void
count
(
const
char
*
func
) {
98
QList
<
quint64
> &
cnt
=
counters
[
func
];
99
if
(
cnt
.
isEmpty
())
100
cnt
.
resize
(64);
101
cnt
[0] += 1;
102
}
103
104
inline
void
count
(
const
char
*
func
,
unsigned
tag
) {
105
QList
<
quint64
> &
cnt
=
counters
[
func
];
106
if
(
cnt
.
isEmpty
())
107
cnt
.
resize
(64);
108
cnt
[
mangle
(
tag
)] += 1;
109
}
110
111
inline
void
count
(
const
char
*
func
,
unsigned
tag1
,
unsigned
tag2
) {
112
QList
<
quint64
> &
cnt
=
counters
[
func
];
113
if
(
cnt
.
isEmpty
())
114
cnt
.
resize
(64);
115
cnt
[
mangle
(
tag1
,
tag2
)] += 1;
116
}
117
118
struct
Line
{
119
const
char
*
func
;
120
Type
tag1
,
tag2
;
121
quint64
count
;
122
123
static
bool
less
(
const
Line
&
line1
,
const
Line
&
line2
) {
124
return
line1
.
count
>
line2
.
count
;
125
}
126
};
127
128
void
dump
()
const
{
129
QBuffer
buf
;
130
buf
.
open
(
QIODevice
::
WriteOnly
);
131
QTextStream
outs
(&
buf
);
132
QList
<
Line
>
lines
;
133
for
(
auto
it
=
counters
.
cbegin
(),
end
=
counters
.
cend
();
it
!=
end
; ++
it
) {
134
const
Counters
&
fCount
=
it
.
value
();
135
for
(
int
i
= 0,
ei
=
fCount
.
size
();
i
!=
ei
; ++
i
) {
136
quint64
count
=
fCount
[
i
];
137
if
(!
count
)
138
continue
;
139
Line
line
;
140
line
.
func
=
it
.
key
();
141
unmangle
(
i
,
line
.
tag1
,
line
.
tag2
);
142
line
.
count
=
count
;
143
lines
.
append
(
line
);
144
}
145
}
146
std
::
sort
(
lines
.
begin
(),
lines
.
end
(),
Line
::
less
);
147
outs
<<
lines
.
size
() <<
" counters:"
<<
endl
;
148
for
(
const
Line
&
line
:
std
::
as_const
(
lines
))
149
outs
<<
qSetFieldWidth
(10) <<
line
.
count
<<
qSetFieldWidth
(0)
150
<<
" | "
<<
line
.
func
151
<<
" | "
<<
pretty
(
line
.
tag1
)
152
<<
" | "
<<
pretty
(
line
.
tag2
)
153
<<
endl
;
154
qDebug
(
"%s"
,
buf
.
data
().
constData
());
155
}
156
};
157
158
RuntimeCounters
*
RuntimeCounters
::
instance
= 0;
159
static
RuntimeCounters
runtimeCountersInstance
;
160
RuntimeCounters
::
RuntimeCounters
()
161
:
d
(
new
Data
)
162
{
163
if
(!
instance
)
164
instance
=
this
;
165
}
166
167
RuntimeCounters
::~
RuntimeCounters
()
168
{
169
d
->
dump
();
170
delete
d
;
171
}
172
173
void
RuntimeCounters
::
count
(
const
char
*
func
)
174
{
175
d
->
count
(
func
);
176
}
177
178
void
RuntimeCounters
::
count
(
const
char
*
func
,
uint
tag
)
179
{
180
d
->
count
(
func
,
tag
);
181
}
182
183
void
RuntimeCounters
::
count
(
const
char
*
func
,
uint
tag1
,
uint
tag2
)
184
{
185
d
->
count
(
func
,
tag1
,
tag2
);
186
}
187
188
#
endif
// QV4_COUNT_RUNTIME_FUNCTIONS
189
190
static
QV4
::Lookup *
runtimeLookup
(Function *f, uint i)
191
{
192
return
f->executableCompilationUnit()->runtimeLookups + i;
193
}
194
195
void
RuntimeHelpers
::
numberToString
(
QString
*
result
,
double
num
,
int
radix
)
196
{
197
Q_ASSERT
(
result
);
198
199
if
(
std
::
isnan
(
num
)) {
200
*
result
=
QStringLiteral
(
"NaN"
);
201
return
;
202
}
else
if
(
qt_is_inf
(
num
)) {
203
*
result
=
num
< 0 ?
QStringLiteral
(
"-Infinity"
) :
QStringLiteral
(
"Infinity"
);
204
return
;
205
}
206
207
if
(
radix
== 10) {
208
// We cannot use our usual locale->toString(...) here, because EcmaScript has special rules
209
// about the longest permissible number, depending on if it's <0 or >0.
210
const
int
ecma_shortest_low
= -6;
211
const
int
ecma_shortest_high
= 21;
212
213
const
QLatin1Char
zero
(
'0'
);
214
const
QLatin1Char
dot
(
'.'
);
215
216
int
decpt
= 0;
217
int
sign
= 0;
218
*
result
=
qdtoa
(
num
, &
decpt
, &
sign
);
219
220
if
(
decpt
<=
ecma_shortest_low
||
decpt
>
ecma_shortest_high
) {
221
if
(
result
->
size
() > 1)
222
result
->
insert
(1,
dot
);
223
result
->
append
(
QLatin1Char
(
'e'
));
224
if
(
decpt
> 0)
225
result
->
append
(
QLatin1Char
(
'+'
));
226
result
->
append
(
QString
::
number
(
decpt
- 1));
227
}
else
if
(
decpt
<= 0) {
228
result
->
prepend
(
QLatin1String
(
"0."
) +
QString
(-
decpt
,
zero
));
229
}
else
if
(
decpt
<
result
->
size
()) {
230
result
->
insert
(
decpt
,
dot
);
231
}
else
{
232
result
->
append
(
QString
(
decpt
-
result
->
size
(),
zero
));
233
}
234
235
if
(
sign
&&
num
)
236
result
->
prepend
(
QLatin1Char
(
'-'
));
237
238
return
;
239
}
240
241
result
->
clear
();
242
bool
negative
=
false
;
243
244
if
(
num
< 0) {
245
negative
=
true
;
246
num
= -
num
;
247
}
248
249
double
frac
=
num
- ::
floor
(
num
);
250
num
=
Value
::
toInteger
(
num
);
251
252
do
{
253
char
c
= (
char
)::
fmod
(
num
,
radix
);
254
c
= (
c
< 10) ? (
c
+
'0'
) : (
c
- 10 +
'a'
);
255
result
->
prepend
(
QLatin1Char
(
c
));
256
num
= ::
floor
(
num
/
radix
);
257
}
while
(
num
!= 0);
258
259
if
(
frac
!= 0) {
260
result
->
append
(
QLatin1Char
(
'.'
));
261
double
magnitude
= 1;
262
double
next
=
frac
;
263
do
{
264
next
*=
radix
;
265
const
int
floored
= ::
floor
(
next
);
266
char
c
=
char
(
floored
);
267
c
= (
c
< 10) ? (
c
+
'0'
) : (
c
- 10 +
'a'
);
268
result
->
append
(
QLatin1Char
(
c
));
269
magnitude
/=
radix
;
270
frac
-=
double
(
floored
) *
magnitude
;
271
next
-=
double
(
floored
);
272
273
// The next digit still makes a difference
274
// if a value of "radix" for it would change frac.
275
// Otherwise we've reached the limit of numerical precision.
276
}
while
(
frac
> 0 &&
frac
-
magnitude
!=
frac
);
277
}
278
279
if
(
negative
)
280
result
->
prepend
(
QLatin1Char
(
'-'
));
281
}
282
283
ReturnedValue
Runtime
::
Closure
::
call
(
ExecutionEngine
*
engine
,
int
functionId
)
284
{
285
QV4
::
Function
*
clos
=
engine
->
currentStackFrame
->
v4Function
->
executableCompilationUnit
()
286
->
runtimeFunctions
[
functionId
];
287
Q_ASSERT
(
clos
);
288
ExecutionContext
*
current
=
engine
->
currentContext
();
289
Scope
s
(
engine
);
290
QV4
::
ScopedObject
closure
(
s
);
291
292
if
(
clos
->
isGenerator
())
293
closure
=
GeneratorFunction
::
create
(
current
,
clos
)->
asReturnedValue
();
294
else
295
closure
=
FunctionObject
::
createScriptFunction
(
current
,
clos
)->
asReturnedValue
();
296
// ### TODO: only keep reference to scripts if actually needed; see QTBUG-130795
297
Scoped
<
QV4
::
QmlContext
>
callingQmlContext
(
s
,
s
.
engine
->
qmlContext
());
298
if
(
callingQmlContext
) {
299
// ### TODO: It would be more efficient to use custom Function prototypes instead of setting a property
300
// ==> QTBUG-130798
301
Scoped
<
QV4
::
QQmlContextWrapper
>
qmlContextWrapper
(
s
,
callingQmlContext
->
d
()->
qml
());
302
const
QV4
::
QQmlContextWrapper
*
resource
=
qmlContextWrapper
;
303
QQmlRefPointer
<
QQmlContextData
>
context
=
resource
->
getContext
();
304
/* we can use fromReturnedValue here, as isNullOrUndefined doesn't allocate.
305
fetching importedScripts twvice is less overhead than unconditionally creating a scope,
306
as script imports are rather rare*/
307
if
(!
QV4
::
Value
::
fromReturnedValue
(
context
->
importedScripts
()).
isNullOrUndefined
()) {
308
QV4
::
ScopedString
name
(
s
,
engine
->
newString
(
QLatin1StringView
(
"$importedScripts"
)));
309
QV4
::
ScopedObject
scripts
(
s
,
context
->
importedScripts
());
310
closure
->
insertMember
(
name
,
scripts
,
Attr_Invalid
);
311
}
312
}
313
return
closure
.
asReturnedValue
();
314
}
315
316
Bool
Runtime
::
DeleteProperty_NoThrow
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
base
,
const
Value
&
index
)
317
{
318
Scope
scope
(
engine
);
319
ScopedObject
o
(
scope
,
base
.
toObject
(
engine
));
320
if
(
scope
.
hasException
())
321
return
Encode
::
undefined
();
322
Q_ASSERT
(
o
);
323
324
ScopedPropertyKey
key
(
scope
,
index
.
toPropertyKey
(
engine
));
325
if
(
engine
->
hasException
)
326
return
false
;
327
return
o
->
deleteProperty
(
key
);
328
}
329
330
ReturnedValue
Runtime
::
DeleteProperty
::
call
(
ExecutionEngine
*
engine
,
QV4
::
Function
*
function
,
const
QV4
::
Value
&
base
,
const
QV4
::
Value
&
index
)
331
{
332
if
(!
Runtime
::
DeleteProperty_NoThrow
::
call
(
engine
,
base
,
index
)) {
333
if
(
function
->
isStrict
())
334
engine
->
throwTypeError
();
335
return
Encode
(
false
);
336
}
else
{
337
return
Encode
(
true
);
338
}
339
}
340
341
Bool
Runtime
::
DeleteName_NoThrow
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
)
342
{
343
Scope
scope
(
engine
);
344
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
345
return
engine
->
currentContext
()->
deleteProperty
(
name
);
346
}
347
348
ReturnedValue
Runtime
::
DeleteName
::
call
(
ExecutionEngine
*
engine
,
Function
*
function
,
int
name
)
349
{
350
if
(!
Runtime
::
DeleteName_NoThrow
::
call
(
engine
,
name
)) {
351
if
(
function
->
isStrict
())
352
engine
->
throwTypeError
();
353
return
Encode
(
false
);
354
}
else
{
355
return
Encode
(
true
);
356
}
357
}
358
359
static
QV4
::
ReturnedValue
doInstanceof
(ExecutionEngine *engine,
const
Value &lval,
const
Value &rval)
360
{
361
// 11.8.6, 5: rval must be an Object
362
const
Object *rhs = rval.as<Object>();
363
if
(!rhs)
364
return
engine->throwTypeError();
365
366
const
FunctionObject *f = rhs->as<FunctionObject>();
367
// shortcut hasInstance evaluation. In this case we know that we are calling the regular hasInstance()
368
// method of the FunctionPrototype
369
if
(f && f->d()->prototype() == engine->functionPrototype()->d() && !f->hasHasInstanceProperty())
370
return
Object::checkedInstanceOf(engine, f, lval);
371
372
Scope
scope(engine);
373
ScopedValue
hasInstance(scope, rhs->get(engine->symbol_hasInstance()));
374
if
(hasInstance->isUndefined())
375
return
Encode(rhs->instanceOf(lval));
376
377
FunctionObject *fHasInstance = hasInstance->as<FunctionObject>();
378
if
(!fHasInstance)
379
return
engine->throwTypeError();
380
381
return
Encode(fHasInstance->call(&rval, &lval, 1));
382
}
383
384
QV4
::
ReturnedValue
Runtime
::
Instanceof
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
lval
,
const
Value
&
rval
)
385
{
386
Scope
scope
(
engine
);
387
ScopedValue
result
(
scope
,
doInstanceof
(
engine
,
lval
,
rval
));
388
return
scope
.
hasException
() ?
Encode
::
undefined
() :
Encode
(
result
->
toBoolean
());
389
}
390
391
QV4
::
ReturnedValue
Runtime
::
As
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
lval
,
const
Value
&
rval
)
392
{
393
Scope
scope
(
engine
);
394
ScopedValue
result
(
scope
,
doInstanceof
(
engine
,
lval
,
rval
));
395
396
if
(
scope
.
hasException
()) {
397
// "foo instanceof valueType" must not throw an exception.
398
// So this can only be an object type.
399
engine
->
catchException
();
400
return
Encode
::
null
();
401
}
402
403
if
(
result
->
toBoolean
())
404
return
lval
.
asReturnedValue
();
405
else
if
(
result
->
isBoolean
())
406
return
Encode
::
null
();
407
408
if
(
engine
->
callingQmlContext
()->
valueTypesAreAssertable
())
409
return
Encode
::
undefined
();
410
411
// Try to convert the value type
412
Scoped
<
QQmlTypeWrapper
>
typeWrapper
(
scope
,
rval
);
413
if
(!
typeWrapper
)
414
return
Encode
::
undefined
();
415
416
const
auto
*
stackFrame
=
engine
->
currentStackFrame
;
417
if
(
lval
.
as
<
QQmlValueTypeWrapper
>()) {
418
qCWarning
(
lcCoercingTypeAssertion
).
nospace
().
noquote
()
419
<<
stackFrame
->
source
() <<
':'
<<
stackFrame
->
lineNumber
() <<
':'
420
<<
" Coercing between incompatible value types mistakenly yields null rather than"
421
<<
" undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent this."
;
422
return
Encode
::
null
();
423
}
424
425
if
(
lval
.
as
<
QV4
::
QObjectWrapper
>()) {
426
qCWarning
(
lcCoercingTypeAssertion
).
nospace
().
noquote
()
427
<<
stackFrame
->
source
() <<
':'
<<
stackFrame
->
lineNumber
() <<
':'
428
<<
" Coercing from instances of object types to value types mistakenly yields null"
429
<<
" rather than undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent"
430
<<
" this."
;
431
return
Encode
::
null
();
432
}
433
434
result
=
coerce
(
engine
,
lval
,
typeWrapper
->
d
()->
type
(),
false
);
435
if
(
result
->
isUndefined
())
436
return
Encode
::
undefined
();
437
438
qCWarning
(
lcCoercingTypeAssertion
).
nospace
().
noquote
()
439
<<
stackFrame
->
source
() <<
':'
<<
stackFrame
->
lineNumber
() <<
':'
440
<<
" Coercing a value to "
<<
typeWrapper
->
toQStringNoThrow
()
441
<<
" using a type assertion. This behavior is deprecated."
442
<<
" Add 'pragma ValueTypeBehavior: Assertable' to prevent it."
;
443
return
result
->
asReturnedValue
();
444
}
445
446
QV4
::
ReturnedValue
Runtime
::
In
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
left
,
const
Value
&
right
)
447
{
448
Object
*
ro
=
right
.
objectValue
();
449
if
(!
ro
)
450
return
engine
->
throwTypeError
();
451
Scope
scope
(
engine
);
452
ScopedPropertyKey
s
(
scope
,
left
.
toPropertyKey
(
engine
));
453
if
(
scope
.
hasException
())
454
return
Encode
::
undefined
();
455
bool
r
=
ro
->
hasProperty
(
s
);
456
return
Encode
(
r
);
457
}
458
459
double
RuntimeHelpers
::
stringToNumber
(
const
QString
&
string
)
460
{
461
// The actual maximum valid length is certainly shorter, but due to the sheer number of
462
// different number formatting variants, we rather err on the side of caution here.
463
// For example, you can have up to 772 valid decimal digits left of the dot, as stated in the
464
// libdoubleconversion sources. The same maximum value would be represented by roughly 3.5 times
465
// as many binary digits.
466
const
int
excessiveLength
= 16 * 1024;
467
if
(
string
.
size
() >
excessiveLength
)
468
return
qQNaN
();
469
470
const
QStringView
s
=
QStringView
(
string
).
trimmed
();
471
if
(
s
.
startsWith
(
QLatin1Char
(
'0'
))) {
472
int
base
= -1;
473
if
(
s
.
startsWith
(
QLatin1String
(
"0x"
)) ||
s
.
startsWith
(
QLatin1String
(
"0X"
)))
474
base
= 16;
475
else
if
(
s
.
startsWith
(
QLatin1String
(
"0o"
)) ||
s
.
startsWith
(
QLatin1String
(
"0O"
)))
476
base
= 8;
477
else
if
(
s
.
startsWith
(
QLatin1String
(
"0b"
)) ||
s
.
startsWith
(
QLatin1String
(
"0B"
)))
478
base
= 2;
479
if
(
base
> 0) {
480
bool
ok
=
true
;
481
qlonglong
num
;
482
num
=
s
.
mid
(2).
toLongLong
(&
ok
,
base
);
483
if
(!
ok
)
484
return
qQNaN
();
485
return
num
;
486
}
487
}
488
bool
ok
=
false
;
489
QByteArray
ba
=
s
.
toLatin1
();
490
const
char
*
begin
=
ba
.
constData
();
491
const
char
*
end
=
nullptr
;
492
double
d
=
qstrtod
(
begin
, &
end
, &
ok
);
493
if
(
end
-
begin
!=
ba
.
size
()) {
494
if
(
ba
==
"Infinity"
||
ba
==
"+Infinity"
)
495
d
=
Q_INFINITY
;
496
else
if
(
ba
==
"-Infinity"
)
497
d
= -
Q_INFINITY
;
498
else
499
d
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
500
}
501
return
d
;
502
}
503
504
Heap
::
String
*
RuntimeHelpers
::
stringFromNumber
(
ExecutionEngine
*
engine
,
double
number
)
505
{
506
QString
qstr
;
507
RuntimeHelpers
::
numberToString
(&
qstr
,
number
, 10);
508
return
engine
->
newString
(
qstr
);
509
}
510
511
ReturnedValue
RuntimeHelpers
::
objectDefaultValue
(
const
Object
*
object
,
int
typeHint
)
512
{
513
ExecutionEngine
*
engine
=
object
->
internalClass
()->
engine
;
514
if
(
engine
->
hasException
)
515
return
Encode
::
undefined
();
516
517
String
*
hint
;
518
switch
(
typeHint
) {
519
case
STRING_HINT
:
520
hint
=
engine
->
id_string
();
521
break
;
522
case
NUMBER_HINT
:
523
hint
=
engine
->
id_number
();
524
break
;
525
default
:
526
hint
=
engine
->
id_default
();
527
break
;
528
}
529
530
Scope
scope
(
engine
);
531
Value
v
=
Value
::
fromReturnedValue
(
object
->
get
(
engine
->
symbol_toPrimitive
()));
532
if
(
engine
->
hasException
|| (!
v
.
isNullOrUndefined
() && !
v
.
isFunctionObject
())) {
533
engine
->
throwTypeError
(
QStringLiteral
(
"Value for symbol toPrimitive is not a function"
));
534
return
Encode
::
undefined
();
535
}
536
537
ScopedFunctionObject
toPrimitive
(
scope
,
v
);
538
if
(
toPrimitive
) {
539
ScopedValue
result
(
scope
,
toPrimitive
->
call
(
object
,
hint
, 1));
540
if
(
engine
->
hasException
)
541
return
Encode
::
undefined
();
542
if
(!
result
->
isPrimitive
())
543
return
engine
->
throwTypeError
();
544
return
result
->
asReturnedValue
();
545
}
546
547
if
(
hint
==
engine
->
id_default
())
548
hint
=
engine
->
id_number
();
549
return
ordinaryToPrimitive
(
engine
,
object
,
hint
);
550
}
551
552
553
ReturnedValue
RuntimeHelpers
::
ordinaryToPrimitive
(
ExecutionEngine
*
engine
,
const
Object
*
object
,
String
*
typeHint
)
554
{
555
Q_ASSERT
(!
engine
->
hasException
);
556
557
String
*
meth1
=
engine
->
id_toString
();
558
String
*
meth2
=
engine
->
id_valueOf
();
559
560
if
(
typeHint
->
propertyKey
() ==
engine
->
id_number
()->
propertyKey
()) {
561
qSwap
(
meth1
,
meth2
);
562
}
else
{
563
Q_ASSERT
(
typeHint
->
propertyKey
() ==
engine
->
id_string
()->
propertyKey
());
564
}
565
566
Scope
scope
(
engine
);
567
ScopedValue
result
(
scope
);
568
569
ScopedValue
conv
(
scope
,
object
->
get
(
meth1
));
570
if
(
FunctionObject
*
o
=
conv
->
as
<
FunctionObject
>()) {
571
result
=
o
->
call
(
object
,
nullptr
, 0);
572
if
(
engine
->
hasException
)
573
return
Encode
::
undefined
();
574
if
(
result
->
isPrimitive
())
575
return
result
->
asReturnedValue
();
576
}
577
578
if
(
engine
->
hasException
)
579
return
Encode
::
undefined
();
580
581
conv
=
object
->
get
(
meth2
);
582
if
(
FunctionObject
*
o
=
conv
->
as
<
FunctionObject
>()) {
583
result
=
o
->
call
(
object
,
nullptr
, 0);
584
if
(
engine
->
hasException
)
585
return
Encode
::
undefined
();
586
if
(
result
->
isPrimitive
())
587
return
result
->
asReturnedValue
();
588
}
589
590
return
engine
->
throwTypeError
();
591
}
592
593
594
Heap
::
Object
*
RuntimeHelpers
::
convertToObject
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
595
{
596
Q_ASSERT
(!
value
.
isObject
());
597
switch
(
value
.
type
()) {
598
case
Value
::
Undefined_Type
:
599
engine
->
throwTypeError
(
QLatin1String
(
"Value is undefined and could not be converted to an object"
));
600
return
nullptr
;
601
case
Value
::
Null_Type
:
602
engine
->
throwTypeError
(
QLatin1String
(
"Value is null and could not be converted to an object"
));
603
return
nullptr
;
604
case
Value
::
Boolean_Type
:
605
return
engine
->
newBooleanObject
(
value
.
booleanValue
());
606
case
Value
::
Managed_Type
:
607
Q_ASSERT
(
value
.
isStringOrSymbol
());
608
if
(!
value
.
isString
())
609
return
engine
->
newSymbolObject
(
value
.
symbolValue
());
610
return
engine
->
newStringObject
(
value
.
stringValue
());
611
case
Value
::
Integer_Type
:
612
default
:
// double
613
return
engine
->
newNumberObject
(
value
.
asDouble
());
614
}
615
}
616
617
Heap
::
String
*
RuntimeHelpers
::
convertToString
(
ExecutionEngine
*
engine
,
Value
value
,
TypeHint
hint
)
618
{
619
redo
:
620
switch
(
value
.
type
()) {
621
case
Value
::
Empty_Type
:
622
Q_ASSERT
(!
"empty Value encountered"
);
623
Q_UNREACHABLE
();
624
case
Value
::
Undefined_Type
:
625
return
engine
->
id_undefined
()->
d
();
626
case
Value
::
Null_Type
:
627
return
engine
->
id_null
()->
d
();
628
case
Value
::
Boolean_Type
:
629
if
(
value
.
booleanValue
())
630
return
engine
->
id_true
()->
d
();
631
else
632
return
engine
->
id_false
()->
d
();
633
case
Value
::
Managed_Type
: {
634
if
(
value
.
isString
())
635
return
static_cast
<
const
String
&>(
value
).
d
();
636
if
(
value
.
isSymbol
()) {
637
engine
->
throwTypeError
(
QLatin1String
(
"Cannot convert a symbol to a string."
));
638
return
nullptr
;
639
}
640
value
=
Value
::
fromReturnedValue
(
RuntimeHelpers
::
toPrimitive
(
value
,
hint
));
641
Q_ASSERT
(
value
.
isPrimitive
());
642
if
(
value
.
isString
())
643
return
static_cast
<
const
String
&>(
value
).
d
();
644
goto
redo
;
645
}
646
case
Value
::
Integer_Type
:
647
return
engine
->
newString
(
QString
::
number
(
value
.
int_32
()));
648
default
:
// double
649
return
RuntimeHelpers
::
stringFromNumber
(
engine
,
value
.
doubleValue
());
650
}
// switch
651
}
652
653
// This is slightly different from the method above, as
654
// the + operator requires a slightly different conversion
655
static
Heap
::String *
convert_to_string_add
(ExecutionEngine *engine, Value value)
656
{
657
return
RuntimeHelpers::convertToString(engine, value, PREFERREDTYPE_HINT);
658
}
659
660
QV4
::
ReturnedValue
RuntimeHelpers
::
addHelper
(
ExecutionEngine
*
engine
,
const
Value
&
left
,
const
Value
&
right
)
661
{
662
Scope
scope
(
engine
);
663
664
ScopedValue
pleft
(
scope
,
RuntimeHelpers
::
toPrimitive
(
left
,
PREFERREDTYPE_HINT
));
665
ScopedValue
pright
(
scope
,
RuntimeHelpers
::
toPrimitive
(
right
,
PREFERREDTYPE_HINT
));
666
String
*
sleft
=
pleft
->
stringValue
();
667
String
*
sright
=
pright
->
stringValue
();
668
if
(
sleft
||
sright
) {
669
if
(!
sleft
) {
670
pleft
=
convert_to_string_add
(
engine
,
pleft
);
671
sleft
=
static_cast
<
String
*>(
pleft
.
ptr
);
672
}
673
if
(!
sright
) {
674
pright
=
convert_to_string_add
(
engine
,
pright
);
675
sright
=
static_cast
<
String
*>(
pright
.
ptr
);
676
}
677
if
(
engine
->
hasException
)
678
return
Encode
::
undefined
();
679
if
(!
sleft
->
d
()->
length
())
680
return
sright
->
asReturnedValue
();
681
if
(!
sright
->
d
()->
length
())
682
return
sleft
->
asReturnedValue
();
683
MemoryManager
*
mm
=
engine
->
memoryManager
;
684
return
(
mm
->
alloc
<
ComplexString
>(
sleft
->
d
(),
sright
->
d
()))->
asReturnedValue
();
685
}
686
double
x
=
RuntimeHelpers
::
toNumber
(
pleft
);
687
double
y
=
RuntimeHelpers
::
toNumber
(
pright
);
688
return
Encode
(
x
+
y
);
689
}
690
691
ReturnedValue
Runtime
::
GetTemplateObject
::
call
(
Function
*
function
,
int
index
)
692
{
693
return
function
->
executableCompilationUnit
()->
templateObjectAt
(
index
)->
asReturnedValue
();
694
}
695
696
void
Runtime
::
StoreProperty
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
int
nameIndex
,
const
Value
&
value
)
697
{
698
Scope
scope
(
engine
);
699
QV4
::
Function
*
v4Function
=
engine
->
currentStackFrame
->
v4Function
;
700
ScopedString
name
(
scope
,
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
701
ScopedObject
o
(
scope
,
object
);
702
if
(!
o
) {
703
if
(
v4Function
->
isStrict
()) {
704
engine
->
throwTypeError
();
705
return
;
706
}
707
o
=
object
.
toObject
(
engine
);
708
}
709
if
((!
o
|| !
o
->
put
(
name
,
value
)) &&
v4Function
->
isStrict
())
710
engine
->
throwTypeError
();
711
}
712
713
static
Q_NEVER_INLINE ReturnedValue
getElementIntFallback
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
uint
idx
)
714
{
715
Q_ASSERT
(
idx
<
UINT_MAX
);
716
Scope
scope
(
engine
);
717
718
ScopedObject
o
(
scope
,
object
);
719
if
(!
o
) {
720
if
(
const
String
*
str
=
object
.
as
<
String
>()) {
721
if
(
idx
>= (
uint
)
str
->
toQString
().
size
()) {
722
return
Encode
::
undefined
();
723
}
724
const
QString
s
=
str
->
toQString
().
mid
(
idx
, 1);
725
return
scope
.
engine
->
newString
(
s
)->
asReturnedValue
();
726
}
727
728
if
(
object
.
isNullOrUndefined
()) {
729
QString
message
=
QStringLiteral
(
"Cannot read property '%1' of %2"
).
arg
(
idx
).
arg
(
object
.
toQStringNoThrow
());
730
return
engine
->
throwTypeError
(
message
);
731
}
732
733
o
=
RuntimeHelpers
::
convertToObject
(
scope
.
engine
,
object
);
734
Q_ASSERT
(!!
o
);
// can't fail as null/undefined is covered above
735
}
736
737
if
(
o
->
arrayData
() && !
o
->
arrayData
()->
attrs
) {
738
ScopedValue
v
(
scope
,
o
->
arrayData
()->
get
(
idx
));
739
if
(!
v
->
isEmpty
())
740
return
v
->
asReturnedValue
();
741
}
742
743
return
o
->
get
(
idx
);
744
}
745
746
static
Q_NEVER_INLINE ReturnedValue
getElementFallback
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
const
Value
&
index
)
747
{
748
Q_ASSERT
(!
index
.
isPositiveInt
());
749
750
Scope
scope
(
engine
);
751
752
ScopedObject
o
(
scope
,
object
);
753
if
(!
o
) {
754
if
(
object
.
isNullOrUndefined
()) {
755
const
QString
message
=
756
QStringLiteral
(
"Cannot read property '%1' of %2"
)
757
.
arg
(
index
.
toQStringNoThrow
(),
object
.
toQStringNoThrow
());
758
return
engine
->
throwTypeError
(
message
);
759
}
760
761
o
=
RuntimeHelpers
::
convertToObject
(
scope
.
engine
,
object
);
762
Q_ASSERT
(!!
o
);
// can't fail as null/undefined is covered above
763
}
764
765
ScopedPropertyKey
name
(
scope
,
index
.
toPropertyKey
(
engine
));
766
if
(
scope
.
hasException
())
767
return
Encode
::
undefined
();
768
return
o
->
get
(
name
);
769
}
770
771
ReturnedValue
Runtime
::
LoadElement
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
const
Value
&
index
)
772
{
773
if
(
index
.
isPositiveInt
()) {
774
uint
idx
=
static_cast
<
uint
>(
index
.
int_32
());
775
if
(
Heap
::
Base
*
b
=
object
.
heapObject
()) {
776
if
(
b
->
internalClass
->
vtable
->
isObject
) {
777
Heap
::
Object
*
o
=
static_cast
<
Heap
::
Object
*>(
b
);
778
if
(
o
->
arrayData
&&
o
->
arrayData
->
type
==
Heap
::
ArrayData
::
Simple
) {
779
Heap
::
SimpleArrayData
*
s
=
o
->
arrayData
.
cast
<
Heap
::
SimpleArrayData
>();
780
if
(
idx
<
s
->
values
.
size
)
781
if
(!
s
->
data
(
idx
).
isEmpty
())
782
return
s
->
data
(
idx
).
asReturnedValue
();
783
}
784
}
785
}
786
return
getElementIntFallback
(
engine
,
object
,
idx
);
787
}
788
789
return
getElementFallback
(
engine
,
object
,
index
);
790
}
791
792
static
Q_NEVER_INLINE
bool
setElementFallback
(ExecutionEngine *engine,
const
Value &object,
const
Value &index,
const
Value &value)
793
{
794
Scope
scope(engine);
795
ScopedObject
o(scope, object);
796
if
(!o) {
797
if
(engine->currentStackFrame->v4Function->isStrict()) {
798
engine->throwTypeError();
799
return
false
;
800
}
801
802
o = object.toObject(engine);
803
}
804
if
(engine->hasException)
805
return
false
;
806
807
if
(index.isPositiveInt()) {
808
uint idx =
static_cast
<uint>(index.int_32());
809
if
(o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple) {
810
Heap::SimpleArrayData *s = o->d()->arrayData.cast<Heap::SimpleArrayData>();
811
if
(idx < s->values.size) {
812
s->setData(engine, idx, value);
813
return
true
;
814
}
815
}
816
return
o->put(idx, value);
817
}
818
819
ScopedPropertyKey name(scope, index.toPropertyKey(engine));
820
if
(engine->hasException)
821
return
false
;
822
return
o->put(name, value);
823
}
824
825
void
Runtime
::
StoreElement
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
const
Value
&
index
,
const
Value
&
value
)
826
{
827
if
(
index
.
isPositiveInt
()) {
828
uint
idx
=
static_cast
<
uint
>(
index
.
int_32
());
829
if
(
Heap
::
Base
*
b
=
object
.
heapObject
()) {
830
if
(
b
->
internalClass
->
vtable
->
isObject
) {
831
Heap
::
Object
*
o
=
static_cast
<
Heap
::
Object
*>(
b
);
832
if
(
o
->
arrayData
&&
o
->
arrayData
->
type
==
Heap
::
ArrayData
::
Simple
) {
833
Heap
::
SimpleArrayData
*
s
=
o
->
arrayData
.
cast
<
Heap
::
SimpleArrayData
>();
834
if
(
idx
<
s
->
values
.
size
) {
835
s
->
setData
(
engine
,
idx
,
value
);
836
return
;
837
}
838
}
839
}
840
}
841
}
842
843
if
(!
setElementFallback
(
engine
,
object
,
index
,
value
) &&
engine
->
currentStackFrame
->
v4Function
->
isStrict
())
844
engine
->
throwTypeError
();
845
}
846
847
ReturnedValue
Runtime
::
GetIterator
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
in
,
int
iterator
)
848
{
849
Scope
scope
(
engine
);
850
ScopedObject
o
(
scope
, (
Object
*)
nullptr
);
851
if
(!
in
.
isNullOrUndefined
())
852
o
=
in
.
toObject
(
engine
);
853
if
(
engine
->
hasException
)
854
return
Encode
::
undefined
();
855
if
(
iterator
==
static_cast
<
int
>(
QQmlJS
::
AST
::
ForEachType
::
Of
)) {
856
if
(!
o
)
857
return
engine
->
throwTypeError
();
858
ScopedFunctionObject
f
(
scope
,
o
->
get
(
engine
->
symbol_iterator
()));
859
if
(!
f
)
860
return
engine
->
throwTypeError
();
861
JSCallData
cData
(
o
,
nullptr
, 0);
862
ScopedObject
it
(
scope
,
f
->
call
(
cData
));
863
if
(
engine
->
hasException
)
864
return
Encode
::
undefined
();
865
if
(!
it
)
866
return
engine
->
throwTypeError
();
867
return
it
->
asReturnedValue
();
868
}
869
return
engine
->
newForInIteratorObject
(
o
)->
asReturnedValue
();
870
}
871
872
ReturnedValue
Runtime
::
IteratorNext
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
iterator
,
Value
*
value
)
873
{
874
// if we throw an exception from here, return true, not undefined. This is to ensure iteratorDone is set to true
875
// and the stack unwinding won't close the iterator
876
Q_ASSERT
(
iterator
.
isObject
());
877
878
Scope
scope
(
engine
);
879
ScopedFunctionObject
f
(
scope
,
static_cast
<
const
Object
&>(
iterator
).
get
(
engine
->
id_next
()));
880
if
(!
f
) {
881
engine
->
throwTypeError
();
882
return
Encode
(
true
);
883
}
884
JSCallData
cData
(&
iterator
,
nullptr
, 0);
885
ScopedObject
o
(
scope
,
f
->
call
(
cData
));
886
if
(
scope
.
hasException
())
887
return
Encode
(
true
);
888
if
(!
o
) {
889
engine
->
throwTypeError
();
890
return
Encode
(
true
);
891
}
892
893
ScopedValue
d
(
scope
,
o
->
get
(
engine
->
id_done
()));
894
if
(
scope
.
hasException
())
895
return
Encode
(
true
);
896
bool
done
=
d
->
toBoolean
();
897
if
(
done
) {
898
*
value
=
Encode
::
undefined
();
899
return
Encode
(
true
);
900
}
901
902
*
value
=
o
->
get
(
engine
->
id_value
());
903
if
(
scope
.
hasException
())
904
return
Encode
(
true
);
905
return
Encode
(
false
);
906
}
907
908
ReturnedValue
Runtime
::
IteratorNextForYieldStar
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
received
,
const
Value
&
iterator
,
Value
*
object
)
909
{
910
// the return value encodes how to continue the yield* iteration.
911
// true implies iteration is done, false for iteration to continue
912
// a return value of undefines is a special marker, that the iterator has been invoked with return()
913
914
Scope
scope
(
engine
);
915
Q_ASSERT
(
iterator
.
isObject
());
916
917
const
Value
*
arg
= &
received
;
918
bool
returnCalled
=
false
;
919
FunctionObject
*
f
=
nullptr
;
920
if
(
engine
->
hasException
) {
921
if
(
engine
->
exceptionValue
->
isEmpty
()) {
922
// generator called with return()
923
*
engine
->
exceptionValue
=
Encode
::
undefined
();
924
engine
->
hasException
=
false
;
925
926
ScopedValue
ret
(
scope
,
static_cast
<
const
Object
&>(
iterator
).
get
(
engine
->
id_return
()));
927
if
(
engine
->
hasException
)
928
return
Encode
(
true
);
929
if
(
ret
->
isUndefined
()) {
930
// propagate return()
931
return
Encode
::
undefined
();
932
}
933
returnCalled
=
true
;
934
f
=
ret
->
as
<
FunctionObject
>();
935
}
else
{
936
// generator called with throw
937
ScopedValue
exceptionValue
(
scope
, *
engine
->
exceptionValue
);
938
*
engine
->
exceptionValue
=
Encode
::
undefined
();
939
engine
->
hasException
=
false
;
940
941
ScopedValue
t
(
scope
,
static_cast
<
const
Object
&>(
iterator
).
get
(
engine
->
id_throw
()));
942
if
(
engine
->
hasException
)
943
return
Encode
(
true
);
944
if
(
t
->
isUndefined
()) {
945
// no throw method on the iterator
946
IteratorClose
::
call
(
engine
,
iterator
);
947
if
(!
engine
->
hasException
)
948
engine
->
throwTypeError
();
949
return
Encode
(
true
);
950
}
951
f
=
t
->
as
<
FunctionObject
>();
952
arg
=
exceptionValue
;
953
}
954
}
else
{
955
// generator called with next()
956
ScopedFunctionObject
next
(
scope
,
static_cast
<
const
Object
&>(
iterator
).
get
(
engine
->
id_next
()));
957
f
=
next
->
as
<
FunctionObject
>();
958
}
959
960
if
(!
f
) {
961
engine
->
throwTypeError
();
962
return
Encode
(
true
);
963
}
964
965
ScopedObject
o
(
scope
,
f
->
call
(&
iterator
,
arg
, 1));
966
if
(
scope
.
hasException
())
967
return
Encode
(
true
);
968
if
(!
o
) {
969
engine
->
throwTypeError
();
970
return
Encode
(
true
);
971
}
972
973
ScopedValue
d
(
scope
,
o
->
get
(
engine
->
id_done
()));
974
if
(
scope
.
hasException
())
975
return
Encode
(
true
);
976
bool
done
=
d
->
toBoolean
();
977
if
(
done
) {
978
*
object
=
o
->
get
(
engine
->
id_value
());
979
return
(
returnCalled
&& !
engine
->
hasException
) ?
Encode
::
undefined
() :
Encode
(
true
);
980
}
981
*
object
=
o
;
982
return
Encode
(
false
);
983
}
984
985
ReturnedValue
Runtime
::
IteratorClose
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
iterator
)
986
{
987
Q_ASSERT
(
iterator
.
isObject
());
988
989
Scope
scope
(
engine
);
990
ScopedValue
e
(
scope
);
991
bool
hadException
=
engine
->
hasException
;
992
if
(
hadException
) {
993
e
= *
engine
->
exceptionValue
;
994
engine
->
hasException
=
false
;
995
}
996
997
auto
originalCompletion
= [=]() {
998
if
(
hadException
) {
999
*
engine
->
exceptionValue
=
e
;
1000
engine
->
hasException
=
hadException
;
1001
}
1002
return
Encode
::
undefined
();
1003
};
1004
1005
ScopedValue
ret
(
scope
,
static_cast
<
const
Object
&>(
iterator
).
get
(
engine
->
id_return
()));
1006
ScopedObject
o
(
scope
);
1007
if
(!
ret
->
isUndefined
()) {
1008
FunctionObject
*
f
=
ret
->
as
<
FunctionObject
>();
1009
o
=
f
->
call
(&
iterator
,
nullptr
, 0);
1010
if
(
engine
->
hasException
&& !
hadException
)
1011
return
Encode
::
undefined
();
1012
}
1013
if
(
hadException
||
ret
->
isUndefined
())
1014
return
originalCompletion
();
1015
1016
if
(!
o
)
1017
return
engine
->
throwTypeError
();
1018
1019
return
originalCompletion
();
1020
}
1021
1022
ReturnedValue
Runtime
::
DestructureRestElement
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
iterator
)
1023
{
1024
Q_ASSERT
(
iterator
.
isObject
());
1025
1026
Scope
scope
(
engine
);
1027
ScopedArrayObject
array
(
scope
,
engine
->
newArrayObject
());
1028
array
->
arrayCreate
();
1029
uint
index
= 0;
1030
ScopedValue
n
(
scope
);
1031
ScopedValue
done
(
scope
);
1032
while
(1) {
1033
done
=
IteratorNext
::
call
(
engine
,
iterator
,
n
);
1034
if
(
engine
->
hasException
)
1035
return
Encode
::
undefined
();
1036
Q_ASSERT
(
done
->
isBoolean
());
1037
if
(
done
->
booleanValue
())
1038
break
;
1039
array
->
arraySet
(
index
,
n
);
1040
++
index
;
1041
}
1042
return
array
->
asReturnedValue
();
1043
}
1044
1045
void
Runtime
::
StoreNameSloppy
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
,
const
Value
&
value
)
1046
{
1047
Scope
scope
(
engine
);
1048
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1049
ExecutionContext
::
Error
e
=
engine
->
currentContext
()->
setProperty
(
name
,
value
);
1050
1051
if
(
e
==
ExecutionContext
::
RangeError
)
1052
engine
->
globalObject
->
put
(
name
,
value
);
1053
}
1054
1055
void
Runtime
::
StoreNameStrict
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
,
const
Value
&
value
)
1056
{
1057
Scope
scope
(
engine
);
1058
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1059
ExecutionContext
::
Error
e
=
engine
->
currentContext
()->
setProperty
(
name
,
value
);
1060
if
(
e
==
ExecutionContext
::
TypeError
)
1061
engine
->
throwTypeError
();
1062
else
if
(
e
==
ExecutionContext
::
RangeError
)
1063
engine
->
throwReferenceError
(
name
);
1064
}
1065
1066
ReturnedValue
Runtime
::
LoadProperty
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
object
,
int
nameIndex
)
1067
{
1068
Scope
scope
(
engine
);
1069
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1070
1071
ScopedObject
o
(
scope
,
object
);
1072
if
(
o
)
1073
return
o
->
get
(
name
);
1074
1075
if
(
object
.
isNullOrUndefined
()) {
1076
const
QString
message
=
QStringLiteral
(
"Cannot read property '%1' of %2"
)
1077
.
arg
(
name
->
toQString
(),
object
.
toQStringNoThrow
());
1078
return
engine
->
throwTypeError
(
message
);
1079
}
1080
1081
o
=
RuntimeHelpers
::
convertToObject
(
scope
.
engine
,
object
);
1082
if
(!
o
)
// type error
1083
return
Encode
::
undefined
();
1084
return
o
->
get
(
name
);
1085
}
1086
1087
ReturnedValue
Runtime
::
LoadName
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
)
1088
{
1089
Scope
scope
(
engine
);
1090
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1091
return
engine
->
currentContext
()->
getProperty
(
name
);
1092
}
1093
1094
static
Object *
getSuperBase
(
Scope
&scope)
1095
{
1096
Scoped<JavaScriptFunctionObject> f(scope);
1097
ScopedObject
homeObject(scope);
1098
if
(scope.engine->currentStackFrame->isJSTypesFrame()) {
1099
JSTypesStackFrame *frame =
static_cast
<JSTypesStackFrame *>(
1100
scope.engine->currentStackFrame);
1101
1102
if
(frame->jsFrame->thisObject.isEmpty()) {
1103
scope.engine->throwReferenceError(
1104
QStringLiteral(
"Missing call to super()."
), QString(), 0, 0);
1105
return
nullptr
;
1106
}
1107
1108
f = Value::fromStaticValue(frame->jsFrame->function);
1109
homeObject = f->getHomeObject();
1110
}
else
{
1111
Q_ASSERT(scope.engine->currentStackFrame->isMetaTypesFrame());
1112
MetaTypesStackFrame *frame =
static_cast
<MetaTypesStackFrame *>(
1113
scope.engine->currentStackFrame);
1114
if
(frame->thisObject() ==
nullptr
) {
1115
scope.engine->throwReferenceError(
1116
QStringLiteral(
"Missing call to super()."
), QString(), 0, 0);
1117
return
nullptr
;
1118
}
1119
}
1120
1121
if
(!homeObject) {
1122
ScopedContext
ctx(scope, scope.engine->currentContext());
1123
Q_ASSERT(ctx);
1124
while
(ctx) {
1125
if
(CallContext *c = ctx->asCallContext()) {
1126
f = c->d()->function;
1127
QV4
::Function *fn = f->function();
1128
if
(fn && !fn->isArrowFunction() && fn->kind != Function::Eval)
1129
break
;
1130
}
1131
ctx = ctx->d()->outer;
1132
}
1133
if
(f)
1134
homeObject = f->getHomeObject();
1135
}
1136
if
(!homeObject) {
1137
scope.engine->throwTypeError();
1138
return
nullptr
;
1139
}
1140
Q_ASSERT(homeObject);
1141
ScopedObject
proto(scope, homeObject->getPrototypeOf());
1142
if
(!proto) {
1143
scope.engine->throwTypeError();
1144
return
nullptr
;
1145
}
1146
return
proto;
1147
}
1148
1149
ReturnedValue
Runtime
::
LoadSuperProperty
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
property
)
1150
{
1151
Scope
scope
(
engine
);
1152
Object
*
base
=
getSuperBase
(
scope
);
1153
if
(!
base
)
1154
return
Encode
::
undefined
();
1155
ScopedPropertyKey
key
(
scope
,
property
.
toPropertyKey
(
engine
));
1156
if
(
engine
->
hasException
)
1157
return
Encode
::
undefined
();
1158
1159
if
(
scope
.
engine
->
currentStackFrame
->
isJSTypesFrame
()) {
1160
JSTypesStackFrame
*
frame
=
static_cast
<
JSTypesStackFrame
*>(
1161
scope
.
engine
->
currentStackFrame
);
1162
return
base
->
get
(
key
, &(
frame
->
jsFrame
->
thisObject
.
asValue
<
Value
>()));
1163
}
else
{
1164
Q_ASSERT
(
scope
.
engine
->
currentStackFrame
->
isMetaTypesFrame
());
1165
MetaTypesStackFrame
*
frame
=
static_cast
<
MetaTypesStackFrame
*>(
1166
scope
.
engine
->
currentStackFrame
);
1167
Scoped
<
QObjectWrapper
>
wrapper
(
scope
,
QObjectWrapper
::
wrap
(
engine
,
frame
->
thisObject
()));
1168
return
base
->
get
(
key
,
wrapper
);
1169
}
1170
}
1171
1172
void
Runtime
::
StoreSuperProperty
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
property
,
const
Value
&
value
)
1173
{
1174
Scope
scope
(
engine
);
1175
Object
*
base
=
getSuperBase
(
scope
);
1176
if
(!
base
)
1177
return
;
1178
ScopedPropertyKey
key
(
scope
,
property
.
toPropertyKey
(
engine
));
1179
if
(
engine
->
hasException
)
1180
return
;
1181
1182
bool
result
;
1183
if
(
scope
.
engine
->
currentStackFrame
->
isJSTypesFrame
()) {
1184
JSTypesStackFrame
*
frame
=
static_cast
<
JSTypesStackFrame
*>(
1185
scope
.
engine
->
currentStackFrame
);
1186
result
=
base
->
put
(
key
,
value
, &(
frame
->
jsFrame
->
thisObject
.
asValue
<
Value
>()));
1187
}
else
{
1188
Q_ASSERT
(
scope
.
engine
->
currentStackFrame
->
isMetaTypesFrame
());
1189
MetaTypesStackFrame
*
frame
=
static_cast
<
MetaTypesStackFrame
*>(
1190
scope
.
engine
->
currentStackFrame
);
1191
Scoped
<
QObjectWrapper
>
wrapper
(
scope
,
QObjectWrapper
::
wrap
(
engine
,
frame
->
thisObject
()));
1192
result
=
base
->
put
(
key
,
value
,
wrapper
);
1193
}
1194
1195
if
(!
result
&&
engine
->
currentStackFrame
->
v4Function
->
isStrict
())
1196
engine
->
throwTypeError
();
1197
}
1198
1199
ReturnedValue
Runtime
::
LoadGlobalLookup
::
call
(
ExecutionEngine
*
engine
,
Function
*
f
,
int
index
)
1200
{
1201
Lookup
*
l
=
runtimeLookup
(
f
,
index
);
1202
return
l
->
globalGetter
(
engine
);
1203
}
1204
1205
ReturnedValue
Runtime
::
LoadQmlContextPropertyLookup
::
call
(
ExecutionEngine
*
engine
,
uint
index
)
1206
{
1207
Lookup
*
l
=
runtimeLookup
(
engine
->
currentStackFrame
->
v4Function
,
index
);
1208
return
l
->
contextGetter
(
engine
,
nullptr
);
1209
}
1210
1211
ReturnedValue
Runtime
::
GetLookup
::
call
(
ExecutionEngine
*
engine
,
Function
*
f
,
const
Value
&
base
,
int
index
)
1212
{
1213
Lookup
*
l
=
runtimeLookup
(
f
,
index
);
1214
1215
if
(
base
.
isNullOrUndefined
()) {
1216
const
QString
message
=
QStringLiteral
(
"Cannot read property '%1' of %2"
)
1217
.
arg
(
f
->
compilationUnit
->
runtimeStrings
[
l
->
nameIndex
]->
toQString
(),
1218
base
.
toQStringNoThrow
());
1219
return
engine
->
throwTypeError
(
message
);
1220
}
1221
1222
return
l
->
getter
(
engine
,
base
);
1223
}
1224
1225
void
Runtime
::
SetLookupSloppy
::
call
(
Function
*
f
,
const
Value
&
base
,
int
index
,
const
Value
&
value
)
1226
{
1227
ExecutionEngine
*
engine
=
f
->
internalClass
->
engine
;
1228
QV4
::
Lookup
*
l
=
runtimeLookup
(
f
,
index
);
1229
l
->
setter
(
engine
,
const_cast
<
Value
&>(
base
),
value
);
1230
}
1231
1232
void
Runtime
::
SetLookupStrict
::
call
(
Function
*
f
,
const
Value
&
base
,
int
index
,
const
Value
&
value
)
1233
{
1234
ExecutionEngine
*
engine
=
f
->
internalClass
->
engine
;
1235
QV4
::
Lookup
*
l
=
runtimeLookup
(
f
,
index
);
1236
if
(!
l
->
setter
(
engine
,
const_cast
<
Value
&>(
base
),
value
))
1237
engine
->
throwTypeError
();
1238
}
1239
1240
ReturnedValue
Runtime
::
LoadSuperConstructor
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
t
)
1241
{
1242
if
(
engine
->
currentStackFrame
->
isJSTypesFrame
()) {
1243
JSTypesStackFrame
*
frame
=
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
);
1244
if
(
frame
->
thisObject
() !=
Value
::
emptyValue
().
asReturnedValue
()) {
1245
// ### TODO: fix line number
1246
return
engine
->
throwReferenceError
(
1247
QStringLiteral
(
"super() already called."
),
QString
(), 0, 0);
1248
}
1249
}
else
{
1250
Q_ASSERT
(
engine
->
currentStackFrame
->
isMetaTypesFrame
());
1251
MetaTypesStackFrame
*
frame
=
static_cast
<
MetaTypesStackFrame
*>(
engine
->
currentStackFrame
);
1252
if
(
frame
->
thisObject
() !=
nullptr
) {
1253
// ### TODO: fix line number
1254
return
engine
->
throwReferenceError
(
1255
QStringLiteral
(
"super() already called."
),
QString
(), 0, 0);
1256
}
1257
}
1258
1259
const
FunctionObject
*
f
=
t
.
as
<
FunctionObject
>();
1260
if
(!
f
)
1261
return
engine
->
throwTypeError
();
1262
Heap
::
Object
*
c
=
static_cast
<
const
Object
&>(
t
).
getPrototypeOf
();
1263
if
(!
c
->
vtable
()->
callAsConstructor
)
1264
return
engine
->
throwTypeError
();
1265
return
c
->
asReturnedValue
();
1266
}
1267
1268
uint
RuntimeHelpers
::
equalHelper
(
const
Value
&
x
,
const
Value
&
y
)
1269
{
1270
Q_ASSERT
(
x
.
type
() !=
y
.
type
() || (
x
.
isManaged
() && (
x
.
isString
() !=
y
.
isString
())));
1271
1272
if
(
x
.
isNumber
() &&
y
.
isNumber
())
1273
return
x
.
asDouble
() ==
y
.
asDouble
();
1274
if
(
x
.
isNull
() &&
y
.
isUndefined
()) {
1275
return
true
;
1276
}
else
if
(
x
.
isUndefined
() &&
y
.
isNull
()) {
1277
return
true
;
1278
}
else
if
(
x
.
isNumber
() &&
y
.
isString
()) {
1279
double
dy
=
RuntimeHelpers
::
toNumber
(
y
);
1280
return
x
.
asDouble
() ==
dy
;
1281
}
else
if
(
x
.
isString
() &&
y
.
isNumber
()) {
1282
double
dx
=
RuntimeHelpers
::
toNumber
(
x
);
1283
return
dx
==
y
.
asDouble
();
1284
}
else
if
(
x
.
isBoolean
()) {
1285
return
Runtime
::
CompareEqual
::
call
(
Value
::
fromDouble
((
double
)
x
.
booleanValue
()),
y
);
1286
}
else
if
(
y
.
isBoolean
()) {
1287
return
Runtime
::
CompareEqual
::
call
(
x
,
Value
::
fromDouble
((
double
)
y
.
booleanValue
()));
1288
}
else
{
1289
Object
*
xo
=
x
.
objectValue
();
1290
Object
*
yo
=
y
.
objectValue
();
1291
if
(
yo
&& (
x
.
isNumber
() ||
x
.
isString
())) {
1292
Scope
scope
(
yo
->
engine
());
1293
ScopedValue
py
(
scope
,
RuntimeHelpers
::
objectDefaultValue
(
yo
,
PREFERREDTYPE_HINT
));
1294
return
Runtime
::
CompareEqual
::
call
(
x
,
py
);
1295
}
else
if
(
xo
&& (
y
.
isNumber
() ||
y
.
isString
())) {
1296
Scope
scope
(
xo
->
engine
());
1297
ScopedValue
px
(
scope
,
RuntimeHelpers
::
objectDefaultValue
(
xo
,
PREFERREDTYPE_HINT
));
1298
return
Runtime
::
CompareEqual
::
call
(
px
,
y
);
1299
}
1300
}
1301
1302
return
false
;
1303
}
1304
1305
Bool
RuntimeHelpers
::
strictEqual
(
const
Value
&
x
,
const
Value
&
y
)
1306
{
1307
TRACE2
(x, y);
1308
1309
if
(
x
.
rawValue
() ==
y
.
rawValue
())
1310
// NaN != NaN
1311
return
!
x
.
isNaN
();
1312
1313
if
(
x
.
isNumber
())
1314
return
y
.
isNumber
() &&
x
.
asDouble
() ==
y
.
asDouble
();
1315
if
(
x
.
isManaged
()) {
1316
return
y
.
isManaged
() &&
x
.
cast
<
Managed
>()->
isEqualTo
(
y
.
cast
<
Managed
>());
1317
}
1318
return
false
;
1319
}
1320
1321
QV4
::
Bool
Runtime
::
CompareGreaterThan
::
call
(
const
Value
&
l
,
const
Value
&
r
)
1322
{
1323
TRACE2
(l, r);
1324
if
(
l
.
isInteger
() &&
r
.
isInteger
())
1325
return
l
.
integerValue
() >
r
.
integerValue
();
1326
if
(
l
.
isNumber
() &&
r
.
isNumber
())
1327
return
l
.
asDouble
() >
r
.
asDouble
();
1328
String
*
sl
=
l
.
stringValue
();
1329
String
*
sr
=
r
.
stringValue
();
1330
if
(
sl
&&
sr
) {
1331
return
sr
->
lessThan
(
sl
);
1332
}
1333
1334
Object
*
ro
=
r
.
objectValue
();
1335
Object
*
lo
=
l
.
objectValue
();
1336
if
(
ro
||
lo
) {
1337
QV4
::
ExecutionEngine
*
e
= (
lo
?
lo
:
ro
)->
engine
();
1338
QV4
::
Scope
scope
(
e
);
1339
QV4
::
ScopedValue
pl
(
scope
,
lo
?
RuntimeHelpers
::
objectDefaultValue
(
lo
,
QV4
::
NUMBER_HINT
) :
l
.
asReturnedValue
());
1340
QV4
::
ScopedValue
pr
(
scope
,
ro
?
RuntimeHelpers
::
objectDefaultValue
(
ro
,
QV4
::
NUMBER_HINT
) :
r
.
asReturnedValue
());
1341
return
Runtime
::
CompareGreaterThan
::
call
(
pl
,
pr
);
1342
}
1343
1344
double
dl
=
RuntimeHelpers
::
toNumber
(
l
);
1345
double
dr
=
RuntimeHelpers
::
toNumber
(
r
);
1346
return
dl
>
dr
;
1347
}
1348
1349
QV4
::
Bool
Runtime
::
CompareLessThan
::
call
(
const
Value
&
l
,
const
Value
&
r
)
1350
{
1351
TRACE2
(l, r);
1352
if
(
l
.
isInteger
() &&
r
.
isInteger
())
1353
return
l
.
integerValue
() <
r
.
integerValue
();
1354
if
(
l
.
isNumber
() &&
r
.
isNumber
())
1355
return
l
.
asDouble
() <
r
.
asDouble
();
1356
String
*
sl
=
l
.
stringValue
();
1357
String
*
sr
=
r
.
stringValue
();
1358
if
(
sl
&&
sr
) {
1359
return
sl
->
lessThan
(
sr
);
1360
}
1361
1362
Object
*
ro
=
r
.
objectValue
();
1363
Object
*
lo
=
l
.
objectValue
();
1364
if
(
ro
||
lo
) {
1365
QV4
::
ExecutionEngine
*
e
= (
lo
?
lo
:
ro
)->
engine
();
1366
QV4
::
Scope
scope
(
e
);
1367
QV4
::
ScopedValue
pl
(
scope
,
lo
?
RuntimeHelpers
::
objectDefaultValue
(
lo
,
QV4
::
NUMBER_HINT
) :
l
.
asReturnedValue
());
1368
QV4
::
ScopedValue
pr
(
scope
,
ro
?
RuntimeHelpers
::
objectDefaultValue
(
ro
,
QV4
::
NUMBER_HINT
) :
r
.
asReturnedValue
());
1369
return
Runtime
::
CompareLessThan
::
call
(
pl
,
pr
);
1370
}
1371
1372
double
dl
=
RuntimeHelpers
::
toNumber
(
l
);
1373
double
dr
=
RuntimeHelpers
::
toNumber
(
r
);
1374
return
dl
<
dr
;
1375
}
1376
1377
QV4
::
Bool
Runtime
::
CompareGreaterEqual
::
call
(
const
Value
&
l
,
const
Value
&
r
)
1378
{
1379
TRACE2
(l, r);
1380
if
(
l
.
isInteger
() &&
r
.
isInteger
())
1381
return
l
.
integerValue
() >=
r
.
integerValue
();
1382
if
(
l
.
isNumber
() &&
r
.
isNumber
())
1383
return
l
.
asDouble
() >=
r
.
asDouble
();
1384
String
*
sl
=
l
.
stringValue
();
1385
String
*
sr
=
r
.
stringValue
();
1386
if
(
sl
&&
sr
) {
1387
return
!
sl
->
lessThan
(
sr
);
1388
}
1389
1390
Object
*
ro
=
r
.
objectValue
();
1391
Object
*
lo
=
l
.
objectValue
();
1392
if
(
ro
||
lo
) {
1393
QV4
::
ExecutionEngine
*
e
= (
lo
?
lo
:
ro
)->
engine
();
1394
QV4
::
Scope
scope
(
e
);
1395
QV4
::
ScopedValue
pl
(
scope
,
lo
?
RuntimeHelpers
::
objectDefaultValue
(
lo
,
QV4
::
NUMBER_HINT
) :
l
.
asReturnedValue
());
1396
QV4
::
ScopedValue
pr
(
scope
,
ro
?
RuntimeHelpers
::
objectDefaultValue
(
ro
,
QV4
::
NUMBER_HINT
) :
r
.
asReturnedValue
());
1397
return
Runtime
::
CompareGreaterEqual
::
call
(
pl
,
pr
);
1398
}
1399
1400
double
dl
=
RuntimeHelpers
::
toNumber
(
l
);
1401
double
dr
=
RuntimeHelpers
::
toNumber
(
r
);
1402
return
dl
>=
dr
;
1403
}
1404
1405
QV4
::
Bool
Runtime
::
CompareLessEqual
::
call
(
const
Value
&
l
,
const
Value
&
r
)
1406
{
1407
TRACE2
(l, r);
1408
if
(
l
.
isInteger
() &&
r
.
isInteger
())
1409
return
l
.
integerValue
() <=
r
.
integerValue
();
1410
if
(
l
.
isNumber
() &&
r
.
isNumber
())
1411
return
l
.
asDouble
() <=
r
.
asDouble
();
1412
String
*
sl
=
l
.
stringValue
();
1413
String
*
sr
=
r
.
stringValue
();
1414
if
(
sl
&&
sr
) {
1415
return
!
sr
->
lessThan
(
sl
);
1416
}
1417
1418
Object
*
ro
=
r
.
objectValue
();
1419
Object
*
lo
=
l
.
objectValue
();
1420
if
(
ro
||
lo
) {
1421
QV4
::
ExecutionEngine
*
e
= (
lo
?
lo
:
ro
)->
engine
();
1422
QV4
::
Scope
scope
(
e
);
1423
QV4
::
ScopedValue
pl
(
scope
,
lo
?
RuntimeHelpers
::
objectDefaultValue
(
lo
,
QV4
::
NUMBER_HINT
) :
l
.
asReturnedValue
());
1424
QV4
::
ScopedValue
pr
(
scope
,
ro
?
RuntimeHelpers
::
objectDefaultValue
(
ro
,
QV4
::
NUMBER_HINT
) :
r
.
asReturnedValue
());
1425
return
Runtime
::
CompareLessEqual
::
call
(
pl
,
pr
);
1426
}
1427
1428
double
dl
=
RuntimeHelpers
::
toNumber
(
l
);
1429
double
dr
=
RuntimeHelpers
::
toNumber
(
r
);
1430
return
dl
<=
dr
;
1431
}
1432
1433
Bool
Runtime
::
CompareInstanceof
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
left
,
const
Value
&
right
)
1434
{
1435
TRACE2
(left, right);
1436
1437
Scope
scope
(
engine
);
1438
ScopedValue
v
(
scope
,
Instanceof
::
call
(
engine
,
left
,
right
));
1439
return
v
->
booleanValue
();
1440
}
1441
1442
uint
Runtime
::
CompareIn
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
left
,
const
Value
&
right
)
1443
{
1444
TRACE2
(left, right);
1445
1446
Scope
scope
(
engine
);
1447
ScopedValue
v
(
scope
,
In
::
call
(
engine
,
left
,
right
));
1448
return
v
->
booleanValue
();
1449
}
1450
1451
static
ReturnedValue
throwPropertyIsNotAFunctionTypeError
(ExecutionEngine *engine, Value *thisObject,
const
QString &propertyName)
1452
{
1453
QString objectAsString = QStringLiteral(
"[null]"
);
1454
if
(!thisObject->isUndefined())
1455
objectAsString = thisObject->toQStringNoThrow();
1456
QString msg = QStringLiteral(
"Property '%1' of object %2 is not a function"
)
1457
.arg(propertyName, objectAsString);
1458
return
engine->throwTypeError(msg);
1459
}
1460
1461
ReturnedValue
Runtime
::
CallGlobalLookup
::
call
(
ExecutionEngine
*
engine
,
uint
index
,
Value
argv
[],
int
argc
)
1462
{
1463
Scope
scope
(
engine
);
1464
Lookup
*
l
=
runtimeLookup
(
engine
->
currentStackFrame
->
v4Function
,
index
);
1465
Value
function
=
Value
::
fromReturnedValue
(
l
->
globalGetter
(
engine
));
1466
Value
thisObject
=
Value
::
undefinedValue
();
1467
if
(!
function
.
isFunctionObject
()) {
1468
return
throwPropertyIsNotAFunctionTypeError
(
engine
, &
thisObject
,
1469
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
l
->
nameIndex
]->
toQString
());
1470
}
1471
1472
return
checkedResult
(
engine
,
static_cast
<
FunctionObject
&>(
function
).
call
(
1473
&
thisObject
,
argv
,
argc
));
1474
}
1475
1476
ReturnedValue
Runtime
::
CallQmlContextPropertyLookup
::
call
(
ExecutionEngine
*
engine
,
uint
index
,
1477
Value
*
argv
,
int
argc
)
1478
{
1479
Scope
scope
(
engine
);
1480
ScopedValue
thisObject
(
scope
);
1481
Lookup
*
l
=
runtimeLookup
(
engine
->
currentStackFrame
->
v4Function
,
index
);
1482
Value
function
=
Value
::
fromReturnedValue
(
l
->
contextGetter
(
engine
,
thisObject
));
1483
if
(!
function
.
isFunctionObject
()) {
1484
return
throwPropertyIsNotAFunctionTypeError
(
engine
,
thisObject
,
1485
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
l
->
nameIndex
]->
toQString
());
1486
}
1487
1488
return
checkedResult
(
engine
,
static_cast
<
FunctionObject
&>(
function
).
call
(
1489
thisObject
,
argv
,
argc
));
1490
}
1491
1492
ReturnedValue
Runtime
::
CallPossiblyDirectEval
::
call
(
ExecutionEngine
*
engine
,
Value
*
argv
,
int
argc
)
1493
{
1494
Scope
scope
(
engine
);
1495
ScopedValue
thisObject
(
scope
);
1496
1497
ScopedFunctionObject
function
(
1498
scope
,
engine
->
currentContext
()->
getPropertyAndBase
(
engine
->
id_eval
(),
thisObject
));
1499
if
(
engine
->
hasException
)
1500
return
Encode
::
undefined
();
1501
1502
if
(!
function
)
1503
return
throwPropertyIsNotAFunctionTypeError
(
engine
,
thisObject
,
QLatin1String
(
"eval"
));
1504
1505
if
(
function
->
d
() ==
engine
->
evalFunction
()->
d
())
1506
return
static_cast
<
EvalFunction
*>(
function
.
getPointer
())->
evalCall
(
thisObject
,
argv
,
argc
,
true
);
1507
1508
return
checkedResult
(
engine
,
function
->
call
(
thisObject
,
argv
,
argc
));
1509
}
1510
1511
ReturnedValue
Runtime
::
CallName
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
,
Value
*
argv
,
int
argc
)
1512
{
1513
Scope
scope
(
engine
);
1514
ScopedValue
thisObject
(
scope
);
1515
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1516
1517
ScopedFunctionObject
f
(
scope
,
engine
->
currentContext
()->
getPropertyAndBase
(
name
,
thisObject
));
1518
if
(
engine
->
hasException
)
1519
return
Encode
::
undefined
();
1520
1521
if
(!
f
) {
1522
return
throwPropertyIsNotAFunctionTypeError
(
1523
engine
,
thisObject
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
1524
->
runtimeStrings
[
nameIndex
]->
toQString
());
1525
}
1526
1527
return
checkedResult
(
engine
,
f
->
call
(
thisObject
,
argv
,
argc
));
1528
}
1529
1530
ReturnedValue
Runtime
::
CallProperty
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
baseRef
,
int
nameIndex
,
Value
*
argv
,
int
argc
)
1531
{
1532
const
Value
*
base
= &
baseRef
;
1533
Scope
scope
(
engine
);
1534
ScopedString
name
(
1535
scope
,
1536
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1537
ScopedObject
lookupObject
(
scope
,
base
);
1538
1539
if
(!
lookupObject
) {
1540
Q_ASSERT
(!
base
->
isEmpty
());
1541
if
(
base
->
isNullOrUndefined
()) {
1542
QString
message
=
QStringLiteral
(
"Cannot call method '%1' of %2"
)
1543
.
arg
(
name
->
toQString
(),
base
->
toQStringNoThrow
());
1544
return
engine
->
throwTypeError
(
message
);
1545
}
1546
1547
if
(
base
->
isManaged
()) {
1548
const
Managed
*
m
=
static_cast
<
const
Managed
*>(
base
);
1549
lookupObject
=
m
->
internalClass
()->
prototype
;
1550
Q_ASSERT
(
m
->
internalClass
()->
prototype
);
1551
}
else
{
1552
lookupObject
=
RuntimeHelpers
::
convertToObject
(
engine
, *
base
);
1553
if
(
engine
->
hasException
)
// type error
1554
return
Encode
::
undefined
();
1555
if
(!
engine
->
currentStackFrame
->
v4Function
->
isStrict
())
1556
base
=
lookupObject
;
1557
}
1558
}
1559
1560
ScopedFunctionObject
f
(
scope
,
static_cast
<
Object
*>(
lookupObject
)->
get
(
name
));
1561
1562
if
(!
f
) {
1563
QString
error
=
QStringLiteral
(
"Property '%1' of object %2 is not a function"
)
1564
.
arg
(
name
->
toQString
(),
1565
base
->
toQStringNoThrow
());
1566
return
engine
->
throwTypeError
(
error
);
1567
}
1568
1569
return
checkedResult
(
engine
,
f
->
call
(
base
,
argv
,
argc
));
1570
}
1571
1572
ReturnedValue
Runtime
::
CallPropertyLookup
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
base
,
uint
index
,
Value
*
argv
,
int
argc
)
1573
{
1574
Lookup
*
l
=
runtimeLookup
(
engine
->
currentStackFrame
->
v4Function
,
index
);
1575
1576
if
(
base
.
isNullOrUndefined
()) {
1577
const
QString
message
=
QStringLiteral
(
"Cannot call method '%1' of %2"
)
1578
.
arg
(
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
1579
->
runtimeStrings
[
l
->
nameIndex
]->
toQString
(),
1580
base
.
toQStringNoThrow
());
1581
return
engine
->
throwTypeError
(
message
);
1582
}
1583
1584
// ok to have the value on the stack here
1585
Value
f
=
Value
::
fromReturnedValue
(
l
->
getter
(
engine
,
base
));
1586
1587
if
(
Q_LIKELY
(
f
.
isFunctionObject
()))
1588
return
checkedResult
(
engine
,
static_cast
<
FunctionObject
&>(
f
).
call
(&
base
,
argv
,
argc
));
1589
1590
if
(
QmlSignalHandler
*
handler
=
f
.
as
<
QmlSignalHandler
>())
1591
return
checkedResult
(
engine
,
handler
->
call
(&
base
,
argv
,
argc
));
1592
1593
const
QString
message
=
QStringLiteral
(
"Property '%1' of object %2 is not a function"
)
1594
.
arg
(
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
1595
->
runtimeStrings
[
l
->
nameIndex
]
1596
->
toQString
(),
1597
base
.
toQStringNoThrow
());
1598
return
engine
->
throwTypeError
(
message
);
1599
}
1600
1601
ReturnedValue
Runtime
::
CallValue
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
func
,
Value
*
argv
,
int
argc
)
1602
{
1603
if
(!
func
.
isFunctionObject
())
1604
return
engine
->
throwTypeError
(
QStringLiteral
(
"%1 is not a function"
).
arg
(
func
.
toQStringNoThrow
()));
1605
Value
undef
=
Value
::
undefinedValue
();
1606
return
checkedResult
(
engine
,
static_cast
<
const
FunctionObject
&>(
func
).
call
(
1607
&
undef
,
argv
,
argc
));
1608
}
1609
1610
ReturnedValue
Runtime
::
CallWithReceiver
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
func
,
1611
const
Value
&
thisObject
,
Value
argv
[],
int
argc
)
1612
{
1613
if
(!
func
.
isFunctionObject
())
1614
return
engine
->
throwTypeError
(
QStringLiteral
(
"%1 is not a function"
).
arg
(
func
.
toQStringNoThrow
()));
1615
return
checkedResult
(
engine
,
static_cast
<
const
FunctionObject
&>(
func
).
call
(
1616
&
thisObject
,
argv
,
argc
));
1617
}
1618
1619
struct
CallArgs
{
1620
Value *
argv
;
1621
int
argc
;
1622
};
1623
1624
static
CallArgs
createSpreadArguments
(
Scope
&scope, Value *argv,
int
argc)
1625
{
1626
ScopedValue
it(scope);
1627
ScopedValue
done(scope);
1628
1629
int
argCount = 0;
1630
1631
Value *v = scope.constructUndefined(1);
1632
Value *arguments = v;
1633
for
(
int
i = 0; i < argc; ++i) {
1634
if
(!argv[i].isEmpty()) {
1635
*v = argv[i];
1636
++argCount;
1637
v = scope.constructUndefined(1);
1638
continue
;
1639
}
1640
// spread element
1641
++i;
1642
it = Runtime::GetIterator::call(scope.engine, argv[i],
/* ForInIterator */
1);
1643
if
(scope.hasException())
1644
return
{
nullptr
, 0 };
1645
while
(1) {
1646
done = Runtime::IteratorNext::call(scope.engine, it, v);
1647
if
(scope.hasException())
1648
return
{
nullptr
, 0 };
1649
Q_ASSERT(done->isBoolean());
1650
if
(done->booleanValue())
1651
break
;
1652
++argCount;
1653
constexpr
auto
safetyMargin = 100;
// leave some space on the stack for actual work with the elements
1654
if
(qint64(scope.engine->jsStackLimit - scope.engine->jsStackTop) < safetyMargin) {
1655
scope.engine->throwRangeError(QLatin1String(
"Too many elements in array to use it with the spread operator"
));
1656
return
{
nullptr
, 0 };
1657
}
1658
v = scope.constructUndefined(1);
1659
}
1660
}
1661
return
{ arguments, argCount };
1662
}
1663
1664
ReturnedValue
Runtime
::
CallWithSpread
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
function
,
const
Value
&
thisObject
,
Value
*
argv
,
int
argc
)
1665
{
1666
Q_ASSERT
(
argc
>= 1);
1667
if
(!
function
.
isFunctionObject
())
1668
return
engine
->
throwTypeError
();
1669
1670
Scope
scope
(
engine
);
1671
CallArgs
arguments
=
createSpreadArguments
(
scope
,
argv
,
argc
);
1672
if
(
engine
->
hasException
)
1673
return
Encode
::
undefined
();
1674
1675
return
checkedResult
(
engine
,
static_cast
<
const
FunctionObject
&>(
function
).
call
(
1676
&
thisObject
,
arguments
.
argv
,
arguments
.
argc
));
1677
}
1678
1679
ReturnedValue
Runtime
::
Construct
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
function
,
const
Value
&
newTarget
,
Value
*
argv
,
int
argc
)
1680
{
1681
if
(!
function
.
isFunctionObject
())
1682
return
engine
->
throwTypeError
();
1683
1684
return
static_cast
<
const
FunctionObject
&>(
function
).
callAsConstructor
(
argv
,
argc
, &
newTarget
);
1685
}
1686
1687
ReturnedValue
Runtime
::
ConstructWithSpread
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
function
,
const
Value
&
newTarget
,
Value
*
argv
,
int
argc
)
1688
{
1689
if
(!
function
.
isFunctionObject
())
1690
return
engine
->
throwTypeError
();
1691
1692
Scope
scope
(
engine
);
1693
CallArgs
arguments
=
createSpreadArguments
(
scope
,
argv
,
argc
);
1694
if
(
engine
->
hasException
)
1695
return
Encode
::
undefined
();
1696
1697
return
static_cast
<
const
FunctionObject
&>(
function
).
callAsConstructor
(
arguments
.
argv
,
arguments
.
argc
, &
newTarget
);
1698
}
1699
1700
ReturnedValue
Runtime
::
TailCall
::
call
(
JSTypesStackFrame
*
frame
,
ExecutionEngine
*
engine
)
1701
{
1702
// IMPORTANT! The JIT assumes that this method has the same amount (or less) arguments than
1703
// the jitted function, so it can safely do a tail call.
1704
1705
Value
*
tos
=
engine
->
jsStackTop
;
1706
const
Value
&
function
=
tos
[
StackOffsets
::
tailCall_function
];
1707
const
Value
&
thisObject
=
tos
[
StackOffsets
::
tailCall_thisObject
];
1708
Value
*
argv
=
reinterpret_cast
<
Value
*>(
frame
->
jsFrame
) +
tos
[
StackOffsets
::
tailCall_argv
].
int_32
();
1709
int
argc
=
tos
[
StackOffsets
::
tailCall_argc
].
int_32
();
1710
Q_ASSERT
(
argc
>= 0);
1711
1712
const
JavaScriptFunctionObject
*
jsfo
=
function
.
as
<
JavaScriptFunctionObject
>();
1713
if
(!
jsfo
) {
1714
if
(
const
FunctionObject
*
fo
=
function
.
as
<
FunctionObject
>())
1715
return
checkedResult
(
engine
,
fo
->
call
(&
thisObject
,
argv
,
argc
));
1716
return
engine
->
throwTypeError
();
1717
}
1718
1719
if
(!
frame
->
callerCanHandleTailCall
() || !
jsfo
->
canBeTailCalled
() ||
engine
->
debugger
()
1720
||
unsigned
(
argc
) >
jsfo
->
formalParameterCount
()) {
1721
// Cannot tailcall, do a normal call:
1722
return
checkedResult
(
engine
,
jsfo
->
call
(&
thisObject
,
argv
,
argc
));
1723
}
1724
1725
memmove
(
frame
->
jsFrame
->
args
,
argv
,
argc
*
sizeof
(
Value
));
1726
frame
->
init
(
jsfo
->
function
(),
frame
->
jsFrame
->
argValues
<
Value
>(),
argc
,
1727
frame
->
callerCanHandleTailCall
());
1728
frame
->
setupJSFrame
(
frame
->
framePointer
(), *
jsfo
,
jsfo
->
scope
(),
thisObject
,
1729
Primitive
::
undefinedValue
());
1730
engine
->
jsStackTop
=
frame
->
framePointer
() +
frame
->
requiredJSStackFrameSize
();
1731
frame
->
setPendingTailCall
(
true
);
1732
return
Encode
::
undefined
();
1733
}
1734
1735
void
Runtime
::
ThrowException
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
1736
{
1737
if
(!
value
.
isEmpty
())
1738
engine
->
throwError
(
value
);
1739
}
1740
1741
ReturnedValue
Runtime
::
TypeofValue
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
1742
{
1743
Scope
scope
(
engine
);
1744
ScopedString
res
(
scope
);
1745
switch
(
value
.
type
()) {
1746
case
Value
::
Undefined_Type
:
1747
res
=
engine
->
id_undefined
();
1748
break
;
1749
case
Value
::
Null_Type
:
1750
res
=
engine
->
id_object
();
1751
break
;
1752
case
Value
::
Boolean_Type
:
1753
res
=
engine
->
id_boolean
();
1754
break
;
1755
case
Value
::
Managed_Type
:
1756
if
(
value
.
isString
())
1757
res
=
engine
->
id_string
();
1758
else
if
(
value
.
isSymbol
())
1759
res
=
engine
->
id_symbol
();
1760
else
if
(
value
.
objectValue
()->
as
<
FunctionObject
>())
1761
res
=
engine
->
id_function
();
1762
else
1763
res
=
engine
->
id_object
();
// ### implementation-defined
1764
break
;
1765
default
:
1766
res
=
engine
->
id_number
();
1767
break
;
1768
}
1769
return
res
.
asReturnedValue
();
1770
}
1771
1772
QV4
::
ReturnedValue
Runtime
::
TypeofName
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
)
1773
{
1774
Scope
scope
(
engine
);
1775
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1776
ScopedValue
prop
(
scope
,
engine
->
currentContext
()->
getProperty
(
name
));
1777
// typeof doesn't throw. clear any possible exception
1778
scope
.
engine
->
hasException
=
false
;
1779
return
TypeofValue
::
call
(
engine
,
prop
);
1780
}
1781
1782
void
Runtime
::
PushCallContext
::
call
(
JSTypesStackFrame
*
frame
)
1783
{
1784
frame
->
jsFrame
->
context
=
ExecutionContext
::
newCallContext
(
frame
)->
asReturnedValue
();
1785
}
1786
1787
ReturnedValue
Runtime
::
PushWithContext
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
acc
)
1788
{
1789
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1790
CallData
*
jsFrame
=
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
)->
jsFrame
;
1791
Value
&
newAcc
=
jsFrame
->
accumulator
.
asValue
<
Value
>();
1792
newAcc
=
Value
::
fromHeapObject
(
acc
.
toObject
(
engine
));
1793
if
(!
engine
->
hasException
) {
1794
Q_ASSERT
(
newAcc
.
isObject
());
1795
const
Object
&
obj
=
static_cast
<
const
Object
&>(
newAcc
);
1796
Value
&
context
=
jsFrame
->
context
.
asValue
<
Value
>();
1797
auto
ec
=
static_cast
<
const
ExecutionContext
*>(&
context
);
1798
context
=
ec
->
newWithContext
(
obj
.
d
())->
asReturnedValue
();
1799
}
1800
return
newAcc
.
asReturnedValue
();
1801
}
1802
1803
void
Runtime
::
PushCatchContext
::
call
(
ExecutionEngine
*
engine
,
int
blockIndex
,
int
exceptionVarNameIndex
)
1804
{
1805
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1806
auto
name
=
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
exceptionVarNameIndex
];
1807
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
)->
jsFrame
->
context
1808
=
ExecutionContext
::
newCatchContext
(
engine
->
currentStackFrame
,
blockIndex
,
name
)->
asReturnedValue
();
1809
}
1810
1811
void
Runtime
::
PushBlockContext
::
call
(
ExecutionEngine
*
engine
,
int
index
)
1812
{
1813
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1814
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
)->
jsFrame
->
context
1815
=
ExecutionContext
::
newBlockContext
(
engine
->
currentStackFrame
,
index
)->
asReturnedValue
();
1816
}
1817
1818
void
Runtime
::
CloneBlockContext
::
call
(
ExecutionEngine
*
engine
)
1819
{
1820
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1821
auto
frame
=
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
);
1822
auto
context
=
static_cast
<
Heap
::
CallContext
*>(
1823
Value
::
fromStaticValue
(
frame
->
jsFrame
->
context
).
m
());
1824
frame
->
jsFrame
->
context
=
1825
ExecutionContext
::
cloneBlockContext
(
engine
,
context
)->
asReturnedValue
();
1826
}
1827
1828
void
Runtime
::
PushScriptContext
::
call
(
ExecutionEngine
*
engine
,
int
index
)
1829
{
1830
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1831
Q_ASSERT
(
engine
->
currentContext
()->
d
()->
type
==
Heap
::
ExecutionContext
::
Type_GlobalContext
||
1832
engine
->
currentContext
()->
d
()->
type
==
Heap
::
ExecutionContext
::
Type_QmlContext
);
1833
ReturnedValue
c
=
ExecutionContext
::
newBlockContext
(
engine
->
currentStackFrame
,
index
)->
asReturnedValue
();
1834
engine
->
setScriptContext
(
c
);
1835
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
)->
jsFrame
->
context
=
c
;
1836
}
1837
1838
void
Runtime
::
PopScriptContext
::
call
(
ExecutionEngine
*
engine
)
1839
{
1840
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
1841
ReturnedValue
root
=
engine
->
rootContext
()->
asReturnedValue
();
1842
engine
->
setScriptContext
(
root
);
1843
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
)->
jsFrame
->
context
=
root
;
1844
}
1845
1846
void
Runtime
::
ThrowReferenceError
::
call
(
ExecutionEngine
*
engine
,
int
nameIndex
)
1847
{
1848
Scope
scope
(
engine
);
1849
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1850
engine
->
throwReferenceError
(
name
);
1851
}
1852
1853
void
Runtime
::
ThrowOnNullOrUndefined
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
v
)
1854
{
1855
if
(
v
.
isNullOrUndefined
())
1856
engine
->
throwTypeError
();
1857
}
1858
1859
void
Runtime
::
MarkCustom
::
call
(
const
Value
&
toBeMarked
)
1860
{
1861
auto
*
h
=
toBeMarked
.
heapObject
();
1862
if
(!
h
)
1863
return
;
1864
Q_ASSERT
(
h
->
internalClass
);
1865
auto
engine
=
h
->
internalClass
->
engine
;
1866
Q_ASSERT
(
engine
);
1867
// runtime function is only meant to be called while gc is ongoing
1868
Q_ASSERT
(
engine
->
isGCOngoing
);
1869
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
ms
) {
1870
h
->
mark
(
ms
);
1871
});
1872
}
1873
1874
ReturnedValue
Runtime
::
ConvertThisToObject
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
t
)
1875
{
1876
if
(!
t
.
isObject
()) {
1877
if
(
t
.
isNullOrUndefined
()) {
1878
return
engine
->
globalObject
->
asReturnedValue
();
1879
}
else
{
1880
return
t
.
toObject
(
engine
)->
asReturnedValue
();
1881
}
1882
}
1883
return
t
.
asReturnedValue
();
1884
}
1885
1886
void
Runtime
::
DeclareVar
::
call
(
ExecutionEngine
*
engine
,
Bool
deletable
,
int
nameIndex
)
1887
{
1888
Scope
scope
(
engine
);
1889
ScopedString
name
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeStrings
[
nameIndex
]);
1890
engine
->
currentContext
()->
createMutableBinding
(
name
,
deletable
);
1891
}
1892
1893
ReturnedValue
Runtime
::
ArrayLiteral
::
call
(
ExecutionEngine
*
engine
,
Value
*
values
,
uint
length
)
1894
{
1895
return
engine
->
newArrayObject
(
values
,
length
)->
asReturnedValue
();
1896
}
1897
1898
ReturnedValue
Runtime
::
ObjectLiteral
::
call
(
ExecutionEngine
*
engine
,
int
classId
,
QV4
::
Value
args
[],
int
argc
)
1899
{
1900
Scope
scope
(
engine
);
1901
Scoped
<
InternalClass
>
klass
(
scope
,
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeClasses
[
classId
]);
1902
ScopedObject
o
(
scope
,
engine
->
newObject
(
klass
->
d
()));
1903
1904
Q_ASSERT
(
uint
(
argc
) >=
klass
->
d
()->
size
);
1905
1906
for
(
uint
i
= 0;
i
<
klass
->
d
()->
size
; ++
i
)
1907
o
->
setProperty
(
i
, *
args
++);
1908
1909
Q_ASSERT
((
argc
-
klass
->
d
()->
size
) % 3 == 0);
1910
int
additionalArgs
= (
argc
-
int
(
klass
->
d
()->
size
))/3;
1911
1912
if
(!
additionalArgs
)
1913
return
o
->
asReturnedValue
();
1914
1915
ScopedPropertyKey
name
(
scope
);
1916
ScopedProperty
pd
(
scope
);
1917
ScopedFunctionObject
fn
(
scope
);
1918
ScopedString
fnName
(
scope
);
1919
ScopedValue
value
(
scope
);
1920
for
(
int
i
= 0;
i
<
additionalArgs
; ++
i
) {
1921
Q_ASSERT
(
args
->
isInteger
());
1922
ObjectLiteralArgument
arg
=
ObjectLiteralArgument
(
args
->
integerValue
());
1923
name
=
args
[1].
toPropertyKey
(
engine
);
1924
value
=
args
[2];
1925
if
(
engine
->
hasException
)
1926
return
Encode
::
undefined
();
1927
if
(
arg
!=
ObjectLiteralArgument
::
Value
) {
1928
Q_ASSERT
(
args
[2].
isInteger
());
1929
int
functionId
=
args
[2].
integerValue
();
1930
QV4
::
Function
*
clos
=
engine
->
currentStackFrame
->
v4Function
->
executableCompilationUnit
()
1931
->
runtimeFunctions
[
functionId
];
1932
Q_ASSERT
(
clos
);
1933
1934
PropertyKey
::
FunctionNamePrefix
prefix
=
PropertyKey
::
None
;
1935
if
(
arg
==
ObjectLiteralArgument
::
Getter
)
1936
prefix
=
PropertyKey
::
Getter
;
1937
else
if
(
arg
==
ObjectLiteralArgument
::
Setter
)
1938
prefix
=
PropertyKey
::
Setter
;
1939
else
1940
arg
=
ObjectLiteralArgument
::
Value
;
1941
fnName
=
name
->
asFunctionName
(
engine
,
prefix
);
1942
1943
ExecutionContext
*
current
=
engine
->
currentContext
();
1944
if
(
clos
->
isGenerator
())
1945
value
=
MemberGeneratorFunction
::
create
(
current
,
clos
,
o
,
fnName
)->
asReturnedValue
();
1946
else
1947
value
=
FunctionObject
::
createMemberFunction
(
current
,
clos
,
o
,
fnName
)->
asReturnedValue
();
1948
}
else
if
(
args
[2].
isFunctionObject
()) {
1949
fn
=
static_cast
<
const
FunctionObject
&>(
args
[2]);
1950
1951
fnName
=
name
->
asFunctionName
(
engine
,
PropertyKey
::
None
);
1952
fn
->
setName
(
fnName
);
1953
}
1954
Q_ASSERT
(
arg
!=
ObjectLiteralArgument
::
Method
);
1955
Q_ASSERT
(
arg
==
ObjectLiteralArgument
::
Value
||
value
->
isFunctionObject
());
1956
if
(
arg
==
ObjectLiteralArgument
::
Value
||
arg
==
ObjectLiteralArgument
::
Getter
) {
1957
pd
->
value
=
value
;
1958
pd
->
set
=
Value
::
emptyValue
();
1959
}
else
{
1960
pd
->
value
=
Value
::
emptyValue
();
1961
pd
->
set
=
value
;
1962
}
1963
bool
ok
=
o
->
defineOwnProperty
(
name
,
pd
, (
arg
==
ObjectLiteralArgument
::
Value
?
Attr_Data
:
Attr_Accessor
));
1964
if
(!
ok
)
1965
return
engine
->
throwTypeError
();
1966
1967
args
+= 3;
1968
}
1969
return
o
.
asReturnedValue
();
1970
}
1971
1972
ReturnedValue
Runtime
::
CreateClass
::
call
(
ExecutionEngine
*
engine
,
int
classIndex
,
1973
const
Value
&
superClass
,
Value
computedNames
[])
1974
{
1975
const
QV4
::
ExecutableCompilationUnit
*
unit
1976
=
engine
->
currentStackFrame
->
v4Function
->
executableCompilationUnit
();
1977
const
QV4
::
CompiledData
::
Class
*
cls
=
unit
->
unitData
()->
classAt
(
classIndex
);
1978
1979
Scope
scope
(
engine
);
1980
ScopedObject
protoParent
(
scope
,
engine
->
objectPrototype
());
1981
ScopedObject
constructorParent
(
scope
,
engine
->
functionPrototype
());
1982
if
(!
superClass
.
isEmpty
()) {
1983
if
(
superClass
.
isNull
()) {
1984
protoParent
=
Encode
::
null
();
1985
}
else
{
1986
const
FunctionObject
*
superFunction
=
superClass
.
as
<
FunctionObject
>();
1987
// ### check that the heritage object is a constructor
1988
if
(!
superFunction
|| !
superFunction
->
isConstructor
())
1989
return
engine
->
throwTypeError
(
QStringLiteral
(
"The superclass is not a function object."
));
1990
const
FunctionObject
*
s
=
static_cast
<
const
FunctionObject
*>(&
superClass
);
1991
ScopedValue
result
(
scope
,
s
->
get
(
scope
.
engine
->
id_prototype
()));
1992
if
(!
result
->
isObject
() && !
result
->
isNull
())
1993
return
engine
->
throwTypeError
(
QStringLiteral
(
"The value of the superclass's prototype property is not an object."
));
1994
protoParent
= *
result
;
1995
constructorParent
=
superClass
;
1996
}
1997
}
1998
1999
ScopedObject
proto
(
scope
,
engine
->
newObject
());
2000
proto
->
setPrototypeUnchecked
(
protoParent
);
2001
ExecutionContext
*
current
=
engine
->
currentContext
();
2002
2003
ScopedFunctionObject
constructor
(
scope
);
2004
QV4
::
Function
*
f
=
cls
->
constructorFunction
!=
UINT_MAX
?
unit
->
runtimeFunctions
[
cls
->
constructorFunction
] :
nullptr
;
2005
constructor
=
FunctionObject
::
createConstructorFunction
(
current
,
f
,
proto
, !
superClass
.
isEmpty
())->
asReturnedValue
();
2006
constructor
->
setPrototypeUnchecked
(
constructorParent
);
2007
Value
argCount
=
Value
::
fromInt32
(
f
?
f
->
nFormals
: 0);
2008
constructor
->
defineReadonlyConfigurableProperty
(
scope
.
engine
->
id_length
(),
argCount
);
2009
constructor
->
defineReadonlyConfigurableProperty
(
engine
->
id_prototype
(),
proto
);
2010
proto
->
defineDefaultProperty
(
engine
->
id_constructor
(),
constructor
);
2011
2012
ScopedString
name
(
scope
);
2013
if
(
cls
->
nameIndex
!=
UINT_MAX
) {
2014
name
=
unit
->
runtimeStrings
[
cls
->
nameIndex
];
2015
constructor
->
defineReadonlyConfigurableProperty
(
engine
->
id_name
(),
name
);
2016
}
2017
2018
ScopedObject
receiver
(
scope
, *
constructor
);
2019
ScopedPropertyKey
propertyName
(
scope
);
2020
ScopedFunctionObject
function
(
scope
);
2021
ScopedProperty
property
(
scope
);
2022
const
CompiledData
::
Method
*
methods
=
cls
->
methodTable
();
2023
for
(
uint
i
= 0;
i
<
cls
->
nStaticMethods
+
cls
->
nMethods
; ++
i
) {
2024
if
(
i
==
cls
->
nStaticMethods
)
2025
receiver
=
proto
;
2026
if
(
methods
[
i
].
name
==
UINT_MAX
) {
2027
propertyName
=
computedNames
->
toPropertyKey
(
engine
);
2028
if
(
propertyName
==
scope
.
engine
->
id_prototype
()->
propertyKey
() &&
receiver
->
d
() ==
constructor
->
d
())
2029
return
engine
->
throwTypeError
(
QStringLiteral
(
"Cannot declare a static method named 'prototype'."
));
2030
if
(
engine
->
hasException
)
2031
return
Encode
::
undefined
();
2032
++
computedNames
;
2033
}
else
{
2034
name
=
unit
->
runtimeStrings
[
methods
[
i
].
name
];
2035
propertyName
=
name
->
toPropertyKey
();
2036
}
2037
QV4
::
Function
*
f
=
unit
->
runtimeFunctions
[
methods
[
i
].
function
];
2038
Q_ASSERT
(
f
);
2039
PropertyKey
::
FunctionNamePrefix
prefix
=
PropertyKey
::
None
;
2040
if
(
methods
[
i
].
type
==
CompiledData
::
Method
::
Getter
)
2041
prefix
=
PropertyKey
::
Getter
;
2042
else
if
(
methods
[
i
].
type
==
CompiledData
::
Method
::
Setter
)
2043
prefix
=
PropertyKey
::
Setter
;
2044
2045
name
=
propertyName
->
asFunctionName
(
engine
,
prefix
);
2046
2047
if
(
f
->
isGenerator
())
2048
function
=
MemberGeneratorFunction
::
create
(
current
,
f
,
receiver
,
name
);
2049
else
2050
function
=
FunctionObject
::
createMemberFunction
(
current
,
f
,
receiver
,
name
);
2051
Q_ASSERT
(
function
);
2052
PropertyAttributes
attributes
;
2053
switch
(
methods
[
i
].
type
) {
2054
case
CompiledData
::
Method
::
Getter
:
2055
property
->
setGetter
(
function
);
2056
property
->
set
=
Value
::
emptyValue
();
2057
attributes
=
Attr_Accessor
|
Attr_NotEnumerable
;
2058
break
;
2059
case
CompiledData
::
Method
::
Setter
:
2060
property
->
value
=
Value
::
emptyValue
();
2061
property
->
setSetter
(
function
);
2062
attributes
=
Attr_Accessor
|
Attr_NotEnumerable
;
2063
break
;
2064
default
:
// Regular
2065
property
->
value
=
function
;
2066
property
->
set
=
Value
::
emptyValue
();
2067
attributes
=
Attr_Data
|
Attr_NotEnumerable
;
2068
break
;
2069
}
2070
receiver
->
defineOwnProperty
(
propertyName
,
property
,
attributes
);
2071
}
2072
2073
return
constructor
->
asReturnedValue
();
2074
}
2075
2076
QV4
::
ReturnedValue
Runtime
::
CreateMappedArgumentsObject
::
call
(
ExecutionEngine
*
engine
)
2077
{
2078
Q_ASSERT
(
engine
->
currentContext
()->
d
()->
type
==
Heap
::
ExecutionContext
::
Type_CallContext
);
2079
Heap
::
InternalClass
*
ic
=
engine
->
internalClasses
(
EngineBase
::
Class_ArgumentsObject
);
2080
return
engine
->
memoryManager
->
allocObject
<
ArgumentsObject
>(
ic
,
engine
->
currentStackFrame
)->
asReturnedValue
();
2081
}
2082
2083
QV4
::
ReturnedValue
Runtime
::
CreateUnmappedArgumentsObject
::
call
(
ExecutionEngine
*
engine
)
2084
{
2085
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
2086
Heap
::
InternalClass
*
ic
=
engine
->
internalClasses
(
EngineBase
::
Class_StrictArgumentsObject
);
2087
return
engine
->
memoryManager
->
allocObject
<
StrictArgumentsObject
>(
2088
ic
,
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
))->
asReturnedValue
();
2089
}
2090
2091
QV4
::
ReturnedValue
Runtime
::
CreateRestParameter
::
call
(
ExecutionEngine
*
engine
,
int
argIndex
)
2092
{
2093
Q_ASSERT
(
engine
->
currentStackFrame
->
isJSTypesFrame
());
2094
JSTypesStackFrame
*
frame
=
static_cast
<
JSTypesStackFrame
*>(
engine
->
currentStackFrame
);
2095
const
Value
*
values
=
frame
->
argv
() +
argIndex
;
2096
int
nValues
=
frame
->
argc
() -
argIndex
;
2097
if
(
nValues
<= 0)
2098
return
engine
->
newArrayObject
(0)->
asReturnedValue
();
2099
return
engine
->
newArrayObject
(
values
,
nValues
)->
asReturnedValue
();
2100
}
2101
2102
ReturnedValue
Runtime
::
RegexpLiteral
::
call
(
ExecutionEngine
*
engine
,
int
id
)
2103
{
2104
const
auto
val
2105
=
engine
->
currentStackFrame
->
v4Function
->
compilationUnit
->
runtimeRegularExpressions
[
id
];
2106
Heap
::
RegExpObject
*
ro
=
engine
->
newRegExpObject
(
Value
::
fromStaticValue
(
val
).
as
<
RegExp
>());
2107
return
ro
->
asReturnedValue
();
2108
}
2109
2110
ReturnedValue
Runtime
::
ToObject
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
obj
)
2111
{
2112
if
(
obj
.
isObject
())
2113
return
obj
.
asReturnedValue
();
2114
2115
return
obj
.
toObject
(
engine
)->
asReturnedValue
();
2116
}
2117
2118
Bool
Runtime
::
ToBoolean
::
call
(
const
Value
&
obj
)
2119
{
2120
return
obj
.
toBoolean
();
2121
}
2122
2123
ReturnedValue
Runtime
::
ToNumber
::
call
(
ExecutionEngine
*,
const
Value
&
v
)
2124
{
2125
return
Encode
(
v
.
toNumber
());
2126
}
2127
2128
ReturnedValue
Runtime
::
UMinus
::
call
(
const
Value
&
value
)
2129
{
2130
TRACE1
(value);
2131
2132
// +0 != -0, so we need to convert to double when negating 0
2133
if
(
value
.
isInteger
() &&
value
.
integerValue
() &&
2134
value
.
integerValue
() !=
std
::
numeric_limits
<
int
>::
min
())
2135
return
Encode
(-
value
.
integerValue
());
2136
else
{
2137
double
n
=
RuntimeHelpers
::
toNumber
(
value
);
2138
return
Encode
(-
n
);
2139
}
2140
}
2141
2142
// binary operators
2143
2144
ReturnedValue
Runtime
::
Add
::
call
(
ExecutionEngine
*
engine
,
const
Value
&
left
,
const
Value
&
right
)
2145
{
2146
TRACE2
(left, right);
2147
2148
if
(
Q_LIKELY
(
left
.
integerCompatible
() &&
right
.
integerCompatible
()))
2149
return
add_int32
(
left
.
integerValue
(),
right
.
integerValue
());
2150
if
(
left
.
isNumber
() &&
right
.
isNumber
())
2151
return
Value
::
fromDouble
(
left
.
asDouble
() +
right
.
asDouble
()).
asReturnedValue
();
2152
2153
return
RuntimeHelpers
::
addHelper
(
engine
,
left
,
right
);
2154
}
2155
2156
ReturnedValue
Runtime
::
Sub
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2157
{
2158
TRACE2
(left, right);
2159
2160
if
(
Q_LIKELY
(
left
.
integerCompatible
() &&
right
.
integerCompatible
()))
2161
return
sub_int32
(
left
.
integerValue
(),
right
.
integerValue
());
2162
2163
double
lval
=
left
.
isNumber
() ?
left
.
asDouble
() :
left
.
toNumberImpl
();
2164
double
rval
=
right
.
isNumber
() ?
right
.
asDouble
() :
right
.
toNumberImpl
();
2165
2166
return
Value
::
fromDouble
(
lval
-
rval
).
asReturnedValue
();
2167
}
2168
2169
ReturnedValue
Runtime
::
Mul
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2170
{
2171
TRACE2
(left, right);
2172
2173
if
(
Q_LIKELY
(
left
.
integerCompatible
() &&
right
.
integerCompatible
()))
2174
return
mul_int32
(
left
.
integerValue
(),
right
.
integerValue
());
2175
2176
double
lval
=
left
.
isNumber
() ?
left
.
asDouble
() :
left
.
toNumberImpl
();
2177
double
rval
=
right
.
isNumber
() ?
right
.
asDouble
() :
right
.
toNumberImpl
();
2178
2179
return
Value
::
fromDouble
(
lval
*
rval
).
asReturnedValue
();
2180
}
2181
2182
ReturnedValue
Runtime
::
Div
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2183
{
2184
TRACE2
(left, right);
2185
2186
if
(
Value
::
integerCompatible
(
left
,
right
)) {
2187
int
lval
=
left
.
integerValue
();
2188
int
rval
=
right
.
integerValue
();
2189
if
(
rval
!= 0
// division by zero should result in a NaN
2190
&& !(
lval
==
std
::
numeric_limits
<
int
>::
min
() &&
rval
== -1)
// doesn't fit in int
2191
&& (
lval
%
rval
== 0)
// fractions can't be stored in an int
2192
&& !(
lval
== 0 &&
rval
< 0))
// 0 / -something results in -0.0
2193
return
Encode
(
int
(
lval
/
rval
));
2194
else
2195
return
Encode
(
double
(
lval
) /
rval
);
2196
}
2197
2198
double
lval
=
left
.
toNumber
();
2199
double
rval
=
right
.
toNumber
();
2200
return
Value
::
fromDouble
(
lval
/
rval
).
asReturnedValue
();
2201
}
2202
2203
ReturnedValue
Runtime
::
Mod
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2204
{
2205
TRACE2
(left, right);
2206
2207
if
(
Value
::
integerCompatible
(
left
,
right
) &&
left
.
integerValue
() >= 0 &&
right
.
integerValue
() > 0) {
2208
// special cases are handled by fmod, among them:
2209
// - arithmic execeptions for ints in c++, eg: INT_MIN % -1
2210
// - undefined behavior in c++, e.g.: anything % 0
2211
// - uncommon cases which would complicate the condition, e.g.: negative integers
2212
// (this makes sure that -1 % 1 == -0 by passing it to fmod)
2213
return
Encode
(
left
.
integerValue
() %
right
.
integerValue
());
2214
}
2215
2216
double
lval
=
RuntimeHelpers
::
toNumber
(
left
);
2217
double
rval
=
RuntimeHelpers
::
toNumber
(
right
);
2218
#
ifdef
fmod
2219
#
undef
fmod
2220
#
endif
2221
return
Value
::
fromDouble
(
std
::
fmod
(
lval
,
rval
)).
asReturnedValue
();
2222
}
2223
2224
ReturnedValue
Runtime
::
Exp
::
call
(
const
Value
&
base
,
const
Value
&
exp
)
2225
{
2226
double
b
=
base
.
toNumber
();
2227
double
e
=
exp
.
toNumber
();
2228
return
Encode
(
QQmlPrivate
::
jsExponentiate
(
b
,
e
));
2229
}
2230
2231
ReturnedValue
Runtime
::
BitAnd
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2232
{
2233
TRACE2
(left, right);
2234
2235
int
lval
=
left
.
toInt32
();
2236
int
rval
=
right
.
toInt32
();
2237
return
Encode
((
int
)(
lval
&
rval
));
2238
}
2239
2240
ReturnedValue
Runtime
::
BitOr
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2241
{
2242
TRACE2
(left, right);
2243
2244
int
lval
=
left
.
toInt32
();
2245
int
rval
=
right
.
toInt32
();
2246
return
Encode
((
int
)(
lval
|
rval
));
2247
}
2248
2249
ReturnedValue
Runtime
::
BitXor
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2250
{
2251
TRACE2
(left, right);
2252
2253
int
lval
=
left
.
toInt32
();
2254
int
rval
=
right
.
toInt32
();
2255
return
Encode
((
int
)(
lval
^
rval
));
2256
}
2257
2258
ReturnedValue
Runtime
::
Shl
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2259
{
2260
TRACE2
(left, right);
2261
2262
int
lval
=
left
.
toInt32
();
2263
int
rval
=
right
.
toInt32
() & 0x1f;
2264
return
Encode
((
int
)(
lval
<<
rval
));
2265
}
2266
2267
ReturnedValue
Runtime
::
Shr
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2268
{
2269
TRACE2
(left, right);
2270
2271
int
lval
=
left
.
toInt32
();
2272
unsigned
rval
=
right
.
toUInt32
() & 0x1f;
2273
return
Encode
((
int
)(
lval
>>
rval
));
2274
}
2275
2276
ReturnedValue
Runtime
::
UShr
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2277
{
2278
TRACE2
(left, right);
2279
2280
unsigned
lval
=
left
.
toUInt32
();
2281
unsigned
rval
=
right
.
toUInt32
() & 0x1f;
2282
uint
res
=
lval
>>
rval
;
2283
2284
return
Encode
(
res
);
2285
}
2286
2287
ReturnedValue
Runtime
::
GreaterThan
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2288
{
2289
TRACE2
(left, right);
2290
2291
bool
r
=
CompareGreaterThan
::
call
(
left
,
right
);
2292
return
Encode
(
r
);
2293
}
2294
2295
ReturnedValue
Runtime
::
LessThan
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2296
{
2297
TRACE2
(left, right);
2298
2299
bool
r
=
CompareLessThan
::
call
(
left
,
right
);
2300
return
Encode
(
r
);
2301
}
2302
2303
ReturnedValue
Runtime
::
GreaterEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2304
{
2305
TRACE2
(left, right);
2306
2307
bool
r
=
CompareGreaterEqual
::
call
(
left
,
right
);
2308
return
Encode
(
r
);
2309
}
2310
2311
ReturnedValue
Runtime
::
LessEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2312
{
2313
TRACE2
(left, right);
2314
2315
bool
r
=
CompareLessEqual
::
call
(
left
,
right
);
2316
return
Encode
(
r
);
2317
}
2318
2319
struct
LazyScope
2320
{
2321
ExecutionEngine *
engine
=
nullptr
;
2322
Value *
stackMark
=
nullptr
;
2323
~
LazyScope
() {
2324
if
(
engine
)
2325
engine
->jsStackTop =
stackMark
;
2326
}
2327
template
<
typename
T>
2328
void
set
(Value **scopedValue, T value, ExecutionEngine *e) {
2329
if
(!
engine
) {
2330
engine
= e;
2331
stackMark
=
engine
->jsStackTop;
2332
}
2333
if
(!*scopedValue)
2334
*scopedValue = e->jsAlloca(1);
2335
**scopedValue = value;
2336
}
2337
};
2338
2339
Bool
Runtime
::
CompareEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2340
{
2341
TRACE2
(left, right);
2342
2343
Value
lhs
=
left
;
2344
Value
rhs
=
right
;
2345
2346
LazyScope
scope
;
2347
Value
*
lhsGuard
=
nullptr
;
2348
Value
*
rhsGuard
=
nullptr
;
2349
2350
redo
:
2351
if
(
lhs
.
asReturnedValue
() ==
rhs
.
asReturnedValue
())
2352
return
!
lhs
.
isNaN
();
2353
2354
quint32
lt
=
lhs
.
quickType
();
2355
quint32
rt
=
rhs
.
quickType
();
2356
2357
// LHS: Check if managed
2358
if
((
lt
& (
Value
::
ManagedMask
>>
Value
::
Tag_Shift
)) == 0) {
2359
if
(
lhs
.
isUndefined
())
2360
return
rhs
.
isNullOrUndefined
();
2361
2362
// RHS: Check if managed
2363
if
((
rt
& (
Value
::
ManagedMask
>>
Value
::
Tag_Shift
)) == 0) {
2364
if
(
rhs
.
isUndefined
())
2365
return
false
;
2366
2367
Heap
::
Base
*
l
=
lhs
.
m
();
2368
Heap
::
Base
*
r
=
rhs
.
m
();
2369
Q_ASSERT
(
l
);
2370
Q_ASSERT
(
r
);
2371
if
(
l
->
internalClass
->
vtable
->
isStringOrSymbol
==
r
->
internalClass
->
vtable
->
isStringOrSymbol
)
2372
return
static_cast
<
QV4
::
Managed
&>(
lhs
).
isEqualTo
(&
static_cast
<
QV4
::
Managed
&>(
rhs
));
2373
if
(
l
->
internalClass
->
vtable
->
isStringOrSymbol
) {
2374
scope
.
set
(&
rhsGuard
,
RuntimeHelpers
::
objectDefaultValue
(&
static_cast
<
QV4
::
Object
&>(
rhs
),
PREFERREDTYPE_HINT
),
r
->
internalClass
->
engine
);
2375
rhs
=
rhsGuard
->
asReturnedValue
();
2376
goto
redo
;
2377
}
else
{
2378
Q_ASSERT
(
r
->
internalClass
->
vtable
->
isStringOrSymbol
);
2379
scope
.
set
(&
lhsGuard
,
RuntimeHelpers
::
objectDefaultValue
(&
static_cast
<
QV4
::
Object
&>(
lhs
),
PREFERREDTYPE_HINT
),
l
->
internalClass
->
engine
);
2380
lhs
=
lhsGuard
->
asReturnedValue
();
2381
goto
redo
;
2382
}
2383
return
false
;
2384
}
2385
2386
lhs_managed_and_rhs_not
:
2387
switch
(
rt
) {
2388
case
QV4
::
Value
::
QT_Empty
:
2389
Q_UNREACHABLE
();
2390
case
QV4
::
Value
::
QT_Null
:
2391
return
false
;
2392
case
QV4
::
Value
::
QT_Bool
:
2393
case
QV4
::
Value
::
QT_Int
:
2394
rhs
=
Value
::
fromDouble
(
rhs
.
int_32
());
2395
Q_FALLTHROUGH
();
2396
default
:
// double
2397
if
(
lhs
.
m
()->
internalClass
->
vtable
->
isStringOrSymbol
) {
2398
return
lhs
.
m
()->
internalClass
->
vtable
->
isString
? (
RuntimeHelpers
::
toNumber
(
lhs
) ==
rhs
.
doubleValue
()) :
false
;
2399
}
else
{
2400
scope
.
set
(&
lhsGuard
,
RuntimeHelpers
::
objectDefaultValue
(&
static_cast
<
QV4
::
Object
&>(
lhs
),
PREFERREDTYPE_HINT
),
lhs
.
m
()->
internalClass
->
engine
);
2401
lhs
=
lhsGuard
->
asReturnedValue
();
2402
}
2403
}
2404
goto
redo
;
2405
}
else
if
((
rt
& (
Value
::
ManagedMask
>>
Value
::
Tag_Shift
)) == 0) {
2406
if
(
rhs
.
isUndefined
())
2407
return
lhs
.
isNull
();
// Can't be undefined
2408
qSwap
(
lhs
,
rhs
);
2409
qSwap
(
lt
,
rt
);
2410
goto
lhs_managed_and_rhs_not
;
2411
}
2412
2413
switch
(
lt
) {
2414
case
QV4
::
Value
::
QT_Empty
:
2415
Q_UNREACHABLE
();
2416
case
QV4
::
Value
::
QT_Null
:
2417
return
rhs
.
isNull
();
2418
case
QV4
::
Value
::
QT_Bool
:
2419
case
QV4
::
Value
::
QT_Int
:
2420
switch
(
rt
) {
2421
case
QV4
::
Value
::
QT_Empty
:
2422
Q_UNREACHABLE
();
2423
case
QV4
::
Value
::
QT_Null
:
2424
return
false
;
2425
case
QV4
::
Value
::
QT_Bool
:
2426
case
QV4
::
Value
::
QT_Int
:
2427
return
lhs
.
int_32
() ==
rhs
.
int_32
();
2428
default
:
// double
2429
return
lhs
.
int_32
() ==
rhs
.
doubleValue
();
2430
}
2431
default
:
// double
2432
switch
(
rt
) {
2433
case
QV4
::
Value
::
QT_Empty
:
2434
Q_UNREACHABLE
();
2435
case
QV4
::
Value
::
QT_Null
:
2436
return
false
;
2437
case
QV4
::
Value
::
QT_Bool
:
2438
case
QV4
::
Value
::
QT_Int
:
2439
return
lhs
.
doubleValue
() ==
rhs
.
int_32
();
2440
default
:
// double
2441
return
lhs
.
doubleValue
() ==
rhs
.
doubleValue
();
2442
}
2443
}
2444
}
2445
2446
ReturnedValue
Runtime
::
Equal
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2447
{
2448
TRACE2
(left, right);
2449
2450
bool
r
=
CompareEqual
::
call
(
left
,
right
);
2451
return
Encode
(
r
);
2452
}
2453
2454
ReturnedValue
Runtime
::
NotEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2455
{
2456
TRACE2
(left, right);
2457
2458
bool
r
= !
CompareEqual
::
call
(
left
,
right
);
2459
return
Encode
(
r
);
2460
}
2461
2462
ReturnedValue
Runtime
::
StrictEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2463
{
2464
TRACE2
(left, right);
2465
2466
bool
r
=
RuntimeHelpers
::
strictEqual
(
left
,
right
);
2467
return
Encode
(
r
);
2468
}
2469
2470
ReturnedValue
Runtime
::
StrictNotEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2471
{
2472
TRACE2
(left, right);
2473
2474
bool
r
= !
RuntimeHelpers
::
strictEqual
(
left
,
right
);
2475
return
Encode
(
r
);
2476
}
2477
2478
Bool
Runtime
::
CompareNotEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2479
{
2480
TRACE2
(left, right);
2481
2482
return
!
Runtime
::
CompareEqual
::
call
(
left
,
right
);
2483
}
2484
2485
Bool
Runtime
::
CompareStrictEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2486
{
2487
TRACE2
(left, right);
2488
2489
return
RuntimeHelpers
::
strictEqual
(
left
,
right
);
2490
}
2491
2492
Bool
Runtime
::
CompareStrictNotEqual
::
call
(
const
Value
&
left
,
const
Value
&
right
)
2493
{
2494
TRACE2
(left, right);
2495
2496
return
!
RuntimeHelpers
::
strictEqual
(
left
,
right
);
2497
}
2498
2499
template
<
typename
Operation>
2500
static
inline
const
void
*
symbol
()
2501
{
2502
return
reinterpret_cast
<
void
*>(&Operation::call);
2503
}
2504
2505
QHash
<
const
void
*,
const
char
*>
Runtime
::
symbolTable
()
2506
{
2507
static
const
QHash
<
const
void
*,
const
char
*>
symbols
({
2508
#
ifndef
V4_BOOTSTRAP
2509
{
symbol
<
CallGlobalLookup
>(),
"CallGlobalLookup"
},
2510
{
symbol
<
CallQmlContextPropertyLookup
>(),
"CallQmlContextPropertyLookup"
},
2511
{
symbol
<
CallName
>(),
"CallName"
},
2512
{
symbol
<
CallProperty
>(),
"CallProperty"
},
2513
{
symbol
<
CallPropertyLookup
>(),
"CallPropertyLookup"
},
2514
{
symbol
<
CallValue
>(),
"CallValue"
},
2515
{
symbol
<
CallWithReceiver
>(),
"CallWithReceiver"
},
2516
{
symbol
<
CallPossiblyDirectEval
>(),
"CallPossiblyDirectEval"
},
2517
{
symbol
<
CallWithSpread
>(),
"CallWithSpread"
},
2518
{
symbol
<
TailCall
>(),
"TailCall"
},
2519
2520
{
symbol
<
Construct
>(),
"Construct"
},
2521
{
symbol
<
ConstructWithSpread
>(),
"ConstructWithSpread"
},
2522
2523
{
symbol
<
StoreNameStrict
>(),
"StoreNameStrict"
},
2524
{
symbol
<
StoreNameSloppy
>(),
"StoreNameSloppy"
},
2525
{
symbol
<
StoreProperty
>(),
"StoreProperty"
},
2526
{
symbol
<
StoreElement
>(),
"StoreElement"
},
2527
{
symbol
<
LoadProperty
>(),
"LoadProperty"
},
2528
{
symbol
<
LoadName
>(),
"LoadName"
},
2529
{
symbol
<
LoadElement
>(),
"LoadElement"
},
2530
{
symbol
<
LoadSuperProperty
>(),
"LoadSuperProperty"
},
2531
{
symbol
<
StoreSuperProperty
>(),
"StoreSuperProperty"
},
2532
{
symbol
<
LoadSuperConstructor
>(),
"LoadSuperConstructor"
},
2533
{
symbol
<
LoadGlobalLookup
>(),
"LoadGlobalLookup"
},
2534
{
symbol
<
LoadQmlContextPropertyLookup
>(),
"LoadQmlContextPropertyLookup"
},
2535
{
symbol
<
GetLookup
>(),
"GetLookup"
},
2536
{
symbol
<
SetLookupStrict
>(),
"SetLookupStrict"
},
2537
{
symbol
<
SetLookupSloppy
>(),
"SetLookupSloppy"
},
2538
2539
{
symbol
<
TypeofValue
>(),
"TypeofValue"
},
2540
{
symbol
<
TypeofName
>(),
"TypeofName"
},
2541
2542
{
symbol
<
DeleteProperty_NoThrow
>(),
"DeleteProperty_NoThrow"
},
2543
{
symbol
<
DeleteProperty
>(),
"DeleteProperty"
},
2544
{
symbol
<
DeleteName_NoThrow
>(),
"DeleteName_NoThrow"
},
2545
{
symbol
<
DeleteName
>(),
"DeleteName"
},
2546
2547
{
symbol
<
ThrowException
>(),
"ThrowException"
},
2548
{
symbol
<
PushCallContext
>(),
"PushCallContext"
},
2549
{
symbol
<
PushWithContext
>(),
"PushWithContext"
},
2550
{
symbol
<
PushCatchContext
>(),
"PushCatchContext"
},
2551
{
symbol
<
PushBlockContext
>(),
"PushBlockContext"
},
2552
{
symbol
<
CloneBlockContext
>(),
"CloneBlockContext"
},
2553
{
symbol
<
PushScriptContext
>(),
"PushScriptContext"
},
2554
{
symbol
<
PopScriptContext
>(),
"PopScriptContext"
},
2555
{
symbol
<
ThrowReferenceError
>(),
"ThrowReferenceError"
},
2556
{
symbol
<
ThrowOnNullOrUndefined
>(),
"ThrowOnNullOrUndefined"
},
2557
2558
{
symbol
<
Closure
>(),
"Closure"
},
2559
2560
{
symbol
<
MarkCustom
>(),
"MarkCustom"
},
2561
2562
{
symbol
<
ConvertThisToObject
>(),
"ConvertThisToObject"
},
2563
{
symbol
<
DeclareVar
>(),
"DeclareVar"
},
2564
{
symbol
<
CreateMappedArgumentsObject
>(),
"CreateMappedArgumentsObject"
},
2565
{
symbol
<
CreateUnmappedArgumentsObject
>(),
"CreateUnmappedArgumentsObject"
},
2566
{
symbol
<
CreateRestParameter
>(),
"CreateRestParameter"
},
2567
2568
{
symbol
<
ArrayLiteral
>(),
"ArrayLiteral"
},
2569
{
symbol
<
ObjectLiteral
>(),
"ObjectLiteral"
},
2570
{
symbol
<
CreateClass
>(),
"CreateClass"
},
2571
2572
{
symbol
<
GetIterator
>(),
"GetIterator"
},
2573
{
symbol
<
IteratorNext
>(),
"IteratorNext"
},
2574
{
symbol
<
IteratorNextForYieldStar
>(),
"IteratorNextForYieldStar"
},
2575
{
symbol
<
IteratorClose
>(),
"IteratorClose"
},
2576
{
symbol
<
DestructureRestElement
>(),
"DestructureRestElement"
},
2577
2578
{
symbol
<
ToObject
>(),
"ToObject"
},
2579
{
symbol
<
ToBoolean
>(),
"ToBoolean"
},
2580
{
symbol
<
ToNumber
>(),
"ToNumber"
},
2581
2582
{
symbol
<
UMinus
>(),
"UMinus"
},
2583
2584
{
symbol
<
Instanceof
>(),
"Instanceof"
},
2585
{
symbol
<
As
>(),
"As"
},
2586
{
symbol
<
In
>(),
"In"
},
2587
{
symbol
<
Add
>(),
"Add"
},
2588
{
symbol
<
Sub
>(),
"Sub"
},
2589
{
symbol
<
Mul
>(),
"Mul"
},
2590
{
symbol
<
Div
>(),
"Div"
},
2591
{
symbol
<
Mod
>(),
"Mod"
},
2592
{
symbol
<
Exp
>(),
"Exp"
},
2593
{
symbol
<
BitAnd
>(),
"BitAnd"
},
2594
{
symbol
<
BitOr
>(),
"BitOr"
},
2595
{
symbol
<
BitXor
>(),
"BitXor"
},
2596
{
symbol
<
Shl
>(),
"Shl"
},
2597
{
symbol
<
Shr
>(),
"Shr"
},
2598
{
symbol
<
UShr
>(),
"UShr"
},
2599
{
symbol
<
GreaterThan
>(),
"GreaterThan"
},
2600
{
symbol
<
LessThan
>(),
"LessThan"
},
2601
{
symbol
<
GreaterEqual
>(),
"GreaterEqual"
},
2602
{
symbol
<
LessEqual
>(),
"LessEqual"
},
2603
{
symbol
<
Equal
>(),
"Equal"
},
2604
{
symbol
<
NotEqual
>(),
"NotEqual"
},
2605
{
symbol
<
StrictEqual
>(),
"StrictEqual"
},
2606
{
symbol
<
StrictNotEqual
>(),
"StrictNotEqual"
},
2607
2608
{
symbol
<
CompareGreaterThan
>(),
"CompareGreaterThan"
},
2609
{
symbol
<
CompareLessThan
>(),
"CompareLessThan"
},
2610
{
symbol
<
CompareGreaterEqual
>(),
"CompareGreaterEqual"
},
2611
{
symbol
<
CompareLessEqual
>(),
"CompareLessEqual"
},
2612
{
symbol
<
CompareEqual
>(),
"CompareEqual"
},
2613
{
symbol
<
CompareNotEqual
>(),
"CompareNotEqual"
},
2614
{
symbol
<
CompareStrictEqual
>(),
"CompareStrictEqual"
},
2615
{
symbol
<
CompareStrictNotEqual
>(),
"CompareStrictNotEqual"
},
2616
2617
{
symbol
<
CompareInstanceof
>(),
"CompareInstanceOf"
},
2618
{
symbol
<
CompareIn
>(),
"CompareIn"
},
2619
2620
{
symbol
<
RegexpLiteral
>(),
"RegexpLiteral"
},
2621
{
symbol
<
GetTemplateObject
>(),
"GetTemplateObject"
}
2622
#
endif
2623
});
2624
2625
return
symbols
;
2626
}
2627
2628
}
// namespace QV4
2629
2630
QT_END_NAMESPACE
QV4::Heap
Definition
qv4compileddata_p.h:76
QV4
Definition
qjsvalue.h:24
QV4::doInstanceof
static QV4::ReturnedValue doInstanceof(ExecutionEngine *engine, const Value &lval, const Value &rval)
Definition
qv4runtime.cpp:359
QV4::convert_to_string_add
static Heap::String * convert_to_string_add(ExecutionEngine *engine, Value value)
Definition
qv4runtime.cpp:655
QV4::ScopedObject
Scoped< Object > ScopedObject
Definition
qv4global_p.h:169
QV4::throwPropertyIsNotAFunctionTypeError
static ReturnedValue throwPropertyIsNotAFunctionTypeError(ExecutionEngine *engine, Value *thisObject, const QString &propertyName)
Definition
qv4runtime.cpp:1451
QV4::runtimeLookup
static QV4::Lookup * runtimeLookup(Function *f, uint i)
Definition
qv4runtime.cpp:190
QV4::setElementFallback
static Q_NEVER_INLINE bool setElementFallback(ExecutionEngine *engine, const Value &object, const Value &index, const Value &value)
Definition
qv4runtime.cpp:792
QV4::getSuperBase
static Object * getSuperBase(Scope &scope)
Definition
qv4runtime.cpp:1094
QV4::createSpreadArguments
static CallArgs createSpreadArguments(Scope &scope, Value *argv, int argc)
Definition
qv4runtime.cpp:1624
QV4::symbol
static const void * symbol()
Definition
qv4runtime.cpp:2500
QV4::ScopedContext
Scoped< ExecutionContext > ScopedContext
Definition
qv4global_p.h:172
Q_STATIC_LOGGING_CATEGORY
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
TRACE1
#define TRACE1(x)
Definition
qv4runtime_p.h:52
TRACE2
#define TRACE2(x, y)
Definition
qv4runtime_p.h:53
QV4::CallArgs
Definition
qv4runtime.cpp:1619
QV4::CallArgs::argv
Value * argv
Definition
qv4runtime.cpp:1620
QV4::CallArgs::argc
int argc
Definition
qv4runtime.cpp:1621
QV4::LazyScope
Definition
qv4runtime.cpp:2320
QV4::LazyScope::engine
ExecutionEngine * engine
Definition
qv4runtime.cpp:2321
QV4::LazyScope::set
void set(Value **scopedValue, T value, ExecutionEngine *e)
Definition
qv4runtime.cpp:2328
QV4::LazyScope::~LazyScope
~LazyScope()
Definition
qv4runtime.cpp:2323
QV4::LazyScope::stackMark
Value * stackMark
Definition
qv4runtime.cpp:2322
QV4::Scope
Definition
qv4scopedvalue_p.h:61
QV4::ScopedValue
Definition
qv4scopedvalue_p.h:156
qtdeclarative
src
qml
jsruntime
qv4runtime.cpp
Generated on
for Qt by
1.18.0