• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

Global_as.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 //
00019 
00020 #ifndef GNASH_GLOBAL_H
00021 #define GNASH_GLOBAL_H
00022 
00023 #include "as_object.h" // for inheritance
00024 #include "fn_call.h"
00025 #include "log.h"
00026 
00027 #include <string>
00028 #include <boost/preprocessor/arithmetic/inc.hpp>
00029 #include <boost/preprocessor/repetition/enum_params.hpp>
00030 #include <boost/preprocessor/repetition/repeat.hpp>
00031 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
00032 #include <boost/preprocessor/seq/for_each.hpp>
00033 
00034 // Forward declarations
00035 namespace gnash {
00036         class builtin_function;
00037         class as_value;
00038         class VM;
00039         class ClassHierarchy;
00040 }
00041 
00042 namespace gnash {
00043 
00045 //
00049 //
00052 class Global_as : public as_object
00053 {
00054 public:
00055 
00056     typedef as_value(*ASFunction)(const fn_call& fn);
00057     typedef void(*Properties)(as_object&);
00058 
00059     virtual const ClassHierarchy& classHierarchy() const = 0;
00060     virtual ClassHierarchy& classHierarchy() = 0;
00061 
00062     explicit Global_as(VM& vm)
00063         :
00064         as_object(vm)
00065     {}
00066 
00068     virtual builtin_function* createFunction(ASFunction function) = 0;
00069 
00071     //
00075     virtual as_object* createClass(ASFunction ctor, as_object* prototype) = 0;
00076 
00078     //
00082     virtual as_object* createString(const std::string& s) = 0;
00083 
00085     //
00089     virtual as_object* createNumber(double d) = 0;
00090 
00092     //
00096     virtual as_object* createBoolean(bool b) = 0;
00097 
00099     //
00101     virtual as_object* createArray() = 0;
00102 
00104     //
00110     //
00113     virtual as_object* createObject() = 0;
00114 
00115     virtual Global_as& global() {
00116         return *this;
00117     }
00118 
00119     virtual VM& getVM() const = 0;
00120 };
00121 
00122 
00124 //
00126 //
00129 //
00132 //
00139 inline as_object*
00140 registerBuiltinObject(as_object& where, Global_as::Properties p,
00141         const ObjectURI& uri)
00142 {
00143 
00144     // This is going to be the global Mouse "class"/"function"
00145     Global_as& gl = getGlobal(where);
00146     as_object* obj = gl.createObject();
00147     if (p) p(*obj);
00148     
00149     where.init_member(uri, obj, as_object::DefaultFlags);
00150 
00151     return obj;
00152 }
00153 
00155 //
00157 //
00161 //
00171 inline as_object*
00172 registerBuiltinClass(as_object& where, Global_as::ASFunction ctor,
00173         Global_as::Properties p, Global_as::Properties c, const ObjectURI& uri)
00174 {
00175     Global_as& gl = getGlobal(where);
00176     as_object* proto = gl.createObject();
00177     as_object* cl = gl.createClass(ctor, proto);
00178  
00179     // Attach class properties to class
00180     if (c) c(*cl);
00181 
00182     // Attach prototype properties to prototype
00183     if (p) p(*proto);
00184 
00185     // Register class with specified object.
00186     where.init_member(uri, cl, as_object::DefaultFlags);
00187     return cl;
00188 }
00189 
00191 //
00193 inline DSOEXPORT as_value
00194 invoke(const as_value& method, const as_environment& env, as_object* this_ptr,
00195         fn_call::Args& args, as_object* super = 0,
00196         const movie_definition* callerDef = 0)
00197 {
00198 
00199         as_value val;
00200         fn_call call(this_ptr, env, args);
00201         call.super = super;
00202     call.callerDef = callerDef;
00203 
00204         try {
00205                 if (as_object* func = method.to_object(getGlobal(env))) {
00206             // Call function.
00207                     val = func->call(call);
00208                 }
00209                 else {
00210             IF_VERBOSE_ASCODING_ERRORS(
00211                 log_aserror("Attempt to call a value which is not "
00212                     "a function (%s)", method);
00213             );
00214             return val;
00215                 }
00216         }
00217         catch (ActionTypeError& e) {
00218                 assert(val.is_undefined());
00219                 IF_VERBOSE_ASCODING_ERRORS(
00220             log_aserror("%s", e.what());
00221                 );
00222         }
00223         return val;
00224 }
00225 
00227 #define VALUE_ARG(z, n, t) BOOST_PP_COMMA_IF(n) t arg##n
00228 
00230 //
00233 //
00236 //
00239 //
00248 #define CALL_METHOD(x, n, t) \
00249 inline as_value \
00250 callMethod(as_object* obj, string_table::key key BOOST_PP_COMMA_IF(n)\
00251         BOOST_PP_REPEAT(n, VALUE_ARG, const as_value&)) {\
00252     if (!obj) return as_value();\
00253     as_value func;\
00254     if (!obj->get_member(key, &func)) return as_value();\
00255     fn_call::Args args;\
00256     BOOST_PP_EXPR_IF(n, (args += BOOST_PP_REPEAT(n, VALUE_ARG, ));)\
00257     return invoke(func, as_environment(getVM(*obj)), obj, args);\
00258 }
00259 
00261 #define MAX_ARGS 4
00262 BOOST_PP_REPEAT(BOOST_PP_INC(MAX_ARGS), CALL_METHOD, )
00263 
00264 
00265 //
00267 inline as_function*
00268 getClassConstructor(const fn_call& fn, const std::string& s)
00269 {
00270     const as_value ctor(fn.env().find_object(s));
00271     return ctor.to_function();
00272 }
00273 
00274 } // namespace gnash
00275 
00276 #endif 

Generated on Thu Sep 30 2010 14:34:58 for Gnash by  doxygen 1.7.1