2 * Copyright (C) 2013, 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "SetPrototype.h"
29 #include "BuiltinNames.h"
31 #include "ExceptionHelpers.h"
32 #include "IteratorOperations.h"
33 #include "JSCInlines.h"
37 #include "SetPrototype.lut.h"
41 const ClassInfo SetPrototype::s_info = { "Set", &Base::s_info, &setPrototypeTable, nullptr, CREATE_METHOD_TABLE(SetPrototype) };
43 /* Source for SetIteratorPrototype.lut.h
44 @begin setPrototypeTable
45 forEach JSBuiltin DontEnum|Function 0
46 entries JSBuiltin DontEnum|Function 0
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*);
56 static EncodedJSValue JSC_HOST_CALL setProtoFuncSize(ExecState*);
58 void SetPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
60 Base::finishCreation(vm);
61 ASSERT(inherits(vm, info()));
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);
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);
77 JSC_NATIVE_GETTER(vm.propertyNames->size, setProtoFuncSize, PropertyAttribute::DontEnum | PropertyAttribute::Accessor);
80 ALWAYS_INLINE static JSSet* getSet(CallFrame* callFrame, JSValue thisValue)
82 VM& vm = callFrame->vm();
83 auto scope = DECLARE_THROW_SCOPE(vm);
85 if (UNLIKELY(!thisValue.isCell())) {
86 throwVMError(callFrame, scope, createNotAnObjectError(callFrame, thisValue));
89 if (LIKELY(thisValue.asCell()->type() == JSSetType))
90 return jsCast<JSSet*>(thisValue);
91 throwTypeError(callFrame, scope, ASCIILiteral("Set operation called on non-Set object"));
95 EncodedJSValue JSC_HOST_CALL setProtoFuncAdd(CallFrame* callFrame)
97 JSValue thisValue = callFrame->thisValue();
98 JSSet* set = getSet(callFrame, thisValue);
100 return JSValue::encode(jsUndefined());
101 set->add(callFrame, callFrame->argument(0));
102 return JSValue::encode(thisValue);
105 EncodedJSValue JSC_HOST_CALL setProtoFuncClear(CallFrame* callFrame)
107 JSSet* set = getSet(callFrame, callFrame->thisValue());
109 return JSValue::encode(jsUndefined());
110 set->clear(callFrame);
111 return JSValue::encode(jsUndefined());
114 EncodedJSValue JSC_HOST_CALL setProtoFuncDelete(CallFrame* callFrame)
116 JSSet* set = getSet(callFrame, callFrame->thisValue());
118 return JSValue::encode(jsUndefined());
119 return JSValue::encode(jsBoolean(set->remove(callFrame, callFrame->argument(0))));
122 EncodedJSValue JSC_HOST_CALL setProtoFuncHas(CallFrame* callFrame)
124 JSSet* set = getSet(callFrame, callFrame->thisValue());
126 return JSValue::encode(jsUndefined());
127 return JSValue::encode(jsBoolean(set->has(callFrame, callFrame->argument(0))));
130 EncodedJSValue JSC_HOST_CALL setProtoFuncSize(CallFrame* callFrame)
132 JSSet* set = getSet(callFrame, callFrame->thisValue());
134 return JSValue::encode(jsUndefined());
135 return JSValue::encode(jsNumber(set->size()));