Update with current status
[gnash.git] / libcore / as_object.cpp
blobed0a53a4444690f48b4fe37f1fc9213d4bd0f9a6
1 // as_object.cpp: ActionScript Object class and its properties, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "as_object.h"
22 #include <set>
23 #include <string>
24 #include <boost/algorithm/string/case_conv.hpp>
25 #include <utility> // for std::pair
27 #include "RunResources.h"
28 #include "log.h"
29 #include "as_function.h"
30 #include "as_environment.h"
31 #include "movie_root.h"
32 #include "event_id.h"
33 #include "Property.h"
34 #include "VM.h"
35 #include "GnashException.h"
36 #include "fn_call.h"
37 #include "Array_as.h"
38 #include "as_function.h"
39 #include "Global_as.h"
40 #include "GnashAlgorithm.h"
41 #include "DisplayObject.h"
42 #include "namedStrings.h"
44 namespace gnash {
45 template<typename T>
46 class
47 as_object::PrototypeRecursor
49 public:
50 PrototypeRecursor(as_object* top, const ObjectURI& uri, T cmp = T())
52 _object(top),
53 _uri(uri),
54 _iterations(0),
55 _condition(std::move(cmp))
57 _visited.insert(top);
60 /// Iterate to the next object in the inheritance chain.
62 /// This function throws an ActionLimitException when the maximum
63 /// number of recursions is reached.
65 /// @return false if there is no next object. In this case calling
66 /// the other functions will abort.
67 bool operator()()
69 ++_iterations;
71 // See swfdec/prototype-recursion-get-?.swf
72 if (_iterations > 256) {
73 throw ActionLimitException("Lookup depth exceeded.");
76 _object = _object->get_prototype();
78 // TODO: there is recursion prevention anyway; is this extra
79 // check for circularity really necessary?
80 if (!_visited.insert(_object).second) return 0;
81 return _object && !_object->displayObject();
84 /// Return the wanted property if it exists and satisfies the predicate.
86 /// This will abort if there is no current object.
87 Property* getProperty(as_object** owner = nullptr) const {
89 assert(_object);
90 Property* prop = _object->_members.getProperty(_uri);
92 if (prop && _condition(*prop)) {
93 if (owner) *owner = _object;
94 return prop;
96 return nullptr;
99 private:
100 as_object* _object;
101 const ObjectURI& _uri;
102 std::set<const as_object*> _visited;
103 size_t _iterations;
104 T _condition;
107 // Anonymous namespace used for module-static defs
108 namespace {
110 as_function*
111 getConstructor(as_object& o)
113 as_value ctorVal;
114 if (!o.get_member(NSV::PROP_uuCONSTRUCTORuu, &ctorVal)) {
115 return nullptr;
117 return ctorVal.to_function();
120 /// 'super' is a special kind of object
122 /// See http://wiki.gnashdev.org/wiki/index.php/ActionScriptSuper
124 /// We make it derive from as_function instead of as_object
125 /// to avoid touching too many files (ie: an as_object is not considered
126 /// something that can be called by current Gnash code). We may want
127 /// to change this in the future to implement what ECMA-262 refers to
128 /// as the [[Call]] property of objects.
130 class as_super : public as_object
132 public:
134 as_super(Global_as& gl, as_object* super)
136 as_object(gl),
137 _super(super)
139 set_prototype(prototype());
142 virtual bool isSuper() const { return true; }
144 virtual as_object* get_super(const ObjectURI& fname);
146 // Fetching members from 'super' yelds a lookup on the associated prototype
147 virtual bool get_member(const ObjectURI& uri, as_value* val)
149 as_object* proto = prototype();
150 if (proto) return proto->get_member(uri, val);
151 log_debug("Super has no associated prototype");
152 return false;
155 /// Dispatch.
156 virtual as_value call(const fn_call& fn)
159 // TODO: this is a hack to make sure objects are constructed, not
160 // converted (fn.isInstantiation() must be true).
161 fn_call::Args::container_type argsIn(fn.getArgs());
162 fn_call::Args args;
163 args.swap(argsIn);
165 fn_call fn2(fn.this_ptr, fn.env(), args, fn.super, true);
166 assert(fn2.isInstantiation());
167 as_function* ctor = constructor();
168 if (ctor) return ctor->call(fn2);
169 log_debug("Super has no associated constructor");
170 return as_value();
173 protected:
175 virtual void markReachableResources() const
177 if (_super) _super->setReachable();
178 as_object::markReachableResources();
181 private:
183 as_object* prototype() {
184 return _super ? _super->get_prototype() : nullptr;
187 as_function* constructor() {
188 return _super ? getConstructor(*_super) : nullptr;
191 as_object* _super;
194 as_object*
195 as_super::get_super(const ObjectURI& fname)
197 // Super references the super class of our class prototype.
198 // Our class prototype is __proto__.
199 // Our class superclass prototype is __proto__.__proto__
201 // Our class prototype is __proto__.
202 as_object* proto = get_prototype();
203 if (!proto) return new as_super(getGlobal(*this), nullptr);
205 if (fname.empty() || getSWFVersion(*this) <= 6) {
206 return new as_super(getGlobal(*this), proto);
209 as_object* owner = nullptr;
210 proto->findProperty(fname, &owner);
211 if (!owner) return nullptr;
213 if (owner == proto) return new as_super(getGlobal(*this), proto);
215 as_object* tmp = proto;
216 while (tmp && tmp->get_prototype() != owner) {
217 tmp = tmp->get_prototype();
219 // ok, now 'tmp' should be the object whose __proto__ member
220 // contains the actual named method.
222 // in the C:B:A:F case this would be B when calling
223 // super.myName() from C.prototype.myName()
225 // well, since we found the property, it must be somewhere!
226 assert(tmp);
228 if (tmp != proto) { return new as_super(getGlobal(*this), tmp); }
229 return new as_super(getGlobal(*this), owner);
233 /// A PropertyList visitor copying properties to an object
234 class PropsCopier : public PropertyVisitor
237 public:
239 /// \brief
240 /// Initialize a PropsCopier instance associating it
241 /// with a target object (an object whose members has to be set)
243 PropsCopier(as_object& tgt)
245 _tgt(tgt)
249 /// Set *inherited* properties of the given target object
250 bool accept(const ObjectURI& uri, const as_value& val) {
251 if (getName(uri) == NSV::PROP_uuPROTOuu) return true;
252 _tgt.set_member(uri, val);
253 return true;
255 private:
256 as_object& _tgt;
259 class PropertyEnumerator : public PropertyVisitor
261 public:
262 PropertyEnumerator(SortedPropertyList& to)
264 _to(to)
267 bool accept(const ObjectURI& uri, const as_value& val) {
268 _to.push_back(std::make_pair(uri, val));
269 return true;
271 private:
272 SortedPropertyList& _to;
275 } // anonymous namespace
278 const int as_object::DefaultFlags;
280 as_object::as_object(const Global_as& gl)
282 GcResource(getRoot(gl).gc()),
283 _displayObject(nullptr),
284 _array(false),
285 _vm(getVM(gl)),
286 _members(*this)
290 as_object::as_object(VM& vm)
292 GcResource(vm.getRoot().gc()),
293 _displayObject(nullptr),
294 _array(false),
295 _vm(vm),
296 _members(*this)
300 as_value
301 as_object::call(const fn_call& /*fn*/)
303 throw ActionTypeError();
306 std::string
307 as_object::stringValue() const
309 return "[object Object]";
312 std::pair<bool,bool>
313 as_object::delProperty(const ObjectURI& uri)
315 return _members.delProperty(uri);
319 void
320 as_object::add_property(const std::string& name, as_function& getter,
321 as_function* setter)
323 const ObjectURI& uri = getURI(vm(), name);
325 Property* prop = _members.getProperty(uri);
327 if (prop) {
328 const as_value& cacheVal = prop->getCache();
329 // Used to return the return value of addGetterSetter, but this
330 // is always true.
331 _members.addGetterSetter(uri, getter, setter, cacheVal);
332 return;
333 // NOTE: watch triggers not called when adding a new
334 // getter-setter property
336 else {
338 _members.addGetterSetter(uri, getter, setter, as_value());
340 // Nothing more to do if there are no triggers.
341 if (!_trigs.get()) return;
343 // check if we have a trigger, if so, invoke it
344 // and set val to its return
345 TriggerContainer::iterator trigIter = _trigs->find(uri);
347 if (trigIter != _trigs->end()) {
349 Trigger& trig = trigIter->second;
351 log_debug("add_property: property %s is being watched", name);
352 as_value v = trig.call(as_value(), as_value(), *this);
354 // The trigger call could have deleted the property,
355 // so we check for its existence again, and do NOT put
356 // it back in if it was deleted
357 prop = _members.getProperty(uri);
358 if (!prop) {
359 log_debug("Property %s deleted by trigger on create (getter-setter)", name);
360 return;
362 prop->setCache(v);
364 return;
369 /// Order of property lookup:
371 /// 1. Visible own properties.
372 /// 2. If DisplayObject, magic properties
373 /// 3. Visible own properties of all __proto__ objects (a DisplayObject
374 /// ends the chain).
375 /// 4. __resolve property of this object and all __proto__ objects (a Display
376 /// Object ends the chain). This should ignore visibility but doesn't.
377 bool
378 as_object::get_member(const ObjectURI& uri, as_value* val)
380 assert(val);
382 const int version = getSWFVersion(*this);
384 PrototypeRecursor<IsVisible> pr(this, uri, IsVisible(version));
386 Property* prop = pr.getProperty();
387 if (!prop) {
388 if (displayObject()) {
389 DisplayObject* d = displayObject();
390 if (getDisplayObjectProperty(*d, uri, *val)) return true;
392 while (pr()) {
393 if ((prop = pr.getProperty())) break;
397 // If the property isn't found or doesn't apply to any objects in the
398 // inheritance chain, try the __resolve property.
399 if (!prop) {
401 PrototypeRecursor<Exists> pr(this, NSV::PROP_uuRESOLVE);
403 as_value resolve;
405 for (;;) {
406 Property* res = pr.getProperty();
407 if (res) {
408 resolve = res->isGetterSetter() ? res->getCache() :
409 res->getValue(*this);
410 if (version < 7) break;
411 if (resolve.is_object()) break;
413 // Finished searching.
414 if (!pr()) return false;
417 // If __resolve exists, call it with the name of the undefined
418 // property.
419 string_table& st = getStringTable(*this);
420 const std::string& undefinedName = st.value(getName(uri));
422 fn_call::Args args;
423 args += undefinedName;
425 // Invoke the __resolve property.
426 *val = invoke(resolve, as_environment(getVM(*this)), this, args);
428 return true;
431 try {
432 *val = prop->getValue(*this);
433 return true;
435 catch (const ActionTypeError& exc) {
436 IF_VERBOSE_ASCODING_ERRORS(
437 log_aserror(_("Caught exception: %s"), exc.what());
439 return false;
444 as_object*
445 as_object::get_super(const ObjectURI& fname)
447 // Super references the super class of our class prototype.
448 // Our class prototype is __proto__.
449 // Our class superclass prototype is __proto__.__proto__
451 // Our class prototype is __proto__.
452 as_object* proto = get_prototype();
454 if ( ! fname.empty() && getSWFVersion(*this) > 6) {
455 as_object* owner = nullptr;
456 findProperty(fname, &owner);
457 // should be 0 if findProperty returned 0
458 if (owner != this) proto = owner;
461 as_object* super = new as_super(getGlobal(*this), proto);
463 return super;
466 as_object*
467 as_object::get_super()
469 // Our class prototype is __proto__.
470 as_object* proto = get_prototype();
471 as_object* super = new as_super(getGlobal(*this), proto);
473 return super;
476 Property*
477 as_object::findProperty(const ObjectURI& uri, as_object** owner)
480 const int version = getSWFVersion(*this);
482 PrototypeRecursor<IsVisible> pr(this, uri, IsVisible(version));
484 do {
485 Property* prop = pr.getProperty(owner);
486 if (prop) return prop;
487 } while (pr());
489 // No Property found
490 return nullptr;
493 Property*
494 as_object::findUpdatableProperty(const ObjectURI& uri)
497 PrototypeRecursor<Exists> pr(this, uri);
499 Property* prop = pr.getProperty();
501 // We won't scan the inheritance chain if we find a member,
502 // even if invisible.
503 if (prop) return prop;
505 const int swfVersion = getSWFVersion(*this);
507 while (pr()) {
508 if ((prop = pr.getProperty())) {
509 if (prop->isGetterSetter() && visible(*prop, swfVersion)) {
510 return prop;
514 return nullptr;
517 void
518 as_object::set_prototype(const as_value& proto)
520 // TODO: check what happens if __proto__ is set as a user-defined
521 // getter/setter
522 // TODO: check triggers !!
523 _members.setValue(NSV::PROP_uuPROTOuu, proto, as_object::DefaultFlags);
526 void
527 as_object::executeTriggers(Property* prop, const ObjectURI& uri,
528 const as_value& val)
531 // check if we have a trigger, if so, invoke it
532 // and set val to its return
533 TriggerContainer::iterator trigIter;
535 // If there are no triggers or the trigger is not found, just set
536 // the property.
537 if (!_trigs.get() || (trigIter = _trigs->find(uri)) == _trigs->end()) {
538 if (prop) {
539 prop->setValue(*this, val);
540 prop->clearVisible(getSWFVersion(*this));
542 return;
545 Trigger& trig = trigIter->second;
547 if (trig.dead()) {
548 _trigs->erase(trigIter);
549 return;
552 // WARNING: getValue might itself invoke a trigger
553 // (getter-setter)... ouch ?
554 // TODO: in this case, return the underlying value !
555 const as_value& curVal = prop ? prop->getCache() : as_value();
556 const as_value& newVal = trig.call(curVal, val, *this);
558 // This is a particularly clear and concise way of removing dead triggers.
559 EraseIf(*_trigs, std::bind(std::mem_fn(&Trigger::dead),
560 std::bind(&TriggerContainer::value_type::second,
561 std::placeholders::_1)));
563 // The trigger call could have deleted the property,
564 // so we check for its existence again, and do NOT put
565 // it back in if it was deleted
566 prop = findUpdatableProperty(uri);
567 if (!prop) return;
569 prop->setValue(*this, newVal);
570 prop->clearVisible(getSWFVersion(*this));
574 /// Order of property lookup:
576 /// 0. MovieClip textfield variables. TODO: this is a hack and should be
577 /// eradicated.
578 /// 1. Own properties even if invisible or not getter-setters.
579 /// 2. If DisplayObject, magic properties
580 /// 3. Visible own getter-setter properties of all __proto__ objects
581 /// (a DisplayObject ends the chain).
582 bool
583 as_object::set_member(const ObjectURI& uri, const as_value& val, bool ifFound)
586 bool tfVarFound = false;
587 if (displayObject()) {
588 MovieClip* mc = dynamic_cast<MovieClip*>(displayObject());
589 if (mc) tfVarFound = mc->setTextFieldVariables(uri, val);
590 // We still need to set the member.
593 // Handle the length property for arrays. NB: checkArrayLength() will
594 // call this function again if the key is a valid index.
595 if (array()) checkArrayLength(*this, uri, val);
597 PrototypeRecursor<Exists> pr(this, uri);
599 Property* prop = pr.getProperty();
601 // We won't scan the inheritance chain if we find a member,
602 // even if invisible.
603 if (!prop) {
605 if (displayObject()) {
606 DisplayObject* d = displayObject();
607 if (setDisplayObjectProperty(*d, uri, val)) return true;
608 // TODO: should we execute triggers?
611 const int version = getSWFVersion(*this);
612 while (pr()) {
613 if ((prop = pr.getProperty())) {
614 if ((prop->isGetterSetter()) && visible(*prop, version)) {
615 break;
617 else prop = nullptr;
622 if (prop) {
623 if (readOnly(*prop)) {
624 IF_VERBOSE_ASCODING_ERRORS(
625 ObjectURI::Logger l(getStringTable(*this));
626 log_aserror(_("Attempt to set read-only property '%s'"),
627 l(uri));
629 return true;
632 try {
633 executeTriggers(prop, uri, val);
635 catch (const ActionTypeError& exc) {
636 IF_VERBOSE_ASCODING_ERRORS(
637 log_aserror(
638 _("%s: %s"), getStringTable(*this).value(getName(uri)), exc.what());
642 return true;
645 // Else, add new property...
646 if (ifFound) return false;
648 // Property does not exist, so it won't be read-only. Set it.
649 if (!_members.setValue(uri, val)) {
651 IF_VERBOSE_ASCODING_ERRORS(
652 ObjectURI::Logger l(getStringTable(*this));
653 log_aserror(_("Unknown failure in setting property '%s' on "
654 "object '%p'"), l(uri), (void*) this);
656 return false;
659 executeTriggers(prop, uri, val);
661 // Return true if we found a textfield variable.
662 if (tfVarFound) return true;
664 return false;
668 void
669 as_object::init_member(const std::string& key1, const as_value& val, int flags)
671 const ObjectURI& uri(getURI(vm(), key1));
672 init_member(uri, val, flags);
675 void
676 as_object::init_member(const ObjectURI& uri, const as_value& val, int flags)
679 // Set (or create) a SimpleProperty
680 if (!_members.setValue(uri, val, flags)) {
681 ObjectURI::Logger l(getStringTable(*this));
682 log_error(_("Attempt to initialize read-only property '%s'"
683 " on object '%p' twice"), l(uri), (void*)this);
684 // We shouldn't attempt to initialize a member twice, should we ?
685 abort();
689 void
690 as_object::init_property(const std::string& name, as_function& getter,
691 as_function& setter, int flags)
693 const ObjectURI& uri = getURI(vm(), name);
694 init_property(uri, getter, setter, flags);
697 void
698 as_object::init_property(const ObjectURI& uri, as_function& getter,
699 as_function& setter, int flags)
701 _members.addGetterSetter(uri, getter, &setter, as_value(), flags);
704 void
705 as_object::init_property(const std::string& name, as_c_function_ptr getter,
706 as_c_function_ptr setter, int flags)
708 const ObjectURI& uri = getURI(vm(), name);
709 init_property(uri, getter, setter, flags);
712 void
713 as_object::init_property(const ObjectURI& uri, as_c_function_ptr getter,
714 as_c_function_ptr setter, int flags)
716 _members.addGetterSetter(uri, getter, setter, flags);
719 bool
720 as_object::init_destructive_property(const ObjectURI& uri, as_function& getter,
721 int flags)
723 return _members.addDestructiveGetter(uri, getter, flags);
726 bool
727 as_object::init_destructive_property(const ObjectURI& uri,
728 as_c_function_ptr getter, int flags)
730 return _members.addDestructiveGetter(uri, getter, flags);
733 void
734 as_object::init_readonly_property(const std::string& name, as_function& getter,
735 int initflags)
737 const ObjectURI& uri = getURI(vm(), name);
739 init_property(uri, getter, getter, initflags | PropFlags::readOnly);
740 assert(_members.getProperty(uri));
743 void
744 as_object::init_readonly_property(const std::string& name,
745 as_c_function_ptr getter, int initflags)
747 const ObjectURI& uri = getURI(vm(), name);
748 init_property(uri, getter, getter, initflags | PropFlags::readOnly);
749 assert(_members.getProperty(uri));
752 void
753 as_object::set_member_flags(const ObjectURI& uri, int setTrue, int setFalse)
755 _members.setFlags(uri, setTrue, setFalse);
758 void
759 as_object::addInterface(as_object* obj)
761 assert(obj);
762 if (std::find(_interfaces.begin(), _interfaces.end(), obj) ==
763 _interfaces.end()) {
764 _interfaces.push_back(obj);
768 bool
769 as_object::instanceOf(as_object* ctor)
772 /// An object is never an instance of a null prototype.
773 if (!ctor) return false;
775 as_value protoVal;
776 if (!ctor->get_member(NSV::PROP_PROTOTYPE, &protoVal)) {
777 #ifdef GNASH_DEBUG_INSTANCE_OF
778 log_debug("Object %p can't be an instance of an object (%p) with no 'prototype'",
779 (void*)this, (void*)ctor);
780 #endif
781 return false;
784 as_object* ctorProto = toObject(protoVal, getVM(*this));
785 if (!ctorProto) {
786 #ifdef GNASH_DEBUG_INSTANCE_OF
787 log_debug("Object %p can't be an instance of an object (%p) with non-object 'prototype' (%s)",
788 (void*)this, (void*)ctor, protoVal);
789 #endif
790 return false;
793 // TODO: cleanup the iteration, make it more readable ...
794 std::set<as_object*> visited;
796 as_object* obj = this;
797 while (obj && visited.insert(obj).second) {
798 as_object* thisProto = obj->get_prototype();
799 if (!thisProto) {
800 break;
803 // Check our proto
804 if (thisProto == ctorProto) {
805 #ifdef GNASH_DEBUG_INSTANCE_OF
806 log_debug("Object %p is an instance of constructor %p as the constructor exposes our __proto__ %p",
807 (void*)obj, (void*)ctor, (void*)thisProto);
808 #endif
809 return true;
812 // Check our proto interfaces
813 if (std::find(thisProto->_interfaces.begin(),
814 thisProto->_interfaces.end(), ctorProto)
815 != thisProto->_interfaces.end()) {
817 #ifdef GNASH_DEBUG_INSTANCE_OF
818 log_debug("Object %p __proto__ %p had one interface matching with the constructor prototype %p",
819 (void*)obj, (void*)thisProto, (void*)ctorProto);
820 #endif
821 return true;
824 obj = thisProto;
827 return false;
830 bool
831 as_object::prototypeOf(as_object& instance)
833 as_object* obj = &instance;
835 std::set<as_object*> visited;
837 while (obj && visited.insert(obj).second ) {
838 if (obj->get_prototype() == this) return true;
839 obj = obj->get_prototype();
842 // See actionscript.all/Inheritance.as for a way to trigger this
843 IF_VERBOSE_ASCODING_ERRORS(
844 if (obj) log_aserror(_("Circular inheritance chain detected "
845 "during isPrototypeOf call"));
848 return false;
851 void
852 as_object::dump_members()
854 log_debug("%d members of object %p follow", _members.size(),
855 static_cast<const void*>(this));
856 _members.dump();
859 void
860 as_object::setPropFlags(const as_value& props_val, int set_false, int set_true)
863 if (props_val.is_null()) {
864 // Take all the members of the object
865 _members.setFlagsAll(set_true, set_false);
866 return;
869 std::string propstr = props_val.to_string();
871 for (;;) {
873 std::string prop;
874 size_t next_comma=propstr.find(",");
875 if (next_comma == std::string::npos) {
876 prop = propstr;
878 else {
879 prop = propstr.substr(0,next_comma);
880 propstr = propstr.substr(next_comma+1);
883 // set_member_flags will take care of case conversion
884 set_member_flags(getURI(vm(), prop), set_true, set_false);
886 if (next_comma == std::string::npos) {
887 break;
890 return;
894 void
895 as_object::copyProperties(const as_object& o)
897 PropsCopier copier(*this);
899 // TODO: check if non-visible properties should be also copied !
900 o.visitProperties<Exists>(copier);
903 void
904 as_object::visitKeys(KeyVisitor& visitor) const
906 // Hack to handle MovieClips.
907 if (displayObject()) {
908 displayObject()->visitNonProperties(visitor);
911 // this set will keep track of visited objects,
912 // to avoid infinite loops
913 std::set<const as_object*> visited;
915 PropertyList::PropertyTracker doneList;
917 const as_object* current(this);
918 while (current && visited.insert(current).second) {
919 current->_members.visitKeys(visitor, doneList);
920 current = current->get_prototype();
925 Property*
926 as_object::getOwnProperty(const ObjectURI& uri)
928 return _members.getProperty(uri);
931 as_object*
932 as_object::get_prototype() const
934 int swfVersion = getSWFVersion(*this);
936 Property* prop = _members.getProperty(NSV::PROP_uuPROTOuu);
937 if (!prop) return nullptr;
938 if (!visible(*prop, swfVersion)) return nullptr;
940 const as_value& proto = prop->getValue(*this);
942 return toObject(proto, getVM(*this));
945 std::string
946 getURLEncodedVars(as_object& o)
948 SortedPropertyList props = enumerateProperties(o);
950 std::string data;
951 string_table& st = getStringTable(o);
953 for (SortedPropertyList::const_reverse_iterator i = props.rbegin(),
954 e = props.rend(); i != e; ++i) {
956 const std::string& name = i->first.toString(st);
957 const std::string& value = i->second.to_string();
959 // see bug #22006
960 if (!name.empty() && name[0] == '$') continue;
962 URL::encode(value);
963 if (i != props.rbegin()) data += '&';
965 data += name + "=" + value;
968 return data;
971 bool
972 as_object::watch(const ObjectURI& uri, as_function& trig,
973 const as_value& cust)
976 std::string propname = getStringTable(*this).value(getName(uri));
978 if (!_trigs.get()) _trigs.reset(new TriggerContainer);
980 TriggerContainer::iterator it = _trigs->find(uri);
981 if (it == _trigs->end()) {
982 return _trigs->insert(
983 std::make_pair(uri, Trigger(propname, trig, cust))).second;
985 it->second = Trigger(propname, trig, cust);
986 return true;
989 bool
990 as_object::unwatch(const ObjectURI& uri)
992 if (!_trigs.get()) return false;
994 TriggerContainer::iterator trigIter = _trigs->find(uri);
995 if (trigIter == _trigs->end()) {
996 log_debug("No watch for property %s",
997 getStringTable(*this).value(getName(uri)));
998 return false;
1000 Property* prop = _members.getProperty(uri);
1001 if (prop && prop->isGetterSetter()) {
1002 log_debug("Watch on %s not removed (is a getter-setter)",
1003 getStringTable(*this).value(getName(uri)));
1004 return false;
1006 trigIter->second.kill();
1007 return true;
1010 void
1011 as_object::markReachableResources() const
1013 _members.setReachable();
1015 if (_trigs.get()) {
1016 for (TriggerContainer::const_iterator it = _trigs->begin();
1017 it != _trigs->end(); ++it) {
1018 it->second.setReachable();
1022 // Mark interfaces reachable.
1023 std::for_each(_interfaces.begin(), _interfaces.end(),
1024 std::mem_fun(&as_object::setReachable));
1026 // Proxy objects can contain references to other as_objects.
1027 if (_relay) _relay->setReachable();
1028 if (_displayObject) _displayObject->setReachable();
1031 void
1032 Trigger::setReachable() const
1034 _func->setReachable();
1035 _customArg.setReachable();
1038 as_value
1039 Trigger::call(const as_value& oldval, const as_value& newval,
1040 as_object& this_obj)
1042 assert(!_dead);
1044 if (_executing) return newval;
1046 _executing = true;
1048 try {
1050 const as_environment env(getVM(this_obj));
1052 fn_call::Args args;
1053 args += _propname, oldval, newval, _customArg;
1055 fn_call fn(&this_obj, env, args);
1056 as_value ret = _func->call(fn);
1057 _executing = false;
1059 return ret;
1062 catch (const GnashException&) {
1063 _executing = false;
1064 throw;
1068 SortedPropertyList
1069 enumerateProperties(as_object& obj)
1072 // this set will keep track of visited objects,
1073 // to avoid infinite loops
1074 std::set<as_object*> visited;
1076 SortedPropertyList to;
1077 PropertyEnumerator e(to);
1078 as_object* current(&obj);
1080 while (current && visited.insert(current).second) {
1081 current->visitProperties<IsEnumerable>(e);
1082 current = current->get_prototype();
1084 return to;
1088 as_object*
1089 getPathElement(as_object& o, const ObjectURI& uri)
1091 as_value tmp;
1092 if (!o.get_member(uri, &tmp)) return nullptr;
1093 if (!tmp.is_object()) return nullptr;
1094 return toObject(tmp, getVM(o));
1098 void
1099 sendEvent(as_object& o, const as_environment& env, const ObjectURI& name)
1101 Property* prop = o.findProperty(name);
1102 if (prop) {
1103 fn_call::Args args;
1104 invoke(prop->getValue(o), env, &o, args);
1108 as_object*
1109 getObjectWithPrototype(Global_as& gl, const ObjectURI& c)
1111 as_object* ctor = toObject(getMember(gl, c), getVM(gl));
1112 as_object* proto = ctor ?
1113 toObject(getMember(*ctor, NSV::PROP_PROTOTYPE), getVM(gl)) : nullptr;
1115 as_object* o = createObject(gl);
1116 o->set_prototype(proto ? proto : as_value());
1117 return o;
1120 /// Get the VM from an as_object
1122 getVM(const as_object& o)
1124 return o.vm();
1127 /// Get the movie_root from an as_object
1128 movie_root&
1129 getRoot(const as_object& o)
1131 return o.vm().getRoot();
1134 /// Get the string_table from an as_object
1135 string_table&
1136 getStringTable(const as_object& o)
1138 return o.vm().getStringTable();
1141 const RunResources&
1142 getRunResources(const as_object& o)
1144 return o.vm().getRoot().runResources();
1148 getSWFVersion(const as_object& o)
1150 return o.vm().getSWFVersion();
1153 Global_as&
1154 getGlobal(const as_object& o)
1156 return *o.vm().getGlobal();
1159 } // end of gnash namespace
1161 // local Variables:
1162 // mode: C++
1163 // indent-tabs-mode: nil
1164 // End: