Skip to content

Commit 4df9902

Browse files
authored
Merge pull request #131 from sue445/add_types
Add types
2 parents 07e9018 + 4dcd425 commit 4df9902

File tree

19 files changed

+296
-40
lines changed

19 files changed

+296
-40
lines changed

.rubocop_todo.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-09-15 15:09:33 UTC using RuboCop version 1.66.1.
3+
# on 2024-09-17 16:12:35 UTC using RuboCop version 1.66.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 4
9+
# Offense count: 5
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
1212
Max: 64
1313

14-
# Offense count: 2
14+
# Offense count: 1
1515
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
1616
# AllowedMethods: refine
1717
Metrics/BlockLength:
18-
Max: 40
18+
Max: 41
19+
20+
# Offense count: 1
21+
# Configuration parameters: CountBlocks, CountModifierForms.
22+
Metrics/BlockNesting:
23+
Max: 4
1924

20-
# Offense count: 2
25+
# Offense count: 3
2126
# Configuration parameters: CountComments, CountAsOne.
2227
Metrics/ClassLength:
23-
Max: 169
28+
Max: 175
2429

2530
# Offense count: 4
2631
# Configuration parameters: AllowedMethods, AllowedPatterns.
2732
Metrics/CyclomaticComplexity:
28-
Max: 13
33+
Max: 17
2934

3035
# Offense count: 9
3136
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
3237
Metrics/MethodLength:
3338
Max: 58
3439

35-
# Offense count: 4
40+
# Offense count: 3
3641
# Configuration parameters: AllowedMethods, AllowedPatterns.
3742
Metrics/PerceivedComplexity:
3843
Max: 15

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source "https://rubygems.org"
55
gem "rake"
66
gem "rspec"
77
gem "rspec-its"
8+
gem "rspec-parameterized"
89
gem "rubocop", require: false
910
gem "rubocop_auto_corrector", require: false
1011
gem "serverspec"

Gemfile.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ GEM
1515
ast (2.4.2)
1616
base64 (0.2.0)
1717
bigdecimal (3.1.8)
18+
binding_of_caller (1.0.1)
19+
debug_inspector (>= 1.2.0)
1820
concurrent-ruby (1.3.4)
1921
connection_pool (2.4.1)
2022
csv (3.3.0)
23+
debug_inspector (1.2.0)
2124
diff-lcs (1.5.1)
2225
drb (2.2.1)
2326
ffi (1.17.0)
@@ -42,6 +45,10 @@ GEM
4245
ast (~> 2.4.1)
4346
racc
4447
power_assert (2.0.3)
48+
proc_to_ast (0.2.0)
49+
parser
50+
rouge
51+
unparser
4552
racc (1.8.1)
4653
rainbow (3.1.1)
4754
rake (13.2.1)
@@ -53,6 +60,7 @@ GEM
5360
rbs (3.5.3)
5461
logger
5562
regexp_parser (2.9.2)
63+
rouge (4.4.0)
5664
rspec (3.13.0)
5765
rspec-core (~> 3.13.0)
5866
rspec-expectations (~> 3.13.0)
@@ -68,6 +76,17 @@ GEM
6876
rspec-mocks (3.13.1)
6977
diff-lcs (>= 1.2.0, < 2.0)
7078
rspec-support (~> 3.13.0)
79+
rspec-parameterized (1.0.2)
80+
rspec-parameterized-core (< 2)
81+
rspec-parameterized-table_syntax (< 2)
82+
rspec-parameterized-core (1.0.1)
83+
parser
84+
proc_to_ast (>= 0.2.0)
85+
rspec (>= 2.13, < 4)
86+
unparser
87+
rspec-parameterized-table_syntax (1.0.1)
88+
binding_of_caller
89+
rspec-parameterized-core (< 2)
7190
rspec-support (3.13.1)
7291
rubocop (1.66.1)
7392
json (~> 2.3)
@@ -120,6 +139,9 @@ GEM
120139
tzinfo (2.0.6)
121140
concurrent-ruby (~> 1.0)
122141
unicode-display_width (2.6.0)
142+
unparser (0.6.15)
143+
diff-lcs (~> 1.3)
144+
parser (>= 3.3.0)
123145
yard (0.9.37)
124146

125147
PLATFORMS
@@ -131,6 +153,7 @@ DEPENDENCIES
131153
rake-compiler
132154
rspec
133155
rspec-its
156+
rspec-parameterized
134157
rubocop
135158
rubocop_auto_corrector
136159
serverspec

_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RubyHToGo
55
class ArgumentDefinition
66
extend Forwardable
77

8-
def_delegators :@definition, :==, :type, :type=, :name, :name=, :pointer, :pointer=, :pointer?
8+
def_delegators :@definition, :==, :type, :type=, :name, :name=, :pointer, :pointer=, :pointer?, :length, :length=
99

1010
include Helper
1111

@@ -33,5 +33,12 @@ def go_name
3333
def go_function_arg
3434
"#{go_name} #{ruby_c_type_to_go_type(type, pointer:, type: :arg)}"
3535
end
36+
37+
# @return [String]
38+
def cast_to_cgo
39+
return "toCArray[#{ruby_c_type_to_go_type(type)}, #{cast_to_cgo_type(type)}](#{go_name})" if pointer == :array
40+
41+
"#{cast_to_cgo_type(type)}(#{go_name})"
42+
end
3643
end
3744
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def copy_go_files
6868
# with automatically generated files. (Currently output to `dist/` as it is incomplete)
6969
%w[
7070
c_types.go
71+
types.go
7172
wrapper.go
7273
].each do |file|
7374
FileUtils.cp(File.join(__dir__, "..", "..", "..", "..", "ruby", file), dist_dir)

_tools/ruby_h_to_go/lib/ruby_h_to_go/function_definition.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def generate_go_content
5757
call_c_method = "C.#{name}("
5858

5959
casted_go_args = []
60-
char_var_count = args.count { |c_arg| c_arg.type == "char" && c_arg.pointer }
60+
char_var_count = args.count { |c_arg| c_arg.type == "char" && c_arg.pointer == :ref }
6161

6262
before_call_function_lines = []
6363
after_call_function_lines = []
6464

6565
args.each do |c_arg|
66-
if c_arg.type == "char" && c_arg.pointer?
66+
if c_arg.type == "char" && c_arg.pointer == :ref
6767
if char_var_count >= 2
6868
char_var_name = "char#{snake_to_camel(c_arg.go_name)}"
6969
clean_var_name = "cleanChar#{c_arg.go_name}"
@@ -83,14 +83,14 @@ def generate_go_content
8383
else
8484
c_var_name = "c#{snake_to_camel(c_arg.go_name)}"
8585

86-
before_call_function_lines << "var #{c_var_name} C.#{c_arg.type}"
86+
before_call_function_lines << "var #{c_var_name} #{cast_to_cgo_type(c_arg.type)}"
8787
after_call_function_lines <<
8888
"*#{c_arg.go_name} = #{ruby_c_type_to_go_type(c_arg.type, type: :arg)}(#{c_var_name})"
8989

9090
casted_go_args << "&#{c_var_name}"
9191
end
9292
else
93-
casted_go_args << "#{cast_to_cgo_type(c_arg.type)}(#{c_arg.go_name})"
93+
casted_go_args << c_arg.cast_to_cgo
9494
end
9595
end
9696

_tools/ruby_h_to_go/lib/ruby_h_to_go/helper.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ def ruby_c_type_to_go_type(typename, type: nil, pointer: nil)
4949
if pointer
5050
case typename
5151
when "char", "const char"
52-
case type
53-
when :arg, :return
54-
return "string"
55-
else
56-
return "char2String"
52+
if pointer == :ref
53+
case type
54+
when :arg, :return
55+
return "string"
56+
else
57+
return "char2String"
58+
end
5759
end
5860
when "void"
5961
return "unsafe.Pointer"
@@ -69,8 +71,14 @@ def ruby_c_type_to_go_type(typename, type: nil, pointer: nil)
6971
case typename
7072
when "unsigned int", "unsigned long"
7173
return "uint"
74+
when "unsigned short"
75+
return "Ushort"
7276
when "unsigned char"
7377
return "Uchar"
78+
when "LONG_LONG"
79+
return "Longlong"
80+
when "unsigned LONG_LONG"
81+
return "Ulonglong"
7482
when /^VALUE\s*\(\*func\)\s*\(ANYARGS\)$/
7583
return "unsafe.Pointer"
7684
when /^[A-Z]+$/, "int"
@@ -94,8 +102,12 @@ def cast_to_cgo_type(typename)
94102
return "C.uint"
95103
when "unsigned char"
96104
return "C.uchar"
97-
when "VALUE*"
98-
return "toCValueArray"
105+
when "unsigned short"
106+
return "C.ushort"
107+
when "LONG_LONG"
108+
return "C.Longlong"
109+
when "unsigned LONG_LONG"
110+
return "C.Ulonglong"
99111
when /^VALUE\s*\(\*func\)\s*\(ANYARGS\)$/
100112
return "toCPointer"
101113
end

_tools/ruby_h_to_go/lib/ruby_header_parser/argument_definition.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,29 @@ class ArgumentDefinition
1212
attr_accessor :name
1313

1414
# @!attribute pointer
15-
# @return [Symbol,nil]
15+
# @return [Symbol,nil] :ref, :array
1616
attr_accessor :pointer
1717

18+
# @!attribute length
19+
# @return [Integer]
20+
attr_accessor :length
21+
1822
# @param type [String]
1923
# @param name [String]
20-
# @param pointer [Symbol,nil] :ref
21-
def initialize(type:, name:, pointer: nil)
24+
# @param pointer [Symbol,nil] :ref, :array
25+
# @param length [String]
26+
def initialize(type:, name:, pointer: nil, length: 0)
2227
@type = type
2328
@name = name
2429
@pointer = pointer
30+
@length = length
2531
end
2632

2733
# @param other [ArgumentDefinition]
2834
# @return [Boolean]
2935
def ==(other)
30-
other.is_a?(ArgumentDefinition) && type == other.type && name == other.name && pointer == other.pointer
36+
other.is_a?(ArgumentDefinition) && type == other.type && name == other.name && pointer == other.pointer &&
37+
length == other.length
3138
end
3239

3340
# @return [Boolean]

_tools/ruby_h_to_go/lib/ruby_header_parser/parser.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def should_generate_struct?(struct_name)
159159
end
160160

161161
ALLOW_TYPE_NAME_PREFIXES = %w[rb_ st_].freeze
162-
ALLOW_TYPE_NAMES = %w[id value long_long].freeze
162+
ALLOW_TYPE_NAMES = %w[id value].freeze
163163

164164
# Whether generate C type to go
165165
# @param type_name [String]
@@ -196,25 +196,33 @@ def parse_definition_args(function_name, signature)
196196
)
197197
else
198198
loop do
199-
break unless parts[-1].start_with?("*")
199+
pointer_index = parts.index("*")
200+
break unless pointer_index
200201

201-
parts[-1].delete_prefix!("*")
202-
parts[-2] << "*"
202+
parts[pointer_index - 1] << "*"
203+
parts.delete_at(pointer_index)
204+
end
205+
206+
pointer = nil
207+
length = 0
208+
209+
if parts[-1] =~ /\[([0-9]+)\]$/
210+
parts[-1].gsub!(/\[([0-9]+)\]$/, "")
211+
length = ::Regexp.last_match(1).to_i
212+
pointer = :array
203213
end
204214

205215
unless parts[-1] =~ /^[0-9a-zA-Z_]+$/
206216
# last elements isn't dummy argument
207217
parts << "arg#{arg_pos}"
208218
end
209219

210-
type = parts[0...-1].join(" ")
211-
type = type.delete_prefix("const ").delete_prefix("volatile ").delete_prefix("struct ").strip
220+
type = Util.sanitize_type(parts[0...-1].join(" "))
212221
name = parts[-1]
213222

214-
pointer = nil
215223
if type.match?(/\*+$/)
216224
type = type.gsub(/\*+$/, "").strip
217-
pointer = function_arg_pointer_hint(function_name, arg_pos - 1)
225+
pointer ||= function_arg_pointer_hint(function_name, arg_pos - 1)
218226
elsif /^void\s*\s/.match?(type) || /\(.*\)/.match?(type)
219227
# function pointer (e.g. void *(*func)(void *)) is treated as `void*`
220228
type = "void"
@@ -225,6 +233,7 @@ def parse_definition_args(function_name, signature)
225233
type:,
226234
name:,
227235
pointer:,
236+
length:,
228237
)
229238
end
230239
end.compact
@@ -245,6 +254,8 @@ def function_arg_pointer_hint(function_name, index)
245254
# @return [RubyHeaderParser::TyperefDefinition]
246255
def create_typeref(definition, function_name)
247256
typeref_type = definition[0...definition.index(function_name)].gsub("char *", "char*").strip
257+
typeref_type = Util.sanitize_type(typeref_type)
258+
248259
typeref_pointer = nil
249260
if typeref_type.match?(/\*+$/)
250261
typeref_type = typeref_type.gsub(/\*+$/, "").strip

_tools/ruby_h_to_go/lib/ruby_header_parser/util.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ def self.find_field(array, field_name)
1919
def self.split_signature(signature)
2020
signature.scan(/[^,]+\([^()]*\)|[^,]+/).map(&:strip)
2121
end
22+
23+
# @param type [String]
24+
# @return [String]
25+
def self.sanitize_type(type)
26+
type.gsub(/(RUBY_EXTERN|enum|volatile|const|struct)\s+/i, "").gsub("const*", "").strip
27+
end
2228
end
2329
end

0 commit comments

Comments
 (0)