| 1 | /* |
| 2 | * Copyright (C) 2013-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "MapConstructor.h" |
| 28 | |
| 29 | #include "Error.h" |
| 30 | #include "GetterSetter.h" |
| 31 | #include "IteratorOperations.h" |
| 32 | #include "JSCInlines.h" |
| 33 | #include "JSGlobalObject.h" |
| 34 | #include "JSMap.h" |
| 35 | #include "JSObjectInlines.h" |
| 36 | #include "MapPrototype.h" |
| 37 | |
| 38 | namespace JSC { |
| 39 | |
| 40 | const ClassInfo MapConstructor::s_info = { "Function" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(MapConstructor) }; |
| 41 | |
| 42 | void MapConstructor::finishCreation(VM& vm, MapPrototype* mapPrototype, GetterSetter* speciesSymbol) |
| 43 | { |
| 44 | Base::finishCreation(vm, "Map"_s , NameAdditionMode::WithoutStructureTransition); |
| 45 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, mapPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); |
| 46 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
| 47 | putDirectNonIndexAccessorWithoutTransition(vm, vm.propertyNames->speciesSymbol, speciesSymbol, PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
| 48 | } |
| 49 | |
| 50 | static EncodedJSValue JSC_HOST_CALL callMap(JSGlobalObject*, CallFrame*); |
| 51 | static EncodedJSValue JSC_HOST_CALL constructMap(JSGlobalObject*, CallFrame*); |
| 52 | |
| 53 | MapConstructor::MapConstructor(VM& vm, Structure* structure) |
| 54 | : Base(vm, structure, callMap, constructMap) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | static EncodedJSValue JSC_HOST_CALL callMap(JSGlobalObject* globalObject, CallFrame*) |
| 59 | { |
| 60 | VM& vm = globalObject->vm(); |
| 61 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 62 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(globalObject, scope, "Map" )); |
| 63 | } |
| 64 | |
| 65 | static EncodedJSValue JSC_HOST_CALL constructMap(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 66 | { |
| 67 | VM& vm = globalObject->vm(); |
| 68 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 69 | |
| 70 | Structure* mapStructure = InternalFunction::createSubclassStructure(globalObject, callFrame->jsCallee(), callFrame->newTarget(), globalObject->mapStructure()); |
| 71 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 72 | |
| 73 | JSValue iterable = callFrame->argument(0); |
| 74 | if (iterable.isUndefinedOrNull()) |
| 75 | RELEASE_AND_RETURN(scope, JSValue::encode(JSMap::create(globalObject, vm, mapStructure))); |
| 76 | |
| 77 | if (auto* iterableMap = jsDynamicCast<JSMap*>(vm, iterable)) { |
| 78 | if (iterableMap->canCloneFastAndNonObservable(mapStructure)) |
| 79 | RELEASE_AND_RETURN(scope, JSValue::encode(iterableMap->clone(globalObject, vm, mapStructure))); |
| 80 | } |
| 81 | |
| 82 | JSMap* map = JSMap::create(globalObject, vm, mapStructure); |
| 83 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 84 | |
| 85 | JSValue adderFunction = map->JSObject::get(globalObject, vm.propertyNames->set); |
| 86 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 87 | |
| 88 | CallData adderFunctionCallData; |
| 89 | CallType adderFunctionCallType = getCallData(vm, adderFunction, adderFunctionCallData); |
| 90 | if (adderFunctionCallType == CallType::None) |
| 91 | return JSValue::encode(throwTypeError(globalObject, scope)); |
| 92 | |
| 93 | scope.release(); |
| 94 | forEachInIterable(globalObject, iterable, [&](VM& vm, JSGlobalObject* globalObject, JSValue nextItem) { |
| 95 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 96 | if (!nextItem.isObject()) { |
| 97 | throwTypeError(globalObject, scope); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | JSValue key = nextItem.get(globalObject, static_cast<unsigned>(0)); |
| 102 | RETURN_IF_EXCEPTION(scope, void()); |
| 103 | |
| 104 | JSValue value = nextItem.get(globalObject, static_cast<unsigned>(1)); |
| 105 | RETURN_IF_EXCEPTION(scope, void()); |
| 106 | |
| 107 | MarkedArgumentBuffer arguments; |
| 108 | arguments.append(key); |
| 109 | arguments.append(value); |
| 110 | ASSERT(!arguments.hasOverflowed()); |
| 111 | scope.release(); |
| 112 | call(globalObject, adderFunction, adderFunctionCallType, adderFunctionCallData, map, arguments); |
| 113 | }); |
| 114 | |
| 115 | return JSValue::encode(map); |
| 116 | } |
| 117 | |
| 118 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketHead(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 119 | { |
| 120 | ASSERT_UNUSED(globalObject, jsDynamicCast<JSMap*>(globalObject->vm(), callFrame->argument(0))); |
| 121 | JSMap* map = jsCast<JSMap*>(callFrame->uncheckedArgument(0)); |
| 122 | auto* head = map->head(); |
| 123 | ASSERT(head); |
| 124 | return JSValue::encode(head); |
| 125 | } |
| 126 | |
| 127 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketNext(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 128 | { |
| 129 | ASSERT(jsDynamicCast<JSMap::BucketType*>(globalObject->vm(), callFrame->argument(0))); |
| 130 | auto* bucket = jsCast<JSMap::BucketType*>(callFrame->uncheckedArgument(0)); |
| 131 | ASSERT(bucket); |
| 132 | bucket = bucket->next(); |
| 133 | while (bucket) { |
| 134 | if (!bucket->deleted()) |
| 135 | return JSValue::encode(bucket); |
| 136 | bucket = bucket->next(); |
| 137 | } |
| 138 | return JSValue::encode(globalObject->vm().sentinelMapBucket()); |
| 139 | } |
| 140 | |
| 141 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketKey(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 142 | { |
| 143 | ASSERT_UNUSED(globalObject, jsDynamicCast<JSMap::BucketType*>(globalObject->vm(), callFrame->argument(0))); |
| 144 | auto* bucket = jsCast<JSMap::BucketType*>(callFrame->uncheckedArgument(0)); |
| 145 | ASSERT(bucket); |
| 146 | return JSValue::encode(bucket->key()); |
| 147 | } |
| 148 | |
| 149 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketValue(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 150 | { |
| 151 | ASSERT_UNUSED(globalObject, jsDynamicCast<JSMap::BucketType*>(globalObject->vm(), callFrame->argument(0))); |
| 152 | auto* bucket = jsCast<JSMap::BucketType*>(callFrame->uncheckedArgument(0)); |
| 153 | ASSERT(bucket); |
| 154 | return JSValue::encode(bucket->value()); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |