@@ -1633,13 +1633,22 @@ test "creating a child process with stdin and stdout behavior set to StdIo.Pipe"
1633
1633
const child_str2 =
1634
1634
\\ const std = @import("std");
1635
1635
\\ const builtin = @import("builtin");
1636
+ \\ const windowsPtrDigits = std.math.log10(std.math.maxInt(usize));
1637
+ \\ const otherPtrDigits = std.math.log10(std.math.maxInt(u32)) + 1;
1636
1638
\\ pub fn main() !void {
1637
1639
\\ const alloc = std.testing.allocator;
1638
1640
\\ const stdin = std.io.getStdIn();
1639
1641
\\ const stdin_reader = stdin.reader();
1640
1642
\\ const stdin_cont = try stdin_reader.readUntilDelimiterAlloc(alloc, '\n', 2_000);
1641
1643
\\ defer alloc.free(stdin_cont);
1642
- \\ var file_handle = try std.fmt.parseInt(std.os.fd_t, stdin_cont, 11);
1644
+ \\ var file_handle = file_handle: {
1645
+ \\ if (builtin.target.os.tag == .windows) {
1646
+ \\ var handle_int = try std.fmt.parseInt(usize, stdin_cont, windowsPtrDigits);
1647
+ \\ break :file_handle @intToPtr(std.os.windows.HANDLE, handle_int);
1648
+ \\ } else {
1649
+ \\ break :file_handle try std.fmt.parseInt(std.os.fd_t, stdin_cont, otherPtrDigits);
1650
+ \\ }
1651
+ \\ };
1643
1652
\\ var extra_stream_in = std.fs.File{ .handle = file_handle };
1644
1653
\\ defer extra_stream_in.close();
1645
1654
\\ const extra_str_in_rd = extra_stream_in.reader();
@@ -1650,15 +1659,32 @@ const child_str2 =
1650
1659
;
1651
1660
1652
1661
fn testPipeInfo (self : * ChildProcess ) ChildProcess.SpawnError ! void {
1662
+ const windowsPtrDigits : usize = std .math .log10 (math .maxInt (usize ));
1663
+ const otherPtrDigits : usize = std .math .log10 (math .maxInt (u32 )) + 1 ; // +1 for sign
1653
1664
if (self .extra_streams ) | extra_streams | {
1654
1665
for (extra_streams ) | * extra | {
1655
- var buf : [11 ]u8 = [_ ]u8 {0 } ** 11 ; // -2**32 has 11 digits
1666
+ const size = comptime size : {
1667
+ if (builtin .target .os .tag == .windows ) {
1668
+ break :size windowsPtrDigits ;
1669
+ } else {
1670
+ break :size otherPtrDigits ;
1671
+ }
1672
+ };
1673
+ var buf = comptime [_ ]u8 {0 } ** size ;
1656
1674
var s_chpipe_h : []u8 = undefined ;
1657
1675
std .debug .assert (extra .direction == .parent_to_child );
1676
+ const handle = handle : {
1677
+ if (builtin .target .os .tag == .windows ) {
1678
+ // handle is *anyopaque and there is no other way to cast
1679
+ break :handle @ptrToInt (extra .* .input .? .handle );
1680
+ } else {
1681
+ break :handle extra .* .input .? .handle ;
1682
+ }
1683
+ };
1658
1684
s_chpipe_h = std .fmt .bufPrint (
1659
1685
buf [0.. ],
1660
1686
"{d}" ,
1661
- .{extra .* . input .? . handle },
1687
+ .{handle },
1662
1688
) catch unreachable ;
1663
1689
self .stdin .? .writer ().writeAll (s_chpipe_h ) catch unreachable ;
1664
1690
self .stdin .? .writer ().writeAll ("\n " ) catch unreachable ;
0 commit comments