We should support CreateThis in the FTL
[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 "Lookup.h"
36
37 #include "SetPrototype.lut.h"
38
39 namespace JSC {
40
41 const ClassInfo SetPrototype::s_info = { "Set", &Base::s_info, &setPrototypeTable, nullptr, CREATE_METHOD_TABLE(SetPrototype) };
42
43 /* Source for SetIteratorPrototype.lut.h
44 @begin setPrototypeTable
45   forEach   JSBuiltin  DontEnum|Function 0
46   entries   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
55
56 static EncodedJSValue JSC_HOST_CALL setProtoFuncSize(ExecState*);
57
58 void SetPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
59 {
60     Base::finishCreation(vm);
61     ASSERT(inherits(vm, info()));
62     didBecomePrototype();
63
64     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->add, setProtoFuncAdd, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSSetAddIntrinsic);
65     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->clear, setProtoFuncClear, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
66     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->deleteKeyword, setProtoFuncDelete, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
67     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->has, setProtoFuncHas, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSSetHasIntrinsic);
68     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().hasPrivateName(), setProtoFuncHas, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSSetHasIntrinsic);
69     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().addPrivateName(), setProtoFuncAdd, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSSetAddIntrinsic);
70
71     JSFunction* values = JSFunction::create(vm, setPrototypeValuesCodeGenerator(vm), globalObject);
72     putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().valuesPublicName(), values, static_cast<unsigned>(PropertyAttribute::DontEnum));
73     putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().keysPublicName(), values, static_cast<unsigned>(PropertyAttribute::DontEnum));
74     putDirectWithoutTransition(vm, vm.propertyNames->iteratorSymbol, values, static_cast<unsigned>(PropertyAttribute::DontEnum));
75     putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Set"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
76
77     JSC_NATIVE_GETTER(vm.propertyNames->size, setProtoFuncSize, PropertyAttribute::DontEnum | PropertyAttribute::Accessor);
78 }
79
80 ALWAYS_INLINE static JSSet* getSet(CallFrame* callFrame, JSValue thisValue)
81 {
82     VM& vm = callFrame->vm();
83     auto scope = DECLARE_THROW_SCOPE(vm);
84
85     if (UNLIKELY(!thisValue.isCell())) {
86         throwVMError(callFrame, scope, createNotAnObjectError(callFrame, thisValue));
87         return nullptr;
88     }
89     auto* set = jsDynamicCast<JSSet*>(vm, thisValue.asCell());
90     if (LIKELY(set))
91         return set;
92     throwTypeError(callFrame, scope, "Set operation called on non-Set object"_s);
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()));
137 }
138
139 }