source: webkit/trunk/JavaScriptCore/kjs/context.h@ 20019

Last change on this file since 20019 was 16117, checked in by ggaren, 19 years ago

JavaScriptCore:

Reviewed, tweaked by ggaren.


  • kjs/ExecState.cpp: (KJS::ExecState::ExecState):
  • kjs/ExecState.h:
  • kjs/context.h: (KJS::Context::setExecState): (KJS::Context::execState):

LayoutTests:

Reviewed by ggaren.

WebCore:

Reviewed, tweaked by ggaren.


  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 * Copyright (C) 2001 Peter Kelly ([email protected])
6 * Copyright (C) 2003, 2006 Apple Computer, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#ifndef KJS_Context_h
26#define KJS_Context_h
27
28#include "function.h"
29
30namespace KJS {
31
32 class ExecState;
33
34 /**
35 * @short Execution context.
36 *
37 * Represents an execution context, as specified by section 10 of the ECMA
38 * spec.
39 *
40 * An execution context contains information about the current state of the
41 * script - the scope for variable lookup, the value of "this", etc. A new
42 * execution context is entered whenever global code is executed (e.g. with
43 * Interpreter::evaluate()), a function is called (see
44 * Object::call()), or the builtin "eval" function is executed.
45 *
46 * Most inheritable functions in the KJS api take a ExecState pointer as
47 * their first parameter. This can be used to obtain a handle to the current
48 * execution context.
49 */
50 class Context {
51 public:
52 Context(JSObject* global, Interpreter*, JSObject* thisV,
53 FunctionBodyNode* currentBody, CodeType type = GlobalCode,
54 Context* callingContext = 0, FunctionImp* function = 0, const List* args = 0);
55 ~Context();
56
57 /**
58 * Returns the scope chain for this execution context. This is used for
59 * variable lookup, with the list being searched from start to end until a
60 * variable is found.
61 *
62 * @return The execution context's scope chain
63 */
64 const ScopeChain& scopeChain() const { return scope; }
65
66 /**
67 * Returns the variable object for the execution context. This contains a
68 * property for each variable declared in the execution context.
69 *
70 * @return The execution context's variable object
71 */
72 JSObject* variableObject() const { return m_variable; }
73 void setVariableObject(JSObject* v) { m_variable = v; }
74
75 /**
76 * Returns the "this" value for the execution context. This is the value
77 * returned when a script references the special variable "this". It should
78 * always be an Object, unless application-specific code has passed in a
79 * different type.
80 *
81 * The object that is used as the "this" value depends on the type of
82 * execution context - for global contexts, the global object is used. For
83 * function objewcts, the value is given by the caller (e.g. in the case of
84 * obj.func(), obj would be the "this" value). For code executed by the
85 * built-in "eval" function, the this value is the same as the calling
86 * context.
87 *
88 * @return The execution context's "this" value
89 */
90 JSObject* thisValue() const { return m_thisVal; }
91
92 /**
93 * Returns the context from which the current context was invoked. For
94 * global code this will be a null context (i.e. one for which
95 * isNull() returns true). You should check isNull() on the returned
96 * value before calling any of it's methods.
97 *
98 * @return The calling execution context
99 */
100 Context* callingContext() { return m_callingContext; }
101
102 JSObject* activationObject() { return m_activation; }
103 CodeType codeType() { return m_codeType; }
104 FunctionBodyNode* currentBody() { return m_currentBody; }
105 FunctionImp* function() const { return m_function; }
106 const List* arguments() const { return m_arguments; }
107
108 void pushScope(JSObject* s) { scope.push(s); }
109 void popScope() { scope.pop(); }
110 LabelStack* seenLabels() { return &ls; }
111
112 void pushIteration() { m_iterationDepth++; }
113 void popIteration() { m_iterationDepth--; }
114 bool inIteration() const { return (m_iterationDepth > 0); }
115
116 void pushSwitch() { m_switchDepth++; }
117 void popSwitch() { m_switchDepth--; }
118 bool inSwitch() const { return (m_switchDepth > 0); }
119
120 void mark();
121
122 void setExecState(ExecState* exec) { m_execState = exec; }
123 ExecState* execState() { return m_execState; }
124
125 private:
126 // Contexts are always stack-allocated, and the garbage collector
127 // marks the stack, so we don't need to protect the objects below from GC.
128
129 Interpreter* m_interpreter;
130 Context* m_callingContext;
131 FunctionBodyNode* m_currentBody;
132 ExecState* m_execState;
133
134 FunctionImp* m_function;
135 const List* m_arguments;
136 JSObject* m_activation;
137
138 ScopeChain scope;
139 JSObject* m_variable;
140 JSObject* m_thisVal;
141
142 LabelStack ls;
143 int m_iterationDepth;
144 int m_switchDepth;
145 CodeType m_codeType;
146 };
147
148} // namespace KJS
149
150#endif
151
Note: See TracBrowser for help on using the repository browser.