blob: 140eea5ad01e63e0f485b25225d375177ef43955 [file] [log] [blame]
tommyw@google.com221edb62012-07-31 11:59:37 +00001/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +00003 * Copyright (C) 2016 Apple Inc. All Rights Reserved.
tommyw@google.com221edb62012-07-31 11:59:37 +00004 *
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
33using namespace JSC;
34
35namespace WebCore {
36
37ArrayValue::ArrayValue()
38 : m_exec(0)
39{
40}
41
42ArrayValue::ArrayValue(JSC::ExecState* exec, JSC::JSValue value)
43 : m_exec(exec)
44{
45 if (!value.isUndefinedOrNull() && isJSArray(value))
46 m_value = value;
47}
48
49ArrayValue& ArrayValue::operator=(const ArrayValue& other)
50{
51 m_exec = other.m_exec;
52 m_value = other.m_value;
53 return *this;
54}
55
56bool ArrayValue::isUndefinedOrNull() const
57{
58 return m_value.isEmpty() || m_value.isUndefinedOrNull();
59}
60
61bool 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
71bool ArrayValue::get(size_t index, Dictionary& value) const
72{
73 if (isUndefinedOrNull())
74 return false;
75
fpizlo@apple.com7ebfaed2012-09-25 23:42:52 +000076 JSValue indexedValue = asArray(m_value)->getIndex(m_exec, index);
tommyw@google.com221edb62012-07-31 11:59:37 +000077 if (indexedValue.isUndefinedOrNull() || !indexedValue.isObject())
78 return false;
79
80 value = Dictionary(m_exec, indexedValue);
81 return true;
82}
83
andersca@apple.comad7d8c82016-01-28 21:17:29 +000084bool ArrayValue::get(size_t index, String& value) const
85{
mark.lam@apple.com451de992016-09-07 22:10:50 +000086 VM& vm = m_exec->vm();
87 auto scope = DECLARE_THROW_SCOPE(vm);
88
andersca@apple.comad7d8c82016-01-28 21:17:29 +000089 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.come1ab17c2016-09-26 19:11:17 +000097 RETURN_IF_EXCEPTION(scope, false);
andersca@apple.comad7d8c82016-01-28 21:17:29 +000098
99 return true;
100}
101
tommyw@google.com221edb62012-07-31 11:59:37 +0000102} // namespace WebCore