| 1 | /* |
| 2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2008, 2016-2017 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(ExecState*); |
| 30 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptors(ExecState*); |
| 31 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertySymbols(ExecState*); |
| 32 | EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(ExecState*); |
| 33 | EncodedJSValue JSC_HOST_CALL objectConstructorKeys(ExecState*); |
| 34 | |
| 35 | class ObjectPrototype; |
| 36 | |
| 37 | class ObjectConstructor final : public InternalFunction { |
| 38 | public: |
| 39 | typedef InternalFunction Base; |
| 40 | static const 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(ExecState* exec, Structure* structure) |
| 64 | { |
| 65 | return JSFinalObject::create(exec, structure); |
| 66 | } |
| 67 | |
| 68 | inline JSFinalObject* constructEmptyObject(ExecState* exec, JSObject* prototype, unsigned inlineCapacity) |
| 69 | { |
| 70 | JSGlobalObject* globalObject = exec->lexicalGlobalObject(); |
| 71 | StructureCache& structureCache = globalObject->vm().structureCache; |
| 72 | Structure* structure = structureCache.emptyObjectStructureForPrototype(globalObject, prototype, inlineCapacity); |
| 73 | return constructEmptyObject(exec, structure); |
| 74 | } |
| 75 | |
| 76 | inline JSFinalObject* constructEmptyObject(ExecState* exec, JSObject* prototype) |
| 77 | { |
| 78 | return constructEmptyObject(exec, prototype, JSFinalObject::defaultInlineCapacity()); |
| 79 | } |
| 80 | |
| 81 | inline JSFinalObject* constructEmptyObject(ExecState* exec) |
| 82 | { |
| 83 | return constructEmptyObject(exec, exec->lexicalGlobalObject()->objectStructureForObjectConstructor()); |
| 84 | } |
| 85 | |
| 86 | inline JSObject* constructObject(ExecState* exec, JSGlobalObject* globalObject, JSValue arg) |
| 87 | { |
| 88 | if (arg.isUndefinedOrNull()) |
| 89 | return constructEmptyObject(exec, globalObject->objectPrototype()); |
| 90 | return arg.toObject(exec, 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(ExecState* exec, const PropertyDescriptor& descriptor) |
| 96 | { |
| 97 | VM& vm = exec->vm(); |
| 98 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 99 | JSObject* description = constructEmptyObject(exec); |
| 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(ExecState*, JSObject*); |
| 121 | JS_EXPORT_PRIVATE JSObject* objectConstructorSeal(ExecState*, JSObject*); |
| 122 | JSValue objectConstructorGetOwnPropertyDescriptor(ExecState*, JSObject*, const Identifier&); |
| 123 | JSValue objectConstructorGetOwnPropertyDescriptors(ExecState*, JSObject*); |
| 124 | JSArray* ownPropertyKeys(ExecState*, JSObject*, PropertyNameMode, DontEnumPropertiesMode); |
| 125 | bool toPropertyDescriptor(ExecState*, JSValue, PropertyDescriptor&); |
| 126 | |
| 127 | } // namespace JSC |
| 128 | |