diff options
author | 2022-12-07 14:06:18 -0800 | |
---|---|---|
committer | 2022-12-07 14:06:18 -0800 | |
commit | 42ea8b24025960bac6e9079c41ca3d4ad0ed90c8 (patch) | |
tree | f2393256a96fd8c727fa99045bdf59bd987a1fbc | |
parent | a13e64c28645f0a1dcd36bd986fc57cf2d91ef7e (diff) | |
download | bun-42ea8b24025960bac6e9079c41ca3d4ad0ed90c8.tar.gz bun-42ea8b24025960bac6e9079c41ca3d4ad0ed90c8.tar.zst bun-42ea8b24025960bac6e9079c41ca3d4ad0ed90c8.zip |
[node:events] EventEmitter#on and EventEmitter#off should return the EventEmitter instance
-rw-r--r-- | src/bun.js/bindings/webcore/JSEventEmitter.cpp | 7 | ||||
-rw-r--r-- | test/bun.js/event-emitter.test.ts | 10 |
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); +}); |