diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-05-27 22:58:32 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-05-28 10:40:08 +0000 |
commit | 3ccd529e350fdf20ad3fb7b296220d63f8bcc747 (patch) | |
tree | c755f0c029543fbf2afd510156e1d51c28a8b28b /src/qml/jsruntime/qv4dateobject.cpp | |
parent | 23aa9d58d006dd50adb3f582df9b7dab4f64be0b (diff) |
Implement toPrimitive() the way the ES7 spec wants it
Add Dat.prototype[Symbol.toPrimitive] and make use of those
methods in the toPrimitive implementation.
Change-Id: I82a9a94dcae6822100de364373b3f4de1e3d749b
Reviewed-by: Robin Burchell <robin.burchell@crimson.no>
Diffstat (limited to 'src/qml/jsruntime/qv4dateobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4dateobject.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4dateobject.cpp b/src/qml/jsruntime/qv4dateobject.cpp index e9a2056a61..092c36e52a 100644 --- a/src/qml/jsruntime/qv4dateobject.cpp +++ b/src/qml/jsruntime/qv4dateobject.cpp @@ -44,6 +44,7 @@ #include "qv4runtime_p.h" #include "qv4string_p.h" #include "qv4jscall_p.h" +#include "qv4symbol_p.h" #include <QtCore/QDebug> #include <QtCore/QDateTime> @@ -859,6 +860,7 @@ void DatePrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(QStringLiteral("toISOString"), method_toISOString, 0); defineDefaultProperty(QStringLiteral("toJSON"), method_toJSON, 1); + defineDefaultProperty(engine->symbol_toPrimitive(), method_symbolToPrimitive, 1, Attr_ReadOnly_ButConfigurable); } double DatePrototype::getThisDate(ExecutionEngine *v4, const Value *thisObject) @@ -1515,6 +1517,22 @@ ReturnedValue DatePrototype::method_toJSON(const FunctionObject *b, const Value return toIso->call(O, nullptr, 0); } +ReturnedValue DatePrototype::method_symbolToPrimitive(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) +{ + ExecutionEngine *e = f->engine(); + if (!thisObject->isObject() || !argc || !argv->isString()) + return e->throwTypeError(); + + String *hint = argv->stringValue(); + Identifier id = hint->identifier(); + if (id == e->id_default()->identifier()) + hint = e->id_string(); + else if (id != e->id_string()->identifier() && id != e->id_number()->identifier()) + return e->throwTypeError(); + + return RuntimeHelpers::ordinaryToPrimitive(e, static_cast<const Object *>(thisObject), hint); +} + void DatePrototype::timezoneUpdated(ExecutionEngine *e) { e->localTZA = getLocalTZA(); |