X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..14957cd040308e3eeec43d26bae5d76da13fcd85:/runtime/JSONObject.cpp diff --git a/runtime/JSONObject.cpp b/runtime/JSONObject.cpp index ce0dcff..8d261a2 100644 --- a/runtime/JSONObject.cpp +++ b/runtime/JSONObject.cpp @@ -30,17 +30,22 @@ #include "Error.h" #include "ExceptionHelpers.h" #include "JSArray.h" +#include "JSGlobalObject.h" #include "LiteralParser.h" +#include "Local.h" +#include "LocalScope.h" +#include "Lookup.h" #include "PropertyNameArray.h" -#include "StringBuilder.h" +#include "UStringBuilder.h" +#include "UStringConcatenate.h" #include namespace JSC { ASSERT_CLASS_FITS_IN_CELL(JSONObject); -static JSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*, JSObject*, JSValue, const ArgList&); +static EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*); +static EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*); } @@ -48,6 +53,12 @@ static JSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*, JSObject*, JSVal namespace JSC { +JSONObject::JSONObject(JSGlobalObject* globalObject, Structure* structure) + : JSObjectWithGlobalObject(globalObject, structure) +{ + ASSERT(inherits(&s_info)); +} + // PropertyNameForFunctionCall objects must be on the stack, since the JSValue that they create is not marked. class PropertyNameForFunctionCall { public: @@ -62,25 +73,25 @@ private: mutable JSValue m_value; }; -class Stringifier : public Noncopyable { +class Stringifier { + WTF_MAKE_NONCOPYABLE(Stringifier); public: - Stringifier(ExecState*, JSValue replacer, JSValue space); - ~Stringifier(); - JSValue stringify(JSValue); + Stringifier(ExecState*, const Local& replacer, const Local& space); + Local stringify(Handle); - void markAggregate(MarkStack&); + void visitAggregate(SlotVisitor&); private: class Holder { public: - Holder(JSObject*); + Holder(JSGlobalData&, JSObject*); - JSObject* object() const { return m_object; } + JSObject* object() const { return m_object.get(); } - bool appendNextProperty(Stringifier&, StringBuilder&); + bool appendNextProperty(Stringifier&, UStringBuilder&); private: - JSObject* const m_object; + Local m_object; const bool m_isArray; bool m_isJSArray; unsigned m_index; @@ -90,28 +101,26 @@ private: friend class Holder; - static void appendQuotedString(StringBuilder&, const UString&); + static void appendQuotedString(UStringBuilder&, const UString&); JSValue toJSON(JSValue, const PropertyNameForFunctionCall&); enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedValue }; - StringifyResult appendStringifiedValue(StringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&); + StringifyResult appendStringifiedValue(UStringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&); bool willIndent() const; void indent(); void unindent(); - void startNewLine(StringBuilder&) const; + void startNewLine(UStringBuilder&) const; - Stringifier* const m_nextStringifierToMark; ExecState* const m_exec; - const JSValue m_replacer; + const Local m_replacer; bool m_usingArrayReplacer; PropertyNameArray m_arrayReplacerPropertyNames; CallType m_replacerCallType; CallData m_replacerCallData; const UString m_gap; - HashSet m_holderCycleDetector; Vector m_holderStack; UString m_repeatedGap; UString m_indent; @@ -124,18 +133,18 @@ static inline JSValue unwrapBoxedPrimitive(ExecState* exec, JSValue value) if (!value.isObject()) return value; JSObject* object = asObject(value); - if (object->inherits(&NumberObject::info)) - return jsNumber(exec, object->toNumber(exec)); - if (object->inherits(&StringObject::info)) + if (object->inherits(&NumberObject::s_info)) + return jsNumber(object->toNumber(exec)); + if (object->inherits(&StringObject::s_info)) return jsString(exec, object->toString(exec)); - if (object->inherits(&BooleanObject::info)) + if (object->inherits(&BooleanObject::s_info)) return object->toPrimitive(exec); return value; } static inline UString gap(ExecState* exec, JSValue space) { - const int maxGapLength = 10; + const unsigned maxGapLength = 10; space = unwrapBoxedPrimitive(exec, space); // If the space value is a number, create a gap string with that number of spaces. @@ -156,8 +165,8 @@ static inline UString gap(ExecState* exec, JSValue space) // If the space value is a string, use it as the gap string, otherwise use no gap string. UString spaces = space.getString(exec); - if (spaces.size() > maxGapLength) { - spaces = spaces.substr(0, maxGapLength); + if (spaces.length() > maxGapLength) { + spaces = spaces.substringSharingImpl(0, maxGapLength); } return spaces; } @@ -181,30 +190,27 @@ JSValue PropertyNameForFunctionCall::value(ExecState* exec) const if (m_identifier) m_value = jsString(exec, m_identifier->ustring()); else - m_value = jsNumber(exec, m_number); + m_value = jsNumber(m_number); } return m_value; } // ------------------------------ Stringifier -------------------------------- -Stringifier::Stringifier(ExecState* exec, JSValue replacer, JSValue space) - : m_nextStringifierToMark(exec->globalData().firstStringifierToMark) - , m_exec(exec) +Stringifier::Stringifier(ExecState* exec, const Local& replacer, const Local& space) + : m_exec(exec) , m_replacer(replacer) , m_usingArrayReplacer(false) , m_arrayReplacerPropertyNames(exec) , m_replacerCallType(CallTypeNone) - , m_gap(gap(exec, space)) + , m_gap(gap(exec, space.get())) { - exec->globalData().firstStringifierToMark = this; - if (!m_replacer.isObject()) return; - if (asObject(m_replacer)->inherits(&JSArray::info)) { + if (m_replacer.asObject()->inherits(&JSArray::s_info)) { m_usingArrayReplacer = true; - JSObject* array = asObject(m_replacer); + Handle array = m_replacer.asObject(); unsigned length = array->get(exec, exec->globalData().propertyNames->length).toUInt32(exec); for (unsigned i = 0; i < length; ++i) { JSValue name = array->get(exec, i); @@ -224,7 +230,7 @@ Stringifier::Stringifier(ExecState* exec, JSValue replacer, JSValue space) } if (name.isObject()) { - if (!asObject(name)->inherits(&NumberObject::info) && !asObject(name)->inherits(&StringObject::info)) + if (!asObject(name)->inherits(&NumberObject::s_info) && !asObject(name)->inherits(&StringObject::s_info)) continue; propertyName = name.toString(exec); if (exec->hadException()) @@ -235,52 +241,34 @@ Stringifier::Stringifier(ExecState* exec, JSValue replacer, JSValue space) return; } - m_replacerCallType = asObject(m_replacer)->getCallData(m_replacerCallData); + m_replacerCallType = m_replacer.asObject()->getCallData(m_replacerCallData); } -Stringifier::~Stringifier() -{ - ASSERT(m_exec->globalData().firstStringifierToMark == this); - m_exec->globalData().firstStringifierToMark = m_nextStringifierToMark; -} - -void Stringifier::markAggregate(MarkStack& markStack) -{ - for (Stringifier* stringifier = this; stringifier; stringifier = stringifier->m_nextStringifierToMark) { - size_t size = m_holderStack.size(); - for (size_t i = 0; i < size; ++i) - markStack.append(m_holderStack[i].object()); - } -} - -JSValue Stringifier::stringify(JSValue value) +Local Stringifier::stringify(Handle value) { JSObject* object = constructEmptyObject(m_exec); if (m_exec->hadException()) - return jsNull(); + return Local(m_exec->globalData(), jsNull()); PropertyNameForFunctionCall emptyPropertyName(m_exec->globalData().propertyNames->emptyIdentifier); - object->putDirect(m_exec->globalData().propertyNames->emptyIdentifier, value); + object->putDirect(m_exec->globalData(), m_exec->globalData().propertyNames->emptyIdentifier, value.get()); - StringBuilder result; - if (appendStringifiedValue(result, value, object, emptyPropertyName) != StringifySucceeded) - return jsUndefined(); + UStringBuilder result; + if (appendStringifiedValue(result, value.get(), object, emptyPropertyName) != StringifySucceeded) + return Local(m_exec->globalData(), jsUndefined()); if (m_exec->hadException()) - return jsNull(); + return Local(m_exec->globalData(), jsNull()); - return jsString(m_exec, result.release()); + return Local(m_exec->globalData(), jsString(m_exec, result.toUString())); } -void Stringifier::appendQuotedString(StringBuilder& builder, const UString& value) +void Stringifier::appendQuotedString(UStringBuilder& builder, const UString& value) { - int length = value.size(); - - // String length plus 2 for quote marks plus 8 so we can accomodate a few escaped characters. - builder.reserveCapacity(builder.size() + length + 2 + 8); + int length = value.length(); builder.append('"'); - const UChar* data = value.data(); + 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] != '\\')) @@ -321,7 +309,7 @@ void Stringifier::appendQuotedString(StringBuilder& builder, const UString& valu 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, sizeof(hex) / sizeof(UChar)); + builder.append(hex, WTF_ARRAY_LENGTH(hex)); break; } } @@ -349,11 +337,11 @@ inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionC return value; JSValue list[] = { propertyName.value(m_exec) }; - ArgList args(list, sizeof(list) / sizeof(JSValue)); + ArgList args(list, WTF_ARRAY_LENGTH(list)); return call(m_exec, object, callType, callData, value, args); } -Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName) +Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName) { // Call the toJSON function. value = toJSON(value, propertyName); @@ -363,13 +351,13 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& // Call the replacer function. if (m_replacerCallType != CallTypeNone) { JSValue list[] = { propertyName.value(m_exec), value }; - ArgList args(list, sizeof(list) / sizeof(JSValue)); - value = call(m_exec, m_replacer, m_replacerCallType, m_replacerCallData, holder, args); + ArgList args(list, WTF_ARRAY_LENGTH(list)); + 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::info)) + if (value.isUndefined() && !holder->inherits(&JSArray::s_info)) return StringifyFailedDueToUndefinedValue; if (value.isNull()) { @@ -398,7 +386,7 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& if (!isfinite(numericValue)) builder.append("null"); else - builder.append(UString::from(numericValue)); + builder.append(UString::number(numericValue)); return StringifySucceeded; } @@ -409,7 +397,7 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& CallData callData; if (object->getCallData(callData) != CallTypeNone) { - if (holder->inherits(&JSArray::info)) { + if (holder->inherits(&JSArray::s_info)) { builder.append("null"); return StringifySucceeded; } @@ -417,12 +405,14 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& } // Handle cycle detection, and put the holder on the stack. - if (!m_holderCycleDetector.add(object).second) { - throwError(m_exec, TypeError, "JSON.stringify cannot serialize cyclic structures."); - return StringifyFailed; + 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.")); + return StringifyFailed; + } } bool holderStackWasEmpty = m_holderStack.isEmpty(); - m_holderStack.append(object); + m_holderStack.append(Holder(m_exec->globalData(), object)); if (!holderStackWasEmpty) return StringifySucceeded; @@ -436,13 +426,12 @@ Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder& return StringifyFailed; if (!--tickCount) { if (localTimeoutChecker.didTimeOut(m_exec)) { - m_exec->setException(createInterruptedExecutionException(&m_exec->globalData())); + throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); return StringifyFailed; } tickCount = localTimeoutChecker.ticksUntilNextCheck(); } } - m_holderCycleDetector.remove(m_holderStack.last().object()); m_holderStack.removeLast(); } while (!m_holderStack.isEmpty()); return StringifySucceeded; @@ -456,20 +445,20 @@ inline bool Stringifier::willIndent() const inline void Stringifier::indent() { // Use a single shared string, m_repeatedGap, so we don't keep allocating new ones as we indent and unindent. - int newSize = m_indent.size() + m_gap.size(); - if (newSize > m_repeatedGap.size()) - m_repeatedGap = makeString(m_repeatedGap, m_gap); - ASSERT(newSize <= m_repeatedGap.size()); - m_indent = m_repeatedGap.substr(0, newSize); + unsigned newSize = m_indent.length() + m_gap.length(); + if (newSize > m_repeatedGap.length()) + m_repeatedGap = makeUString(m_repeatedGap, m_gap); + ASSERT(newSize <= m_repeatedGap.length()); + m_indent = m_repeatedGap.substringSharingImpl(0, newSize); } inline void Stringifier::unindent() { - ASSERT(m_indent.size() >= m_gap.size()); - m_indent = m_repeatedGap.substr(0, m_indent.size() - m_gap.size()); + ASSERT(m_indent.length() >= m_gap.length()); + m_indent = m_repeatedGap.substringSharingImpl(0, m_indent.length() - m_gap.length()); } -inline void Stringifier::startNewLine(StringBuilder& builder) const +inline void Stringifier::startNewLine(UStringBuilder& builder) const { if (m_gap.isEmpty()) return; @@ -477,14 +466,14 @@ inline void Stringifier::startNewLine(StringBuilder& builder) const builder.append(m_indent); } -inline Stringifier::Holder::Holder(JSObject* object) - : m_object(object) - , m_isArray(object->inherits(&JSArray::info)) +inline Stringifier::Holder::Holder(JSGlobalData& globalData, JSObject* object) + : m_object(globalData, object) + , m_isArray(object->inherits(&JSArray::s_info)) , m_index(0) { } -bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBuilder& builder) +bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBuilder& builder) { ASSERT(m_index <= m_size); @@ -493,7 +482,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui // First time through, initialize. if (!m_index) { if (m_isArray) { - m_isJSArray = isJSArray(&exec->globalData(), m_object); + m_isJSArray = isJSArray(&exec->globalData(), m_object.get()); m_size = m_object->get(exec, exec->globalData().propertyNames->length).toUInt32(exec); builder.append('['); } else { @@ -513,7 +502,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui // Last time through, finish up and return false. if (m_index == m_size) { stringifier.unindent(); - if (m_size && builder[builder.size() - 1] != '{') + if (m_size && builder[builder.length() - 1] != '{') stringifier.startNewLine(builder); builder.append(m_isArray ? ']' : '}'); return false; @@ -526,10 +515,10 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui if (m_isArray) { // Get the value. JSValue value; - if (m_isJSArray && asArray(m_object)->canGetIndex(index)) - value = asArray(m_object)->getIndex(index); + if (m_isJSArray && asArray(m_object.get())->canGetIndex(index)) + value = asArray(m_object.get())->getIndex(index); else { - PropertySlot slot(m_object); + PropertySlot slot(m_object.get()); if (!m_object->getOwnPropertySlot(exec, index, slot)) slot.setUndefined(); if (exec->hadException()) @@ -543,10 +532,10 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui stringifier.startNewLine(builder); // Append the stringified value. - stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object, index); + stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), index); } else { // Get the value. - PropertySlot slot(m_object); + PropertySlot slot(m_object.get()); Identifier& propertyName = m_propertyNames->propertyNameVector()[index]; if (!m_object->getOwnPropertySlot(exec, propertyName, slot)) return true; @@ -554,7 +543,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui if (exec->hadException()) return false; - rollBackPoint = builder.size(); + rollBackPoint = builder.length(); // Append the separator string. if (builder[rollBackPoint - 1] != '{') @@ -568,7 +557,7 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui builder.append(' '); // Append the stringified value. - stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object, propertyName); + stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), propertyName); } // From this point on, no access to the this pointer or to any members, because the @@ -594,12 +583,12 @@ bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, StringBui // ------------------------------ JSONObject -------------------------------- -const ClassInfo JSONObject::info = { "JSON", 0, 0, ExecState::jsonTable }; +const ClassInfo JSONObject::s_info = { "JSON", &JSObjectWithGlobalObject::s_info, 0, ExecState::jsonTable }; /* Source for JSONObject.lut.h @begin jsonTable - parse JSONProtoFuncParse DontEnum|Function 1 - stringify JSONProtoFuncStringify DontEnum|Function 1 + parse JSONProtoFuncParse DontEnum|Function 2 + stringify JSONProtoFuncStringify DontEnum|Function 3 @end */ @@ -615,16 +604,11 @@ bool JSONObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& pro return getStaticFunctionDescriptor(exec, ExecState::jsonTable(exec), this, propertyName, descriptor); } -void JSONObject::markStringifiers(MarkStack& markStack, Stringifier* stringifier) -{ - stringifier->markAggregate(markStack); -} - class Walker { public: - Walker(ExecState* exec, JSObject* function, CallType callType, CallData callData) + Walker(ExecState* exec, Handle function, CallType callType, CallData callData) : m_exec(exec) - , m_function(function) + , m_function(exec->globalData(), function) , m_callType(callType) , m_callData(callData) { @@ -635,13 +619,13 @@ private: { JSValue args[] = { property, unfiltered }; ArgList argList(args, 2); - return call(m_exec, m_function, m_callType, m_callData, thisObj, argList); + return call(m_exec, m_function.get(), m_callType, m_callData, thisObj, argList); } friend class Holder; ExecState* m_exec; - JSObject* m_function; + Local m_function; CallType m_callType; CallData m_callData; }; @@ -655,8 +639,8 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) { Vector propertyStack; Vector indexStack; - Vector objectStack; - Vector arrayStack; + LocalStack objectStack(m_exec->globalData()); + LocalStack arrayStack(m_exec->globalData()); Vector stateStack; WalkerState state = StateUnknown; @@ -671,32 +655,28 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) arrayStartState: case ArrayStartState: { ASSERT(inValue.isObject()); - ASSERT(isJSArray(&m_exec->globalData(), asObject(inValue)) || asObject(inValue)->inherits(&JSArray::info)); - if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) { - m_exec->setException(createStackOverflowError(m_exec)); - return jsUndefined(); - } + ASSERT(isJSArray(&m_exec->globalData(), asObject(inValue)) || asObject(inValue)->inherits(&JSArray::s_info)); + if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) + return throwError(m_exec, createStackOverflowError(m_exec)); JSArray* array = asArray(inValue); - arrayStack.append(array); + arrayStack.push(array); indexStack.append(0); // fallthrough } arrayStartVisitMember: case ArrayStartVisitMember: { if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) { - m_exec->setException(createInterruptedExecutionException(&m_exec->globalData())); - return jsUndefined(); - } + if (localTimeoutChecker.didTimeOut(m_exec)) + return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); tickCount = localTimeoutChecker.ticksUntilNextCheck(); } - JSArray* array = arrayStack.last(); + JSArray* array = arrayStack.peek(); uint32_t index = indexStack.last(); if (index == array->length()) { outValue = array; - arrayStack.removeLast(); + arrayStack.pop(); indexStack.removeLast(); break; } @@ -718,13 +698,13 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) // fallthrough } case ArrayEndVisitMember: { - JSArray* array = arrayStack.last(); - JSValue filteredValue = callReviver(array, jsString(m_exec, UString::from(indexStack.last())), outValue); + JSArray* array = arrayStack.peek(); + JSValue filteredValue = callReviver(array, jsString(m_exec, UString::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(indexStack.last(), filteredValue); + array->setIndex(m_exec->globalData(), indexStack.last(), filteredValue); else array->put(m_exec, indexStack.last(), filteredValue); } @@ -736,14 +716,12 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) objectStartState: case ObjectStartState: { ASSERT(inValue.isObject()); - ASSERT(!isJSArray(&m_exec->globalData(), asObject(inValue)) && !asObject(inValue)->inherits(&JSArray::info)); - if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) { - m_exec->setException(createStackOverflowError(m_exec)); - return jsUndefined(); - } + ASSERT(!isJSArray(&m_exec->globalData(), asObject(inValue)) && !asObject(inValue)->inherits(&JSArray::s_info)); + if (objectStack.size() + arrayStack.size() > maximumFilterRecursion) + return throwError(m_exec, createStackOverflowError(m_exec)); JSObject* object = asObject(inValue); - objectStack.append(object); + objectStack.push(object); indexStack.append(0); propertyStack.append(PropertyNameArray(m_exec)); object->getOwnPropertyNames(m_exec, propertyStack.last()); @@ -752,19 +730,17 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) objectStartVisitMember: case ObjectStartVisitMember: { if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) { - m_exec->setException(createInterruptedExecutionException(&m_exec->globalData())); - return jsUndefined(); - } + if (localTimeoutChecker.didTimeOut(m_exec)) + return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); tickCount = localTimeoutChecker.ticksUntilNextCheck(); } - JSObject* object = objectStack.last(); + JSObject* object = objectStack.peek(); uint32_t index = indexStack.last(); PropertyNameArray& properties = propertyStack.last(); if (index == properties.size()) { outValue = object; - objectStack.removeLast(); + objectStack.pop(); indexStack.removeLast(); propertyStack.removeLast(); break; @@ -787,7 +763,7 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) // fallthrough } case ObjectEndVisitMember: { - JSObject* object = objectStack.last(); + JSObject* object = objectStack.peek(); Identifier prop = propertyStack.last()[indexStack.last()]; PutPropertySlot slot; JSValue filteredValue = callReviver(object, jsString(m_exec, prop.ustring()), outValue); @@ -807,7 +783,7 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) break; } JSObject* object = asObject(inValue); - if (isJSArray(&m_exec->globalData(), object) || object->inherits(&JSArray::info)) + if (isJSArray(&m_exec->globalData(), object) || object->inherits(&JSArray::s_info)) goto arrayStartState; goto objectStartState; } @@ -818,10 +794,8 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) stateStack.removeLast(); if (!--tickCount) { - if (localTimeoutChecker.didTimeOut(m_exec)) { - m_exec->setException(createInterruptedExecutionException(&m_exec->globalData())); - return jsUndefined(); - } + if (localTimeoutChecker.didTimeOut(m_exec)) + return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData())); tickCount = localTimeoutChecker.ticksUntilNextCheck(); } } @@ -832,40 +806,51 @@ NEVER_INLINE JSValue Walker::walk(JSValue unfiltered) } // ECMA-262 v5 15.12.2 -JSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec) { - if (args.isEmpty()) - return throwError(exec, GeneralError, "JSON.parse requires at least one parameter"); - JSValue value = args.at(0); + 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); if (exec->hadException()) - return jsNull(); - - LiteralParser jsonParser(exec, source, LiteralParser::StrictJSON); + return JSValue::encode(jsNull()); + + LocalScope scope(exec->globalData()); + LiteralParser jsonParser(exec, source.characters(), source.length(), LiteralParser::StrictJSON); JSValue unfiltered = jsonParser.tryLiteralParse(); if (!unfiltered) - return throwError(exec, SyntaxError, "Unable to parse JSON string"); + return throwVMError(exec, createSyntaxError(exec, "Unable to parse JSON string")); - if (args.size() < 2) - return unfiltered; + if (exec->argumentCount() < 2) + return JSValue::encode(unfiltered); - JSValue function = args.at(1); + JSValue function = exec->argument(1); CallData callData; - CallType callType = function.getCallData(callData); + CallType callType = getCallData(function, callData); if (callType == CallTypeNone) - return unfiltered; - return Walker(exec, asObject(function), callType, callData).walk(unfiltered); + return JSValue::encode(unfiltered); + return JSValue::encode(Walker(exec, Local(exec->globalData(), asObject(function)), callType, callData).walk(unfiltered)); } // ECMA-262 v5 15.12.3 -JSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState* exec, JSObject*, JSValue, const ArgList& args) +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()); +} + +UString JSONStringify(ExecState* exec, JSValue value, unsigned indent) { - if (args.isEmpty()) - return throwError(exec, GeneralError, "No input to stringify"); - JSValue value = args.at(0); - JSValue replacer = args.at(1); - JSValue space = args.at(2); - return Stringifier(exec, replacer, space).stringify(value); + LocalScope scope(exec->globalData()); + Local result = Stringifier(exec, Local(exec->globalData(), jsNull()), Local(exec->globalData(), jsNumber(indent))).stringify(Local(exec->globalData(), value)); + if (result.isUndefinedOrNull()) + return UString(); + return result.getString(exec); } } // namespace JSC