17int Value::toUInt16()
const
19 if (integerCompatible())
20 return (ushort)(uint)integerValue();
22 double number = toNumber();
25 if ((number >= 0 && number < D16))
26 return static_cast<ushort>(number);
28 if (!std::isfinite(number))
31 double d = ::floor(::fabs(number));
32 if (std::signbit(number))
35 number = ::fmod(d , D16);
40 return (
unsigned short)number;
59double Value::toNumberImpl(Value val)
62 case QV4::Value::Undefined_Type:
63 return std::numeric_limits<
double>::quiet_NaN();
64 case QV4::Value::Managed_Type:
65 if (String *s = val.stringValue())
66 return RuntimeHelpers::stringToNumber(s->toQString());
68 Managed &m =
static_cast<Managed &>(val);
69 m.engine()->throwTypeError();
73 Q_ASSERT(val.isObject());
74 Scope scope(val.objectValue()->engine());
75 ScopedValue protectThis(scope, val);
76 ScopedValue prim(scope, RuntimeHelpers::toPrimitive(val, NUMBER_HINT));
77 if (scope.hasException())
79 return prim->toNumber();
81 case QV4::Value::Null_Type:
82 case QV4::Value::Boolean_Type:
83 case QV4::Value::Integer_Type:
92 switch (value->type()) {
93 case Value::Empty_Type:
94 Q_ASSERT(!
"empty Value encountered");
95 Q_UNREACHABLE_RETURN(QString());
96 case Value::Undefined_Type:
97 return QStringLiteral(
"undefined");
98 case Value::Null_Type:
99 return QStringLiteral(
"null");
100 case Value::Boolean_Type:
101 if (value->booleanValue())
102 return QStringLiteral(
"true");
104 return QStringLiteral(
"false");
105 case Value::Managed_Type:
106 Q_UNREACHABLE_RETURN(QString());
107 case Value::Integer_Type: {
109 RuntimeHelpers::numberToString(&str, (
double)value->int_32(), 10);
112 case Value::Double_Type: {
114 RuntimeHelpers::numberToString(&str, value->doubleValue(), 10);
119 Q_UNREACHABLE_RETURN(QString());
123QString Value::toQStringNoThrow()
const
126 if (String *s = stringValue())
127 return s->toQString();
128 if (Symbol *s = symbolValue())
129 return s->descriptiveString();
131 Q_ASSERT(isObject());
132 Scope scope(objectValue()->engine());
133 ScopedValue ex(scope);
134 bool caughtException =
false;
135 ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*
this, STRING_HINT));
136 if (scope.hasException()) {
137 ex = scope.engine->catchException();
138 caughtException =
true;
139 }
else if (prim->isPrimitive()) {
140 return prim->toQStringNoThrow();
144 if (caughtException) {
145 ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT));
146 if (scope.hasException()) {
147 ex = scope.engine->catchException();
148 }
else if (prim->isPrimitive()) {
149 return prim->toQStringNoThrow();
156 return primitiveToQString(
this);
159QString Value::toQString()
const
162 if (String *s = stringValue())
163 return s->toQString();
166 static_cast<
const Managed *>(
this)->engine()->throwTypeError();
170 Q_ASSERT(isObject());
171 Scope scope(objectValue()->engine());
172 ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*
this, STRING_HINT));
173 return prim->toQString();
176 return primitiveToQString(
this);
179QString Value::toQString(
bool *ok)
const
182 if (String *s = stringValue()) {
184 return s->toQString();
188 static_cast<
const Managed *>(
this)->engine()->throwTypeError();
193 Q_ASSERT(isObject());
194 Scope scope(objectValue()->engine());
195 ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*
this, STRING_HINT));
197 if (scope.hasException()) {
202 return prim->toQString(ok);
205 return primitiveToQString(
this);
208QV4::PropertyKey Value::toPropertyKey(ExecutionEngine *e)
const
210 if (isInteger() && int_32() >= 0)
211 return PropertyKey::fromArrayIndex(
static_cast<uint>(int_32()));
212 if (isStringOrSymbol()) {
214 ScopedStringOrSymbol s(scope,
this);
215 return s->toPropertyKey();
218 ScopedValue v(scope, RuntimeHelpers::toPrimitive(*
this, STRING_HINT));
219 if (!v->isStringOrSymbol())
222 return PropertyKey::invalid();
223 ScopedStringOrSymbol s(scope, v);
224 return s->toPropertyKey();
227bool Value::sameValue(Value other)
const {
228 if (_val == other._val)
230 String *s = stringValue();
231 String *os = other.stringValue();
233 return s->isEqualTo(os);
234 if (isInteger() && other.isDouble())
235 return int_32() ? (
double(int_32()) == other.doubleValue())
236 : (other.doubleValue() == 0 && !std::signbit(other.doubleValue()));
237 if (isDouble() && other.isInteger())
238 return other.int_32() ? (doubleValue() ==
double(other.int_32()))
239 : (doubleValue() == 0 && !std::signbit(doubleValue()));
241 return other.isManaged() && cast<Managed>()->isEqualTo(other.cast<Managed>());
245bool Value::sameValueZero(Value other)
const {
246 if (_val == other._val)
248 String *s = stringValue();
249 String *os = other.stringValue();
251 return s->isEqualTo(os);
252 if (isInteger() && other.isDouble())
253 return double(int_32()) == other.doubleValue();
254 if (isDouble() && other.isInteger())
255 return other.int_32() == doubleValue();
256 if (isDouble() && other.isDouble()) {
257 if (doubleValue() == 0 && other.doubleValue() == 0) {
262 return other.isManaged() && cast<Managed>()->isEqualTo(other.cast<Managed>());