OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 String ScriptValue::toString(ScriptState*) const | 78 String ScriptValue::toString(ScriptState*) const |
79 { | 79 { |
80 v8::TryCatch block; | 80 v8::TryCatch block; |
81 v8::Handle<v8::String> string = v8Value()->ToString(); | 81 v8::Handle<v8::String> string = v8Value()->ToString(); |
82 if (block.HasCaught()) | 82 if (block.HasCaught()) |
83 return String(); | 83 return String(); |
84 return v8StringToWebCoreString<String>(string, DoNotExternalize); | 84 return v8StringToWebCoreString<String>(string, DoNotExternalize); |
85 } | 85 } |
86 | 86 |
87 static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxD
epth) | 87 static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxD
epth, v8::Isolate* isolate) |
88 { | 88 { |
89 if (value.IsEmpty()) { | 89 if (value.IsEmpty()) { |
90 ASSERT_NOT_REACHED(); | 90 ASSERT_NOT_REACHED(); |
91 return 0; | 91 return 0; |
92 } | 92 } |
93 | 93 |
94 if (!maxDepth) | 94 if (!maxDepth) |
95 return 0; | 95 return 0; |
96 maxDepth--; | 96 maxDepth--; |
97 | 97 |
98 if (value->IsNull() || value->IsUndefined()) | 98 if (value->IsNull() || value->IsUndefined()) |
99 return JSONValue::null(); | 99 return JSONValue::null(); |
100 if (value->IsBoolean()) | 100 if (value->IsBoolean()) |
101 return JSONBasicValue::create(value->BooleanValue()); | 101 return JSONBasicValue::create(value->BooleanValue()); |
102 if (value->IsNumber()) | 102 if (value->IsNumber()) |
103 return JSONBasicValue::create(value->NumberValue()); | 103 return JSONBasicValue::create(value->NumberValue()); |
104 if (value->IsString()) | 104 if (value->IsString()) |
105 return JSONString::create(toWebCoreString(value)); | 105 return JSONString::create(toWebCoreString(value)); |
106 if (value->IsArray()) { | 106 if (value->IsArray()) { |
107 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value); | 107 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value); |
108 RefPtr<JSONArray> inspectorArray = JSONArray::create(); | 108 RefPtr<JSONArray> inspectorArray = JSONArray::create(); |
109 uint32_t length = array->Length(); | 109 uint32_t length = array->Length(); |
110 for (uint32_t i = 0; i < length; i++) { | 110 for (uint32_t i = 0; i < length; i++) { |
111 v8::Local<v8::Value> value = array->Get(v8::Int32::New(i)); | 111 v8::Local<v8::Value> value = array->Get(v8::Int32::New(i, isolate)); |
112 RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth); | 112 RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate); |
113 if (!element) | 113 if (!element) |
114 return 0; | 114 return 0; |
115 inspectorArray->pushValue(element); | 115 inspectorArray->pushValue(element); |
116 } | 116 } |
117 return inspectorArray; | 117 return inspectorArray; |
118 } | 118 } |
119 if (value->IsObject()) { | 119 if (value->IsObject()) { |
120 RefPtr<JSONObject> jsonObject = JSONObject::create(); | 120 RefPtr<JSONObject> jsonObject = JSONObject::create(); |
121 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); | 121 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); |
122 v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); | 122 v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); |
123 uint32_t length = propertyNames->Length(); | 123 uint32_t length = propertyNames->Length(); |
124 for (uint32_t i = 0; i < length; i++) { | 124 for (uint32_t i = 0; i < length; i++) { |
125 v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(i)); | 125 v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(i, iso
late)); |
126 // FIXME(yurys): v8::Object should support GetOwnPropertyNames | 126 // FIXME(yurys): v8::Object should support GetOwnPropertyNames |
127 if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8:
:String>::Cast(name))) | 127 if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8:
:String>::Cast(name))) |
128 continue; | 128 continue; |
129 RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), m
axDepth); | 129 RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), m
axDepth, isolate); |
130 if (!propertyValue) | 130 if (!propertyValue) |
131 return 0; | 131 return 0; |
132 jsonObject->setValue(toWebCoreStringWithNullCheck(name), propertyVal
ue); | 132 jsonObject->setValue(toWebCoreStringWithNullCheck(name), propertyVal
ue); |
133 } | 133 } |
134 return jsonObject; | 134 return jsonObject; |
135 } | 135 } |
136 ASSERT_NOT_REACHED(); | 136 ASSERT_NOT_REACHED(); |
137 return 0; | 137 return 0; |
138 } | 138 } |
139 | 139 |
140 PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const | 140 PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const |
141 { | 141 { |
142 v8::HandleScope handleScope(scriptState->isolate()); | 142 v8::HandleScope handleScope(scriptState->isolate()); |
143 // v8::Object::GetPropertyNames() expects current context to be not null. | 143 // v8::Object::GetPropertyNames() expects current context to be not null. |
144 v8::Context::Scope contextScope(scriptState->context()); | 144 v8::Context::Scope contextScope(scriptState->context()); |
145 return v8ToJSONValue(v8Value(), JSONValue::maxDepth); | 145 return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate())
; |
146 } | 146 } |
147 | 147 |
148 } // namespace WebCore | 148 } // namespace WebCore |
OLD | NEW |