Git init
[framework/web/webkit-efl.git] / Source / WebCore / bindings / js / JSJavaScriptCallFrameCustom.cpp
1 /*
2  * Copyright (C) 2008 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
28 #if ENABLE(JAVASCRIPT_DEBUGGER)
29
30 #include "JSJavaScriptCallFrame.h"
31
32 #include "JavaScriptCallFrame.h"
33 #include <runtime/ArrayPrototype.h>
34 #include <runtime/Error.h>
35
36 using namespace JSC;
37
38 namespace WebCore {
39
40 JSValue JSJavaScriptCallFrame::evaluate(ExecState* exec)
41 {
42     JSValue exception;
43     JSValue result = impl()->evaluate(exec->argument(0).toString(exec), exception);
44
45     if (exception)
46         throwError(exec, exception);
47
48     return result;
49 }
50
51 JSValue JSJavaScriptCallFrame::thisObject(ExecState*) const
52 {
53     return impl()->thisObject() ? JSValue(impl()->thisObject()) : jsNull();
54 }
55
56 #if ENABLE(TIZEN_SUPPORT_BLUE_RUNNER)
57 JSValue JSJavaScriptCallFrame::functionObject(ExecState*) const
58 {
59     return impl()->functionObject() ? impl()->functionObject() : jsNull();
60 }
61 #endif
62
63 JSValue JSJavaScriptCallFrame::type(ExecState* exec) const
64 {
65     switch (impl()->type()) {
66         case DebuggerCallFrame::FunctionType:
67             return jsString(exec, UString("function"));
68         case DebuggerCallFrame::ProgramType:
69             return jsString(exec, UString("program"));
70     }
71
72     ASSERT_NOT_REACHED();
73     return jsNull();
74 }
75
76 JSValue JSJavaScriptCallFrame::scopeChain(ExecState* exec) const
77 {
78     if (!impl()->scopeChain())
79         return jsNull();
80
81     ScopeChainNode* scopeChain = impl()->scopeChain();
82     ScopeChainIterator iter = scopeChain->begin();
83     ScopeChainIterator end = scopeChain->end();
84
85     // we must always have something in the scope chain
86     ASSERT(iter != end);
87
88     MarkedArgumentBuffer list;
89     do {
90         list.append(iter->get());
91         ++iter;
92     } while (iter != end);
93
94     return constructArray(exec, globalObject(), list);
95 }
96
97 JSValue JSJavaScriptCallFrame::scopeType(ExecState* exec)
98 {
99     if (!impl()->scopeChain())
100         return jsUndefined();
101
102     if (!exec->argument(0).isInt32())
103         return jsUndefined();
104     int index = exec->argument(0).asInt32();
105
106     ScopeChainNode* scopeChain = impl()->scopeChain();
107     ScopeChainIterator end = scopeChain->end();
108
109     bool foundLocalScope = false;
110     for (ScopeChainIterator iter = scopeChain->begin(); iter != end; ++iter) {
111         JSObject* scope = iter->get();
112         if (scope->isActivationObject()) {
113             if (!foundLocalScope) {
114                 // First activation object is local scope, each successive activation object is closure.
115                 if (!index)
116                     return jsJavaScriptCallFrameLOCAL_SCOPE(exec, JSValue(), Identifier());
117                 foundLocalScope = true;
118             } else if (!index)
119                 return jsJavaScriptCallFrameCLOSURE_SCOPE(exec, JSValue(), Identifier());
120         }
121
122         if (!index) {
123             // Last in the chain is global scope.
124             if (++iter == end)
125                 return jsJavaScriptCallFrameGLOBAL_SCOPE(exec, JSValue(), Identifier());
126             return jsJavaScriptCallFrameWITH_SCOPE(exec, JSValue(), Identifier());
127         }
128
129         --index;
130     }
131     return jsUndefined();
132 }
133
134 } // namespace WebCore
135
136 #endif // ENABLE(JAVASCRIPT_DEBUGGER)