Gnash 0.8.9

as_object.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
00003 //   2011 Free Software 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 #ifndef GNASH_AS_OBJECT_H
00020 #define GNASH_AS_OBJECT_H
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "gnashconfig.h"
00024 #endif
00025 
00026 #include <map>
00027 #include <vector>
00028 #include <cmath>
00029 #include <utility> 
00030 #include <set>
00031 #include <sstream>
00032 #include <boost/scoped_ptr.hpp>
00033 #include <boost/noncopyable.hpp>
00034 
00035 #include "string_table.h"
00036 #include "GC.h" // for inheritance from GcResource (to complete)
00037 #include "PropertyList.h"
00038 #include "PropFlags.h"
00039 #include "Relay.h"
00040 #include "ObjectURI.h"
00041 
00042 // Forward declarations
00043 namespace gnash {
00044     class as_function;
00045     class MovieClip;
00046     class DisplayObject;
00047     class as_environment;
00048     class VM;
00049     class IOChannel;
00050     class movie_root;
00051     class RunResources;
00052     class Global_as;
00053     class as_value;
00054 }
00055 
00056 namespace gnash {
00057 
00058 
00060 class Trigger
00061 {
00062 public:
00063 
00064     Trigger(const std::string& propname, as_function& trig,
00065             const as_value& customArg)
00066         :
00067         _propname(propname),
00068         _func(&trig),
00069         _customArg(customArg),
00070         _executing(false),
00071         _dead(false)
00072     {}
00073 
00075     //
00085     as_value call(const as_value& oldval, const as_value& newval, 
00086             as_object& this_obj);
00087 
00089     bool dead() const { return _dead; }
00090 
00091     void kill() {
00092         _dead = true;
00093     }
00094 
00095     void setReachable() const;
00096 
00097 private:
00098 
00100     //
00106     std::string _propname;
00107 
00109     as_function* _func;
00110 
00113     as_value _customArg;
00114 
00116     bool _executing;
00117 
00119     //
00123     bool _dead;
00124 
00125 };
00126 
00128 //
00133 //
00135 //
00140 //
00143 //
00145 //
00147 //
00153 //
00156 //
00158 //
00162 class as_object : public GcResource, boost::noncopyable
00163 {
00164 
00165 public:
00166     
00168     //
00172     explicit as_object(const Global_as& global);
00173 
00175     virtual ~as_object() {}
00176 
00178     //
00182     virtual as_value call(const fn_call& fn);
00183 
00185     //
00188     virtual std::string stringValue() const;
00189 
00191     //
00193     static const int DefaultFlags = PropFlags::dontDelete |
00194                                     PropFlags::dontEnum;
00195 
00197     //
00203     Property* findProperty(const ObjectURI& uri, as_object** owner = 0);
00204 
00206     VM& vm() const {
00207         return _vm;
00208     }
00209 
00211     //
00215     //
00217     void dump_members();
00218 
00220     //
00229     virtual bool set_member(const ObjectURI& uri, const as_value& val,
00230         bool ifFound = false);
00231 
00233     //
00237     //
00242     void init_member(const std::string& name, const as_value& val, 
00243         int flags = DefaultFlags);
00244 
00246     //
00254     //
00260     void init_member(const ObjectURI& uri, const as_value& val, 
00261         int flags = DefaultFlags);
00262 
00264     //
00284     void init_property(const std::string& key, as_function& getter,
00285         as_function& setter, int flags = DefaultFlags);
00286         
00287 
00289     //
00302     void init_property(const std::string& key, as_c_function_ptr getter,
00303         as_c_function_ptr setter, int flags = DefaultFlags);
00304 
00306     //
00310     //
00318     void init_property(const ObjectURI& uri, as_function& getter,
00319         as_function& setter, int flags = DefaultFlags);
00320 
00322     //
00334     void init_property(const ObjectURI& uri, as_c_function_ptr getter,
00335         as_c_function_ptr setter, int flags = DefaultFlags);
00336 
00338     //
00343     //
00348     bool init_destructive_property(const ObjectURI& uri, as_function& getter,
00349             int flags = PropFlags::dontEnum);
00350 
00352     //
00357     //
00362     bool init_destructive_property(const ObjectURI& uri, 
00363             as_c_function_ptr getter, int flags = PropFlags::dontEnum);
00364 
00366     //
00371     // 
00374     //
00378     void init_readonly_property(const std::string& key, as_function& getter,
00379             int flags = DefaultFlags);
00380 
00382     //
00394     void init_readonly_property(const std::string& key,
00395             as_c_function_ptr getter, int flags = DefaultFlags);
00396 
00398     //
00407     bool watch(const ObjectURI& uri, as_function& trig, const as_value& cust);
00408 
00410     //
00414     bool unwatch(const ObjectURI& uri);
00415 
00417     //
00419     //
00422     //
00428     virtual bool get_member(const ObjectURI& uri, as_value* val);
00429 
00436     virtual as_object* get_super(const ObjectURI& fname);
00437     as_object* get_super();
00438 
00440     //
00442     //
00451     std::pair<bool, bool> delProperty(const ObjectURI& uri);
00452 
00454     //
00456     //
00460     Property* getOwnProperty(const ObjectURI& uri);
00461 
00463     //
00469     void set_member_flags(const ObjectURI& uri, int setTrue, int setFalse = 0);
00470 
00472     virtual as_function* to_function() { return 0; }
00473 
00475     virtual bool isSuper() const { return false; }
00476 
00478     //
00484     //
00487     void addInterface(as_object* ctor);
00488 
00490     //
00494     //
00498     //
00504     bool instanceOf(as_object* ctor);
00505 
00507     //
00509     //
00513     bool prototypeOf(as_object& instance);
00514 
00516     //
00521     //
00524     void setPropFlags(const as_value& props, int set_false, int set_true);
00525 
00527     //
00529     //
00531     void copyProperties(const as_object& o);
00532 
00534     void clearProperties() {
00535         _members.clear();
00536     }
00537 
00539     //
00543     //
00547     template<typename T>
00548     void visitProperties(PropertyVisitor& visitor) const {
00549         _members.visitValues<T>(visitor);
00550     }
00551 
00553     //
00556     //
00561     //
00564     void visitKeys(KeyVisitor& visitor) const;
00565 
00567     //
00574     void add_property(const std::string& key, as_function& getter,
00575         as_function* setter);
00576 
00578     //
00583     as_object* get_prototype() const;
00584 
00586     //
00589     void set_prototype(const as_value& proto);
00590 
00592     //
00596     //
00601     //
00605     void setRelay(Relay* p) {
00606         if (p) _array = false;
00607         if (_relay) _relay->clean();
00608         _relay.reset(p);
00609     }
00610 
00612     //
00617     //
00621     Relay* relay() const {
00622         return _relay.get();
00623     }
00624 
00626     bool array() const {
00627         return _array;
00628     }
00629 
00631     void setArray(bool array = true) {
00632         _array = array;
00633     }
00634 
00636     //
00639     DisplayObject* displayObject() const {
00640         return _displayObject;
00641     }
00642 
00644     void setDisplayObject(DisplayObject* d) {
00645         _displayObject = d;
00646     }
00647 
00648 protected:
00649 
00651     //
00660     explicit as_object(VM& vm);
00661 
00663     //
00669     virtual void markReachableResources() const;
00670 
00671 private:
00672 
00674     //
00676     //
00684     Property* findUpdatableProperty(const ObjectURI& uri);
00685 
00686     void executeTriggers(Property* prop, const ObjectURI& uri,
00687             const as_value& val);
00688 
00690     template<typename T> class PrototypeRecursor;
00691 
00693     //
00696     DisplayObject* _displayObject;
00697 
00699     //
00705     bool _array;
00706 
00708     //
00711     boost::scoped_ptr<Relay> _relay;
00712 
00714     VM& _vm;
00715 
00717     PropertyList _members;
00718 
00720     //
00723     std::vector<as_object*> _interfaces;
00724 
00725     typedef std::map<ObjectURI, Trigger, ObjectURI::LessThan> TriggerContainer;
00726     boost::scoped_ptr<TriggerContainer> _trigs;
00727 };
00728 
00730 //
00734 //
00736 //
00741 void sendEvent(as_object& o, const as_environment& env, const ObjectURI& name);
00742 
00744 //
00747 //
00749 //
00750 //
00756 inline as_value
00757 getMember(as_object& o, const ObjectURI& uri)
00758 {
00759     as_value ret;
00760     o.get_member(uri, &ret);
00761     return ret;
00762 }
00763 
00765 //
00768 //
00771 //
00777 inline as_value
00778 getOwnProperty(as_object& o, const ObjectURI& uri)
00779 {
00780     Property* p = o.getOwnProperty(uri);
00781     return p ? p->getValue(o) : as_value();
00782 }
00783 
00785 class IsVisible
00786 {
00787 public:
00788     IsVisible(int version) : _version(version) {}
00789     bool operator()(const Property& prop) const {
00790         return visible(prop, _version);
00791     }
00792 private:
00793     const int _version;
00794 };
00795 
00796 class Exists
00797 {
00798 public:
00799     Exists() {}
00800     bool operator()(const Property&) const {
00801         return true;
00802     }
00803 };
00804 
00805 class IsEnumerable
00806 {
00807 public:
00808     IsEnumerable() {}
00809     bool operator()(const Property& p) const {
00810         return !p.getFlags().test<PropFlags::dontEnum>();
00811     }
00812 };
00813 
00815 //
00820 //
00824 std::string getURLEncodedVars(as_object& o);
00825 
00827 //
00833 as_object* getPathElement(as_object& o, const ObjectURI& uri);
00834 
00835 
00837 //
00841 template<typename T>
00842 T*
00843 get(as_object* o)
00844 {
00845     if (!o) return 0;
00846     return dynamic_cast<T*>(o->displayObject());
00847 }
00848 
00850 //
00854 inline bool
00855 hasOwnProperty(as_object& o, const ObjectURI& uri)
00856 {
00857     return (o.getOwnProperty(uri));
00858 }
00859 
00860 as_object* getObjectWithPrototype(Global_as& gl, const ObjectURI& c);
00861 
00863 //
00867 //
00874 template<typename T>
00875 bool
00876 isNativeType(const as_object* obj, T*& relay)
00877 {
00878     if (!obj) return false;
00879     relay = dynamic_cast<T*>(obj->relay());
00880     return relay;
00881 }
00882 
00884 //
00888 typedef std::vector<std::pair<ObjectURI, as_value> > SortedPropertyList;
00889     
00891 //
00894 //
00898 //
00901 //
00904 SortedPropertyList enumerateProperties(as_object& o);
00905 
00907 VM& getVM(const as_object& o);
00908 
00910 movie_root& getRoot(const as_object& o);
00911 
00913 string_table& getStringTable(const as_object& o);
00914 
00916 const RunResources& getRunResources(const as_object& o);
00917 
00919 int getSWFVersion(const as_object& o);
00920 
00922 Global_as& getGlobal(const as_object& o);
00923 
00925 inline bool caseless(const as_object& o) {
00926     return getSWFVersion(o) < 7;
00927 }
00928 
00929 } // namespace gnash
00930 
00931 #endif // GNASH_AS_OBJECT_H