aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4runtime_p.h
blob: b38f833ad041334cfd656e6062965124d434f215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QMLJS_RUNTIME_H
#define QMLJS_RUNTIME_H

#include "qv4global_p.h"
#include "qv4value_p.h"
#include "qv4math_p.h"


#include <QtCore/QString>
#include <QtCore/qnumeric.h>
#include <QtCore/QDebug>
#include <QtCore/qurl.h>

#include <cmath>
#include <cassert>
#include <limits>

//#include <wtf/MathExtras.h>

#ifdef DO_TRACE_INSTR
#  define TRACE1(x) fprintf(stderr, "    %s\n", __FUNCTION__);
#  define TRACE2(x, y) fprintf(stderr, "    %s\n", __FUNCTION__);
#else
#  define TRACE1(x)
#  define TRACE2(x, y)
#endif // TRACE1

QT_BEGIN_NAMESPACE

namespace QV4 {

enum TypeHint {
    PREFERREDTYPE_HINT,
    NUMBER_HINT,
    STRING_HINT
};

struct Function;
struct Object;
struct String;
struct ExecutionContext;
struct FunctionObject;
struct BooleanObject;
struct NumberObject;
struct StringObject;
struct DateObject;
struct RegExpObject;
struct ArrayObject;
struct ErrorObject;
struct ExecutionEngine;
struct InternalClass;

// context
void __qmljs_call_activation_property(QV4::ExecutionContext *, QV4::Value *result, QV4::String *name, QV4::Value *args, int argc);
void __qmljs_call_property(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &that, QV4::String *name, QV4::Value *args, int argc);
void __qmljs_call_property_lookup(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &thisObject, uint index, QV4::Value *args, int argc);
void __qmljs_call_element(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &that, const QV4::Value &index, QV4::Value *args, int argc);
void __qmljs_call_value(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value *thisObject, const QV4::Value &func, QV4::Value *args, int argc);

void __qmljs_construct_activation_property(QV4::ExecutionContext *, QV4::Value *result, QV4::String *name, QV4::Value *args, int argc);
void __qmljs_construct_property(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &base, QV4::String *name, QV4::Value *args, int argc);
void __qmljs_construct_value(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &func, QV4::Value *args, int argc);

void __qmljs_builtin_typeof(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &val);
void __qmljs_builtin_typeof_name(QV4::ExecutionContext *context, QV4::Value* result, QV4::String *name);
void __qmljs_builtin_typeof_member(QV4::ExecutionContext* context, QV4::Value* result, const QV4::Value &base, QV4::String *name);
void __qmljs_builtin_typeof_element(QV4::ExecutionContext* context, QV4::Value *result, const QV4::Value &base, const QV4::Value &index);

void __qmljs_builtin_post_increment(QV4::Value *result, QV4::Value *val);
void __qmljs_builtin_post_increment_name(QV4::ExecutionContext *context, QV4::Value *result, QV4::String *name);
void __qmljs_builtin_post_increment_member(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &base, QV4::String *name);
void __qmljs_builtin_post_increment_element(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &base, const QV4::Value *index);

void __qmljs_builtin_post_decrement(QV4::Value *result, QV4::Value *val);
void __qmljs_builtin_post_decrement_name(QV4::ExecutionContext *context, QV4::Value *result, QV4::String *name);
void __qmljs_builtin_post_decrement_member(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &base, QV4::String *name);
void __qmljs_builtin_post_decrement_element(QV4::ExecutionContext *context, QV4::Value *result, const QV4::Value &base, const QV4::Value &index);

void Q_NORETURN __qmljs_builtin_rethrow(QV4::ExecutionContext *context);
QV4::ExecutionContext *__qmljs_builtin_push_with_scope(const QV4::Value &o, QV4::ExecutionContext *ctx);
QV4::ExecutionContext *__qmljs_builtin_push_catch_scope(QV4::String *exceptionVarName, const QV4::Value &exceptionValue, QV4::ExecutionContext *ctx);
QV4::ExecutionContext *__qmljs_builtin_pop_scope(QV4::ExecutionContext *ctx);
void __qmljs_builtin_declare_var(QV4::ExecutionContext *ctx, bool deletable, QV4::String *name);
void __qmljs_builtin_define_property(QV4::ExecutionContext *ctx, const QV4::Value &object, QV4::String *name, QV4::Value *val);
void __qmljs_builtin_define_array(QV4::ExecutionContext *ctx, QV4::Value *array, QV4::Value *values, uint length);
void __qmljs_builtin_define_getter_setter(QV4::ExecutionContext *ctx, const QV4::Value &object, QV4::String *name, const QV4::Value *getter, const QV4::Value *setter);
void __qmljs_builtin_define_object_literal(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value *args, int classId);
void __qmljs_builtin_setup_arguments_object(ExecutionContext *ctx, QV4::Value *result);

void __qmljs_value_from_string(QV4::Value *result, QV4::String *string);
void __qmljs_lookup_runtime_regexp(QV4::ExecutionContext *ctx, QV4::Value *result, int id);

// constructors
void __qmljs_init_closure(QV4::ExecutionContext *ctx, QV4::Value *result, int functionId);

// strings
Q_QML_EXPORT double __qmljs_string_to_number(const QString &s);
QV4::Value __qmljs_string_from_number(QV4::ExecutionContext *ctx, double number);
QV4::String *__qmljs_string_concat(QV4::ExecutionContext *ctx, QV4::String *first, QV4::String *second);

// objects
Q_QML_EXPORT QV4::Value __qmljs_object_default_value(QV4::Object *object, int typeHint);
void __qmljs_set_activation_property(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value& value);
void __qmljs_set_property(QV4::ExecutionContext *ctx, const QV4::Value &object, QV4::String *name, const QV4::Value &value);
void __qmljs_get_property(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &object, QV4::String *name);
void __qmljs_get_activation_property(QV4::ExecutionContext *ctx, QV4::Value *result, QV4::String *name);

void __qmljs_call_global_lookup(QV4::ExecutionContext *context, QV4::Value *result, uint index, QV4::Value *args, int argc);
void __qmljs_construct_global_lookup(QV4::ExecutionContext *context, QV4::Value *result, uint index, QV4::Value *args, int argc);


void __qmljs_get_element(QV4::ExecutionContext *ctx, QV4::Value *retval, const QV4::Value &object, const QV4::Value &index);
void __qmljs_set_element(QV4::ExecutionContext *ctx, const QV4::Value &object, const QV4::Value &index, const QV4::Value &value);

// For each
void __qmljs_foreach_iterator_object(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &in);
void __qmljs_foreach_next_property_name(QV4::Value *result, const QV4::Value &foreach_iterator);

// type conversion and testing
QV4::Value __qmljs_to_primitive(const QV4::Value &value, int typeHint);
Q_QML_EXPORT QV4::Bool __qmljs_to_boolean(const QV4::Value &value);
double __qmljs_to_number(const QV4::Value &value);
QV4::Value __qmljs_to_string(const QV4::Value &value, QV4::ExecutionContext *ctx);
Q_QML_EXPORT QV4::String *__qmljs_convert_to_string(QV4::ExecutionContext *ctx, const QV4::Value &value);
void __qmljs_numberToString(QString *result, double num, int radix = 10);
QV4::Value __qmljs_to_object(QV4::ExecutionContext *ctx, const QV4::Value &value);
QV4::Object *__qmljs_convert_to_object(QV4::ExecutionContext *ctx, const QV4::Value &value);

QV4::Bool __qmljs_equal_helper(const Value &x, const Value &y);
Q_QML_EXPORT QV4::Bool __qmljs_strict_equal(const QV4::Value &x, const QV4::Value &y);

// unary operators
typedef void (*UnaryOpName)(QV4::Value *, const QV4::Value &);
void __qmljs_uplus(QV4::Value *result, const QV4::Value &value);
void __qmljs_uminus(QV4::Value *result, const QV4::Value &value);
void __qmljs_compl(QV4::Value *result, const QV4::Value &value);
void __qmljs_not(QV4::Value *result, const QV4::Value &value);
void __qmljs_increment(QV4::Value *result, const QV4::Value &value);
void __qmljs_decrement(QV4::Value *result, const QV4::Value &value);

Q_QML_EXPORT void __qmljs_value_to_double(double *result, const Value &value);
Q_QML_EXPORT int __qmljs_value_to_int32(const Value &value);
Q_QML_EXPORT int __qmljs_double_to_int32(const double &d);
Q_QML_EXPORT unsigned __qmljs_value_to_uint32(const Value &value);
Q_QML_EXPORT unsigned __qmljs_double_to_uint32(const double &d);

void __qmljs_delete_subscript(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &base, const QV4::Value &index);
void __qmljs_delete_member(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &base, QV4::String *name);
void __qmljs_delete_name(QV4::ExecutionContext *ctx, QV4::Value *result, QV4::String *name);

void Q_NORETURN __qmljs_throw(QV4::ExecutionContext*, const QV4::Value &value);

// binary operators
typedef void (*BinOp)(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
typedef void (*BinOpContext)(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right);

void __qmljs_instanceof(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_in(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_add(ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_bit_or(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_bit_xor(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_bit_and(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_sub(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_mul(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_div(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_mod(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_shl(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_shr(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_ushr(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_gt(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_lt(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_ge(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_le(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_eq(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_ne(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_se(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);
void __qmljs_sne(QV4::Value *result, const QV4::Value &left, const QV4::Value &right);

void __qmljs_add_helper(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right);


typedef void (*InplaceBinOpName)(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_bit_and_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_bit_or_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_bit_xor_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_add_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_sub_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_mul_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_div_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_mod_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_shl_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_shr_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);
void __qmljs_inplace_ushr_name(QV4::ExecutionContext *ctx, QV4::String *name, const QV4::Value &value);

typedef void (*InplaceBinOpElement)(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_bit_and_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_bit_or_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_bit_xor_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_add_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_sub_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_mul_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_div_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_mod_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_shl_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_shr_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);
void __qmljs_inplace_ushr_element(QV4::ExecutionContext *ctx, const QV4::Value &base, const QV4::Value &index, const QV4::Value &rhs);

typedef void (*InplaceBinOpMember)(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_bit_and_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_bit_or_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_bit_xor_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_add_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_sub_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_mul_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_div_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_mod_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_shl_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_shr_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);
void __qmljs_inplace_ushr_member(QV4::ExecutionContext *ctx, const QV4::Value &base, QV4::String *name, const QV4::Value &rhs);

typedef QV4::Bool (*CmpOp)(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_gt(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_lt(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_ge(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_le(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_eq(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_ne(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_se(const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_sne(const QV4::Value &left, const QV4::Value &right);

typedef QV4::Bool (*CmpOpContext)(QV4::ExecutionContext *ctx, const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_instanceof(QV4::ExecutionContext *ctx, const QV4::Value &left, const QV4::Value &right);
QV4::Bool __qmljs_cmp_in(QV4::ExecutionContext *ctx, const QV4::Value &left, const QV4::Value &right);

// type conversion and testing
inline QV4::Value __qmljs_to_primitive(const QV4::Value &value, int typeHint)
{
    QV4::Object *o = value.asObject();
    if (!o)
        return value;
    return __qmljs_object_default_value(o, typeHint);
}

inline double __qmljs_to_number(const QV4::Value &value)
{
    switch (value.type()) {
    case QV4::Value::Undefined_Type:
        return std::numeric_limits<double>::quiet_NaN();
    case QV4::Value::Null_Type:
        return 0;
    case QV4::Value::Boolean_Type:
        return (value.booleanValue() ? 1. : 0.);
    case QV4::Value::Integer_Type:
        return value.int_32;
    case QV4::Value::String_Type:
        return __qmljs_string_to_number(value.stringValue()->toQString());
    case QV4::Value::Object_Type: {
        QV4::Value prim = __qmljs_to_primitive(value, QV4::NUMBER_HINT);
        return __qmljs_to_number(prim);
    }
    default: // double
        return value.doubleValue();
    }
}

Q_QML_EXPORT int __qmljs_value_to_int32(const QV4::Value &value);
Q_QML_EXPORT unsigned __qmljs_value_to_uint32(const QV4::Value &value);

inline QV4::Value __qmljs_to_string(const QV4::Value &value, QV4::ExecutionContext *ctx)
{
    if (value.isString())
        return value;
    return QV4::Value::fromString(__qmljs_convert_to_string(ctx, value));
}

inline QV4::Value __qmljs_to_object(QV4::ExecutionContext *ctx, const QV4::Value &value)
{
    if (value.isObject())
        return value;
    return QV4::Value::fromObject(__qmljs_convert_to_object(ctx, value));
}


inline void __qmljs_uplus(QV4::Value *result, const QV4::Value &value)
{
    TRACE1(value);

    *result = value;
    if (result->tryIntegerConversion())
        return;

    double n = __qmljs_to_number(value);
    *result = QV4::Value::fromDouble(n);
}

inline void __qmljs_uminus(QV4::Value *result, const QV4::Value &value)
{
    TRACE1(value);

    // +0 != -0, so we need to convert to double when negating 0
    if (value.isInteger() && value.integerValue())
        *result = QV4::Value::fromInt32(-value.integerValue());
    else {
        double n = __qmljs_to_number(value);
        *result = QV4::Value::fromDouble(-n);
    }
}

inline void __qmljs_compl(QV4::Value *result, const QV4::Value &value)
{
    TRACE1(value);

    int n;
    if (value.isConvertibleToInt())
        n = value.int_32;
    else
        n = QV4::Value::toInt32(__qmljs_to_number(value));

    *result = QV4::Value::fromInt32(~n);
}

inline void __qmljs_not(QV4::Value *result, const QV4::Value &value)
{
    TRACE1(value);

    bool b = value.toBoolean();
    *result = QV4::Value::fromBoolean(!b);
}

// binary operators
inline void __qmljs_bit_or(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = QV4::Value::fromInt32(left.integerValue() | right.integerValue());
        return;
    }

    int lval = QV4::Value::toInt32(__qmljs_to_number(left));
    int rval = QV4::Value::toInt32(__qmljs_to_number(right));
    *result = QV4::Value::fromInt32(lval | rval);
}

inline void __qmljs_bit_xor(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = QV4::Value::fromInt32(left.integerValue() ^ right.integerValue());
        return;
    }

    int lval = QV4::Value::toInt32(__qmljs_to_number(left));
    int rval = QV4::Value::toInt32(__qmljs_to_number(right));
    *result = QV4::Value::fromInt32(lval ^ rval);
}

inline void __qmljs_bit_and(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = QV4::Value::fromInt32(left.integerValue() & right.integerValue());
        return;
    }

    int lval = QV4::Value::toInt32(__qmljs_to_number(left));
    int rval = QV4::Value::toInt32(__qmljs_to_number(right));
    *result = QV4::Value::fromInt32(lval & rval);
}

inline void __qmljs_add(QV4::ExecutionContext *ctx, QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = add_int32(left.integerValue(), right.integerValue());
        return;
    }

    if (QV4::Value::bothDouble(left, right)) {
        *result = QV4::Value::fromDouble(left.doubleValue() + right.doubleValue());
        return;
    }

    __qmljs_add_helper(ctx, result, left, right);
}

inline void __qmljs_sub(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = sub_int32(left.integerValue(), right.integerValue());
        return;
    }

    double lval = __qmljs_to_number(left);
    double rval = __qmljs_to_number(right);
    *result = QV4::Value::fromDouble(lval - rval);
}

inline void __qmljs_mul(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = mul_int32(left.integerValue(), right.integerValue());
        return;
    }

    double lval = __qmljs_to_number(left);
    double rval = __qmljs_to_number(right);
    *result = QV4::Value::fromDouble(lval * rval);
}

inline void __qmljs_div(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    double lval = __qmljs_to_number(left);
    double rval = __qmljs_to_number(right);
    *result = QV4::Value::fromDouble(lval / rval);
}

inline void __qmljs_mod(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right) && right.integerValue() != 0) {
        int intRes = left.integerValue() % right.integerValue();
        if (intRes != 0 || left.integerValue() >= 0) {
            *result = QV4::Value::fromInt32(intRes);
            return;
        }
    }

    double lval = __qmljs_to_number(left);
    double rval = __qmljs_to_number(right);
    *result = QV4::Value::fromDouble(std::fmod(lval, rval));
}

inline void __qmljs_shl(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = QV4::Value::fromInt32(left.integerValue() << ((uint(right.integerValue()) & 0x1f)));
        return;
    }

    int lval = QV4::Value::toInt32(__qmljs_to_number(left));
    unsigned rval = QV4::Value::toUInt32(__qmljs_to_number(right)) & 0x1f;
    *result = QV4::Value::fromInt32(lval << rval);
}

inline void __qmljs_shr(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (QV4::Value::integerCompatible(left, right)) {
        *result = QV4::Value::fromInt32(left.integerValue() >> ((uint(right.integerValue()) & 0x1f)));
        return;
    }

    int lval = QV4::Value::toInt32(__qmljs_to_number(left));
    unsigned rval = QV4::Value::toUInt32(__qmljs_to_number(right)) & 0x1f;
    *result = QV4::Value::fromInt32(lval >> rval);
}

inline void __qmljs_ushr(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    uint res;
    if (QV4::Value::integerCompatible(left, right)) {
        res = uint(left.integerValue()) >> (uint(right.integerValue()) & 0x1f);
    } else {
        unsigned lval = QV4::Value::toUInt32(__qmljs_to_number(left));
        unsigned rval = QV4::Value::toUInt32(__qmljs_to_number(right)) & 0x1f;
        res = lval >> rval;
    }

    if (res > INT_MAX)
        *result = QV4::Value::fromDouble(res);
    else
        *result = QV4::Value::fromInt32(res);
}

inline void __qmljs_gt(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(__qmljs_cmp_gt(left, right));
}

inline void __qmljs_lt(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(__qmljs_cmp_lt(left, right));
}

inline void __qmljs_ge(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(__qmljs_cmp_ge(left, right));
}

inline void __qmljs_le(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(__qmljs_cmp_le(left, right));
}

inline void __qmljs_eq(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(__qmljs_cmp_eq(left, right));
}

inline void __qmljs_ne(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    *result = QV4::Value::fromBoolean(!__qmljs_cmp_eq(left, right));
}

inline void __qmljs_se(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    bool r = __qmljs_strict_equal(left, right);
    *result = QV4::Value::fromBoolean(r);
}

inline void __qmljs_sne(QV4::Value *result, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    bool r = ! __qmljs_strict_equal(left, right);
    *result = QV4::Value::fromBoolean(r);
}

inline QV4::Bool __qmljs_cmp_gt(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);
    if (QV4::Value::integerCompatible(left, right))
        return left.integerValue() > right.integerValue();

    QV4::Value l = __qmljs_to_primitive(left, QV4::NUMBER_HINT);
    QV4::Value r = __qmljs_to_primitive(right, QV4::NUMBER_HINT);

    if (QV4::Value::bothDouble(l, r)) {
        return l.doubleValue() > r.doubleValue();
    } else if (l.isString() && r.isString()) {
        return r.stringValue()->compare(l.stringValue());
    } else {
        double dl = __qmljs_to_number(l);
        double dr = __qmljs_to_number(r);
        return dl > dr;
    }
}

inline QV4::Bool __qmljs_cmp_lt(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);
    if (QV4::Value::integerCompatible(left, right))
        return left.integerValue() < right.integerValue();

    QV4::Value l = __qmljs_to_primitive(left, QV4::NUMBER_HINT);
    QV4::Value r = __qmljs_to_primitive(right, QV4::NUMBER_HINT);

    if (QV4::Value::bothDouble(l, r)) {
        return l.doubleValue() < r.doubleValue();
    } else if (l.isString() && r.isString()) {
        return l.stringValue()->compare(r.stringValue());
    } else {
        double dl = __qmljs_to_number(l);
        double dr = __qmljs_to_number(r);
        return dl < dr;
    }
}

inline QV4::Bool __qmljs_cmp_ge(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);
    if (QV4::Value::integerCompatible(left, right))
        return left.integerValue() >= right.integerValue();

    QV4::Value l = __qmljs_to_primitive(left, QV4::NUMBER_HINT);
    QV4::Value r = __qmljs_to_primitive(right, QV4::NUMBER_HINT);

    if (QV4::Value::bothDouble(l, r)) {
        return l.doubleValue() >= r.doubleValue();
    } else if (l.isString() && r.isString()) {
        return !l.stringValue()->compare(r.stringValue());
    } else {
        double dl = __qmljs_to_number(l);
        double dr = __qmljs_to_number(r);
        return dl >= dr;
    }
}

inline QV4::Bool __qmljs_cmp_le(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);
    if (QV4::Value::integerCompatible(left, right))
        return left.integerValue() <= right.integerValue();

    QV4::Value l = __qmljs_to_primitive(left, QV4::NUMBER_HINT);
    QV4::Value r = __qmljs_to_primitive(right, QV4::NUMBER_HINT);

    if (QV4::Value::bothDouble(l, r)) {
        return l.doubleValue() <= r.doubleValue();
    } else if (l.isString() && r.isString()) {
        return !r.stringValue()->compare(l.stringValue());
    } else {
        double dl = __qmljs_to_number(l);
        double dr = __qmljs_to_number(r);
        return dl <= dr;
    }
}

inline QV4::Bool __qmljs_cmp_eq(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    if (left.val == right.val)
        // NaN != NaN
        return (left.tag & QV4::Value::NotDouble_Mask) != QV4::Value::NaN_Mask;

    if (left.type() == right.type()) {
        if (left.isManaged())
            return left.managed()->isEqualTo(right.managed());
        return false;
    }

    return __qmljs_equal_helper(left, right);
}

inline QV4::Bool __qmljs_cmp_ne(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    return !__qmljs_cmp_eq(left, right);
}

inline QV4::Bool __qmljs_cmp_se(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    return __qmljs_strict_equal(left, right);
}

inline QV4::Bool __qmljs_cmp_sne(const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    return ! __qmljs_strict_equal(left, right);
}

inline QV4::Bool __qmljs_cmp_instanceof(QV4::ExecutionContext *ctx, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    QV4::Value v;
    __qmljs_instanceof(ctx, &v, left, right);
    return v.booleanValue();
}

inline uint __qmljs_cmp_in(QV4::ExecutionContext *ctx, const QV4::Value &left, const QV4::Value &right)
{
    TRACE2(left, right);

    QV4::Value v;
    __qmljs_in(ctx, &v, left, right);
    return v.booleanValue();
}

} // namespace QV4

QT_END_NAMESPACE

#endif // QMLJS_RUNTIME_H