]> git.saurik.com Git - apple/javascriptcore.git/blame - inspector/InjectedScriptBase.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / InjectedScriptBase.cpp
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 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 "InjectedScriptBase.h"
34
ed1e77d3 35#include "DebuggerEvalEnabler.h"
81345200
A
36#include "InspectorValues.h"
37#include "JSCInlines.h"
38#include "JSGlobalObject.h"
39#include "ScriptFunctionCall.h"
40#include <wtf/text/WTFString.h>
41
42namespace Inspector {
43
44InjectedScriptBase::InjectedScriptBase(const String& name)
45 : m_name(name)
46 , m_environment(nullptr)
47{
48}
49
50InjectedScriptBase::InjectedScriptBase(const String& name, Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
51 : m_name(name)
52 , m_injectedScriptObject(injectedScriptObject)
53 , m_environment(environment)
54{
55}
56
57InjectedScriptBase::~InjectedScriptBase()
58{
59}
60
61void InjectedScriptBase::initialize(Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
62{
63 m_injectedScriptObject = injectedScriptObject;
64 m_environment = environment;
65}
66
67bool InjectedScriptBase::hasAccessToInspectedScriptState() const
68{
69 return m_environment && m_environment->canAccessInspectedScriptState(m_injectedScriptObject.scriptState());
70}
71
72const Deprecated::ScriptObject& InjectedScriptBase::injectedScriptObject() const
73{
74 return m_injectedScriptObject;
75}
76
77Deprecated::ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(Deprecated::ScriptFunctionCall& function, bool& hadException) const
78{
79 if (m_environment)
80 m_environment->willCallInjectedScriptFunction(m_injectedScriptObject.scriptState(), name(), 1);
81
82 JSC::ExecState* scriptState = m_injectedScriptObject.scriptState();
ed1e77d3 83 Deprecated::ScriptValue resultValue;
81345200 84
ed1e77d3
A
85 {
86 JSC::DebuggerEvalEnabler evalEnabler(scriptState);
87 resultValue = function.call(hadException);
88 }
81345200
A
89
90 if (m_environment)
91 m_environment->didCallInjectedScriptFunction(m_injectedScriptObject.scriptState());
92
93 return resultValue;
94}
95
96void InjectedScriptBase::makeCall(Deprecated::ScriptFunctionCall& function, RefPtr<InspectorValue>* result)
97{
98 if (hasNoValue() || !hasAccessToInspectedScriptState()) {
99 *result = InspectorValue::null();
100 return;
101 }
102
103 bool hadException = false;
104 Deprecated::ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
105
106 ASSERT(!hadException);
107 if (!hadException) {
108 *result = resultValue.toInspectorValue(m_injectedScriptObject.scriptState());
109 if (!*result)
110 *result = InspectorString::create(String::format("Object has too long reference chain (must not be longer than %d)", InspectorValue::maxDepth));
111 } else
112 *result = InspectorString::create("Exception while making a call.");
113}
114
ed1e77d3 115void InjectedScriptBase::makeEvalCall(ErrorString& errorString, Deprecated::ScriptFunctionCall& function, RefPtr<Protocol::Runtime::RemoteObject>* objectResult, Protocol::OptOutput<bool>* wasThrown, Protocol::OptOutput<int>* savedResultIndex)
81345200
A
116{
117 RefPtr<InspectorValue> result;
118 makeCall(function, &result);
119 if (!result) {
ed1e77d3 120 errorString = ASCIILiteral("Internal error: result value is empty");
81345200
A
121 return;
122 }
123
ed1e77d3 124 if (result->type() == InspectorValue::Type::String) {
81345200 125 result->asString(errorString);
ed1e77d3 126 ASSERT(errorString.length());
81345200
A
127 return;
128 }
129
ed1e77d3
A
130 RefPtr<InspectorObject> resultTuple;
131 if (!result->asObject(resultTuple)) {
132 errorString = ASCIILiteral("Internal error: result is not an Object");
81345200
A
133 return;
134 }
135
ed1e77d3
A
136 RefPtr<InspectorObject> resultObject;
137 if (!resultTuple->getObject(ASCIILiteral("result"), resultObject)) {
138 errorString = ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
81345200
A
139 return;
140 }
141
ed1e77d3
A
142 bool wasThrownValue = false;
143 if (!resultTuple->getBoolean(ASCIILiteral("wasThrown"), wasThrownValue)) {
144 errorString = ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
145 return;
146 }
147
148 *objectResult = BindingTraits<Protocol::Runtime::RemoteObject>::runtimeCast(resultObject);
149 *wasThrown = wasThrownValue;
150
151 if (savedResultIndex) {
152 int savedIndex = 0;
153 if (resultTuple->getInteger(ASCIILiteral("savedResultIndex"), savedIndex))
154 *savedResultIndex = savedIndex;
155 }
81345200
A
156}
157
158} // namespace Inspector
159