Skip to content

Commit

Permalink
Stub things out for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RX14 committed Dec 20, 2017
1 parent a81ca9b commit c168035
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 319 deletions.
10 changes: 10 additions & 0 deletions src/callstack.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{% if flag?(:win32) %}
struct CallStack
def self.skip(*args)
# do nothing
end
end

{% skip_file() %}
{% end %}

require "c/dlfcn"
require "c/stdio"
require "c/string"
Expand Down
4 changes: 3 additions & 1 deletion src/crystal/hasher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ struct Crystal::Hasher
private HASH_INF_MINUS = (-314159_i64).unsafe_as(UInt64)

@@seed = uninitialized UInt64[2]
Random::Secure.random_bytes(Slice.new(pointerof(@@seed).as(UInt8*), sizeof(typeof(@@seed))))
{% unless flag?(:win32) %}
Random::Secure.random_bytes(Slice.new(pointerof(@@seed).as(UInt8*), sizeof(typeof(@@seed))))
{% end %}

def initialize(@a : UInt64 = @@seed[0], @b : UInt64 = @@seed[1])
end
Expand Down
12 changes: 9 additions & 3 deletions src/crystal/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ module Crystal

# :nodoc:
def self.remember_blocking_state
@@stdin_is_blocking = IO::FileDescriptor.fcntl(0, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
@@stdout_is_blocking = IO::FileDescriptor.fcntl(1, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
@@stderr_is_blocking = IO::FileDescriptor.fcntl(2, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
{% if flag?(:win32) %}
@@stdin_is_blocking = true
@@stdout_is_blocking = true
@@stderr_is_blocking = true
{% else %}
@@stdin_is_blocking = IO::FileDescriptor.fcntl(0, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
@@stdout_is_blocking = IO::FileDescriptor.fcntl(1, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
@@stderr_is_blocking = IO::FileDescriptor.fcntl(2, LibC::F_GETFL) & LibC::O_NONBLOCK == 0
{% end %}
end

# :nodoc:
Expand Down
6 changes: 5 additions & 1 deletion src/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class Exception
# The backtrace is an array of strings, each containing
# “0xAddress: Function at File Line Column”.
def backtrace?
@callstack.try &.printable_backtrace
{% if flag?(:win32) %}
nil
{% else %}
@callstack.try &.printable_backtrace
{% end %}
end

def to_s(io : IO)
Expand Down
49 changes: 29 additions & 20 deletions src/gc/boehm.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@[Link("pthread")]
{% unless flag?(:win32) %}
@[Link("pthread")]
{% end %}

{% if flag?(:freebsd) %}
@[Link("gc-threaded")]
{% else %}
Expand Down Expand Up @@ -54,10 +57,12 @@ lib LibGC

fun size = GC_size(addr : Void*) : LibC::SizeT

# Boehm GC requires to use GC_pthread_create and GC_pthread_join instead of pthread_create and pthread_join
fun pthread_create = GC_pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*) : LibC::Int
fun pthread_join = GC_pthread_join(thread : LibC::PthreadT, value : Void**) : LibC::Int
fun pthread_detach = GC_pthread_detach(thread : LibC::PthreadT) : LibC::Int
{% unless flag?(:win32) %}
# Boehm GC requires to use GC_pthread_create and GC_pthread_join instead of pthread_create and pthread_join
fun pthread_create = GC_pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*) : LibC::Int
fun pthread_join = GC_pthread_join(thread : LibC::PthreadT, value : Void**) : LibC::Int
fun pthread_detach = GC_pthread_detach(thread : LibC::PthreadT) : LibC::Int
{% end %}
end

module GC
Expand All @@ -77,7 +82,9 @@ module GC
end

def self.init
LibGC.set_handle_fork(1)
{% unless flag?(:win32) %}
LibGC.set_handle_fork(1)
{% end %}
LibGC.init
end

Expand Down Expand Up @@ -146,22 +153,24 @@ module GC
)
end

# :nodoc:
def self.pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*)
LibGC.pthread_create(thread, attr, start, arg)
end
{% unless flag?(:win32) %}
# :nodoc:
def self.pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*)
LibGC.pthread_create(thread, attr, start, arg)
end

# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibGC.pthread_join(thread, out value)
raise Errno.new("pthread_join") unless ret == 0
value
end
# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibGC.pthread_join(thread, out value)
raise Errno.new("pthread_join") unless ret == 0
value
end

# :nodoc:
def self.pthread_detach(thread : LibC::PthreadT)
LibGC.pthread_detach(thread)
end
# :nodoc:
def self.pthread_detach(thread : LibC::PthreadT)
LibGC.pthread_detach(thread)
end
{% end %}

# :nodoc:
def self.stack_bottom
Expand Down
42 changes: 23 additions & 19 deletions src/gc/none.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@[Link("pthread")]
lib LibC
end
{% unless flag?(:win32) %}
@[Link("pthread")]
lib LibC
end
{% end %}

module GC
def self.init
Expand Down Expand Up @@ -46,22 +48,24 @@ module GC
Stats.new(zero, zero, zero, zero, zero)
end

# :nodoc:
def self.pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*)
LibC.pthread_create(thread, attr, start, arg)
end

# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibC.pthread_join(thread, out value)
raise Errno.new("pthread_join") unless ret == 0
value
end

# :nodoc:
def self.pthread_detach(thread : LibC::PthreadT)
LibC.pthread_detach(thread)
end
{% unless flag?(:win32) %}
# :nodoc:
def self.pthread_create(thread : LibC::PthreadT*, attr : LibC::PthreadAttrT*, start : Void* -> Void*, arg : Void*)
LibC.pthread_create(thread, attr, start, arg)
end

# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibC.pthread_join(thread, out value)
raise Errno.new("pthread_join") unless ret == 0
value
end

# :nodoc:
def self.pthread_detach(thread : LibC::PthreadT)
LibC.pthread_detach(thread)
end
{% end %}

@@stack_bottom = Pointer(Void).null

Expand Down
94 changes: 51 additions & 43 deletions src/io.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "c/stdio"
require "c/errno"
require "c/unistd"
{% unless flag?(:win32) %}
require "c/unistd"
{% end %}

# The `IO` class is the basis for all input and output in Crystal.
#
Expand Down Expand Up @@ -71,6 +73,10 @@ abstract class IO
End = 2
end

@encoding : EncodingOptions?
@encoder : Encoder?
@decoder : Decoder?

# Raised when an `IO` operation times out.
#
# ```
Expand Down Expand Up @@ -128,52 +134,54 @@ abstract class IO
def flush
end

# Creates a pair of pipe endpoints (connected to each other)
# and returns them as a two-element `Tuple`.
#
# ```
# reader, writer = IO.pipe
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# ```
def self.pipe(read_blocking = false, write_blocking = false)
pipe_fds = uninitialized StaticArray(LibC::Int, 2)
if LibC.pipe(pipe_fds) != 0
raise Errno.new("Could not create pipe")
end
{% unless flag?(:win32) %}
# Creates a pair of pipe endpoints (connected to each other)
# and returns them as a two-element `Tuple`.
#
# ```
# reader, writer = IO.pipe
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# ```
def self.pipe(read_blocking = false, write_blocking = false)
pipe_fds = uninitialized StaticArray(LibC::Int, 2)
if LibC.pipe(pipe_fds) != 0
raise Errno.new("Could not create pipe")
end

r = IO::FileDescriptor.new(pipe_fds[0], read_blocking)
w = IO::FileDescriptor.new(pipe_fds[1], write_blocking)
r.close_on_exec = true
w.close_on_exec = true
w.sync = true
r = IO::FileDescriptor.new(pipe_fds[0], read_blocking)
w = IO::FileDescriptor.new(pipe_fds[1], write_blocking)
r.close_on_exec = true
w.close_on_exec = true
w.sync = true

{r, w}
end
{r, w}
end

# Creates a pair of pipe endpoints (connected to each other) and passes them
# to the given block. Both endpoints are closed after the block.
#
# ```
# IO.pipe do |reader, writer|
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# end
# ```
def self.pipe(read_blocking = false, write_blocking = false)
r, w = IO.pipe(read_blocking, write_blocking)
begin
yield r, w
ensure
w.flush
r.close
w.close
# Creates a pair of pipe endpoints (connected to each other) and passes them
# to the given block. Both endpoints are closed after the block.
#
# ```
# IO.pipe do |reader, writer|
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# end
# ```
def self.pipe(read_blocking = false, write_blocking = false)
r, w = IO.pipe(read_blocking, write_blocking)
begin
yield r, w
ensure
w.flush
r.close
w.close
end
end
end
{% end %}

# Writes the given object into this `IO`.
# This ends up calling `to_s(io)` on the object.
Expand Down
2 changes: 2 additions & 0 deletions src/io/console.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% skip_file() if flag?(:win32) %}

require "termios"

class IO::FileDescriptor < IO
Expand Down
Loading

0 comments on commit c168035

Please sign in to comment.