1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected]) |
3 | * Copyright (C) 2008-2019 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 | #pragma once |
22 | |
23 | #include "InternalFunction.h" |
24 | #include "JSGlobalObject.h" |
25 | #include "ObjectPrototype.h" |
26 | |
27 | namespace JSC { |
28 | |
29 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(JSGlobalObject*, CallFrame*); |
30 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptors(JSGlobalObject*, CallFrame*); |
31 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertySymbols(JSGlobalObject*, CallFrame*); |
32 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(JSGlobalObject*, CallFrame*); |
33 | EncodedJSValue JSC_HOST_CALL objectConstructorKeys(JSGlobalObject*, CallFrame*); |
34 | |
35 | class ObjectPrototype; |
36 | |
37 | class ObjectConstructor final : public InternalFunction { |
38 | public: |
39 | typedef InternalFunction Base; |
40 | static constexpr unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable; |
41 | |
42 | static ObjectConstructor* create(VM& vm, JSGlobalObject* globalObject, Structure* structure, ObjectPrototype* objectPrototype) |
43 | { |
44 | ObjectConstructor* constructor = new (NotNull, allocateCell<ObjectConstructor>(vm.heap)) ObjectConstructor(vm, structure); |
45 | constructor->finishCreation(vm, globalObject, objectPrototype); |
46 | return constructor; |
47 | } |
48 | |
49 | DECLARE_INFO; |
50 | |
51 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
52 | { |
53 | return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); |
54 | } |
55 | |
56 | protected: |
57 | void finishCreation(VM&, JSGlobalObject*, ObjectPrototype*); |
58 | |
59 | private: |
60 | ObjectConstructor(VM&, Structure*); |
61 | }; |
62 | |
63 | inline JSFinalObject* constructEmptyObject(VM& vm, Structure* structure) |
64 | { |
65 | return JSFinalObject::create(vm, structure); |
66 | } |
67 | |
68 | inline JSFinalObject* constructEmptyObject(JSGlobalObject* globalObject, JSObject* prototype, unsigned inlineCapacity) |
69 | { |
70 | VM& vm = getVM(globalObject); |
71 | StructureCache& structureCache = vm.structureCache; |
72 | Structure* structure = structureCache.emptyObjectStructureForPrototype(globalObject, prototype, inlineCapacity); |
73 | return constructEmptyObject(vm, structure); |
74 | } |
75 | |
76 | inline JSFinalObject* constructEmptyObject(JSGlobalObject* globalObject, JSObject* prototype) |
77 | { |
78 | return constructEmptyObject(globalObject, prototype, JSFinalObject::defaultInlineCapacity()); |
79 | } |
80 | |
81 | inline JSFinalObject* constructEmptyObject(JSGlobalObject* globalObject) |
82 | { |
83 | return constructEmptyObject(getVM(globalObject), globalObject->objectStructureForObjectConstructor()); |
84 | } |
85 | |
86 | inline JSObject* constructObject(JSGlobalObject* globalObject, JSValue arg) |
87 | { |
88 | if (arg.isUndefinedOrNull()) |
89 | return constructEmptyObject(globalObject, globalObject->objectPrototype()); |
90 | return arg.toObject(globalObject); |
91 | } |
92 | |
93 | // Section 6.2.4.4 of the ES6 specification. |
94 | // https://tc39.github.io/ecma262/#sec-frompropertydescriptor |
95 | inline JSObject* constructObjectFromPropertyDescriptor(JSGlobalObject* globalObject, const PropertyDescriptor& descriptor) |
96 | { |
97 | VM& vm = getVM(globalObject); |
98 | auto scope = DECLARE_THROW_SCOPE(vm); |
99 | JSObject* description = constructEmptyObject(globalObject); |
100 | RETURN_IF_EXCEPTION(scope, nullptr); |
101 | |
102 | if (!descriptor.isAccessorDescriptor()) { |
103 | description->putDirect(vm, vm.propertyNames->value, descriptor.value() ? descriptor.value() : jsUndefined(), 0); |
104 | description->putDirect(vm, vm.propertyNames->writable, jsBoolean(descriptor.writable()), 0); |
105 | } else { |
106 | ASSERT(descriptor.getter() || descriptor.setter()); |
107 | if (descriptor.getter()) |
108 | description->putDirect(vm, vm.propertyNames->get, descriptor.getter(), 0); |
109 | if (descriptor.setter()) |
110 | description->putDirect(vm, vm.propertyNames->set, descriptor.setter(), 0); |
111 | } |
112 | |
113 | description->putDirect(vm, vm.propertyNames->enumerable, jsBoolean(descriptor.enumerable()), 0); |
114 | description->putDirect(vm, vm.propertyNames->configurable, jsBoolean(descriptor.configurable()), 0); |
115 | |
116 | return description; |
117 | } |
118 | |
119 | |
120 | JS_EXPORT_PRIVATE JSObject* objectConstructorFreeze(JSGlobalObject*, JSObject*); |
121 | JS_EXPORT_PRIVATE JSObject* objectConstructorSeal(JSGlobalObject*, JSObject*); |
122 | JSValue objectConstructorGetOwnPropertyDescriptor(JSGlobalObject*, JSObject*, const Identifier&); |
123 | JSValue objectConstructorGetOwnPropertyDescriptors(JSGlobalObject*, JSObject*); |
124 | JSArray* ownPropertyKeys(JSGlobalObject*, JSObject*, PropertyNameMode, DontEnumPropertiesMode); |
125 | bool toPropertyDescriptor(JSGlobalObject*, JSValue, PropertyDescriptor&); |
126 | |
127 | } // namespace JSC |
128 | |