| 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 "WeakMapConstructor.h" |
| 28 | |
| 29 | #include "Error.h" |
| 30 | #include "IteratorOperations.h" |
| 31 | #include "JSCInlines.h" |
| 32 | #include "JSGlobalObject.h" |
| 33 | #include "JSObjectInlines.h" |
| 34 | #include "JSWeakMap.h" |
| 35 | #include "WeakMapPrototype.h" |
| 36 | |
| 37 | namespace JSC { |
| 38 | |
| 39 | const ClassInfo WeakMapConstructor::s_info = { "Function" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WeakMapConstructor) }; |
| 40 | |
| 41 | void WeakMapConstructor::finishCreation(VM& vm, WeakMapPrototype* prototype) |
| 42 | { |
| 43 | Base::finishCreation(vm, "WeakMap"_s , NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
| 44 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); |
| 45 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
| 46 | } |
| 47 | |
| 48 | static EncodedJSValue JSC_HOST_CALL callWeakMap(ExecState*); |
| 49 | static EncodedJSValue JSC_HOST_CALL constructWeakMap(ExecState*); |
| 50 | |
| 51 | WeakMapConstructor::WeakMapConstructor(VM& vm, Structure* structure) |
| 52 | : Base(vm, structure, callWeakMap, constructWeakMap) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | static EncodedJSValue JSC_HOST_CALL callWeakMap(ExecState* exec) |
| 57 | { |
| 58 | VM& vm = exec->vm(); |
| 59 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 60 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "WeakMap" )); |
| 61 | } |
| 62 | |
| 63 | static EncodedJSValue JSC_HOST_CALL constructWeakMap(ExecState* exec) |
| 64 | { |
| 65 | VM& vm = exec->vm(); |
| 66 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 67 | |
| 68 | JSGlobalObject* globalObject = jsCast<InternalFunction*>(exec->jsCallee())->globalObject(vm); |
| 69 | Structure* weakMapStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->weakMapStructure()); |
| 70 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 71 | JSWeakMap* weakMap = JSWeakMap::create(vm, weakMapStructure); |
| 72 | JSValue iterable = exec->argument(0); |
| 73 | if (iterable.isUndefinedOrNull()) |
| 74 | return JSValue::encode(weakMap); |
| 75 | |
| 76 | JSValue adderFunction = weakMap->JSObject::get(exec, vm.propertyNames->set); |
| 77 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 78 | |
| 79 | CallData adderFunctionCallData; |
| 80 | CallType adderFunctionCallType = getCallData(vm, adderFunction, adderFunctionCallData); |
| 81 | if (adderFunctionCallType == CallType::None) |
| 82 | return JSValue::encode(throwTypeError(exec, scope)); |
| 83 | |
| 84 | scope.release(); |
| 85 | forEachInIterable(exec, iterable, [&](VM& vm, ExecState* exec, JSValue nextItem) { |
| 86 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 87 | if (!nextItem.isObject()) { |
| 88 | throwTypeError(exec, scope); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | JSValue key = nextItem.get(exec, static_cast<unsigned>(0)); |
| 93 | RETURN_IF_EXCEPTION(scope, void()); |
| 94 | |
| 95 | JSValue value = nextItem.get(exec, static_cast<unsigned>(1)); |
| 96 | RETURN_IF_EXCEPTION(scope, void()); |
| 97 | |
| 98 | MarkedArgumentBuffer arguments; |
| 99 | arguments.append(key); |
| 100 | arguments.append(value); |
| 101 | ASSERT(!arguments.hasOverflowed()); |
| 102 | scope.release(); |
| 103 | call(exec, adderFunction, adderFunctionCallType, adderFunctionCallData, weakMap, arguments); |
| 104 | }); |
| 105 | |
| 106 | return JSValue::encode(weakMap); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |