Skip to content

Commit fea8132

Browse files
Added way to generate #to_s
1 parent f8cd221 commit fea8132

File tree

2 files changed

+25
-80
lines changed

2 files changed

+25
-80
lines changed

lib/macho/load_commands.rb

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def to_h
398398
# the task's address space. Corresponds to LC_SEGMENT.
399399
class SegmentCommand < LoadCommand
400400
# @return [String] the name of the segment
401-
field :segname, :string, :size => 16
401+
field :segname, :string, :size => 16, :to_s => true
402402

403403
# @return [Integer] the memory address of the segment
404404
field :vmaddr, :uint32
@@ -476,11 +476,6 @@ def guess_align
476476
align
477477
end
478478

479-
# @return [String] a string representation of the segment name
480-
def to_s
481-
segname
482-
end
483-
484479
# @return [Hash] a hash representation of this {SegmentCommand}
485480
def to_h
486481
{
@@ -520,7 +515,7 @@ class SegmentCommand64 < SegmentCommand
520515
class DylibCommand < LoadCommand
521516
# @return [LCStr] the library's path
522517
# name as an LCStr
523-
field :name, :lcstr
518+
field :name, :lcstr, :to_s => true
524519

525520
# @return [Integer] the library's build time stamp
526521
field :timestamp, :uint32
@@ -545,11 +540,6 @@ def serialize(context)
545540
compatibility_version].pack(format) + string_payload
546541
end
547542

548-
# @return [String] a string representation of the library's pathname
549-
def to_s
550-
name.to_s
551-
end
552-
553543
# @return [Hash] a hash representation of this {DylibCommand}
554544
def to_h
555545
{
@@ -567,7 +557,7 @@ def to_h
567557
class DylinkerCommand < LoadCommand
568558
# @return [LCStr] the dynamic linker's
569559
# path name as an LCStr
570-
field :name, :lcstr
560+
field :name, :lcstr, :to_s => true
571561

572562
# @param context [SerializationContext]
573563
# the context
@@ -582,11 +572,6 @@ def serialize(context)
582572
[cmd, cmdsize, string_offsets[:name]].pack(format) + string_payload
583573
end
584574

585-
# @return [String] a string representation of the dynamic linker's pathname
586-
def to_s
587-
name.to_s
588-
end
589-
590575
# @return [Hash] a hash representation of this {DylinkerCommand}
591576
def to_h
592577
{
@@ -600,19 +585,14 @@ def to_h
600585
class PreboundDylibCommand < LoadCommand
601586
# @return [LCStr] the library's path
602587
# name as an LCStr
603-
field :name, :lcstr
588+
field :name, :lcstr, :to_s => true
604589

605590
# @return [Integer] the number of modules in the library
606591
field :nmodules, :uint32
607592

608593
# @return [Integer] a bit vector of linked modules
609594
field :linked_modules, :uint32
610595

611-
# @return [String] a string representation of the library's pathname
612-
def to_s
613-
name.to_s
614-
end
615-
616596
# @return [Hash] a hash representation of this {PreboundDylibCommand}
617597
def to_h
618598
{
@@ -707,12 +687,7 @@ class RoutinesCommand64 < RoutinesCommand
707687
# of an umbrella framework. Corresponds to LC_SUB_FRAMEWORK.
708688
class SubFrameworkCommand < LoadCommand
709689
# @return [LCStr] the umbrella framework name as an LCStr
710-
field :umbrella, :lcstr
711-
712-
# @return [String] a string represenation of the umbrella framework name
713-
def to_s
714-
umbrella.to_s
715-
end
690+
field :umbrella, :lcstr, :to_s => true
716691

717692
# @return [Hash] a hash representation of this {SubFrameworkCommand}
718693
def to_h
@@ -726,12 +701,7 @@ def to_h
726701
# of an umbrella framework. Corresponds to LC_SUB_UMBRELLA.
727702
class SubUmbrellaCommand < LoadCommand
728703
# @return [LCStr] the subumbrella framework name as an LCStr
729-
field :sub_umbrella, :lcstr
730-
731-
# @return [String] a string represenation of the sub-umbrella framework name
732-
def to_s
733-
sub_umbrella.to_s
734-
end
704+
field :sub_umbrella, :lcstr, :to_s => true
735705

736706
# @return [Hash] a hash representation of this {SubUmbrellaCommand}
737707
def to_h
@@ -745,12 +715,7 @@ def to_h
745715
# to LC_SUB_LIBRARY.
746716
class SubLibraryCommand < LoadCommand
747717
# @return [LCStr] the sublibrary name as an LCStr
748-
field :sub_library, :lcstr
749-
750-
# @return [String] a string represenation of the sub-library name
751-
def to_s
752-
sublibrary.to_s
753-
end
718+
field :sub_library, :lcstr, :to_s => true
754719

755720
# @return [Hash] a hash representation of this {SubLibraryCommand}
756721
def to_h
@@ -764,12 +729,7 @@ def to_h
764729
# an umbrella framework. Corresponds to LC_SUB_CLIENT.
765730
class SubClientCommand < LoadCommand
766731
# @return [LCStr] the subclient name as an LCStr
767-
field :sub_client, :lcstr
768-
769-
# @return [String] a string represenation of the sub-client name
770-
def to_s
771-
sub_client.to_s
772-
end
732+
field :sub_client, :lcstr, :to_s => true
773733

774734
# @return [Hash] a hash representation of this {SubClientCommand}
775735
def to_h
@@ -972,7 +932,7 @@ def to_h
972932
# Corresponds to LC_RPATH.
973933
class RpathCommand < LoadCommand
974934
# @return [LCStr] the path to add to the run path as an LCStr
975-
field :path, :lcstr
935+
field :path, :lcstr, :to_s => true
976936

977937
# @param context [SerializationContext] the context
978938
# @return [String] the serialized fields of the load command
@@ -986,11 +946,6 @@ def serialize(context)
986946
[cmd, cmdsize, string_offsets[:path]].pack(format) + string_payload
987947
end
988948

989-
# @return [String] a string representation of the run path
990-
def to_s
991-
path.to_s
992-
end
993-
994949
# @return [Hash] a hash representation of this {RpathCommand}
995950
def to_h
996951
{
@@ -1293,11 +1248,6 @@ def version_string
12931248
segs.join(".")
12941249
end
12951250

1296-
# @return [String] an alias for version_string
1297-
def to_s
1298-
version_string
1299-
end
1300-
13011251
# @return [Hash] a hash representation of this {SourceVersionCommand}
13021252
def to_h
13031253
{
@@ -1335,16 +1285,11 @@ class IdentCommand < LoadCommand
13351285
# memory. Corresponds to LC_FVMFILE.
13361286
class FvmfileCommand < LoadCommand
13371287
# @return [LCStr] the pathname of the file being loaded
1338-
field :name, :lcstr
1288+
field :name, :lcstr, :to_s => true
13391289

13401290
# @return [Integer] the virtual address being loaded at
13411291
field :header_addr, :uint32
13421292

1343-
# @return [String] a string representation of the pathname
1344-
def to_s
1345-
name.to_s
1346-
end
1347-
13481293
# @return [Hash] a hash representation of this {FvmfileCommand}
13491294
def to_h
13501295
{
@@ -1358,19 +1303,14 @@ def to_h
13581303
# into memory. Corresponds to LC_LOADFVMLIB and LC_IDFVMLIB.
13591304
class FvmlibCommand < LoadCommand
13601305
# @return [LCStr] the library's target pathname
1361-
field :name, :lcstr
1306+
field :name, :lcstr, :to_s => true
13621307

13631308
# @return [Integer] the library's minor version number
13641309
field :minor_version, :uint32
13651310

13661311
# @return [Integer] the library's header address
13671312
field :header_addr, :uint32
13681313

1369-
# @return [String] a string representation of the target pathname
1370-
def to_s
1371-
name.to_s
1372-
end
1373-
13741314
# @return [Hash] a hash representation of this {FvmlibCommand}
13751315
def to_h
13761316
{
@@ -1385,19 +1325,14 @@ def to_h
13851325
# Corresponds to LC_NOTE.
13861326
class NoteCommand < LoadCommand
13871327
# @return [String] the name of the owner for this note
1388-
field :data_owner, :string, :size => 16
1328+
field :data_owner, :string, :size => 16, :to_s => true
13891329

13901330
# @return [Integer] the offset, within the file, of the note
13911331
field :offset, :uint64
13921332

13931333
# @return [Integer] the size, in bytes, of the note
13941334
field :size, :uint64
13951335

1396-
# @return [String] a string representation of data owner of this note
1397-
def to_s
1398-
data_owner
1399-
end
1400-
14011336
# @return [Hash] a hash representation of this {NoteCommand}
14021337
def to_h
14031338
{
@@ -1419,7 +1354,7 @@ class FilesetEntryCommand < LoadCommand
14191354
field :fileoff, :uint64
14201355

14211356
# @return [LCStr] the entry's ID
1422-
field :entry_id, :lcstr
1357+
field :entry_id, :lcstr, :to_s => true
14231358

14241359
# @return [void]
14251360
field :reserved, :uint32

lib/macho/structure.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ module Fields
7070
# minimum number of required arguments
7171
@min_args = 0
7272

73-
# Used to dynamically create an instance of the inherited class
74-
# according to the defined fields.
7573
# @param args [Array[Value]] list of field parameters
7674
def initialize(*args)
7775
raise ArgumentError, "Invalid number of arguments" if args.size < self.class.min_args
@@ -138,6 +136,7 @@ def inherited(subclass) # rubocop:disable Lint/MissingSuper
138136
# :mask [Integer] bitmask
139137
# :unpack [String] string format
140138
# :default [Value] default value
139+
# :to_s [Boolean] flag for generating #to_s
141140
# @api private
142141
def field(name, type, **options)
143142
raise ArgumentError, "Invalid field type #{type}" unless Fields::FORMAT_CODE.key?(type)
@@ -168,6 +167,8 @@ def field(name, type, **options)
168167
else
169168
def_reader(name, idx)
170169
end
170+
171+
def_to_s(name) if options[:to_s]
171172
end
172173

173174
#
@@ -257,6 +258,15 @@ def def_reader(name, idx)
257258
@values[idx]
258259
end
259260
end
261+
262+
# Generates the to_s method based on the named field.
263+
# @param name [Symbol] name of the field
264+
# @api private
265+
def def_to_s(name)
266+
define_method(:to_s) do
267+
send(name).to_s
268+
end
269+
end
260270
end
261271
end
262272
end

0 commit comments

Comments
 (0)