Gnash  0.8.11dev
as_object.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 #ifndef GNASH_AS_OBJECT_H
20 #define GNASH_AS_OBJECT_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include "gnashconfig.h"
24 #endif
25 
26 #include <map>
27 #include <vector>
28 #include <cmath>
29 #include <utility>
30 #include <memory>
31 #include <boost/noncopyable.hpp>
32 
33 #include "GC.h" // for inheritance from GcResource (to complete)
34 #include "PropertyList.h"
35 #include "PropFlags.h"
36 #include "Relay.h"
37 #include "ObjectURI.h"
38 #include "dsodefs.h" // for DSOTEXPORT
39 
40 // Forward declarations
41 namespace gnash {
42  class as_function;
43  class MovieClip;
44  class DisplayObject;
45  class as_environment;
46  class VM;
47  class IOChannel;
48  class movie_root;
49  class RunResources;
50  class Global_as;
51  class as_value;
52  class string_table;
53 }
54 
55 namespace gnash {
56 
57 
59 class Trigger
60 {
61 public:
62 
63  Trigger(std::string propname, as_function& trig,
64  as_value customArg)
65  :
66  _propname(std::move(propname)),
67  _func(&trig),
68  _customArg(std::move(customArg)),
69  _executing(false),
70  _dead(false)
71  {}
72 
74  //
84  as_value call(const as_value& oldval, const as_value& newval,
85  as_object& this_obj);
86 
88  bool dead() const { return _dead; }
89 
90  void kill() {
91  _dead = true;
92  }
93 
94  void setReachable() const;
95 
96 private:
97 
99  //
105  std::string _propname;
106 
108  as_function* _func;
109 
112  as_value _customArg;
113 
115  bool _executing;
116 
118  //
122  bool _dead;
123 
124 };
125 
127 //
132 //
134 //
139 //
142 //
144 //
146 //
152 //
155 //
157 //
161 class DSOEXPORT as_object : public GcResource, boost::noncopyable
162 {
163 
164 public:
165 
167  //
171  explicit DSOTEXPORT as_object(const Global_as& global);
172 
174  virtual ~as_object() {}
175 
177  //
181  virtual as_value call(const fn_call& fn);
182 
184  //
187  virtual std::string stringValue() const;
188 
190  //
192  static const int DefaultFlags = PropFlags::dontDelete |
194 
196  //
202  Property* findProperty(const ObjectURI& uri, as_object** owner = nullptr);
203 
205  VM& vm() const {
206  return _vm;
207  }
208 
210  //
214  //
216  void dump_members();
217 
219  //
228  virtual bool set_member(const ObjectURI& uri, const as_value& val,
229  bool ifFound = false);
230 
232  //
236  //
241  void init_member(const std::string& name, const as_value& val,
242  int flags = DefaultFlags);
243 
245  //
253  //
259  void init_member(const ObjectURI& uri, const as_value& val,
260  int flags = DefaultFlags);
261 
263  //
283  void init_property(const std::string& key, as_function& getter,
284  as_function& setter, int flags = DefaultFlags);
285 
286 
288  //
301  void init_property(const std::string& key, as_c_function_ptr getter,
302  as_c_function_ptr setter, int flags = DefaultFlags);
303 
305  //
309  //
317  void init_property(const ObjectURI& uri, as_function& getter,
318  as_function& setter, int flags = DefaultFlags);
319 
321  //
333  void init_property(const ObjectURI& uri, as_c_function_ptr getter,
334  as_c_function_ptr setter, int flags = DefaultFlags);
335 
337  //
342  //
347  bool init_destructive_property(const ObjectURI& uri, as_function& getter,
348  int flags = PropFlags::dontEnum);
349 
351  //
356  //
361  bool init_destructive_property(const ObjectURI& uri,
362  as_c_function_ptr getter, int flags = PropFlags::dontEnum);
363 
365  //
370  //
373  //
377  void init_readonly_property(const std::string& key, as_function& getter,
378  int flags = DefaultFlags);
379 
381  //
393  void init_readonly_property(const std::string& key,
394  as_c_function_ptr getter, int flags = DefaultFlags);
395 
397  //
406  bool watch(const ObjectURI& uri, as_function& trig, const as_value& cust);
407 
409  //
413  bool unwatch(const ObjectURI& uri);
414 
416  //
418  //
421  //
427  virtual bool get_member(const ObjectURI& uri, as_value* val);
428 
435  virtual as_object* get_super(const ObjectURI& fname);
436  as_object* get_super();
437 
439  //
441  //
450  DSOTEXPORT std::pair<bool, bool> delProperty(const ObjectURI& uri);
451 
453  //
455  //
459  Property* getOwnProperty(const ObjectURI& uri);
460 
462  //
468  void set_member_flags(const ObjectURI& uri, int setTrue, int setFalse = 0);
469 
471  virtual as_function* to_function() { return nullptr; }
472 
474  virtual bool isSuper() const { return false; }
475 
477  //
483  //
486  void addInterface(as_object* ctor);
487 
489  //
493  //
497  //
503  bool instanceOf(as_object* ctor);
504 
506  //
508  //
512  bool prototypeOf(as_object& instance);
513 
515  //
520  //
523  void setPropFlags(const as_value& props, int set_false, int set_true);
524 
526  //
528  //
530  void copyProperties(const as_object& o);
531 
534  _members.clear();
535  }
536 
538  //
542  //
546  template<typename T>
547  void visitProperties(PropertyVisitor& visitor) const {
548  _members.visitValues<T>(visitor);
549  }
550 
552  //
555  //
560  //
563  void visitKeys(KeyVisitor& visitor) const;
564 
566  //
573  void add_property(const std::string& key, as_function& getter,
574  as_function* setter);
575 
577  //
582  as_object* get_prototype() const;
583 
585  //
588  void set_prototype(const as_value& proto);
589 
591  //
595  //
600  //
604  void setRelay(Relay* p) {
605  if (p) _array = false;
606  if (_relay) _relay->clean();
607  _relay.reset(p);
608  }
609 
611  //
616  //
620  Relay* relay() const {
621  return _relay.get();
622  }
623 
625  bool array() const {
626  return _array;
627  }
628 
630  void setArray(bool array = true) {
631  _array = array;
632  }
633 
635  //
639  return _displayObject;
640  }
641 
644  _displayObject = d;
645  }
646 
647 protected:
648 
650  //
659  explicit as_object(VM& vm);
660 
662  //
668  virtual void markReachableResources() const;
669 
670 private:
671 
673  //
675  //
683  Property* findUpdatableProperty(const ObjectURI& uri);
684 
685  void executeTriggers(Property* prop, const ObjectURI& uri,
686  const as_value& val);
687 
689  template<typename T> class PrototypeRecursor;
690 
692  //
695  DisplayObject* _displayObject;
696 
698  //
704  bool _array;
705 
707  //
710  std::unique_ptr<Relay> _relay;
711 
713  VM& _vm;
714 
716  PropertyList _members;
717 
719  //
722  std::vector<as_object*> _interfaces;
723 
724  typedef std::map<ObjectURI, Trigger, ObjectURI::LessThan> TriggerContainer;
725  std::unique_ptr<TriggerContainer> _trigs;
726 };
727 
729 //
733 //
735 //
740 void sendEvent(as_object& o, const as_environment& env, const ObjectURI& name);
741 
743 //
746 //
748 //
749 //
755 inline as_value
757 {
758  as_value ret;
759  o.get_member(uri, &ret);
760  return ret;
761 }
762 
764 //
767 //
770 //
776 inline as_value
778 {
779  Property* p = o.getOwnProperty(uri);
780  return p ? p->getValue(o) : as_value();
781 }
782 
785 {
786 public:
787  IsVisible(int version) : _version(version) {}
788  bool operator()(const Property& prop) const {
789  return visible(prop, _version);
790  }
791 private:
792  const int _version;
793 };
794 
795 class Exists
796 {
797 public:
798  Exists() {}
799  bool operator()(const Property&) const {
800  return true;
801  }
802 };
803 
805 {
806 public:
808  bool operator()(const Property& p) const {
809  return !p.getFlags().test<PropFlags::dontEnum>();
810  }
811 };
812 
814 //
819 //
823 std::string getURLEncodedVars(as_object& o);
824 
826 //
833 
834 
836 //
840 template<typename T>
841 T*
843 {
844  if (!o) return nullptr;
845  return dynamic_cast<T*>(o->displayObject());
846 }
847 
849 //
853 inline bool
855 {
856  return (o.getOwnProperty(uri));
857 }
858 
860 
862 //
866 //
873 template<typename T>
874 bool
875 isNativeType(const as_object* obj, T*& relay)
876 {
877  if (!obj) return false;
878  relay = dynamic_cast<T*>(obj->relay());
879  return relay;
880 }
881 
883 //
887 typedef std::vector<std::pair<ObjectURI, as_value> > SortedPropertyList;
888 
890 //
893 //
897 //
900 //
903 SortedPropertyList enumerateProperties(as_object& o);
904 
906 DSOTEXPORT VM& getVM(const as_object& o);
907 
910 
913 
915 const RunResources& getRunResources(const as_object& o);
916 
918 int getSWFVersion(const as_object& o);
919 
922 
924 inline bool caseless(const as_object& o) {
925  return getSWFVersion(o) < 7;
926 }
927 
928 } // namespace gnash
929 
930 #endif // GNASH_AS_OBJECT_H
bool test() const
Definition: PropFlags.h:94
Trigger(std::string propname, as_function &trig, as_value customArg)
Definition: as_object.h:63
void setReachable() const
Definition: as_object.cpp:1032
bool array() const
Return true if this object should be treated as an array.
Definition: as_object.h:625
Definition: GnashKey.h:150
Property * getOwnProperty(const ObjectURI &uri)
Get this object&#39;s own named property, if existing.
Definition: as_object.cpp:926
Protect from deletion.
Definition: PropFlags.h:39
SortedPropertyList enumerateProperties(as_object &obj)
Enumerate all non-hidden properties to the passed container.
Definition: as_object.cpp:1069
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
void clearProperties()
Drop all properties from this object.
Definition: as_object.h:533
ActionScript value type.
Definition: as_value.h:94
bool dead() const
True if this Trigger has been disposed of.
Definition: as_object.h:88
void sendEvent(as_object &o, const as_environment &env, const ObjectURI &name)
Send a system event.
Definition: as_object.cpp:1099
An abstract property.
Definition: Property.h:276
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
virtual ~as_object()
The as_object dtor does nothing special.
Definition: as_object.h:174
uri
Definition: test.py:12
bool operator()(const Property &) const
Definition: as_object.h:799
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Exists()
Definition: as_object.h:798
std::string getURLEncodedVars(as_object &o)
Get url-encoded variables.
Definition: as_object.cpp:946
as_value(* as_c_function_ptr)(const fn_call &fn)
Definition: Property.h:34
Definition: GnashKey.h:149
bool isNativeType(const as_object *obj, T *&relay)
Check whether the object is an instance of a known type.
Definition: as_object.h:875
The base class for all ActionScript objects.
Definition: as_object.h:161
IsVisible(int version)
Definition: as_object.h:787
Definition: GnashKey.h:161
VM & vm() const
Return a reference to this as_object&#39;s global object.
Definition: as_object.h:205
as_value call(const as_value &oldval, const as_value &newval, as_object &this_obj)
Call the trigger.
Definition: as_object.cpp:1039
DisplayObject * displayObject() const
Return the DisplayObject associated with this object.
Definition: as_object.h:638
as_value getOwnProperty(as_object &o, const ObjectURI &uri)
Get an own member of an object.
Definition: as_object.h:777
A general use string table.
Definition: string_table.h:41
A URI for describing as_objects.
Definition: ObjectURI.h:44
as_value getMember(as_object &o, const ObjectURI &uri)
Get a member of an object using AS lookup rules.
Definition: as_object.h:756
bool operator()(const Property &p) const
Definition: as_object.h:808
Provides information about timeline context.
Definition: as_environment.h:50
as_object * getObjectWithPrototype(Global_as &gl, const ObjectURI &c)
Definition: as_object.cpp:1109
VM & getVM(const as_environment &env)
Definition: as_environment.h:222
An abstract property visitor.
Definition: PropertyList.h:49
virtual as_function * to_function()
Cast to a as_function, or return NULL.
Definition: as_object.h:471
bool visible(const Property &prop, int version)
Is this member supposed to be visible by a VM of given version ?
Definition: Property.h:423
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:53
void setDisplayObject(DisplayObject *d)
Set the DisplayObject associated with this as_object.
Definition: as_object.h:643
A trigger that can be associated with a property name.
Definition: as_object.h:59
void setArray(bool array=true)
Set whether this object should be treated as an array.
Definition: as_object.h:630
Relay * relay() const
Access the as_object&#39;s Relay object.
Definition: as_object.h:620
void visitProperties(PropertyVisitor &visitor) const
Visit the properties of this object by key/as_value pairs.
Definition: as_object.h:547
DSOTEXPORT as_value getValue(const as_object &this_ptr) const
Get value of this property.
Definition: Property.cpp:98
Definition: as_object.h:804
std::vector< std::pair< ObjectURI, as_value > > SortedPropertyList
This is used to hold an intermediate copy of an as_object&#39;s properties.
Definition: as_object.h:887
void setRelay(Relay *p)
Set the as_object&#39;s Relay object.
Definition: as_object.h:604
virtual bool isSuper() const
Return true if this is a &#39;super&#39; object.
Definition: as_object.h:474
#define DSOEXPORT
Definition: dsodefs.h:55
void kill()
Definition: as_object.h:90
int getSWFVersion(const as_environment &env)
Definition: as_environment.cpp:657
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
bool operator()(const Property &prop) const
Definition: as_object.h:788
string_table & getStringTable(const as_environment &env)
Definition: as_environment.cpp:639
The AVM1 virtual machine.
Definition: VM.h:71
Definition: GnashKey.h:132
bool caseless(const as_object &o)
Return whether property matching is caseless.
Definition: as_object.h:924
Definition: GnashKey.h:162
Set of properties associated with an ActionScript object.
Definition: PropertyList.h:81
const RunResources & getRunResources(const as_object &o)
Get the RunResources from an as_object.
Definition: as_object.cpp:1142
This is the base class for type-specific object data.
Definition: Relay.h:49
#define DSOTEXPORT
Definition: dsodefs.h:63
Function objects for visiting properties.
Definition: as_object.h:784
virtual bool get_member(const ObjectURI &uri, as_value *val)
Get a property by name if it exists.
Definition: as_object.cpp:378
const PropFlags & getFlags() const
accessor to the properties flags
Definition: Property.h:322
Definition: as_object.h:795
bool hasOwnProperty(as_object &o, const ObjectURI &uri)
Return true if this object has the named property.
Definition: as_object.h:854
Collectable resource.
Definition: GC.h:74
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:117
as_object * getPathElement(as_object &o, const ObjectURI &uri)
Resolve the given relative path component.
Definition: as_object.cpp:1089
IsEnumerable()
Definition: as_object.h:807
An abstract key visitor.
Definition: PropertyList.h:58
std::string name
Definition: LocalConnection_as.cpp:149
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
Protect from enumeration.
Definition: PropFlags.h:36
This class represents the &#39;Stage&#39; and top-level movie.
Definition: movie_root.h:150