Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
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
44
45Q_STATIC_LOGGING_CATEGORY(lcCoercingTypeAssertion, "qt.qml.coercingTypeAssertion");
46
47namespace QV4 {
48
49#ifdef QV4_COUNT_RUNTIME_FUNCTIONS
50struct 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) {
99 if (cnt.isEmpty())
100 cnt.resize(64);
101 cnt[0] += 1;
102 }
103
104 inline void count(const char *func, unsigned tag) {
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) {
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;
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;
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) {
137 if (!count)
138 continue;
139 Line line;
140 line.func = it.key();
142 line.count = count;
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))
150 << " | " << line.func
151 << " | " << pretty(line.tag1)
152 << " | " << pretty(line.tag2)
153 << endl;
154 qDebug("%s", buf.data().constData());
155 }
156};
157
161 : d(new Data)
162{
163 if (!instance)
164 instance = this;
165}
166
168{
169 d->dump();
170 delete d;
171}
172
173void RuntimeCounters::count(const char *func)
174{
175 d->count(func);
176}
177
178void RuntimeCounters::count(const char *func, uint tag)
179{
180 d->count(func, tag);
181}
182
183void 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
190static QV4::Lookup *runtimeLookup(Function *f, uint i)
191{
192 return f->executableCompilationUnit()->runtimeLookups + i;
193}
194
196{
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
221 if (result->size() > 1)
222 result->insert(1, dot);
224 if (decpt > 0)
227 } else if (decpt <= 0) {
229 } else if (decpt < result->size()) {
231 } else {
233 }
234
235 if (sign && num)
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);
251
252 do {
253 char c = (char)::fmod(num, radix);
254 c = (c < 10) ? (c + '0') : (c - 10 + 'a');
256 num = ::floor(num / radix);
257 } while (num != 0);
258
259 if (frac != 0) {
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');
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)
281}
282
284{
287 Q_ASSERT(clos);
289 Scope s(engine);
291
292 if (clos->isGenerator())
294 else
296 // ### TODO: only keep reference to scripts if actually needed; see QTBUG-130795
298 if (callingQmlContext) {
299 // ### TODO: It would be more efficient to use custom Function prototypes instead of setting a property
300 // ==> QTBUG-130798
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*/
308 QV4::ScopedString name(s, engine->newString(QLatin1StringView("$importedScripts")));
311 }
312 }
313 return closure.asReturnedValue();
314}
315
329
331{
333 if (function->isStrict())
335 return Encode(false);
336 } else {
337 return Encode(true);
338 }
339}
340
347
349{
351 if (function->isStrict())
353 return Encode(false);
354 } else {
355 return Encode(true);
356 }
357}
358
359static 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
390
392{
395
396 if (scope.hasException()) {
397 // "foo instanceof valueType" must not throw an exception.
398 // So this can only be an object type.
400 return Encode::null();
401 }
402
403 if (result->toBoolean())
404 return lval.asReturnedValue();
405 else if (result->isBoolean())
406 return Encode::null();
407
409 return Encode::undefined();
410
411 // Try to convert the value type
413 if (!typeWrapper)
414 return Encode::undefined();
415
416 const auto *stackFrame = engine->currentStackFrame;
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>()) {
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
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
447{
449 if (!ro)
450 return engine->throwTypeError();
453 if (scope.hasException())
454 return Encode::undefined();
455 bool r = ro->hasProperty(s);
456 return Encode(r);
457}
458
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;
468 return qQNaN();
469
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;
482 num = s.mid(2).toLongLong(&ok, base);
483 if (!ok)
484 return qQNaN();
485 return num;
486 }
487 }
488 bool ok = false;
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
510
512{
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:
527 break;
528 }
529
533 engine->throwTypeError(QStringLiteral("Value for symbol toPrimitive is not a function"));
534 return Encode::undefined();
535 }
536
538 if (toPrimitive) {
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();
550}
551
552
554{
556
559
561 qSwap(meth1, meth2);
562 } else {
564 }
565
568
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);
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
595{
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:
606 case Value::Managed_Type:
608 if (!value.isString())
611 case Value::Integer_Type:
612 default: // double
614 }
615}
616
618{
619 redo:
620 switch (value.type()) {
621 case Value::Empty_Type:
622 Q_ASSERT(!"empty Value encountered");
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 }
642 if (value.isString())
643 return static_cast<const String &>(value).d();
644 goto redo;
645 }
646 case Value::Integer_Type:
648 default: // double
650 } // switch
651}
652
653// This is slightly different from the method above, as
654// the + operator requires a slightly different conversion
655static Heap::String *convert_to_string_add(ExecutionEngine *engine, Value value)
656{
657 return RuntimeHelpers::convertToString(engine, value, PREFERREDTYPE_HINT);
658}
659
661{
663
668 if (sleft || sright) {
669 if (!sleft) {
671 sleft = static_cast<String *>(pleft.ptr);
672 }
673 if (!sright) {
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();
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
695
712
713static Q_NEVER_INLINE ReturnedValue getElementIntFallback(ExecutionEngine *engine, const Value &object, uint idx)
714{
717
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);
726 }
727
729 QString message = QStringLiteral("Cannot read property '%1' of %2").arg(idx).arg(object.toQStringNoThrow());
731 }
732
734 Q_ASSERT(!!o); // can't fail as null/undefined is covered above
735 }
736
737 if (o->arrayData() && !o->arrayData()->attrs) {
739 if (!v->isEmpty())
740 return v->asReturnedValue();
741 }
742
743 return o->get(idx);
744}
745
746static Q_NEVER_INLINE ReturnedValue getElementFallback(ExecutionEngine *engine, const Value &object, const Value &index)
747{
749
751
753 if (!o) {
755 const QString message =
756 QStringLiteral("Cannot read property '%1' of %2")
759 }
760
762 Q_ASSERT(!!o); // can't fail as null/undefined is covered above
763 }
764
766 if (scope.hasException())
767 return Encode::undefined();
768 return o->get(name);
769}
770
772{
773 if (index.isPositiveInt()) {
774 uint idx = static_cast<uint>(index.int_32());
775 if (Heap::Base *b = object.heapObject()) {
777 Heap::Object *o = static_cast<Heap::Object *>(b);
778 if (o->arrayData && o->arrayData->type == Heap::ArrayData::Simple) {
780 if (idx < s->values.size)
781 if (!s->data(idx).isEmpty())
782 return s->data(idx).asReturnedValue();
783 }
784 }
785 }
787 }
788
790}
791
792static 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
826{
827 if (index.isPositiveInt()) {
828 uint idx = static_cast<uint>(index.int_32());
829 if (Heap::Base *b = object.heapObject()) {
831 Heap::Object *o = static_cast<Heap::Object *>(b);
832 if (o->arrayData && o->arrayData->type == Heap::ArrayData::Simple) {
834 if (idx < s->values.size) {
836 return;
837 }
838 }
839 }
840 }
841 }
842
845}
846
848{
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();
859 if (!f)
860 return engine->throwTypeError();
861 JSCallData cData(o, nullptr, 0);
863 if (engine->hasException)
864 return Encode::undefined();
865 if (!it)
866 return engine->throwTypeError();
867 return it->asReturnedValue();
868 }
870}
871
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
877
879 ScopedFunctionObject f(scope, static_cast<const Object &>(iterator).get(engine->id_next()));
880 if (!f) {
882 return Encode(true);
883 }
884 JSCallData cData(&iterator, nullptr, 0);
886 if (scope.hasException())
887 return Encode(true);
888 if (!o) {
890 return Encode(true);
891 }
892
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
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
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()
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
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
947 if (!engine->hasException)
949 return Encode(true);
950 }
951 f = t->as<FunctionObject>();
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) {
962 return Encode(true);
963 }
964
966 if (scope.hasException())
967 return Encode(true);
968 if (!o) {
970 return Encode(true);
971 }
972
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
986{
988
992 if (hadException) {
994 engine->hasException = false;
995 }
996
997 auto originalCompletion = [=]() {
998 if (hadException) {
1001 }
1002 return Encode::undefined();
1003 };
1004
1005 ScopedValue ret(scope, static_cast<const Object &>(iterator).get(engine->id_return()));
1007 if (!ret->isUndefined()) {
1009 o = f->call(&iterator, nullptr, 0);
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
1023{
1025
1028 array->arrayCreate();
1029 uint index = 0;
1032 while (1) {
1034 if (engine->hasException)
1035 return Encode::undefined();
1037 if (done->booleanValue())
1038 break;
1039 array->arraySet(index, n);
1040 ++index;
1041 }
1042 return array->asReturnedValue();
1043}
1044
1054
1065
1067{
1070
1072 if (o)
1073 return o->get(name);
1074
1075 if (object.isNullOrUndefined()) {
1076 const QString message = QStringLiteral("Cannot read property '%1' of %2")
1078 return engine->throwTypeError(message);
1079 }
1080
1082 if (!o) // type error
1083 return Encode::undefined();
1084 return o->get(name);
1085}
1086
1093
1094static 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
1171
1198
1204
1210
1212{
1214
1215 if (base.isNullOrUndefined()) {
1216 const QString message = QStringLiteral("Cannot read property '%1' of %2")
1219 return engine->throwTypeError(message);
1220 }
1221
1222 return l->getter(engine, base);
1223}
1224
1226{
1229 l->setter(engine, const_cast<Value &>(base), value);
1230}
1231
1233{
1236 if (!l->setter(engine, const_cast<Value &>(base), value))
1238}
1239
1241{
1245 // ### TODO: fix line number
1247 QStringLiteral("super() already called."), QString(), 0, 0);
1248 }
1249 } else {
1252 if (frame->thisObject() != nullptr) {
1253 // ### TODO: fix line number
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
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());
1294 return Runtime::CompareEqual::call(x, py);
1295 } else if (xo && (y.isNumber() || y.isString())) {
1296 Scope scope(xo->engine());
1298 return Runtime::CompareEqual::call(px, y);
1299 }
1300 }
1301
1302 return false;
1303}
1304
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
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);
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
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);
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
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);
1398 }
1399
1400 double dl = RuntimeHelpers::toNumber(l);
1401 double dr = RuntimeHelpers::toNumber(r);
1402 return dl >= dr;
1403}
1404
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);
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
1434{
1435 TRACE2(left, right);
1436
1439 return v->booleanValue();
1440}
1441
1443{
1444 TRACE2(left, right);
1445
1448 return v->booleanValue();
1449}
1450
1451static 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
1475
1491
1510
1529
1531{
1532 const Value *base = &baseRef;
1535 scope,
1538
1539 if (!lookupObject) {
1540 Q_ASSERT(!base->isEmpty());
1541 if (base->isNullOrUndefined()) {
1542 QString message = QStringLiteral("Cannot call method '%1' of %2")
1544 return engine->throwTypeError(message);
1545 }
1546
1547 if (base->isManaged()) {
1548 const Managed *m = static_cast<const Managed *>(base);
1551 } else {
1553 if (engine->hasException) // type error
1554 return Encode::undefined();
1557 }
1558 }
1559
1561
1562 if (!f) {
1563 QString error = QStringLiteral("Property '%1' of object %2 is not a function")
1564 .arg(name->toQString(),
1566 return engine->throwTypeError(error);
1567 }
1568
1569 return checkedResult(engine, f->call(base, argv, argc));
1570}
1571
1573{
1575
1576 if (base.isNullOrUndefined()) {
1577 const QString message = QStringLiteral("Cannot call method '%1' of %2")
1581 return engine->throwTypeError(message);
1582 }
1583
1584 // ok to have the value on the stack here
1586
1588 return checkedResult(engine, static_cast<FunctionObject &>(f).call(&base, argv, argc));
1589
1592
1593 const QString message = QStringLiteral("Property '%1' of object %2 is not a function")
1596 ->toQString(),
1598 return engine->throwTypeError(message);
1599}
1600
1602{
1603 if (!func.isFunctionObject())
1604 return engine->throwTypeError(QStringLiteral("%1 is not a function").arg(func.toQStringNoThrow()));
1606 return checkedResult(engine, static_cast<const FunctionObject &>(func).call(
1607 &undef, argv, argc));
1608}
1609
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
1619struct CallArgs {
1620 Value *argv;
1621 int argc;
1622};
1623
1624static 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
1678
1680{
1682 return engine->throwTypeError();
1683
1684 return static_cast<const FunctionObject &>(function).callAsConstructor(argv, argc, &newTarget);
1685}
1686
1699
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
1708 Value *argv = reinterpret_cast<Value *>(frame->jsFrame) + tos[StackOffsets::tailCall_argv].int_32();
1710 Q_ASSERT(argc >= 0);
1711
1713 if (!jsfo) {
1714 if (const FunctionObject *fo = function.as<FunctionObject>())
1716 return engine->throwTypeError();
1717 }
1718
1720 || unsigned(argc) > jsfo->formalParameterCount()) {
1721 // Cannot tailcall, do a normal call:
1723 }
1724
1725 memmove(frame->jsFrame->args, argv, argc * sizeof(Value));
1732 return Encode::undefined();
1733}
1734
1740
1742{
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
1781
1786
1802
1810
1817
1827
1837
1845
1852
1858
1860{
1861 auto *h = toBeMarked.heapObject();
1862 if (!h)
1863 return;
1865 auto engine = h->internalClass->engine;
1867 // runtime function is only meant to be called while gc is ongoing
1870 h->mark(ms);
1871 });
1872}
1873
1875{
1876 if (!t.isObject()) {
1877 if (t.isNullOrUndefined()) {
1879 } else {
1880 return t.toObject(engine)->asReturnedValue();
1881 }
1882 }
1883 return t.asReturnedValue();
1884}
1885
1892
1897
1899{
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
1920 for (int i = 0; i < additionalArgs; ++i) {
1924 value = args[2];
1925 if (engine->hasException)
1926 return Encode::undefined();
1928 Q_ASSERT(args[2].isInteger());
1929 int functionId = args[2].integerValue();
1932 Q_ASSERT(clos);
1933
1937 else if (arg == ObjectLiteralArgument::Setter)
1939 else
1942
1944 if (clos->isGenerator())
1946 else
1948 } else if (args[2].isFunctionObject()) {
1949 fn = static_cast<const FunctionObject &>(args[2]);
1950
1952 fn->setName(fnName);
1953 }
1957 pd->value = value;
1958 pd->set = Value::emptyValue();
1959 } else {
1960 pd->value = Value::emptyValue();
1961 pd->set = value;
1962 }
1964 if (!ok)
1965 return engine->throwTypeError();
1966
1967 args += 3;
1968 }
1969 return o.asReturnedValue();
1970}
1971
1974{
1978
1982 if (!superClass.isEmpty()) {
1983 if (superClass.isNull()) {
1984 protoParent = Encode::null();
1985 } else {
1987 // ### check that the heritage object is a constructor
1989 return engine->throwTypeError(QStringLiteral("The superclass is not a function object."));
1990 const FunctionObject *s = static_cast<const FunctionObject *>(&superClass);
1992 if (!result->isObject() && !result->isNull())
1993 return engine->throwTypeError(QStringLiteral("The value of the superclass's prototype property is not an object."));
1996 }
1997 }
1998
2002
2011
2013 if (cls->nameIndex != UINT_MAX) {
2016 }
2017
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) {
2029 return engine->throwTypeError(QStringLiteral("Cannot declare a static method named 'prototype'."));
2030 if (engine->hasException)
2031 return Encode::undefined();
2032 ++computedNames;
2033 } else {
2036 }
2038 Q_ASSERT(f);
2042 else if (methods[i].type == CompiledData::Method::Setter)
2044
2046
2047 if (f->isGenerator())
2049 else
2053 switch (methods[i].type) {
2054 case CompiledData::Method::Getter:
2058 break;
2059 case CompiledData::Method::Setter:
2063 break;
2064 default: // Regular
2068 break;
2069 }
2071 }
2072
2073 return constructor->asReturnedValue();
2074}
2075
2082
2090
2101
2109
2111{
2112 if (obj.isObject())
2113 return obj.asReturnedValue();
2114
2116}
2117
2119{
2120 return obj.toBoolean();
2121}
2122
2124{
2125 return Encode(v.toNumber());
2126}
2127
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
2155
2157{
2158 TRACE2(left, right);
2159
2162
2163 double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl();
2164 double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl();
2165
2167}
2168
2170{
2171 TRACE2(left, right);
2172
2175
2176 double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl();
2177 double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl();
2178
2180}
2181
2183{
2184 TRACE2(left, right);
2185
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();
2201}
2202
2204{
2205 TRACE2(left, right);
2206
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)
2214 }
2215
2216 double lval = RuntimeHelpers::toNumber(left);
2217 double rval = RuntimeHelpers::toNumber(right);
2218#ifdef fmod
2219# undef fmod
2220#endif
2222}
2223
2225{
2226 double b = base.toNumber();
2227 double e = exp.toNumber();
2228 return Encode(QQmlPrivate::jsExponentiate(b, e));
2229}
2230
2232{
2233 TRACE2(left, right);
2234
2235 int lval = left.toInt32();
2236 int rval = right.toInt32();
2237 return Encode((int)(lval & rval));
2238}
2239
2241{
2242 TRACE2(left, right);
2243
2244 int lval = left.toInt32();
2245 int rval = right.toInt32();
2246 return Encode((int)(lval | rval));
2247}
2248
2250{
2251 TRACE2(left, right);
2252
2253 int lval = left.toInt32();
2254 int rval = right.toInt32();
2255 return Encode((int)(lval ^ rval));
2256}
2257
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
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
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
2288{
2289 TRACE2(left, right);
2290
2292 return Encode(r);
2293}
2294
2296{
2297 TRACE2(left, right);
2298
2299 bool r = CompareLessThan::call(left, right);
2300 return Encode(r);
2301}
2302
2304{
2305 TRACE2(left, right);
2306
2308 return Encode(r);
2309}
2310
2312{
2313 TRACE2(left, right);
2314
2315 bool r = CompareLessEqual::call(left, right);
2316 return Encode(r);
2317}
2318
2320{
2321 ExecutionEngine *engine = nullptr;
2322 Value *stackMark = nullptr;
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
2340{
2341 TRACE2(left, right);
2342
2343 Value lhs = left;
2344 Value rhs = right;
2345
2347 Value *lhsGuard = nullptr;
2348 Value *rhsGuard = nullptr;
2349
2350 redo:
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);
2372 return static_cast<QV4::Managed &>(lhs).isEqualTo(&static_cast<QV4::Managed &>(rhs));
2376 goto redo;
2377 } else {
2381 goto redo;
2382 }
2383 return false;
2384 }
2385
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:
2395 Q_FALLTHROUGH();
2396 default: // double
2398 return lhs.m()->internalClass->vtable->isString ? (RuntimeHelpers::toNumber(lhs) == rhs.doubleValue()) : false;
2399 } else {
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);
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
2447{
2448 TRACE2(left, right);
2449
2450 bool r = CompareEqual::call(left, right);
2451 return Encode(r);
2452}
2453
2455{
2456 TRACE2(left, right);
2457
2458 bool r = !CompareEqual::call(left, right);
2459 return Encode(r);
2460}
2461
2463{
2464 TRACE2(left, right);
2465
2467 return Encode(r);
2468}
2469
2471{
2472 TRACE2(left, right);
2473
2475 return Encode(r);
2476}
2477
2479{
2480 TRACE2(left, right);
2481
2482 return !Runtime::CompareEqual::call(left, right);
2483}
2484
2486{
2487 TRACE2(left, right);
2488
2490}
2491
2493{
2494 TRACE2(left, right);
2495
2497}
2498
2499template<typename Operation>
2500static inline const void *symbol()
2501{
2502 return reinterpret_cast<void *>(&Operation::call);
2503}
2504
2505QHash<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
2630QT_END_NAMESPACE
Definition qjsvalue.h:24
static QV4::ReturnedValue doInstanceof(ExecutionEngine *engine, const Value &lval, const Value &rval)
static Heap::String * convert_to_string_add(ExecutionEngine *engine, Value value)
Scoped< Object > ScopedObject
static ReturnedValue throwPropertyIsNotAFunctionTypeError(ExecutionEngine *engine, Value *thisObject, const QString &propertyName)
static QV4::Lookup * runtimeLookup(Function *f, uint i)
static Q_NEVER_INLINE bool setElementFallback(ExecutionEngine *engine, const Value &object, const Value &index, const Value &value)
static Object * getSuperBase(Scope &scope)
static CallArgs createSpreadArguments(Scope &scope, Value *argv, int argc)
static const void * symbol()
Scoped< ExecutionContext > ScopedContext
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
#define TRACE1(x)
#define TRACE2(x, y)
ExecutionEngine * engine
void set(Value **scopedValue, T value, ExecutionEngine *e)