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 <memory>
25 
26 #include "as_object.h"
27 #include "fn_call.h"
28 #include "log.h"
29 #include "ClassHierarchy.h"
30 #include "dsodefs.h" // for DSOTEXPORT
31 
32 // Forward declarations
33 namespace gnash {
34  class as_value;
35  class VM;
36  class Extension;
37 }
38 
39 namespace gnash {
40 
42 //
46 //
49 class Global_as : public as_object
50 {
51 public:
52 
53  typedef as_value(*ASFunction)(const fn_call& fn);
54  typedef void(*Properties)(as_object&);
55 
56  explicit Global_as(VM& vm);
57  virtual ~Global_as();
58 
59  void registerClasses();
60 
62 
63  VM& getVM() const {
64  return vm();
65  }
66 
69 
71  //
75  as_object* prototype);
76 
77  void makeObject(as_object& o) const;
78 
79 protected:
80 
81  virtual void markReachableResources() const;
82 
83 private:
84 
85  void loadExtensions();
86  std::unique_ptr<Extension> _et;
87 
88  ClassHierarchy _classes;
89 
90  as_object* _objectProto;
91 
92 };
93 
95 
96 
98 //
100 //
103 //
106 //
113 inline as_object*
115  const ObjectURI& uri)
116 {
117  Global_as& gl = getGlobal(where);
118  as_object* obj = createObject(gl);
119  if (p) p(*obj);
120 
121  where.init_member(uri, obj, as_object::DefaultFlags);
122 
123  return obj;
124 }
125 
127 //
129 //
133 //
143 inline as_object*
146 {
147  Global_as& gl = getGlobal(where);
148  as_object* proto = createObject(gl);
149  as_object* cl = gl.createClass(ctor, proto);
150 
151  // Attach class properties to class
152  if (c) c(*cl);
153 
154  // Attach prototype properties to prototype
155  if (p) p(*proto);
156 
157  // Register class with specified object.
158  where.init_member(uri, cl, as_object::DefaultFlags);
159  return cl;
160 }
161 
163 //
165 inline DSOEXPORT as_value
166 invoke(const as_value& method, const as_environment& env, as_object* this_ptr,
167  fn_call::Args& args, as_object* super = nullptr,
168  const movie_definition* callerDef = nullptr)
169 {
170 
171  as_value val;
172  fn_call call(this_ptr, env, args);
173  call.super = super;
174  call.callerDef = callerDef;
175 
176  try {
177  if (as_object* func = toObject(method, getVM(env))) {
178  // Call function.
179  val = func->call(call);
180  }
181  else {
183  log_aserror("Attempt to call a value which is not "
184  "a function (%s)", method);
185  );
186  return val;
187  }
188  }
189  catch (ActionTypeError& e) {
190  assert(val.is_undefined());
192  log_aserror("%s", e.what());
193  );
194  }
195  return val;
196 }
197 
199 //
202 //
205 //
208 //
217 
218 inline as_value
220 {
221  if (!obj) return as_value();
222  as_value func;
223  if (!obj->get_member(uri, &func)) return as_value();
224 
225  return invoke(func, as_environment(getVM(*obj)), obj, args);
226 }
227 
228 template <typename Param, typename... Params>
229 inline as_value
230 callMethod(fn_call::Args& args, as_object* obj, const ObjectURI& uri, Param param, Params... params)
231 {
232  args += param;
233  return callMethod(args, obj, uri, params...);
234 }
235 
236 
237 template <typename... Params>
238 inline as_value
239 callMethod(as_object* obj, const ObjectURI& uri, Params... params)
240 {
241  fn_call::Args args;
242  return callMethod(args, obj, uri, params...);
243 }
244 
246 //
248 inline as_function*
249 getClassConstructor(const fn_call& fn, const std::string& s)
250 {
251  const as_value ctor(findObject(fn.env(), s));
252  return ctor.to_function();
253 }
254 
255 inline as_value
257 {
258  return as_value();
259 }
260 
261 } // namespace gnash
262 
263 #endif
void makeObject(as_object &o) const
Definition: Global_as.cpp:331
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
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
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
virtual as_value call(const fn_call &fn)
Function dispatch.
Definition: as_object.cpp:301
as_function * getClassConstructor(const fn_call &fn, const std::string &s)
Convenience function for finding a class constructor.
Definition: Global_as.h:249
as_value emptyFunction(const fn_call &)
Definition: Global_as.h:256
ActionScript value type.
Definition: as_value.h:94
DSOEXPORT as_value invoke(const as_value &method, const as_environment &env, as_object *this_ptr, fn_call::Args &args, as_object *super=nullptr, const movie_definition *callerDef=nullptr)
Call an as_value on an as_object.
Definition: Global_as.h:166
as_function * to_function() const
Return the value as a function only if it is a function.
Definition: as_value.cpp:499
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
uri
Definition: test.py:12
void(* Properties)(as_object &)
Definition: Global_as.h:54
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:181
as_object * createArray()
Construct an Array.
Definition: Global_as.cpp:207
virtual void markReachableResources() const
Mark all reachable resources, override from GcResource.
Definition: Global_as.cpp:227
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:159
Definition: GnashKey.h:161
VM & vm() const
Return a reference to this as_object&#39;s global object.
Definition: as_object.h:205
Register all of the ActionScript classes, with their dependencies.
Definition: ClassHierarchy.h:40
A URI for describing as_objects.
Definition: ObjectURI.h:44
VM & getVM() const
Definition: Global_as.h:63
Provides information about timeline context.
Definition: as_environment.h:50
as_object * super
The "super" object in this function call context.
Definition: fn_call.h:175
static const int DefaultFlags
The most common flags for built-in properties.
Definition: as_object.h:192
void log_aserror(StringType msg, Args... args)
Definition: log.h:331
as_object * createClass(Global_as::ASFunction ctor, as_object *prototype)
Create an ActionScript class.
Definition: Global_as.cpp:180
A class to contain transferable arguments for a fn_call.
Definition: as_function.h:30
#define IF_VERBOSE_ASCODING_ERRORS(x)
Definition: log.h:397
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:144
#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:114
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
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:669
as_value callMethod(fn_call::Args &args, as_object *obj, const ObjectURI &uri)
Call a member function of this object in an AS-compatible way.
Definition: Global_as.h:219
Global_as(VM &vm)
Definition: Global_as.cpp:143
Definition: GnashKey.h:162
Definition: GnashKey.h:151
bool is_undefined() const
Definition: as_value.h:336
#define DSOTEXPORT
Definition: dsodefs.h:63
const as_environment & env() const
Definition: fn_call.h:209
virtual bool get_member(const ObjectURI &uri, as_value *val)
Get a property by name if it exists.
Definition: as_object.cpp:378
as_object * createObject(const Global_as &gl)
Definition: Global_as.cpp:303
as_value(* ASFunction)(const fn_call &fn)
Definition: Global_as.h:53
void registerClasses()
Definition: Global_as.cpp:235
Definition: GnashKey.h:165
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:117
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62
virtual ~Global_as()
Definition: Global_as.cpp:154