Revert "Use browsers JSON.parse for parsing JSON (#3)"

Review URL: https://codereview.chromium.org//12313069

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18911 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sra@google.com
2013-02-22 19:34:08 +00:00
parent 5b0fea791f
commit 0d95ced3dc
12 changed files with 5 additions and 228 deletions
-9
View File
@@ -1,9 +0,0 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// JSON parsing and serialization.
patch parse(String json, [reviver(var key, var value)]) {
return _parse(json, reviver);
}
-9
View File
@@ -1,9 +0,0 @@
# Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
{
'sources': [
'json_patch.dart',
],
}
+1 -1
View File
@@ -72,7 +72,7 @@ RawScript* Bootstrap::LoadIsolateScript(bool patch) {
RawScript* Bootstrap::LoadJsonScript(bool patch) {
const char* url = patch ? "dart:json-patch" : "dart:json";
const char* source = patch ? json_patch_ : json_source_;
const char* source = patch ? json_source_ : json_source_;
return LoadScript(url, source, patch);
}
-1
View File
@@ -45,7 +45,6 @@ class Bootstrap : public AllStatic {
static const char isolate_source_[];
static const char isolate_patch_[];
static const char json_source_[];
static const char json_patch_[];
static const char math_source_[];
static const char math_patch_[];
static const char mirrors_source_[];
-5
View File
@@ -1101,11 +1101,6 @@ RawError* Object::Init(Isolate* isolate) {
INIT_LIBRARY(Json,
Bootstrap::LoadJsonScript(false),
Library::JsonLibrary());
patch_script = Bootstrap::LoadJsonScript(true);
error = lib.Patch(patch_script);
if (!error.IsNull()) {
return error.raw();
}
INIT_LIBRARY(Utf,
Bootstrap::LoadUtfScript(false),
Library::UtfLibrary());
-41
View File
@@ -19,7 +19,6 @@
'isolate_cc_file': '<(SHARED_INTERMEDIATE_DIR)/isolate_gen.cc',
'isolate_patch_cc_file': '<(SHARED_INTERMEDIATE_DIR)/isolate_patch_gen.cc',
'json_cc_file': '<(SHARED_INTERMEDIATE_DIR)/json_gen.cc',
'json_patch_cc_file': '<(SHARED_INTERMEDIATE_DIR)/json_patch_gen.cc',
'scalarlist_cc_file': '<(SHARED_INTERMEDIATE_DIR)/scalarlist_gen.cc',
'scalarlist_patch_cc_file': '<(SHARED_INTERMEDIATE_DIR)/scalarlist_patch_gen.cc',
'uri_cc_file': '<(SHARED_INTERMEDIATE_DIR)/uri_gen.cc',
@@ -102,7 +101,6 @@
'generate_isolate_cc_file',
'generate_isolate_patch_cc_file',
'generate_json_cc_file',
'generate_json_patch_cc_file',
'generate_mirrors_cc_file',
'generate_mirrors_patch_cc_file',
'generate_scalarlist_cc_file',
@@ -133,7 +131,6 @@
'<(isolate_cc_file)',
'<(isolate_patch_cc_file)',
'<(json_cc_file)',
'<(json_patch_cc_file)',
'<(mirrors_cc_file)',
'<(mirrors_patch_cc_file)',
'<(scalarlist_cc_file)',
@@ -850,44 +847,6 @@
},
]
},
{
'target_name': 'generate_json_patch_cc_file',
'type': 'none',
'includes': [
# Load the shared json library sources.
'../lib/json_sources.gypi',
],
'sources/': [
# Exclude all .[cc|h] files.
# This is only here for reference. Excludes happen after
# variable expansion, so the script has to do its own
# exclude processing of the sources being passed.
['exclude', '\\.cc|h$'],
],
'actions': [
{
'action_name': 'generate_json_patch_cc',
'inputs': [
'../tools/create_string_literal.py',
'<(builtin_in_cc_file)',
'<@(_sources)',
],
'outputs': [
'<(json_patch_cc_file)',
],
'action': [
'python',
'tools/create_string_literal.py',
'--output', '<(json_patch_cc_file)',
'--input_cc', '<(builtin_in_cc_file)',
'--include', 'vm/bootstrap.h',
'--var_name', 'dart::Bootstrap::json_patch_',
'<@(_sources)',
],
'message': 'Generating ''<(json_patch_cc_file)'' file.'
},
]
},
{
'target_name': 'generate_scalarlist_cc_file',
'type': 'none',
@@ -1,91 +0,0 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Patch file for dart:json library.
import 'dart:_foreign_helper' show JS;
/**
* Parses [json] and builds the corresponding parsed JSON value.
*
* Parsed JSON values are of the types [num], [String], [bool], [Null],
* [List]s of parsed JSON values or [Map]s from [String] to parsed
* JSON values.
*
* The optional [reviver] function, if provided, is called once for each object
* or list property parsed. The arguments are the property name ([String]) or
* list index ([int]), and the value is the parsed value. The return value of
* the reviver will be used as the value of that property instead of the parsed
* value. The top level value is passed to the reviver with the empty string as
* a key.
*
* Throws [FormatException] if the input is not valid JSON text.
*/
patch parse(String json, [reviver(var key, var value)]) {
if (json is! String) throw new ArgumentError(json);
var parsed;
try {
parsed = JS('=Object|=List|Null|bool|num|String', 'JSON.parse(#)', json);
} catch (e) {
throw new FormatException(JS('String', 'String(#)', e));
}
return _convertJsonToDart(parsed, reviver);
}
/**
* Walks the raw JavaScript value [json], replacing JavaScript Objects with
* Maps. [json] is expected to be freshly allocated so elements can be replaced
* in-place.
*/
_convertJsonToDart(json, reviver(key, value)) {
var revive = reviver == null ? (key, value) => value : reviver;
walk(e) {
// JavaScript null, string, number, bool are in the correct representation.
if (JS('bool', '# == null', e) || JS('bool', 'typeof # != "object"', e)) {
return e;
}
// This test is needed to avoid identifing '{"__proto__":[]}' as an Array.
// TODO(sra): Replace this test with cheaper '#.constructor === Array' when
// bug 621 below is fixed.
if (JS('bool', 'Object.getPrototypeOf(#) === Array.prototype', e)) {
var list = JS('=List', '#', e); // Teach compiler the type is known.
// In-place update of the elements since JS Array is a Dart List.
for (int i = 0; i < list.length; i++) {
// Use JS indexing to avoid range checks. We know this is the only
// reference to the list, but the compiler will likely never be able to
// tell that this instance of the list cannot have its length changed by
// the reviver even though it later will be passed to the reviver at the
// outer level.
var item = JS('', '#[#]', list, i);
JS('', '#[#]=#', list, i, revive(i, walk(item)));
}
return list;
}
// Otherwise it is a plain Object, so copy to a Map.
var keys = JS('=List', 'Object.keys(#)', e);
Map map = {};
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
map[key] = revive(key, walk(JS('', '#[#]', e, key)));
}
// V8 has a bug with properties named "__proto__"
// https://code.google.com/p/v8/issues/detail?id=621
var proto = JS('', '#.__proto__', e);
// __proto__ can be undefined on IE9.
if (JS('bool',
'typeof # !== "undefined" && # !== Object.prototype',
proto, proto)) {
map['__proto__'] = revive('__proto__', walk(proto));
}
return map;
}
return revive('', walk(json));
}
+1 -2
View File
@@ -66,8 +66,7 @@ const Map<String, LibraryInfo> LIBRARIES = const {
dart2jsPatchPath: "_internal/compiler/implementation/lib/isolate_patch.dart"),
"json": const LibraryInfo(
"json/json.dart",
dart2jsPatchPath: "_internal/compiler/implementation/lib/json_patch.dart"),
"json/json.dart"),
"math": const LibraryInfo(
"math/math.dart",
+1 -3
View File
@@ -47,9 +47,7 @@ class JsonUnsupportedObjectError implements Error {
*
* Throws [FormatException] if the input is not valid JSON text.
*/
external parse(String json, [reviver(var key, var value)]);
_parse(String json, reviver(var key, var value)) {
parse(String json, [reviver(var key, var value)]) {
BuildJsonListener listener;
if (reviver == null) {
listener = new BuildJsonListener();
-1
View File
@@ -51,7 +51,6 @@ core_runtime_types_test: Fail
[ $compiler == dart2js && $runtime == ie9 ]
date_time7_test: Fail # BUG(3304): Maybe this doesn't time out?
json_leading_zeros_test: Fail # IE parses slightly harmless no-standard JSON.
string_base_vm_test: Fail # BUG(3304): Maybe this doesn't time out?
[ $compiler == dart2dart ]
@@ -1,60 +0,0 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// This is a trimmed-down version of json_test to isolate a difference between
// IE and other runtimes.
library json_test;
import "dart:json";
bool badFormat(e) => e is FormatException;
void testThrows(json) {
Expect.throws(() => parse(json), badFormat);
}
testNumbers() {
// Positive tests for number formats.
var integerList = ["0","9","9999"];
var signList = ["", "-"];
var fractionList = ["", ".0", ".1", ".99999"];
var exponentList = [""];
for (var exphead in ["e", "E", "e-", "E-", "e+", "E+"]) {
for (var expval in ["0", "1", "200"]) {
exponentList.add("$exphead$expval");
}
}
// Negative tests (syntax error).
// testError thoroughly tests the given parts with a lot of valid
// values for the other parts.
testError({signs, integers, fractions, exponents}) {
def(value, defaultValue) {
if (value == null) return defaultValue;
if (value is List) return value;
return [value];
}
signs = def(signs, signList);
integers = def(integers, integerList);
fractions = def(fractions, fractionList);
exponents = def(exponents, exponentList);
for (var integer in integers) {
for (var sign in signs) {
for (var fraction in fractions) {
for (var exponent in exponents) {
var literal = "$sign$integer$fraction$exponent";
testThrows(literal);
}
}
}
}
}
// Initial zero only allowed for zero integer part.
testError(integers: ["00", "01"]);
}
main() {
testNumbers();
}
+2 -5
View File
@@ -97,11 +97,8 @@ testNumbers() {
// Integer part cannot be omitted:
testError(integers: "");
// Test for "Initial zero only allowed for zero integer part" moved to
// json_leading_zeros_test.dart because IE's JSON.parse accepts additional
// initial zeros.
// Initial zero only allowed for zero integer part.
testError(integers: ["00", "01"]);
// Only minus allowed as sign.
testError(signs: "+");
// Requires digits after decimal point.