• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2008 Apple Inc. All rights reserved.
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library 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 GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #include "config.h"
22 #include "ObjectConstructor.h"
23 
24 #include "JSFunction.h"
25 #include "JSGlobalObject.h"
26 #include "ObjectPrototype.h"
27 #include "PrototypeFunction.h"
28 
29 namespace JSC {
30 
31 ASSERT_CLASS_FITS_IN_CELL(ObjectConstructor);
32 
33 static JSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState*, JSObject*, JSValue, const ArgList&);
34 
ObjectConstructor(ExecState * exec,PassRefPtr<Structure> structure,ObjectPrototype * objectPrototype,Structure * prototypeFunctionStructure)35 ObjectConstructor::ObjectConstructor(ExecState* exec, PassRefPtr<Structure> structure, ObjectPrototype* objectPrototype, Structure* prototypeFunctionStructure)
36     : InternalFunction(&exec->globalData(), structure, Identifier(exec, "Object"))
37 {
38     // ECMA 15.2.3.1
39     putDirectWithoutTransition(exec->propertyNames().prototype, objectPrototype, DontEnum | DontDelete | ReadOnly);
40 
41     // no. of arguments for constructor
42     putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
43 
44     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().getPrototypeOf, objectConstructorGetPrototypeOf), DontEnum);
45 }
46 
47 // ECMA 15.2.2
constructObject(ExecState * exec,const ArgList & args)48 static ALWAYS_INLINE JSObject* constructObject(ExecState* exec, const ArgList& args)
49 {
50     JSValue arg = args.at(0);
51     if (arg.isUndefinedOrNull())
52         return new (exec) JSObject(exec->lexicalGlobalObject()->emptyObjectStructure());
53     return arg.toObject(exec);
54 }
55 
constructWithObjectConstructor(ExecState * exec,JSObject *,const ArgList & args)56 static JSObject* constructWithObjectConstructor(ExecState* exec, JSObject*, const ArgList& args)
57 {
58     return constructObject(exec, args);
59 }
60 
getConstructData(ConstructData & constructData)61 ConstructType ObjectConstructor::getConstructData(ConstructData& constructData)
62 {
63     constructData.native.function = constructWithObjectConstructor;
64     return ConstructTypeHost;
65 }
66 
callObjectConstructor(ExecState * exec,JSObject *,JSValue,const ArgList & args)67 static JSValue JSC_HOST_CALL callObjectConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
68 {
69     return constructObject(exec, args);
70 }
71 
getCallData(CallData & callData)72 CallType ObjectConstructor::getCallData(CallData& callData)
73 {
74     callData.native.function = callObjectConstructor;
75     return CallTypeHost;
76 }
77 
objectConstructorGetPrototypeOf(ExecState * exec,JSObject *,JSValue,const ArgList & args)78 JSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState* exec, JSObject*, JSValue, const ArgList& args)
79 {
80     if (!args.at(0).isObject())
81         return throwError(exec, TypeError, "Requested prototype of a value that is not an object.");
82     return asObject(args.at(0))->prototype();
83 }
84 
85 } // namespace JSC
86