1 // as_function.cpp: ActionScript Functions, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
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"
28 #include "GnashException.h"
29 #include "Global_as.h"
30 #include "namedStrings.h"
34 as_function::as_function(Global_as
& gl
)
41 as_function::stringValue() const
43 return "[type Function]";
47 constructInstance(as_function
& ctor
, const as_environment
& env
,
50 Global_as
& gl
= getGlobal(ctor
);
52 // Create an empty object, with a ref to the constructor's prototype.
53 // The function's prototype property always becomes the new object's
54 // __proto__ member, regardless of whether it is an object and regardless
56 as_object
* newobj
= new as_object(gl
);
57 Property
* proto
= ctor
.getOwnProperty(NSV::PROP_PROTOTYPE
);
58 if (proto
) newobj
->set_prototype(proto
->getValue(ctor
));
60 return ctor
.construct(*newobj
, env
, args
);
64 as_function::construct(as_object
& newobj
, const as_environment
& env
,
67 const int swfversion
= getSWFVersion(env
);
69 // Add a __constructor__ member to the new object visible from version 6.
70 const int flags
= PropFlags::dontEnum
|
71 PropFlags::onlySWF6Up
;
73 newobj
.init_member(NSV::PROP_uuCONSTRUCTORuu
, this, flags
);
76 newobj
.init_member(NSV::PROP_CONSTRUCTOR
, this, PropFlags::dontEnum
);
79 // Don't set a super so that it will be constructed only if required
81 fn_call
fn(&newobj
, env
, args
, nullptr, true);
87 catch (const GnashException
& ex
) {
88 // Catching a std::exception here can mask all sorts of bad
89 // behaviour, as (for instance) a poorly constructed string may
90 // smash the stack, throw an exception, but not abort.
91 // This is very effective at confusing debugging tools.
92 // We only throw GnashExceptions. A std::bad_alloc may also be
93 // reasonable, but anything else shouldn't be caught here.
94 log_error(_("Native function called as constructor threw exception: %s")
97 // If a constructor throws an exception, throw it back to the
98 // caller. This is the only way to signal that a constructor
99 // did not return anything.
103 // Some built-in constructors do things properly and operate on the
104 // 'this' pointer. Others return a new object. This is to handle those
106 if (isBuiltin() && ret
.is_object()) {
107 as_object
* fakeobj
= toObject(ret
, getVM(env
));
109 fakeobj
->init_member(NSV::PROP_uuCONSTRUCTORuu
, as_value(this),
112 // Also for SWF5+ only?
113 if (swfversion
< 7) {
114 fakeobj
->init_member(NSV::PROP_CONSTRUCTOR
, as_value(this),
115 PropFlags::dontEnum
);