From f6d0f64ee6dea7243a95319369b13096e4c5ba03 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 14 Feb 2013 23:16:50 +0100 Subject: [PATCH] cleanup __qmljs_to_boolean vs Value.toBoolean() Change-Id: Ic93eed2d4e68972d373bf1521387331ce26bac43 Reviewed-by: Simon Hausmann --- src/v4/llvm_runtime.cpp | 2 +- src/v4/moth/qv4vme_moth.cpp | 2 +- src/v4/qmljs_runtime.cpp | 6 ++++++ src/v4/qmljs_runtime.h | 26 +++----------------------- src/v4/qmljs_value.cpp | 5 ----- src/v4/qmljs_value.h | 24 +++++++++++++++++++++++- src/v4/qv4arrayobject.cpp | 6 +++--- src/v4/qv4booleanobject.cpp | 4 ++-- src/v4/qv4isel_masm.cpp | 2 +- src/v4/qv4objectproto.cpp | 6 +++--- 10 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/v4/llvm_runtime.cpp b/src/v4/llvm_runtime.cpp index 7e6f438..004a7f3 100644 --- a/src/v4/llvm_runtime.cpp +++ b/src/v4/llvm_runtime.cpp @@ -101,7 +101,7 @@ void __qmljs_llvm_init_closure(ExecutionContext *ctx, Value *result, bool __qmljs_llvm_to_boolean(ExecutionContext *ctx, const Value *value) { - return __qmljs_to_boolean(*value, ctx); + return __qmljs_to_boolean(*value); } void __qmljs_llvm_bit_and(ExecutionContext *ctx, Value *result, Value *left, Value *right) diff --git a/src/v4/moth/qv4vme_moth.cpp b/src/v4/moth/qv4vme_moth.cpp index 4fea463..290e4b5 100644 --- a/src/v4/moth/qv4vme_moth.cpp +++ b/src/v4/moth/qv4vme_moth.cpp @@ -399,7 +399,7 @@ VM::Value VME::operator()(QQmlJS::VM::ExecutionContext *context, const uchar *co MOTH_END_INSTR(Jump) MOTH_BEGIN_INSTR(CJump) - uint cond = __qmljs_to_boolean(VALUE(instr.condition), context); + uint cond = __qmljs_to_boolean(VALUE(instr.condition)); TRACE(condition, "%s", cond ? "TRUE" : "FALSE"); if (cond) code = ((uchar *)&instr.offset) + instr.offset; diff --git a/src/v4/qmljs_runtime.cpp b/src/v4/qmljs_runtime.cpp index 0b05f84..706775c 100644 --- a/src/v4/qmljs_runtime.cpp +++ b/src/v4/qmljs_runtime.cpp @@ -487,6 +487,12 @@ Value __qmljs_object_default_value(ExecutionContext *ctx, Value object, int type return Value::undefinedValue(); } +Bool __qmljs_to_boolean(const Value &value) +{ + return value.toBoolean(); +} + + Object *__qmljs_convert_to_object(ExecutionContext *ctx, const Value &value) { assert(!value.isObject()); diff --git a/src/v4/qmljs_runtime.h b/src/v4/qmljs_runtime.h index 2176d59..3001f85 100644 --- a/src/v4/qmljs_runtime.h +++ b/src/v4/qmljs_runtime.h @@ -167,7 +167,7 @@ Value __qmljs_get_thisObject(ExecutionContext *ctx); // type conversion and testing Value __qmljs_to_primitive(const Value &value, ExecutionContext *ctx, int typeHint); -Bool __qmljs_to_boolean(const Value &value, ExecutionContext *ctx); +Bool __qmljs_to_boolean(const Value &value); double __qmljs_to_number(const Value &value, ExecutionContext *ctx); Value __qmljs_to_string(const Value &value, ExecutionContext *ctx); Q_V4_EXPORT String *__qmljs_convert_to_string(ExecutionContext *ctx, const Value &value); @@ -284,26 +284,6 @@ inline Value __qmljs_to_primitive(const Value &value, ExecutionContext *ctx, int return __qmljs_default_value(value, ctx, typeHint); } -inline Bool __qmljs_to_boolean(const Value &value, ExecutionContext *ctx) -{ - switch (value.type()) { - case Value::Undefined_Type: - case Value::Null_Type: - return false; - case Value::Boolean_Type: - case Value::Integer_Type: - return (bool)value.int_32; - case Value::String_Type: - return value.stringValue()->toQString().length() > 0; - case Value::Object_Type: - return true; - default: // double - if (! value.doubleValue() || isnan(value.doubleValue())) - return false; - return true; - } -} - inline double __qmljs_to_number(const Value &value, ExecutionContext *ctx) { switch (value.type()) { @@ -387,11 +367,11 @@ inline void __qmljs_compl(ExecutionContext *ctx, Value *result, const Value &val *result = Value::fromInt32(~n); } -inline void __qmljs_not(ExecutionContext *ctx, Value *result, const Value &value) +inline void __qmljs_not(ExecutionContext *, Value *result, const Value &value) { TRACE1(value); - bool b = __qmljs_to_boolean(value, ctx); + bool b = value.toBoolean(); *result = Value::fromBoolean(!b); } diff --git a/src/v4/qmljs_value.cpp b/src/v4/qmljs_value.cpp index e1bd2a6..91f4ec8 100644 --- a/src/v4/qmljs_value.cpp +++ b/src/v4/qmljs_value.cpp @@ -74,11 +74,6 @@ int Value::toUInt16(ExecutionContext *ctx) const return (unsigned short)number; } -Bool Value::toBoolean(ExecutionContext *ctx) const -{ - return __qmljs_to_boolean(*this, ctx); -} - double Value::toInteger(ExecutionContext *ctx) const { if (isConvertibleToInt()) diff --git a/src/v4/qmljs_value.h b/src/v4/qmljs_value.h index 62ca005..56d9094 100644 --- a/src/v4/qmljs_value.h +++ b/src/v4/qmljs_value.h @@ -48,6 +48,8 @@ #include #include "qv4managed.h" +#include + QT_BEGIN_NAMESPACE namespace QQmlJS { @@ -205,7 +207,7 @@ struct Q_V4_EXPORT Value int toInt32(ExecutionContext *ctx) const; unsigned int toUInt32(ExecutionContext *ctx) const; - Bool toBoolean(ExecutionContext *ctx) const; + Bool toBoolean() const; double toInteger(ExecutionContext *ctx) const; double toNumber(ExecutionContext *ctx) const; String *toString(ExecutionContext *ctx) const; @@ -353,6 +355,26 @@ inline Value Value::fromObject(Object *o) return v; } +inline Bool Value::toBoolean() const +{ + switch (type()) { + case Value::Undefined_Type: + case Value::Null_Type: + return false; + case Value::Boolean_Type: + case Value::Integer_Type: + return (bool)int_32; + case Value::String_Type: + return stringValue()->toQString().length() > 0; + case Value::Object_Type: + return true; + default: // double + if (! doubleValue() || isnan(doubleValue())) + return false; + return true; + } +} + inline String *Value::toString(ExecutionContext *ctx) const { if (isString()) diff --git a/src/v4/qv4arrayobject.cpp b/src/v4/qv4arrayobject.cpp index 1878c15..04fbc1f 100644 --- a/src/v4/qv4arrayobject.cpp +++ b/src/v4/qv4arrayobject.cpp @@ -642,7 +642,7 @@ Value ArrayPrototype::method_every(ExecutionContext *ctx) args[1] = Value::fromDouble(k); args[2] = ctx->thisObject; Value r = callback->call(ctx, thisArg, args, 3); - ok = __qmljs_to_boolean(r, ctx); + ok = r.toBoolean(); } return Value::fromBoolean(ok); } @@ -670,7 +670,7 @@ Value ArrayPrototype::method_some(ExecutionContext *ctx) args[1] = Value::fromDouble(k); args[2] = ctx->thisObject; Value r = callback->call(ctx, thisArg, args, 3); - if (__qmljs_to_boolean(r, ctx)) + if (r.toBoolean()) return Value::fromBoolean(true); } return Value::fromBoolean(false); @@ -762,7 +762,7 @@ Value ArrayPrototype::method_filter(ExecutionContext *ctx) args[1] = Value::fromDouble(k); args[2] = ctx->thisObject; Value selected = callback->call(ctx, thisArg, args, 3); - if (__qmljs_to_boolean(selected, ctx)) { + if (selected.toBoolean()) { a->arraySet(to, v); ++to; } diff --git a/src/v4/qv4booleanobject.cpp b/src/v4/qv4booleanobject.cpp index b2a72f1..8b1af93 100644 --- a/src/v4/qv4booleanobject.cpp +++ b/src/v4/qv4booleanobject.cpp @@ -53,13 +53,13 @@ BooleanCtor::BooleanCtor(ExecutionContext *scope) Value BooleanCtor::construct(Managed *, ExecutionContext *ctx, Value *args, int argc) { - bool n = argc ? args[0].toBoolean(ctx) : false; + bool n = argc ? args[0].toBoolean() : false; return Value::fromObject(ctx->engine->newBooleanObject(Value::fromBoolean(n))); } Value BooleanCtor::call(Managed *, ExecutionContext *parentCtx, const Value &thisObject, Value *argv, int argc) { - bool value = argc ? argv[0].toBoolean(parentCtx) : 0; + bool value = argc ? argv[0].toBoolean() : 0; return Value::fromBoolean(value); } diff --git a/src/v4/qv4isel_masm.cpp b/src/v4/qv4isel_masm.cpp index 7dfd8ed..2f2c9ce 100644 --- a/src/v4/qv4isel_masm.cpp +++ b/src/v4/qv4isel_masm.cpp @@ -930,7 +930,7 @@ void InstructionSelection::visitCJump(IR::CJump *s) booleanConversion.link(_as); { - generateFunctionCall(Assembler::ReturnValueRegister, __qmljs_to_boolean, Assembler::PointerToValue(t), Assembler::ContextRegister); + generateFunctionCall(Assembler::ReturnValueRegister, __qmljs_to_boolean, Assembler::PointerToValue(t)); } testBoolean.link(_as); diff --git a/src/v4/qv4objectproto.cpp b/src/v4/qv4objectproto.cpp index 1cdf61c..21d6182 100644 --- a/src/v4/qv4objectproto.cpp +++ b/src/v4/qv4objectproto.cpp @@ -475,11 +475,11 @@ void ObjectPrototype::toPropertyDescriptor(ExecutionContext *ctx, Value v, Prope desc->enumberable = PropertyDescriptor::Undefined; if (o->__hasProperty__(ctx, ctx->engine->id_enumerable)) - desc->enumberable = __qmljs_to_boolean(o->__get__(ctx, ctx->engine->id_enumerable), ctx) ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; + desc->enumberable = o->__get__(ctx, ctx->engine->id_enumerable).toBoolean() ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; desc->configurable = PropertyDescriptor::Undefined; if (o->__hasProperty__(ctx, ctx->engine->id_configurable)) - desc->configurable = __qmljs_to_boolean(o->__get__(ctx, ctx->engine->id_configurable), ctx) ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; + desc->configurable = o->__get__(ctx, ctx->engine->id_configurable).toBoolean() ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; desc->get = 0; if (o->__hasProperty__(ctx, ctx->engine->id_get)) { @@ -513,7 +513,7 @@ void ObjectPrototype::toPropertyDescriptor(ExecutionContext *ctx, Value v, Prope if (o->__hasProperty__(ctx, ctx->engine->id_writable)) { if (desc->isAccessor()) ctx->throwTypeError(); - desc->writable = __qmljs_to_boolean(o->__get__(ctx, ctx->engine->id_writable), ctx) ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; + desc->writable = o->__get__(ctx, ctx->engine->id_writable).toBoolean() ? PropertyDescriptor::Enabled : PropertyDescriptor::Disabled; // writable forces it to be a data descriptor desc->value = Value::undefinedValue(); } -- 2.7.4