X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..93a3786624b2768d89bfa27e46598dc64e2fb70a:/runtime/JSGlobalObjectFunctions.cpp?ds=sidebyside diff --git a/runtime/JSGlobalObjectFunctions.cpp b/runtime/JSGlobalObjectFunctions.cpp index 35507e8..0efaf84 100644 --- a/runtime/JSGlobalObjectFunctions.cpp +++ b/runtime/JSGlobalObjectFunctions.cpp @@ -1,7 +1,7 @@ /* * Copyright (C) 1999-2002 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) - * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved. * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) * Copyright (C) 2007 Maks Orlovich * @@ -27,21 +27,23 @@ #include "CallFrame.h" #include "Interpreter.h" +#include "JSFunction.h" #include "JSGlobalObject.h" #include "JSString.h" #include "JSStringBuilder.h" #include "Lexer.h" #include "LiteralParser.h" #include "Nodes.h" +#include "Operations.h" #include "Parser.h" -#include "UStringBuilder.h" -#include "dtoa.h" +#include #include #include #include #include #include #include +#include #include using namespace WTF; @@ -51,10 +53,9 @@ namespace JSC { static JSValue encode(ExecState* exec, const char* doNotEscape) { - UString str = exec->argument(0).toString(exec); - CString cstr = str.utf8(true); + CString cstr = exec->argument(0).toString(exec)->value(exec).utf8(String::StrictConversion); if (!cstr.data()) - return throwError(exec, createURIError(exec, "String contained an illegal UTF-16 sequence.")); + return throwError(exec, createURIError(exec, ASCIILiteral("String contained an illegal UTF-16 sequence."))); JSStringBuilder builder; const char* p = cstr.data(); @@ -71,30 +72,29 @@ static JSValue encode(ExecState* exec, const char* doNotEscape) return builder.build(exec); } -static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict) +template +ALWAYS_INLINE +static JSValue decode(ExecState* exec, const CharType* characters, int length, const char* doNotUnescape, bool strict) { JSStringBuilder builder; - UString str = exec->argument(0).toString(exec); int k = 0; - int len = str.length(); - const UChar* d = str.characters(); UChar u = 0; - while (k < len) { - const UChar* p = d + k; - UChar c = *p; + while (k < length) { + const CharType* p = characters + k; + CharType c = *p; if (c == '%') { int charLen = 0; - if (k <= len - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) { - const char b0 = Lexer::convertHex(p[1], p[2]); + if (k <= length - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) { + const char b0 = Lexer::convertHex(p[1], p[2]); const int sequenceLen = UTF8SequenceLength(b0); - if (sequenceLen != 0 && k <= len - sequenceLen * 3) { + if (sequenceLen && k <= length - sequenceLen * 3) { charLen = sequenceLen * 3; char sequence[5]; sequence[0] = b0; for (int i = 1; i < sequenceLen; ++i) { - const UChar* q = p + i * 3; + const CharType* q = p + i * 3; if (q[0] == '%' && isASCIIHexDigit(q[1]) && isASCIIHexDigit(q[2])) - sequence[i] = Lexer::convertHex(q[1], q[2]); + sequence[i] = Lexer::convertHex(q[1], q[2]); else { charLen = 0; break; @@ -116,19 +116,23 @@ static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict) } if (charLen == 0) { if (strict) - return throwError(exec, createURIError(exec, "URI error")); + return throwError(exec, createURIError(exec, ASCIILiteral("URI error"))); // The only case where we don't use "strict" mode is the "unescape" function. // For that, it's good to support the wonky "%u" syntax for compatibility with WinIE. - if (k <= len - 6 && p[1] == 'u' + if (k <= length - 6 && p[1] == 'u' && isASCIIHexDigit(p[2]) && isASCIIHexDigit(p[3]) && isASCIIHexDigit(p[4]) && isASCIIHexDigit(p[5])) { charLen = 6; - u = Lexer::convertUnicode(p[2], p[3], p[4], p[5]); + u = Lexer::convertUnicode(p[2], p[3], p[4], p[5]); } } if (charLen && (u == 0 || u >= 128 || !strchr(doNotUnescape, u))) { - c = u; - k += charLen - 1; + if (u < 256) + builder.append(static_cast(u)); + else + builder.append(u); + k += charLen; + continue; } } k++; @@ -137,6 +141,16 @@ static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict) return builder.build(exec); } +static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict) +{ + JSStringBuilder builder; + String str = exec->argument(0).toString(exec)->value(exec); + + if (str.is8Bit()) + return decode(exec, str.characters8(), str.length(), doNotUnescape, strict); + return decode(exec, str.characters16(), str.length(), doNotUnescape, strict); +} + bool isStrWhiteSpace(UChar c) { switch (c) { @@ -173,15 +187,15 @@ static int parseDigit(unsigned short c, int radix) return digit; } -double parseIntOverflow(const char* s, int length, int radix) +double parseIntOverflow(const LChar* s, int length, int radix) { double number = 0.0; double radixMultiplier = 1.0; - for (const char* p = s + length - 1; p >= s; p--) { - if (radixMultiplier == Inf) { + for (const LChar* p = s + length - 1; p >= s; p--) { + if (radixMultiplier == std::numeric_limits::infinity()) { if (*p != '0') { - number = Inf; + number = std::numeric_limits::infinity(); break; } } else { @@ -201,9 +215,9 @@ double parseIntOverflow(const UChar* s, int length, int radix) double radixMultiplier = 1.0; for (const UChar* p = s + length - 1; p >= s; p--) { - if (radixMultiplier == Inf) { + if (radixMultiplier == std::numeric_limits::infinity()) { if (*p != '0') { - number = Inf; + number = std::numeric_limits::infinity(); break; } } else { @@ -217,15 +231,23 @@ double parseIntOverflow(const UChar* s, int length, int radix) return number; } -static double parseInt(const UString& s, int radix) +// ES5.1 15.1.2.2 +template +ALWAYS_INLINE +static double parseInt(const String& s, const CharType* data, int radix) { + // 1. Let inputString be ToString(string). + // 2. Let S be a newly created substring of inputString consisting of the first character that is not a + // StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white + // space.) If inputString does not contain any such characters, let S be the empty string. int length = s.length(); - const UChar* data = s.characters(); int p = 0; - while (p < length && isStrWhiteSpace(data[p])) ++p; + // 3. Let sign be 1. + // 4. If S is not empty and the first character of S is a minus sign -, let sign be -1. + // 5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S. double sign = 1; if (p < length) { if (data[p] == '+') @@ -236,19 +258,33 @@ static double parseInt(const UString& s, int radix) } } + // 6. Let R = ToInt32(radix). + // 7. Let stripPrefix be true. + // 8. If R != 0,then + // b. If R != 16, let stripPrefix be false. + // 9. Else, R == 0 + // a. LetR = 10. + // 10. If stripPrefix is true, then + // a. If the length of S is at least 2 and the first two characters of S are either ―0x or ―0X, + // then remove the first two characters from S and let R = 16. + // 11. If S contains any character that is not a radix-R digit, then let Z be the substring of S + // consisting of all characters before the first such character; otherwise, let Z be S. if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) { radix = 16; p += 2; - } else if (radix == 0) { - if (p < length && data[p] == '0') - radix = 8; - else - radix = 10; - } + } else if (radix == 0) + radix = 10; + // 8.a If R < 2 or R > 36, then return NaN. if (radix < 2 || radix > 36) - return NaN; - + return QNaN; + + // 13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters + // A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant + // digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; + // and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the + // mathematical integer value that is represented by Z in radix-R notation.) + // 14. Let number be the Number value for mathInt. int firstDigitPosition = p; bool sawDigit = false; double number = 0; @@ -262,22 +298,34 @@ static double parseInt(const UString& s, int radix) ++p; } + // 12. If Z is empty, return NaN. + if (!sawDigit) + return QNaN; + + // Alternate code path for certain large numbers. if (number >= mantissaOverflowLowerBound) { - if (radix == 10) - number = WTF::strtod(s.substringSharingImpl(firstDigitPosition, p - firstDigitPosition).utf8().data(), 0); - else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) + if (radix == 10) { + size_t parsedLength; + number = parseDouble(s.characters() + firstDigitPosition, p - firstDigitPosition, parsedLength); + } else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) number = parseIntOverflow(s.substringSharingImpl(firstDigitPosition, p - firstDigitPosition).utf8().data(), p - firstDigitPosition, radix); } - if (!sawDigit) - return NaN; - + // 15. Return sign x number. return sign * number; } +static double parseInt(const String& s, int radix) +{ + if (s.is8Bit()) + return parseInt(s, s.characters8(), radix); + return parseInt(s, s.characters16(), radix); +} + static const int SizeOfInfinity = 8; -static bool isInfinity(const UChar* data, const UChar* end) +template +static bool isInfinity(const CharType* data, const CharType* end) { return (end - data) >= SizeOfInfinity && data[0] == 'I' @@ -291,11 +339,12 @@ static bool isInfinity(const UChar* data, const UChar* end) } // See ecma-262 9.3.1 -static double jsHexIntegerLiteral(const UChar*& data, const UChar* end) +template +static double jsHexIntegerLiteral(const CharType*& data, const CharType* end) { // Hex number. data += 2; - const UChar* firstDigitPosition = data; + const CharType* firstDigitPosition = data; double number = 0; while (true) { number = number * 16 + toASCIIHexValue(*data); @@ -312,24 +361,15 @@ static double jsHexIntegerLiteral(const UChar*& data, const UChar* end) } // See ecma-262 9.3.1 -static double jsStrDecimalLiteral(const UChar*& data, const UChar* end) +template +static double jsStrDecimalLiteral(const CharType*& data, const CharType* end) { - ASSERT(data < end); + RELEASE_ASSERT(data < end); - // Copy the sting into a null-terminated byte buffer, and call strtod. - Vector byteBuffer; - for (const UChar* characters = data; characters < end; ++characters) { - UChar character = *characters; - byteBuffer.append(isASCII(character) ? character : 0); - } - byteBuffer.append(0); - char* endOfNumber; - double number = WTF::strtod(byteBuffer.data(), &endOfNumber); - - // Check if strtod found a number; if so return it. - ptrdiff_t consumed = endOfNumber - byteBuffer.data(); - if (consumed) { - data += consumed; + size_t parsedLength; + double number = parseDouble(data, end - data, parsedLength); + if (parsedLength) { + data += parsedLength; return number; } @@ -338,85 +378,109 @@ static double jsStrDecimalLiteral(const UChar*& data, const UChar* end) case 'I': if (isInfinity(data, end)) { data += SizeOfInfinity; - return Inf; + return std::numeric_limits::infinity(); } break; case '+': if (isInfinity(data + 1, end)) { data += SizeOfInfinity + 1; - return Inf; + return std::numeric_limits::infinity(); } break; case '-': if (isInfinity(data + 1, end)) { data += SizeOfInfinity + 1; - return -Inf; + return -std::numeric_limits::infinity(); } break; } // Not a number. - return NaN; + return QNaN; } -// See ecma-262 9.3.1 -double jsToNumber(const UString& s) +template +static double toDouble(const CharType* characters, unsigned size) { - unsigned size = s.length(); - - if (size == 1) { - UChar c = s.characters()[0]; - if (isASCIIDigit(c)) - return c - '0'; - if (isStrWhiteSpace(c)) - return 0; - return NaN; - } - - const UChar* data = s.characters(); - const UChar* end = data + size; + const CharType* endCharacters = characters + size; // Skip leading white space. - for (; data < end; ++data) { - if (!isStrWhiteSpace(*data)) + for (; characters < endCharacters; ++characters) { + if (!isStrWhiteSpace(*characters)) break; } - + // Empty string. - if (data == end) + if (characters == endCharacters) return 0.0; - + double number; - if (data[0] == '0' && data + 2 < end && (data[1] | 0x20) == 'x' && isASCIIHexDigit(data[2])) - number = jsHexIntegerLiteral(data, end); + if (characters[0] == '0' && characters + 2 < endCharacters && (characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2])) + number = jsHexIntegerLiteral(characters, endCharacters); else - number = jsStrDecimalLiteral(data, end); - + number = jsStrDecimalLiteral(characters, endCharacters); + // Allow trailing white space. - for (; data < end; ++data) { - if (!isStrWhiteSpace(*data)) + for (; characters < endCharacters; ++characters) { + if (!isStrWhiteSpace(*characters)) break; } - if (data != end) - return NaN; - + if (characters != endCharacters) + return QNaN; + return number; } -static double parseFloat(const UString& s) +// See ecma-262 9.3.1 +double jsToNumber(const String& s) { unsigned size = s.length(); if (size == 1) { - UChar c = s.characters()[0]; + UChar c = s[0]; if (isASCIIDigit(c)) return c - '0'; - return NaN; + if (isStrWhiteSpace(c)) + return 0; + return QNaN; } - const UChar* data = s.characters(); + if (s.is8Bit()) + return toDouble(s.characters8(), size); + return toDouble(s.characters16(), size); +} + +static double parseFloat(const String& s) +{ + unsigned size = s.length(); + + if (size == 1) { + UChar c = s[0]; + if (isASCIIDigit(c)) + return c - '0'; + return QNaN; + } + + if (s.is8Bit()) { + const LChar* data = s.characters8(); + const LChar* end = data + size; + + // Skip leading white space. + for (; data < end; ++data) { + if (!isStrWhiteSpace(*data)) + break; + } + + // Empty string. + if (data == end) + return QNaN; + + return jsStrDecimalLiteral(data, end); + } + + const UChar* data = s.characters16(); const UChar* end = data + size; // Skip leading white space. @@ -427,73 +491,82 @@ static double parseFloat(const UString& s) // Empty string. if (data == end) - return NaN; + return QNaN; return jsStrDecimalLiteral(data, end); } EncodedJSValue JSC_HOST_CALL globalFuncEval(ExecState* exec) { - JSObject* thisObject = exec->hostThisValue().toThisObject(exec); - JSObject* unwrappedObject = thisObject->unwrappedObject(); - if (!unwrappedObject->isGlobalObject() || static_cast(unwrappedObject)->evalFunction() != exec->callee()) - return throwVMError(exec, createEvalError(exec, "The \"this\" value passed to eval must be the global object from which eval originated")); - JSValue x = exec->argument(0); if (!x.isString()) return JSValue::encode(x); - UString s = x.toString(exec); + String s = x.toString(exec)->value(exec); - LiteralParser preparser(exec, s.characters(), s.length(), LiteralParser::NonStrictJSON); - if (JSValue parsedObject = preparser.tryLiteralParse()) - return JSValue::encode(parsedObject); + if (s.is8Bit()) { + LiteralParser preparser(exec, s.characters8(), s.length(), NonStrictJSON); + if (JSValue parsedObject = preparser.tryLiteralParse()) + return JSValue::encode(parsedObject); + } else { + LiteralParser preparser(exec, s.characters16(), s.length(), NonStrictJSON); + if (JSValue parsedObject = preparser.tryLiteralParse()) + return JSValue::encode(parsedObject); + } - EvalExecutable* eval = EvalExecutable::create(exec, makeSource(s), false); - JSObject* error = eval->compile(exec, static_cast(unwrappedObject)->globalScopeChain()); + JSGlobalObject* calleeGlobalObject = exec->callee()->globalObject(); + EvalExecutable* eval = EvalExecutable::create(exec, exec->vm().codeCache(), makeSource(s), false); + JSObject* error = eval->compile(exec, calleeGlobalObject); if (error) return throwVMError(exec, error); - return JSValue::encode(exec->interpreter()->execute(eval, exec, thisObject, static_cast(unwrappedObject)->globalScopeChain())); + return JSValue::encode(exec->interpreter()->execute(eval, exec, calleeGlobalObject->globalThis(), calleeGlobalObject)); } EncodedJSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec) { JSValue value = exec->argument(0); - int32_t radix = exec->argument(1).toInt32(exec); - - if (radix != 0 && radix != 10) - return JSValue::encode(jsNumber(parseInt(value.toString(exec), radix))); - - if (value.isInt32()) - return JSValue::encode(value); - - if (value.isDouble()) { - double d = value.asDouble(); - if (isfinite(d)) - return JSValue::encode(jsNumber((d > 0) ? floor(d) : ceil(d))); - if (isnan(d) || isinf(d)) - return JSValue::encode(jsNaN()); - return JSValue::encode(jsNumber(0)); + JSValue radixValue = exec->argument(1); + + // Optimized handling for numbers: + // If the argument is 0 or a number in range 10^-6 <= n < INT_MAX+1, then parseInt + // results in a truncation to integer. In the case of -0, this is converted to 0. + // + // This is also a truncation for values in the range INT_MAX+1 <= n < 10^21, + // however these values cannot be trivially truncated to int since 10^21 exceeds + // even the int64_t range. Negative numbers are a little trickier, the case for + // values in the range -10^21 < n <= -1 are similar to those for integer, but + // values in the range -1 < n <= -10^-6 need to truncate to -0, not 0. + static const double tenToTheMinus6 = 0.000001; + static const double intMaxPlusOne = 2147483648.0; + if (value.isNumber()) { + double n = value.asNumber(); + if (((n < intMaxPlusOne && n >= tenToTheMinus6) || !n) && radixValue.isUndefinedOrNull()) + return JSValue::encode(jsNumber(static_cast(n))); } - return JSValue::encode(jsNumber(parseInt(value.toString(exec), radix))); + // If ToString throws, we shouldn't call ToInt32. + String s = value.toString(exec)->value(exec); + if (exec->hadException()) + return JSValue::encode(jsUndefined()); + + return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec)))); } EncodedJSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec) { - return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec)))); + return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec)->value(exec)))); } EncodedJSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec) { - return JSValue::encode(jsBoolean(isnan(exec->argument(0).toNumber(exec)))); + return JSValue::encode(jsBoolean(std::isnan(exec->argument(0).toNumber(exec)))); } EncodedJSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec) { double n = exec->argument(0).toNumber(exec); - return JSValue::encode(jsBoolean(!isnan(n) && !isinf(n))); + return JSValue::encode(jsBoolean(std::isfinite(n))); } EncodedJSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec) @@ -540,8 +613,24 @@ EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec) "*+-./@_"; JSStringBuilder builder; - UString str = exec->argument(0).toString(exec); - const UChar* c = str.characters(); + String str = exec->argument(0).toString(exec)->value(exec); + if (str.is8Bit()) { + const LChar* c = str.characters8(); + for (unsigned k = 0; k < str.length(); k++, c++) { + int u = c[0]; + if (u && strchr(do_not_escape, static_cast(u))) + builder.append(c, 1); + else { + char tmp[4]; + snprintf(tmp, sizeof(tmp), "%%%02X", u); + builder.append(tmp); + } + } + + return JSValue::encode(builder.build(exec)); + } + + const UChar* c = str.characters16(); for (unsigned k = 0; k < str.length(); k++, c++) { int u = c[0]; if (u > 255) { @@ -562,29 +651,94 @@ EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec) EncodedJSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec) { - UStringBuilder builder; - UString str = exec->argument(0).toString(exec); + StringBuilder builder; + String str = exec->argument(0).toString(exec)->value(exec); int k = 0; int len = str.length(); - while (k < len) { - const UChar* c = str.characters() + k; - UChar u; - if (c[0] == '%' && k <= len - 6 && c[1] == 'u') { - if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) { - u = Lexer::convertUnicode(c[2], c[3], c[4], c[5]); - c = &u; - k += 5; + + if (str.is8Bit()) { + const LChar* characters = str.characters8(); + LChar convertedLChar; + while (k < len) { + const LChar* c = characters + k; + if (c[0] == '%' && k <= len - 6 && c[1] == 'u') { + if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) { + builder.append(Lexer::convertUnicode(c[2], c[3], c[4], c[5])); + k += 6; + continue; + } + } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) { + convertedLChar = LChar(Lexer::convertHex(c[1], c[2])); + c = &convertedLChar; + k += 2; + } + builder.append(*c); + k++; + } + } else { + const UChar* characters = str.characters16(); + + while (k < len) { + const UChar* c = characters + k; + UChar convertedUChar; + if (c[0] == '%' && k <= len - 6 && c[1] == 'u') { + if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) { + convertedUChar = Lexer::convertUnicode(c[2], c[3], c[4], c[5]); + c = &convertedUChar; + k += 5; + } + } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) { + convertedUChar = UChar(Lexer::convertHex(c[1], c[2])); + c = &convertedUChar; + k += 2; } - } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) { - u = UChar(Lexer::convertHex(c[1], c[2])); - c = &u; - k += 2; + k++; + builder.append(*c); } - k++; - builder.append(*c); } - return JSValue::encode(jsString(exec, builder.toUString())); + return JSValue::encode(jsString(exec, builder.toString())); +} + +EncodedJSValue JSC_HOST_CALL globalFuncThrowTypeError(ExecState* exec) +{ + return throwVMTypeError(exec); +} + +EncodedJSValue JSC_HOST_CALL globalFuncProtoGetter(ExecState* exec) +{ + if (!exec->thisValue().isObject()) + return JSValue::encode(exec->thisValue().synthesizePrototype(exec)); + + JSObject* thisObject = asObject(exec->thisValue()); + if (!thisObject->allowsAccessFrom(exec->trueCallerFrame())) + return JSValue::encode(jsUndefined()); + + return JSValue::encode(thisObject->prototype()); +} + +EncodedJSValue JSC_HOST_CALL globalFuncProtoSetter(ExecState* exec) +{ + JSValue value = exec->argument(0); + + // Setting __proto__ of a primitive should have no effect. + if (!exec->thisValue().isObject()) + return JSValue::encode(jsUndefined()); + + JSObject* thisObject = asObject(exec->thisValue()); + if (!thisObject->allowsAccessFrom(exec->trueCallerFrame())) + return JSValue::encode(jsUndefined()); + + // Setting __proto__ to a non-object, non-null value is silently ignored to match Mozilla. + if (!value.isObject() && !value.isNull()) + return JSValue::encode(jsUndefined()); + + if (!thisObject->isExtensible()) + return throwVMError(exec, createTypeError(exec, StrictModeReadonlyPropertyWriteError)); + + if (!thisObject->setPrototypeWithCycleCheck(exec->vm(), value)) + throwError(exec, createError(exec, "cyclic __proto__ value")); + return JSValue::encode(jsUndefined()); } } // namespace JSC