Commit f2408624 authored by yangguo's avatar yangguo Committed by Commit bot

[json] use OrderedHashSet for replacer property list.

R=ishell@chromium.org
BUG=v8:6071

Review-Url: https://codereview.chromium.org/2740073002
Cr-Commit-Position: refs/heads/master@{#43690}
parent 59a97842
......@@ -101,15 +101,6 @@ MaybeHandle<Object> JsonStringifier::Stringify(Handle<Object> object,
return MaybeHandle<Object>();
}
bool IsInList(Handle<String> key, List<Handle<String> >* list) {
// TODO(yangguo): This is O(n^2) for n properties in the list. Deal with this
// if this becomes an issue.
for (const Handle<String>& existing : *list) {
if (String::Equals(existing, key)) return true;
}
return false;
}
bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) {
DCHECK(property_list_.is_null());
DCHECK(replacer_function_.is_null());
......@@ -117,7 +108,7 @@ bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) {
if (is_array.IsNothing()) return false;
if (is_array.FromJust()) {
HandleScope handle_scope(isolate_);
List<Handle<String> > list;
Handle<OrderedHashSet> set = factory()->NewOrderedHashSet();
Handle<Object> length_obj;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate_, length_obj,
......@@ -140,12 +131,12 @@ bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) {
}
}
if (key.is_null()) continue;
if (!IsInList(key, &list)) list.Add(key);
}
property_list_ = factory()->NewUninitializedFixedArray(list.length());
for (int i = 0; i < list.length(); i++) {
property_list_->set(i, *list[i]);
// Object keys are internalized, so do it here.
key = factory()->InternalizeString(key);
set = OrderedHashSet::Add(set, key);
}
property_list_ = OrderedHashSet::ConvertToKeysArray(
set, GetKeysConversion::kKeepNumbers);
property_list_ = handle_scope.CloseAndEscape(property_list_);
} else if (replacer->IsCallable()) {
replacer_function_ = Handle<JSReceiver>::cast(replacer);
......
......@@ -327,6 +327,7 @@ assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x']));
assertEquals('{\n "a": "b",\n "c": "d"\n}',
JSON.stringify({a:"b",c:"d"}, null, 1));
assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));
assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x', 'x', 'y']));
// toJSON get string keys.
var checker = {};
......
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