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:
113  as_object* _object;
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 
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:
gnash::ActionExec::code
const action_buffer & code
The actual action buffer.
Definition: ActionExec.h:153
gnash::SWF::SWFHandlers::instance
static const SWFHandlers & instance()
Return the singleton instance of SWFHandlers class.
Definition: ASHandlers.cpp:421
gnash::With::end_pc
size_t end_pc() const
Definition: ActionExec.h:104
action_buffer.h
gnash::action_buffer::size
size_t size() const
Definition: action_buffer.h:65
gnash::ActionExec::operator()
void operator()()
Execute.
Definition: ActionExec.cpp:125
SystemClock.h
movie_root.h
gnash::ActionExec::getThisPointer
as_object * getThisPointer()
Get the current 'this' pointer, for use in function calls.
Definition: ActionExec.cpp:706
name
std::string name
Definition: LocalConnection_as.cpp:149
gnash::VM::setSWFVersion
void setSWFVersion(int v)
Set SWF version of the currently executing code.
Definition: VM.cpp:74
gnash::as_value::is_exception
bool is_exception() const
Definition: as_value.h:348
gnash::ActionExec::pushReturn
void pushReturn(const as_value &t)
Set the return value.
Definition: ActionExec.cpp:659
gnash::SWF::SWFHandlers
A singleton containing the supported SWF Action handlers.
Definition: ASHandlers.h:74
gnash::log_swferror
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
gnash::as_object
The base class for all ActionScript objects.
Definition: as_object.h:162
gnash::SystemClock::restart
void restart()
Restart the clock.
Definition: SystemClock.cpp:53
gnash::ActionExec::isFunction
bool isFunction() const
Is this execution thread a function call ?
Definition: ActionExec.h:162
gnash::log_action
void log_action(StringType msg, Args... args)
Definition: log.h:307
gnash::TryBlock::TRY_END
@ TRY_END
Definition: ActionExec.h:50
gnash::ActionExec::getNextPC
size_t getNextPC() const
Definition: ActionExec.h:234
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::ActionExec::retval
as_value * retval
TODO: provide a setter and make private ?
Definition: ActionExec.h:159
gnash::key::i
@ i
Definition: GnashKey.h:155
gnash::delVariable
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
gnash::action_buffer::disasm
std::string disasm(size_t pc) const
Disassemble instruction at given offset and return as a string.
Definition: action_buffer.cpp:454
gnash::movie_definition::get_url
virtual const std::string & get_url() const =0
Return the URL of the SWF stream this definition has been read from.
gnash::typeName
std::string typeName(const T &inst)
Definition: utility.h:93
gnash::setLocal
void setLocal(CallFrame &c, const ObjectURI &name, const as_value &val)
Set a local variable in this CallFrame.
Definition: CallStack.cpp:80
gnash::Function
A simple SWF-defined Function.
Definition: Function.h:64
CallStack.h
gnash::getVariable
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
_
#define _(String)
Definition: log.h:44
gnash::VM::getRoot
movie_root & getRoot() const
Get a pointer to this VM's Root movie (stage)
Definition: VM.cpp:143
gnash::as_environment
Provides information about timeline context.
Definition: as_environment.h:51
gnash::SWF::ActionType
ActionType
SWF action ids. Symbolic names copied from Ming.
Definition: SWF.h:125
gnash::VM
The AVM1 virtual machine.
Definition: VM.h:72
gnash::ActionExec::setLocalVariable
void setLocalVariable(const std::string &name, const as_value &val)
Set a function-local variable.
Definition: ActionExec.cpp:627
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::ActionExec::skip_actions
void skip_actions(size_t offset)
Skip the specified number of action tags.
Definition: ActionExec.cpp:554
ActionExec.h
gnash::action_buffer::getMovieDefinition
const movie_definition & getMovieDefinition() const
Definition: action_buffer.h:185
start
@ start
Definition: klash_part.cpp:330
gnash::DisplayObject::getTarget
std::string DSOEXPORT getTarget() const
Return full path to this object, in dot notation.
Definition: DisplayObject.cpp:621
gnash::ActionExec
Executor of an action_buffer.
Definition: ActionExec.h:119
gnash::getVM
VM & getVM(const as_environment &env)
Definition: as_environment.h:222
gnash::SWF::ACTION_END
@ ACTION_END
Definition: SWF.h:126
gnash::ActionExec::getVariable
as_value getVariable(const std::string &name, as_object **target=nullptr)
Get a named variable, seeking for it in the with stack if any.
Definition: ActionExec.cpp:621
gnash::ActionExec::setVariable
void setVariable(const std::string &name, const as_value &val)
Set a named variable, seeking for it in the with stack if any.
Definition: ActionExec.cpp:615
gnash::ActionExec::pushWith
bool pushWith(const With &entry)
Push an entry to the with stack.
Definition: ActionExec.cpp:590
gnash::With
Definition: ActionExec.h:94
IF_VERBOSE_MALFORMED_SWF
#define IF_VERBOSE_MALFORMED_SWF(x)
Definition: log.h:404
gnash::With::object
as_object * object() const
Definition: ActionExec.h:108
gnash::ActionExec::getStopPC
size_t getStopPC() const
Definition: ActionExec.h:238
gnash::key::t
@ t
Definition: GnashKey.h:166
gnash::TryBlock::tryState
tryState
Definition: ActionExec.h:46
gnash::movie_root::queryInterface
bool queryInterface(const std::string &what) const
Ask the host interface a question.
Definition: movie_root.cpp:246
gnash::ActionExec::pushTryBlock
void pushTryBlock(TryBlock t)
Use this to push a try block. It will be copied.
Definition: ActionExec.cpp:649
gnash::as_environment::push
void push(const as_value &val)
Push a value on the stack.
Definition: as_environment.h:83
gnash::as_environment::stack_size
size_t stack_size() const
Definition: as_environment.h:117
IF_VERBOSE_ASCODING_ERRORS
#define IF_VERBOSE_ASCODING_ERRORS(x)
Definition: log.h:397
gnash::key::code
code
Definition: GnashKey.h:44
length
@ length
Definition: klash_part.cpp:329
gnash::VM::setRegister
void setRegister(size_t index, const as_value &val)
Set value of a register (local or global).
Definition: VM.cpp:206
gnash::ActionExec::ActionExec
ActionExec(const action_buffer &abuf, as_environment &newEnv, bool abortOnUnloaded=true)
Create an execution thread.
Definition: ActionExec.cpp:103
gnash::as_environment::ScopeStack
std::vector< as_object * > ScopeStack
A stack of objects used for variables/members lookup.
Definition: as_environment.h:55
gnash::action_buffer::read_int16
std::int16_t read_int16(size_t pc) const
Get a signed integer value from given offset.
Definition: action_buffer.h:105
gnash::movie_root::getTimeoutLimit
std::uint16_t getTimeoutLimit() const
Definition: movie_root.h:777
gnash::TryBlock::TRY_FINALLY
@ TRY_FINALLY
Definition: ActionExec.h:49
gnash::TryBlock::TryBlock
TryBlock(size_t cur_off, size_t try_size, size_t catch_size, size_t finally_size, std::string catchName)
Definition: ActionExec.h:53
gnash::ActionExec::setNextPC
void setNextPC(size_t pc)
Definition: ActionExec.h:236
gnash::ActionExec::getTarget
as_object * getTarget()
Get current target.
Definition: ActionExec.cpp:640
gnash::as_environment::target
DisplayObject * target() const
Definition: as_environment.h:61
gnash::VM::getSWFVersion
int getSWFVersion() const
Get SWF version context for the currently running actions.
Definition: VM.h:106
gnash::ActionExec::atActionTag
bool atActionTag(SWF::ActionType t)
Definition: ActionExec.h:226
gnash::SystemClock::elapsed
unsigned long int elapsed() const
Return number of milliseconds elapsed since start.
Definition: SystemClock.cpp:47
gnash::as_value::unflag_exception
void unflag_exception()
Definition: as_value.h:363
gnash::ActionExec::getScopeStack
const ScopeStack & getScopeStack() const
Returns the scope stack associated with this execution thread.
Definition: ActionExec.h:168
gnash::as_value
ActionScript value type.
Definition: as_value.h:95
gnash::TryBlock::TRY_TRY
@ TRY_TRY
Definition: ActionExec.h:47
gnash::ActionExec::skipRemainingBuffer
void skipRemainingBuffer()
Definition: ActionExec.h:230
gnash::DisplayObject::unloaded
bool unloaded() const
Return true if this DisplayObject was unloaded from the stage.
Definition: DisplayObject.h:785
VM.h
gnash::ActionExec::getCurrentPC
size_t getCurrentPC() const
Definition: ActionExec.h:228
gnash::as_environment::drop
void drop(size_t count)
Drop 'count' values off the top of the stack.
Definition: as_environment.h:111
gnash::CallFrame::function
UserFunction & function()
Get the function for which this CallFrame provides a scope.
Definition: CallStack.h:78
gnash::log_aserror
void log_aserror(StringType msg, Args... args)
Definition: log.h:331
DisplayObject.h
log.h
gnash::setVariable
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
gnash::TryBlock::TRY_CATCH
@ TRY_CATCH
Definition: ActionExec.h:48
gnash::getRoot
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
IF_VERBOSE_ACTION
#define IF_VERBOSE_ACTION(x)
Definition: log.h:384
gnash::ActionLimitException
An ActionScript limit exception.
Definition: GnashException.h:136
gnash::as_environment::get_original_target
DisplayObject * get_original_target() const
Definition: as_environment.h:77
as_environment.h
gnash::getObject
as_object * getObject(const DisplayObject *d)
Return the as_object associated with a DisplayObject if it exists.
Definition: DisplayObject.h:1160
gnashconfig.h
gnash::image::end
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
SWF.h
gnash::TryBlock::TryBlock
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
gnash::as_environment::set_target
void set_target(DisplayObject *target)
Set default target for timeline opcodes.
Definition: as_environment.h:68
gnash::ActionExec::env
as_environment & env
TODO: provide a getter and make private ?
Definition: ActionExec.h:156
gnash::VM::dumpState
void dumpState(std::ostream &o, size_t limit=0)
Print stack, call stack, and registers to the specified ostream.
Definition: VM.cpp:299
gnash::as_environment::pop
as_value pop()
Pops an as_value off the stack top and return it.
Definition: as_environment.h:88
gnash::movie_root::flushHigherPriorityActionQueues
void flushHigherPriorityActionQueues()
Definition: movie_root.cpp:1428
Function.h
ASHandlers.h
STACK_DUMP_LIMIT
#define STACK_DUMP_LIMIT
Definition: ActionExec.cpp:52
gnash::getURI
ObjectURI getURI(const VM &vm, const std::string &str, bool lowerCaseHint=false)
Definition: VM.h:290
gnash::ActionExec::adjustNextPC
void adjustNextPC(int offset)
Definition: ActionExec.cpp:668
gnash::DisplayObject
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:169
gnash::action_buffer
A code segment.
Definition: action_buffer.h:50
gnash::ActionExec::delVariable
bool delVariable(const std::string &name)
Delete named variable, seeking for it in the with stack if any.
Definition: ActionExec.cpp:609
gnash::SystemClock
A system-clock based virtual clock.
Definition: SystemClock.h:37
gnash::CallFrame
A CallFrame is an element of a CallStack.
Definition: CallStack.h:44
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::SWF::SWFHandlers::execute
void execute(ActionType type, ActionExec &thread) const
Execute the action identified by 'type' action type.
Definition: ASHandlers.cpp:428
gnash::With::With
With(as_object *obj, size_t end)
Definition: ActionExec.h:97
GnashException.h
gnash::TryBlock
Definition: ActionExec.h:41
gnash::CallFrame::locals
as_object & locals()
Access the local variables for this function call.
Definition: CallStack.h:73
gnash::action_buffer::getDefinitionVersion
int getDefinitionVersion() const
Return version of the SWF this action block was found in.
Definition: action_buffer.cpp:479
gnash::as_environment::top
as_value & top(size_t dist) const
Get stack value at the given distance from top.
Definition: as_environment.h:102
gnash::VM::currentCall
CallFrame & currentCall()
Return the CallFrame of the currently executing function.
Definition: VM.cpp:229
gnash::movie_root::scriptsDisabled
bool scriptsDisabled() const
Return true if scripts execution is disabled.
Definition: movie_root.h:607