~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// 
//   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
//   Free Software Foundation, Inc
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


#ifndef GNASH_PROPERTY_H
#define GNASH_PROPERTY_H

#include <boost/variant.hpp>
#include <cassert>
#include <boost/bind.hpp>
#include <typeinfo>

#include "PropFlags.h"
#include "as_value.h"
#include "ObjectURI.h"

namespace gnash {
    typedef as_value (*as_c_function_ptr)(const fn_call& fn);
    class as_function;
}

namespace gnash {

/// Holder for getter/setter functions
//
/// Getter setter can be user-defined or native ones.
/// This class abstracts the two.
class GetterSetter
{
    class NativeGetterSetter;

    // The following helper structs define common operations on the 
    // Two types of GetterSetter. Some operations are applicable only to
    // one type.

    /// Get or set a GetterSetter
    //
    /// @tparam S   A type that determines what operation to call on the
    ///             GetterSetter
    /// @param Arg  The type of the argument to the get or set function.
    template<typename Arg, typename S>
    struct GetSetVisitor : boost::static_visitor<typename S::result_type>
    {
        GetSetVisitor(const Arg& arg) : _arg(arg) {}
        template<typename T> typename S::result_type operator()(T& t) const {
            return S()(t, _arg);
        }
    private:
        const Arg& _arg;
    };

    /// Set a GetterSetter
    struct Set
    {
        typedef void result_type;
        template<typename T, typename Arg>
        result_type operator()(T& t, Arg& a) const {
            t.set(a);
        }
    };
				
    /// Get a GetterSetter
    struct Get
    {
        typedef as_value result_type;
        template<typename T, typename Arg>
        result_type operator()(T& t, Arg& a) const {
            return t.get(a);
        }
    };

    /// Set the underlying value of a GetterSetter
    //
    /// This does not apply to NativeGetterSetters.
    struct SetUnderlying : boost::static_visitor<>
    {
        template<typename T>
        result_type operator()(T& gs, const as_value& val) const {
            gs.setUnderlying(val);
        }
        result_type operator()(NativeGetterSetter&, const as_value&) const {}
    };
    
    /// Get the underlying value of a GetterSetter
    //
    /// This does not apply to NativeGetterSetters.
    struct GetUnderlying : boost::static_visitor<as_value>
    {
        template<typename T>
        result_type operator()(const T& gs) const {
            return gs.getUnderlying();
        }
        result_type operator()(const NativeGetterSetter&) const {
            return result_type();
        }
    };
    
    /// Mark a GetterSetter reachable
    struct MarkReachable : boost::static_visitor<>
    {
        template<typename T>
        result_type operator()(const T& gs) const {
            gs.markReachableResources();
        }
    };

public:

	/// Construct a user-defined getter-setter
	GetterSetter(as_function* getter, as_function* setter)
		:
		_getset(UserDefinedGetterSetter(getter, setter))
	{}

	/// Construct a native getter-setter
	GetterSetter(as_c_function_ptr getter, as_c_function_ptr setter)
		:
		_getset(NativeGetterSetter(getter, setter))
	{}

	/// Invoke the getter
	as_value get(fn_call& fn) const {
        GetSetVisitor<const fn_call, Get> s(fn);
        return boost::apply_visitor(s, _getset);
	}

	/// Invoke the setter
	void set(const fn_call& fn) {
        GetSetVisitor<fn_call, Set> s(fn);
        boost::apply_visitor(s, _getset);
	}

	/// Set the cache value (for user-defined getter-setters)
	void setCache(const as_value& v) {
        boost::apply_visitor(boost::bind(SetUnderlying(), _1, v), _getset);
    }

	/// Get the cache value (for user-defined getter-setters)
	as_value getCache() const {
        return boost::apply_visitor(GetUnderlying(), _getset);
	}

	void markReachableResources() const {
        boost::apply_visitor(MarkReachable(), _getset);
	}

private:

	/// User-defined getter/setter
	class UserDefinedGetterSetter
    {
	public:

		UserDefinedGetterSetter(as_function* get, as_function* set)
			:
			_getter(get),
			_setter(set),
			_underlyingValue(),
			_beingAccessed(false)
		{}

		/// Invoke the getter
		as_value get(const fn_call& fn) const;

		/// Invoke the setter
		void set(const fn_call& fn);

		/// Get the underlying value
		const as_value& getUnderlying() const { return _underlyingValue; }

		/// Set the underlying value
		void setUnderlying(const as_value& v) { _underlyingValue = v; }

		void markReachableResources() const;

	private:

		/// For SWF6 (not higher) a user-defined getter-setter would not
        /// be invoked while being set. This ScopedLock helps marking a
        /// Getter-Setter as being invoked in an exception-safe manner.
        //
        /// Note this is not thread safe and does not attempt to provide 
        /// thread safety.
		class ScopedLock : boost::noncopyable
        {
		public:

			explicit ScopedLock(const UserDefinedGetterSetter& na)
                :
                _a(na),
                _obtainedLock(_a._beingAccessed ? false : true)
			{
                // If we didn't obtain the lock it would be true anyway,
                // but it's probably polite to avoid touching it.
                if (_obtainedLock) _a._beingAccessed = true;
			}

			~ScopedLock() { if ( _obtainedLock) _a._beingAccessed = false; }

			/// Return true if the lock was obtained
			//
			/// If false is returned, we're being called recursively,
			/// which means we should set the underlyingValue instead
			/// of calling the setter (for SWF6, again).
			///
			bool obtainedLock() const { return _obtainedLock; }

        private:

			const UserDefinedGetterSetter& _a;
			bool _obtainedLock;

        };

		as_function* _getter;
		as_function* _setter;
		as_value _underlyingValue;
		mutable bool _beingAccessed;
    };

	/// Native GetterSetter
	class NativeGetterSetter 
    {
	public:

		NativeGetterSetter(as_c_function_ptr get, as_c_function_ptr set)
			:
			_getter(get), _setter(set) {}

		/// Invoke the getter
		as_value get(const fn_call& fn) const {
			return _getter(fn);
		}

		/// Invoke the setter
		void set(const fn_call& fn) {
			_setter(fn);
		}

        /// Nothing to do for native setters.
        void markReachableResources() const {}

	private:
		as_c_function_ptr _getter;
		as_c_function_ptr _setter;
	};

	boost::variant<UserDefinedGetterSetter, NativeGetterSetter> _getset;

};

/// An abstract property
//
/// A Property is a holder for a value or a getter-setter.
//
/// Properties have special const semantics: the value of a Property does
/// not affect its outward state, so the value of a const Property can be
/// changed.
class Property
{

    /// Mark the stored value reachable.
    struct SetReachable : boost::static_visitor<>
    {
        result_type operator()(const as_value& val) const {
            val.setReachable();
        }
        result_type operator()(const GetterSetter& gs) const {
            return gs.markReachableResources();
        }
    };

public:

	Property(const ObjectURI& uri, const as_value& value,
            const PropFlags& flags)
        :
        _bound(value),
		_uri(uri),
		_flags(flags),
        _destructive(false)
	{}

	Property(const ObjectURI& uri,
		as_function* getter, as_function* setter, 
		const PropFlags& flags, bool destroy = false)
        :
        _bound(GetterSetter(getter, setter)),
        _uri(uri),
		_flags(flags), 
		_destructive(destroy)
	{}

	Property(const ObjectURI& uri, as_c_function_ptr getter,
            as_c_function_ptr setter, const PropFlags& flags,
            bool destroy = false)
		:
        _bound(GetterSetter(getter, setter)),
        _uri(uri),
		_flags(flags),
        _destructive(destroy)
	{}

    /// Copy constructor
	Property(const Property& p)
        :
        _bound(p._bound),
        _uri(p._uri),
		_flags(p._flags),
        _destructive(p._destructive)
	{}

	/// accessor to the properties flags
	const PropFlags& getFlags() const { return _flags; }

    /// Set the flags of the property
    void setFlags(const PropFlags& flags) const {
        _flags = flags;
    }

	/// Get value of this property
	//
	/// @param this_ptr
	/// 	The as_object used to set the 'this' pointer.
	/// 	for calling getter function (GetterSetterProperty);
	/// 	it will be unused when getting or setting SimpleProperty
	/// 	properties.
	/// @return the value of this property
	///
	as_value getValue(const as_object& this_ptr) const;

	/// Get internal cached value of this property
	//
	/// For simple properties, this is the usual value;
	/// for user-defined getter-setter this is a cached value
	/// to watch for infinitely recurse on calling the getter
	/// or setter; Native getter-setter has no cache,
	/// undefined will be returned for them.
	as_value getCache() const;

	/// Set internal cached value of this property
	//
	/// For simple properties, this is the usual value;
	/// for user-defined getter-setter this is a cached value
	/// to watch for infinitely recurse on calling the getter
	/// or setter; Native getter-setter has no cache,
	/// nothing would happen for them.
	void setCache(const as_value& v);

	/// Set value of this property
	//
	/// @param this_ptr The as_object used to set the 'this' pointer
	///                 for calling getter/setter function
	///                 It will be unused when getting or setting
    ///                 simple properties.
	///	                This parameter is non-const as nothing prevents an
	///	                eventual "Setter" function from actually modifying it,
	///	                so we can't promise constness.
	/// @param value    The new value for this property. It will be used as first
	///	                argument of the 'setter' function if this is a Getter/Setter
	///                 property. @see isGetterSetter().
    /// @return         true if the property was set, otherwise false. Read-only
    ///                 properties can generally not be set, but destructive
    ///                 properties are still allowed to be replaced, as this
    ///                 should be invisible to the user.
	bool setValue(as_object& this_ptr, const as_value &value) const;

	/// Is this a getter/setter property?
	bool isGetterSetter() const {
        return _bound.type() == typeid(GetterSetter);
    }

	/// Clear visibility flags
	void clearVisible(int swfVersion) { _flags.clear_visible(swfVersion); }

    /// The name-namespace pair (ObjectURI) of this Property
    const ObjectURI& uri() const {
        return _uri;
    }

	/// Mark this property as being reachable (for the GC)
	void setReachable() const {
        return boost::apply_visitor(SetReachable(), _bound);
    }

private:

	// Store the various types of things that can be held.
	typedef boost::variant<as_value, GetterSetter> BoundType;

    /// The value of the property.
	mutable BoundType _bound;
	
    /// The property identifier (name).
    ObjectURI _uri;

	/// Properties flags
	mutable PropFlags _flags;

	// If true, as soon as getValue has been invoked once, the
	// returned value becomes a fixed return (though it can be
	// overwritten if not readOnly)
	mutable bool _destructive;

};
	
/// is this a read-only member ?
inline bool
readOnly(const Property& prop) {
    return prop.getFlags().test<PropFlags::readOnly>();
}

/// Is this member supposed to be visible by a VM of given version ?
inline bool
visible(const Property& prop, int version) {
    return prop.getFlags().get_visible(version);
}

} // namespace gnash

#endif // GNASH_PROPERTY_H