Changeset 44974 in webkit


Ignore:
Timestamp:
Jun 22, 2009 10:44:55 PM (15 years ago)
Author:
oliver@apple.com
Message:

Bug 26640: JSON.stringify needs to special case Boolean objects
<https://bugs.webkit.org/show_bug.cgi?id=26640>

Reviewed by Alexey Proskuryakov.

Add special case handling of the Boolean object so we match current
ES5 errata.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r44968 r44974  
     12009-06-22  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Alexey Proskuryakov.
     4
     5        Bug 26640: JSON.stringify needs to special case Boolean objects
     6        <https://bugs.webkit.org/show_bug.cgi?id=26640>
     7
     8        Add special case handling of the Boolean object so we match current
     9        ES5 errata.
     10
     11        * runtime/JSONObject.cpp:
     12        (JSC::unwrapBoxedPrimitive): renamed from unwrapNumberOrString
     13        (JSC::gap):
     14        (JSC::Stringifier::appendStringifiedValue):
     15
    1162009-06-22  Oliver Hunt  <oliver@apple.com>
    217
  • trunk/JavaScriptCore/runtime/JSONObject.cpp

    r44968 r44974  
    2727#include "JSONObject.h"
    2828
     29#include "BooleanObject.h"
    2930#include "Error.h"
    3031#include "ExceptionHelpers.h"
     
    120121// ------------------------------ helper functions --------------------------------
    121122
    122 static inline JSValue unwrapNumberOrString(JSValue value)
     123static inline JSValue unwrapBoxedPrimitive(JSValue value)
    123124{
    124125    if (!value.isObject())
    125126        return value;
    126     if (!asObject(value)->inherits(&NumberObject::info) && !asObject(value)->inherits(&StringObject::info))
     127    if (!asObject(value)->inherits(&NumberObject::info) && !asObject(value)->inherits(&StringObject::info) && !asObject(value)->inherits(&BooleanObject::info))
    127128        return value;
    128129    return static_cast<JSWrapperObject*>(asObject(value))->internalValue();
     
    131132static inline UString gap(JSValue space)
    132133{
    133     space = unwrapNumberOrString(space);
     134    space = unwrapBoxedPrimitive(space);
    134135
    135136    // If the space value is a number, create a gap string with that number of spaces.
     
    356357    }
    357358
     359    value = unwrapBoxedPrimitive(value);
     360
    358361    if (value.isBoolean()) {
    359362        builder.append(value.getBoolean() ? "true" : "false");
    360363        return StringifySucceeded;
    361364    }
    362 
    363     value = unwrapNumberOrString(value);
    364365
    365366    UString stringValue;
  • trunk/LayoutTests/ChangeLog

    r44969 r44974  
     12009-06-22  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Alexey Proskuryakov.
     4
     5        Bug 26640: JSON.stringify needs to special case Boolean objects
     6        <https://bugs.webkit.org/show_bug.cgi?id=26640>
     7
     8        Add tests for serialisation of wrapped and unwrapped primitives.
     9
     10        * fast/js/JSON-stringify-expected.txt:
     11        * fast/js/resources/JSON-stringify.js:
     12        * fast/js/resources/json2-es5-compat.js:
     13          Modify Str() to match ES5 errata
     14
    1152009-06-22  Sam Weinig  <sam@webkit.org>
    216
  • trunk/LayoutTests/fast/js/JSON-stringify-expected.txt

    r44935 r44974  
     1function (jsonObject) {
     2        return jsonObject.stringify(1);
     3    }
     4PASS tests[i](nativeJSON) is tests[i](JSON)
     5function (jsonObject) {
     6        return jsonObject.stringify(1.5);
     7    }
     8PASS tests[i](nativeJSON) is tests[i](JSON)
     9function (jsonObject) {
     10        return jsonObject.stringify(-1);
     11    }
     12PASS tests[i](nativeJSON) is tests[i](JSON)
     13function (jsonObject) {
     14        return jsonObject.stringify(-1.5);
     15    }
     16PASS tests[i](nativeJSON) is tests[i](JSON)
     17function (jsonObject) {
     18        return jsonObject.stringify(null);
     19    }
     20PASS tests[i](nativeJSON) is tests[i](JSON)
     21function (jsonObject) {
     22        return jsonObject.stringify("string");
     23    }
     24PASS tests[i](nativeJSON) is tests[i](JSON)
     25function (jsonObject) {
     26        return jsonObject.stringify(new Number(0));
     27    }
     28PASS tests[i](nativeJSON) is tests[i](JSON)
     29function (jsonObject) {
     30        return jsonObject.stringify(new Number(1));
     31    }
     32PASS tests[i](nativeJSON) is tests[i](JSON)
     33function (jsonObject) {
     34        return jsonObject.stringify(new Number(1.5));
     35    }
     36PASS tests[i](nativeJSON) is tests[i](JSON)
     37function (jsonObject) {
     38        return jsonObject.stringify(new Number(-1));
     39    }
     40PASS tests[i](nativeJSON) is tests[i](JSON)
     41function (jsonObject) {
     42        return jsonObject.stringify(new Number(-1.5));
     43    }
     44PASS tests[i](nativeJSON) is tests[i](JSON)
     45function (jsonObject) {
     46        return jsonObject.stringify(new String("a string object"));
     47    }
     48PASS tests[i](nativeJSON) is tests[i](JSON)
     49function (jsonObject) {
     50        return jsonObject.stringify(new Boolean(true));
     51    }
     52PASS tests[i](nativeJSON) is tests[i](JSON)
     53function (jsonObject) {
     54        return jsonObject.stringify(new Boolean(false));
     55    }
     56PASS tests[i](nativeJSON) is tests[i](JSON)
     57function (jsonObject) {
     58        return jsonObject.stringify(true);
     59    }
     60PASS tests[i](nativeJSON) is tests[i](JSON)
     61function (jsonObject) {
     62        return jsonObject.stringify(false);
     63    }
     64PASS tests[i](nativeJSON) is tests[i](JSON)
    165function (jsonObject) {
    266        return jsonObject.stringify(new Date(0));
  • trunk/LayoutTests/fast/js/resources/JSON-stringify.js

    r44931 r44974  
    66    var complexObject = {a:"1", b:"2", c:"3", d:undefined, e:null, "":12, get f(){ return simpleArray; }, array: complexArray};
    77    var result = [];
     8    result.push(function(jsonObject){
     9        return jsonObject.stringify(1);
     10    });
     11    result.push(function(jsonObject){
     12        return jsonObject.stringify(1.5);
     13    });
     14    result.push(function(jsonObject){
     15        return jsonObject.stringify(-1);
     16    });
     17    result.push(function(jsonObject){
     18        return jsonObject.stringify(-1.5);
     19    });
     20    result.push(function(jsonObject){
     21        return jsonObject.stringify(null);
     22    });
     23    result.push(function(jsonObject){
     24        return jsonObject.stringify("string");
     25    });
     26    result.push(function(jsonObject){
     27        return jsonObject.stringify(new Number(0));
     28    });
     29    result.push(function(jsonObject){
     30        return jsonObject.stringify(new Number(1));
     31    });
     32    result.push(function(jsonObject){
     33        return jsonObject.stringify(new Number(1.5));
     34    });
     35    result.push(function(jsonObject){
     36        return jsonObject.stringify(new Number(-1));
     37    });
     38    result.push(function(jsonObject){
     39        return jsonObject.stringify(new Number(-1.5));
     40    });
     41    result.push(function(jsonObject){
     42        return jsonObject.stringify(new String("a string object"));
     43    });
     44    result.push(function(jsonObject){
     45        return jsonObject.stringify(new Boolean(true));
     46    });
     47    result.push(function(jsonObject){
     48        return jsonObject.stringify(new Boolean(false));
     49    });
     50    result.push(function(jsonObject){
     51        return jsonObject.stringify(true);
     52    });
     53    result.push(function(jsonObject){
     54        return jsonObject.stringify(false);
     55    });
    856    result.push(function(jsonObject){
    957        return jsonObject.stringify(new Date(0));
  • trunk/LayoutTests/fast/js/resources/json2-es5-compat.js

    r44550 r44974  
    179179                 f(this.getUTCMinutes())   + ':' +
    180180                 f(this.getUTCSeconds())   + 'Z';
    181         };
    182 
    183         String.prototype.toJSON =
    184         Number.prototype.toJSON =
    185         Boolean.prototype.toJSON = function (key) {
    186             return this.valueOf();
    187181        };
    188182    }
     
    250244// What happens next depends on the value's type.
    251245
     246        if (value && ((typeof value) === "object")) {
     247            if (value.constructor === String || value.constructor === Number || value.constructor === Boolean)
     248                value = value.valueOf();
     249        }
     250
    252251        switch (typeof value) {
    253252        case 'string':
Note: See TracChangeset for help on using the changeset viewer.