aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/webcore/JSEventEmitterCustom.cpp
blob: 1f4e847808a6490b99ab292cf4130962d0f01980 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "config.h"
#include "JSEventEmitter.h"

#include "EventEmitter.h"
#include "JSDOMWrapperCache.h"
#include "JSEventListener.h"
#include "ZigGlobalObject.h"

#include "JSDOMConstructor.h"
#include "JSDOMConvertBase.h"
#include "JSDOMConvertBoolean.h"
#include "JSDOMConvertDictionary.h"
#include "JSDOMConvertInterface.h"
#include "JSDOMConvertNullable.h"
#include "JSDOMConvertNumbers.h"
#include "JSDOMConvertSequences.h"
#include "JSDOMConvertStrings.h"
#include "BunClientData.h"

namespace WebCore {
using namespace JSC;

JSValue toJSNewlyCreated(JSGlobalObject*, JSDOMGlobalObject* globalObject, Ref<EventEmitter>&& value)
{
    return createWrapper<EventEmitter>(globalObject, WTFMove(value));
}

EventEmitter* JSEventEmitter::toWrapped(VM& vm, JSValue value)
{
    if (value.inherits<JSEventEmitter>())
        return &jsCast<JSEventEmitter*>(asObject(value))->wrapped();
    return nullptr;
}

std::unique_ptr<JSEventEmitterWrapper> jsEventEmitterCast(VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, JSValue thisValue)
{
    if (auto* emitter = jsEventEmitterCastFast(vm, lexicalGlobalObject, thisValue)) {
        return std::make_unique<JSEventEmitterWrapper>(emitter->wrapped(), asObject(thisValue));
    }

    return nullptr;
}

JSEventEmitter* jsEventEmitterCastFast(VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, JSValue thisValue)
{
    if (UNLIKELY(!thisValue.isCell())) {
        return nullptr;
    }

    JSCell* thisCell = thisValue.asCell();
    if (UNLIKELY(!thisCell->isObject())) {
        return nullptr;
    }

    auto* thisObject = asObject(thisCell);

    if (thisObject->inherits<JSEventEmitter>())
        return jsCast<JSEventEmitter*>(thisObject);

    auto clientData = WebCore::clientData(vm);
    auto name = clientData->builtinNames()._eventsPublicName();
    if (JSValue _events = thisObject->getIfPropertyExists(lexicalGlobalObject, name)) {
        if (_events.isCell() && _events.inherits<JSEventEmitter>()) {
            return jsCast<JSEventEmitter*>(asObject(_events));
        }
    }

    auto scope = DECLARE_CATCH_SCOPE(vm);
    auto* globalObject = reinterpret_cast<Zig::GlobalObject*>(lexicalGlobalObject);
    auto impl = EventEmitter::create(*globalObject->scriptExecutionContext());
    impl->setThisObject(thisObject);

    auto throwScope = DECLARE_THROW_SCOPE(vm);
    auto result = toJSNewlyCreated<IDLInterface<EventEmitter>>(*lexicalGlobalObject, *globalObject, throwScope, WTFMove(impl));

    thisObject->putDirect(vm, name, result, 0);

    if (scope.exception()) {
        scope.clearException();
        return nullptr;
    }

    RETURN_IF_EXCEPTION(throwScope, nullptr);

    return jsCast<JSEventEmitter*>(asObject(result));
}

template<typename Visitor>
void JSEventEmitter::visitAdditionalChildren(Visitor& visitor)
{
    wrapped().eventListenerMap().visitJSEventListeners(visitor);
}

DEFINE_VISIT_ADDITIONAL_CHILDREN(JSEventEmitter);

} // namespace WebCore