| 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 , NameVisibility::Visible, 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(ExecState*); |
| 51 | static EncodedJSValue JSC_HOST_CALL constructMap(ExecState*); |
| 52 | |
| 53 | MapConstructor::MapConstructor(VM& vm, Structure* structure) |
| 54 | : Base(vm, structure, callMap, constructMap) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | static EncodedJSValue JSC_HOST_CALL callMap(ExecState* exec) |
| 59 | { |
| 60 | VM& vm = exec->vm(); |
| 61 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 62 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "Map" )); |
| 63 | } |
| 64 | |
| 65 | static EncodedJSValue JSC_HOST_CALL constructMap(ExecState* exec) |
| 66 | { |
| 67 | VM& vm = exec->vm(); |
| 68 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 69 | |
| 70 | JSGlobalObject* globalObject = jsCast<InternalFunction*>(exec->jsCallee())->globalObject(vm); |
| 71 | Structure* mapStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->mapStructure()); |
| 72 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 73 | |
| 74 | JSValue iterable = exec->argument(0); |
| 75 | if (iterable.isUndefinedOrNull()) |
| 76 | RELEASE_AND_RETURN(scope, JSValue::encode(JSMap::create(exec, vm, mapStructure))); |
| 77 | |
| 78 | if (auto* iterableMap = jsDynamicCast<JSMap*>(vm, iterable)) { |
| 79 | if (iterableMap->canCloneFastAndNonObservable(mapStructure)) |
| 80 | RELEASE_AND_RETURN(scope, JSValue::encode(iterableMap->clone(exec, vm, mapStructure))); |
| 81 | } |
| 82 | |
| 83 | JSMap* map = JSMap::create(exec, vm, mapStructure); |
| 84 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 85 | |
| 86 | JSValue adderFunction = map->JSObject::get(exec, vm.propertyNames->set); |
| 87 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 88 | |
| 89 | CallData adderFunctionCallData; |
| 90 | CallType adderFunctionCallType = getCallData(vm, adderFunction, adderFunctionCallData); |
| 91 | if (adderFunctionCallType == CallType::None) |
| 92 | return JSValue::encode(throwTypeError(exec, scope)); |
| 93 | |
| 94 | scope.release(); |
| 95 | forEachInIterable(exec, iterable, [&](VM& vm, ExecState* exec, JSValue nextItem) { |
| 96 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 97 | if (!nextItem.isObject()) { |
| 98 | throwTypeError(exec, scope); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | JSValue key = nextItem.get(exec, static_cast<unsigned>(0)); |
| 103 | RETURN_IF_EXCEPTION(scope, void()); |
| 104 | |
| 105 | JSValue value = nextItem.get(exec, static_cast<unsigned>(1)); |
| 106 | RETURN_IF_EXCEPTION(scope, void()); |
| 107 | |
| 108 | MarkedArgumentBuffer arguments; |
| 109 | arguments.append(key); |
| 110 | arguments.append(value); |
| 111 | ASSERT(!arguments.hasOverflowed()); |
| 112 | scope.release(); |
| 113 | call(exec, adderFunction, adderFunctionCallType, adderFunctionCallData, map, arguments); |
| 114 | }); |
| 115 | |
| 116 | return JSValue::encode(map); |
| 117 | } |
| 118 | |
| 119 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketHead(ExecState* exec) |
| 120 | { |
| 121 | ASSERT(jsDynamicCast<JSMap*>(exec->vm(), exec->argument(0))); |
| 122 | JSMap* map = jsCast<JSMap*>(exec->uncheckedArgument(0)); |
| 123 | auto* head = map->head(); |
| 124 | ASSERT(head); |
| 125 | return JSValue::encode(head); |
| 126 | } |
| 127 | |
| 128 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketNext(ExecState* exec) |
| 129 | { |
| 130 | ASSERT(jsDynamicCast<JSMap::BucketType*>(exec->vm(), exec->argument(0))); |
| 131 | auto* bucket = jsCast<JSMap::BucketType*>(exec->uncheckedArgument(0)); |
| 132 | ASSERT(bucket); |
| 133 | bucket = bucket->next(); |
| 134 | while (bucket) { |
| 135 | if (!bucket->deleted()) |
| 136 | return JSValue::encode(bucket); |
| 137 | bucket = bucket->next(); |
| 138 | } |
| 139 | return JSValue::encode(exec->vm().sentinelMapBucket()); |
| 140 | } |
| 141 | |
| 142 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketKey(ExecState* exec) |
| 143 | { |
| 144 | ASSERT(jsDynamicCast<JSMap::BucketType*>(exec->vm(), exec->argument(0))); |
| 145 | auto* bucket = jsCast<JSMap::BucketType*>(exec->uncheckedArgument(0)); |
| 146 | ASSERT(bucket); |
| 147 | return JSValue::encode(bucket->key()); |
| 148 | } |
| 149 | |
| 150 | EncodedJSValue JSC_HOST_CALL mapPrivateFuncMapBucketValue(ExecState* exec) |
| 151 | { |
| 152 | ASSERT(jsDynamicCast<JSMap::BucketType*>(exec->vm(), exec->argument(0))); |
| 153 | auto* bucket = jsCast<JSMap::BucketType*>(exec->uncheckedArgument(0)); |
| 154 | ASSERT(bucket); |
| 155 | return JSValue::encode(bucket->value()); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |