aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/bindings/webcore/JSEventEmitter.cpp7
-rw-r--r--test/bun.js/event-emitter.test.ts10
2 files changed, 13 insertions, 4 deletions
diff --git a/src/bun.js/bindings/webcore/JSEventEmitter.cpp b/src/bun.js/bindings/webcore/JSEventEmitter.cpp
index 8ef858166..ad10687f9 100644
--- a/src/bun.js/bindings/webcore/JSEventEmitter.cpp
+++ b/src/bun.js/bindings/webcore/JSEventEmitter.cpp
@@ -237,7 +237,7 @@ static inline JSC::EncodedJSValue addListener(JSC::JSGlobalObject* lexicalGlobal
auto result = JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.addListenerForBindings(WTFMove(eventType), WTFMove(listener), once, prepend); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
vm.writeBarrier(&static_cast<JSObject&>(*castedThis), argument1.value());
- return result;
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(castedThis));
}
static inline JSC::EncodedJSValue jsEventEmitterPrototypeFunction_addListenerBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSEventEmitter>::ClassParameter castedThis)
@@ -332,7 +332,7 @@ static inline JSC::EncodedJSValue jsEventEmitterPrototypeFunction_removeListener
auto result = JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.removeListenerForBindings(WTFMove(eventType), WTFMove(listener)); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
vm.writeBarrier(&static_cast<JSObject&>(*castedThis), argument1.value());
- return result;
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(castedThis));
}
JSC_DEFINE_HOST_FUNCTION(jsEventEmitterPrototypeFunction_removeListener, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
@@ -573,10 +573,9 @@ JSC_DEFINE_HOST_FUNCTION(Events_functionOnce,
EnsureStillAliveScope argument2 = callFrame->uncheckedArgument(2);
auto listener = convert<IDLNullable<IDLEventListener<JSEventListener>>>(*lexicalGlobalObject, argument2.value(), *argument0, [](JSC::JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope) { throwArgumentMustBeObjectError(lexicalGlobalObject, scope, 2, "listener", "EventEmitter", "removeListener"); });
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
- auto result = JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.addListenerForBindings(WTFMove(eventType), WTFMove(listener), true, false); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
vm.writeBarrier(argument0, argument2.value());
- return result;
+ RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(argument0));
}
JSC_DEFINE_HOST_FUNCTION(Events_functionOn,
diff --git a/test/bun.js/event-emitter.test.ts b/test/bun.js/event-emitter.test.ts
index d7c320fa1..701b28df3 100644
--- a/test/bun.js/event-emitter.test.ts
+++ b/test/bun.js/event-emitter.test.ts
@@ -119,3 +119,13 @@ for (let create of waysOfCreating) {
expect(called).toBe(true);
});
}
+
+test("EventEmitter.on", () => {
+ var myEmitter = new EventEmitter();
+ expect(myEmitter.on("foo", () => {})).toBe(myEmitter);
+});
+
+test("EventEmitter.off", () => {
+ var myEmitter = new EventEmitter();
+ expect(myEmitter.off("foo", () => {})).toBe(myEmitter);
+});