1/* 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (c) 2011 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include "config.h" 31#include "ScriptValue.h" 32 33#include "InspectorValues.h" 34#include "JSDOMBinding.h" 35#include "SerializedScriptValue.h" 36 37#include <JavaScriptCore/APICast.h> 38#include <JavaScriptCore/JSValueRef.h> 39 40#include <heap/Strong.h> 41#include <runtime/JSLock.h> 42 43using namespace JSC; 44 45namespace WebCore { 46 47bool ScriptValue::getString(ScriptState* scriptState, String& result) const 48{ 49 if (!m_value) 50 return false; 51 JSLockHolder lock(scriptState); 52 if (!m_value.get().getString(scriptState, result)) 53 return false; 54 return true; 55} 56 57String ScriptValue::toString(ScriptState* scriptState) const 58{ 59 String result = m_value.get().toString(scriptState)->value(scriptState); 60 // Handle the case where an exception is thrown as part of invoking toString on the object. 61 if (scriptState->hadException()) 62 scriptState->clearException(); 63 return result; 64} 65 66bool ScriptValue::isEqual(ScriptState* scriptState, const ScriptValue& anotherValue) const 67{ 68 if (hasNoValue()) 69 return anotherValue.hasNoValue(); 70 71 return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), 0); 72} 73 74bool ScriptValue::isNull() const 75{ 76 if (!m_value) 77 return false; 78 return m_value.get().isNull(); 79} 80 81bool ScriptValue::isUndefined() const 82{ 83 if (!m_value) 84 return false; 85 return m_value.get().isUndefined(); 86} 87 88bool ScriptValue::isObject() const 89{ 90 if (!m_value) 91 return false; 92 return m_value.get().isObject(); 93} 94 95bool ScriptValue::isFunction() const 96{ 97 CallData callData; 98 return getCallData(m_value.get(), callData) != CallTypeNone; 99} 100 101PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState, SerializationErrorMode throwExceptions) 102{ 103 return SerializedScriptValue::create(scriptState, jsValue(), 0, 0, throwExceptions); 104} 105 106PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, bool& didThrow) 107{ 108 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValue::create(scriptState, jsValue(), messagePorts, arrayBuffers); 109 didThrow = scriptState->hadException(); 110 return serializedValue.release(); 111} 112 113ScriptValue ScriptValue::deserialize(ScriptState* scriptState, SerializedScriptValue* value, SerializationErrorMode throwExceptions) 114{ 115 return ScriptValue(scriptState->vm(), value->deserialize(scriptState, scriptState->lexicalGlobalObject(), 0, throwExceptions)); 116} 117 118#if ENABLE(INSPECTOR) 119static PassRefPtr<InspectorValue> jsToInspectorValue(ScriptState* scriptState, JSValue value, int maxDepth) 120{ 121 if (!value) { 122 ASSERT_NOT_REACHED(); 123 return 0; 124 } 125 126 if (!maxDepth) 127 return 0; 128 maxDepth--; 129 130 if (value.isNull() || value.isUndefined()) 131 return InspectorValue::null(); 132 if (value.isBoolean()) 133 return InspectorBasicValue::create(value.asBoolean()); 134 if (value.isNumber()) 135 return InspectorBasicValue::create(value.asNumber()); 136 if (value.isString()) { 137 String s = value.getString(scriptState); 138 return InspectorString::create(String(s.characters(), s.length())); 139 } 140 if (value.isObject()) { 141 if (isJSArray(value)) { 142 RefPtr<InspectorArray> inspectorArray = InspectorArray::create(); 143 JSArray* array = asArray(value); 144 unsigned length = array->length(); 145 for (unsigned i = 0; i < length; i++) { 146 JSValue element = array->getIndex(scriptState, i); 147 RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth); 148 if (!elementValue) 149 return 0; 150 inspectorArray->pushValue(elementValue); 151 } 152 return inspectorArray; 153 } 154 RefPtr<InspectorObject> inspectorObject = InspectorObject::create(); 155 JSObject* object = value.getObject(); 156 PropertyNameArray propertyNames(scriptState); 157 object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, ExcludeDontEnumProperties); 158 for (size_t i = 0; i < propertyNames.size(); i++) { 159 const Identifier& name = propertyNames[i]; 160 JSValue propertyValue = object->get(scriptState, name); 161 RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth); 162 if (!inspectorValue) 163 return 0; 164 inspectorObject->setValue(String(name.characters(), name.length()), inspectorValue); 165 } 166 return inspectorObject; 167 } 168 ASSERT_NOT_REACHED(); 169 return 0; 170} 171 172PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ScriptState* scriptState) const 173{ 174 JSC::JSLockHolder holder(scriptState); 175 return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth); 176} 177#endif // ENABLE(INSPECTOR) 178 179} // namespace WebCore 180