OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium 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 "platform/JSONValuesForV8.h" | 5 #include "platform/JSONValuesForV8.h" |
6 | 6 |
7 namespace blink { | 7 namespace blink { |
8 | 8 |
9 static String coreString(v8::Local<v8::String> v8String) | 9 static String coreString(v8::Local<v8::String> v8String) |
10 { | 10 { |
11 int length = v8String->Length(); | 11 int length = v8String->Length(); |
12 UChar* buffer; | 12 UChar* buffer; |
13 String result = String::createUninitialized(length, buffer); | 13 String result = String::createUninitialized(length, buffer); |
14 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | 14 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); |
15 return result; | 15 return result; |
16 } | 16 } |
17 | 17 |
18 PassRefPtr<JSONValue> toJSONValue(v8::Isolate* isolate, v8::Local<v8::Value> val
ue, int maxDepth) | 18 PassRefPtr<JSONValue> toJSONValue(v8::Local<v8::Context> context, v8::Local<v8::
Value> value, int maxDepth) |
19 { | 19 { |
20 if (value.IsEmpty()) { | 20 if (value.IsEmpty()) { |
21 ASSERT_NOT_REACHED(); | 21 ASSERT_NOT_REACHED(); |
22 return nullptr; | 22 return nullptr; |
23 } | 23 } |
24 | 24 |
25 if (!maxDepth) | 25 if (!maxDepth) |
26 return nullptr; | 26 return nullptr; |
27 maxDepth--; | 27 maxDepth--; |
28 | 28 |
29 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
30 if (value->IsNull() || value->IsUndefined()) | 29 if (value->IsNull() || value->IsUndefined()) |
31 return JSONValue::null(); | 30 return JSONValue::null(); |
32 if (value->IsBoolean()) | 31 if (value->IsBoolean()) |
33 return JSONBasicValue::create(value.As<v8::Boolean>()->Value()); | 32 return JSONBasicValue::create(value.As<v8::Boolean>()->Value()); |
34 if (value->IsNumber()) | 33 if (value->IsNumber()) |
35 return JSONBasicValue::create(value.As<v8::Number>()->Value()); | 34 return JSONBasicValue::create(value.As<v8::Number>()->Value()); |
36 if (value->IsString()) | 35 if (value->IsString()) |
37 return JSONString::create(coreString(value.As<v8::String>())); | 36 return JSONString::create(coreString(value.As<v8::String>())); |
38 if (value->IsArray()) { | 37 if (value->IsArray()) { |
39 v8::Local<v8::Array> array = value.As<v8::Array>(); | 38 v8::Local<v8::Array> array = value.As<v8::Array>(); |
40 RefPtr<JSONArray> inspectorArray = JSONArray::create(); | 39 RefPtr<JSONArray> inspectorArray = JSONArray::create(); |
41 uint32_t length = array->Length(); | 40 uint32_t length = array->Length(); |
42 for (uint32_t i = 0; i < length; i++) { | 41 for (uint32_t i = 0; i < length; i++) { |
43 v8::Local<v8::Value> value; | 42 v8::Local<v8::Value> value; |
44 if (!array->Get(context, i).ToLocal(&value)) | 43 if (!array->Get(context, i).ToLocal(&value)) |
45 return nullptr; | 44 return nullptr; |
46 RefPtr<JSONValue> element = toJSONValue(isolate, value, maxDepth); | 45 RefPtr<JSONValue> element = toJSONValue(context, value, maxDepth); |
47 if (!element) | 46 if (!element) |
48 return nullptr; | 47 return nullptr; |
49 inspectorArray->pushValue(element); | 48 inspectorArray->pushValue(element); |
50 } | 49 } |
51 return inspectorArray; | 50 return inspectorArray; |
52 } | 51 } |
53 if (value->IsObject()) { | 52 if (value->IsObject()) { |
54 RefPtr<JSONObject> jsonObject = JSONObject::create(); | 53 RefPtr<JSONObject> jsonObject = JSONObject::create(); |
55 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | 54 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); |
56 v8::Local<v8::Array> propertyNames; | 55 v8::Local<v8::Array> propertyNames; |
57 if (!object->GetPropertyNames(context).ToLocal(&propertyNames)) | 56 if (!object->GetPropertyNames(context).ToLocal(&propertyNames)) |
58 return nullptr; | 57 return nullptr; |
59 uint32_t length = propertyNames->Length(); | 58 uint32_t length = propertyNames->Length(); |
60 for (uint32_t i = 0; i < length; i++) { | 59 for (uint32_t i = 0; i < length; i++) { |
61 v8::Local<v8::Value> name; | 60 v8::Local<v8::Value> name; |
62 if (!propertyNames->Get(context, i).ToLocal(&name)) | 61 if (!propertyNames->Get(context, i).ToLocal(&name)) |
63 return nullptr; | 62 return nullptr; |
64 // FIXME(yurys): v8::Object should support GetOwnPropertyNames | 63 // FIXME(yurys): v8::Object should support GetOwnPropertyNames |
65 if (name->IsString()) { | 64 if (name->IsString()) { |
66 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedPrope
rty(context, v8::Local<v8::String>::Cast(name)); | 65 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedPrope
rty(context, v8::Local<v8::String>::Cast(name)); |
67 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.From
Just()) | 66 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.From
Just()) |
68 continue; | 67 continue; |
69 } | 68 } |
70 v8::Local<v8::String> propertyName; | 69 v8::Local<v8::String> propertyName; |
71 if (!name->ToString(context).ToLocal(&propertyName)) | 70 if (!name->ToString(context).ToLocal(&propertyName)) |
72 continue; | 71 continue; |
73 v8::Local<v8::Value> property; | 72 v8::Local<v8::Value> property; |
74 if (!object->Get(context, name).ToLocal(&property)) | 73 if (!object->Get(context, name).ToLocal(&property)) |
75 return nullptr; | 74 return nullptr; |
76 RefPtr<JSONValue> propertyValue = toJSONValue(isolate, property, max
Depth); | 75 RefPtr<JSONValue> propertyValue = toJSONValue(context, property, max
Depth); |
77 if (!propertyValue) | 76 if (!propertyValue) |
78 return nullptr; | 77 return nullptr; |
79 jsonObject->setValue(coreString(propertyName), propertyValue); | 78 jsonObject->setValue(coreString(propertyName), propertyValue); |
80 } | 79 } |
81 return jsonObject; | 80 return jsonObject; |
82 } | 81 } |
83 ASSERT_NOT_REACHED(); | 82 ASSERT_NOT_REACHED(); |
84 return nullptr; | 83 return nullptr; |
85 } | 84 } |
86 | 85 |
87 } // namespace blink | 86 } // namespace blink |
OLD | NEW |