WeakObjectRefPrototype.cpp [plain text]
#include "config.h"
#include "WeakObjectRefPrototype.h"
#include "JSCInlines.h"
#include "JSWeakObjectRef.h"
namespace JSC {
const ClassInfo WeakObjectRefPrototype::s_info = { "WeakRef", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WeakObjectRefPrototype) };
static JSC_DECLARE_HOST_FUNCTION(protoFuncWeakRefDeref);
void WeakObjectRefPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->deref, protoFuncWeakRefDeref, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
ALWAYS_INLINE static JSWeakObjectRef* getWeakRef(JSGlobalObject* globalObject, JSValue value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(!value.isObject())) {
throwTypeError(globalObject, scope, "Called WeakRef function on non-object"_s);
return nullptr;
}
auto* ref = jsDynamicCast<JSWeakObjectRef*>(vm, asObject(value));
if (LIKELY(ref))
return ref;
throwTypeError(globalObject, scope, "Called WeakRef function on a non-WeakRef object"_s);
return nullptr;
}
JSC_DEFINE_HOST_FUNCTION(protoFuncWeakRefDeref, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto* ref = getWeakRef(globalObject, callFrame->thisValue());
if (!ref)
return JSValue::encode(jsUndefined());
auto* value = ref->deref(vm);
return value ? JSValue::encode(value) : JSValue::encode(jsNull());
}
}