JSValueInWrappedObject.h [plain text]
#pragma once
#include "DOMWrapperWorld.h"
#include "JSDOMWrapper.h"
#include <JavaScriptCore/JSCJSValue.h>
#include <JavaScriptCore/SlotVisitor.h>
#include <JavaScriptCore/Weak.h>
#include <wtf/Variant.h>
namespace WebCore {
class JSValueInWrappedObject {
public:
JSValueInWrappedObject(JSC::JSValue = { });
JSValueInWrappedObject(const JSValueInWrappedObject&);
operator JSC::JSValue() const;
explicit operator bool() const;
JSValueInWrappedObject& operator=(const JSValueInWrappedObject& other);
void visit(JSC::SlotVisitor&) const;
void clear();
private:
using Weak = JSC::Weak<JSC::JSCell>;
using Value = Variant<JSC::JSValue, Weak>;
static Value makeValue(JSC::JSValue);
Value m_value;
};
JSC::JSValue cachedPropertyValue(JSC::JSGlobalObject&, const JSDOMObject& owner, JSValueInWrappedObject& cacheSlot, const WTF::Function<JSC::JSValue()>&);
inline auto JSValueInWrappedObject::makeValue(JSC::JSValue value) -> Value
{
if (!value.isCell())
return value;
return Weak { value.asCell() };
}
inline JSValueInWrappedObject::JSValueInWrappedObject(JSC::JSValue value)
: m_value(makeValue(JSC::JSValue(value)))
{
}
inline JSValueInWrappedObject::JSValueInWrappedObject(const JSValueInWrappedObject& value)
: m_value(makeValue(value))
{
}
inline JSValueInWrappedObject::operator JSC::JSValue() const
{
return WTF::switchOn(m_value, [] (JSC::JSValue value) {
return value;
}, [] (const Weak& value) {
return value.get();
});
}
inline JSValueInWrappedObject::operator bool() const
{
return JSC::JSValue { *this }.operator bool();
}
inline JSValueInWrappedObject& JSValueInWrappedObject::operator=(const JSValueInWrappedObject& other)
{
m_value = makeValue(JSC::JSValue(other));
return *this;
}
inline void JSValueInWrappedObject::visit(JSC::SlotVisitor& visitor) const
{
return WTF::switchOn(m_value, [] (JSC::JSValue) {
}, [&visitor] (const Weak& value) {
visitor.append(value);
});
}
inline void JSValueInWrappedObject::clear()
{
WTF::switchOn(m_value, [] (Weak& value) {
value.clear();
}, [] (auto&) { });
}
inline JSC::JSValue cachedPropertyValue(JSC::JSGlobalObject& lexicalGlobalObject, const JSDOMObject& owner, JSValueInWrappedObject& cachedValue, const WTF::Function<JSC::JSValue()>& function)
{
if (cachedValue && isWorldCompatible(lexicalGlobalObject, cachedValue))
return cachedValue;
cachedValue = cloneAcrossWorlds(lexicalGlobalObject, owner, function());
ASSERT(isWorldCompatible(lexicalGlobalObject, cachedValue));
return cachedValue;
}
}