source: webkit/trunk/JavaScriptCore/runtime/JSONObject.h@ 45011

Last change on this file since 45011 was 44813, checked in by Darin Adler, 16 years ago

JavaScriptCore:

2009-06-17 Darin Adler <Darin Adler>

Reviewed by Oliver Hunt.

Bug 26429: Make JSON.stringify non-recursive so it can handle objects
of arbitrary complexity
https://bugs.webkit.org/show_bug.cgi?id=26429

For marking I decided not to use gcProtect, because this is inside the engine
so it's easy enough to just do marking. And that darned gcProtect does locking!
Oliver tried to convince me to used MarkedArgumentBuffer, but the constructor
for that class says "FIXME: Remove all clients of this API, then remove this API."

  • runtime/Collector.cpp: (JSC::Heap::collect): Add a call to JSONObject::markStringifiers.
  • runtime/CommonIdentifiers.cpp: (JSC::CommonIdentifiers::CommonIdentifiers): Added emptyIdentifier.
  • runtime/CommonIdentifiers.h: Ditto.
  • runtime/JSGlobalData.cpp: (JSC::JSGlobalData::JSGlobalData): Initialize firstStringifierToMark to 0.
  • runtime/JSGlobalData.h: Added firstStringifierToMark.
  • runtime/JSONObject.cpp: Cut down the includes to the needed ones only. (JSC::unwrapNumberOrString): Added. Helper for unwrapping number and string objects to get their number and string values. (JSC::ReplacerPropertyName::ReplacerPropertyName): Added. The class is used to wrap an identifier or integer so we don't have to do any work unless we actually call a replacer. (JSC::ReplacerPropertyName::value): Added. (JSC::gap): Added. Helper function for the Stringifier constructor. (JSC::PropertyNameForFunctionCall::PropertyNameForFunctionCall): Added. The class is used to wrap an identifier or integer so we don't have to allocate a number or string until we actually call toJSON or a replacer. (JSC::PropertyNameForFunctionCall::asJSValue): Added. (JSC::Stringifier::Stringifier): Updated and moved out of the class definition. Added code to hook this into a singly linked list for marking. (JSC::Stringifier::~Stringifier): Remove from the singly linked list. (JSC::Stringifier::mark): Mark all the objects in the holder stacks. (JSC::Stringifier::stringify): Updated. (JSC::Stringifier::appendQuotedString): Tweaked and streamlined a bit. (JSC::Stringifier::toJSON): Renamed from toJSONValue. (JSC::Stringifier::appendStringifiedValue): Renamed from stringify. Added code to use the m_holderStack to do non-recursive stringify of objects and arrays. This code also uses the timeout checker since in pathological cases it could be slow even without calling into the JavaScript virtual machine. (JSC::Stringifier::willIndent): Added. (JSC::Stringifier::indent): Added. (JSC::Stringifier::unindent): Added. (JSC::Stringifier::startNewLine): Added. (JSC::Stringifier::Holder::Holder): Added. (JSC::Stringifier::Holder::appendNextProperty): Added. This is the function that handles the format of arrays and objects. (JSC::JSONObject::getOwnPropertySlot): Moved this down to the bottom of the file so the JSONObject class is not interleaved with the Stringifier class. (JSC::JSONObject::markStringifiers): Added. Calls mark. (JSC::JSONProtoFuncStringify): Streamlined the code here. The code to compute the gap string is now a separate function.
  • runtime/JSONObject.h: Made everything private. Added markStringifiers.

LayoutTests:

2009-06-17 Darin Adler <Darin Adler>

Reviewed by Oliver Hunt.

Bug 26429: Make JSON.stringify non-recursive so it can handle objects
of arbitrary complexity
https://bugs.webkit.org/show_bug.cgi?id=26429

  • fast/js/JSON-stringify-expected.txt: Updated.
  • fast/js/resources/JSON-stringify.js: Changed the infinite object and infinite array tests to instead just test something a fixed number of levels deep. Otherwise we end up with an infinite loop in the test, which would lead to the slow-script dialog in the production web browser. Also raised the number from 512 to 2048 since there's no fixed limit any more.
File size: 2.0 KB
Line 
1/*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef JSONObject_h
27#define JSONObject_h
28
29#include "JSObject.h"
30
31namespace JSC {
32
33 class Stringifier;
34
35 class JSONObject : public JSObject {
36 public:
37 JSONObject(PassRefPtr<Structure> structure)
38 : JSObject(structure)
39 {
40 }
41
42 static PassRefPtr<Structure> createStructure(JSValue prototype)
43 {
44 return Structure::create(prototype, TypeInfo(ObjectType));
45 }
46
47 static void markStringifiers(Stringifier*);
48
49 private:
50 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
51
52 virtual const ClassInfo* classInfo() const { return &info; }
53 static const ClassInfo info;
54 };
55
56} // namespace JSC
57
58#endif // JSONObject_h
Note: See TracBrowser for help on using the repository browser.