2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_AS_ENVIRONMENT_H
20 #define GNASH_AS_ENVIRONMENT_H
22 #include "smart_ptr.h" // GNASH_USE_GC
23 #include "as_value.h" // for composition (vector + frame_slot)
24 #include "SafeStack.h"
26 #include <string> // for frame_slot name
31 // Forward declarations
39 /// ActionScript execution environment.
45 /// A stack of objects used for variables/members lookup
46 typedef std::vector
<as_object
*> ScopeStack
;
48 as_environment(VM
& vm
);
50 VM
& getVM() const { return _vm
; }
52 DisplayObject
* get_target() const { return m_target
; }
54 /// Set default target for timeline opcodes
57 /// A DisplayObject to apply timeline opcodes on.
58 /// Zero is a valid target, disabling timeline
59 /// opcodes (would get ignored).
61 void set_target(DisplayObject
* target
);
63 void set_original_target(DisplayObject
* target
) {
64 _original_target
= target
;
67 DisplayObject
* get_original_target() { return _original_target
; }
69 // Reset target to its original value
70 void reset_target() { m_target
= _original_target
; }
72 /// Push a value on the stack
73 void push(const as_value
& val
) {
77 /// Pops an as_value off the stack top and return it.
82 } catch (StackException
&) {
87 /// Get stack value at the given distance from top.
89 /// top(0) is actual stack top
91 /// Throw StackException if index is out of range
93 as_value
& top(size_t dist
)
96 return _stack
.top(dist
);
97 } catch (StackException
&) {
102 /// Get stack value at the given distance from bottom.
104 /// bottom(stack_size()-1) is actual stack top
106 /// Throw StackException if index is out of range
108 as_value
& bottom(size_t index
) const
111 return _stack
.value(index
);
112 } catch (StackException
&) {
117 /// Drop 'count' values off the top of the stack.
118 void drop(size_t count
)
120 // in case count > stack size, just drop all, forget about
122 _stack
.drop(std::min(count
, _stack
.size()));
125 size_t stack_size() const { return _stack
.size(); }
128 /// Delete a variable, w/out support for the path, using
132 /// Variable name. Must not contain path elements.
133 /// TODO: should be case-insensitive up to SWF6.
134 /// NOTE: no case conversion is performed currently,
135 /// so make sure you do it yourself. Note that
136 /// ActionExec performs the conversion
137 /// before calling this method.
139 /// @param scopeStack
140 /// The Scope stack to use for lookups.
141 bool delVariableRaw(const std::string
& varname
,
142 const ScopeStack
& scopeStack
);
144 /// Return the (possibly UNDEFINED) value of the named var.
147 /// Variable name. Can contain path elements.
148 /// TODO: should be case-insensitive up to SWF6.
149 /// NOTE: no case conversion is performed currently,
150 /// so make sure you do it yourself. Note that
151 /// ActionExec performs the conversion
152 /// before calling this method.
154 /// @param scopeStack
155 /// The Scope stack to use for lookups.
158 /// If not NULL, the pointer will be set to the actual object containing the
159 /// found variable (if found).
161 as_value
get_variable(const std::string
& varname
,
162 const ScopeStack
& scopeStack
, as_object
** retTarget
=NULL
) const;
165 /// Given a path to variable, set its value.
167 /// If no variable with that name is found, a new one is created.
169 /// For path-less variables, this would act as a proxy for
170 /// set_variable_raw.
174 /// TODO: should be case-insensitive up to SWF6.
177 /// The value to assign to the variable.
179 /// @param scopeStack
180 /// The Scope stack to use for lookups.
182 void set_variable(const std::string
& path
, const as_value
& val
,
183 const ScopeStack
& scopeStack
);
186 /// Mark all reachable resources.
188 /// Reachable resources from an as_environment
189 /// would be global registers, stack (expected to be empty
190 /// actually), stack frames and targets (original and current).
192 void markReachableResources() const;
195 /// Find the sprite/movie referenced by the given path.
197 /// Supports both /slash/syntax and dot.syntax
198 /// Case insensitive for SWF up to 6, sensitive from 7 up
200 DisplayObject
* find_target(const std::string
& path
) const;
202 /// Find the object referenced by the given path.
204 /// Supports both /slash/syntax and dot.syntax
205 /// Case insensitive for SWF up to 6, sensitive from 7 up
207 as_object
* find_object(const std::string
& path
,
208 const ScopeStack
* scopeStack
= 0) const;
210 /// Return the SWF version we're running for.
212 /// NOTE: this is the version encoded in the first loaded
213 /// movie, and cannot be changed during play even if
214 /// replacing the root movie with an externally loaded one.
216 int get_version() const;
222 /// Stack of as_values in this environment
223 SafeStack
<as_value
>& _stack
;
226 DisplayObject
* m_target
;
229 DisplayObject
* _original_target
;
231 /// Given a variable name, set its value (no support for path)
233 /// If no variable with that name is found, a new one
234 /// will be created as a member of current target.
237 /// Variable name. Can not contain path elements.
238 /// TODO: should be case-insensitive up to SWF6.
241 /// The value to assign to the variable, if found.
242 void set_variable_raw(const std::string
& path
, const as_value
& val
,
243 const ScopeStack
& scopeStack
);
245 /// Same of the above, but no support for path.
248 /// If not NULL, the pointer will be set to the actual object containing the
249 /// found variable (if found).
251 as_value
get_variable_raw(const std::string
& varname
,
252 const ScopeStack
& scopeStack
, as_object
** retTarget
=NULL
) const;
254 static as_value undefVal
;
258 /// See if the given variable name is actually a sprite path
259 /// followed by a variable name. These come in the format:
261 /// /path/to/some/sprite/:varname
263 /// (or same thing, without the last '/')
268 /// If that's the format, puts the path part (no colon or
269 /// trailing slash) in *path, and the varname part (no colon, no dot)
270 /// in *var and returns true.
272 /// If no colon or dot, returns false and leaves *path & *var alone.
274 /// TODO: return an integer: 0 not a path, 1 a slash-based path, 2 a
277 bool parsePath(const std::string
& var_path
, std::string
& path
,
281 getVM(const as_environment
& env
)
286 movie_root
& getRoot(const as_environment
& env
);
287 string_table
& getStringTable(const as_environment
& env
);
288 int getSWFVersion(const as_environment
& env
);
289 Global_as
& getGlobal(const as_environment
&env
);
291 } // end namespace gnash
294 #endif // GNASH_AS_ENVIRONMENT_H
299 // indent-tabs-mode: t