Ignore:
Timestamp:
Jun 21, 2009, 4:02:13 PM (16 years ago)
Author:
[email protected]
Message:

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

Reviewed by Darin Adler and Cameron Zwarich.

Extend the LiteralParser to support the full strict JSON
grammar, fix a few places where the grammar was incorrectly
lenient. Doesn't yet support the JSON.parse reviver function
but that does not block the JSON.parse functionality itself.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/JSONObject.cpp

    r44813 r44923  
    3030#include "ExceptionHelpers.h"
    3131#include "JSArray.h"
     32#include "LiteralParser.h"
    3233#include "PropertyNameArray.h"
    3334#include <wtf/MathExtras.h>
     
    3738ASSERT_CLASS_FITS_IN_CELL(JSONObject);
    3839
     40static JSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState*, JSObject*, JSValue, const ArgList&);
    3941static JSValue JSC_HOST_CALL JSONProtoFuncStringify(ExecState*, JSObject*, JSValue, const ArgList&);
    4042
     
    563565/* Source for JSONObject.lut.h
    564566@begin jsonTable
     567  parse         JSONProtoFuncParse             DontEnum|Function 1
    565568  stringify     JSONProtoFuncStringify         DontEnum|Function 1
    566569@end
     
    583586{
    584587    stringifier->mark();
     588}
     589
     590// ECMA-262 v5 15.12.3
     591JSValue JSC_HOST_CALL JSONProtoFuncParse(ExecState* exec, JSObject*, JSValue, const ArgList& args)
     592{
     593    if (args.isEmpty())
     594        return throwError(exec, GeneralError, "JSON.parse requires at least one parameter");
     595    JSValue value = args.at(0);
     596    UString source = value.toString(exec);
     597    if (exec->hadException())
     598        return jsNull();
     599   
     600    LiteralParser jsonParser(exec, source, LiteralParser::StrictJSON);
     601    JSValue parsedObject = jsonParser.tryLiteralParse();
     602    if (!parsedObject)
     603        return throwError(exec, SyntaxError, "Unable to parse JSON string");
     604   
     605    return parsedObject;
    585606}
    586607
Note: See TracChangeset for help on using the changeset viewer.