X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/runtime/JSONObject.cpp diff --git a/runtime/JSONObject.cpp b/runtime/JSONObject.cpp index 8d261a2..61dc046 100644 --- a/runtime/JSONObject.cpp +++ b/runtime/JSONObject.cpp @@ -35,17 +35,18 @@ #include "Local.h" #include "LocalScope.h" #include "Lookup.h" +#include "ObjectConstructor.h" +#include "JSCInlines.h" #include "PropertyNameArray.h" -#include "UStringBuilder.h" -#include "UStringConcatenate.h" #include +#include namespace JSC { -ASSERT_CLASS_FITS_IN_CELL(JSONObject); +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*); } @@ -53,10 +54,15 @@ static EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*); namespace JSC { -JSONObject::JSONObject(JSGlobalObject* globalObject, Structure* structure) - : JSObjectWithGlobalObject(globalObject, structure) +JSONObject::JSONObject(VM& vm, Structure* structure) + : JSNonFinalObject(vm, structure) { - ASSERT(inherits(&s_info)); +} + +void JSONObject::finishCreation(VM& vm) +{ + Base::finishCreation(vm); + ASSERT(inherits(info())); } // PropertyNameForFunctionCall objects must be on the stack, since the JSValue that they create is not marked. @@ -84,11 +90,11 @@ public: private: class Holder { public: - Holder(JSGlobalData&, JSObject*); + Holder(VM&, JSObject*); JSObject* object() const { return m_object.get(); } - bool appendNextProperty(Stringifier&, UStringBuilder&); + bool appendNextProperty(Stringifier&, StringBuilder&); private: Local m_object; @@ -101,17 +107,15 @@ private: friend class Holder; - static void appendQuotedString(UStringBuilder&, const UString&); - JSValue toJSON(JSValue, const PropertyNameForFunctionCall&); enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedValue }; - StringifyResult appendStringifiedValue(UStringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&); + StringifyResult appendStringifiedValue(StringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&); bool willIndent() const; void indent(); void unindent(); - void startNewLine(UStringBuilder&) const; + void startNewLine(StringBuilder&) const; ExecState* const m_exec; const Local m_replacer; @@ -119,11 +123,11 @@ private: PropertyNameArray m_arrayReplacerPropertyNames; CallType m_replacerCallType; CallData m_replacerCallData; - const UString m_gap; + const String m_gap; - Vector m_holderStack; - UString m_repeatedGap; - UString m_indent; + Vector m_holderStack; + String m_repeatedGap; + String m_indent; }; // ------------------------------ helper functions -------------------------------- @@ -133,23 +137,23 @@ static inline JSValue unwrapBoxedPrimitive(ExecState* exec, JSValue value) if (!value.isObject()) return value; JSObject* object = asObject(value); - if (object->inherits(&NumberObject::s_info)) + if (object->inherits(NumberObject::info())) return jsNumber(object->toNumber(exec)); - if (object->inherits(&StringObject::s_info)) - return jsString(exec, object->toString(exec)); - if (object->inherits(&BooleanObject::s_info)) + if (object->inherits(StringObject::info())) + return object->toString(exec); + if (object->inherits(BooleanObject::info())) return object->toPrimitive(exec); return value; } -static inline UString gap(ExecState* exec, JSValue space) +static inline String gap(ExecState* exec, JSValue space) { const unsigned maxGapLength = 10; space = unwrapBoxedPrimitive(exec, space); // If the space value is a number, create a gap string with that number of spaces. - double spaceCount; - if (space.getNumber(spaceCount)) { + if (space.isNumber()) { + double spaceCount = space.asNumber(); int count; if (spaceCount > maxGapLength) count = maxGapLength; @@ -160,11 +164,11 @@ static inline UString gap(ExecState* exec, JSValue space) UChar spaces[maxGapLength]; for (int i = 0; i < count; ++i) spaces[i] = ' '; - return UString(spaces, count); + return String(spaces, count); } // If the space value is a string, use it as the gap string, otherwise use no gap string. - UString spaces = space.getString(exec); + String spaces = space.getString(exec); if (spaces.length() > maxGapLength) { spaces = spaces.substringSharingImpl(0, maxGapLength); } @@ -188,7 +192,7 @@ JSValue PropertyNameForFunctionCall::value(ExecState* exec) const { if (!m_value) { if (m_identifier) - m_value = jsString(exec, m_identifier->ustring()); + m_value = jsString(exec, m_identifier->string()); else m_value = jsNumber(m_number); } @@ -208,122 +212,54 @@ Stringifier::Stringifier(ExecState* exec, const Local& replacer, const if (!m_replacer.isObject()) return; - if (m_replacer.asObject()->inherits(&JSArray::s_info)) { + if (m_replacer.asObject()->inherits(JSArray::info())) { m_usingArrayReplacer = true; Handle array = m_replacer.asObject(); - unsigned length = array->get(exec, exec->globalData().propertyNames->length).toUInt32(exec); + unsigned length = array->get(exec, exec->vm().propertyNames->length).toUInt32(exec); for (unsigned i = 0; i < length; ++i) { JSValue name = array->get(exec, i); if (exec->hadException()) break; - UString propertyName; - if (name.getString(exec, propertyName)) { - m_arrayReplacerPropertyNames.add(Identifier(exec, propertyName)); - continue; - } - - double value = 0; - if (name.getNumber(value)) { - m_arrayReplacerPropertyNames.add(Identifier::from(exec, value)); - continue; - } - if (name.isObject()) { - if (!asObject(name)->inherits(&NumberObject::s_info) && !asObject(name)->inherits(&StringObject::s_info)) + if (!asObject(name)->inherits(NumberObject::info()) && !asObject(name)->inherits(StringObject::info())) continue; - propertyName = name.toString(exec); - if (exec->hadException()) - break; - m_arrayReplacerPropertyNames.add(Identifier(exec, propertyName)); - } + } else if (!name.isNumber() && !name.isString()) + continue; + + m_arrayReplacerPropertyNames.add(name.toString(exec)->toIdentifier(exec)); } return; } - m_replacerCallType = m_replacer.asObject()->getCallData(m_replacerCallData); + m_replacerCallType = m_replacer.asObject()->methodTable()->getCallData(m_replacer.asObject().get(), m_replacerCallData); } Local Stringifier::stringify(Handle value) { JSObject* object = constructEmptyObject(m_exec); if (m_exec->hadException()) - return Local(m_exec->globalData(), jsNull()); + return Local(m_exec->vm(), jsNull()); - PropertyNameForFunctionCall emptyPropertyName(m_exec->globalData().propertyNames->emptyIdentifier); - object->putDirect(m_exec->globalData(), m_exec->globalData().propertyNames->emptyIdentifier, value.get()); + PropertyNameForFunctionCall emptyPropertyName(m_exec->vm().propertyNames->emptyIdentifier); + object->putDirect(m_exec->vm(), m_exec->vm().propertyNames->emptyIdentifier, value.get()); - UStringBuilder result; + StringBuilder result; if (appendStringifiedValue(result, value.get(), object, emptyPropertyName) != StringifySucceeded) - return Local(m_exec->globalData(), jsUndefined()); + return Local(m_exec->vm(), jsUndefined()); if (m_exec->hadException()) - return Local(m_exec->globalData(), jsNull()); + return Local(m_exec->vm(), jsNull()); - return Local(m_exec->globalData(), jsString(m_exec, result.toUString())); -} - -void Stringifier::appendQuotedString(UStringBuilder& builder, const UString& value) -{ - int length = value.length(); - - builder.append('"'); - - const UChar* data = value.characters(); - 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]; - UChar hex[] = { '\\', 'u', hexDigits[(ch >> 12) & 0xF], hexDigits[(ch >> 8) & 0xF], hexDigits[(ch >> 4) & 0xF], hexDigits[ch & 0xF] }; - builder.append(hex, WTF_ARRAY_LENGTH(hex)); - break; - } - } - - builder.append('"'); + return Local(m_exec->vm(), jsString(m_exec, result.toString())); } inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName) { ASSERT(!m_exec->hadException()); - if (!value.isObject() || !asObject(value)->hasProperty(m_exec, m_exec->globalData().propertyNames->toJSON)) + if (!value.isObject() || !asObject(value)->hasProperty(m_exec, m_exec->vm().propertyNames->toJSON)) return value; - JSValue toJSONFunction = asObject(value)->get(m_exec, m_exec->globalData().propertyNames->toJSON); + JSValue toJSONFunction = asObject(value)->get(m_exec, m_exec->vm().propertyNames->toJSON); if (m_exec->hadException()) return jsNull(); @@ -332,16 +268,16 @@ inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionC JSObject* object = asObject(toJSONFunction); CallData callData; - CallType callType = object->getCallData(callData); + CallType callType = object->methodTable()->getCallData(object, callData); if (callType == CallTypeNone) return value; - JSValue list[] = { propertyName.value(m_exec) }; - ArgList args(list, WTF_ARRAY_LENGTH(list)); + MarkedArgumentBuffer args; + args.append(propertyName.value(m_exec)); return call(m_exec, object, callType, callData, value, args); } -Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName) +Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName) { // Call the toJSON function. value = toJSON(value, propertyName); @@ -350,18 +286,19 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& // Call the replacer function. if (m_replacerCallType != CallTypeNone) { - JSValue list[] = { propertyName.value(m_exec), value }; - ArgList args(list, WTF_ARRAY_LENGTH(list)); + 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; } - if (value.isUndefined() && !holder->inherits(&JSArray::s_info)) + if (value.isUndefined() && !holder->inherits(JSArray::info())) return StringifyFailedDueToUndefinedValue; if (value.isNull()) { - builder.append("null"); + builder.appendLiteral("null"); return StringifySucceeded; } @@ -371,22 +308,28 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& return StringifyFailed; if (value.isBoolean()) { - builder.append(value.getBoolean() ? "true" : "false"); + if (value.isTrue()) + builder.appendLiteral("true"); + else + builder.appendLiteral("false"); return StringifySucceeded; } - UString stringValue; - if (value.getString(m_exec, stringValue)) { - appendQuotedString(builder, stringValue); + if (value.isString()) { + builder.appendQuotedJSONString(asString(value)->value(m_exec)); return StringifySucceeded; } - double numericValue; - if (value.getNumber(numericValue)) { - if (!isfinite(numericValue)) - builder.append("null"); - else - builder.append(UString::number(numericValue)); + if (value.isNumber()) { + 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; } @@ -396,9 +339,9 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& JSObject* object = asObject(value); CallData callData; - if (object->getCallData(callData) != CallTypeNone) { - if (holder->inherits(&JSArray::s_info)) { - builder.append("null"); + if (object->methodTable()->getCallData(object, callData) != CallTypeNone) { + if (holder->inherits(JSArray::info())) { + builder.appendLiteral("null"); return StringifySucceeded; } return StringifyFailedDueToUndefinedValue; @@ -407,30 +350,19 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& // 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) { - throwError(m_exec, createTypeError(m_exec, "JSON.stringify cannot serialize cyclic structures.")); + m_exec->vm().throwException(m_exec, createTypeError(m_exec, ASCIILiteral("JSON.stringify cannot serialize cyclic structures."))); return StringifyFailed; } } bool holderStackWasEmpty = m_holderStack.isEmpty(); - m_holderStack.append(Holder(m_exec->globalData(), object)); + m_holderStack.append(Holder(m_exec->vm(), object)); if (!holderStackWasEmpty) return StringifySucceeded; - // If this is the outermost call, then loop to handle everything on the holder stack. - TimeoutChecker localTimeoutChecker(m_exec->globalData().timeoutChecker); - localTimeoutChecker.reset(); - unsigned tickCount = localTimeoutChecker.ticksUntilNextCheck(); do { while (m_holderStack.last().appendNextProperty(*this, builder)) { if (m_exec->hadException()) return StringifyFailed; - if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) { - throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); - return StringifyFailed; - } - tickCount = localTimeoutChecker.ticksUntilNextCheck(); - } } m_holderStack.removeLast(); } while (!m_holderStack.isEmpty()); @@ -447,7 +379,7 @@ inline void Stringifier::indent() // Use a single shared string, m_repeatedGap, so we don't keep allocating new ones as we indent and unindent. unsigned newSize = m_indent.length() + m_gap.length(); if (newSize > m_repeatedGap.length()) - m_repeatedGap = makeUString(m_repeatedGap, m_gap); + m_repeatedGap = makeString(m_repeatedGap, m_gap); ASSERT(newSize <= m_repeatedGap.length()); m_indent = m_repeatedGap.substringSharingImpl(0, newSize); } @@ -458,7 +390,7 @@ inline void Stringifier::unindent() m_indent = m_repeatedGap.substringSharingImpl(0, m_indent.length() - m_gap.length()); } -inline void Stringifier::startNewLine(UStringBuilder& builder) const +inline void Stringifier::startNewLine(StringBuilder& builder) const { if (m_gap.isEmpty()) return; @@ -466,14 +398,17 @@ inline void Stringifier::startNewLine(UStringBuilder& builder) const builder.append(m_indent); } -inline Stringifier::Holder::Holder(JSGlobalData& globalData, JSObject* object) - : m_object(globalData, object) - , m_isArray(object->inherits(&JSArray::s_info)) +inline Stringifier::Holder::Holder(VM& vm, JSObject* object) + : m_object(vm, object) + , m_isArray(object->inherits(JSArray::info())) , m_index(0) +#ifndef NDEBUG + , m_size(0) +#endif { } -bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBuilder& builder) +bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBuilder& builder) { ASSERT(m_index <= m_size); @@ -482,15 +417,18 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu // First time through, initialize. if (!m_index) { if (m_isArray) { - m_isJSArray = isJSArray(&exec->globalData(), m_object.get()); - m_size = m_object->get(exec, exec->globalData().propertyNames->length).toUInt32(exec); + m_isJSArray = isJSArray(m_object.get()); + if (m_isJSArray) + m_size = asArray(m_object.get())->length(); + else + m_size = m_object->get(exec, exec->vm().propertyNames->length).toUInt32(exec); builder.append('['); } else { if (stringifier.m_usingArrayReplacer) m_propertyNames = stringifier.m_arrayReplacerPropertyNames.data(); else { PropertyNameArray objectPropertyNames(exec); - m_object->getOwnPropertyNames(exec, objectPropertyNames); + m_object->methodTable()->getOwnPropertyNames(m_object.get(), exec, objectPropertyNames, EnumerationMode()); m_propertyNames = objectPropertyNames.releaseData(); } m_size = m_propertyNames->propertyNameVector().size(); @@ -515,15 +453,16 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu if (m_isArray) { // Get the value. JSValue value; - if (m_isJSArray && asArray(m_object.get())->canGetIndex(index)) - value = asArray(m_object.get())->getIndex(index); + 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->getOwnPropertySlot(exec, index, slot)) - slot.setUndefined(); - if (exec->hadException()) - return false; - value = slot.getValue(exec, index); + if (m_object->methodTable()->getOwnPropertySlotByIndex(m_object.get(), exec, index, slot)) { + value = slot.getValue(exec, index); + if (exec->hadException()) + return false; + } else + value = jsUndefined(); } // Append the separator string. @@ -537,7 +476,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu // Get the value. PropertySlot slot(m_object.get()); Identifier& propertyName = m_propertyNames->propertyNameVector()[index]; - if (!m_object->getOwnPropertySlot(exec, propertyName, slot)) + if (!m_object->methodTable()->getOwnPropertySlot(m_object.get(), exec, propertyName, slot)) return true; JSValue value = slot.getValue(exec, propertyName); if (exec->hadException()) @@ -551,7 +490,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu stringifier.startNewLine(builder); // Append the property name. - appendQuotedString(builder, propertyName.ustring()); + builder.appendQuotedJSONString(propertyName.string()); builder.append(':'); if (stringifier.willIndent()) builder.append(' '); @@ -566,7 +505,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu switch (stringifyResult) { case StringifyFailed: - builder.append("null"); + builder.appendLiteral("null"); break; case StringifySucceeded: break; @@ -583,7 +522,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBu // ------------------------------ JSONObject -------------------------------- -const ClassInfo JSONObject::s_info = { "JSON", &JSObjectWithGlobalObject::s_info, 0, ExecState::jsonTable }; +const ClassInfo JSONObject::s_info = { "JSON", &JSNonFinalObject::s_info, &jsonTable, CREATE_METHOD_TABLE(JSONObject) }; /* Source for JSONObject.lut.h @begin jsonTable @@ -594,21 +533,16 @@ const ClassInfo JSONObject::s_info = { "JSON", &JSObjectWithGlobalObject::s_info // ECMA 15.8 -bool JSONObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) -{ - return getStaticFunctionSlot(exec, ExecState::jsonTable(exec), this, propertyName, slot); -} - -bool JSONObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) +bool JSONObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot) { - return getStaticFunctionDescriptor(exec, ExecState::jsonTable(exec), this, propertyName, descriptor); + return getStaticFunctionSlot(exec, jsonTable, jsCast(object), propertyName, slot); } class Walker { public: Walker(ExecState* exec, Handle function, CallType callType, CallData callData) : m_exec(exec) - , m_function(exec->globalData(), function) + , m_function(exec->vm(), function) , m_callType(callType) , m_callData(callData) { @@ -617,9 +551,10 @@ public: private: JSValue callReviver(JSObject* thisObj, JSValue property, JSValue unfiltered) { - JSValue args[] = { property, unfiltered }; - ArgList argList(args, 2); - return call(m_exec, m_function.get(), m_callType, m_callData, thisObj, argList); + MarkedArgumentBuffer args; + args.append(property); + args.append(unfiltered); + return call(m_exec, m_function.get(), m_callType, m_callData, thisObj, args); } friend class Holder; @@ -630,48 +565,38 @@ private: CallData m_callData; }; -// We clamp recursion well beyond anything reasonable, but we also have a timeout check -// to guard against "infinite" execution by inserting arbitrarily large objects. +// We clamp recursion well beyond anything reasonable. static const unsigned maximumFilterRecursion = 40000; enum WalkerState { StateUnknown, ArrayStartState, ArrayStartVisitMember, ArrayEndVisitMember, ObjectStartState, ObjectStartVisitMember, ObjectEndVisitMember }; NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) { - Vector propertyStack; - Vector indexStack; - LocalStack objectStack(m_exec->globalData()); - LocalStack arrayStack(m_exec->globalData()); + Vector propertyStack; + Vector indexStack; + LocalStack objectStack(m_exec->vm()); + LocalStack arrayStack(m_exec->vm()); - Vector stateStack; + Vector stateStack; WalkerState state = StateUnknown; JSValue inValue = unfiltered; JSValue outValue = jsNull(); - TimeoutChecker localTimeoutChecker(m_exec->globalData().timeoutChecker); - localTimeoutChecker.reset(); - unsigned tickCount = localTimeoutChecker.ticksUntilNextCheck(); while (1) { switch (state) { arrayStartState: case ArrayStartState: { ASSERT(inValue.isObject()); - ASSERT(isJSArray(&m_exec->globalData(), asObject(inValue)) || asObject(inValue)->inherits(&JSArray::s_info)); + ASSERT(isJSArray(asObject(inValue)) || asObject(inValue)->inherits(JSArray::info())); if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) - return throwError(m_exec, createStackOverflowError(m_exec)); + return throwStackOverflowError(m_exec); JSArray* array = asArray(inValue); arrayStack.push(array); indexStack.append(0); - // fallthrough } arrayStartVisitMember: + FALLTHROUGH; case ArrayStartVisitMember: { - if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) - return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); - tickCount = localTimeoutChecker.ticksUntilNextCheck(); - } - JSArray* array = arrayStack.peek(); uint32_t index = indexStack.last(); if (index == array->length()) { @@ -680,11 +605,11 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) indexStack.removeLast(); break; } - if (isJSArray(&m_exec->globalData(), array) && array->canGetIndex(index)) - inValue = array->getIndex(index); + if (isJSArray(array) && array->canGetIndexQuickly(index)) + inValue = array->getIndexQuickly(index); else { - PropertySlot slot; - if (array->getOwnPropertySlot(m_exec, index, slot)) + PropertySlot slot(array); + if (array->methodTable()->getOwnPropertySlotByIndex(array, m_exec, index, slot)) inValue = slot.getValue(m_exec, index); else inValue = jsUndefined(); @@ -695,19 +620,15 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) goto stateUnknown; } else outValue = inValue; - // fallthrough + FALLTHROUGH; } case ArrayEndVisitMember: { JSArray* array = arrayStack.peek(); - JSValue filteredValue = callReviver(array, jsString(m_exec, UString::number(indexStack.last())), outValue); + JSValue filteredValue = callReviver(array, jsString(m_exec, String::number(indexStack.last())), outValue); if (filteredValue.isUndefined()) - array->deleteProperty(m_exec, indexStack.last()); - else { - if (isJSArray(&m_exec->globalData(), array) && array->canSetIndex(indexStack.last())) - array->setIndex(m_exec->globalData(), indexStack.last(), filteredValue); - else - array->put(m_exec, indexStack.last(), filteredValue); - } + array->methodTable()->deletePropertyByIndex(array, m_exec, indexStack.last()); + else + array->putDirectIndex(m_exec, indexStack.last(), filteredValue); if (m_exec->hadException()) return jsNull(); indexStack.last()++; @@ -716,25 +637,19 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) objectStartState: case ObjectStartState: { ASSERT(inValue.isObject()); - ASSERT(!isJSArray(&m_exec->globalData(), asObject(inValue)) && !asObject(inValue)->inherits(&JSArray::s_info)); + ASSERT(!isJSArray(asObject(inValue)) && !asObject(inValue)->inherits(JSArray::info())); if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) - return throwError(m_exec, createStackOverflowError(m_exec)); + return throwStackOverflowError(m_exec); JSObject* object = asObject(inValue); objectStack.push(object); indexStack.append(0); propertyStack.append(PropertyNameArray(m_exec)); - object->getOwnPropertyNames(m_exec, propertyStack.last()); - // fallthrough + object->methodTable()->getOwnPropertyNames(object, m_exec, propertyStack.last(), EnumerationMode()); } objectStartVisitMember: + FALLTHROUGH; case ObjectStartVisitMember: { - if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) - return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); - tickCount = localTimeoutChecker.ticksUntilNextCheck(); - } - JSObject* object = objectStack.peek(); uint32_t index = indexStack.last(); PropertyNameArray& properties = propertyStack.last(); @@ -745,8 +660,8 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) propertyStack.removeLast(); break; } - PropertySlot slot; - if (object->getOwnPropertySlot(m_exec, properties[index], slot)) + PropertySlot slot(object); + if (object->methodTable()->getOwnPropertySlot(object, m_exec, properties[index], slot)) inValue = slot.getValue(m_exec, properties[index]); else inValue = jsUndefined(); @@ -760,17 +675,17 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) goto stateUnknown; } else outValue = inValue; - // fallthrough + FALLTHROUGH; } case ObjectEndVisitMember: { JSObject* object = objectStack.peek(); Identifier prop = propertyStack.last()[indexStack.last()]; - PutPropertySlot slot; - JSValue filteredValue = callReviver(object, jsString(m_exec, prop.ustring()), outValue); + PutPropertySlot slot(object); + JSValue filteredValue = callReviver(object, jsString(m_exec, prop.string()), outValue); if (filteredValue.isUndefined()) - object->deleteProperty(m_exec, prop); + object->methodTable()->deleteProperty(object, m_exec, prop); else - object->put(m_exec, prop, filteredValue, slot); + object->methodTable()->put(object, m_exec, prop, filteredValue, slot); if (m_exec->hadException()) return jsNull(); indexStack.last()++; @@ -783,7 +698,7 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) break; } JSObject* object = asObject(inValue); - if (isJSArray(&m_exec->globalData(), object) || object->inherits(&JSArray::s_info)) + if (isJSArray(object) || object->inherits(JSArray::info())) goto arrayStartState; goto objectStartState; } @@ -792,16 +707,10 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) state = stateStack.last(); stateStack.removeLast(); - - if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) - return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); - tickCount = localTimeoutChecker.ticksUntilNextCheck(); - } } JSObject* finalHolder = constructEmptyObject(m_exec); - PutPropertySlot slot; - finalHolder->put(m_exec, m_exec->globalData().propertyNames->emptyIdentifier, outValue, slot); + PutPropertySlot slot(finalHolder); + finalHolder->methodTable()->put(finalHolder, m_exec, m_exec->vm().propertyNames->emptyIdentifier, outValue, slot); return callReviver(finalHolder, jsEmptyString(m_exec), outValue); } @@ -809,47 +718,68 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec) { if (!exec->argumentCount()) - return throwVMError(exec, createError(exec, "JSON.parse requires at least one parameter")); - JSValue value = exec->argument(0); - UString source = value.toString(exec); + return throwVMError(exec, createError(exec, ASCIILiteral("JSON.parse requires at least one parameter"))); + StringView source = exec->uncheckedArgument(0).toString(exec)->view(exec); if (exec->hadException()) return JSValue::encode(jsNull()); - LocalScope scope(exec->globalData()); - LiteralParser jsonParser(exec, source.characters(), source.length(), LiteralParser::StrictJSON); - JSValue unfiltered = jsonParser.tryLiteralParse(); - if (!unfiltered) - return throwVMError(exec, createSyntaxError(exec, "Unable to parse JSON string")); + JSValue unfiltered; + LocalScope scope(exec->vm()); + if (source.is8Bit()) { + LiteralParser jsonParser(exec, source.characters8(), source.length(), StrictJSON); + unfiltered = jsonParser.tryLiteralParse(); + if (!unfiltered) + return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage())); + } else { + LiteralParser jsonParser(exec, source.characters16(), source.length(), StrictJSON); + unfiltered = jsonParser.tryLiteralParse(); + if (!unfiltered) + return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage())); + } if (exec->argumentCount() < 2) return JSValue::encode(unfiltered); - JSValue function = exec->argument(1); + JSValue function = exec->uncheckedArgument(1); CallData callData; CallType callType = getCallData(function, callData); if (callType == CallTypeNone) return JSValue::encode(unfiltered); - return JSValue::encode(Walker(exec, Local(exec->globalData(), asObject(function)), callType, callData).walk(unfiltered)); + return JSValue::encode(Walker(exec, Local(exec->vm(), asObject(function)), callType, callData).walk(unfiltered)); } // ECMA-262 v5 15.12.3 EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState* exec) { if (!exec->argumentCount()) - return throwVMError(exec, createError(exec, "No input to stringify")); - LocalScope scope(exec->globalData()); - Local value(exec->globalData(), exec->argument(0)); - Local replacer(exec->globalData(), exec->argument(1)); - Local space(exec->globalData(), exec->argument(2)); - return JSValue::encode(Stringifier(exec, replacer, space).stringify(value).get()); + 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 JSValue::encode(result); +} + +JSValue JSONParse(ExecState* exec, const String& json) +{ + LocalScope scope(exec->vm()); + + if (json.is8Bit()) { + LiteralParser jsonParser(exec, json.characters8(), json.length(), StrictJSON); + return jsonParser.tryLiteralParse(); + } + + LiteralParser jsonParser(exec, json.characters16(), json.length(), StrictJSON); + return jsonParser.tryLiteralParse(); } -UString JSONStringify(ExecState* exec, JSValue value, unsigned indent) +String JSONStringify(ExecState* exec, JSValue value, unsigned indent) { - LocalScope scope(exec->globalData()); - Local result = Stringifier(exec, Local(exec->globalData(), jsNull()), Local(exec->globalData(), jsNumber(indent))).stringify(Local(exec->globalData(), value)); + 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()) - return UString(); + return String(); return result.getString(exec); }