#include "root.h" #include "JavaScriptCore/JSGlobalObject.h" #include "JavaScriptCore/ArgList.h" #include "JavaScriptCore/JSGlobalObjectInlines.h" #include "JSEventTarget.h" #include "JavaScriptCore/JSArray.h" #include "wtf/text/MakeString.h" #include "ErrorCode.h" namespace Bun { using namespace JSC; using namespace WebCore; JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeEventsGetEventListeners, (JSGlobalObject * globalObject, CallFrame* callFrame)) { auto& vm = JSC::getVM(globalObject); auto throwScope = DECLARE_THROW_SCOPE(vm); if (callFrame->argumentCount() < 2) { throwTypeError(globalObject, throwScope, "getEventListeners needs 2 arguments"_s); return {}; } JSValue thisValue = callFrame->argument(0); auto* thisObject = jsDynamicCast(thisValue); RETURN_IF_EXCEPTION(throwScope, {}); auto eventType = callFrame->argument(1).toWTFString(globalObject); RETURN_IF_EXCEPTION(throwScope, {}); if (!thisObject) [[unlikely]] { return Bun::throwError(globalObject, throwScope, Bun::ErrorCode::ERR_INVALID_ARG_TYPE, "ERR_INVALID_ARG_TYPE: first argument must be of type EventEmitter"_s); } MarkedArgumentBuffer values; auto& listeners = thisObject->wrapped().eventListeners(WTF::makeAtomString(eventType)); for (auto& listener : listeners) { auto* function = listener->callback().jsFunction(); if (function) { values.append(function); } } RELEASE_AND_RETURN(throwScope, JSValue::encode(constructArray(globalObject, static_cast(nullptr), values))); } }