Update with current status
[gnash.git] / libcore / Function.h
blob77812d9e7a95b7fc41fe3334804ae3f2eea6991f
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
19 #ifndef GNASH_SWF_FUNCTION_H
20 #define GNASH_SWF_FUNCTION_H
22 #include <vector>
23 #include <cassert>
24 #include <string>
26 #include "ConstantPool.h"
27 #include "UserFunction.h"
28 #include "ObjectURI.h"
30 // Forward declarations
31 namespace gnash {
32 class action_buffer;
33 class as_object;
34 class VM;
37 namespace gnash {
39 class TargetGuard
41 public:
43 // @param ch : target to set temporarely
44 // @param och : original target to set temporarily
45 TargetGuard(as_environment& e, DisplayObject* ch, DisplayObject* och);
46 ~TargetGuard();
48 private:
50 as_environment& env;
51 DisplayObject* from;
52 DisplayObject* from_orig;
56 /// A simple SWF-defined Function
58 /// This represents a callable Function defined in a SWF. The basic version
59 /// creates a scope in which 'arguments' array, 'this', 'super', and the
60 /// expected argument names are defined.
62 /// For a more advanced function, see Function2.
63 class Function : public UserFunction
66 public:
68 typedef std::vector<as_object*> ScopeStack;
70 /// \brief
71 /// Create an ActionScript function as defined in an
72 /// action_buffer starting at offset 'start'
74 Function(const action_buffer& ab, as_environment& env, size_t start,
75 ScopeStack with_stack);
77 virtual ~Function() {}
79 const ScopeStack& getScopeStack() const {
80 return _scopeStack;
83 const action_buffer& getActionBuffer() const {
84 return _action_buffer;
87 size_t getStartPC() const {
88 return _startPC;
91 size_t getLength() const {
92 return _length;
95 /// Get the number of registers required for function execution.
97 /// For ordinary Functions this is always 0.
98 virtual std::uint8_t registers() const {
99 return 0;
102 /// Add an expected argument for the function.
104 /// For ordinary Functions the register is disregarded. This is only
105 /// relevant for Function2s.
107 /// All argument names are declared as variables in the function scope,
108 /// whether the argument is passed or not.
110 /// @param reg The register for the argument.
111 /// @param name The name of the argument.
112 void add_arg(std::uint8_t reg, const ObjectURI& name) {
113 _args.emplace_back(reg, name);
116 /// Set the length in bytes of the function code.
117 void setLength(size_t len);
119 /// Dispatch.
120 virtual as_value call(const fn_call& fn);
122 /// Mark reachable resources. Override from as_object
124 /// Reachable resources from this object are its scope stack
125 /// and the prototype.
127 virtual void markReachableResources() const;
129 protected:
131 struct Argument
133 Argument(std::uint8_t r, ObjectURI n) : reg(r), name(std::move(n)) {}
134 std::uint8_t reg;
135 ObjectURI name;
138 std::vector<Argument> _args;
140 /// @@ might need some kind of ref count here, but beware cycles
141 as_environment& _env;
143 /// The ConstantPool in effect at time of function definition
144 const ConstantPool* _pool;
146 private:
148 /// Action buffer containing the function definition
149 const action_buffer& _action_buffer;
151 /// Scope stack on function definition.
152 ScopeStack _scopeStack;
154 /// \brief
155 /// Offset within the action_buffer where
156 /// start of the function is found.
157 size_t _startPC;
159 /// Length of the function within the action_buffer
161 /// This is currently expressed in bytes as the
162 /// action_buffer is just a blocḱ of memory corresponding
163 /// to a DoAction block
164 size_t _length;
168 /// Add properties to an 'arguments' object.
170 /// The 'arguments' variable is an array with an additional
171 /// 'callee' member, set to the function being called.
172 as_object* getArguments(Function& callee, as_object& args,
173 const fn_call& fn, as_object* caller);
176 } // end of gnash namespace
178 #endif