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#ifndef QMLJS_RUNTIME_H
4#define QMLJS_RUNTIME_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include "qv4global_p.h"
18#include "qv4value_p.h"
19#include "qv4runtimeapi_p.h"
20#include <QtCore/qnumeric.h>
21
22QT_BEGIN_NAMESPACE
23
24#undef QV4_COUNT_RUNTIME_FUNCTIONS
25
26namespace QV4 {
27
28#ifdef QV4_COUNT_RUNTIME_FUNCTIONS
29class RuntimeCounters
30{
31public:
32 RuntimeCounters();
33 ~RuntimeCounters();
34
35 static RuntimeCounters *instance;
36
37 void count(const char *func);
38 void count(const char *func, uint tag);
39 void count(const char *func, uint tag1, uint tag2);
40
41private:
42 struct Data;
43 Data *d;
44};
45
46# define TRACE0() RuntimeCounters::instance->count(Q_FUNC_INFO);
47# define TRACE1(x) RuntimeCounters::instance->count(Q_FUNC_INFO, x.type());
48# define TRACE2(x, y) RuntimeCounters::instance->count(Q_FUNC_INFO, x.type(), y.type());
49#else
50# define TRACE0()
51# define TRACE1(x)
52# define TRACE2(x, y)
53#endif // QV4_COUNT_RUNTIME_FUNCTIONS
54
55enum TypeHint {
56 PREFERREDTYPE_HINT,
57 NUMBER_HINT,
58 STRING_HINT
59};
60
61struct Q_QML_PRIVATE_EXPORT RuntimeHelpers {
62 static ReturnedValue objectDefaultValue(const Object *object, int typeHint);
63 static ReturnedValue toPrimitive(const Value &value, TypeHint typeHint);
64 static ReturnedValue ordinaryToPrimitive(ExecutionEngine *engine, const Object *object, String *typeHint);
65
66 static double stringToNumber(const QString &s);
67 static Heap::String *stringFromNumber(ExecutionEngine *engine, double number);
68 static double toNumber(const Value &value);
69 static void numberToString(QString *result, double num, int radix = 10);
70
71 static Heap::String *convertToString(ExecutionEngine *engine, Value value, TypeHint = STRING_HINT);
72 static Heap::Object *convertToObject(ExecutionEngine *engine, const Value &value);
73
74 static Bool equalHelper(const Value &x, const Value &y);
75 static Bool strictEqual(const Value &x, const Value &y);
76
77 static ReturnedValue addHelper(ExecutionEngine *engine, const Value &left, const Value &right);
78};
79
80
81// type conversion and testing
82inline ReturnedValue RuntimeHelpers::toPrimitive(const Value &value, TypeHint typeHint)
83{
84 if (!value.isObject())
85 return value.asReturnedValue();
86 return RuntimeHelpers::objectDefaultValue(object: &reinterpret_cast<const Object &>(value), typeHint);
87}
88
89inline double RuntimeHelpers::toNumber(const Value &value)
90{
91 return value.toNumber();
92}
93} // namespace QV4
94
95QT_END_NAMESPACE
96
97#endif // QMLJS_RUNTIME_H
98

source code of qtdeclarative/src/qml/jsruntime/qv4runtime_p.h