"Fossies" - the Fresh Open Source Software Archive

Member "gnash-0.8.10/libcore/vm/ActionExec.h" (19 Jan 2012, 9513 Bytes) of package /linux/www/old/gnash-0.8.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code 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 <list>
   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         _hasName(true),
   60         _name(catchName),
   61         _registerIndex(0),
   62         _tryState(TryBlock::TRY_TRY),
   63         _lastThrow()
   64     {}
   65 
   66     TryBlock(size_t cur_off, size_t try_size, size_t catch_size,
   67         size_t finally_size, boost::uint8_t register_index)
   68         :
   69         _catchOffset(cur_off + try_size),
   70         _finallyOffset(cur_off + try_size + catch_size),
   71         _afterTriedOffset(cur_off + try_size + catch_size + finally_size),
   72         _hasName(false),
   73         _name(),
   74         _registerIndex(register_index),
   75         _tryState(TryBlock::TRY_TRY),
   76         _lastThrow()
   77     {}
   78 
   79 private:
   80     size_t _catchOffset;
   81     size_t _finallyOffset;
   82     size_t _afterTriedOffset;
   83     size_t _savedEndOffset;
   84     bool _hasName;
   85     std::string _name;
   86     unsigned int _registerIndex;
   87     tryState _tryState;
   88     as_value _lastThrow;
   89 };
   90 
   91 class With
   92 {
   93 public: 
   94 
   95     With(as_object* obj, size_t end)
   96         :
   97         _object(obj),
   98         _block_end_pc(end)
   99     {
  100     }
  101 
  102     size_t end_pc() const {
  103         return _block_end_pc;
  104     }
  105 
  106     as_object* object() const {
  107         return _object;
  108     }
  109 
  110 private:
  111     as_object* _object;
  112     size_t _block_end_pc;
  113 };
  114 
  115 /// Executor of an action_buffer 
  116 class ActionExec : boost::noncopyable
  117 {
  118 
  119     typedef as_environment::ScopeStack ScopeStack;
  120 
  121 public:
  122 
  123     /// Create an execution thread 
  124     //
  125     /// @param abuf             the action code
  126     /// @param newEnv           the timeline context.
  127     /// @param abortOnUnloaded  If true (default) execution aborts as soon
  128     ///                         as the target sprite is unloaded.
  129     /// NOTE: original target is fetched from the environment.
  130     ActionExec(const action_buffer& abuf, as_environment& newEnv,
  131             bool abortOnUnloaded = true);
  132 
  133     /// Create an execution thread for a function call.
  134     //
  135     /// @param func     The function 
  136     /// @param newEnv   The execution environment (variables scope, stack etc.)
  137     /// @param nRetval  Where to return a value. If NULL any return will
  138     ///                 be discarded.
  139     ActionExec(const Function& func, as_environment& newEnv,
  140             as_value* nRetVal, as_object* this_ptr);
  141 
  142     /// Use this to push a try block. It will be copied
  143     void pushTryBlock(TryBlock t);
  144 
  145     /// Set the return value.
  146     void pushReturn(const as_value& t);
  147 
  148     /// The actual action buffer
  149     //
  150     /// TODO: provide a getter and make private
  151     const action_buffer& code;
  152 
  153     /// TODO: provide a getter and make private ?
  154     as_environment& env;
  155 
  156     /// TODO: provide a setter and make private ?
  157     as_value* retval;
  158 
  159     /// Is this execution thread a function call ?
  160     bool isFunction() const { return _func != 0; }
  161 
  162     /// Get the current 'this' pointer, for use in function calls
  163     as_object* getThisPointer();
  164 
  165     /// Returns the scope stack associated with this execution thread
  166     const ScopeStack& getScopeStack() const {
  167         return _scopeStack;
  168     }
  169 
  170     /// Push an entry to the with stack
  171     //
  172     /// @return     true if the entry was pushed, false otherwise. This
  173     ///             depends on the with stack limit.
  174     bool pushWith(const With& entry);
  175 
  176     /// Skip the specified number of action tags 
  177     //
  178     /// The offset is relative to next_pc
  179     void skip_actions(size_t offset);
  180 
  181     /// Delete named variable, seeking for it in the with stack if any
  182     //
  183     /// @param name     Name of the variable. Supports slash and dot syntax.
  184     bool delVariable(const std::string& name);
  185 
  186     /// Set a named variable, seeking for it in the with stack if any.
  187     //
  188     /// @param name     Name of the variable. Supports slash and dot syntax.
  189     void setVariable(const std::string& name, const as_value& val);
  190 
  191     /// Set a function-local variable
  192     //
  193     /// If we're not in a function, set a normal variable.
  194     //
  195     /// @param name Name of the variable. Supports slash and dot syntax.
  196     /// @param val  The value to set the variable to.
  197     void setLocalVariable(const std::string& name, const as_value& val);
  198 
  199     /// Get a named variable, seeking for it in the with stack if any.
  200     //
  201     /// @param name     Name of the variable. Supports slash and dot syntax.
  202     /// @param target   An output parameter, will be set to point to the object
  203     ///                 containing any found variable. If you aren't interested,
  204     ///                 pass null (default). If the variable does not belong
  205     ///                 to an object, target will be set to null.
  206     as_value getVariable(const std::string& name, as_object** target = 0);
  207 
  208     /// Get current target.
  209     //
  210     /// This function returns top 'with' stack entry, if any.
  211     /// Main use for this function is for calling methods and
  212     /// properly setting the "this" pointer. 
  213     ///
  214     /// TODO:
  215     /// A better, cleaner and less error-prone approach
  216     /// would be providing a callFunction() method in
  217     /// ActionExec. This will likely help debugger too
  218     as_object* getTarget();
  219 
  220     /// Execute.
  221     void operator()();
  222 
  223     // TODO: cut down these accessors.
  224     bool atActionTag(SWF::ActionType t) { return code[pc] == t; }
  225     
  226     size_t getCurrentPC() const { return pc; }
  227     
  228     void skipRemainingBuffer() { next_pc = stop_pc; }
  229     
  230     void adjustNextPC(int offset);
  231     
  232     size_t getNextPC() const { return next_pc; }
  233     
  234     void setNextPC(size_t pc) { next_pc = pc; }
  235     
  236     size_t getStopPC() const { return stop_pc; }
  237     
  238 private: 
  239 
  240     /// \brief
  241     /// Debugging function:
  242     /// print opcodes from start (included) to end (not-included) PCs.
  243     //
  244     /// @param start
  245     /// First opcode to dump
  246     ///
  247     /// @param end
  248     /// One-past last opcode to dump
  249     ///
  250     /// @param os
  251     /// Output stream to dump to
  252     ///
  253     void dumpActions(size_t start, size_t end, std::ostream& os);
  254 
  255     /// Processes the current try - catch - finally block
  256     //
  257     /// This function is called after each stage of a
  258     /// try/catch/finally block. It ensures it is called
  259     /// after each stage by setting stop_pc to the appropriate
  260     /// number. If an exception is on the stack at any stage,
  261     /// it takes the appropriate action (catch, set register
  262     /// values, return, or leave it on the stack). Return
  263     /// false means that the action processing loop should be
  264     /// interrupted.
  265     //
  266     /// @return whether to continue executing the buffer
  267     /// @param t the try block to process.
  268     bool processExceptions(TryBlock& t);
  269 
  270     /// Run after a complete run, or after an run interrupted by 
  271     /// a bail-out exception (ActionLimitException, for example)
  272     //
  273     /// The method restores original target of the as_environment,
  274     /// checks for stack smashing (stack contains less entries
  275     /// then it had at time of execution start) or leftovers
  276     /// (stack contains more entries then it had at time of execution
  277     /// start) and finally gives movie_root a chance to execute
  278     /// actions queued in higher priority action queues.
  279     ///
  280     /// The higher priority action queue flush is needed to allow
  281     /// initialize/construct/initactions queued by effect of gotoFrame
  282     /// calls in DOACTION block before frame actions queued by the same
  283     /// cause (the latter would be pushed in the same level gotoFrame is
  284     /// found)
  285     void cleanupAfterRun();
  286 
  287     /// the 'with' stack associated with this execution thread
  288     std::vector<With> _withStack;
  289 
  290     /// the scope stack associated with this execution thread
  291     ScopeStack _scopeStack;
  292 
  293     /// A pointer to the function being executed, or NULL
  294     /// for non-function execution
  295     ///
  296     /// TODO: 
  297     /// This should likely be put in a larger
  298     /// structure including return address 
  299     /// and maintained in a stack (the call stack)
  300     ///
  301     const Function* _func;
  302 
  303     /// The 'this' pointer, if this is a function call
  304     as_object* _this_ptr;
  305 
  306     /// Stack size at start of execution
  307     size_t _initialStackSize;
  308 
  309     DisplayObject* _originalTarget;
  310 
  311     int _origExecSWFVersion;
  312 
  313     std::list<TryBlock> _tryList;
  314 
  315     bool _returning;
  316 
  317     bool _abortOnUnload;
  318 
  319     /// Program counter (offset of current action tag)
  320     size_t pc;
  321 
  322     /// Offset to next action tag
  323     size_t next_pc;
  324 
  325     /// End of current function execution
  326     /// Used for try/throw/catch blocks.
  327     size_t stop_pc;
  328 
  329 };
  330 
  331 } // namespace gnash
  332 
  333 #endif // GNASH_ACTIONEXEC_H
  334 
  335 // Local Variables:
  336 // mode: C++
  337 // indent-tabs-mode: t
  338 // End: