Commit e896e504 authored by oliver@apple.com's avatar oliver@apple.com

Bug 26592: Support standard toJSON functions

<https://bugs.webkit.org/show_bug.cgi?id=26592>

Reviewed by Darin Adler

Add support for the standard Date.toJSON function.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@44931 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 5f911b2e
2009-06-21 Oliver Hunt <oliver@apple.com>
Reviewed by Darin Adler.
Bug 26592: Support standard toJSON functions
<https://bugs.webkit.org/show_bug.cgi?id=26592>
Add support for the standard Date.toJSON function.
* runtime/DatePrototype.cpp:
(JSC::dateProtoFuncToJSON):
2009-06-21 Oliver Hunt <oliver@apple.com>
Reviewed by Sam Weinig.
......
......@@ -59,6 +59,7 @@
macro(test) \
macro(toExponential) \
macro(toFixed) \
macro(toISOString) \
macro(toJSON) \
macro(toLocaleString) \
macro(toPrecision) \
......
......@@ -115,6 +115,8 @@ static JSValue JSC_HOST_CALL dateProtoFuncToTimeString(ExecState*, JSObject*, JS
static JSValue JSC_HOST_CALL dateProtoFuncToUTCString(ExecState*, JSObject*, JSValue, const ArgList&);
static JSValue JSC_HOST_CALL dateProtoFuncToISOString(ExecState*, JSObject*, JSValue, const ArgList&);
static JSValue JSC_HOST_CALL dateProtoFuncToJSON(ExecState*, JSObject*, JSValue, const ArgList&);
}
#include "DatePrototype.lut.h"
......@@ -387,6 +389,7 @@ const ClassInfo DatePrototype::info = {"Date", &DateInstance::info, 0, ExecState
setUTCFullYear dateProtoFuncSetUTCFullYear DontEnum|Function 3
setYear dateProtoFuncSetYear DontEnum|Function 1
getYear dateProtoFuncGetYear DontEnum|Function 0
toJSON dateProtoFuncToJSON DontEnum|Function 0
@end
*/
......@@ -1076,4 +1079,27 @@ JSValue JSC_HOST_CALL dateProtoFuncGetYear(ExecState* exec, JSObject*, JSValue t
return jsNumber(exec, t.year);
}
JSValue JSC_HOST_CALL dateProtoFuncToJSON(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
{
JSObject* object = thisValue.toThisObject(exec);
if (exec->hadException())
return jsNull();
JSValue toISOValue = object->get(exec, exec->globalData().propertyNames->toISOString);
if (exec->hadException())
return jsNull();
CallData callData;
CallType callType = toISOValue.getCallData(callData);
if (callType == CallTypeNone)
return throwError(exec, TypeError, "toISOString is not a function");
JSValue result = call(exec, asObject(toISOValue), callType, callData, object, exec->emptyList());
if (exec->hadException())
return jsNull();
if (result.isObject())
return throwError(exec, TypeError, "toISOString did not return a primitive value");
return result;
}
} // namespace JSC
2009-06-21 Oliver Hunt <oliver@apple.com>
Reviewed by Darin Adler.
Bug 26592: Support standard toJSON functions
Add tests of Date.toJSON.
* fast/js/JSON-stringify-expected.txt:
* fast/js/resources/JSON-stringify.js:
(createTests.result):
(createTests.result.push.):
2009-06-21 Oliver Hunt <oliver@apple.com>
Reviewed by Sam Weinig.
......
function (jsonObject) {
return jsonObject.stringify(new Date(0));
}
PASS tests[i](nativeJSON) is tests[i](JSON)
function (jsonObject) {
return jsonObject.stringify({toJSON: Date.prototype.toJSON});
}
PASS tests[i](nativeJSON) threw exception TypeError: toISOString is not a function.
function (jsonObject) {
return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }});
}
PASS tests[i](nativeJSON) is tests[i](JSON)
function (jsonObject) {
return jsonObject.stringify({get Foo() { return "bar"; }});
}
......
......@@ -5,6 +5,35 @@ function createTests() {
var complexArray = ['a', 'b', 'c',,,simpleObject, simpleArray, [simpleObject,simpleArray]];
var complexObject = {a:"1", b:"2", c:"3", d:undefined, e:null, "":12, get f(){ return simpleArray; }, array: complexArray};
var result = [];
result.push(function(jsonObject){
return jsonObject.stringify(new Date(0));
});
result.push(function(jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON});
});
result[result.length - 1].throws = true;
result.push(function(jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }});
});
result.push(function(jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return {}; }});
});
result[result.length - 1].throws = true;
result.push(function(jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ throw "An exception"; }});
});
result[result.length - 1].throws = true;
result.push(function(jsonObject){
var d = new Date(0);
d.toISOString = null;
return jsonObject.stringify(d);
});
result[result.length - 1].throws = true;
result.push(function(jsonObject){
var d = new Date(0);
d.toJSON = undefined;
return jsonObject.stringify(d);
});
result.push(function(jsonObject){
return jsonObject.stringify({get Foo() { return "bar"; }});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment