1/* 2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "ScriptValue.h" 33 34#include "InspectorValues.h" 35#include "ScriptScope.h" 36#include "SerializedScriptValue.h" 37#include "V8Binding.h" 38 39namespace WebCore { 40 41PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState) 42{ 43 ScriptScope scope(scriptState); 44 return SerializedScriptValue::create(v8Value()); 45} 46 47ScriptValue ScriptValue::deserialize(ScriptState* scriptState, SerializedScriptValue* value) 48{ 49 ScriptScope scope(scriptState); 50 return ScriptValue(value->deserialize()); 51} 52 53bool ScriptValue::getString(String& result) const 54{ 55 if (m_value.IsEmpty()) 56 return false; 57 58 if (!m_value->IsString()) 59 return false; 60 61 result = toWebCoreString(m_value); 62 return true; 63} 64 65String ScriptValue::toString(ScriptState*) const 66{ 67 v8::TryCatch block; 68 v8::Handle<v8::String> s = m_value->ToString(); 69 // Handle the case where an exception is thrown as part of invoking toString on the object. 70 if (block.HasCaught()) 71 return String(); 72 return v8StringToWebCoreString<String>(s, DoNotExternalize); 73} 74 75#if ENABLE(INSPECTOR) 76static PassRefPtr<InspectorValue> v8ToInspectorValue(v8::Handle<v8::Value> value) 77{ 78 if (value.IsEmpty()) { 79 ASSERT_NOT_REACHED(); 80 return 0; 81 } 82 if (value->IsNull() || value->IsUndefined()) 83 return InspectorValue::null(); 84 if (value->IsBoolean()) 85 return InspectorBasicValue::create(value->BooleanValue()); 86 if (value->IsNumber()) 87 return InspectorBasicValue::create(value->NumberValue()); 88 if (value->IsString()) 89 return InspectorString::create(toWebCoreString(value)); 90 if (value->IsArray()) { 91 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value); 92 RefPtr<InspectorArray> inspectorArray = InspectorArray::create(); 93 uint32_t length = array->Length(); 94 for (uint32_t i = 0; i < length; i++) { 95 v8::Local<v8::Value> value = array->Get(v8::Int32::New(i)); 96 RefPtr<InspectorValue> element = v8ToInspectorValue(value); 97 if (!element) { 98 ASSERT_NOT_REACHED(); 99 element = InspectorValue::null(); 100 } 101 inspectorArray->pushValue(element); 102 } 103 return inspectorArray; 104 } 105 if (value->IsObject()) { 106 RefPtr<InspectorObject> inspectorObject = InspectorObject::create(); 107 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); 108 v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); 109 uint32_t length = propertyNames->Length(); 110 for (uint32_t i = 0; i < length; i++) { 111 v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(i)); 112 // FIXME(yurys): v8::Object should support GetOwnPropertyNames 113 if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8::String>::Cast(name))) 114 continue; 115 RefPtr<InspectorValue> propertyValue = v8ToInspectorValue(object->Get(name)); 116 if (!propertyValue) { 117 ASSERT_NOT_REACHED(); 118 continue; 119 } 120 inspectorObject->setValue(toWebCoreStringWithNullCheck(name), propertyValue); 121 } 122 return inspectorObject; 123 } 124 ASSERT_NOT_REACHED(); 125 return 0; 126} 127 128PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ScriptState* scriptState) const 129{ 130 v8::HandleScope handleScope; 131 // v8::Object::GetPropertyNames() expects current context to be not null. 132 v8::Context::Scope contextScope(scriptState->context()); 133 return v8ToInspectorValue(m_value); 134} 135#endif 136 137} // namespace WebCore 138