Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow /SUBSYSTEM:WINDOWS on Windows #13332

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,19 @@ module Crystal::System::FileDescriptor
end

private def windows_handle
FileDescriptor.windows_handle!(fd)
end

def self.windows_handle(fd)
ret = LibC._get_osfhandle(fd)
return LibC::INVALID_HANDLE_VALUE if ret == -1 || ret == -2
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
LibC::HANDLE.new(ret)
end

def self.windows_handle!(fd)
ret = LibC._get_osfhandle(fd)
raise RuntimeError.from_errno("_get_osfhandle") if ret == -1
raise RuntimeError.new("_get_osfhandle returned -2") if ret == -2
LibC::HANDLE.new(ret)
end

Expand Down Expand Up @@ -154,9 +165,7 @@ module Crystal::System::FileDescriptor
end

def self.pread(fd, buffer, offset)
handle = LibC._get_osfhandle(fd)
raise IO::Error.from_errno("_get_osfhandle") if handle == -1
handle = LibC::HANDLE.new(handle)
handle = windows_handle!(fd)

overlapped = LibC::OVERLAPPED.new
overlapped.union.offset.offset = LibC::DWORD.new(offset)
Expand All @@ -172,9 +181,8 @@ module Crystal::System::FileDescriptor

def self.from_stdio(fd)
console_handle = false
handle = LibC._get_osfhandle(fd)
if handle != -1
handle = LibC::HANDLE.new(handle)
handle = windows_handle(fd)
if handle != LibC::INVALID_HANDLE_VALUE
# TODO: use `out old_mode` after implementing interpreter out closured var
old_mode = uninitialized LibC::DWORD
if LibC.GetConsoleMode(handle, pointerof(old_mode)) != 0
Expand All @@ -187,7 +195,7 @@ module Crystal::System::FileDescriptor
end
end

io = IO::FileDescriptor.new(fd)
io = IO::FileDescriptor.new(fd, blocking: true)
# Set sync or flush_on_newline as described in STDOUT and STDERR docs.
# See https://crystal-lang.org/api/toplevel.html#STDERR
if console_handle
Expand Down
4 changes: 1 addition & 3 deletions src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ struct Crystal::System::Process
end

private def self.handle_from_io(io : IO::FileDescriptor, parent_io)
ret = LibC._get_osfhandle(io.fd)
raise RuntimeError.from_winerror("_get_osfhandle") if ret == -1
source_handle = LibC::HANDLE.new(ret)
source_handle = FileDescriptor.windows_handle!(io.fd)

cur_proc = LibC.GetCurrentProcess
if LibC.DuplicateHandle(cur_proc, source_handle, cur_proc, out new_handle, 0, true, LibC::DUPLICATE_SAME_ACCESS) == 0
Expand Down