Skip to content

Commit 53cf34b

Browse files
authored
Return the number of bytes written from 'write'. (#339)
1 parent baebaa8 commit 53cf34b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/bitcode.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ Base.parse(::Type{Module}, data::Vector; ctx::Context) =
1515

1616
## writer
1717

18-
Base.write(io::IOStream, mod::Module) =
19-
API.LLVMWriteBitcodeToFD(mod, Cint(fd(io)), convert(Bool, false), convert(Bool, true))
20-
2118
Base.convert(::Type{MemoryBuffer}, mod::Module) = MemoryBuffer(
2219
API.LLVMWriteBitcodeToMemoryBuffer(mod))
2320

24-
Base.convert(::Type{Vector{T}}, mod::Module) where {T<:Union{UInt8,Int8}} =
25-
convert(Vector{T}, convert(MemoryBuffer, mod))
21+
function Base.convert(::Type{Vector{T}}, mod::Module) where {T<:Union{UInt8,Int8}}
22+
buf = convert(MemoryBuffer, mod)
23+
vec = convert(Vector{T}, buf)
24+
dispose(buf)
25+
return vec
26+
end
27+
28+
function Base.write(io::IOStream, mod::Module)
29+
# XXX: can't use the LLVM API because it returns 0, not the number of bytes written
30+
#API.LLVMWriteBitcodeToFD(mod, Cint(fd(io)), convert(Bool, false), convert(Bool, true))
31+
buf = convert(MemoryBuffer, mod)
32+
vec = unsafe_wrap(Array, pointer(buf), length(buf))
33+
nb = write(io, vec)
34+
dispose(buf)
35+
return nb
36+
end

src/buffer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ Base.length(membuf::MemoryBuffer) = API.LLVMGetBufferSize(membuf)
5757
Base.pointer(membuf::MemoryBuffer) = convert(Ptr{UInt8}, API.LLVMGetBufferStart(membuf))
5858

5959
Base.convert(::Type{Vector{UInt8}}, membuf::MemoryBuffer) =
60-
unsafe_wrap(Array, pointer(membuf), length(membuf))
60+
copy(unsafe_wrap(Array, pointer(membuf), length(membuf)))

test/bitcode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838

3939
mktemp() do path, io
4040
mark(io)
41-
write(io, source_mod)
41+
@test write(io, source_mod) > 0
4242
flush(io)
4343
reset(io)
4444

0 commit comments

Comments
 (0)