use the VERSION instead of master in the revno.h generated from a src tarball
[gnash.git] / libcore / Function.h
blob31994fdb81dddb39416ba934b6437ad98db2dd02
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_SWF_FUNCTION_H
20 #define GNASH_SWF_FUNCTION_H
22 #include <vector>
23 #include <cassert>
24 #include <string>
26 #include "UserFunction.h"
27 #include "smart_ptr.h"
28 #include "ObjectURI.h"
30 // Forward declarations
31 namespace gnash {
32 class action_buffer;
33 class as_object;
36 namespace gnash {
38 class TargetGuard
40 public:
42 // @param ch : target to set temporarely
43 // @param och : original target to set temporarily
44 TargetGuard(as_environment& e, DisplayObject* ch, DisplayObject* och);
45 ~TargetGuard();
47 private:
49 as_environment& env;
50 DisplayObject* from;
51 DisplayObject* from_orig;
55 /// A simple SWF-defined Function
57 /// This represents a callable Function defined in a SWF. The basic version
58 /// creates a scope in which and 'arguments' array, 'this', 'super', and the
59 /// expected argument names are defined.
61 /// For a more advanced function, see Function2.
62 class Function : public UserFunction
65 public:
67 typedef std::vector<as_object*> ScopeStack;
69 /// \brief
70 /// Create an ActionScript function as defined in an
71 /// action_buffer starting at offset 'start'
73 Function(const action_buffer& ab, as_environment& env, size_t start,
74 const ScopeStack& with_stack);
76 virtual ~Function() {}
78 const ScopeStack& getScopeStack() const {
79 return _scopeStack;
82 const action_buffer& getActionBuffer() const {
83 return _action_buffer;
86 size_t getStartPC() const {
87 return _startPC;
90 size_t getLength() const {
91 return _length;
94 /// Get the number of registers required for function execution.
96 /// For ordinary Functions this is always 0.
97 virtual boost::uint8_t registers() const {
98 return 0;
101 /// Add an expected argument for the function.
103 /// For ordinary Functions the register is disregarded. This is only
104 /// relevant for Function2s.
106 /// All argument names are declared as variables in the function scope,
107 /// whether the argument is passed or not.
109 /// @param reg The register for the argument.
110 /// @param name The name of the argument.
111 void add_arg(boost::uint8_t reg, const ObjectURI& name) {
112 _args.push_back(Argument(reg, name));
115 /// Set the length in bytes of the function code.
116 void setLength(size_t len);
118 /// Dispatch.
119 virtual as_value call(const fn_call& fn);
121 /// Mark reachable resources. Override from as_object
123 /// Reachable resources from this object are its scope stack
124 /// and the prototype.
126 virtual void markReachableResources() const;
128 protected:
130 struct Argument
132 Argument(boost::uint8_t r, const ObjectURI& n) : reg(r), name(n) {}
133 boost::uint8_t reg;
134 ObjectURI name;
137 std::vector<Argument> _args;
139 /// @@ might need some kind of ref count here, but beware cycles
140 as_environment& _env;
142 private:
144 /// Action buffer containing the function definition
145 const action_buffer& _action_buffer;
147 /// Scope stack on function definition.
148 ScopeStack _scopeStack;
150 /// \brief
151 /// Offset within the action_buffer where
152 /// start of the function is found.
153 size_t _startPC;
155 /// Length of the function within the action_buffer
157 /// This is currently expressed in bytes as the
158 /// action_buffer is just a blocḱ of memory corresponding
159 /// to a DoAction block
160 size_t _length;
165 /// Add properties to an 'arguments' object.
167 /// The 'arguments' variable is an array with an additional
168 /// 'callee' member, set to the function being called.
169 as_object* getArguments(Function& callee, as_object& args,
170 const fn_call& fn, as_object* caller);
173 } // end of gnash namespace
175 #endif