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 
64  FunctionArgs() = default;
65  FunctionArgs(FunctionArgs&& other) = default;
66 
68  FunctionArgs(const FunctionArgs& other) = default;
69 
71  _v.push_back(std::move(t));
72  return *this;
73  }
74 
76  _v.push_back(std::move(t));
77  return *this;
78  }
79 
80  template <typename U>
82  _v.emplace_back(std::forward<U>(u));
83  return *this;
84  }
85 
86  template <typename U>
88  _v.emplace_back(std::forward<U>(u));
89  return *this;
90  }
91 
93  //
96  void setReachable() const {
97  std::for_each(_v.begin(), _v.end(),
98  std::mem_fun_ref(&as_value::setReachable));
99  }
100 
101  void swap(std::vector<T>& to) {
102  std::swap(_v, to);
103  }
104 
105  size_type size() const {
106  return _v.size();
107  }
108 
109 private:
110  std::vector<T> _v;
111 };
112 
113 
117 class fn_call
118 {
119 public:
120 
122 
124  //
131  fn_call(as_object* this_in, const as_environment& env_in,
132  Args& args, as_object* sup = nullptr, bool isNew = false)
133  :
134  this_ptr(this_in),
135  super(sup),
136  nargs(args.size()),
137  callerDef(nullptr),
138  _env(env_in),
139  _new(isNew)
140  {
141  args.swap(_args);
142  }
143 
144  fn_call(as_object* this_in, const as_environment& env_in)
145  :
146  this_ptr(this_in),
147  super(nullptr),
148  nargs(0),
149  callerDef(nullptr),
150  _env(env_in),
151  _new(false)
152  {
153  }
154 
156  fn_call(const fn_call& fn)
157  :
158  this_ptr(fn.this_ptr),
159  super(fn.super),
160  nargs(fn.nargs),
161  callerDef(fn.callerDef),
162  _env(fn._env),
163  _args(fn._args),
164  _new(false)
165  {
166  }
167 
171 
173  //
176 
179 
182 
184  VM& getVM() const {
185  return _env.getVM();
186  }
187 
189  bool isInstantiation() const {
190  return _new;
191  }
192 
194  const Args::value_type& arg(unsigned int n) const {
195  assert(n < nargs);
196  return _args[n];
197  }
198 
199  const Args::container_type& getArgs() const {
200  return _args;
201  }
202 
203  void drop_bottom() {
204  assert(!_args.empty());
205  _args.erase(_args.begin());
206  --nargs;
207  }
208 
209  const as_environment& env() const {
210  return _env;
211  }
212 
214  void dump_args(std::ostream& os) const {
215  for (size_t i = 0; i < nargs; ++i) {
216  if (i) os << ", ";
217  os << arg(i);
218  }
219  }
220 
221  void resetArgs() {
222  nargs = 0;
223  _args.clear();
224  }
225 
226  void pushArg(const Args::value_type& arg) {
227  ++nargs;
228  _args.push_back(arg);
229  }
230 
231 private:
232 
235  const as_environment& _env;
236 
238  Args::container_type _args;
239 
240  bool _new;
241 
242 };
243 
244 
246 //
248 template<typename T>
250 {
251  typedef T value_type;
252  value_type* operator()(const as_object* o) const {
253  return dynamic_cast<value_type*>(o->relay());
254  }
255 };
256 
258 //
260 template<typename T = DisplayObject>
262 {
263  typedef T value_type;
264  value_type* operator()(const as_object* o) const {
265  if (!o) return nullptr;
266  return dynamic_cast<T*>(o->displayObject());
267  }
268 };
269 
271 struct ValidThis
272 {
274  value_type* operator()(as_object* o) const {
275  return o;
276  }
277 };
278 
280 //
284 //
289 //
297 template<typename T>
298 typename T::value_type*
299 ensure(const fn_call& fn)
300 {
301  as_object* obj = fn.this_ptr;
302  if (!obj) throw ActionTypeError();
303 
304  typename T::value_type* ret = T()(obj);
305 
306  if (!ret) {
307  std::string target = typeName(ret);
308  std::string source = typeName(obj);
309 
310  std::string msg = "Function requiring " + target + " as 'this' "
311  "called from " + source + " instance.";
312 
313  throw ActionTypeError(msg);
314  }
315  return ret;
316 }
317 
318 inline string_table&
320 {
321  return fn.getVM().getStringTable();
322 }
323 
324 inline movie_root&
325 getRoot(const fn_call& fn)
326 {
327  return fn.getVM().getRoot();
328 }
329 
330 inline int
332 {
333  return fn.getVM().getSWFVersion();
334 }
335 
336 inline VM&
337 getVM(const fn_call& fn)
338 {
339  return fn.getVM();
340 }
341 
342 inline Global_as&
343 getGlobal(const fn_call& fn)
344 {
345  return *fn.getVM().getGlobal();
346 }
347 
348 } // namespace gnash
349 
350 
351 #endif
352 
353 
354 // Local Variables:
355 // mode: C++
356 // indent-tabs-mode: nil
357 // End:
An ActionScript type error.
Definition: GnashException.h:160
fn_call(as_object *this_in, const as_environment &env_in)
Definition: fn_call.h:144
Check that the &#39;this&#39; pointer is a DisplayObject.
Definition: fn_call.h:261
FunctionArgs & operator,(U &&u)
Definition: fn_call.h:81
Global_as * getGlobal() const
Get a pointer to this VM&#39;s _global Object.
Definition: VM.cpp:149
Client program&#39;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:690
Definition: klash_part.cpp:329
as_object * this_ptr
Definition: fn_call.h:170
movie_root & getRoot() const
Get a pointer to this VM&#39;s Root movie (stage)
Definition: VM.cpp:143
const Args::value_type & arg(unsigned int n) const
Access a particular argument.
Definition: fn_call.h:194
Check that the &#39;this&#39; pointer is not null.
Definition: fn_call.h:271
string_table & getStringTable() const
Get a reference to the string table used by the VM.
Definition: VM.h:117
as_object value_type
Definition: fn_call.h:273
const Args::container_type & getArgs() const
Definition: fn_call.h:199
Definition: GnashKey.h:167
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
const as_environment & _env
Definition: Array_as.cpp:682
Check that the &#39;this&#39; pointer has a particular native type (&#39;Relay&#39;).
Definition: fn_call.h:249
void setReachable() const
Mark any reachable resources.
Definition: fn_call.h:96
size_type size() const
Definition: fn_call.h:105
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
void drop_bottom()
Definition: fn_call.h:203
const movie_definition * callerDef
Definition containing caller code. 0 if spontaneous (system event).
Definition: fn_call.h:181
FunctionArgs & operator+=(T t)
Definition: fn_call.h:70
void resetArgs()
Definition: fn_call.h:221
The base class for all ActionScript objects.
Definition: as_object.h:161
Definition: GnashKey.h:161
DisplayObject * displayObject() const
Return the DisplayObject associated with this object.
Definition: as_object.h:638
A general use string table.
Definition: string_table.h:41
T value_type
Definition: fn_call.h:263
Definition: GnashKey.h:160
Provides information about timeline context.
Definition: as_environment.h:50
Definition: GnashKey.h:166
FunctionArgs & operator+=(U &&u)
Definition: fn_call.h:87
FunctionArgs()=default
as_object * super
The "super" object in this function call context.
Definition: fn_call.h:175
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
bool isInstantiation() const
Return true if this call is an object instantiation.
Definition: fn_call.h:189
Relay * relay() const
Access the as_object&#39;s Relay object.
Definition: as_object.h:620
FunctionArgs & operator,(T t)
Definition: fn_call.h:75
void pushArg(const Args::value_type &arg)
Definition: fn_call.h:226
int getSWFVersion(const as_environment &env)
Definition: as_environment.cpp:657
FunctionArgs< as_value > Args
Definition: fn_call.h:121
Definition: GnashKey.h:133
value_type * operator()(as_object *o) const
Definition: fn_call.h:274
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
fn_call(as_object *this_in, const as_environment &env_in, Args &args, as_object *sup=nullptr, bool isNew=false)
Construct a fn_call.
Definition: fn_call.h:131
string_table & getStringTable(const as_environment &env)
Definition: as_environment.cpp:639
int getSWFVersion() const
Get SWF version context for the currently running actions.
Definition: VM.h:106
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:101
void dump_args(std::ostream &os) const
Dump arguments to given output stream.
Definition: fn_call.h:214
Definition: GnashKey.h:155
fn_call(const fn_call &fn)
Copy constructor.
Definition: fn_call.h:156
const as_environment & env() const
Definition: fn_call.h:209
value_type * operator()(const as_object *o) const
Definition: fn_call.h:252
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:117
std::vector< T > container_type
Definition: fn_call.h:61
T value_type
Definition: fn_call.h:62
void setReachable() const
Set any object value as reachable (for the GC)
Definition: as_value.cpp:691
T::value_type * ensure(const fn_call &fn)
Templated function to check the validity of a function call.
Definition: fn_call.h:299
value_type * operator()(const as_object *o) const
Definition: fn_call.h:264
std::string typeName(const T &inst)
Definition: utility.h:93
T value_type
Definition: fn_call.h:251
VM & getVM() const
Return the VM this fn_call is running from.
Definition: fn_call.h:184
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
This class represents the &#39;Stage&#39; and top-level movie.
Definition: movie_root.h:150
Args::size_type nargs
Number of arguments to this ActionScript function call.
Definition: fn_call.h:178