Gnash  0.8.11dev
Global_as.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 
20 #ifndef GNASH_GLOBAL_H
21 #define GNASH_GLOBAL_H
22 
23 #include <string>
24 #include <boost/preprocessor/arithmetic/inc.hpp>
25 #include <boost/preprocessor/repetition/enum_params.hpp>
26 #include <boost/preprocessor/repetition/repeat.hpp>
27 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
28 #include <boost/preprocessor/seq/for_each.hpp>
29 #include <boost/preprocessor/facilities/empty.hpp>
30 #include <boost/scoped_ptr.hpp>
31 
32 #include "as_object.h"
33 #include "fn_call.h"
34 #include "log.h"
35 #include "ClassHierarchy.h"
36 #include "dsodefs.h" // for DSOTEXPORT
37 
38 // Forward declarations
39 namespace gnash {
40  class as_value;
41  class VM;
42  class Extension;
43 }
44 
45 namespace gnash {
46 
48 //
52 //
55 class Global_as : public as_object
56 {
57 public:
58 
59  typedef as_value(*ASFunction)(const fn_call& fn);
60  typedef void(*Properties)(as_object&);
61 
62  explicit Global_as(VM& vm);
63  virtual ~Global_as();
64 
65  void registerClasses();
66 
68 
69  VM& getVM() const {
70  return vm();
71  }
72 
75 
77  //
81  as_object* prototype);
82 
83  void makeObject(as_object& o) const;
84 
85 protected:
86 
87  virtual void markReachableResources() const;
88 
89 private:
90 
91  void loadExtensions();
92  boost::scoped_ptr<Extension> _et;
93 
94  ClassHierarchy _classes;
95 
96  as_object* _objectProto;
97 
98 };
99 
100 DSOTEXPORT as_object* createObject(const Global_as& gl);
101 
102 
104 //
106 //
109 //
112 //
119 inline as_object*
121  const ObjectURI& uri)
122 {
123  Global_as& gl = getGlobal(where);
124  as_object* obj = createObject(gl);
125  if (p) p(*obj);
126 
127  where.init_member(uri, obj, as_object::DefaultFlags);
128 
129  return obj;
130 }
131 
133 //
135 //
139 //
149 inline as_object*
152 {
153  Global_as& gl = getGlobal(where);
154  as_object* proto = createObject(gl);
155  as_object* cl = gl.createClass(ctor, proto);
156 
157  // Attach class properties to class
158  if (c) c(*cl);
159 
160  // Attach prototype properties to prototype
161  if (p) p(*proto);
162 
163  // Register class with specified object.
164  where.init_member(uri, cl, as_object::DefaultFlags);
165  return cl;
166 }
167 
169 //
171 inline DSOEXPORT as_value
172 invoke(const as_value& method, const as_environment& env, as_object* this_ptr,
173  fn_call::Args& args, as_object* super = 0,
174  const movie_definition* callerDef = 0)
175 {
176 
177  as_value val;
178  fn_call call(this_ptr, env, args);
179  call.super = super;
180  call.callerDef = callerDef;
181 
182  try {
183  if (as_object* func = toObject(method, getVM(env))) {
184  // Call function.
185  val = func->call(call);
186  }
187  else {
189  log_aserror("Attempt to call a value which is not "
190  "a function (%s)", method);
191  );
192  return val;
193  }
194  }
195  catch (ActionTypeError& e) {
196  assert(val.is_undefined());
198  log_aserror("%s", e.what());
199  );
200  }
201  return val;
202 }
203 
205 #define FUNC_PARAM(z, n, t) BOOST_PP_COMMA_IF(n) t arg##n
206 #define VALUE_ARG(z, n, t) BOOST_PP_COMMA_IF(n) arg##n
207 
209 //
212 //
215 //
218 //
227 #define CALL_METHOD(x, n, t) \
228 inline as_value \
229 callMethod(as_object* obj, const ObjectURI& uri BOOST_PP_COMMA_IF(n)\
230  BOOST_PP_REPEAT(n, FUNC_PARAM, const as_value&)) {\
231  if (!obj) return as_value();\
232  as_value func;\
233  if (!obj->get_member(uri, &func)) return as_value();\
234  fn_call::Args args;\
235  BOOST_PP_EXPR_IF(n, (args += BOOST_PP_REPEAT(n, VALUE_ARG, BOOST_PP_EMPTY));)\
236  return invoke(func, as_environment(getVM(*obj)), obj, args);\
237 }
238 
240 #define MAX_ARGS 4
241 BOOST_PP_REPEAT(BOOST_PP_INC(MAX_ARGS), CALL_METHOD, BOOST_PP_EMPTY)
242 
243 #undef VALUE_ARG
244 #undef FUNC_PARAM
245 #undef MAX_ARGS
246 #undef CALL_METHOD
247 
249 //
251 inline as_function*
252 getClassConstructor(const fn_call& fn, const std::string& s)
253 {
254  const as_value ctor(findObject(fn.env(), s));
255  return ctor.to_function();
256 }
257 
258 inline as_value
260 {
261  return as_value();
262 }
263 
264 } // namespace gnash
265 
266 #endif
An ActionScript type error.
Definition: GnashException.h:160
as_object * toObject(const as_value &v, VM &vm)
Convert an as_value to an object.
Definition: VM.cpp:457
void makeObject(as_object &o) const
Definition: Global_as.cpp:332
Client program's interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
as_function * getClassConstructor(const fn_call &fn, const std::string &s)
Convenience function for finding a class constructor.
Definition: Global_as.h:252
as_value emptyFunction(const fn_call &)
Definition: Global_as.h:259
SWFStream & s
Definition: DefineBitsTag.cpp:73
ActionScript value type.
Definition: as_value.h:95
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
const movie_definition * callerDef
Definition containing caller code. 0 if spontaneous (system event).
Definition: fn_call.h:171
virtual void markReachableResources() const
Mark all reachable resources, override from GcResource.
Definition: Global_as.cpp:228
as_object * createArray()
Construct an Array.
Definition: Global_as.cpp:208
Definition: GnashKey.h:149
The base class for all ActionScript objects.
Definition: as_object.h:161
as_function * createFunction(Global_as::ASFunction function)
Create an ActionScript function.
Definition: Global_as.cpp:160
Definition: GnashKey.h:161
void(* Properties)(as_object &)
Definition: Global_as.h:60
Register all of the ActionScript classes, with their dependencies.
Definition: ClassHierarchy.h:40
A URI for describing as_objects.
Definition: ObjectURI.h:44
as_value(* ASFunction)(const fn_call &fn)
Definition: Global_as.h:59
Provides information about timeline context.
Definition: as_environment.h:50
DSOEXPORT as_value invoke(const as_value &method, const as_environment &env, as_object *this_ptr, fn_call::Args &args, as_object *super=0, const movie_definition *callerDef=0)
Call an as_value on an as_object.
Definition: Global_as.h:172
as_object * super
The "super" object in this function call context.
Definition: fn_call.h:165
VM & getVM(const as_environment &env)
Definition: as_environment.h:222
static const int DefaultFlags
The most common flags for built-in properties.
Definition: as_object.h:192
as_object * createClass(Global_as::ASFunction ctor, as_object *prototype)
Create an ActionScript class.
Definition: Global_as.cpp:181
#define IF_VERBOSE_ASCODING_ERRORS(x)
Definition: log.h:383
VM & getVM() const
Definition: Global_as.h:69
as_object * registerBuiltinClass(as_object &where, Global_as::ASFunction ctor, Global_as::Properties p, Global_as::Properties c, const ObjectURI &uri)
Register a built-in class.
Definition: Global_as.h:150
bool is_undefined() const
Definition: as_value.h:323
VM & vm() const
Return a reference to this as_object's global object.
Definition: as_object.h:205
as_object * findObject(const as_environment &ctx, const std::string &path, const as_environment::ScopeStack *scope)
Find the object referenced by the given path.
Definition: as_environment.cpp:116
#define DSOEXPORT
Definition: dsodefs.h:55
as_object * registerBuiltinObject(as_object &where, Global_as::Properties p, const ObjectURI &uri)
Register a built-in object.
Definition: Global_as.h:120
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:55
A class to contain transferable arguments for a fn_call.
Definition: as_function.h:30
The AVM1 virtual machine.
Definition: VM.h:71
void init_member(const std::string &name, const as_value &val, int flags=DefaultFlags)
Initialize a member value by string.
Definition: as_object.cpp:670
Global_as(VM &vm)
Definition: Global_as.cpp:144
Definition: GnashKey.h:162
Definition: GnashKey.h:151
#define DSOTEXPORT
Definition: dsodefs.h:63
as_object * createObject(const Global_as &gl)
Definition: Global_as.cpp:304
#define CALL_METHOD(x, n, t)
Call a member function of this object in an AS-compatible way.
Definition: Global_as.h:227
void registerClasses()
Definition: Global_as.cpp:236
const as_environment & env() const
Definition: fn_call.h:199
as_function * to_function() const
Return the value as a function only if it is a function.
Definition: as_value.cpp:500
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:107
#define MAX_ARGS
The maximum number of as_value arguments allowed in callMethod functions.
Definition: Global_as.h:240
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62
virtual ~Global_as()
Definition: Global_as.cpp:155