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