blob: 51d1fe658d56a95aad5bdba4339fc2c92aaea3c4 [file] [log] [blame]
yurys@chromium.org02ff0d62010-11-08 14:05:32 +00001/*
joepeck@webkit.org1f45df92014-02-06 23:45:53 +00002 * Copyright (C) 2014 Apple Inc. All rights reserved.
yurys@chromium.org02ff0d62010-11-08 14:05:32 +00003 * Copyright (c) 2010 Google Inc. All rights reserved.
joepeck@webkit.org1f45df92014-02-06 23:45:53 +00004 *
yurys@chromium.org02ff0d62010-11-08 14:05:32 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
joepeck@webkit.org1f45df92014-02-06 23:45:53 +00008 *
yurys@chromium.org02ff0d62010-11-08 14:05:32 +00009 * * 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.
joepeck@webkit.org1f45df92014-02-06 23:45:53 +000018 *
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000019 * 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
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000035#include "JSCInlines.h"
joepeck@webkit.org1f45df92014-02-06 23:45:53 +000036#include "ScriptValue.h"
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000037
joepeck@webkit.org1f45df92014-02-06 23:45:53 +000038namespace Inspector {
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000039
gyuyoung.kim@webkit.org20685892015-06-09 02:17:48 +000040Ref<ScriptArguments> ScriptArguments::create(JSC::ExecState* scriptState, Vector<Deprecated::ScriptValue>& arguments)
yurys@chromium.orgec8a30f2010-11-30 08:15:28 +000041{
gyuyoung.kim@webkit.org20685892015-06-09 02:17:48 +000042 return adoptRef(*new ScriptArguments(scriptState, arguments));
yurys@chromium.orgec8a30f2010-11-30 08:15:28 +000043}
44
joepeck@webkit.org1f45df92014-02-06 23:45:53 +000045ScriptArguments::ScriptArguments(JSC::ExecState* execState, Vector<Deprecated::ScriptValue>& arguments)
46 : m_globalObject(execState->vm(), execState->lexicalGlobalObject())
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000047{
48 m_arguments.swap(arguments);
49}
50
51ScriptArguments::~ScriptArguments()
52{
53}
54
joepeck@webkit.orga3198442013-12-11 22:40:23 +000055const Deprecated::ScriptValue &ScriptArguments::argumentAt(size_t index) const
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000056{
57 ASSERT(m_arguments.size() > index);
58 return m_arguments[index];
59}
60
weinig@apple.com4da3e8d2013-09-14 01:30:06 +000061JSC::ExecState* ScriptArguments::globalState() const
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000062{
andersca@apple.com22504ad2013-04-16 20:05:38 +000063 if (m_globalObject)
64 return const_cast<JSC::JSGlobalObject*>(m_globalObject.get())->globalExec();
joepeck@webkit.org1f45df92014-02-06 23:45:53 +000065
joepeck@webkit.orged8058e2014-01-20 21:36:28 +000066 return nullptr;
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000067}
68
69bool ScriptArguments::getFirstArgumentAsString(String& result, bool checkForNullOrUndefined)
70{
71 if (!argumentCount())
72 return false;
73
joepeck@webkit.orga3198442013-12-11 22:40:23 +000074 const Deprecated::ScriptValue& value = argumentAt(0);
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000075 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 }
joepeck@webkit.org1f45df92014-02-06 23:45:53 +0000101
yurys@chromium.org02ff0d62010-11-08 14:05:32 +0000102 return true;
103}
104
joepeck@webkit.org1f45df92014-02-06 23:45:53 +0000105} // namespace Inspector