1 // as_function.cpp: ActionScript Functions, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
21 #include "as_function.h"
25 #include "smart_ptr.h"
29 #include "GnashException.h"
30 #include "Global_as.h"
31 #include "namedStrings.h"
35 as_function::as_function(Global_as
& gl
)
42 as_function::stringValue() const
44 return "[type Function]";
48 constructInstance(as_function
& ctor
, const as_environment
& env
,
51 Global_as
& gl
= getGlobal(ctor
);
53 // Create an empty object, with a ref to the constructor's prototype.
54 // The function's prototype property always becomes the new object's
55 // __proto__ member, regardless of whether it is an object and regardless
57 as_object
* newobj
= new as_object(gl
);
58 Property
* proto
= ctor
.getOwnProperty(NSV::PROP_PROTOTYPE
);
59 if (proto
) newobj
->set_prototype(proto
->getValue(ctor
));
61 return ctor
.construct(*newobj
, env
, args
);
65 as_function::construct(as_object
& newobj
, const as_environment
& env
,
68 const int swfversion
= getSWFVersion(env
);
70 // Add a __constructor__ member to the new object visible from version 6.
71 const int flags
= PropFlags::dontEnum
|
72 PropFlags::onlySWF6Up
;
74 newobj
.init_member(NSV::PROP_uuCONSTRUCTORuu
, this, flags
);
77 newobj
.init_member(NSV::PROP_CONSTRUCTOR
, this, PropFlags::dontEnum
);
80 // Don't set a super so that it will be constructed only if required
82 fn_call
fn(&newobj
, env
, args
, 0, true);
88 catch (const GnashException
& ex
) {
89 // Catching a std::exception here can mask all sorts of bad
90 // behaviour, as (for instance) a poorly constructed string may
91 // smash the stack, throw an exception, but not abort.
92 // This is very effective at confusing debugging tools.
93 // We only throw GnashExceptions. A std::bad_alloc may also be
94 // reasonable, but anything else shouldn't be caught here.
95 log_debug("Native function called as constructor threw exception: "
98 // If a constructor throws an exception, throw it back to the
99 // caller. This is the only way to signal that a constructor
100 // did not return anything.
104 // Some built-in constructors do things properly and operate on the
105 // 'this' pointer. Others return a new object. This is to handle those
107 if (isBuiltin() && ret
.is_object()) {
108 as_object
* fakeobj
= toObject(ret
, getVM(env
));
110 fakeobj
->init_member(NSV::PROP_uuCONSTRUCTORuu
, as_value(this),
113 // Also for SWF5+ only?
114 if (swfversion
< 7) {
115 fakeobj
->init_member(NSV::PROP_CONSTRUCTOR
, as_value(this),
116 PropFlags::dontEnum
);