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