tommyw@google.com | 221edb6 | 2012-07-31 11:59:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
mark.lam@apple.com | e1ab17c | 2016-09-26 19:11:17 +0000 | [diff] [blame] | 3 | * Copyright (C) 2016 Apple Inc. All Rights Reserved. |
tommyw@google.com | 221edb6 | 2012-07-31 11:59:37 +0000 | [diff] [blame] | 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 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "ArrayValue.h" |
| 29 | |
| 30 | #include "Dictionary.h" |
| 31 | #include <runtime/JSArray.h> |
| 32 | |
| 33 | using namespace JSC; |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | ArrayValue::ArrayValue() |
| 38 | : m_exec(0) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | ArrayValue::ArrayValue(JSC::ExecState* exec, JSC::JSValue value) |
| 43 | : m_exec(exec) |
| 44 | { |
| 45 | if (!value.isUndefinedOrNull() && isJSArray(value)) |
| 46 | m_value = value; |
| 47 | } |
| 48 | |
| 49 | ArrayValue& ArrayValue::operator=(const ArrayValue& other) |
| 50 | { |
| 51 | m_exec = other.m_exec; |
| 52 | m_value = other.m_value; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | bool ArrayValue::isUndefinedOrNull() const |
| 57 | { |
| 58 | return m_value.isEmpty() || m_value.isUndefinedOrNull(); |
| 59 | } |
| 60 | |
| 61 | bool ArrayValue::length(size_t& length) const |
| 62 | { |
| 63 | if (isUndefinedOrNull()) |
| 64 | return false; |
| 65 | |
| 66 | JSArray* array = asArray(m_value); |
| 67 | length = array->length(); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool ArrayValue::get(size_t index, Dictionary& value) const |
| 72 | { |
| 73 | if (isUndefinedOrNull()) |
| 74 | return false; |
| 75 | |
fpizlo@apple.com | 7ebfaed | 2012-09-25 23:42:52 +0000 | [diff] [blame] | 76 | JSValue indexedValue = asArray(m_value)->getIndex(m_exec, index); |
tommyw@google.com | 221edb6 | 2012-07-31 11:59:37 +0000 | [diff] [blame] | 77 | if (indexedValue.isUndefinedOrNull() || !indexedValue.isObject()) |
| 78 | return false; |
| 79 | |
| 80 | value = Dictionary(m_exec, indexedValue); |
| 81 | return true; |
| 82 | } |
| 83 | |
andersca@apple.com | ad7d8c8 | 2016-01-28 21:17:29 +0000 | [diff] [blame] | 84 | bool ArrayValue::get(size_t index, String& value) const |
| 85 | { |
mark.lam@apple.com | 451de99 | 2016-09-07 22:10:50 +0000 | [diff] [blame] | 86 | VM& vm = m_exec->vm(); |
| 87 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 88 | |
andersca@apple.com | ad7d8c8 | 2016-01-28 21:17:29 +0000 | [diff] [blame] | 89 | if (isUndefinedOrNull()) |
| 90 | return false; |
| 91 | |
| 92 | JSValue indexedValue = asArray(m_value)->getIndex(m_exec, index); |
| 93 | if (indexedValue.isUndefinedOrNull() || !indexedValue.isString()) |
| 94 | return false; |
| 95 | |
| 96 | value = indexedValue.toWTFString(m_exec); |
mark.lam@apple.com | e1ab17c | 2016-09-26 19:11:17 +0000 | [diff] [blame] | 97 | RETURN_IF_EXCEPTION(scope, false); |
andersca@apple.com | ad7d8c8 | 2016-01-28 21:17:29 +0000 | [diff] [blame] | 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
tommyw@google.com | 221edb6 | 2012-07-31 11:59:37 +0000 | [diff] [blame] | 102 | } // namespace WebCore |