| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 v8::Handle<v8::String> string = v8Value()->ToString(); | 61 v8::Handle<v8::String> string = v8Value()->ToString(); |
| 62 if (block.HasCaught()) | 62 if (block.HasCaught()) |
| 63 return String(); | 63 return String(); |
| 64 return v8StringToWebCoreString<String>(string, DoNotExternalize); | 64 return v8StringToWebCoreString<String>(string, DoNotExternalize); |
| 65 } | 65 } |
| 66 | 66 |
| 67 static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxD
epth, v8::Isolate* isolate) | 67 static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxD
epth, v8::Isolate* isolate) |
| 68 { | 68 { |
| 69 if (value.IsEmpty()) { | 69 if (value.IsEmpty()) { |
| 70 ASSERT_NOT_REACHED(); | 70 ASSERT_NOT_REACHED(); |
| 71 return 0; | 71 return nullptr; |
| 72 } | 72 } |
| 73 | 73 |
| 74 if (!maxDepth) | 74 if (!maxDepth) |
| 75 return 0; | 75 return nullptr; |
| 76 maxDepth--; | 76 maxDepth--; |
| 77 | 77 |
| 78 if (value->IsNull() || value->IsUndefined()) | 78 if (value->IsNull() || value->IsUndefined()) |
| 79 return JSONValue::null(); | 79 return JSONValue::null(); |
| 80 if (value->IsBoolean()) | 80 if (value->IsBoolean()) |
| 81 return JSONBasicValue::create(value->BooleanValue()); | 81 return JSONBasicValue::create(value->BooleanValue()); |
| 82 if (value->IsNumber()) | 82 if (value->IsNumber()) |
| 83 return JSONBasicValue::create(value->NumberValue()); | 83 return JSONBasicValue::create(value->NumberValue()); |
| 84 if (value->IsString()) | 84 if (value->IsString()) |
| 85 return JSONString::create(toCoreString(value.As<v8::String>())); | 85 return JSONString::create(toCoreString(value.As<v8::String>())); |
| 86 if (value->IsArray()) { | 86 if (value->IsArray()) { |
| 87 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value); | 87 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value); |
| 88 RefPtr<JSONArray> inspectorArray = JSONArray::create(); | 88 RefPtr<JSONArray> inspectorArray = JSONArray::create(); |
| 89 uint32_t length = array->Length(); | 89 uint32_t length = array->Length(); |
| 90 for (uint32_t i = 0; i < length; i++) { | 90 for (uint32_t i = 0; i < length; i++) { |
| 91 v8::Local<v8::Value> value = array->Get(v8::Int32::New(isolate, i)); | 91 v8::Local<v8::Value> value = array->Get(v8::Int32::New(isolate, i)); |
| 92 RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate); | 92 RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate); |
| 93 if (!element) | 93 if (!element) |
| 94 return 0; | 94 return nullptr; |
| 95 inspectorArray->pushValue(element); | 95 inspectorArray->pushValue(element); |
| 96 } | 96 } |
| 97 return inspectorArray; | 97 return inspectorArray; |
| 98 } | 98 } |
| 99 if (value->IsObject()) { | 99 if (value->IsObject()) { |
| 100 RefPtr<JSONObject> jsonObject = JSONObject::create(); | 100 RefPtr<JSONObject> jsonObject = JSONObject::create(); |
| 101 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); | 101 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); |
| 102 v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); | 102 v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); |
| 103 uint32_t length = propertyNames->Length(); | 103 uint32_t length = propertyNames->Length(); |
| 104 for (uint32_t i = 0; i < length; i++) { | 104 for (uint32_t i = 0; i < length; i++) { |
| 105 v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(isolat
e, i)); | 105 v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(isolat
e, i)); |
| 106 // FIXME(yurys): v8::Object should support GetOwnPropertyNames | 106 // FIXME(yurys): v8::Object should support GetOwnPropertyNames |
| 107 if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8:
:String>::Cast(name))) | 107 if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8:
:String>::Cast(name))) |
| 108 continue; | 108 continue; |
| 109 RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), m
axDepth, isolate); | 109 RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), m
axDepth, isolate); |
| 110 if (!propertyValue) | 110 if (!propertyValue) |
| 111 return 0; | 111 return nullptr; |
| 112 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullChec
k>, nameString, name, 0); | 112 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullChec
k>, nameString, name, nullptr); |
| 113 jsonObject->setValue(nameString, propertyValue); | 113 jsonObject->setValue(nameString, propertyValue); |
| 114 } | 114 } |
| 115 return jsonObject; | 115 return jsonObject; |
| 116 } | 116 } |
| 117 ASSERT_NOT_REACHED(); | 117 ASSERT_NOT_REACHED(); |
| 118 return 0; | 118 return nullptr; |
| 119 } | 119 } |
| 120 | 120 |
| 121 PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const | 121 PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const |
| 122 { | 122 { |
| 123 v8::HandleScope handleScope(scriptState->isolate()); | 123 v8::HandleScope handleScope(scriptState->isolate()); |
| 124 // v8::Object::GetPropertyNames() expects current context to be not null. | 124 // v8::Object::GetPropertyNames() expects current context to be not null. |
| 125 v8::Context::Scope contextScope(scriptState->context()); | 125 v8::Context::Scope contextScope(scriptState->context()); |
| 126 return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate())
; | 126 return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate())
; |
| 127 } | 127 } |
| 128 | 128 |
| 129 } // namespace WebCore | 129 } // namespace WebCore |
| OLD | NEW |