Gnash  0.8.11dev
fn_call.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_FN_CALL_H
20 #define GNASH_FN_CALL_H
21 
22 #include <string>
23 #include <vector>
24 #include <cassert>
25 #include <ostream>
26 #include <algorithm>
27 
28 #include "utility.h" // for typeName
29 #include "as_object.h"
30 #include "as_value.h"
31 #include "VM.h"
32 #include "GnashException.h"
33 #include "as_environment.h"
34 
35 
36 // Forward declarations
37 namespace gnash {
38  class movie_definition;
39 }
40 
41 namespace gnash {
42 
44 //
46 //
48 //
51 //
55 template<typename T>
56 class FunctionArgs
57 {
58 public:
59 
60  typedef typename std::vector<T>::size_type size_type;
61  typedef std::vector<T> container_type;
62  typedef T value_type;
63 
65 
67  FunctionArgs(const FunctionArgs& other)
68  :
69  _v(other._v)
70  {}
71 
73  _v.push_back(t);
74  return *this;
75  }
76 
78  _v.push_back(t);
79  return *this;
80  }
81 
83  //
86  void setReachable() const {
87  std::for_each(_v.begin(), _v.end(),
88  std::mem_fun_ref(&as_value::setReachable));
89  }
90 
91  void swap(std::vector<T>& to) {
92  std::swap(_v, to);
93  }
94 
95  size_type size() const {
96  return _v.size();
97  }
98 
99 private:
100  std::vector<T> _v;
101 };
102 
103 
107 class fn_call
108 {
109 public:
110 
112 
114  //
121  fn_call(as_object* this_in, const as_environment& env_in,
122  Args& args, as_object* sup = 0, bool isNew = false)
123  :
124  this_ptr(this_in),
125  super(sup),
126  nargs(args.size()),
127  callerDef(0),
128  _env(env_in),
129  _new(isNew)
130  {
131  args.swap(_args);
132  }
133 
134  fn_call(as_object* this_in, const as_environment& env_in)
135  :
136  this_ptr(this_in),
137  super(0),
138  nargs(0),
139  callerDef(0),
140  _env(env_in),
141  _new(false)
142  {
143  }
144 
146  fn_call(const fn_call& fn)
147  :
148  this_ptr(fn.this_ptr),
149  super(fn.super),
150  nargs(fn.nargs),
151  callerDef(fn.callerDef),
152  _env(fn._env),
153  _args(fn._args),
154  _new(false)
155  {
156  }
157 
161 
163  //
166 
169 
172 
174  VM& getVM() const {
175  return _env.getVM();
176  }
177 
179  bool isInstantiation() const {
180  return _new;
181  }
182 
184  const Args::value_type& arg(unsigned int n) const {
185  assert(n < nargs);
186  return _args[n];
187  }
188 
189  const Args::container_type& getArgs() const {
190  return _args;
191  }
192 
193  void drop_bottom() {
194  assert(!_args.empty());
195  _args.erase(_args.begin());
196  --nargs;
197  }
198 
199  const as_environment& env() const {
200  return _env;
201  }
202 
204  void dump_args(std::ostream& os) const {
205  for (size_t i = 0; i < nargs; ++i) {
206  if (i) os << ", ";
207  os << arg(i);
208  }
209  }
210 
211  void resetArgs() {
212  nargs = 0;
213  _args.clear();
214  }
215 
216  void pushArg(const Args::value_type& arg) {
217  ++nargs;
218  _args.push_back(arg);
219  }
220 
221 private:
222 
225  const as_environment& _env;
226 
228  Args::container_type _args;
229 
230  bool _new;
231 
232 };
233 
234 
236 //
238 template<typename T>
240 {
241  typedef T value_type;
242  value_type* operator()(const as_object* o) const {
243  return dynamic_cast<value_type*>(o->relay());
244  }
245 };
246 
248 //
250 template<typename T = DisplayObject>
252 {
253  typedef T value_type;
254  value_type* operator()(const as_object* o) const {
255  if (!o) return 0;
256  return dynamic_cast<T*>(o->displayObject());
257  }
258 };
259 
261 struct ValidThis
262 {
265  return o;
266  }
267 };
268 
270 //
274 //
279 //
287 template<typename T>
288 typename T::value_type*
289 ensure(const fn_call& fn)
290 {
291  as_object* obj = fn.this_ptr;
292  if (!obj) throw ActionTypeError();
293 
294  typename T::value_type* ret = T()(obj);
295 
296  if (!ret) {
297  std::string target = typeName(ret);
298  std::string source = typeName(obj);
299 
300  std::string msg = "Function requiring " + target + " as 'this' "
301  "called from " + source + " instance.";
302 
303  throw ActionTypeError(msg);
304  }
305  return ret;
306 }
307 
308 inline string_table&
310 {
311  return fn.getVM().getStringTable();
312 }
313 
314 inline movie_root&
315 getRoot(const fn_call& fn)
316 {
317  return fn.getVM().getRoot();
318 }
319 
320 inline int
322 {
323  return fn.getVM().getSWFVersion();
324 }
325 
326 inline VM&
327 getVM(const fn_call& fn)
328 {
329  return fn.getVM();
330 }
331 
332 inline Global_as&
333 getGlobal(const fn_call& fn)
334 {
335  return *fn.getVM().getGlobal();
336 }
337 
338 } // namespace gnash
339 
340 
341 #endif
342 
343 
344 // Local Variables:
345 // mode: C++
346 // indent-tabs-mode: nil
347 // End:
movie_root & getRoot() const
Get a pointer to this VM's Root movie (stage)
Definition: VM.cpp:143
An ActionScript type error.
Definition: GnashException.h:160
fn_call(as_object *this_in, const as_environment &env_in)
Definition: fn_call.h:134
Check that the 'this' pointer is a DisplayObject.
Definition: fn_call.h:251
Client program's interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
void for_each(C &container, R(T::*pmf)(const A &), const A &arg)
Definition: Renderer_ogl.cpp:692
Definition: klash_part.cpp:329
value_type * operator()(const as_object *o) const
Definition: fn_call.h:242
DisplayObject * displayObject() const
Return the DisplayObject associated with this object.
Definition: as_object.h:638
as_object * this_ptr
Definition: fn_call.h:160
Check that the 'this' pointer is not null.
Definition: fn_call.h:261
as_object value_type
Definition: fn_call.h:263
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
FunctionArgs & operator+=(const T &t)
Definition: fn_call.h:72
size_type size() const
Definition: fn_call.h:95
Check that the 'this' pointer has a particular native type ('Relay').
Definition: fn_call.h:239
bool isInstantiation() const
Return true if this call is an object instantiation.
Definition: fn_call.h:179
void drop_bottom()
Definition: fn_call.h:193
const movie_definition * callerDef
Definition containing caller code. 0 if spontaneous (system event).
Definition: fn_call.h:171
void resetArgs()
Definition: fn_call.h:211
int getSWFVersion() const
Get SWF version context for the currently running actions.
Definition: VM.h:106
The base class for all ActionScript objects.
Definition: as_object.h:161
Definition: GnashKey.h:161
const Args::value_type & arg(unsigned int n) const
Access a particular argument.
Definition: fn_call.h:184
T value_type
Definition: fn_call.h:253
Definition: GnashKey.h:160
Provides information about timeline context.
Definition: as_environment.h:50
Definition: GnashKey.h:166
as_object * super
The "super" object in this function call context.
Definition: fn_call.h:165
void dump_args(std::ostream &os) const
Dump arguments to given output stream.
Definition: fn_call.h:204
VM & getVM(const as_environment &env)
Definition: as_environment.h:222
A class to contain transferable arguments for a fn_call.
Definition: as_function.h:30
FunctionArgs()
Definition: fn_call.h:64
fn_call(as_object *this_in, const as_environment &env_in, Args &args, as_object *sup=0, bool isNew=false)
Construct a fn_call.
Definition: fn_call.h:121
void pushArg(const Args::value_type &arg)
Definition: fn_call.h:216
value_type * operator()(as_object *o) const
Definition: fn_call.h:264
int getSWFVersion(const as_environment &env)
Definition: as_environment.cpp:657
VM & getVM() const
Definition: as_environment.h:59
FunctionArgs< as_value > Args
Definition: fn_call.h:111
void setReachable() const
Mark any reachable resources.
Definition: fn_call.h:86
string_table & getStringTable(const as_environment &env)
Definition: as_environment.cpp:639
VM & getVM() const
Return the VM this fn_call is running from.
Definition: fn_call.h:174
The AVM1 virtual machine.
Definition: VM.h:71
Definition: GnashKey.h:132
std::vector< T >::size_type size_type
Definition: fn_call.h:60
void swap(std::vector< T > &to)
Definition: fn_call.h:91
const Args::container_type & getArgs() const
Definition: fn_call.h:189
Definition: GnashKey.h:155
fn_call(const fn_call &fn)
Copy constructor.
Definition: fn_call.h:146
string_table & getStringTable() const
Get a reference to the string table used by the VM.
Definition: VM.h:117
Relay * relay() const
Access the as_object's Relay object.
Definition: as_object.h:620
const as_environment & env() const
Definition: fn_call.h:199
FunctionArgs(const FunctionArgs &other)
The copy constructor copies all the arguments.
Definition: fn_call.h:67
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:107
std::vector< T > container_type
Definition: fn_call.h:61
T value_type
Definition: fn_call.h:62
FunctionArgs & operator,(const T &t)
Definition: fn_call.h:77
Global_as * getGlobal() const
Get a pointer to this VM's _global Object.
Definition: VM.cpp:149
void setReachable() const
Set any object value as reachable (for the GC)
Definition: as_value.cpp:692
T::value_type * ensure(const fn_call &fn)
Templated function to check the validity of a function call.
Definition: fn_call.h:289
value_type * operator()(const as_object *o) const
Definition: fn_call.h:254
std::string typeName(const T &inst)
Definition: utility.h:93
T value_type
Definition: fn_call.h:241
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
Args::size_type nargs
Number of arguments to this ActionScript function call.
Definition: fn_call.h:168