~ubuntu-branches/ubuntu/karmic/gnash/karmic

« back to all changes in this revision

Viewing changes to server/as_object.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2008-10-13 14:29:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081013142949-f6qdvnu4mn05ltdc
Tags: 0.8.4~~bzr9980-0ubuntu1
* new upstream release 0.8.4 (LP: #240325)
* ship new lib usr/lib/gnash/libmozsdk.so.* in mozilla-plugin-gnash
  - update debian/mozilla-plugin-gnash.install
* ship new lib usr/lib/gnash/libgnashnet.so.* in gnash-common
  - update debian/gnash-common.install
* add basic debian/build_head script to build latest CVS head packages.
  - add debian/build_head
* new sound architecture requires build depend on libsdl1.2-dev
  - update debian/control
* head build script now has been completely migrated to bzr (upstream +
  ubuntu)
  - update debian/build_head
* disable kde gui until klash/qt4 has been fixed; keep kde packages as empty
  packages for now.
  - update debian/rules
  - debian/klash.install
  - debian/klash.links
  - debian/klash.manpages
  - debian/konqueror-plugin-gnash.install
* drop libkonq5-dev build dependency accordingly
  - update debian/control
* don't install headers manually anymore. gnash doesnt provide a -dev
  package after all
  - update debian/rules
* update libs installed in gnash-common; libgnashserver-*.so is not available
  anymore (removed); in turn we add the new libgnashcore-*.so
  - update debian/gnash-common.install
* use -Os for optimization and properly pass CXXFLAGS=$(CFLAGS) to configure
  - update debian/rules
* touch firefox .autoreg in postinst of mozilla plugin
  - update debian/mozilla-plugin-gnash.postinst
* link gnash in ubufox plugins directory for the plugin alternative switcher
  - add debian/mozilla-plugin-gnash.links
* suggest ubufox accordingly
  - update debian/control
* add new required build-depends on libgif-dev
  - update debian/control
* add Xb-Npp-Description and Xb-Npp-File as new plugin database meta data
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// as_object.cpp:  ActionScript Object class and its properties, for Gnash.
2
 
// 
3
 
//   Copyright (C) 2005, 2006, 2007, 2008 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
 
#ifdef HAVE_CONFIG_H
20
 
#include "gnashconfig.h"
21
 
#endif
22
 
 
23
 
#include "log.h"
24
 
 
25
 
#include "as_object.h"
26
 
#include "as_function.h"
27
 
#include "as_environment.h" // for enumerateProperties
28
 
#include "Property.h" // for findGetterSetter
29
 
#include "VM.h"
30
 
#include "GnashException.h"
31
 
#include "fn_call.h" // for generic methods
32
 
#include "Object.h" // for getObjectInterface
33
 
#include "action.h" // for call_method
34
 
#include "array.h" // for setPropFlags
35
 
 
36
 
#include <set>
37
 
#include <string>
38
 
#include <boost/algorithm/string/case_conv.hpp>
39
 
#include <utility> // for std::pair
40
 
#include "namedStrings.h"
41
 
#include "asName.h"
42
 
#include "asClass.h"
43
 
 
44
 
// Anonymous namespace used for module-static defs
45
 
namespace {
46
 
 
47
 
using namespace gnash;
48
 
 
49
 
// A PropertyList visitor copying properties to an object
50
 
class PropsCopier {
51
 
 
52
 
        as_object& _tgt;
53
 
 
54
 
public:
55
 
 
56
 
        /// \brief
57
 
        /// Initialize a PropsCopier instance associating it
58
 
        /// with a target object (an object whose members has to be set)
59
 
        ///
60
 
        PropsCopier(as_object& tgt)
61
 
                :
62
 
                _tgt(tgt)
63
 
        {}
64
 
 
65
 
        /// \brief
66
 
        /// Use the set_member function to properly set *inherited* properties
67
 
        /// of the given target object
68
 
        ///
69
 
        void operator() (string_table::key name, const as_value& val)
70
 
        {
71
 
                if (name == NSV::PROP_uuPROTOuu) return;
72
 
                //log_debug(_("Setting member '%s' to value '%s'"), name.c_str(), val.to_debug_string().c_str());
73
 
                _tgt.set_member(name, val);
74
 
        }
75
 
};
76
 
 
77
 
} // end of anonymous namespace
78
 
 
79
 
 
80
 
namespace gnash {
81
 
 
82
 
bool
83
 
as_object::add_property(const std::string& name, as_function& getter,
84
 
                as_function& setter)
85
 
{
86
 
        string_table &st = _vm.getStringTable();
87
 
        return _members.addGetterSetter(st.find(PROPNAME(name)), getter, setter);
88
 
}
89
 
 
90
 
/*protected*/
91
 
bool
92
 
as_object::get_member_default(string_table::key name, as_value* val,
93
 
        string_table::key nsname)
94
 
{
95
 
        assert(val);
96
 
 
97
 
        Property* prop = findProperty(name, nsname);
98
 
        if (!prop)
99
 
                return false;
100
 
 
101
 
        try 
102
 
        {
103
 
                *val = prop->getValue(*this);
104
 
                return true;
105
 
        }
106
 
        catch (ActionLimitException& exc)
107
 
        {
108
 
                // will be logged by outer catcher
109
 
                throw;
110
 
        }
111
 
        catch (ActionException& exc)
112
 
        {
113
 
                // TODO: check if this should be an 'as' error.. (log_aserror)
114
 
                log_error(_("Caught exception: %s"), exc.what());
115
 
                return false;
116
 
        }
117
 
}
118
 
 
119
 
Property*
120
 
as_object::getByIndex(int index)
121
 
{
122
 
        // The low byte is used to contain the depth of the property.
123
 
        unsigned char depth = index & 0xFF;
124
 
        index /= 256; // Signed
125
 
        as_object *obj = this;
126
 
        while (depth--)
127
 
        {
128
 
                obj = obj->get_prototype().get();
129
 
                if (!obj)
130
 
                        return NULL;
131
 
        }
132
 
 
133
 
        return const_cast<Property *>(obj->_members.getPropertyByOrder(index));
134
 
}
135
 
 
136
 
as_object*
137
 
as_object::get_super()
138
 
{
139
 
        static bool getting = false;
140
 
        as_object *owner = NULL;
141
 
 
142
 
        Property *p = NULL;
143
 
 
144
 
        if (getting)
145
 
                return NULL;
146
 
 
147
 
        getting = true;
148
 
 
149
 
        // Super is this.__proto__.__constructor__.prototype
150
 
        as_object *proto = get_prototype().get();
151
 
        if (!proto)
152
 
        {
153
 
                getting = false;
154
 
                return NULL;
155
 
        }
156
 
 
157
 
        // If an object is its own prototype, we stop looking.
158
 
        if (proto == this)
159
 
        {
160
 
                getting = false;
161
 
                return this;
162
 
        }
163
 
 
164
 
        p = proto->findProperty(NSV::PROP_uuCONSTRUCTORuu, 0, &owner);
165
 
        if (!p)
166
 
        {
167
 
                getting = false;
168
 
                return NULL;
169
 
        }
170
 
 
171
 
        as_value ctor = p->getValue(*owner);
172
 
        as_object *ctor_obj = ctor.to_object().get();
173
 
        if (!ctor_obj)
174
 
        {
175
 
                getting = false;
176
 
                return NULL;
177
 
        }
178
 
 
179
 
        p = ctor_obj->findProperty(NSV::PROP_PROTOTYPE, 0, &owner);
180
 
        if (!p)
181
 
        {
182
 
                getting = false;
183
 
                return NULL;
184
 
        }
185
 
 
186
 
        as_value ctor_proto = p->getValue(*owner);
187
 
        as_object *super = ctor_proto.to_object().get();
188
 
 
189
 
        getting = false;
190
 
 
191
 
        return super;
192
 
}
193
 
 
194
 
as_function*
195
 
as_object::get_constructor()
196
 
{
197
 
        // TODO: Implement
198
 
        return NULL;
199
 
}
200
 
 
201
 
int
202
 
as_object::nextIndex(int index, as_object **owner)
203
 
{
204
 
skip_duplicates:
205
 
        unsigned char depth = index & 0xFF;
206
 
        unsigned char i = depth;
207
 
        index /= 256; // Signed
208
 
        as_object *obj = this;
209
 
        while (i--)
210
 
        {
211
 
                obj = obj->get_prototype().get();
212
 
                if (!obj)
213
 
                        return 0;
214
 
        }
215
 
        
216
 
        const Property *p = obj->_members.getOrderAfter(index);
217
 
        if (!p)
218
 
        {
219
 
                obj = obj->get_prototype().get();
220
 
                if (!obj)
221
 
                        return 0;
222
 
                p = obj->_members.getOrderAfter(0);
223
 
                ++depth;
224
 
        }
225
 
        if (p)
226
 
        {
227
 
                if (findProperty(p->getName(), p->getNamespace()) != p)
228
 
                {
229
 
                        index = p->getOrder() * 256 | depth;
230
 
                        goto skip_duplicates; // Faster than recursion.
231
 
                }
232
 
                if (owner)
233
 
                        *owner = obj;
234
 
                return p->getOrder() * 256 | depth;
235
 
        }
236
 
        return 0;
237
 
}
238
 
 
239
 
/*private*/
240
 
Property*
241
 
as_object::findProperty(string_table::key key, string_table::key nsname, 
242
 
        as_object **owner)
243
 
{
244
 
        // don't enter an infinite loop looking for __proto__ ...
245
 
        if (key == NSV::PROP_uuPROTOuu && !nsname)
246
 
        {
247
 
                if (owner != NULL)
248
 
                        *owner = this;
249
 
                return _members.getProperty(key, nsname);
250
 
        }
251
 
 
252
 
        // keep track of visited objects, avoid infinite loops.
253
 
        std::set<as_object*> visited;
254
 
 
255
 
        int swfVersion = _vm.getSWFVersion();
256
 
        int i = 0;
257
 
 
258
 
        boost::intrusive_ptr<as_object> obj = this;
259
 
        while (obj && visited.insert(obj.get()).second)
260
 
        {
261
 
                ++i;
262
 
                if ((i > 255 && swfVersion == 5) || i > 257)
263
 
                        throw ActionLimitException("Lookup depth exceeded.");
264
 
 
265
 
                Property* prop = obj->_members.getProperty(key);
266
 
                if (prop && prop->isVisible(swfVersion) )
267
 
                {
268
 
                        if (owner != NULL)
269
 
                                *owner = obj.get();
270
 
                        return prop;
271
 
                }
272
 
                else
273
 
                        obj = obj->get_prototype();
274
 
        }
275
 
 
276
 
        // No Property found
277
 
        return NULL;
278
 
}
279
 
 
280
 
Property*
281
 
as_object::findUpdatableProperty(string_table::key key, string_table::key nsname)
282
 
{
283
 
        int swfVersion = _vm.getSWFVersion();
284
 
 
285
 
        Property* prop = _members.getProperty(key, nsname);
286
 
        // 
287
 
        // We won't scan the inheritance chain if we find a member,
288
 
        // even if invisible.
289
 
        // 
290
 
        if ( prop )     return prop; 
291
 
 
292
 
        // don't enter an infinite loop looking for __proto__ ...
293
 
        if (key == NSV::PROP_uuPROTOuu) return NULL;
294
 
 
295
 
        std::set<as_object*> visited;
296
 
        visited.insert(this);
297
 
 
298
 
        int i = 0;
299
 
 
300
 
        boost::intrusive_ptr<as_object> obj = get_prototype();
301
 
        while (obj && visited.insert(obj.get()).second)
302
 
        {
303
 
                ++i;
304
 
                if ((i > 255 && swfVersion == 5) || i > 257)
305
 
                        throw ActionLimitException("Property lookup depth exceeded.");
306
 
 
307
 
                Property* p = obj->_members.getProperty(key, nsname);
308
 
                if (p && (p->isGetterSetter() | p->isStatic()) && p->isVisible(swfVersion))
309
 
                {
310
 
                        return p; // What should we do if this is not a getter/setter ?
311
 
                }
312
 
                obj = obj->get_prototype();
313
 
        }
314
 
        return NULL;
315
 
}
316
 
 
317
 
/*protected*/
318
 
void
319
 
as_object::set_prototype(boost::intrusive_ptr<as_object> proto, int flags)
320
 
{
321
 
        static string_table::key key = NSV::PROP_uuPROTOuu;
322
 
 
323
 
        // TODO: check what happens if __proto__ is set as a user-defined getter/setter
324
 
        if (_members.setValue(key, as_value(proto.get()), *this, 0) )
325
 
        {
326
 
                // TODO: optimize this, don't scan again !
327
 
                _members.setFlags(key, flags, 0);
328
 
        }
329
 
}
330
 
 
331
 
void
332
 
as_object::reserveSlot(string_table::key name, string_table::key nsId,
333
 
        unsigned short slotId)
334
 
{
335
 
        _members.reserveSlot(name, nsId, slotId);
336
 
}
337
 
 
338
 
// Handles read_only and static properties properly.
339
 
void
340
 
as_object::set_member_default(string_table::key key, const as_value& val,
341
 
        string_table::key nsname)
342
 
{
343
 
        //log_debug(_("set_member_default(%s)"), key.c_str());
344
 
        Property* prop = findUpdatableProperty(key, nsname);
345
 
        if (prop)
346
 
        {
347
 
                if (prop->isReadOnly())
348
 
                {
349
 
                        IF_VERBOSE_ASCODING_ERRORS(log_aserror(_(""
350
 
                                "Attempt to set read-only property '%s'"),
351
 
                                _vm.getStringTable().value(key).c_str()););
352
 
                        return;
353
 
                }
354
 
 
355
 
                try
356
 
                {
357
 
                        prop->setValue(*this, val);
358
 
                        prop->clearVisible(_vm.getSWFVersion());
359
 
                        return;
360
 
                }
361
 
                catch (ActionException& exc)
362
 
                {
363
 
                        log_aserror(_("%s: Exception %s. Will create a new member"),
364
 
                                _vm.getStringTable().value(key).c_str(), exc.what());
365
 
                }
366
 
 
367
 
                return;
368
 
        }
369
 
 
370
 
        // Else, add new property...
371
 
 
372
 
        // Property does not exist, so it won't be read-only. Set it.
373
 
        if (!_members.setValue(key, const_cast<as_value&>(val), *this, nsname))
374
 
        {
375
 
                IF_VERBOSE_ASCODING_ERRORS(
376
 
                        log_aserror(_("Unknown failure in setting property '%s' on "
377
 
                        "object '%p'"), _vm.getStringTable().value(key).c_str(),
378
 
                        (void*) this););
379
 
        }
380
 
}
381
 
 
382
 
std::pair<bool,bool>
383
 
as_object::update_member(string_table::key key, const as_value& val,
384
 
        string_table::key nsname)
385
 
{
386
 
        std::pair<bool,bool> ret; // first is found, second is updated
387
 
 
388
 
        //log_debug(_("set_member_default(%s)"), key.c_str());
389
 
        Property* prop = findUpdatableProperty(key, nsname);
390
 
        if (prop)
391
 
        {
392
 
                if (prop->isReadOnly())
393
 
                {
394
 
                        IF_VERBOSE_ASCODING_ERRORS(log_aserror(_(""
395
 
                                "Attempt to set read-only property '%s'"),
396
 
                                _vm.getStringTable().value(key).c_str()););
397
 
                        return std::make_pair(true, false);
398
 
                }
399
 
 
400
 
                try
401
 
                {
402
 
                        prop->setValue(*this, val);
403
 
                        return std::make_pair(true, true);
404
 
                }
405
 
                catch (ActionException& exc)
406
 
                {
407
 
                        log_debug(_("%s: Exception %s. Will create a new member"),
408
 
                                _vm.getStringTable().value(key).c_str(), exc.what());
409
 
                }
410
 
 
411
 
                return std::make_pair(true, false);
412
 
        }
413
 
 
414
 
        return std::make_pair(false, false);
415
 
 
416
 
}
417
 
 
418
 
void
419
 
as_object::init_member(const std::string& key1, const as_value& val, int flags,
420
 
        string_table::key nsname)
421
 
{
422
 
        init_member(_vm.getStringTable().find(PROPNAME(key1)), val, flags, nsname);
423
 
}
424
 
 
425
 
void
426
 
as_object::init_member(string_table::key key, const as_value& val, int flags,
427
 
        string_table::key nsname, int order)
428
 
{
429
 
        //log_debug(_("Initializing member %s for object %p"), _vm.getStringTable().value(key).c_str(), (void*) this);
430
 
 
431
 
        if (order >= 0 && !_members.
432
 
                reserveSlot(static_cast<unsigned short>(order), key, nsname))
433
 
        {
434
 
                log_error(_("Attempt to set a slot for either a slot or a property "
435
 
                        "which already exists."));
436
 
                return;
437
 
        }
438
 
                
439
 
        // Set (or create) a SimpleProperty 
440
 
        if (! _members.setValue(key, const_cast<as_value&>(val), *this, nsname) )
441
 
        {
442
 
                log_error(_("Attempt to initialize read-only property ``%s''"
443
 
                        " on object ``%p'' twice"),
444
 
                        _vm.getStringTable().value(key).c_str(), (void*)this);
445
 
                // We shouldn't attempt to initialize a member twice, should we ?
446
 
                abort();
447
 
        }
448
 
        // TODO: optimize this, don't scan again !
449
 
        _members.setFlags(key, flags, nsname);
450
 
}
451
 
 
452
 
void
453
 
as_object::init_property(const std::string& key, as_function& getter,
454
 
                as_function& setter, int flags, string_table::key nsname)
455
 
{
456
 
        string_table::key k = _vm.getStringTable().find(PROPNAME(key));
457
 
        init_property(k, getter, setter, flags, nsname);
458
 
 
459
 
}
460
 
 
461
 
void
462
 
as_object::init_property(string_table::key key, as_function& getter,
463
 
                as_function& setter, int flags, string_table::key nsname)
464
 
{
465
 
        bool success;
466
 
        success = _members.addGetterSetter(key, getter, setter, nsname);
467
 
 
468
 
        // We shouldn't attempt to initialize a property twice, should we ?
469
 
        assert(success);
470
 
 
471
 
        //log_debug(_("Initialized property '%s'"), name.c_str());
472
 
 
473
 
        // TODO: optimize this, don't scan again !
474
 
        _members.setFlags(key, flags, nsname);
475
 
 
476
 
}
477
 
 
478
 
bool
479
 
as_object::init_destructive_property(string_table::key key, as_function& getter,
480
 
        as_function& setter, int flags, string_table::key nsname)
481
 
{
482
 
        bool success;
483
 
 
484
 
        // No case check, since we've already got the key.
485
 
        success = _members.addDestructiveGetterSetter(key, getter, setter, nsname);
486
 
        _members.setFlags(key, flags, nsname);
487
 
        return success;
488
 
}
489
 
 
490
 
void
491
 
as_object::init_readonly_property(const std::string& key, as_function& getter,
492
 
        int initflags, string_table::key nsname)
493
 
{
494
 
        string_table::key k = _vm.getStringTable().find(PROPNAME(key));
495
 
 
496
 
        init_property(k, getter, getter, initflags | as_prop_flags::readOnly
497
 
                | as_prop_flags::isProtected, nsname);
498
 
        assert(_members.getProperty(k, nsname));
499
 
}
500
 
 
501
 
std::string
502
 
as_object::asPropName(string_table::key name)
503
 
{
504
 
        std::string orig = _vm.getStringTable().value(name);
505
 
 
506
 
        return PROPNAME(orig); // why is PROPNAME needed here ?
507
 
}
508
 
 
509
 
 
510
 
bool
511
 
as_object::set_member_flags(string_table::key name,
512
 
                int setTrue, int setFalse, string_table::key nsname)
513
 
{
514
 
        return _members.setFlags(name, setTrue, setFalse, nsname);
515
 
}
516
 
 
517
 
void
518
 
as_object::add_interface(as_object* obj)
519
 
{
520
 
        assert(obj);
521
 
 
522
 
        if (std::find(mInterfaces.begin(), mInterfaces.end(), obj) == mInterfaces.end())
523
 
                mInterfaces.push_back(obj);
524
 
}
525
 
 
526
 
bool
527
 
as_object::instanceOf(as_function* ctor)
528
 
{
529
 
        if (this == ctor->getPrototype())
530
 
                return true;
531
 
 
532
 
        if (!mInterfaces.empty())
533
 
        {
534
 
                // TODO: Make this work.
535
 
                if (std::find(mInterfaces.begin(), mInterfaces.end(), ctor->getPrototype()) != mInterfaces.end())
536
 
                {
537
 
                        return true;
538
 
                }
539
 
        }
540
 
 
541
 
        as_object *proto = this->get_prototype().get();
542
 
        if (proto)
543
 
                return proto->instanceOf(ctor);
544
 
        else
545
 
                return false;
546
 
}
547
 
 
548
 
bool
549
 
as_object::prototypeOf(as_object& instance)
550
 
{
551
 
        boost::intrusive_ptr<as_object> obj = &instance;
552
 
 
553
 
        std::set< as_object* > visited;
554
 
 
555
 
        while (obj && visited.insert(obj.get()).second )
556
 
        {
557
 
                if ( obj->get_prototype() == this ) return true;
558
 
                obj = obj->get_prototype(); 
559
 
        }
560
 
 
561
 
        // See actionscript.all/Inheritance.as for a way to trigger this
562
 
        IF_VERBOSE_ASCODING_ERRORS(
563
 
        if ( obj ) log_aserror(_("Circular inheritance chain detected during isPrototypeOf call"));
564
 
        );
565
 
 
566
 
        return false;
567
 
}
568
 
 
569
 
void
570
 
as_object::dump_members() 
571
 
{
572
 
        log_debug(_(SIZET_FMT " members of object %p follow"),
573
 
                _members.size(), (const void*)this);
574
 
        _members.dump(*this);
575
 
}
576
 
 
577
 
void
578
 
as_object::dump_members(std::map<std::string, as_value>& to)
579
 
{
580
 
        _members.dump(*this, to);
581
 
}
582
 
 
583
 
class FlagsSetterVisitor {
584
 
        string_table& _st;
585
 
        PropertyList& _pl;
586
 
        int _setTrue;
587
 
        int _setFalse;
588
 
public:
589
 
        FlagsSetterVisitor(string_table& st, PropertyList& pl, int setTrue, int setFalse)
590
 
                :
591
 
                _st(st),
592
 
                _pl(pl),
593
 
                _setTrue(setTrue),
594
 
                _setFalse(setFalse)
595
 
        {}
596
 
 
597
 
        void visit(as_value& v)
598
 
        {
599
 
                string_table::key key = _st.find(v.to_string());
600
 
                _pl.setFlags(key, _setTrue, _setFalse);
601
 
        }
602
 
};
603
 
 
604
 
void
605
 
as_object::setPropFlags(as_value& props_val, int set_false, int set_true)
606
 
{
607
 
        if (props_val.is_string())
608
 
        {
609
 
                std::string propstr = PROPNAME(props_val.to_string()); 
610
 
 
611
 
                for(;;)
612
 
                {
613
 
                        std::string prop;
614
 
                        size_t next_comma=propstr.find(",");
615
 
                        if ( next_comma == std::string::npos )
616
 
                        {
617
 
                                prop=propstr;
618
 
                        } 
619
 
                        else
620
 
                        {
621
 
                                prop=propstr.substr(0,next_comma);
622
 
                                propstr=propstr.substr(next_comma+1);
623
 
                        }
624
 
 
625
 
                        // set_member_flags will take care of case conversion
626
 
                        if (!set_member_flags(_vm.getStringTable().find(prop), set_true, set_false) )
627
 
                        {
628
 
                                IF_VERBOSE_ASCODING_ERRORS(
629
 
                                log_aserror(_("Can't set propflags on object "
630
 
                                        "property %s "
631
 
                                        "(either not found or protected)"),
632
 
                                        prop.c_str());
633
 
                                );
634
 
                        }
635
 
 
636
 
                        if ( next_comma == std::string::npos )
637
 
                        {
638
 
                                break;
639
 
                        }
640
 
                }
641
 
                return;
642
 
        }
643
 
 
644
 
        // Evan: it seems that if set_true == 0 and set_false == 0,
645
 
        // this function acts as if the parameters were (object, null, 0x1, 0)
646
 
#if 0 // bullshit, see actionscript.all/Global.as
647
 
        if (set_false == 0 && set_true == 0)
648
 
        {
649
 
            props_val.set_null();
650
 
            set_false = 0;
651
 
            set_true = 0x1;
652
 
        }
653
 
#endif
654
 
 
655
 
        if (props_val.is_null())
656
 
        {
657
 
                // Take all the members of the object
658
 
                //std::pair<size_t, size_t> result = 
659
 
                _members.setFlagsAll(set_true, set_false);
660
 
 
661
 
                // Are we sure we need to descend to __proto__ ?
662
 
                // should we recurse then ?
663
 
#if 0
664
 
                if (m_prototype)
665
 
                {
666
 
                        m_prototype->_members.setFlagsAll(set_true, set_false);
667
 
                }
668
 
#endif
669
 
                return;
670
 
        }
671
 
 
672
 
        boost::intrusive_ptr<as_object> props = props_val.to_object();
673
 
        as_array_object* ary = dynamic_cast<as_array_object*>(props.get());
674
 
        if ( ! ary )
675
 
        {
676
 
                IF_VERBOSE_ASCODING_ERRORS(
677
 
                log_aserror(_("Invalid call to AsSetPropFlags: "
678
 
                        "invalid second argument %s "
679
 
                        "(expected string, null or an array)"),
680
 
                        props_val.to_debug_string().c_str());
681
 
                );
682
 
                return;
683
 
        }
684
 
 
685
 
        // The passed argument has to be considered an array
686
 
        //std::pair<size_t, size_t> result = 
687
 
        FlagsSetterVisitor visitor(getVM().getStringTable(), _members, set_true, set_false);
688
 
        ary->visitAll(visitor);
689
 
        //_members.setFlagsAll(props->_members, set_true, set_false);
690
 
}
691
 
 
692
 
 
693
 
void
694
 
as_object::copyProperties(const as_object& o)
695
 
{
696
 
        PropsCopier copier(*this);
697
 
 
698
 
        // TODO: check if non-visible properties should be also copied !
699
 
        o.visitPropertyValues(copier);
700
 
}
701
 
 
702
 
void
703
 
as_object::enumerateProperties(as_environment& env) const
704
 
{
705
 
        assert( env.top(0).is_null() );
706
 
 
707
 
        enumerateNonProperties(env);
708
 
 
709
 
        // this set will keep track of visited objects,
710
 
        // to avoid infinite loops
711
 
        std::set< as_object* > visited;
712
 
        PropertyList::propNameSet named;
713
 
 
714
 
        boost::intrusive_ptr<as_object> obj = const_cast<as_object*>(this);
715
 
        while ( obj && visited.insert(obj.get()).second )
716
 
        {
717
 
                obj->_members.enumerateKeys(env, named);
718
 
                obj = obj->get_prototype();
719
 
        }
720
 
 
721
 
        // This happens always since top object in hierarchy
722
 
        // is always Object, which in turn derives from itself
723
 
        //if ( obj ) log_error(_("prototype loop during Enumeration"));
724
 
}
725
 
 
726
 
void
727
 
as_object::enumerateProperties(std::map<std::string, std::string>& to)
728
 
{
729
 
 
730
 
        // this set will keep track of visited objects,
731
 
        // to avoid infinite loops
732
 
        std::set< as_object* > visited;
733
 
 
734
 
        boost::intrusive_ptr<as_object> obj = this;
735
 
        while ( obj && visited.insert(obj.get()).second )
736
 
        {
737
 
                obj->_members.enumerateKeyValue(*this, to);
738
 
                obj = obj->get_prototype();
739
 
        }
740
 
 
741
 
}
742
 
 
743
 
as_object::as_object()
744
 
        :
745
 
        _members(),
746
 
        _vm(VM::get())
747
 
        //, m_prototype(NULL)
748
 
{
749
 
}
750
 
 
751
 
as_object::as_object(as_object* proto)
752
 
        :
753
 
        _members(),
754
 
        _vm(VM::get())
755
 
        //, m_prototype(proto)
756
 
{
757
 
        init_member("__proto__", as_value(proto));
758
 
}
759
 
 
760
 
as_object::as_object(boost::intrusive_ptr<as_object> proto)
761
 
        :
762
 
        _members(),
763
 
        _vm(VM::get())
764
 
        //, m_prototype(proto)
765
 
{
766
 
        //set_prototype(proto);
767
 
        init_member("__proto__", as_value(proto));
768
 
}
769
 
 
770
 
as_object::as_object(const as_object& other)
771
 
        :
772
 
#ifndef GNASH_USE_GC
773
 
        ref_counted(),
774
 
#else
775
 
        GcResource(), 
776
 
#endif
777
 
        _members(other._members),
778
 
        _vm(VM::get())
779
 
        //, m_prototype(other.m_prototype) // done by _members copy
780
 
{
781
 
}
782
 
 
783
 
std::pair<bool,bool>
784
 
as_object::delProperty(string_table::key name, string_table::key nsname)
785
 
{
786
 
        return _members.delProperty(name, nsname);
787
 
}
788
 
 
789
 
Property*
790
 
as_object::getOwnProperty(string_table::key key, string_table::key nsname)
791
 
{
792
 
        return _members.getProperty(key, nsname);
793
 
}
794
 
 
795
 
as_value
796
 
as_object::tostring_method(const fn_call& fn)
797
 
{
798
 
        boost::intrusive_ptr<as_object> obj = fn.this_ptr;
799
 
 
800
 
        std::string text_val = obj->get_text_value();
801
 
        return as_value(text_val);
802
 
}
803
 
 
804
 
as_value
805
 
as_object::valueof_method(const fn_call& fn)
806
 
{
807
 
        boost::intrusive_ptr<as_object> obj = fn.this_ptr;
808
 
 
809
 
        return obj->get_primitive_value();
810
 
}
811
 
 
812
 
boost::intrusive_ptr<as_object>
813
 
as_object::get_prototype()
814
 
{
815
 
        static string_table::key key = NSV::PROP_uuPROTOuu;
816
 
 
817
 
        int swfVersion = _vm.getSWFVersion();
818
 
 
819
 
        boost::intrusive_ptr<as_object> nullRet = NULL;
820
 
 
821
 
        Property* prop = _members.getProperty(key);
822
 
        if ( ! prop ) return nullRet;
823
 
        if ( ! prop->isVisible(swfVersion) ) return nullRet;
824
 
 
825
 
        as_value tmp = prop->getValue(*this);
826
 
 
827
 
        return tmp.to_object();
828
 
}
829
 
 
830
 
bool
831
 
as_object::on_event(const event_id& id )
832
 
{
833
 
        as_value event_handler;
834
 
 
835
 
        if (get_member(id.get_function_key(), &event_handler) )
836
 
        {
837
 
                call_method(event_handler, NULL, this, 0, 0);
838
 
                return true;
839
 
        }
840
 
 
841
 
        return false;
842
 
}
843
 
 
844
 
as_value
845
 
as_object::getMember(string_table::key name, string_table::key nsname)
846
 
{
847
 
        as_value ret;
848
 
        get_member(name, &ret, nsname);
849
 
        //get_member(PROPNAME(name), &ret);
850
 
        return ret;
851
 
}
852
 
 
853
 
as_value
854
 
as_object::callMethod(string_table::key methodName)
855
 
{
856
 
        as_value ret;
857
 
        as_value method;
858
 
 
859
 
        if (! get_member(methodName, &method))
860
 
        {
861
 
                return ret;
862
 
        }
863
 
 
864
 
        as_environment env;
865
 
 
866
 
        return call_method(method, &env, this, 0, env.stack_size());
867
 
}
868
 
 
869
 
as_value
870
 
as_object::callMethod(string_table::key methodName, const as_value& arg0)
871
 
{
872
 
        as_value ret;
873
 
        as_value method;
874
 
 
875
 
        if (!get_member(methodName, &method))
876
 
        {
877
 
                return ret;
878
 
        }
879
 
 
880
 
        as_environment env;
881
 
        env.push(arg0);
882
 
 
883
 
        ret = call_method(method, &env, this, 1, env.stack_size()-1);
884
 
 
885
 
        env.drop(1);
886
 
 
887
 
        return ret;
888
 
}
889
 
 
890
 
as_value
891
 
as_object::callMethod(string_table::key methodName,
892
 
        const as_value& arg0, const as_value& arg1)
893
 
{
894
 
        as_value ret;
895
 
        as_value method;
896
 
 
897
 
        if (! get_member(methodName, &method))
898
 
        {
899
 
                return ret;
900
 
        }
901
 
 
902
 
        as_environment env;
903
 
 
904
 
#ifndef NDEBUG
905
 
        size_t origStackSize = env.stack_size();
906
 
#endif
907
 
 
908
 
        env.push(arg1);
909
 
        env.push(arg0);
910
 
 
911
 
        ret = call_method(method, &env, this, 2, env.stack_size()-1);
912
 
 
913
 
        env.drop(2);
914
 
 
915
 
#ifndef NDEBUG
916
 
        assert(origStackSize == env.stack_size());
917
 
#endif
918
 
 
919
 
        return ret;
920
 
}
921
 
 
922
 
as_value
923
 
as_object::callMethod(string_table::key methodName,
924
 
        const as_value& arg0, const as_value& arg1, const as_value& arg2)
925
 
{
926
 
        as_value ret;
927
 
        as_value method;
928
 
 
929
 
        if (! get_member(methodName, &method))
930
 
        {
931
 
                return ret;
932
 
        }
933
 
 
934
 
        as_environment env;
935
 
 
936
 
#ifndef NDEBUG
937
 
        size_t origStackSize = env.stack_size();
938
 
#endif
939
 
 
940
 
        env.push(arg2);
941
 
        env.push(arg1);
942
 
        env.push(arg0);
943
 
 
944
 
        ret = call_method(method, &env, this, 3, env.stack_size()-1);
945
 
 
946
 
        env.drop(3);
947
 
 
948
 
#ifndef NDEBUG
949
 
        assert(origStackSize == env.stack_size());
950
 
#endif
951
 
 
952
 
        return ret;
953
 
}
954
 
 
955
 
as_value
956
 
as_object::callMethod(string_table::key methodName,
957
 
        const as_value& arg0, const as_value& arg1,
958
 
        const as_value& arg2, const as_value& arg3)
959
 
{
960
 
        as_value ret;
961
 
        as_value method;
962
 
 
963
 
        if (! get_member(methodName, &method))
964
 
        {
965
 
                return ret;
966
 
        }
967
 
 
968
 
        as_environment env;
969
 
 
970
 
#ifndef NDEBUG
971
 
        size_t origStackSize = env.stack_size();
972
 
#endif
973
 
 
974
 
        env.push(arg3);
975
 
        env.push(arg2);
976
 
        env.push(arg1);
977
 
        env.push(arg0);
978
 
 
979
 
        ret = call_method(method, &env, this, 4, env.stack_size()-1);
980
 
 
981
 
        env.drop(4);
982
 
 
983
 
#ifndef NDEBUG
984
 
        assert(origStackSize == env.stack_size());
985
 
#endif
986
 
 
987
 
        return ret;
988
 
}
989
 
 
990
 
as_object*
991
 
as_object::get_path_element(string_table::key key)
992
 
{
993
 
//#define DEBUG_TARGET_FINDING 1
994
 
 
995
 
        as_value tmp;
996
 
        if ( ! get_member(key, &tmp ) )
997
 
        {
998
 
#ifdef DEBUG_TARGET_FINDING 
999
 
                log_debug("Member %s not found in object %p",
1000
 
                        _vm.getStringTable().value(key).c_str(),
1001
 
                        (void*)this);
1002
 
#endif
1003
 
                return NULL;
1004
 
        }
1005
 
        if ( ! tmp.is_object() )
1006
 
        {
1007
 
#ifdef DEBUG_TARGET_FINDING 
1008
 
                log_debug("Member %s of object %p is not an object (%s)",
1009
 
                        _vm.getStringTable().value(key).c_str(), (void*)this,
1010
 
                        tmp.to_debug_string().c_str());
1011
 
#endif
1012
 
                return NULL;
1013
 
        }
1014
 
 
1015
 
        return tmp.to_object().get();
1016
 
}
1017
 
 
1018
 
void
1019
 
as_object::getURLEncodedVars(std::string& data)
1020
 
{
1021
 
    typedef std::map<std::string, std::string> PropMap;
1022
 
    PropMap props;
1023
 
    enumerateProperties(props);
1024
 
 
1025
 
    std::string del;
1026
 
    data.clear();
1027
 
    
1028
 
    for (PropMap::const_iterator i=props.begin(), e=props.end(); i!=e; ++i)
1029
 
    {
1030
 
      std::string name = i->first;
1031
 
      std::string value = i->second;
1032
 
      if ( ! name.empty() && name[0] == '$' ) continue; // see bug #22006
1033
 
      URL::encode(value);
1034
 
      
1035
 
      data += del + name + "=" + value;
1036
 
      
1037
 
      del = "&";
1038
 
        
1039
 
    }
1040
 
    
1041
 
}
1042
 
 
1043
 
 
1044
 
} // end of gnash namespace