From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/JavaScriptCore/runtime/JSONObject.cpp | 464 ++++++++++++++------------- 1 file changed, 242 insertions(+), 222 deletions(-) (limited to 'Source/JavaScriptCore/runtime/JSONObject.cpp') diff --git a/Source/JavaScriptCore/runtime/JSONObject.cpp b/Source/JavaScriptCore/runtime/JSONObject.cpp index 982628917..0dbcd8f9a 100644 --- a/Source/JavaScriptCore/runtime/JSONObject.cpp +++ b/Source/JavaScriptCore/runtime/JSONObject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Apple Inc. All rights reserved. + * Copyright (C) 2009, 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -26,6 +26,7 @@ #include "config.h" #include "JSONObject.h" +#include "ArrayConstructor.h" #include "BooleanObject.h" #include "Error.h" #include "ExceptionHelpers.h" @@ -36,7 +37,7 @@ #include "LocalScope.h" #include "Lookup.h" #include "ObjectConstructor.h" -#include "Operations.h" +#include "JSCInlines.h" #include "PropertyNameArray.h" #include #include @@ -45,8 +46,8 @@ namespace JSC { STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSONObject); -static EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*); -static EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*); +EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*); +EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*); } @@ -62,7 +63,9 @@ JSONObject::JSONObject(VM& vm, Structure* structure) void JSONObject::finishCreation(VM& vm) { Base::finishCreation(vm); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); + + putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "JSON"), DontEnum | ReadOnly); } // PropertyNameForFunctionCall objects must be on the stack, since the JSValue that they create is not marked. @@ -90,16 +93,19 @@ public: private: class Holder { public: - Holder(VM&, JSObject*); + enum RootHolderTag { RootHolder }; + Holder(VM&, ExecState*, JSObject*); + Holder(RootHolderTag, VM&, JSObject*); JSObject* object() const { return m_object.get(); } + bool isArray() const { return m_isArray; } bool appendNextProperty(Stringifier&, StringBuilder&); private: Local m_object; const bool m_isArray; - bool m_isJSArray; + const bool m_isJSArray; unsigned m_index; unsigned m_size; RefPtr m_propertyNames; @@ -107,12 +113,11 @@ private: friend class Holder; - static void appendQuotedString(StringBuilder&, const String&); - JSValue toJSON(JSValue, const PropertyNameForFunctionCall&); + JSValue toJSONImpl(JSValue value, JSValue toJSONFunction, const PropertyNameForFunctionCall&); - enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedValue }; - StringifyResult appendStringifiedValue(StringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&); + enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedOrSymbolValue }; + StringifyResult appendStringifiedValue(StringBuilder&, JSValue, const Holder&, const PropertyNameForFunctionCall&); bool willIndent() const; void indent(); @@ -125,7 +130,7 @@ private: PropertyNameArray m_arrayReplacerPropertyNames; CallType m_replacerCallType; CallData m_replacerCallData; - const String m_gap; + String m_gap; Vector m_holderStack; String m_repeatedGap; @@ -136,22 +141,30 @@ private: static inline JSValue unwrapBoxedPrimitive(ExecState* exec, JSValue value) { + VM& vm = exec->vm(); if (!value.isObject()) return value; JSObject* object = asObject(value); - if (object->inherits(NumberObject::info())) + if (object->inherits(vm, NumberObject::info())) return jsNumber(object->toNumber(exec)); - if (object->inherits(StringObject::info())) + if (object->inherits(vm, StringObject::info())) return object->toString(exec); - if (object->inherits(BooleanObject::info())) + if (object->inherits(vm, BooleanObject::info())) return object->toPrimitive(exec); + + // Do not unwrap SymbolObject to Symbol. It is not performed in the spec. + // http://www.ecma-international.org/ecma-262/6.0/#sec-serializejsonproperty return value; } static inline String gap(ExecState* exec, JSValue space) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + const unsigned maxGapLength = 10; space = unwrapBoxedPrimitive(exec, space); + RETURN_IF_EXCEPTION(scope, { }); // If the space value is a number, create a gap string with that number of spaces. if (space.isNumber()) { @@ -195,8 +208,12 @@ JSValue PropertyNameForFunctionCall::value(ExecState* exec) const if (!m_value) { if (m_identifier) m_value = jsString(exec, m_identifier->string()); - else - m_value = jsNumber(m_number); + else { + VM& vm = exec->vm(); + if (m_number <= 9) + return vm.smallStrings.singleCharacterString(m_number + '0'); + m_value = jsNontrivialString(&vm, vm.numericStrings.add(m_number)); + } } return m_value; } @@ -207,28 +224,37 @@ Stringifier::Stringifier(ExecState* exec, const Local& replacer, const : m_exec(exec) , m_replacer(replacer) , m_usingArrayReplacer(false) - , m_arrayReplacerPropertyNames(exec) - , m_replacerCallType(CallTypeNone) - , m_gap(gap(exec, space.get())) + , m_arrayReplacerPropertyNames(exec, PropertyNameMode::Strings) + , m_replacerCallType(CallType::None) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + m_gap = gap(exec, space.get()); + if (UNLIKELY(scope.exception())) + return; + if (!m_replacer.isObject()) return; - if (m_replacer.asObject()->inherits(JSArray::info())) { + if (m_replacer.asObject()->inherits(vm, JSArray::info())) { m_usingArrayReplacer = true; Handle array = m_replacer.asObject(); - unsigned length = array->get(exec, exec->vm().propertyNames->length).toUInt32(exec); + unsigned length = array->get(exec, vm.propertyNames->length).toUInt32(exec); + if (UNLIKELY(scope.exception())) + return; for (unsigned i = 0; i < length; ++i) { JSValue name = array->get(exec, i); - if (exec->hadException()) + if (UNLIKELY(scope.exception())) break; if (name.isObject()) { - if (!asObject(name)->inherits(NumberObject::info()) && !asObject(name)->inherits(StringObject::info())) + if (!asObject(name)->inherits(vm, NumberObject::info()) && !asObject(name)->inherits(vm, StringObject::info())) continue; - } + } else if (!name.isNumber() && !name.isString()) + continue; - m_arrayReplacerPropertyNames.add(Identifier(exec, name.toString(exec)->value(exec))); + m_arrayReplacerPropertyNames.add(name.toString(exec)->toIdentifier(exec)); } return; } @@ -238,136 +264,78 @@ Stringifier::Stringifier(ExecState* exec, const Local& replacer, const Local Stringifier::stringify(Handle value) { + VM& vm = m_exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); JSObject* object = constructEmptyObject(m_exec); - if (m_exec->hadException()) - return Local(m_exec->vm(), jsNull()); + RETURN_IF_EXCEPTION(scope, Local(vm, jsNull())); - PropertyNameForFunctionCall emptyPropertyName(m_exec->vm().propertyNames->emptyIdentifier); - object->putDirect(m_exec->vm(), m_exec->vm().propertyNames->emptyIdentifier, value.get()); + PropertyNameForFunctionCall emptyPropertyName(vm.propertyNames->emptyIdentifier); + object->putDirect(vm, vm.propertyNames->emptyIdentifier, value.get()); StringBuilder result; - if (appendStringifiedValue(result, value.get(), object, emptyPropertyName) != StringifySucceeded) - return Local(m_exec->vm(), jsUndefined()); - if (m_exec->hadException()) - return Local(m_exec->vm(), jsNull()); - - return Local(m_exec->vm(), jsString(m_exec, result.toString())); + Holder root(Holder::RootHolder, vm, object); + auto stringifyResult = appendStringifiedValue(result, value.get(), root, emptyPropertyName); + ASSERT(!scope.exception() || (stringifyResult != StringifySucceeded)); + if (UNLIKELY(stringifyResult != StringifySucceeded)) + return Local(vm, jsUndefined()); + + scope.release(); + return Local(vm, jsString(m_exec, result.toString())); } -template -static void appendStringToStringBuilder(StringBuilder& builder, const CharType* data, int length) +ALWAYS_INLINE JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName) { - for (int i = 0; i < length; ++i) { - int start = i; - while (i < length && (data[i] > 0x1F && data[i] != '"' && data[i] != '\\')) - ++i; - builder.append(data + start, i - start); - if (i >= length) - break; - switch (data[i]) { - case '\t': - builder.append('\\'); - builder.append('t'); - break; - case '\r': - builder.append('\\'); - builder.append('r'); - break; - case '\n': - builder.append('\\'); - builder.append('n'); - break; - case '\f': - builder.append('\\'); - builder.append('f'); - break; - case '\b': - builder.append('\\'); - builder.append('b'); - break; - case '"': - builder.append('\\'); - builder.append('"'); - break; - case '\\': - builder.append('\\'); - builder.append('\\'); - break; - default: - static const char hexDigits[] = "0123456789abcdef"; - UChar ch = data[i]; - LChar hex[] = { '\\', 'u', static_cast(hexDigits[(ch >> 12) & 0xF]), static_cast(hexDigits[(ch >> 8) & 0xF]), static_cast(hexDigits[(ch >> 4) & 0xF]), static_cast(hexDigits[ch & 0xF]) }; - builder.append(hex, WTF_ARRAY_LENGTH(hex)); - break; - } - } -} - -void escapeStringToBuilder(StringBuilder& builder, const String& message) -{ - if (message.is8Bit()) - appendStringToStringBuilder(builder, message.characters8(), message.length()); - else - appendStringToStringBuilder(builder, message.characters16(), message.length()); -} - -void Stringifier::appendQuotedString(StringBuilder& builder, const String& value) -{ - int length = value.length(); - - builder.append('"'); - - if (value.is8Bit()) - appendStringToStringBuilder(builder, value.characters8(), length); - else - appendStringToStringBuilder(builder, value.characters16(), length); + VM& vm = m_exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + ASSERT(!scope.exception()); + if (!value.isObject()) + return value; + + JSObject* object = asObject(value); + PropertySlot slot(object, PropertySlot::InternalMethodType::Get); + bool hasProperty = object->getPropertySlot(m_exec, vm.propertyNames->toJSON, slot); + ASSERT(!scope.exception() || !hasProperty); + if (!hasProperty) + return value; - builder.append('"'); + JSValue toJSONFunction = slot.getValue(m_exec, vm.propertyNames->toJSON); + RETURN_IF_EXCEPTION(scope, { }); + scope.release(); + return toJSONImpl(value, toJSONFunction, propertyName); } -inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName) +JSValue Stringifier::toJSONImpl(JSValue value, JSValue toJSONFunction, const PropertyNameForFunctionCall& propertyName) { - ASSERT(!m_exec->hadException()); - if (!value.isObject() || !asObject(value)->hasProperty(m_exec, m_exec->vm().propertyNames->toJSON)) - return value; - - JSValue toJSONFunction = asObject(value)->get(m_exec, m_exec->vm().propertyNames->toJSON); - if (m_exec->hadException()) - return jsNull(); - - if (!toJSONFunction.isObject()) - return value; - - JSObject* object = asObject(toJSONFunction); + CallType callType; CallData callData; - CallType callType = object->methodTable()->getCallData(object, callData); - if (callType == CallTypeNone) + if (!toJSONFunction.isCallable(callType, callData)) return value; MarkedArgumentBuffer args; args.append(propertyName.value(m_exec)); - return call(m_exec, object, callType, callData, value, args); + return call(m_exec, asObject(toJSONFunction), callType, callData, value, args); } -Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName) +Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& builder, JSValue value, const Holder& holder, const PropertyNameForFunctionCall& propertyName) { + VM& vm = m_exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + // Call the toJSON function. value = toJSON(value, propertyName); - if (m_exec->hadException()) - return StringifyFailed; + RETURN_IF_EXCEPTION(scope, StringifyFailed); // Call the replacer function. - if (m_replacerCallType != CallTypeNone) { + if (m_replacerCallType != CallType::None) { MarkedArgumentBuffer args; args.append(propertyName.value(m_exec)); args.append(value); - value = call(m_exec, m_replacer.get(), m_replacerCallType, m_replacerCallData, holder, args); - if (m_exec->hadException()) - return StringifyFailed; + value = call(m_exec, m_replacer.get(), m_replacerCallType, m_replacerCallData, holder.object(), args); + RETURN_IF_EXCEPTION(scope, StringifyFailed); } - if (value.isUndefined() && !holder->inherits(JSArray::info())) - return StringifyFailedDueToUndefinedValue; + if ((value.isUndefined() || value.isSymbol()) && !holder.isArray()) + return StringifyFailedDueToUndefinedOrSymbolValue; if (value.isNull()) { builder.appendLiteral("null"); @@ -376,8 +344,7 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& value = unwrapBoxedPrimitive(m_exec, value); - if (m_exec->hadException()) - return StringifyFailed; + RETURN_IF_EXCEPTION(scope, StringifyFailed); if (value.isBoolean()) { if (value.isTrue()) @@ -387,18 +354,21 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& return StringifySucceeded; } - String stringValue; - if (value.getString(m_exec, stringValue)) { - appendQuotedString(builder, stringValue); + if (value.isString()) { + builder.appendQuotedJSONString(asString(value)->value(m_exec)); return StringifySucceeded; } if (value.isNumber()) { - double number = value.asNumber(); - if (!std::isfinite(number)) - builder.appendLiteral("null"); - else - builder.append(String::numberToStringECMAScript(number)); + if (value.isInt32()) + builder.appendNumber(value.asInt32()); + else { + double number = value.asNumber(); + if (!std::isfinite(number)) + builder.appendLiteral("null"); + else + builder.appendECMAScriptNumber(number); + } return StringifySucceeded; } @@ -408,31 +378,32 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& JSObject* object = asObject(value); CallData callData; - if (object->methodTable()->getCallData(object, callData) != CallTypeNone) { - if (holder->inherits(JSArray::info())) { + if (object->methodTable()->getCallData(object, callData) != CallType::None) { + if (holder.isArray()) { builder.appendLiteral("null"); return StringifySucceeded; } - return StringifyFailedDueToUndefinedValue; + return StringifyFailedDueToUndefinedOrSymbolValue; } // Handle cycle detection, and put the holder on the stack. for (unsigned i = 0; i < m_holderStack.size(); i++) { if (m_holderStack[i].object() == object) { - m_exec->vm().throwException(m_exec, createTypeError(m_exec, ASCIILiteral("JSON.stringify cannot serialize cyclic structures."))); + throwTypeError(m_exec, scope, ASCIILiteral("JSON.stringify cannot serialize cyclic structures.")); return StringifyFailed; } } + bool holderStackWasEmpty = m_holderStack.isEmpty(); - m_holderStack.append(Holder(m_exec->vm(), object)); + m_holderStack.append(Holder(vm, m_exec, object)); + RETURN_IF_EXCEPTION(scope, StringifyFailed); if (!holderStackWasEmpty) return StringifySucceeded; do { - while (m_holderStack.last().appendNextProperty(*this, builder)) { - if (m_exec->hadException()) - return StringifyFailed; - } + while (m_holderStack.last().appendNextProperty(*this, builder)) + RETURN_IF_EXCEPTION(scope, StringifyFailed); + RETURN_IF_EXCEPTION(scope, StringifyFailed); m_holderStack.removeLast(); } while (!m_holderStack.isEmpty()); return StringifySucceeded; @@ -467,9 +438,21 @@ inline void Stringifier::startNewLine(StringBuilder& builder) const builder.append(m_indent); } -inline Stringifier::Holder::Holder(VM& vm, JSObject* object) +inline Stringifier::Holder::Holder(VM& vm, ExecState* exec, JSObject* object) + : m_object(vm, object) + , m_isArray(JSC::isArray(exec, object)) + , m_isJSArray(m_isArray && isJSArray(object)) + , m_index(0) +#ifndef NDEBUG + , m_size(0) +#endif +{ +} + +inline Stringifier::Holder::Holder(RootHolderTag, VM& vm, JSObject* object) : m_object(vm, object) - , m_isArray(object->inherits(JSArray::info())) + , m_isArray(false) + , m_isJSArray(false) , m_index(0) #ifndef NDEBUG , m_size(0) @@ -482,19 +465,28 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui ASSERT(m_index <= m_size); ExecState* exec = stringifier.m_exec; + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); // First time through, initialize. if (!m_index) { if (m_isArray) { - m_isJSArray = isJSArray(m_object.get()); - m_size = m_object->get(exec, exec->vm().propertyNames->length).toUInt32(exec); + if (m_isJSArray) + m_size = asArray(m_object.get())->length(); + else { + JSValue value = m_object->get(exec, vm.propertyNames->length); + RETURN_IF_EXCEPTION(scope, false); + m_size = value.toUInt32(exec); + RETURN_IF_EXCEPTION(scope, false); + } builder.append('['); } else { if (stringifier.m_usingArrayReplacer) m_propertyNames = stringifier.m_arrayReplacerPropertyNames.data(); else { - PropertyNameArray objectPropertyNames(exec); - m_object->methodTable()->getOwnPropertyNames(m_object.get(), exec, objectPropertyNames, ExcludeDontEnumProperties); + PropertyNameArray objectPropertyNames(exec, PropertyNameMode::Strings); + m_object->methodTable()->getOwnPropertyNames(m_object.get(), exec, objectPropertyNames, EnumerationMode()); + RETURN_IF_EXCEPTION(scope, false); m_propertyNames = objectPropertyNames.releaseData(); } m_size = m_propertyNames->propertyNameVector().size(); @@ -522,13 +514,12 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui if (m_isJSArray && asArray(m_object.get())->canGetIndexQuickly(index)) value = asArray(m_object.get())->getIndexQuickly(index); else { - PropertySlot slot(m_object.get()); - if (m_object->methodTable()->getOwnPropertySlotByIndex(m_object.get(), exec, index, slot)) { + PropertySlot slot(m_object.get(), PropertySlot::InternalMethodType::Get); + if (m_object->methodTable()->getOwnPropertySlotByIndex(m_object.get(), exec, index, slot)) value = slot.getValue(exec, index); - if (exec->hadException()) - return false; - } else + else value = jsUndefined(); + RETURN_IF_EXCEPTION(scope, false); } // Append the separator string. @@ -537,16 +528,16 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui stringifier.startNewLine(builder); // Append the stringified value. - stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), index); + stringifyResult = stringifier.appendStringifiedValue(builder, value, *this, index); + ASSERT(stringifyResult != StringifyFailedDueToUndefinedOrSymbolValue); } else { // Get the value. - PropertySlot slot(m_object.get()); + PropertySlot slot(m_object.get(), PropertySlot::InternalMethodType::Get); Identifier& propertyName = m_propertyNames->propertyNameVector()[index]; if (!m_object->methodTable()->getOwnPropertySlot(m_object.get(), exec, propertyName, slot)) return true; JSValue value = slot.getValue(exec, propertyName); - if (exec->hadException()) - return false; + RETURN_IF_EXCEPTION(scope, false); rollBackPoint = builder.length(); @@ -556,14 +547,15 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui stringifier.startNewLine(builder); // Append the property name. - appendQuotedString(builder, propertyName.string()); + builder.appendQuotedJSONString(propertyName.string()); builder.append(':'); if (stringifier.willIndent()) builder.append(' '); // Append the stringified value. - stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), propertyName); + stringifyResult = stringifier.appendStringifiedValue(builder, value, *this, propertyName); } + RETURN_IF_EXCEPTION(scope, false); // From this point on, no access to the this pointer or to any members, because the // Holder object may have moved if the call to stringify pushed a new Holder onto @@ -575,10 +567,10 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui break; case StringifySucceeded: break; - case StringifyFailedDueToUndefinedValue: - // This only occurs when get an undefined value for an object property. - // In this case we don't want the separator and property name that we - // already appended, so roll back. + case StringifyFailedDueToUndefinedOrSymbolValue: + // This only occurs when get an undefined value or a symbol value for + // an object property. In this case we don't want the separator and + // property name that we already appended, so roll back. builder.resize(rollBackPoint); break; } @@ -588,7 +580,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui // ------------------------------ JSONObject -------------------------------- -const ClassInfo JSONObject::s_info = { "JSON", &JSNonFinalObject::s_info, 0, ExecState::jsonTable, CREATE_METHOD_TABLE(JSONObject) }; +const ClassInfo JSONObject::s_info = { "JSON", &JSNonFinalObject::s_info, &jsonTable, CREATE_METHOD_TABLE(JSONObject) }; /* Source for JSONObject.lut.h @begin jsonTable @@ -599,11 +591,6 @@ const ClassInfo JSONObject::s_info = { "JSON", &JSNonFinalObject::s_info, 0, Exe // ECMA 15.8 -bool JSONObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot) -{ - return getStaticFunctionSlot(exec, ExecState::jsonTable(exec->vm()), jsCast(object), propertyName, slot); -} - class Walker { public: Walker(ExecState* exec, Handle function, CallType callType, CallData callData) @@ -637,10 +624,14 @@ enum WalkerState { StateUnknown, ArrayStartState, ArrayStartVisitMember, ArrayEn ObjectStartState, ObjectStartVisitMember, ObjectEndVisitMember }; NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) { + VM& vm = m_exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + Vector propertyStack; Vector indexStack; - LocalStack objectStack(m_exec->vm()); - LocalStack arrayStack(m_exec->vm()); + LocalStack objectStack(vm); + LocalStack arrayStack(vm); + Vector arrayLengthStack; Vector stateStack; WalkerState state = StateUnknown; @@ -652,12 +643,13 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) arrayStartState: case ArrayStartState: { ASSERT(inValue.isObject()); - ASSERT(isJSArray(asObject(inValue)) || asObject(inValue)->inherits(JSArray::info())); + ASSERT(isJSArray(asObject(inValue)) || asObject(inValue)->inherits(vm, JSArray::info())); if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) - return m_exec->vm().throwException(m_exec, createStackOverflowError(m_exec)); + return throwStackOverflowError(m_exec, scope); JSArray* array = asArray(inValue); arrayStack.push(array); + arrayLengthStack.append(array->length()); indexStack.append(0); } arrayStartVisitMember: @@ -665,20 +657,23 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) case ArrayStartVisitMember: { JSArray* array = arrayStack.peek(); uint32_t index = indexStack.last(); - if (index == array->length()) { + unsigned arrayLength = arrayLengthStack.last(); + if (index == arrayLength) { outValue = array; arrayStack.pop(); + arrayLengthStack.removeLast(); indexStack.removeLast(); break; } if (isJSArray(array) && array->canGetIndexQuickly(index)) inValue = array->getIndexQuickly(index); else { - PropertySlot slot(array); - if (array->methodTable()->getOwnPropertySlotByIndex(array, m_exec, index, slot)) + PropertySlot slot(array, PropertySlot::InternalMethodType::Get); + if (array->methodTable(vm)->getOwnPropertySlotByIndex(array, m_exec, index, slot)) inValue = slot.getValue(m_exec, index); else inValue = jsUndefined(); + RETURN_IF_EXCEPTION(scope, { }); } if (inValue.isObject()) { @@ -691,27 +686,28 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) case ArrayEndVisitMember: { JSArray* array = arrayStack.peek(); JSValue filteredValue = callReviver(array, jsString(m_exec, String::number(indexStack.last())), outValue); + RETURN_IF_EXCEPTION(scope, { }); if (filteredValue.isUndefined()) - array->methodTable()->deletePropertyByIndex(array, m_exec, indexStack.last()); + array->methodTable(vm)->deletePropertyByIndex(array, m_exec, indexStack.last()); else - array->putDirectIndex(m_exec, indexStack.last(), filteredValue); - if (m_exec->hadException()) - return jsNull(); + array->putDirectIndex(m_exec, indexStack.last(), filteredValue, 0, PutDirectIndexShouldNotThrow); + RETURN_IF_EXCEPTION(scope, { }); indexStack.last()++; goto arrayStartVisitMember; } objectStartState: case ObjectStartState: { ASSERT(inValue.isObject()); - ASSERT(!isJSArray(asObject(inValue)) && !asObject(inValue)->inherits(JSArray::info())); + ASSERT(!isJSArray(asObject(inValue)) && !asObject(inValue)->inherits(vm, JSArray::info())); if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) - return m_exec->vm().throwException(m_exec, createStackOverflowError(m_exec)); + return throwStackOverflowError(m_exec, scope); JSObject* object = asObject(inValue); objectStack.push(object); indexStack.append(0); - propertyStack.append(PropertyNameArray(m_exec)); - object->methodTable()->getOwnPropertyNames(object, m_exec, propertyStack.last(), ExcludeDontEnumProperties); + propertyStack.append(PropertyNameArray(m_exec, PropertyNameMode::Strings)); + object->methodTable(vm)->getOwnPropertyNames(object, m_exec, propertyStack.last(), EnumerationMode()); + RETURN_IF_EXCEPTION(scope, { }); } objectStartVisitMember: FALLTHROUGH; @@ -726,15 +722,14 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) propertyStack.removeLast(); break; } - PropertySlot slot(object); - if (object->methodTable()->getOwnPropertySlot(object, m_exec, properties[index], slot)) + PropertySlot slot(object, PropertySlot::InternalMethodType::Get); + if (object->methodTable(vm)->getOwnPropertySlot(object, m_exec, properties[index], slot)) inValue = slot.getValue(m_exec, properties[index]); else inValue = jsUndefined(); // The holder may be modified by the reviver function so any lookup may throw - if (m_exec->hadException()) - return jsNull(); + RETURN_IF_EXCEPTION(scope, { }); if (inValue.isObject()) { stateStack.append(ObjectEndVisitMember); @@ -748,12 +743,12 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) Identifier prop = propertyStack.last()[indexStack.last()]; PutPropertySlot slot(object); JSValue filteredValue = callReviver(object, jsString(m_exec, prop.string()), outValue); + RETURN_IF_EXCEPTION(scope, { }); if (filteredValue.isUndefined()) - object->methodTable()->deleteProperty(object, m_exec, prop); + object->methodTable(vm)->deleteProperty(object, m_exec, prop); else - object->methodTable()->put(object, m_exec, prop, filteredValue, slot); - if (m_exec->hadException()) - return jsNull(); + object->methodTable(vm)->put(object, m_exec, prop, filteredValue, slot); + RETURN_IF_EXCEPTION(scope, { }); indexStack.last()++; goto objectStartVisitMember; } @@ -764,7 +759,7 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) break; } JSObject* object = asObject(inValue); - if (isJSArray(object) || object->inherits(JSArray::info())) + if (isJSArray(object) || object->inherits(vm, JSArray::info())) goto arrayStartState; goto objectStartState; } @@ -776,31 +771,42 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) } JSObject* finalHolder = constructEmptyObject(m_exec); PutPropertySlot slot(finalHolder); - finalHolder->methodTable()->put(finalHolder, m_exec, m_exec->vm().propertyNames->emptyIdentifier, outValue, slot); + finalHolder->methodTable(vm)->put(finalHolder, m_exec, vm.propertyNames->emptyIdentifier, outValue, slot); + RETURN_IF_EXCEPTION(scope, { }); + scope.release(); return callReviver(finalHolder, jsEmptyString(m_exec), outValue); } // ECMA-262 v5 15.12.2 EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!exec->argumentCount()) - return throwVMError(exec, createError(exec, ASCIILiteral("JSON.parse requires at least one parameter"))); - String source = exec->uncheckedArgument(0).toString(exec)->value(exec); - if (exec->hadException()) - return JSValue::encode(jsNull()); + return throwVMError(exec, scope, createError(exec, ASCIILiteral("JSON.parse requires at least one parameter"))); + auto viewWithString = exec->uncheckedArgument(0).toString(exec)->viewWithUnderlyingString(*exec); + RETURN_IF_EXCEPTION(scope, { }); + StringView view = viewWithString.view; JSValue unfiltered; - LocalScope scope(exec->vm()); - if (source.is8Bit()) { - LiteralParser jsonParser(exec, source.characters8(), source.length(), StrictJSON); + LocalScope localScope(vm); + if (view.is8Bit()) { + LiteralParser jsonParser(exec, view.characters8(), view.length(), StrictJSON); unfiltered = jsonParser.tryLiteralParse(); - if (!unfiltered) - return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage())); + ASSERT(!scope.exception() || !unfiltered); + if (!unfiltered) { + RETURN_IF_EXCEPTION(scope, { }); + return throwVMError(exec, scope, createSyntaxError(exec, jsonParser.getErrorMessage())); + } } else { - LiteralParser jsonParser(exec, source.characters16(), source.length(), StrictJSON); + LiteralParser jsonParser(exec, view.characters16(), view.length(), StrictJSON); unfiltered = jsonParser.tryLiteralParse(); - if (!unfiltered) - return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage())); + ASSERT(!scope.exception() || !unfiltered); + if (!unfiltered) { + RETURN_IF_EXCEPTION(scope, { }); + return throwVMError(exec, scope, createSyntaxError(exec, jsonParser.getErrorMessage())); + } } if (exec->argumentCount() < 2) @@ -809,21 +815,28 @@ EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec) JSValue function = exec->uncheckedArgument(1); CallData callData; CallType callType = getCallData(function, callData); - if (callType == CallTypeNone) + if (callType == CallType::None) return JSValue::encode(unfiltered); - return JSValue::encode(Walker(exec, Local(exec->vm(), asObject(function)), callType, callData).walk(unfiltered)); + scope.release(); + return JSValue::encode(Walker(exec, Local(vm, asObject(function)), callType, callData).walk(unfiltered)); } // ECMA-262 v5 15.12.3 EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState* exec) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!exec->argumentCount()) - return throwVMError(exec, createError(exec, ASCIILiteral("No input to stringify"))); - LocalScope scope(exec->vm()); - Local value(exec->vm(), exec->uncheckedArgument(0)); - Local replacer(exec->vm(), exec->argument(1)); - Local space(exec->vm(), exec->argument(2)); - JSValue result = Stringifier(exec, replacer, space).stringify(value).get(); + return throwVMError(exec, scope, createError(exec, ASCIILiteral("No input to stringify"))); + LocalScope localScope(vm); + Local value(vm, exec->uncheckedArgument(0)); + Local replacer(vm, exec->argument(1)); + Local space(vm, exec->argument(2)); + Stringifier stringifier(exec, replacer, space); + RETURN_IF_EXCEPTION(scope, { }); + scope.release(); + JSValue result = stringifier.stringify(value).get(); return JSValue::encode(result); } @@ -831,6 +844,9 @@ JSValue JSONParse(ExecState* exec, const String& json) { LocalScope scope(exec->vm()); + if (json.isNull()) + return JSValue(); + if (json.is8Bit()) { LiteralParser jsonParser(exec, json.characters8(), json.length(), StrictJSON); return jsonParser.tryLiteralParse(); @@ -842,9 +858,13 @@ JSValue JSONParse(ExecState* exec, const String& json) String JSONStringify(ExecState* exec, JSValue value, unsigned indent) { - LocalScope scope(exec->vm()); - Local result = Stringifier(exec, Local(exec->vm(), jsNull()), Local(exec->vm(), jsNumber(indent))).stringify(Local(exec->vm(), value)); - if (result.isUndefinedOrNull()) + VM& vm = exec->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + LocalScope scope(vm); + Stringifier stringifier(exec, Local(vm, jsNull()), Local(vm, jsNumber(indent))); + RETURN_IF_EXCEPTION(throwScope, { }); + Local result = stringifier.stringify(Local(vm, value)); + if (UNLIKELY(throwScope.exception()) || result.isUndefinedOrNull()) return String(); return result.getString(exec); } -- cgit v1.2.1