1/*
2 * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ProfilerOriginStack.h"
28
29#include "CodeOrigin.h"
30#include "InlineCallFrame.h"
31#include "JSGlobalObject.h"
32#include "JSCInlines.h"
33#include "ProfilerDatabase.h"
34
35namespace JSC { namespace Profiler {
36
37OriginStack::OriginStack(WTF::HashTableDeletedValueType)
38{
39 m_stack.append(Origin(WTF::HashTableDeletedValue));
40}
41
42OriginStack::OriginStack(const Origin& origin)
43{
44 m_stack.append(origin);
45}
46
47OriginStack::OriginStack(Database& database, CodeBlock* codeBlock, const CodeOrigin& codeOrigin)
48{
49 Vector<CodeOrigin> stack = codeOrigin.inlineStack();
50
51 append(Origin(database, codeBlock, stack[0].bytecodeIndex()));
52
53 for (unsigned i = 1; i < stack.size(); ++i) {
54 append(Origin(
55 database.ensureBytecodesFor(stack[i].inlineCallFrame()->baselineCodeBlock.get()),
56 stack[i].bytecodeIndex()));
57 }
58}
59
60OriginStack::~OriginStack() { }
61
62void OriginStack::append(const Origin& origin)
63{
64 m_stack.append(origin);
65}
66
67bool OriginStack::operator==(const OriginStack& other) const
68{
69 if (m_stack.size() != other.m_stack.size())
70 return false;
71
72 for (unsigned i = m_stack.size(); i--;) {
73 if (m_stack[i] != other.m_stack[i])
74 return false;
75 }
76
77 return true;
78}
79
80unsigned OriginStack::hash() const
81{
82 unsigned result = m_stack.size();
83
84 for (unsigned i = m_stack.size(); i--;) {
85 result *= 3;
86 result += m_stack[i].hash();
87 }
88
89 return result;
90}
91
92void OriginStack::dump(PrintStream& out) const
93{
94 for (unsigned i = 0; i < m_stack.size(); ++i) {
95 if (i)
96 out.print(" --> ");
97 out.print(m_stack[i]);
98 }
99}
100
101JSValue OriginStack::toJS(ExecState* exec) const
102{
103 VM& vm = exec->vm();
104 auto scope = DECLARE_THROW_SCOPE(vm);
105 JSArray* result = constructEmptyArray(exec, 0);
106 RETURN_IF_EXCEPTION(scope, { });
107
108 for (unsigned i = 0; i < m_stack.size(); ++i) {
109 result->putDirectIndex(exec, i, m_stack[i].toJS(exec));
110 RETURN_IF_EXCEPTION(scope, { });
111 }
112
113 return result;
114}
115
116} } // namespace JSC::Profiler
117
118