1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003-2020 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "JSCJSValue.h"
#include "BigIntObject.h"
#include "BooleanConstructor.h"
#include "CustomGetterSetter.h"
#include "GetterSetter.h"
#include "JSBigInt.h"
#include "JSCInlines.h"
#include "NumberObject.h"
#include "NumberPrototype.h"
#include "ParseInt.h"
#include "TypeError.h"
#include <wtf/text/MakeString.h>
namespace JSC {
double JSValue::toIntegerPreserveNaN(JSGlobalObject* globalObject) const
{
if (isInt32())
return asInt32();
return trunc(toNumber(globalObject));
}
uint64_t JSValue::toLength(JSGlobalObject* globalObject) const
{
// ECMA 7.1.15
// http://www.ecma-international.org/ecma-262/6.0/#sec-tolength
if (isInt32())
return static_cast<uint64_t>(std::max<int32_t>(asInt32(), 0));
double d = toIntegerOrInfinity(globalObject);
if (d <= 0)
return 0;
return static_cast<uint64_t>(std::min(d, maxSafeInteger()));
}
double JSValue::toNumberSlowCase(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(!isInt32() && !isDouble());
if (isCell())
RELEASE_AND_RETURN(scope, asCell()->toNumber(globalObject));
#if USE(BIGINT32)
if (isBigInt32()) {
throwTypeError(globalObject, scope, "Conversion from 'BigInt' to 'number' is not allowed."_s);
return 0.0;
}
#endif
if (isTrue())
return 1.0;
return isUndefined() ? PNaN : 0; // null and false both convert to 0.
}
std::optional<double> JSValue::toNumberFromPrimitive() const
{
if (isEmpty())
return std::nullopt;
if (isNumber())
return asNumber();
if (isBoolean())
return asBoolean();
if (isUndefined())
return PNaN;
if (isNull())
return 0;
return std::nullopt;
}
// https://tc39.es/ecma262/#sec-tobigint
JSValue JSValue::toBigInt(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue primitive = toPrimitive(globalObject, PreferNumber);
RETURN_IF_EXCEPTION(scope, { });
if (primitive.isBigInt())
return primitive;
if (primitive.isBoolean()) {
#if USE(BIGINT32)
return jsBigInt32(primitive.asBoolean());
#else
RELEASE_AND_RETURN(scope, JSBigInt::createFrom(globalObject, primitive.asBoolean()));
#endif
}
if (primitive.isString()) {
scope.release();
return toStringView(globalObject, primitive, [&] (StringView view) {
return JSBigInt::parseInt(globalObject, view);
});
}
ASSERT(primitive.isUndefinedOrNull() || primitive.isNumber() || primitive.isSymbol());
throwTypeError(globalObject, scope, "Invalid argument type in ToBigInt operation"_s);
return jsUndefined();
}
// https://tc39.es/ecma262/#sec-tobigint64
int64_t JSValue::toBigInt64(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = toBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, { });
return JSBigInt::toBigInt64(value);
}
// https://tc39.es/ecma262/#sec-tobiguint64
uint64_t JSValue::toBigUInt64(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = toBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, { });
return JSBigInt::toBigUInt64(value);
}
JSObject* JSValue::toObjectSlowCase(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(!isCell());
if (isInt32() || isDouble())
return constructNumber(globalObject, asValue());
if (isTrue() || isFalse())
return constructBooleanFromImmediateBoolean(globalObject, asValue());
#if USE(BIGINT32)
if (isBigInt32())
return BigIntObject::create(vm, globalObject, *this);
#endif
ASSERT(isUndefinedOrNull());
throwException(globalObject, scope, createNotAnObjectError(globalObject, *this));
return nullptr;
}
JSValue JSValue::toThisSloppySlowCase(JSGlobalObject* globalObject) const
{
if (isInt32() || isDouble())
return constructNumber(globalObject, asValue());
if (isTrue() || isFalse())
return constructBooleanFromImmediateBoolean(globalObject, asValue());
#if USE(BIGINT32)
if (isBigInt32())
return BigIntObject::create(globalObject->vm(), globalObject, *this);
#endif
ASSERT(isCell());
return toObject(globalObject);
}
JSObject* JSValue::synthesizePrototype(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (isCell()) {
if (isString())
return globalObject->stringPrototype();
if (isHeapBigInt())
return globalObject->bigIntPrototype();
ASSERT(isSymbol());
return globalObject->symbolPrototype();
}
if (isNumber())
return globalObject->numberPrototype();
if (isBoolean())
return globalObject->booleanPrototype();
#if USE(BIGINT32)
if (isBigInt32())
return globalObject->bigIntPrototype();
#endif
ASSERT(isUndefinedOrNull());
throwException(globalObject, scope, createNotAnObjectError(globalObject, *this));
return nullptr;
}
// https://tc39.es/ecma262/#sec-ordinaryset
bool JSValue::putToPrimitive(JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (std::optional<uint32_t> index = parseIndex(propertyName))
RELEASE_AND_RETURN(scope, putToPrimitiveByIndex(globalObject, index.value(), value, slot.isStrictMode()));
if (isString() && propertyName.uid() == vm.propertyNames->length.impl())
return typeError(globalObject, scope, slot.isStrictMode(), ReadonlyPropertyWriteError);
// Check if there are any setters or getters in the prototype chain
JSObject* obj = synthesizePrototype(globalObject);
EXCEPTION_ASSERT(!!scope.exception() == !obj);
if (UNLIKELY(!obj))
return false;
RELEASE_AND_RETURN(scope, obj->methodTable()->put(obj, globalObject, propertyName, value, slot));
}
bool JSValue::putToPrimitiveByIndex(JSGlobalObject* globalObject, unsigned propertyName, JSValue value, bool shouldThrow)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (propertyName > MAX_ARRAY_INDEX) {
PutPropertySlot slot(*this, shouldThrow);
return putToPrimitive(globalObject, Identifier::from(vm, propertyName), value, slot);
}
JSObject* prototype = synthesizePrototype(globalObject);
EXCEPTION_ASSERT(!!scope.exception() == !prototype);
if (UNLIKELY(!prototype))
return false;
bool putResult = false;
bool success = prototype->attemptToInterceptPutByIndexOnHoleForPrototype(globalObject, *this, propertyName, value, shouldThrow, putResult);
RETURN_IF_EXCEPTION(scope, false);
if (success)
return putResult;
return typeError(globalObject, scope, shouldThrow, ReadonlyPropertyWriteError);
}
void JSValue::dump(PrintStream& out) const
{
dumpInContext(out, nullptr);
}
void JSValue::dumpInContext(PrintStream& out, DumpContext* context) const
{
dumpInContextAssumingStructure(
out, context, (!!*this && isCell()) ? asCell()->structure() : nullptr);
}
void JSValue::dumpInContextAssumingStructure(
PrintStream& out, DumpContext* context, Structure* structure) const
{
if (!*this)
out.print("<JSValue()>");
else if (isInt32())
out.printf("Int32: %d", asInt32());
else if (isDouble()) {
#if USE(JSVALUE64)
out.printf("Double: %lld, %lf", (long long)reinterpretDoubleToInt64(asDouble()), asDouble());
#else
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
union {
double asDouble;
uint32_t asTwoInt32s[2];
} u;
u.asDouble = asDouble();
out.printf("Double: %08x:%08x, %lf", u.asTwoInt32s[1], u.asTwoInt32s[0], asDouble());
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif
} else if (isCell()) {
if (structure->classInfoForCells()->isSubClassOf(JSString::info())) {
JSString* string = asString(asCell());
out.print("String");
if (string->isRope())
out.print(" (rope)");
const StringImpl* impl = string->tryGetValueImpl();
if (impl) {
if (impl->isAtom())
out.print(" (atomic)");
if (impl->isSymbol())
out.print(" (symbol)");
} else
out.print(" (unresolved)");
if (string->is8Bit())
out.print(",8Bit:(1)");
else
out.print(",8Bit:(0)");
out.print(",length:(", string->length(), ")");
out.print(": ", impl);
} else if (structure->classInfoForCells()->isSubClassOf(RegExp::info()))
out.print("RegExp: ", *jsCast<RegExp*>(asCell()));
else if (structure->classInfoForCells()->isSubClassOf(Symbol::info()))
out.print("Symbol: ", RawPointer(asCell()));
else if (structure->classInfoForCells()->isSubClassOf(Structure::info()))
out.print("Structure: ", inContext(*jsCast<Structure*>(asCell()), context));
else if (isHeapBigInt())
out.print("BigInt[heap-allocated]: addr=", RawPointer(asCell()), ", length=", jsCast<JSBigInt*>(asCell())->length(), ", sign=", jsCast<JSBigInt*>(asCell())->sign());
else if (structure->classInfoForCells()->isSubClassOf(JSObject::info())) {
out.print("Object: ", RawPointer(asCell()));
out.print(" with butterfly ", RawPointer(asObject(asCell())->butterfly()), "(base=", RawPointer(asObject(asCell())->butterfly()->base(structure)), ")");
out.print(" (Structure ", inContext(*structure, context), ")");
} else {
out.print("Cell: ", RawPointer(asCell()));
out.print(" (", inContext(*structure, context), ")");
}
#if USE(JSVALUE64)
out.print(", StructureID: ", asCell()->structureID().bits());
#endif
} else if (isTrue())
out.print("True");
else if (isFalse())
out.print("False");
else if (isNull())
out.print("Null");
else if (isUndefined())
out.print("Undefined");
#if USE(BIGINT32)
else if (isBigInt32())
out.printf("BigInt[inline]: %d", bigInt32AsInt32());
#endif
else
out.print("INVALID");
}
void JSValue::dumpForBacktrace(PrintStream& out) const
{
if (!*this)
out.print("<JSValue()>");
else if (isInt32())
out.printf("%d", asInt32());
else if (isDouble())
out.printf("%lf", asDouble());
else if (isCell()) {
if (asCell()->inherits<JSString>()) {
JSString* string = asString(asCell());
const StringImpl* impl = string->tryGetValueImpl();
if (impl)
out.print("\"", impl, "\"");
else
out.print("(unresolved string)");
} else if (asCell()->inherits<Structure>()) {
out.print("Structure[ ", asCell()->structure()->classInfoForCells()->className);
out.print("]: ", RawPointer(asCell()));
} else {
out.print("Cell[", asCell()->structure()->classInfoForCells()->className);
out.print("]: ", RawPointer(asCell()));
}
} else if (isTrue())
out.print("True");
else if (isFalse())
out.print("False");
else if (isNull())
out.print("Null");
else if (isUndefined())
out.print("Undefined");
#if USE(BIGINT32)
else if (isBigInt32())
out.printf("BigInt[inline]: %d", bigInt32AsInt32());
#endif
else
out.print("INVALID");
}
JSString* JSValue::toStringSlowCase(JSGlobalObject* globalObject, bool returnEmptyStringOnError) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto errorValue = [&] () -> JSString* {
if (returnEmptyStringOnError)
return jsEmptyString(vm);
return nullptr;
};
ASSERT(!isString());
if (isInt32())
return int32ToString(vm, asInt32(), 10);
if (isDouble())
return JSC::numberToString(vm, asDouble(), 10);
if (isTrue())
return vm.smallStrings.trueString();
if (isFalse())
return vm.smallStrings.falseString();
if (isNull())
return vm.smallStrings.nullString();
if (isUndefined())
return vm.smallStrings.undefinedString();
#if USE(BIGINT32)
if (isBigInt32())
return int32ToString(vm, bigInt32AsInt32(), 10);
#endif
JSString* returnString = asCell()->toStringInline(globalObject);
RETURN_IF_EXCEPTION(scope, errorValue());
return returnString;
}
String JSValue::toWTFStringSlowCase(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (isInt32())
return vm.numericStrings.add(asInt32());
if (isDouble())
return vm.numericStrings.add(asDouble());
if (isTrue())
return vm.propertyNames->trueKeyword.string();
if (isFalse())
return vm.propertyNames->falseKeyword.string();
if (isNull())
return vm.propertyNames->nullKeyword.string();
if (isUndefined())
return vm.propertyNames->undefinedKeyword.string();
JSString* string = toString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
RELEASE_AND_RETURN(scope, string->value(globalObject));
}
WTF::String JSValue::toWTFStringForConsole(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSString* string = toString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
auto result = string->value(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (isString())
return tryMakeString('"', result.data, '"');
if (jsDynamicCast<JSArray*>(*this))
return tryMakeString('[', result.data, ']');
if (jsDynamicCast<JSBigInt*>(*this))
return tryMakeString(result.data, 'n');
return result;
}
} // namespace JSC
|