WeakSetPrototype.cpp [plain text]
#include "config.h"
#include "WeakSetPrototype.h"
#include "JSCInlines.h"
#include "JSWeakSet.h"
namespace JSC {
const ClassInfo WeakSetPrototype::s_info = { "WeakSet", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WeakSetPrototype) };
static JSC_DECLARE_HOST_FUNCTION(protoFuncWeakSetDelete);
static JSC_DECLARE_HOST_FUNCTION(protoFuncWeakSetHas);
static JSC_DECLARE_HOST_FUNCTION(protoFuncWeakSetAdd);
void WeakSetPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->deleteKeyword, protoFuncWeakSetDelete, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->has, protoFuncWeakSetHas, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSWeakSetHasIntrinsic);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->add, protoFuncWeakSetAdd, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSWeakSetAddIntrinsic);
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
ALWAYS_INLINE static JSWeakSet* getWeakSet(JSGlobalObject* globalObject, JSValue value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(!value.isObject())) {
throwTypeError(globalObject, scope, "Called WeakSet function on non-object"_s);
return nullptr;
}
auto* set = jsDynamicCast<JSWeakSet*>(vm, asObject(value));
if (LIKELY(set))
return set;
throwTypeError(globalObject, scope, "Called WeakSet function on a non-WeakSet object"_s);
return nullptr;
}
JSC_DEFINE_HOST_FUNCTION(protoFuncWeakSetDelete, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
auto* set = getWeakSet(globalObject, callFrame->thisValue());
if (!set)
return JSValue::encode(jsUndefined());
JSValue key = callFrame->argument(0);
return JSValue::encode(jsBoolean(key.isObject() && set->remove(asObject(key))));
}
JSC_DEFINE_HOST_FUNCTION(protoFuncWeakSetHas, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
auto* set = getWeakSet(globalObject, callFrame->thisValue());
if (!set)
return JSValue::encode(jsUndefined());
JSValue key = callFrame->argument(0);
return JSValue::encode(jsBoolean(key.isObject() && set->has(asObject(key))));
}
JSC_DEFINE_HOST_FUNCTION(protoFuncWeakSetAdd, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* set = getWeakSet(globalObject, callFrame->thisValue());
EXCEPTION_ASSERT(!!scope.exception() == !set);
if (!set)
return JSValue::encode(jsUndefined());
JSValue key = callFrame->argument(0);
if (!key.isObject())
return JSValue::encode(throwTypeError(globalObject, scope, "Attempted to add a non-object key to a WeakSet"_s));
set->add(vm, asObject(key));
return JSValue::encode(callFrame->thisValue());
}
}