Skip to content

Commit 24babf5

Browse files
authored
Merge pull request #14 from staticfloat/sf/keys_and_friends
Quality of life improvements
2 parents 6eefb9e + 64b00cc commit 24babf5

File tree

9 files changed

+25
-33
lines changed

9 files changed

+25
-33
lines changed

src/Abstract/DynamicLink.jl

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export DynamicLinks API
22
export DynamicLinks,
3-
getindex, length, iterate, lastindex, eltype,
3+
getindex, length, iterate, keys, lastindex, eltype,
44
handle, header
55

66
# Export Dynamic Link API
@@ -12,7 +12,7 @@ export RPath,
1212
handle, rpaths, canonical_rpaths, find_library
1313

1414
# Import iteration protocol
15-
import Base: iterate, length, lastindex
15+
import Base: iterate, keys, length, lastindex
1616

1717
"""
1818
DynamicLinks
@@ -28,6 +28,7 @@ given below, with methods that subclasses must implement marked in emphasis:
2828
- *getindex()*
2929
- *lastindex()*
3030
- iterate()
31+
- keys()
3132
- eltype()
3233
3334
### Misc.
@@ -36,6 +37,7 @@ given below, with methods that subclasses must implement marked in emphasis:
3637
abstract type DynamicLinks{H <: ObjectHandle} end
3738

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

109111
lastindex(rpath::RPath) = lastindex(rpaths(rpath))
110-
iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
112+
keys(rpath::RPath) = 1:length(rpath)
113+
iterate(rpath::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
111114
length(rpath::RPath) = lastindex(rpath)
112115
eltype(::Type{D}) where {D <: RPath} = String
113116
getindex(rpath::RPath, idx) = rpaths(rpath)[idx]

src/Abstract/Section.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export Sections API
22
export Sections,
3-
getindex, length, lastindex, iterate, eltype,
3+
getindex, length, lastindex, iterate, keys, eltype,
44
findall, findfirst,
55
handle, header, format_string
66

@@ -15,7 +15,7 @@ export SectionRef,
1515

1616
# Import Base methods for extension
1717
import Base: read, seek, seekstart, eof, length, eltype, findall,
18-
findfirst, iterate, lastindex
18+
findfirst, iterate, keys, lastindex
1919

2020
"""
2121
Sections
@@ -35,6 +35,7 @@ in emphasis:
3535
- *lastindex()*
3636
- length()
3737
- iterate()
38+
- keys()
3839
- eltype()
3940
4041
### Search
@@ -49,6 +50,7 @@ abstract type Sections{H<:ObjectHandle} end
4950
# Fairly simple iteration interface specification
5051
@mustimplement lastindex(sections::Sections)
5152
length(sections::Sections) = lastindex(sections)
53+
keys(sections::Sections) = 1:length(sections)
5254
iterate(sections::Sections, idx=1) = idx > length(sections) ? nothing : (sections[idx], idx+1)
5355
eltype(::Type{S}) where {S <: Sections} = SectionRef
5456

src/Abstract/Segment.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export Segment, deref, segment_name, segment_offset, segment_file_size,
88
export SegmentRef, segment_number
99

1010
# Import Base stuff for extension
11-
import Base: getindex, length, eltype, findfirst, findall, iterate, lastindex
11+
import Base: getindex, length, eltype, findfirst, findall, iterate, keys, lastindex
1212

1313
"""
1414
Segments
@@ -28,6 +28,7 @@ in emphasis:
2828
- *lastindex()*
2929
- length()
3030
- iterate()
31+
- keys()
3132
- eltype()
3233
3334
### Search
@@ -42,6 +43,7 @@ abstract type Segments{H<:ObjectHandle} end
4243
# Fairly simple iteration interface specification
4344
@mustimplement lastindex(segs::Segments)
4445
length(segs::Segments) = lastindex(segs)
46+
keys(segs::Segments) = 1:length(segs)
4547
iterate(segs::Segments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
4648
eltype(::Type{S}) where {S <: Segments} = SegmentRef
4749

@@ -190,4 +192,4 @@ Return the index of the referred segment.
190192

191193

192194
## Printing
193-
show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H)
195+
show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H)

src/COFF/COFF.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, length, lastindex, iterate, eltype, handle, header, path,
9+
getindex, length, lastindex, iterate, keys, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,

src/ELF/ELFRelocation.jl

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function getindex{T}(s::Relocations{T},n)
5353
RelocationRef{T}(s.sec.handle,unpack(s.sec.handle, T))
5454
end
5555

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

5859
# Utilities for extracting information from relocation entries

src/ELF/ELFSegment.jl

+1-19
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,12 @@ export ELFSegments, ELFSegment, ELFSegment32, ELFSegment64
66
ELF segment header table type, containing information about the number of
77
segments within the ELF object, the location of the segment headers, etc...
88
"""
9-
struct ELFSegments{H <: ELFHandle}
9+
struct ELFSegments{H <: ELFHandle} <: Segments{H}
1010
handle::H
1111
end
1212
Segments(oh::ELFHandle) = ELFSegments(oh)
1313
handle(segs::ELFSegments) = segs.handle
14-
15-
# Iteration
16-
iterate(segs::ELFSegments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
1714
lastindex(segs::ELFSegments) = header(handle(segs)).e_phnum
18-
length(segs::ELFSegments) = lastindex(segs)
19-
eltype(::Type{S}) where {S <: ELFSegments} = ELFSegmentRef
20-
21-
function getindex(segs::ELFSegments{H}, idx) where {H <: ELFHandle}
22-
# Punt off to `getindex_ref`
23-
oh = handle(segs)
24-
return getindex_ref(
25-
segs,
26-
segment_header_offset(oh),
27-
segment_header_size(oh),
28-
segment_header_type(oh),
29-
SegmentRef,
30-
idx
31-
)
32-
end
3315

3416

3517
"""

src/MachO/LoadCmds/DylibCmds.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ Command.
121121
"""
122122
dylib_compatibilty(cmd::DylibCmd) = cmd.stub.compatibilty
123123

124-
@derefmethod dylib_name(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
125-
@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
126-
@derefmethod dylib_version(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
127-
@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd}
124+
@derefmethod dylib_name(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
125+
@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
126+
@derefmethod dylib_version(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
127+
@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd}
128128

129129

130130
"""

src/MachO/MachO.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, length, iterate, lastindex, eltype, handle, header, path,
9+
getindex, length, iterate, keys, lastindex, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,

src/MachO/MachOLoadCmd.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export MachOLoadCmd, MachOLoadCmdHeader, MachOLoadCmds, MachOLoadCmdRef
33

44

55
"""
6-
MachOLoadCmdHeader
6+
MachOLoadCmdHeader
77
88
All MachO Load Commands have a common header, containing information about what
99
kind of Load Command it is, and how large the load command is.
@@ -54,6 +54,7 @@ Allows iteration over the LoadCmds within a Mach-O file.
5454
- getindex()
5555
- lastindex()
5656
- iterate()
57+
- keys()
5758
- length()
5859
- eltype()
5960
@@ -144,6 +145,7 @@ handle(lcs::MachOLoadCmds) = lcs.handle
144145
header(lcs::MachOLoadCmds) = header(handle(lcs))
145146

146147
# Iteration
148+
keys(lcs::MachOLoadCmds) = 1:length(lcs)
147149
iterate(lcs::MachOLoadCmds, idx=1) = idx > length(lcs) ? nothing : (lcs.cmds[idx], idx+1)
148150
lastindex(lcs::MachOLoadCmds) = lastindex(lcs.cmds)
149151
length(lcs::MachOLoadCmds) = length(lcs.cmds)

0 commit comments

Comments
 (0)