diff options
author | 2023-09-24 03:16:51 -0700 | |
---|---|---|
committer | 2023-09-24 03:16:51 -0700 | |
commit | b6a4161cc5773c3ebf8222d38e9dfaf3d195f6a5 (patch) | |
tree | 079b1582e92fbe67f3b52929ac34015da0cecb55 | |
parent | a5908e9f2704f801587115007c2bec5788bd718a (diff) | |
download | bun-b6a4161cc5773c3ebf8222d38e9dfaf3d195f6a5.tar.gz bun-b6a4161cc5773c3ebf8222d38e9dfaf3d195f6a5.tar.zst bun-b6a4161cc5773c3ebf8222d38e9dfaf3d195f6a5.zip |
Fixes #5985 (#5986)
* Fixes #5985
* Update confirm-fixture.js
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/bun.js/bindings/FFI.zig | 3 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 2 | ||||
-rw-r--r-- | src/bun.js/webcore.zig | 9 | ||||
-rw-r--r-- | test/js/web/confirm-fixture.js | 3 | ||||
-rw-r--r-- | test/js/web/web-globals.test.js | 40 |
5 files changed, 50 insertions, 7 deletions
diff --git a/src/bun.js/bindings/FFI.zig b/src/bun.js/bindings/FFI.zig index 9d16bd78e..da98438ba 100644 --- a/src/bun.js/bindings/FFI.zig +++ b/src/bun.js/bindings/FFI.zig @@ -20,8 +20,9 @@ pub const EncodedJSValue = union_EncodedJSValue; pub export var ValueUndefined: EncodedJSValue = EncodedJSValue{ .asInt64 = @as(i64, @bitCast(@as(c_longlong, @as(c_int, 2) | @as(c_int, 8)))), }; +pub const TrueI64 = @as(i64, @bitCast(@as(c_longlong, (@as(c_int, 2) | @as(c_int, 4)) | @as(c_int, 1)))); pub export var ValueTrue: EncodedJSValue = EncodedJSValue{ - .asInt64 = @as(i64, @bitCast(@as(c_longlong, (@as(c_int, 2) | @as(c_int, 4)) | @as(c_int, 1)))), + .asInt64 = TrueI64, }; pub const JSContext = ?*anyopaque; pub inline fn JSVALUE_IS_CELL(arg_val: EncodedJSValue) bool { diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 6c1e11e34..60f8f9376 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -3034,7 +3034,7 @@ pub const JSValue = enum(JSValueReprInt) { zero = 0, undefined = @as(JSValueReprInt, @bitCast(@as(i64, 0xa))), null = @as(JSValueReprInt, @bitCast(@as(i64, 0x2))), - true = @as(JSValueReprInt, @bitCast(@as(i64, 0x4))), + true = FFI.TrueI64, false = @as(JSValueReprInt, @bitCast(@as(i64, 0x6))), _, diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index 168c4339b..411d3ed2e 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -107,9 +107,11 @@ fn confirm(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callcon // 6. Pause until the user responds either positively or negatively. var stdin = std.io.getStdIn(); - var reader = stdin.reader(); + var unbuffered_reader = stdin.reader(); + var buffered = std.io.bufferedReader(unbuffered_reader); + var reader = buffered.reader(); - const first_byte = reader.readByte() catch { + var first_byte = reader.readByte() catch { return .false; }; @@ -122,13 +124,14 @@ fn confirm(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callcon 'y', 'Y' => { const next_byte = reader.readByte() catch { // They may have said yes, but the stdin is invalid. + return .false; }; if (next_byte == '\n') { // 8. If the user responded positively, return true; // otherwise, the user responded negatively: return false. - return .false; + return .true; } }, else => {}, diff --git a/test/js/web/confirm-fixture.js b/test/js/web/confirm-fixture.js new file mode 100644 index 000000000..908570bce --- /dev/null +++ b/test/js/web/confirm-fixture.js @@ -0,0 +1,3 @@ +const result = confirm("What is your answer?"); + +console.error(result ? "Yes" : "No"); diff --git a/test/js/web/web-globals.test.js b/test/js/web/web-globals.test.js index abcad63f7..74b28c0b0 100644 --- a/test/js/web/web-globals.test.js +++ b/test/js/web/web-globals.test.js @@ -1,6 +1,6 @@ -import { unsafe } from "bun"; +import { spawn } from "bun"; import { expect, it, test } from "bun:test"; -import { withoutAggressiveGC } from "harness"; +import { bunEnv, bunExe, withoutAggressiveGC } from "harness"; test("exists", () => { expect(typeof URL !== "undefined").toBe(true); @@ -233,3 +233,39 @@ test("navigator", () => { expect(navigator.platform).toBe("Linux x86_64"); } }); + +test("confirm (yes)", async () => { + const proc = spawn({ + cmd: [bunExe(), require("path").join(import.meta.dir, "./confirm-fixture.js")], + stderr: "pipe", + stdin: "pipe", + stdout: "pipe", + env: bunEnv, + }); + + proc.stdin.write("Y"); + await proc.stdin.flush(); + + proc.stdin.write("\n"); + await proc.stdin.flush(); + + await proc.exited; + + expect(await new Response(proc.stderr).text()).toBe("Yes\n"); +}); + +test("confirm (no)", async () => { + const proc = spawn({ + cmd: [bunExe(), require("path").join(import.meta.dir, "./confirm-fixture.js")], + stderr: "pipe", + stdin: "pipe", + stdout: "pipe", + env: bunEnv, + }); + + proc.stdin.write("poask\n"); + await proc.stdin.flush(); + await proc.exited; + + expect(await new Response(proc.stderr).text()).toBe("No\n"); +}); |