Drop the bool operator for ObjectURI, to avoid getting the kind of side-effect that...
[gnash.git] / libcore / as_environment.h
blob59cb3c036e2c1a9eb9e91fc7a96d574612d1059d
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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.
14 //
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
27 #include <vector>
29 namespace gnash {
31 // Forward declarations
32 class DisplayObject;
33 class VM;
34 class Global_as;
35 class movie_root;
36 class string_table;
37 class UserFunction;
39 /// ActionScript execution environment.
40 class as_environment
43 public:
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
56 /// @param target
57 /// A DisplayObject to apply timeline opcodes on.
58 /// Zero is a valid target, disabling timeline
59 /// opcodes (would get ignored).
60 ///
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) {
74 _stack.push(val);
77 /// Pops an as_value off the stack top and return it.
78 as_value pop()
80 try {
81 return _stack.pop();
82 } catch (StackException&) {
83 return undefVal;
87 /// Get stack value at the given distance from top.
89 /// top(0) is actual stack top
90 ///
91 /// Throw StackException if index is out of range
92 ///
93 as_value& top(size_t dist)
95 try {
96 return _stack.top(dist);
97 } catch (StackException&) {
98 return undefVal;
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
110 try {
111 return _stack.value(index);
112 } catch (StackException&) {
113 return undefVal;
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
121 // exceptions...
122 _stack.drop(std::min(count, _stack.size()));
125 size_t stack_size() const { return _stack.size(); }
127 /// \brief
128 /// Delete a variable, w/out support for the path, using
129 /// a ScopeStack.
131 /// @param varname
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.
146 /// @param varname
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.
157 /// @param retTarget
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;
164 /// \brief
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.
172 /// @param path
173 /// Variable path.
174 /// TODO: should be case-insensitive up to SWF6.
176 /// @param val
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);
185 #ifdef GNASH_USE_GC
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;
193 #endif
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;
218 private:
220 VM& _vm;
222 /// Stack of as_values in this environment
223 SafeStack<as_value>& _stack;
225 /// Movie target.
226 DisplayObject* m_target;
228 /// Movie 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.
236 /// @param var
237 /// Variable name. Can not contain path elements.
238 /// TODO: should be case-insensitive up to SWF6.
240 /// @param val
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.
247 /// @param retTarget
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 '/')
265 /// or
266 /// path.to.some.var
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
275 /// dot-based path
277 bool parsePath(const std::string& var_path, std::string& path,
278 std::string& var);
280 inline VM&
281 getVM(const as_environment& env)
283 return env.getVM();
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
297 // Local Variables:
298 // mode: C++
299 // indent-tabs-mode: t
300 // End: