Rolling out r214038 and r213697: Crashes when using computed properties with rest...
[WebKit-https.git] / Source / JavaScriptCore / runtime / SetPrototype.cpp
1 /*
2  * Copyright (C) 2013, 2016 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 "SetPrototype.h"
28
29 #include "BuiltinNames.h"
30 #include "Error.h"
31 #include "ExceptionHelpers.h"
32 #include "IteratorOperations.h"
33 #include "JSCInlines.h"
34 #include "JSSet.h"
35 #include "JSSetIterator.h"
36 #include "Lookup.h"
37
38 #include "SetPrototype.lut.h"
39
40 namespace JSC {
41
42 const ClassInfo SetPrototype::s_info = { "Set", &Base::s_info, &setPrototypeTable, CREATE_METHOD_TABLE(SetPrototype) };
43
44 /* Source for SetIteratorPrototype.lut.h
45 @begin setPrototypeTable
46   forEach   JSBuiltin  DontEnum|Function 0
47 @end
48 */
49
50 static EncodedJSValue JSC_HOST_CALL setProtoFuncAdd(ExecState*);
51 static EncodedJSValue JSC_HOST_CALL setProtoFuncClear(ExecState*);
52 static EncodedJSValue JSC_HOST_CALL setProtoFuncDelete(ExecState*);
53 static EncodedJSValue JSC_HOST_CALL setProtoFuncHas(ExecState*);
54 static EncodedJSValue JSC_HOST_CALL setProtoFuncValues(ExecState*);
55 static EncodedJSValue JSC_HOST_CALL setProtoFuncEntries(ExecState*);
56
57
58 static EncodedJSValue JSC_HOST_CALL setProtoFuncSize(ExecState*);
59
60 void SetPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
61 {
62     Base::finishCreation(vm);
63     ASSERT(inherits(vm, info()));
64     vm.prototypeMap.addPrototype(this);
65
66     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->add, setProtoFuncAdd, DontEnum, 1);
67     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->clear, setProtoFuncClear, DontEnum, 0);
68     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->deleteKeyword, setProtoFuncDelete, DontEnum, 1);
69     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->has, setProtoFuncHas, DontEnum, 1, JSSetHasIntrinsic);
70     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().entriesPublicName(), setProtoFuncEntries, DontEnum, 0);
71
72     JSFunction* values = JSFunction::create(vm, globalObject, 0, vm.propertyNames->builtinNames().valuesPublicName().string(), setProtoFuncValues);
73     putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().valuesPublicName(), values, DontEnum);
74     putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().keysPublicName(), values, DontEnum);
75     putDirectWithoutTransition(vm, vm.propertyNames->iteratorSymbol, values, DontEnum);
76     putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Set"), DontEnum | ReadOnly);
77
78     JSC_NATIVE_GETTER(vm.propertyNames->size, setProtoFuncSize, DontEnum | Accessor);
79 }
80
81 ALWAYS_INLINE static JSSet* getSet(CallFrame* callFrame, JSValue thisValue)
82 {
83     VM& vm = callFrame->vm();
84     auto scope = DECLARE_THROW_SCOPE(vm);
85
86     if (UNLIKELY(!thisValue.isCell())) {
87         throwVMError(callFrame, scope, createNotAnObjectError(callFrame, thisValue));
88         return nullptr;
89     }
90     if (LIKELY(thisValue.asCell()->type() == JSSetType))
91         return jsCast<JSSet*>(thisValue);
92     throwTypeError(callFrame, scope, ASCIILiteral("Set operation called on non-Set object"));
93     return nullptr;
94 }
95
96 EncodedJSValue JSC_HOST_CALL setProtoFuncAdd(CallFrame* callFrame)
97 {
98     JSValue thisValue = callFrame->thisValue();
99     JSSet* set = getSet(callFrame, thisValue);
100     if (!set)
101         return JSValue::encode(jsUndefined());
102     set->add(callFrame, callFrame->argument(0));
103     return JSValue::encode(thisValue);
104 }
105
106 EncodedJSValue JSC_HOST_CALL setProtoFuncClear(CallFrame* callFrame)
107 {
108     JSSet* set = getSet(callFrame, callFrame->thisValue());
109     if (!set)
110         return JSValue::encode(jsUndefined());
111     set->clear(callFrame);
112     return JSValue::encode(jsUndefined());
113 }
114
115 EncodedJSValue JSC_HOST_CALL setProtoFuncDelete(CallFrame* callFrame)
116 {
117     JSSet* set = getSet(callFrame, callFrame->thisValue());
118     if (!set)
119         return JSValue::encode(jsUndefined());
120     return JSValue::encode(jsBoolean(set->remove(callFrame, callFrame->argument(0))));
121 }
122
123 EncodedJSValue JSC_HOST_CALL setProtoFuncHas(CallFrame* callFrame)
124 {
125     JSSet* set = getSet(callFrame, callFrame->thisValue());
126     if (!set)
127         return JSValue::encode(jsUndefined());
128     return JSValue::encode(jsBoolean(set->has(callFrame, callFrame->argument(0))));
129 }
130
131 EncodedJSValue JSC_HOST_CALL setProtoFuncSize(CallFrame* callFrame)
132 {
133     JSSet* set = getSet(callFrame, callFrame->thisValue());
134     if (!set)
135         return JSValue::encode(jsUndefined());
136     return JSValue::encode(jsNumber(set->size(callFrame)));
137 }
138     
139 EncodedJSValue JSC_HOST_CALL setProtoFuncValues(CallFrame* callFrame)
140 {
141     VM& vm = callFrame->vm();
142     auto scope = DECLARE_THROW_SCOPE(vm);
143
144     JSSet* thisObj = jsDynamicCast<JSSet*>(vm, callFrame->thisValue());
145     if (!thisObj)
146         return JSValue::encode(throwTypeError(callFrame, scope, ASCIILiteral("Cannot create a Set value iterator for a non-Set object.")));
147     return JSValue::encode(JSSetIterator::create(vm, callFrame->jsCallee()->globalObject()->setIteratorStructure(), thisObj, IterateValue));
148 }
149
150 EncodedJSValue JSC_HOST_CALL setProtoFuncEntries(CallFrame* callFrame)
151 {
152     VM& vm = callFrame->vm();
153     auto scope = DECLARE_THROW_SCOPE(vm);
154
155     JSSet* thisObj = jsDynamicCast<JSSet*>(vm, callFrame->thisValue());
156     if (!thisObj)
157         return JSValue::encode(throwTypeError(callFrame, scope, ASCIILiteral("Cannot create a Set entry iterator for a non-Set object.")));
158     return JSValue::encode(JSSetIterator::create(vm, callFrame->jsCallee()->globalObject()->setIteratorStructure(), thisObj, IterateKeyValue));
159 }
160
161 EncodedJSValue JSC_HOST_CALL privateFuncSetIterator(ExecState* exec)
162 {
163
164     ASSERT(jsDynamicCast<JSSet*>(exec->vm(), exec->uncheckedArgument(0)));
165     JSSet* set = jsCast<JSSet*>(exec->uncheckedArgument(0));
166     return JSValue::encode(JSSetIterator::create(exec->vm(), exec->jsCallee()->globalObject()->setIteratorStructure(), set, IterateKey));
167 }
168
169 EncodedJSValue JSC_HOST_CALL privateFuncSetIteratorNext(ExecState* exec)
170 {
171     ASSERT(jsDynamicCast<JSSetIterator*>(exec->vm(), exec->thisValue()));
172     JSSetIterator* iterator = jsCast<JSSetIterator*>(exec->thisValue());
173     JSValue result;
174     if (iterator->next(exec, result)) {
175         JSArray* resultArray = jsCast<JSArray*>(exec->uncheckedArgument(0));
176         resultArray->putDirectIndex(exec, 0, result);
177         return JSValue::encode(jsBoolean(false));
178     }
179     return JSValue::encode(jsBoolean(true));
180 }
181
182 }