Fix test for bug #32625
[gnash.git] / libcore / as_function.cpp
blob94910f97702e0481f7bb33cdce923b7b219c4af7
1 // as_function.cpp: ActionScript Functions, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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.
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
19 //
21 #include "as_function.h"
23 #include <string>
25 #include "smart_ptr.h"
26 #include "log.h"
27 #include "as_value.h"
28 #include "fn_call.h"
29 #include "GnashException.h"
30 #include "Global_as.h"
31 #include "namedStrings.h"
33 namespace gnash {
35 as_function::as_function(Global_as& gl)
37 as_object(gl)
41 std::string
42 as_function::stringValue() const
44 return "[type Function]";
47 as_object*
48 constructInstance(as_function& ctor, const as_environment& env,
49 fn_call::Args& args)
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
56 // of its visibility.
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);
64 as_object*
65 as_function::construct(as_object& newobj, const as_environment& env,
66 fn_call::Args& args)
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);
76 if (swfversion < 7) {
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
81 // by the function.
82 fn_call fn(&newobj, env, args, 0, true);
83 as_value ret;
85 try {
86 ret = call(fn);
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: "
96 "%s", ex.what());
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.
101 throw;
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
106 // cases.
107 if (isBuiltin() && ret.is_object()) {
108 as_object* fakeobj = toObject(ret, getVM(env));
110 fakeobj->init_member(NSV::PROP_uuCONSTRUCTORuu, as_value(this),
111 flags);
113 // Also for SWF5+ only?
114 if (swfversion < 7) {
115 fakeobj->init_member(NSV::PROP_CONSTRUCTOR, as_value(this),
116 PropFlags::dontEnum);
118 return fakeobj;
121 return &newobj;
124 } // namespace gnash