Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: src/json-parser.cc

Issue 2026563002: [json] implement InternalizeJSONProperty in C++. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/json-parser.h" 5 #include "src/json-parser.h"
6 6
7 #include "src/char-predicates-inl.h" 7 #include "src/char-predicates-inl.h"
8 #include "src/conversions.h" 8 #include "src/conversions.h"
9 #include "src/debug/debug.h" 9 #include "src/debug/debug.h"
10 #include "src/factory.h" 10 #include "src/factory.h"
11 #include "src/field-type.h" 11 #include "src/field-type.h"
12 #include "src/messages.h" 12 #include "src/messages.h"
13 #include "src/objects-inl.h" 13 #include "src/objects-inl.h"
14 #include "src/parsing/scanner.h" 14 #include "src/parsing/scanner.h"
15 #include "src/parsing/token.h" 15 #include "src/parsing/token.h"
16 #include "src/property-descriptor.h"
16 #include "src/transitions.h" 17 #include "src/transitions.h"
17 18
18 namespace v8 { 19 namespace v8 {
19 namespace internal { 20 namespace internal {
20 21
22 MaybeHandle<Object> JsonParseInternalizer::Internalize(Isolate* isolate,
23 Handle<Object> object,
24 Handle<Object> reviver) {
25 DCHECK(reviver->IsCallable());
26 JsonParseInternalizer internalizer(isolate,
27 Handle<JSReceiver>::cast(reviver));
28 Handle<JSObject> holder =
29 isolate->factory()->NewJSObject(isolate->object_function());
30 Handle<String> name = isolate->factory()->empty_string();
31 JSObject::AddProperty(holder, name, object, NONE);
32 return internalizer.InternalizeJsonProperty(holder, name);
33 }
34
35 MaybeHandle<Object> JsonParseInternalizer::InternalizeJsonProperty(
36 Handle<JSReceiver> holder, Handle<String> name) {
37 HandleScope outer_scope(isolate_);
38 Handle<Object> value;
39 ASSIGN_RETURN_ON_EXCEPTION(
40 isolate_, value, Object::GetPropertyOrElement(holder, name), Object);
41 if (value->IsJSReceiver()) {
42 Handle<JSReceiver> object = Handle<JSReceiver>::cast(value);
43 Maybe<bool> is_array = Object::IsArray(object);
44 if (is_array.IsNothing()) return MaybeHandle<Object>();
45 if (is_array.FromJust()) {
46 Handle<Object> length_object;
47 ASSIGN_RETURN_ON_EXCEPTION(
48 isolate_, length_object,
49 Object::GetLengthFromArrayLike(isolate_, object), Object);
50 double length = length_object->Number();
51 for (double i = 0; i < length; i++) {
52 HandleScope inner_scope(isolate_);
53 Handle<Object> index = isolate_->factory()->NewNumber(i);
54 Handle<String> name = isolate_->factory()->NumberToString(index);
55 if (!RecurseAndApply(object, name)) return MaybeHandle<Object>();
56 }
57 } else {
58 Handle<FixedArray> contents;
59 ASSIGN_RETURN_ON_EXCEPTION(
60 isolate_, contents,
61 KeyAccumulator::GetKeys(object, OWN_ONLY, ENUMERABLE_STRINGS,
62 CONVERT_TO_STRING),
63 Object);
64 for (int i = 0; i < contents->length(); i++) {
65 HandleScope inner_scope(isolate_);
66 Handle<String> name(String::cast(contents->get(i)), isolate_);
67 if (!RecurseAndApply(object, name)) return MaybeHandle<Object>();
68 }
69 }
70 }
71 Handle<Object> argv[] = {name, value};
72 Handle<Object> result;
73 ASSIGN_RETURN_ON_EXCEPTION(
74 isolate_, result, Execution::Call(isolate_, reviver_, holder, 2, argv),
75 Object);
76 return outer_scope.CloseAndEscape(result);
77 }
78
79 bool JsonParseInternalizer::RecurseAndApply(Handle<JSReceiver> holder,
80 Handle<String> name) {
81 Handle<Object> result;
82 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
83 isolate_, result, InternalizeJsonProperty(holder, name), false);
84 Maybe<bool> change_result = Nothing<bool>();
85 if (result->IsUndefined()) {
86 change_result = JSReceiver::DeletePropertyOrElement(holder, name, SLOPPY);
87 } else {
88 PropertyDescriptor desc;
89 desc.set_value(result);
90 desc.set_configurable(true);
91 desc.set_enumerable(true);
92 desc.set_writable(true);
93 change_result = JSReceiver::DefineOwnProperty(isolate_, holder, name, &desc,
94 Object::DONT_THROW);
95 }
96 MAYBE_RETURN(change_result, false);
97 return true;
98 }
99
21 template <bool seq_one_byte> 100 template <bool seq_one_byte>
22 JsonParser<seq_one_byte>::JsonParser(Handle<String> source) 101 JsonParser<seq_one_byte>::JsonParser(Isolate* isolate, Handle<String> source)
23 : source_(source), 102 : source_(source),
24 source_length_(source->length()), 103 source_length_(source->length()),
25 isolate_(source->map()->GetHeap()->isolate()), 104 isolate_(isolate),
26 factory_(isolate_->factory()), 105 factory_(isolate_->factory()),
27 zone_(isolate_->allocator()), 106 zone_(isolate_->allocator()),
28 object_constructor_(isolate_->native_context()->object_function(), 107 object_constructor_(isolate_->native_context()->object_function(),
29 isolate_), 108 isolate_),
30 position_(-1) { 109 position_(-1) {
31 source_ = String::Flatten(source_); 110 source_ = String::Flatten(source_);
32 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED; 111 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
33 112
34 // Optimized fast case where we only have Latin1 characters. 113 // Optimized fast case where we only have Latin1 characters.
35 if (seq_one_byte) { 114 if (seq_one_byte) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // We should sent compile error event because we compile JSON object in 162 // We should sent compile error event because we compile JSON object in
84 // separated source file. 163 // separated source file.
85 isolate()->debug()->OnCompileError(script); 164 isolate()->debug()->OnCompileError(script);
86 MessageLocation location(script, position_, position_ + 1); 165 MessageLocation location(script, position_, position_ + 1);
87 Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2); 166 Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2);
88 return isolate()->template Throw<Object>(error, &location); 167 return isolate()->template Throw<Object>(error, &location);
89 } 168 }
90 return result; 169 return result;
91 } 170 }
92 171
172 MaybeHandle<Object> InternalizeJsonProperty(Handle<JSObject> holder,
173 Handle<String> key);
174
93 template <bool seq_one_byte> 175 template <bool seq_one_byte>
94 void JsonParser<seq_one_byte>::Advance() { 176 void JsonParser<seq_one_byte>::Advance() {
95 position_++; 177 position_++;
96 if (position_ >= source_length_) { 178 if (position_ >= source_length_) {
97 c0_ = kEndOfString; 179 c0_ = kEndOfString;
98 } else if (seq_one_byte) { 180 } else if (seq_one_byte) {
99 c0_ = seq_source_->SeqOneByteStringGet(position_); 181 c0_ = seq_source_->SeqOneByteStringGet(position_);
100 } else { 182 } else {
101 c0_ = source_->Get(position_); 183 c0_ = source_->Get(position_);
102 } 184 }
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 AdvanceSkipWhitespace(); 803 AdvanceSkipWhitespace();
722 return result; 804 return result;
723 } 805 }
724 806
725 // Explicit instantiation. 807 // Explicit instantiation.
726 template class JsonParser<true>; 808 template class JsonParser<true>;
727 template class JsonParser<false>; 809 template class JsonParser<false>;
728 810
729 } // namespace internal 811 } // namespace internal
730 } // namespace v8 812 } // namespace v8
OLDNEW
« no previous file with comments | « src/json-parser.h ('k') | src/json-stringifier.cc » ('j') | src/json-stringifier.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698