Changeset 44521 in webkit


Ignore:
Timestamp:
Jun 8, 2009 11:02:24 PM (15 years ago)
Author:
oliver@apple.com
Message:

Bug 26249: Support JSON.stringify
<https://bugs.webkit.org/show_bug.cgi?id=26249>

Reviewed by Sam Weinig.

Implement JSON.stringify. This patch handles all the semantics of the ES5
JSON.stringify function, including replacer functions and arrays and both
string and numeric gap arguments.

Currently uses a clamped recursive algorithm basically identical to the spec
description but with a few minor tweaks for performance and corrected semantics
discussed in the es-discuss mailing list.

Location:
trunk
Files:
6 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r44518 r44521  
     12009-06-07  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Bug 26249: Support JSON.stringify
     6        <https://bugs.webkit.org/show_bug.cgi?id=26249>
     7       
     8        Implement JSON.stringify.  This patch handles all the semantics of the ES5
     9        JSON.stringify function, including replacer functions and arrays and both
     10        string and numeric gap arguments.
     11       
     12        Currently uses a clamped recursive algorithm basically identical to the spec
     13        description but with a few minor tweaks for performance and corrected semantics
     14        discussed in the es-discuss mailing list.
     15
     16        * DerivedSources.make:
     17        * GNUmakefile.am:
     18        * JavaScriptCore.pri:
     19        * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
     20        * JavaScriptCore.xcodeproj/project.pbxproj:
     21        * interpreter/CallFrame.h:
     22        (JSC::ExecState::jsonTable):
     23        * runtime/CommonIdentifiers.h:
     24           add toJSON to the list of common identifiers
     25
     26        * runtime/JSGlobalData.cpp:
     27        (JSC::JSGlobalData::JSGlobalData):
     28        (JSC::JSGlobalData::~JSGlobalData):
     29        * runtime/JSGlobalData.h:
     30        * runtime/JSGlobalObject.cpp:
     31        (JSC::JSGlobalObject::reset):
     32            Add support for the JSON object lookup table
     33
     34        * runtime/JSONObject.cpp: Added.
     35        (JSC::):
     36        (JSC::JSONObject::getOwnPropertySlot):
     37        (JSC::Stringifier::):
     38        (JSC::Stringifier::Stringifier):
     39        (JSC::Stringifier::stringify):
     40        (JSC::Stringifier::appendString):
     41
     42        (JSC::Stringifier::StringKeyGenerator::StringKeyGenerator):
     43        (JSC::Stringifier::StringKeyGenerator::getKey):
     44        (JSC::Stringifier::IntKeyGenerator::IntKeyGenerator):
     45        (JSC::Stringifier::IntKeyGenerator::getKey):
     46            These KeyGenerator classes are used to abstract away the lazy evaluation of keys for
     47            toJSON and replacer functions.
     48
     49        (JSC::Stringifier::toJSONValue):
     50        (JSC::Stringifier::stringifyArray):
     51        (JSC::Stringifier::stringifyObject):
     52        (JSC::JSONProtoFuncStringify):
     53        * runtime/JSONObject.h: Added.
     54        (JSC::JSONObject:::JSObject):
     55        (JSC::JSONObject::classInfo):
     56        (JSC::JSONObject::createStructure):
     57
    1582009-06-08  Mark Rowe  <mrowe@apple.com>
    259
  • trunk/JavaScriptCore/DerivedSources.make

    r40187 r44521  
    4141    DatePrototype.lut.h \
    4242    Grammar.cpp \
     43    JSONObject.lut.h \
    4344    Lexer.lut.h \
    4445    MathObject.lut.h \
  • trunk/JavaScriptCore/GNUmakefile.am

    r44516 r44521  
    3636        JavaScriptCore/runtime/ArrayPrototype.lut.h \
    3737        JavaScriptCore/runtime/DatePrototype.lut.h \
     38        JavaScriptCore/runtime/JSONObject.lut.h \
    3839        JavaScriptCore/runtime/MathObject.lut.h \
    3940        JavaScriptCore/runtime/NumberConstructor.lut.h \
     
    183184        JavaScriptCore/runtime/JSNotAnObject.cpp \
    184185        JavaScriptCore/runtime/JSNotAnObject.h \
     186        JavaScriptCore/runtime/JSONObject.cpp \
     187        JavaScriptCore/runtime/JSONObject.h \
    185188        JavaScriptCore/runtime/JSPropertyNameIterator.cpp \
    186189        JavaScriptCore/runtime/JSPropertyNameIterator.h \
     
    591594        JavaScriptCore/runtime/ArrayPrototype.lut.h \
    592595        JavaScriptCore/runtime/DatePrototype.lut.h \
     596        JavaScriptCore/runtime/JSONObject.lut.h \
    593597        JavaScriptCore/runtime/MathObject.lut.h \
    594598        JavaScriptCore/runtime/NumberConstructor.lut.h \
  • trunk/JavaScriptCore/JavaScriptCore.pri

    r44508 r44521  
    111111    runtime/JSActivation.cpp \
    112112    runtime/JSNotAnObject.cpp \
     113    runtime/JSONObject.cpp \
    113114    runtime/LiteralParser.cpp \
    114115    runtime/TimeoutChecker.cpp \
  • trunk/JavaScriptCore/JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj

    r44508 r44521  
    794794                        <File
    795795                                RelativePath="..\..\runtime\JSNumberCell.h"
     796                                >
     797                        <File
     798                                RelativePath="..\..\runtime\JSONObject.cpp"
     799                                >
     800                        </File>
     801                        <File
     802                                RelativePath="..\..\runtime\JSONObject.h"
    796803                                >
    797804                        </File>
  • trunk/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r44514 r44521  
    199199                A7E2EA6B0FB460CF00601F06 /* LiteralParser.h in Headers */ = {isa = PBXBuildFile; fileRef = A7E2EA690FB460CF00601F06 /* LiteralParser.h */; };
    200200                A7E2EA6C0FB460CF00601F06 /* LiteralParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E2EA6A0FB460CF00601F06 /* LiteralParser.cpp */; };
     201                A7F9935F0FD7325100A0B2D0 /* JSONObject.h in Headers */ = {isa = PBXBuildFile; fileRef = A7F9935D0FD7325100A0B2D0 /* JSONObject.h */; };
     202                A7F993600FD7325100A0B2D0 /* JSONObject.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7F9935E0FD7325100A0B2D0 /* JSONObject.cpp */; };
     203                A7F9949B0FD746A300A0B2D0 /* JSONObject.lut.h in Headers */ = {isa = PBXBuildFile; fileRef = A7F9949A0FD746A300A0B2D0 /* JSONObject.lut.h */; };
    201204                BC02E90D0E1839DB000F9297 /* ErrorConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9050E1839DB000F9297 /* ErrorConstructor.h */; };
    202205                BC02E90F0E1839DB000F9297 /* ErrorPrototype.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9070E1839DB000F9297 /* ErrorPrototype.h */; };
     
    735738                A7F8690E0F9584A100558697 /* CachedCall.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CachedCall.h; sourceTree = "<group>"; };
    736739                A7F869EC0F95C2EC00558697 /* CallFrameClosure.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CallFrameClosure.h; sourceTree = "<group>"; };
     740                A7F9935D0FD7325100A0B2D0 /* JSONObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONObject.h; sourceTree = "<group>"; };
     741                A7F9935E0FD7325100A0B2D0 /* JSONObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSONObject.cpp; sourceTree = "<group>"; };
     742                A7F9949A0FD746A300A0B2D0 /* JSONObject.lut.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSONObject.lut.h; path = /Users/oliver/builds/Debug/DerivedSources/JavaScriptCore/JSONObject.lut.h; sourceTree = "<absolute>"; };
    737743                A8E894310CD0602400367179 /* JSCallbackObjectFunctions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCallbackObjectFunctions.h; sourceTree = "<group>"; };
    738744                A8E894330CD0603F00367179 /* JSGlobalObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGlobalObject.h; sourceTree = "<group>"; };
     
    11431149                                BC18C5230E16FC8A00B34460 /* ArrayPrototype.lut.h */,
    11441150                                BCD203E70E1718F4002C7E82 /* DatePrototype.lut.h */,
     1151                                A7F9949A0FD746A300A0B2D0 /* JSONObject.lut.h */,
    11451152                                BC18C5290E16FCC200B34460 /* MathObject.lut.h */,
    11461153                                BC2680E60E16D52300A06E92 /* NumberConstructor.lut.h */,
     
    14631470                                F692A8850255597D01FF60F7 /* UString.cpp */,
    14641471                                F692A8860255597D01FF60F7 /* UString.h */,
     1472                                A7F9935D0FD7325100A0B2D0 /* JSONObject.h */,
     1473                                A7F9935E0FD7325100A0B2D0 /* JSONObject.cpp */,
    14651474                        );
    14661475                        path = runtime;
     
    18471856                                0BDFFAE00FC6192900D69EF4 /* CrossThreadRefCounted.h in Headers */,
    18481857                                0BDFFAE10FC6193100D69EF4 /* OwnFastMallocPtr.h in Headers */,
     1858                                A7F9935F0FD7325100A0B2D0 /* JSONObject.h in Headers */,
     1859                                A7F9949B0FD746A300A0B2D0 /* JSONObject.lut.h in Headers */,
    18491860                                86ADD1450FDDEA980006EEC2 /* ARMv7Assembler.h in Headers */,
    18501861                                86ADD1460FDDEA980006EEC2 /* MacroAssemblerARMv7.h in Headers */,
     
    22102221                                93052C340FB792190048FDC3 /* ParserArena.cpp in Sources */,
    22112222                                BCDD51EB0FB8DF74004A8BDC /* JITOpcodes.cpp in Sources */,
     2223                                A7F993600FD7325100A0B2D0 /* JSONObject.cpp in Sources */,
    22122224                        );
    22132225                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/JavaScriptCore/interpreter/CallFrame.h

    r44344 r44521  
    9090        static const HashTable* arrayTable(CallFrame* callFrame) { return callFrame->globalData().arrayTable; }
    9191        static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->globalData().dateTable; }
     92        static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->globalData().jsonTable; }
    9293        static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->globalData().mathTable; }
    9394        static const HashTable* numberTable(CallFrame* callFrame) { return callFrame->globalData().numberTable; }
  • trunk/JavaScriptCore/runtime/CommonIdentifiers.h

    r44224 r44521  
    6060    macro(toExponential) \
    6161    macro(toFixed) \
     62    macro(toJSON) \
    6263    macro(toLocaleString) \
    6364    macro(toPrecision) \
  • trunk/JavaScriptCore/runtime/JSGlobalData.cpp

    r44417 r44521  
    6161
    6262extern const HashTable arrayTable;
     63extern const HashTable jsonTable;
    6364extern const HashTable dateTable;
    6465extern const HashTable mathTable;
     
    106107    , arrayTable(fastNew<HashTable>(JSC::arrayTable))
    107108    , dateTable(fastNew<HashTable>(JSC::dateTable))
     109    , jsonTable(fastNew<HashTable>(JSC::jsonTable))
    108110    , mathTable(fastNew<HashTable>(JSC::mathTable))
    109111    , numberTable(fastNew<HashTable>(JSC::numberTable))
     
    156158    arrayTable->deleteTable();
    157159    dateTable->deleteTable();
     160    jsonTable->deleteTable();
    158161    mathTable->deleteTable();
    159162    numberTable->deleteTable();
     
    167170    fastDelete(const_cast<HashTable*>(arrayTable));
    168171    fastDelete(const_cast<HashTable*>(dateTable));
     172    fastDelete(const_cast<HashTable*>(jsonTable));
    169173    fastDelete(const_cast<HashTable*>(mathTable));
    170174    fastDelete(const_cast<HashTable*>(numberTable));
  • trunk/JavaScriptCore/runtime/JSGlobalData.h

    r44344 r44521  
    8383        const HashTable* arrayTable;
    8484        const HashTable* dateTable;
     85        const HashTable* jsonTable;
    8586        const HashTable* mathTable;
    8687        const HashTable* numberTable;
  • trunk/JavaScriptCore/runtime/JSGlobalObject.cpp

    r44076 r44521  
    5151#include "JSGlobalObjectFunctions.h"
    5252#include "JSLock.h"
     53#include "JSONObject.h"
    5354#include "Interpreter.h"
    5455#include "MathObject.h"
     
    319320        GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(exec), DontEnum | DontDelete),
    320321        GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(exec, Inf), DontEnum | DontDelete),
    321         GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete)
     322        GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete),
     323        GlobalPropertyInfo(Identifier(exec, "JSON"), new (exec) JSONObject(JSONObject::createStructure(d()->objectPrototype)), DontEnum | DontDelete)
    322324    };
    323325
  • trunk/LayoutTests/ChangeLog

    r44519 r44521  
     12009-06-07  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Bug 26249: Support JSON.stringify
     6        <https://bugs.webkit.org/show_bug.cgi?id=26249>
     7       
     8        Add test coverage for JSON.stringify -- covers use of replacer function,
     9        replacer array, no replacer, numeric and string gaps.  Also tests call
     10        sequencing (toJSON, toString, replacer functions, and getters).
     11
     12        * fast/js/JSON-stringify-expected.txt: Added.
     13        * fast/js/JSON-stringify.html: Added.
     14        * fast/js/resources/JSON-stringify.js: Added.
     15        * fast/js/resources/json2-es5-compat.js: Added.
     16           Modified version of json2.js to force strict es5 semantics on stringify
     17           to allow us to compare our results to the expected without having to
     18           stuff large arbitrary strings into the testcase
     19
    1202009-06-08  Brady Eidson  <beidson@apple.com>
    221
Note: See TracChangeset for help on using the changeset viewer.