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 
37 // Forward declarations
38 namespace gnash {
39  class as_value;
40  class VM;
41  class Extension;
42 }
43 
44 namespace gnash {
45 
47 //
51 //
54 class Global_as : public as_object
55 {
56 public:
57 
58  typedef as_value(*ASFunction)(const fn_call& fn);
59  typedef void(*Properties)(as_object&);
60 
61  explicit Global_as(VM& vm);
62  virtual ~Global_as();
63 
64  void registerClasses();
65 
67 
68  VM& getVM() const {
69  return vm();
70  }
71 
74 
76  //
80  as_object* prototype);
81 
82  void makeObject(as_object& o) const;
83 
84 protected:
85 
86  virtual void markReachableResources() const;
87 
88 private:
89 
90  void loadExtensions();
91  boost::scoped_ptr<Extension> _et;
92 
93  ClassHierarchy _classes;
94 
95  as_object* _objectProto;
96 
97 };
98 
99 as_object* createObject(const Global_as& gl);
100 
101 
103 //
105 //
108 //
111 //
118 inline as_object*
120  const ObjectURI& uri)
121 {
122  Global_as& gl = getGlobal(where);
123  as_object* obj = createObject(gl);
124  if (p) p(*obj);
125 
126  where.init_member(uri, obj, as_object::DefaultFlags);
127 
128  return obj;
129 }
130 
132 //
134 //
138 //
148 inline as_object*
151 {
152  Global_as& gl = getGlobal(where);
153  as_object* proto = createObject(gl);
154  as_object* cl = gl.createClass(ctor, proto);
155 
156  // Attach class properties to class
157  if (c) c(*cl);
158 
159  // Attach prototype properties to prototype
160  if (p) p(*proto);
161 
162  // Register class with specified object.
163  where.init_member(uri, cl, as_object::DefaultFlags);
164  return cl;
165 }
166 
168 //
170 inline DSOEXPORT as_value
171 invoke(const as_value& method, const as_environment& env, as_object* this_ptr,
172  fn_call::Args& args, as_object* super = 0,
173  const movie_definition* callerDef = 0)
174 {
175 
176  as_value val;
177  fn_call call(this_ptr, env, args);
178  call.super = super;
179  call.callerDef = callerDef;
180 
181  try {
182  if (as_object* func = toObject(method, getVM(env))) {
183  // Call function.
184  val = func->call(call);
185  }
186  else {
188  log_aserror("Attempt to call a value which is not "
189  "a function (%s)", method);
190  );
191  return val;
192  }
193  }
194  catch (ActionTypeError& e) {
195  assert(val.is_undefined());
197  log_aserror("%s", e.what());
198  );
199  }
200  return val;
201 }
202 
204 #define FUNC_PARAM(z, n, t) BOOST_PP_COMMA_IF(n) t arg##n
205 #define VALUE_ARG(z, n, t) BOOST_PP_COMMA_IF(n) arg##n
206 
208 //
211 //
214 //
217 //
226 #define CALL_METHOD(x, n, t) \
227 inline as_value \
228 callMethod(as_object* obj, const ObjectURI& uri BOOST_PP_COMMA_IF(n)\
229  BOOST_PP_REPEAT(n, FUNC_PARAM, const as_value&)) {\
230  if (!obj) return as_value();\
231  as_value func;\
232  if (!obj->get_member(uri, &func)) return as_value();\
233  fn_call::Args args;\
234  BOOST_PP_EXPR_IF(n, (args += BOOST_PP_REPEAT(n, VALUE_ARG, BOOST_PP_EMPTY));)\
235  return invoke(func, as_environment(getVM(*obj)), obj, args);\
236 }
237 
239 #define MAX_ARGS 4
240 BOOST_PP_REPEAT(BOOST_PP_INC(MAX_ARGS), CALL_METHOD, BOOST_PP_EMPTY)
241 
242 #undef VALUE_ARG
243 #undef FUNC_PARAM
244 #undef MAX_ARGS
245 #undef CALL_METHOD
246 
248 //
250 inline as_function*
251 getClassConstructor(const fn_call& fn, const std::string& s)
252 {
253  const as_value ctor(findObject(fn.env(), s));
254  return ctor.to_function();
255 }
256 
257 inline as_value
259 {
260  return as_value();
261 }
262 
263 } // namespace gnash
264 
265 #endif