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 "SetConstructor.h"
28
29#include "Error.h"
30#include "GetterSetter.h"
31#include "IteratorOperations.h"
32#include "JSCInlines.h"
33#include "JSGlobalObject.h"
34#include "JSObjectInlines.h"
35#include "JSSet.h"
36#include "SetPrototype.h"
37
38namespace JSC {
39
40const ClassInfo SetConstructor::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(SetConstructor) };
41
42void SetConstructor::finishCreation(VM& vm, SetPrototype* setPrototype, GetterSetter* speciesSymbol)
43{
44 Base::finishCreation(vm, vm.propertyNames->Set.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
45 putDirectWithoutTransition(vm, vm.propertyNames->prototype, setPrototype, 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
50static EncodedJSValue JSC_HOST_CALL callSet(ExecState*);
51static EncodedJSValue JSC_HOST_CALL constructSet(ExecState*);
52
53SetConstructor::SetConstructor(VM& vm, Structure* structure)
54 : Base(vm, structure, callSet, constructSet)
55{
56}
57
58static EncodedJSValue JSC_HOST_CALL callSet(ExecState* exec)
59{
60 VM& vm = exec->vm();
61 auto scope = DECLARE_THROW_SCOPE(vm);
62 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "Set"));
63}
64
65static EncodedJSValue JSC_HOST_CALL constructSet(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* setStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->setStructure());
72 RETURN_IF_EXCEPTION(scope, encodedJSValue());
73
74 JSValue iterable = exec->argument(0);
75 if (iterable.isUndefinedOrNull())
76 RELEASE_AND_RETURN(scope, JSValue::encode(JSSet::create(exec, vm, setStructure)));
77
78 if (auto* iterableSet = jsDynamicCast<JSSet*>(vm, iterable)) {
79 if (iterableSet->canCloneFastAndNonObservable(setStructure))
80 RELEASE_AND_RETURN(scope, JSValue::encode(iterableSet->clone(exec, vm, setStructure)));
81 }
82
83 JSSet* set = JSSet::create(exec, vm, setStructure);
84 RETURN_IF_EXCEPTION(scope, encodedJSValue());
85
86 JSValue adderFunction = set->JSObject::get(exec, vm.propertyNames->add);
87 RETURN_IF_EXCEPTION(scope, encodedJSValue());
88
89 CallData adderFunctionCallData;
90 CallType adderFunctionCallType = getCallData(vm, adderFunction, adderFunctionCallData);
91 if (UNLIKELY(adderFunctionCallType == CallType::None))
92 return JSValue::encode(throwTypeError(exec, scope));
93
94 scope.release();
95 forEachInIterable(exec, iterable, [&](VM&, ExecState* exec, JSValue nextValue) {
96 MarkedArgumentBuffer arguments;
97 arguments.append(nextValue);
98 ASSERT(!arguments.hasOverflowed());
99 call(exec, adderFunction, adderFunctionCallType, adderFunctionCallData, set, arguments);
100 });
101
102 return JSValue::encode(set);
103}
104
105EncodedJSValue JSC_HOST_CALL setPrivateFuncSetBucketHead(ExecState* exec)
106{
107 ASSERT(jsDynamicCast<JSSet*>(exec->vm(), exec->argument(0)));
108 JSSet* set = jsCast<JSSet*>(exec->uncheckedArgument(0));
109 auto* head = set->head();
110 ASSERT(head);
111 return JSValue::encode(head);
112}
113
114EncodedJSValue JSC_HOST_CALL setPrivateFuncSetBucketNext(ExecState* exec)
115{
116 ASSERT(jsDynamicCast<JSSet::BucketType*>(exec->vm(), exec->argument(0)));
117 auto* bucket = jsCast<JSSet::BucketType*>(exec->uncheckedArgument(0));
118 ASSERT(bucket);
119 bucket = bucket->next();
120 while (bucket) {
121 if (!bucket->deleted())
122 return JSValue::encode(bucket);
123 bucket = bucket->next();
124 }
125 return JSValue::encode(exec->vm().sentinelSetBucket());
126}
127
128EncodedJSValue JSC_HOST_CALL setPrivateFuncSetBucketKey(ExecState* exec)
129{
130 ASSERT(jsDynamicCast<JSSet::BucketType*>(exec->vm(), exec->argument(0)));
131 auto* bucket = jsCast<JSSet::BucketType*>(exec->uncheckedArgument(0));
132 ASSERT(bucket);
133 return JSValue::encode(bucket->key());
134}
135
136}
137