[DFG][FTL] Support MapSet / SetAdd intrinsics
[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     if (LIKELY(thisValue.asCell()->type() == JSSetType))
90         return jsCast<JSSet*>(thisValue);
91     throwTypeError(callFrame, scope, ASCIILiteral("Set operation called on non-Set object"));
92     return nullptr;
93 }
94
95 EncodedJSValue JSC_HOST_CALL setProtoFuncAdd(CallFrame* callFrame)
96 {
97     JSValue thisValue = callFrame->thisValue();
98     JSSet* set = getSet(callFrame, thisValue);
99     if (!set)
100         return JSValue::encode(jsUndefined());
101     set->add(callFrame, callFrame->argument(0));
102     return JSValue::encode(thisValue);
103 }
104
105 EncodedJSValue JSC_HOST_CALL setProtoFuncClear(CallFrame* callFrame)
106 {
107     JSSet* set = getSet(callFrame, callFrame->thisValue());
108     if (!set)
109         return JSValue::encode(jsUndefined());
110     set->clear(callFrame);
111     return JSValue::encode(jsUndefined());
112 }
113
114 EncodedJSValue JSC_HOST_CALL setProtoFuncDelete(CallFrame* callFrame)
115 {
116     JSSet* set = getSet(callFrame, callFrame->thisValue());
117     if (!set)
118         return JSValue::encode(jsUndefined());
119     return JSValue::encode(jsBoolean(set->remove(callFrame, callFrame->argument(0))));
120 }
121
122 EncodedJSValue JSC_HOST_CALL setProtoFuncHas(CallFrame* callFrame)
123 {
124     JSSet* set = getSet(callFrame, callFrame->thisValue());
125     if (!set)
126         return JSValue::encode(jsUndefined());
127     return JSValue::encode(jsBoolean(set->has(callFrame, callFrame->argument(0))));
128 }
129
130 EncodedJSValue JSC_HOST_CALL setProtoFuncSize(CallFrame* callFrame)
131 {
132     JSSet* set = getSet(callFrame, callFrame->thisValue());
133     if (!set)
134         return JSValue::encode(jsUndefined());
135     return JSValue::encode(jsNumber(set->size()));
136 }
137
138 }