2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "JSONObject.h"
29 #include "BooleanObject.h"
31 #include "ExceptionHelpers.h"
33 #include "JSGlobalObject.h"
34 #include "LiteralParser.h"
36 #include "LocalScope.h"
38 #include "PropertyNameArray.h"
39 #include "UStringBuilder.h"
40 #include "UStringConcatenate.h"
41 #include <wtf/MathExtras.h>
45 ASSERT_CLASS_FITS_IN_CELL(JSONObject);
47 static EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*);
48 static EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*);
52 #include "JSONObject.lut.h"
56 JSONObject::JSONObject(JSGlobalObject* globalObject, Structure* structure)
57 : JSNonFinalObject(globalObject->globalData(), structure)
61 void JSONObject::finishCreation(JSGlobalObject* globalObject)
63 Base::finishCreation(globalObject->globalData());
64 ASSERT(inherits(&s_info));
67 // PropertyNameForFunctionCall objects must be on the stack, since the JSValue that they create is not marked.
68 class PropertyNameForFunctionCall {
70 PropertyNameForFunctionCall(const Identifier&);
71 PropertyNameForFunctionCall(unsigned);
73 JSValue value(ExecState*) const;
76 const Identifier* m_identifier;
78 mutable JSValue m_value;
82 WTF_MAKE_NONCOPYABLE(Stringifier);
84 Stringifier(ExecState*, const Local<Unknown>& replacer, const Local<Unknown>& space);
85 Local<Unknown> stringify(Handle<Unknown>);
87 void visitAggregate(SlotVisitor&);
92 Holder(JSGlobalData&, JSObject*);
94 JSObject* object() const { return m_object.get(); }
96 bool appendNextProperty(Stringifier&, UStringBuilder&);
99 Local<JSObject> m_object;
100 const bool m_isArray;
104 RefPtr<PropertyNameArrayData> m_propertyNames;
109 static void appendQuotedString(UStringBuilder&, const UString&);
111 JSValue toJSON(JSValue, const PropertyNameForFunctionCall&);
113 enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedValue };
114 StringifyResult appendStringifiedValue(UStringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&);
116 bool willIndent() const;
119 void startNewLine(UStringBuilder&) const;
121 ExecState* const m_exec;
122 const Local<Unknown> m_replacer;
123 bool m_usingArrayReplacer;
124 PropertyNameArray m_arrayReplacerPropertyNames;
125 CallType m_replacerCallType;
126 CallData m_replacerCallData;
129 Vector<Holder, 16> m_holderStack;
130 UString m_repeatedGap;
134 // ------------------------------ helper functions --------------------------------
136 static inline JSValue unwrapBoxedPrimitive(ExecState* exec, JSValue value)
138 if (!value.isObject())
140 JSObject* object = asObject(value);
141 if (object->inherits(&NumberObject::s_info))
142 return jsNumber(object->toNumber(exec));
143 if (object->inherits(&StringObject::s_info))
144 return jsString(exec, object->toString(exec));
145 if (object->inherits(&BooleanObject::s_info))
146 return object->toPrimitive(exec);
150 static inline UString gap(ExecState* exec, JSValue space)
152 const unsigned maxGapLength = 10;
153 space = unwrapBoxedPrimitive(exec, space);
155 // If the space value is a number, create a gap string with that number of spaces.
156 if (space.isNumber()) {
157 double spaceCount = space.asNumber();
159 if (spaceCount > maxGapLength)
160 count = maxGapLength;
161 else if (!(spaceCount > 0))
164 count = static_cast<int>(spaceCount);
165 UChar spaces[maxGapLength];
166 for (int i = 0; i < count; ++i)
168 return UString(spaces, count);
171 // If the space value is a string, use it as the gap string, otherwise use no gap string.
172 UString spaces = space.getString(exec);
173 if (spaces.length() > maxGapLength) {
174 spaces = spaces.substringSharingImpl(0, maxGapLength);
179 // ------------------------------ PropertyNameForFunctionCall --------------------------------
181 inline PropertyNameForFunctionCall::PropertyNameForFunctionCall(const Identifier& identifier)
182 : m_identifier(&identifier)
186 inline PropertyNameForFunctionCall::PropertyNameForFunctionCall(unsigned number)
192 JSValue PropertyNameForFunctionCall::value(ExecState* exec) const
196 m_value = jsString(exec, m_identifier->ustring());
198 m_value = jsNumber(m_number);
203 // ------------------------------ Stringifier --------------------------------
205 Stringifier::Stringifier(ExecState* exec, const Local<Unknown>& replacer, const Local<Unknown>& space)
207 , m_replacer(replacer)
208 , m_usingArrayReplacer(false)
209 , m_arrayReplacerPropertyNames(exec)
210 , m_replacerCallType(CallTypeNone)
211 , m_gap(gap(exec, space.get()))
213 if (!m_replacer.isObject())
216 if (m_replacer.asObject()->inherits(&JSArray::s_info)) {
217 m_usingArrayReplacer = true;
218 Handle<JSObject> array = m_replacer.asObject();
219 unsigned length = array->get(exec, exec->globalData().propertyNames->length).toUInt32(exec);
220 for (unsigned i = 0; i < length; ++i) {
221 JSValue name = array->get(exec, i);
222 if (exec->hadException())
225 UString propertyName;
226 if (name.getString(exec, propertyName)) {
227 m_arrayReplacerPropertyNames.add(Identifier(exec, propertyName));
231 if (name.isNumber()) {
232 m_arrayReplacerPropertyNames.add(Identifier::from(exec, name.asNumber()));
236 if (name.isObject()) {
237 if (!asObject(name)->inherits(&NumberObject::s_info) && !asObject(name)->inherits(&StringObject::s_info))
239 propertyName = name.toString(exec);
240 if (exec->hadException())
242 m_arrayReplacerPropertyNames.add(Identifier(exec, propertyName));
248 m_replacerCallType = m_replacer.asObject()->methodTable()->getCallData(m_replacer.asObject().get(), m_replacerCallData);
251 Local<Unknown> Stringifier::stringify(Handle<Unknown> value)
253 JSObject* object = constructEmptyObject(m_exec);
254 if (m_exec->hadException())
255 return Local<Unknown>(m_exec->globalData(), jsNull());
257 PropertyNameForFunctionCall emptyPropertyName(m_exec->globalData().propertyNames->emptyIdentifier);
258 object->putDirect(m_exec->globalData(), m_exec->globalData().propertyNames->emptyIdentifier, value.get());
260 UStringBuilder result;
261 if (appendStringifiedValue(result, value.get(), object, emptyPropertyName) != StringifySucceeded)
262 return Local<Unknown>(m_exec->globalData(), jsUndefined());
263 if (m_exec->hadException())
264 return Local<Unknown>(m_exec->globalData(), jsNull());
266 return Local<Unknown>(m_exec->globalData(), jsString(m_exec, result.toUString()));
269 void Stringifier::appendQuotedString(UStringBuilder& builder, const UString& value)
271 int length = value.length();
275 const UChar* data = value.characters();
276 for (int i = 0; i < length; ++i) {
278 while (i < length && (data[i] > 0x1F && data[i] != '"' && data[i] != '\\'))
280 builder.append(data + start, i - start);
285 builder.append('\\');
289 builder.append('\\');
293 builder.append('\\');
297 builder.append('\\');
301 builder.append('\\');
305 builder.append('\\');
309 builder.append('\\');
310 builder.append('\\');
313 static const char hexDigits[] = "0123456789abcdef";
315 UChar hex[] = { '\\', 'u', hexDigits[(ch >> 12) & 0xF], hexDigits[(ch >> 8) & 0xF], hexDigits[(ch >> 4) & 0xF], hexDigits[ch & 0xF] };
316 builder.append(hex, WTF_ARRAY_LENGTH(hex));
324 inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName)
326 ASSERT(!m_exec->hadException());
327 if (!value.isObject() || !asObject(value)->hasProperty(m_exec, m_exec->globalData().propertyNames->toJSON))
330 JSValue toJSONFunction = asObject(value)->get(m_exec, m_exec->globalData().propertyNames->toJSON);
331 if (m_exec->hadException())
334 if (!toJSONFunction.isObject())
337 JSObject* object = asObject(toJSONFunction);
339 CallType callType = object->methodTable()->getCallData(object, callData);
340 if (callType == CallTypeNone)
343 MarkedArgumentBuffer args;
344 args.append(propertyName.value(m_exec));
345 return call(m_exec, object, callType, callData, value, args);
348 Stringifier::StringifyResult Stringifier::appendStringifiedValue(UStringBuilder& builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall& propertyName)
350 // Call the toJSON function.
351 value = toJSON(value, propertyName);
352 if (m_exec->hadException())
353 return StringifyFailed;
355 // Call the replacer function.
356 if (m_replacerCallType != CallTypeNone) {
357 MarkedArgumentBuffer args;
358 args.append(propertyName.value(m_exec));
360 value = call(m_exec, m_replacer.get(), m_replacerCallType, m_replacerCallData, holder, args);
361 if (m_exec->hadException())
362 return StringifyFailed;
365 if (value.isUndefined() && !holder->inherits(&JSArray::s_info))
366 return StringifyFailedDueToUndefinedValue;
368 if (value.isNull()) {
369 builder.append("null");
370 return StringifySucceeded;
373 value = unwrapBoxedPrimitive(m_exec, value);
375 if (m_exec->hadException())
376 return StringifyFailed;
378 if (value.isBoolean()) {
379 builder.append(value.isTrue() ? "true" : "false");
380 return StringifySucceeded;
384 if (value.getString(m_exec, stringValue)) {
385 appendQuotedString(builder, stringValue);
386 return StringifySucceeded;
389 if (value.isNumber()) {
390 double number = value.asNumber();
391 if (!isfinite(number))
392 builder.append("null");
394 builder.append(UString::number(number));
395 return StringifySucceeded;
398 if (!value.isObject())
399 return StringifyFailed;
401 JSObject* object = asObject(value);
404 if (object->methodTable()->getCallData(object, callData) != CallTypeNone) {
405 if (holder->inherits(&JSArray::s_info)) {
406 builder.append("null");
407 return StringifySucceeded;
409 return StringifyFailedDueToUndefinedValue;
412 // Handle cycle detection, and put the holder on the stack.
413 for (unsigned i = 0; i < m_holderStack.size(); i++) {
414 if (m_holderStack[i].object() == object) {
415 throwError(m_exec, createTypeError(m_exec, "JSON.stringify cannot serialize cyclic structures."));
416 return StringifyFailed;
419 bool holderStackWasEmpty = m_holderStack.isEmpty();
420 m_holderStack.append(Holder(m_exec->globalData(), object));
421 if (!holderStackWasEmpty)
422 return StringifySucceeded;
424 // If this is the outermost call, then loop to handle everything on the holder stack.
425 TimeoutChecker localTimeoutChecker(m_exec->globalData().timeoutChecker);
426 localTimeoutChecker.reset();
427 unsigned tickCount = localTimeoutChecker.ticksUntilNextCheck();
429 while (m_holderStack.last().appendNextProperty(*this, builder)) {
430 if (m_exec->hadException())
431 return StringifyFailed;
433 if (localTimeoutChecker.didTimeOut(m_exec)) {
434 throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData()));
435 return StringifyFailed;
437 tickCount = localTimeoutChecker.ticksUntilNextCheck();
440 m_holderStack.removeLast();
441 } while (!m_holderStack.isEmpty());
442 return StringifySucceeded;
445 inline bool Stringifier::willIndent() const
447 return !m_gap.isEmpty();
450 inline void Stringifier::indent()
452 // Use a single shared string, m_repeatedGap, so we don't keep allocating new ones as we indent and unindent.
453 unsigned newSize = m_indent.length() + m_gap.length();
454 if (newSize > m_repeatedGap.length())
455 m_repeatedGap = makeUString(m_repeatedGap, m_gap);
456 ASSERT(newSize <= m_repeatedGap.length());
457 m_indent = m_repeatedGap.substringSharingImpl(0, newSize);
460 inline void Stringifier::unindent()
462 ASSERT(m_indent.length() >= m_gap.length());
463 m_indent = m_repeatedGap.substringSharingImpl(0, m_indent.length() - m_gap.length());
466 inline void Stringifier::startNewLine(UStringBuilder& builder) const
470 builder.append('\n');
471 builder.append(m_indent);
474 inline Stringifier::Holder::Holder(JSGlobalData& globalData, JSObject* object)
475 : m_object(globalData, object)
476 , m_isArray(object->inherits(&JSArray::s_info))
481 bool Stringifier::Holder::appendNextProperty(Stringifier& stringifier, UStringBuilder& builder)
483 ASSERT(m_index <= m_size);
485 ExecState* exec = stringifier.m_exec;
487 // First time through, initialize.
490 m_isJSArray = isJSArray(&exec->globalData(), m_object.get());
491 m_size = m_object->get(exec, exec->globalData().propertyNames->length).toUInt32(exec);
494 if (stringifier.m_usingArrayReplacer)
495 m_propertyNames = stringifier.m_arrayReplacerPropertyNames.data();
497 PropertyNameArray objectPropertyNames(exec);
498 m_object->methodTable()->getOwnPropertyNames(m_object.get(), exec, objectPropertyNames, ExcludeDontEnumProperties);
499 m_propertyNames = objectPropertyNames.releaseData();
501 m_size = m_propertyNames->propertyNameVector().size();
504 stringifier.indent();
507 // Last time through, finish up and return false.
508 if (m_index == m_size) {
509 stringifier.unindent();
510 if (m_size && builder[builder.length() - 1] != '{')
511 stringifier.startNewLine(builder);
512 builder.append(m_isArray ? ']' : '}');
516 // Handle a single element of the array or object.
517 unsigned index = m_index++;
518 unsigned rollBackPoint = 0;
519 StringifyResult stringifyResult;
523 if (m_isJSArray && asArray(m_object.get())->canGetIndex(index))
524 value = asArray(m_object.get())->getIndex(index);
526 PropertySlot slot(m_object.get());
527 if (!m_object->methodTable()->getOwnPropertySlotByIndex(m_object.get(), exec, index, slot))
529 if (exec->hadException())
531 value = slot.getValue(exec, index);
534 // Append the separator string.
537 stringifier.startNewLine(builder);
539 // Append the stringified value.
540 stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), index);
543 PropertySlot slot(m_object.get());
544 Identifier& propertyName = m_propertyNames->propertyNameVector()[index];
545 if (!m_object->methodTable()->getOwnPropertySlot(m_object.get(), exec, propertyName, slot))
547 JSValue value = slot.getValue(exec, propertyName);
548 if (exec->hadException())
551 rollBackPoint = builder.length();
553 // Append the separator string.
554 if (builder[rollBackPoint - 1] != '{')
556 stringifier.startNewLine(builder);
558 // Append the property name.
559 appendQuotedString(builder, propertyName.ustring());
561 if (stringifier.willIndent())
564 // Append the stringified value.
565 stringifyResult = stringifier.appendStringifiedValue(builder, value, m_object.get(), propertyName);
568 // From this point on, no access to the this pointer or to any members, because the
569 // Holder object may have moved if the call to stringify pushed a new Holder onto
572 switch (stringifyResult) {
573 case StringifyFailed:
574 builder.append("null");
576 case StringifySucceeded:
578 case StringifyFailedDueToUndefinedValue:
579 // This only occurs when get an undefined value for an object property.
580 // In this case we don't want the separator and property name that we
581 // already appended, so roll back.
582 builder.resize(rollBackPoint);
589 // ------------------------------ JSONObject --------------------------------
591 const ClassInfo JSONObject::s_info = { "JSON", &JSNonFinalObject::s_info, 0, ExecState::jsonTable, CREATE_METHOD_TABLE(JSONObject) };
593 /* Source for JSONObject.lut.h
595 parse JSONProtoFuncParse DontEnum|Function 2
596 stringify JSONProtoFuncStringify DontEnum|Function 3
602 bool JSONObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
604 return getStaticFunctionSlot<JSObject>(exec, ExecState::jsonTable(exec), jsCast<JSONObject*>(cell), propertyName, slot);
607 bool JSONObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
609 return getStaticFunctionDescriptor<JSObject>(exec, ExecState::jsonTable(exec), jsCast<JSONObject*>(object), propertyName, descriptor);
614 Walker(ExecState* exec, Handle<JSObject> function, CallType callType, CallData callData)
616 , m_function(exec->globalData(), function)
617 , m_callType(callType)
618 , m_callData(callData)
621 JSValue walk(JSValue unfiltered);
623 JSValue callReviver(JSObject* thisObj, JSValue property, JSValue unfiltered)
625 MarkedArgumentBuffer args;
626 args.append(property);
627 args.append(unfiltered);
628 return call(m_exec, m_function.get(), m_callType, m_callData, thisObj, args);
634 Local<JSObject> m_function;
639 // We clamp recursion well beyond anything reasonable, but we also have a timeout check
640 // to guard against "infinite" execution by inserting arbitrarily large objects.
641 static const unsigned maximumFilterRecursion = 40000;
642 enum WalkerState { StateUnknown, ArrayStartState, ArrayStartVisitMember, ArrayEndVisitMember,
643 ObjectStartState, ObjectStartVisitMember, ObjectEndVisitMember };
644 NEVER_INLINE JSValue Walker::walk(JSValue unfiltered)
646 Vector<PropertyNameArray, 16> propertyStack;
647 Vector<uint32_t, 16> indexStack;
648 LocalStack<JSObject, 16> objectStack(m_exec->globalData());
649 LocalStack<JSArray, 16> arrayStack(m_exec->globalData());
651 Vector<WalkerState, 16> stateStack;
652 WalkerState state = StateUnknown;
653 JSValue inValue = unfiltered;
654 JSValue outValue = jsNull();
656 TimeoutChecker localTimeoutChecker(m_exec->globalData().timeoutChecker);
657 localTimeoutChecker.reset();
658 unsigned tickCount = localTimeoutChecker.ticksUntilNextCheck();
662 case ArrayStartState: {
663 ASSERT(inValue.isObject());
664 ASSERT(isJSArray(&m_exec->globalData(), asObject(inValue)) || asObject(inValue)->inherits(&JSArray::s_info));
665 if (objectStack.size() + arrayStack.size() > maximumFilterRecursion)
666 return throwError(m_exec, createStackOverflowError(m_exec));
668 JSArray* array = asArray(inValue);
669 arrayStack.push(array);
670 indexStack.append(0);
673 arrayStartVisitMember:
674 case ArrayStartVisitMember: {
676 if (localTimeoutChecker.didTimeOut(m_exec))
677 return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData()));
678 tickCount = localTimeoutChecker.ticksUntilNextCheck();
681 JSArray* array = arrayStack.peek();
682 uint32_t index = indexStack.last();
683 if (index == array->length()) {
686 indexStack.removeLast();
689 if (isJSArray(&m_exec->globalData(), array) && array->canGetIndex(index))
690 inValue = array->getIndex(index);
693 if (array->methodTable()->getOwnPropertySlotByIndex(array, m_exec, index, slot))
694 inValue = slot.getValue(m_exec, index);
696 inValue = jsUndefined();
699 if (inValue.isObject()) {
700 stateStack.append(ArrayEndVisitMember);
706 case ArrayEndVisitMember: {
707 JSArray* array = arrayStack.peek();
708 JSValue filteredValue = callReviver(array, jsString(m_exec, UString::number(indexStack.last())), outValue);
709 if (filteredValue.isUndefined())
710 array->methodTable()->deletePropertyByIndex(array, m_exec, indexStack.last());
712 if (isJSArray(&m_exec->globalData(), array) && array->canSetIndex(indexStack.last()))
713 array->setIndex(m_exec->globalData(), indexStack.last(), filteredValue);
715 array->methodTable()->putByIndex(array, m_exec, indexStack.last(), filteredValue);
717 if (m_exec->hadException())
720 goto arrayStartVisitMember;
723 case ObjectStartState: {
724 ASSERT(inValue.isObject());
725 ASSERT(!isJSArray(&m_exec->globalData(), asObject(inValue)) && !asObject(inValue)->inherits(&JSArray::s_info));
726 if (objectStack.size() + arrayStack.size() > maximumFilterRecursion)
727 return throwError(m_exec, createStackOverflowError(m_exec));
729 JSObject* object = asObject(inValue);
730 objectStack.push(object);
731 indexStack.append(0);
732 propertyStack.append(PropertyNameArray(m_exec));
733 object->methodTable()->getOwnPropertyNames(object, m_exec, propertyStack.last(), ExcludeDontEnumProperties);
736 objectStartVisitMember:
737 case ObjectStartVisitMember: {
739 if (localTimeoutChecker.didTimeOut(m_exec))
740 return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData()));
741 tickCount = localTimeoutChecker.ticksUntilNextCheck();
744 JSObject* object = objectStack.peek();
745 uint32_t index = indexStack.last();
746 PropertyNameArray& properties = propertyStack.last();
747 if (index == properties.size()) {
750 indexStack.removeLast();
751 propertyStack.removeLast();
755 if (object->methodTable()->getOwnPropertySlot(object, m_exec, properties[index], slot))
756 inValue = slot.getValue(m_exec, properties[index]);
758 inValue = jsUndefined();
760 // The holder may be modified by the reviver function so any lookup may throw
761 if (m_exec->hadException())
764 if (inValue.isObject()) {
765 stateStack.append(ObjectEndVisitMember);
771 case ObjectEndVisitMember: {
772 JSObject* object = objectStack.peek();
773 Identifier prop = propertyStack.last()[indexStack.last()];
774 PutPropertySlot slot;
775 JSValue filteredValue = callReviver(object, jsString(m_exec, prop.ustring()), outValue);
776 if (filteredValue.isUndefined())
777 object->methodTable()->deleteProperty(object, m_exec, prop);
779 object->methodTable()->put(object, m_exec, prop, filteredValue, slot);
780 if (m_exec->hadException())
783 goto objectStartVisitMember;
787 if (!inValue.isObject()) {
791 JSObject* object = asObject(inValue);
792 if (isJSArray(&m_exec->globalData(), object) || object->inherits(&JSArray::s_info))
793 goto arrayStartState;
794 goto objectStartState;
796 if (stateStack.isEmpty())
799 state = stateStack.last();
800 stateStack.removeLast();
803 if (localTimeoutChecker.didTimeOut(m_exec))
804 return throwError(m_exec, createInterruptedExecutionException(&m_exec->globalData()));
805 tickCount = localTimeoutChecker.ticksUntilNextCheck();
808 JSObject* finalHolder = constructEmptyObject(m_exec);
809 PutPropertySlot slot;
810 finalHolder->methodTable()->put(finalHolder, m_exec, m_exec->globalData().propertyNames->emptyIdentifier, outValue, slot);
811 return callReviver(finalHolder, jsEmptyString(m_exec), outValue);
814 // ECMA-262 v5 15.12.2
815 EncodedJSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec)
817 if (!exec->argumentCount())
818 return throwVMError(exec, createError(exec, "JSON.parse requires at least one parameter"));
819 JSValue value = exec->argument(0);
820 UString source = value.toString(exec);
821 if (exec->hadException())
822 return JSValue::encode(jsNull());
825 LocalScope scope(exec->globalData());
826 if (source.is8Bit()) {
827 LiteralParser<LChar> jsonParser(exec, source.characters8(), source.length(), StrictJSON);
828 unfiltered = jsonParser.tryLiteralParse();
830 return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage()));
832 LiteralParser<UChar> jsonParser(exec, source.characters16(), source.length(), StrictJSON);
833 unfiltered = jsonParser.tryLiteralParse();
835 return throwVMError(exec, createSyntaxError(exec, jsonParser.getErrorMessage()));
838 if (exec->argumentCount() < 2)
839 return JSValue::encode(unfiltered);
841 JSValue function = exec->argument(1);
843 CallType callType = getCallData(function, callData);
844 if (callType == CallTypeNone)
845 return JSValue::encode(unfiltered);
846 return JSValue::encode(Walker(exec, Local<JSObject>(exec->globalData(), asObject(function)), callType, callData).walk(unfiltered));
849 // ECMA-262 v5 15.12.3
850 EncodedJSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState* exec)
852 if (!exec->argumentCount())
853 return throwVMError(exec, createError(exec, "No input to stringify"));
854 LocalScope scope(exec->globalData());
855 Local<Unknown> value(exec->globalData(), exec->argument(0));
856 Local<Unknown> replacer(exec->globalData(), exec->argument(1));
857 Local<Unknown> space(exec->globalData(), exec->argument(2));
858 return JSValue::encode(Stringifier(exec, replacer, space).stringify(value).get());
861 UString JSONStringify(ExecState* exec, JSValue value, unsigned indent)
863 LocalScope scope(exec->globalData());
864 Local<Unknown> result = Stringifier(exec, Local<Unknown>(exec->globalData(), jsNull()), Local<Unknown>(exec->globalData(), jsNumber(indent))).stringify(Local<Unknown>(exec->globalData(), value));
865 if (result.isUndefinedOrNull())
867 return result.getString(exec);