Skip to content

Commit

Permalink
Merge pull request #14 from staticfloat/sf/keys_and_friends
Browse files Browse the repository at this point in the history
Quality of life improvements
  • Loading branch information
staticfloat authored Mar 8, 2019
2 parents 6eefb9e + 64b00cc commit 24babf5
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 33 deletions.
9 changes: 6 additions & 3 deletions src/Abstract/DynamicLink.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Export DynamicLinks API
export DynamicLinks,
getindex, length, iterate, lastindex, eltype,
getindex, length, iterate, keys, lastindex, eltype,
handle, header

# Export Dynamic Link API
Expand All @@ -12,7 +12,7 @@ export RPath,
handle, rpaths, canonical_rpaths, find_library

# Import iteration protocol
import Base: iterate, length, lastindex
import Base: iterate, keys, length, lastindex

"""
DynamicLinks
Expand All @@ -28,6 +28,7 @@ given below, with methods that subclasses must implement marked in emphasis:
- *getindex()*
- *lastindex()*
- iterate()
- keys()
- eltype()
### Misc.
Expand All @@ -36,6 +37,7 @@ given below, with methods that subclasses must implement marked in emphasis:
abstract type DynamicLinks{H <: ObjectHandle} end

@mustimplement lastindex(dls::DynamicLinks)
keys(dls::DynamicLinks) = 1:length(dls)
iterate(dls::DynamicLinks, idx=1) = idx > length(dls) ? nothing : (dls[idx], idx+1)
length(dls::DynamicLinks) = lastindex(dls)
eltype(::Type{D}) where {D <: DynamicLinks} = DynamicLink
Expand Down Expand Up @@ -107,7 +109,8 @@ Return the list of paths that will be searched for shared libraries.
@mustimplement rpaths(rpath::RPath)

lastindex(rpath::RPath) = lastindex(rpaths(rpath))
iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
keys(rpath::RPath) = 1:length(rpath)
iterate(rpath::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
length(rpath::RPath) = lastindex(rpath)
eltype(::Type{D}) where {D <: RPath} = String
getindex(rpath::RPath, idx) = rpaths(rpath)[idx]
Expand Down
6 changes: 4 additions & 2 deletions src/Abstract/Section.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Export Sections API
export Sections,
getindex, length, lastindex, iterate, eltype,
getindex, length, lastindex, iterate, keys, eltype,
findall, findfirst,
handle, header, format_string

Expand All @@ -15,7 +15,7 @@ export SectionRef,

# Import Base methods for extension
import Base: read, seek, seekstart, eof, length, eltype, findall,
findfirst, iterate, lastindex
findfirst, iterate, keys, lastindex

"""
Sections
Expand All @@ -35,6 +35,7 @@ in emphasis:
- *lastindex()*
- length()
- iterate()
- keys()
- eltype()
### Search
Expand All @@ -49,6 +50,7 @@ abstract type Sections{H<:ObjectHandle} end
# Fairly simple iteration interface specification
@mustimplement lastindex(sections::Sections)
length(sections::Sections) = lastindex(sections)
keys(sections::Sections) = 1:length(sections)
iterate(sections::Sections, idx=1) = idx > length(sections) ? nothing : (sections[idx], idx+1)
eltype(::Type{S}) where {S <: Sections} = SectionRef

Expand Down
6 changes: 4 additions & 2 deletions src/Abstract/Segment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export Segment, deref, segment_name, segment_offset, segment_file_size,
export SegmentRef, segment_number

# Import Base stuff for extension
import Base: getindex, length, eltype, findfirst, findall, iterate, lastindex
import Base: getindex, length, eltype, findfirst, findall, iterate, keys, lastindex

"""
Segments
Expand All @@ -28,6 +28,7 @@ in emphasis:
- *lastindex()*
- length()
- iterate()
- keys()
- eltype()
### Search
Expand All @@ -42,6 +43,7 @@ abstract type Segments{H<:ObjectHandle} end
# Fairly simple iteration interface specification
@mustimplement lastindex(segs::Segments)
length(segs::Segments) = lastindex(segs)
keys(segs::Segments) = 1:length(segs)
iterate(segs::Segments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
eltype(::Type{S}) where {S <: Segments} = SegmentRef

Expand Down Expand Up @@ -190,4 +192,4 @@ Return the index of the referred segment.


## Printing
show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H)
show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H)
2 changes: 1 addition & 1 deletion src/COFF/COFF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using StructIO
using ObjectFile
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
getindex, length, lastindex, iterate, eltype, handle, header, path,
getindex, length, lastindex, iterate, keys, eltype, handle, header, path,
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,
Expand Down
1 change: 1 addition & 0 deletions src/ELF/ELFRelocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function getindex{T}(s::Relocations{T},n)
RelocationRef{T}(s.sec.handle,unpack(s.sec.handle, T))
end

keys(s::Relocations) = 1:length(s)
iterate(s::Relocations, idx=1) = idx > length(s) ? nothing : (s[idx], idx+1)

# Utilities for extracting information from relocation entries
Expand Down
20 changes: 1 addition & 19 deletions src/ELF/ELFSegment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,12 @@ export ELFSegments, ELFSegment, ELFSegment32, ELFSegment64
ELF segment header table type, containing information about the number of
segments within the ELF object, the location of the segment headers, etc...
"""
struct ELFSegments{H <: ELFHandle}
struct ELFSegments{H <: ELFHandle} <: Segments{H}
handle::H
end
Segments(oh::ELFHandle) = ELFSegments(oh)
handle(segs::ELFSegments) = segs.handle

# Iteration
iterate(segs::ELFSegments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
lastindex(segs::ELFSegments) = header(handle(segs)).e_phnum
length(segs::ELFSegments) = lastindex(segs)
eltype(::Type{S}) where {S <: ELFSegments} = ELFSegmentRef

function getindex(segs::ELFSegments{H}, idx) where {H <: ELFHandle}
# Punt off to `getindex_ref`
oh = handle(segs)
return getindex_ref(
segs,
segment_header_offset(oh),
segment_header_size(oh),
segment_header_type(oh),
SegmentRef,
idx
)
end


"""
Expand Down
8 changes: 4 additions & 4 deletions src/MachO/LoadCmds/DylibCmds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ Command.
"""
dylib_compatibilty(cmd::DylibCmd) = cmd.stub.compatibilty

@derefmethod dylib_name(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
@derefmethod dylib_version(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
@derefmethod dylib_name(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
@derefmethod dylib_version(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}


"""
Expand Down
2 changes: 1 addition & 1 deletion src/MachO/MachO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using StructIO
using ObjectFile
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
getindex, length, iterate, lastindex, eltype, handle, header, path,
getindex, length, iterate, keys, lastindex, eltype, handle, header, path,
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,
Expand Down
4 changes: 3 additions & 1 deletion src/MachO/MachOLoadCmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export MachOLoadCmd, MachOLoadCmdHeader, MachOLoadCmds, MachOLoadCmdRef


"""
MachOLoadCmdHeader
MachOLoadCmdHeader
All MachO Load Commands have a common header, containing information about what
kind of Load Command it is, and how large the load command is.
Expand Down Expand Up @@ -54,6 +54,7 @@ Allows iteration over the LoadCmds within a Mach-O file.
- getindex()
- lastindex()
- iterate()
- keys()
- length()
- eltype()
Expand Down Expand Up @@ -144,6 +145,7 @@ handle(lcs::MachOLoadCmds) = lcs.handle
header(lcs::MachOLoadCmds) = header(handle(lcs))

# Iteration
keys(lcs::MachOLoadCmds) = 1:length(lcs)
iterate(lcs::MachOLoadCmds, idx=1) = idx > length(lcs) ? nothing : (lcs.cmds[idx], idx+1)
lastindex(lcs::MachOLoadCmds) = lastindex(lcs.cmds)
length(lcs::MachOLoadCmds) = length(lcs.cmds)
Expand Down

0 comments on commit 24babf5

Please sign in to comment.