From 8e23235181eb25faf99324e136412da23dc8386f Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 29 May 2018 12:14:53 +0200 Subject: Use Symbol.toStringTag in Object.prototype.toString This should make toString comply fully with the JS Spec. Also add a couple of missing Symbol.toStringTag properties. Change-Id: I29e2018b486a0e1d174b58ce7a14f0e42cc78767 Reviewed-by: Simon Hausmann --- src/qml/jsruntime/qv4objectproto.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/qml/jsruntime/qv4objectproto.cpp') diff --git a/src/qml/jsruntime/qv4objectproto.cpp b/src/qml/jsruntime/qv4objectproto.cpp index ebf1c713fb..478e2e5915 100644 --- a/src/qml/jsruntime/qv4objectproto.cpp +++ b/src/qml/jsruntime/qv4objectproto.cpp @@ -47,6 +47,7 @@ #include "qv4objectiterator_p.h" #include "qv4string_p.h" #include "qv4jscall_p.h" +#include "qv4symbol_p.h" #include #include @@ -532,20 +533,33 @@ ReturnedValue ObjectPrototype::method_setPrototypeOf(const FunctionObject *f, co ReturnedValue ObjectPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int) { ExecutionEngine *v4 = b->engine(); + QString string; if (thisObject->isUndefined()) { - return Encode(v4->newString(QStringLiteral("[object Undefined]"))); + string = QStringLiteral("[object Undefined]"); } else if (thisObject->isNull()) { - return Encode(v4->newString(QStringLiteral("[object Null]"))); - } else if (thisObject->isBoolean()) { - return Encode(v4->newString(QStringLiteral("[object Boolean]"))); - } else if (thisObject->isNumber()) { - return Encode(v4->newString(QStringLiteral("[object Number]"))); + string = QStringLiteral("[object Null]"); } else { - Q_ASSERT(thisObject->isManaged()); - const Managed *m = static_cast(thisObject); - QString className = m->className(); - return Encode(v4->newString(QStringLiteral("[object %1]").arg(className))); + const Object *o = thisObject->as(); + if (!o) { + // primitive, get the proper prototype + if (thisObject->isBoolean()) + o = v4->booleanPrototype(); + else if (thisObject->isNumber()) + o = v4->numberPrototype(); + else if (thisObject->isString()) + o = v4->stringPrototype(); + else if (thisObject->isSymbol()) + o = v4->symbolPrototype(); + Q_ASSERT(o); + } + QString name = o->className(); + Scope scope(v4); + ScopedString toStringTag(scope, o->get(v4->symbol_toStringTag())); + if (toStringTag) + name = toStringTag->toQString(); + string = QStringLiteral("[object %1]").arg(name); } + return Encode(v4->newString(string)); } ReturnedValue ObjectPrototype::method_toLocaleString(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -- cgit v1.2.3