1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 * Copyright (c) 2010 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 are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33#include "ScriptArguments.h"
34
35#include "JSCInlines.h"
36#include "ScriptValue.h"
37
38namespace Inspector {
39
40PassRefPtr<ScriptArguments> ScriptArguments::create(JSC::ExecState* scriptState, Vector<Deprecated::ScriptValue>& arguments)
41{
42    return adoptRef(new ScriptArguments(scriptState, arguments));
43}
44
45ScriptArguments::ScriptArguments(JSC::ExecState* execState, Vector<Deprecated::ScriptValue>& arguments)
46    : m_globalObject(execState->vm(), execState->lexicalGlobalObject())
47{
48    m_arguments.swap(arguments);
49}
50
51ScriptArguments::~ScriptArguments()
52{
53}
54
55const Deprecated::ScriptValue &ScriptArguments::argumentAt(size_t index) const
56{
57    ASSERT(m_arguments.size() > index);
58    return m_arguments[index];
59}
60
61JSC::ExecState* ScriptArguments::globalState() const
62{
63    if (m_globalObject)
64        return const_cast<JSC::JSGlobalObject*>(m_globalObject.get())->globalExec();
65
66    return nullptr;
67}
68
69bool ScriptArguments::getFirstArgumentAsString(String& result, bool checkForNullOrUndefined)
70{
71    if (!argumentCount())
72        return false;
73
74    const Deprecated::ScriptValue& value = argumentAt(0);
75    if (checkForNullOrUndefined && (value.isNull() || value.isUndefined()))
76        return false;
77
78    if (!globalState()) {
79        ASSERT_NOT_REACHED();
80        return false;
81    }
82
83    result = value.toString(globalState());
84    return true;
85}
86
87bool ScriptArguments::isEqual(ScriptArguments* other) const
88{
89    if (!other)
90        return false;
91
92    if (m_arguments.size() != other->m_arguments.size())
93        return false;
94    if (!globalState() && m_arguments.size())
95        return false;
96
97    for (size_t i = 0; i < m_arguments.size(); ++i) {
98        if (!m_arguments[i].isEqual(other->globalState(), other->m_arguments[i]))
99            return false;
100    }
101
102    return true;
103}
104
105} // namespace Inspector
106