initial import
[vuplus_webkit] / 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 JSValue JSJavaScriptCallFrame::type(ExecState* exec) const
57 {
58     switch (impl()->type()) {
59         case DebuggerCallFrame::FunctionType:
60             return jsString(exec, UString("function"));
61         case DebuggerCallFrame::ProgramType:
62             return jsString(exec, UString("program"));
63     }
64
65     ASSERT_NOT_REACHED();
66     return jsNull();
67 }
68
69 JSValue JSJavaScriptCallFrame::scopeChain(ExecState* exec) const
70 {
71     if (!impl()->scopeChain())
72         return jsNull();
73
74     ScopeChainNode* scopeChain = impl()->scopeChain();
75     ScopeChainIterator iter = scopeChain->begin();
76     ScopeChainIterator end = scopeChain->end();
77
78     // we must always have something in the scope chain
79     ASSERT(iter != end);
80
81     MarkedArgumentBuffer list;
82     do {
83         list.append(iter->get());
84         ++iter;
85     } while (iter != end);
86
87     return constructArray(exec, globalObject(), list);
88 }
89
90 JSValue JSJavaScriptCallFrame::scopeType(ExecState* exec)
91 {
92     if (!impl()->scopeChain())
93         return jsUndefined();
94
95     if (!exec->argument(0).isInt32())
96         return jsUndefined();
97     int index = exec->argument(0).asInt32();
98
99     ScopeChainNode* scopeChain = impl()->scopeChain();
100     ScopeChainIterator end = scopeChain->end();
101
102     bool foundLocalScope = false;
103     for (ScopeChainIterator iter = scopeChain->begin(); iter != end; ++iter) {
104         JSObject* scope = iter->get();
105         if (scope->isActivationObject()) {
106             if (!foundLocalScope) {
107                 // First activation object is local scope, each successive activation object is closure.
108                 if (!index)
109                     return jsJavaScriptCallFrameLOCAL_SCOPE(exec, JSValue(), Identifier());
110                 foundLocalScope = true;
111             } else if (!index)
112                 return jsJavaScriptCallFrameCLOSURE_SCOPE(exec, JSValue(), Identifier());
113         }
114
115         if (!index) {
116             // Last in the chain is global scope.
117             if (++iter == end)
118                 return jsJavaScriptCallFrameGLOBAL_SCOPE(exec, JSValue(), Identifier());
119             return jsJavaScriptCallFrameWITH_SCOPE(exec, JSValue(), Identifier());
120         }
121
122         --index;
123     }
124     return jsUndefined();
125 }
126
127 } // namespace WebCore
128
129 #endif // ENABLE(JAVASCRIPT_DEBUGGER)