diff options
author | 2023-02-03 17:04:47 -0800 | |
---|---|---|
committer | 2023-02-03 17:04:47 -0800 | |
commit | 4590e2b83acbab63f1516c795f7f79a22f5d3083 (patch) | |
tree | 48a67bc6292e52d7979c3d7449ef03105b605ca3 | |
parent | 2758e0cab9286acbb14a6b4f6d367f078a7a7350 (diff) | |
download | bun-4590e2b83acbab63f1516c795f7f79a22f5d3083.tar.gz bun-4590e2b83acbab63f1516c795f7f79a22f5d3083.tar.zst bun-4590e2b83acbab63f1516c795f7f79a22f5d3083.zip |
Add test for propagating exception
-rw-r--r-- | src/bun.js/api/bun.zig | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 8 | ||||
-rw-r--r-- | test/bun.js/setTimeout.test.js | 23 |
3 files changed, 28 insertions, 5 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig index d7e42dc90..da42e5feb 100644 --- a/src/bun.js/api/bun.zig +++ b/src/bun.js/api/bun.zig @@ -2922,7 +2922,7 @@ pub const Timer = struct { this.deinit(); // get the value out of the promise - _ = promise.result(this.globalThis.vm()); + _ = promise.result(globalThis.vm()); }, .Pending => { result.then(globalThis, this, CallbackJob__onResolve, CallbackJob__onReject); diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 7aa3a5bb9..5eeaf9804 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -797,11 +797,11 @@ JSC_DEFINE_HOST_FUNCTION(functionBunSleepThenCallback, RELEASE_ASSERT(callFrame->argumentCount() == 1); JSPromise* promise = jsCast<JSC::JSPromise*>(callFrame->argument(0)); + RELEASE_ASSERT(promise); - // TODO: optimize this some more promise->resolve(globalObject, JSC::jsUndefined()); - return JSC::JSValue::encode(jsUndefined()); + return JSC::JSValue::encode(promise); } JSC_DEFINE_HOST_FUNCTION(functionBunSleep, @@ -818,7 +818,8 @@ JSC_DEFINE_HOST_FUNCTION(functionBunSleep, Zig::GlobalObject* global = JSC::jsCast<Zig::GlobalObject*>(globalObject); JSC::JSPromise* promise = JSC::JSPromise::create(vm, globalObject->promiseStructure()); - return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(global->bunSleepThenCallback()), JSC::JSValue::encode(millisecondsValue), JSValue::encode(promise)); + Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(global->bunSleepThenCallback()), JSC::JSValue::encode(millisecondsValue), JSValue::encode(promise)); + return JSC::JSValue::encode(promise); } static JSC_DEFINE_HOST_FUNCTION(functionSetInterval, @@ -3537,7 +3538,6 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) visitor.append(thisObject->m_JSFetchHeadersSetterValue); visitor.append(thisObject->m_JSTextEncoderSetterValue); visitor.append(thisObject->m_JSURLSearchParamsSetterValue); - thisObject->m_JSArrayBufferSinkClassStructure.visit(visitor); thisObject->m_JSBufferListClassStructure.visit(visitor); diff --git a/test/bun.js/setTimeout.test.js b/test/bun.js/setTimeout.test.js index 2670f8519..52430bd03 100644 --- a/test/bun.js/setTimeout.test.js +++ b/test/bun.js/setTimeout.test.js @@ -104,3 +104,26 @@ it("Bun.sleep", async () => { expect(sleeps).toBe(3); }); + +it("Bun.sleep propagates exceptions", async () => { + try { + await Bun.sleep(1).then(a => { + throw new Error("TestPassed"); + }); + throw "Should not reach here"; + } catch (err) { + expect(err.message).toBe("TestPassed"); + } +}); + +it("node.js timers/promises setTimeout propagates exceptions", async () => { + const { setTimeout } = require("timers/promises"); + try { + await setTimeout(1).then(a => { + throw new Error("TestPassed"); + }); + throw "Should not reach here"; + } catch (err) { + expect(err.message).toBe("TestPassed"); + } +}); |