Gnash  0.8.11dev
ActionExec.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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
18 
19 #ifndef GNASH_ACTIONEXEC_H
20 #define GNASH_ACTIONEXEC_H
21 
22 #include <string>
23 #include <stack>
24 #include <vector>
25 #include <boost/noncopyable.hpp>
26 
27 #include "as_environment.h"
28 #include "SWF.h"
29 #include "action_buffer.h"
30 
31 // Forward declarations
32 namespace gnash {
33  class as_value;
34  class Function;
35  class ActionExec;
36 }
37 
38 namespace gnash {
39 
40 class TryBlock
41 {
42 public:
43  friend class ActionExec;
44 
45  enum tryState
46  {
47  TRY_TRY, // In a try block.
48  TRY_CATCH, // In a catch block.
49  TRY_FINALLY, // In a finally block.
50  TRY_END // Finished with finally
51  };
52 
53  TryBlock(size_t cur_off, size_t try_size, size_t catch_size,
54  size_t finally_size, std::string catchName)
55  :
56  _catchOffset(cur_off + try_size),
57  _finallyOffset(cur_off + try_size + catch_size),
58  _afterTriedOffset(cur_off + try_size + catch_size + finally_size),
59  _savedEndOffset(0),
60  _hasName(true),
61  _name(std::move(catchName)),
62  _registerIndex(0),
63  _tryState(TryBlock::TRY_TRY),
64  _lastThrow()
65  {}
66 
67  TryBlock(size_t cur_off, size_t try_size, size_t catch_size,
68  size_t finally_size, std::uint8_t register_index)
69  :
70  _catchOffset(cur_off + try_size),
71  _finallyOffset(cur_off + try_size + catch_size),
72  _afterTriedOffset(cur_off + try_size + catch_size + finally_size),
73  _savedEndOffset(0),
74  _hasName(false),
75  _name(),
76  _registerIndex(register_index),
77  _tryState(TryBlock::TRY_TRY),
78  _lastThrow()
79  {}
80 
81 private:
82  size_t _catchOffset;
83  size_t _finallyOffset;
84  size_t _afterTriedOffset;
85  size_t _savedEndOffset;
86  bool _hasName;
87  std::string _name;
88  unsigned int _registerIndex;
89  tryState _tryState;
90  as_value _lastThrow;
91 };
92 
93 class With
94 {
95 public:
96 
97  With(as_object* obj, size_t end)
98  :
99  _object(obj),
100  _block_end_pc(end)
101  {
102  }
103 
104  size_t end_pc() const {
105  return _block_end_pc;
106  }
107 
108  as_object* object() const {
109  return _object;
110  }
111 
112 private:
114  size_t _block_end_pc;
115 };
116 
118 class ActionExec : boost::noncopyable
119 {
120 
121  typedef as_environment::ScopeStack ScopeStack;
122 
123 public:
124 
126  //
132  ActionExec(const action_buffer& abuf, as_environment& newEnv,
133  bool abortOnUnloaded = true);
134 
136  //
141  ActionExec(const Function& func, as_environment& newEnv,
142  as_value* nRetVal, as_object* this_ptr);
143 
145  void pushTryBlock(TryBlock t);
146 
148  void pushReturn(const as_value& t);
149 
151  //
154 
157 
160 
162  bool isFunction() const { return _func != nullptr; }
163 
165  as_object* getThisPointer();
166 
168  const ScopeStack& getScopeStack() const {
169  return _scopeStack;
170  }
171 
173  //
176  bool pushWith(const With& entry);
177 
179  //
181  void skip_actions(size_t offset);
182 
184  //
186  bool delVariable(const std::string& name);
187 
189  //
191  void setVariable(const std::string& name, const as_value& val);
192 
194  //
196  //
199  void setLocalVariable(const std::string& name, const as_value& val);
200 
202  //
208  as_value getVariable(const std::string& name, as_object** target = nullptr);
209 
211  //
220  as_object* getTarget();
221 
223  void operator()();
224 
225  // TODO: cut down these accessors.
226  bool atActionTag(SWF::ActionType t) { return code[pc] == t; }
227 
228  size_t getCurrentPC() const { return pc; }
229 
230  void skipRemainingBuffer() { next_pc = stop_pc; }
231 
232  void adjustNextPC(int offset);
233 
234  size_t getNextPC() const { return next_pc; }
235 
236  void setNextPC(size_t pc) { next_pc = pc; }
237 
238  size_t getStopPC() const { return stop_pc; }
239 
240 private:
241 
245  //
255  void dumpActions(size_t start, size_t end, std::ostream& os);
256 
258  //
267  //
270  bool processExceptions(TryBlock& t);
271 
274  //
287  void cleanupAfterRun();
288 
290  std::vector<With> _withStack;
291 
293  ScopeStack _scopeStack;
294 
303  const Function* _func;
304 
306  as_object* _this_ptr;
307 
309  size_t _initialStackSize;
310 
311  DisplayObject* _originalTarget;
312 
313  int _origExecSWFVersion;
314 
315  std::stack<TryBlock> _tryList;
316 
317  bool _returning;
318 
319  bool _abortOnUnload;
320 
322  size_t pc;
323 
325  size_t next_pc;
326 
329  size_t stop_pc;
330 
331 };
332 
333 } // namespace gnash
334 
335 #endif // GNASH_ACTIONEXEC_H
336 
337 // Local Variables:
338 // mode: C++
339 // indent-tabs-mode: t
340 // End:
Definition: ActionExec.h:40
Definition: ActionExec.h:50
as_object * _object
Definition: Array_as.cpp:680
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
const action_buffer & code
The actual action buffer.
Definition: ActionExec.h:153
Definition: klash_part.cpp:330
void setVariable(const as_environment &env, const std::string &varname, const as_value &val, const as_environment::ScopeStack &scope)
Given a path to variable, set its value.
Definition: as_environment.cpp:328
ActionScript value type.
Definition: as_value.h:94
size_t getStopPC() const
Definition: ActionExec.h:238
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
size_t getNextPC() const
Definition: ActionExec.h:234
A simple SWF-defined Function.
Definition: Function.h:63
ActionType
SWF action ids. Symbolic names copied from Ming.
Definition: SWF.h:124
The base class for all ActionScript objects.
Definition: as_object.h:161
as_value getVariable(const as_environment &env, const std::string &varname, const as_environment::ScopeStack &scope, as_object **retTarget)
Return the (possibly undefined) value of the named var.
Definition: as_environment.cpp:289
bool delVariable(const as_environment &ctx, const std::string &varname, const as_environment::ScopeStack &scope)
Delete a variable, without support for the path, using a ScopeStack.
Definition: as_environment.cpp:357
Definition: ActionExec.h:93
as_environment & env
TODO: provide a getter and make private ?
Definition: ActionExec.h:156
bool isFunction() const
Is this execution thread a function call ?
Definition: ActionExec.h:162
as_object * object() const
Definition: ActionExec.h:108
Provides information about timeline context.
Definition: as_environment.h:50
tryState
Definition: ActionExec.h:45
Definition: GnashKey.h:166
const ScopeStack & getScopeStack() const
Returns the scope stack associated with this execution thread.
Definition: ActionExec.h:168
size_t getCurrentPC() const
Definition: ActionExec.h:228
size_t end_pc() const
Definition: ActionExec.h:104
Executor of an action_buffer.
Definition: ActionExec.h:118
void setNextPC(size_t pc)
Definition: ActionExec.h:236
Definition: ActionExec.h:47
Definition: ActionExec.h:49
TryBlock(size_t cur_off, size_t try_size, size_t catch_size, size_t finally_size, std::string catchName)
Definition: ActionExec.h:53
void skipRemainingBuffer()
Definition: ActionExec.h:230
TryBlock(size_t cur_off, size_t try_size, size_t catch_size, size_t finally_size, std::uint8_t register_index)
Definition: ActionExec.h:67
Definition: ActionExec.h:48
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
as_value * retval
TODO: provide a setter and make private ?
Definition: ActionExec.h:159
A code segment.
Definition: action_buffer.h:49
std::vector< as_object * > ScopeStack
A stack of objects used for variables/members lookup.
Definition: as_environment.h:55
bool atActionTag(SWF::ActionType t)
Definition: ActionExec.h:226
std::string name
Definition: LocalConnection_as.cpp:149
friend class ActionExec
Definition: ActionExec.h:43
With(as_object *obj, size_t end)
Definition: ActionExec.h:97