Skip to content

Commit 3725ff0

Browse files
committed
Use TypeHelper#ruby_c_type_to_go_type instead of GoUtil.ruby_c_type_to_go_type
1 parent 45b0139 commit 3725ff0

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

_tools/ruby_h_to_go/lib/ruby_h_to_go.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ module RubyHToGo
1313
autoload :FunctionDefinition, "ruby_h_to_go/function_definition"
1414
autoload :StructDefinition, "ruby_h_to_go/struct_definition"
1515
autoload :TypeDefinition, "ruby_h_to_go/type_definition"
16+
autoload :TypeHelper, "ruby_h_to_go/type_helper"
1617
autoload :TyperefDefinition, "ruby_h_to_go/typeref_definition"
1718
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class ArgumentDefinition
77

88
def_delegators :@definition, :==, :type, :type=, :name, :name=, :pointer, :pointer=, :pointer?, :length, :length=
99

10+
include TypeHelper
11+
1012
# @param definition [RubyHeaderParser::ArgumentDefinition]
1113
# @param header_dir [String]
1214
def initialize(definition:)
@@ -33,16 +35,16 @@ def go_name
3335

3436
# @return [String]
3537
def go_function_arg
36-
"#{go_name} #{GoUtil.ruby_c_type_to_go_type(type, pointer:, pointer_length: length, pos: :arg)}"
38+
"#{go_name} #{ruby_c_type_to_go_type(pointer:, pointer_length: length, pos: :arg)}"
3739
end
3840

3941
# @return [String]
4042
def cast_to_cgo
4143
case pointer
4244
when :array
43-
return "toCArray[#{GoUtil.ruby_c_type_to_go_type(type)}, #{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
45+
return "toCArray[#{ruby_c_type_to_go_type}, #{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
4446
when :ref_array
45-
return "toCArray[*#{GoUtil.ruby_c_type_to_go_type(type)}, *#{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
47+
return "toCArray[*#{ruby_c_type_to_go_type}, *#{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
4648
when :sref
4749
return go_name if type == "void" && length == 2
4850

@@ -76,7 +78,7 @@ def generate_go_arguments(char_var_count:, chars_var_count:)
7678
c_var_name = "c#{GoUtil.snake_to_camel(go_name)}"
7779

7880
before_call_function_line = "var #{c_var_name} #{GoUtil.cast_to_cgo_type(type)}"
79-
after_call_function_line = "*#{go_name} = #{GoUtil.ruby_c_type_to_go_type(type, pos: :arg)}(#{c_var_name})"
81+
after_call_function_line = "*#{go_name} = #{ruby_c_type_to_go_type(pos: :arg)}(#{c_var_name})"
8082

8183
["&#{c_var_name}", [before_call_function_line], [after_call_function_line]]
8284
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module RubyHToGo
4+
# type attribute helper
5+
module TypeHelper
6+
# @param pos [Symbol,nil] :arg, :typeref, :return
7+
# @param pointer [Symbol,nil] pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)
8+
# @param pointer_length [Integer]
9+
# @return [String]
10+
def ruby_c_type_to_go_type(pos: nil, pointer: nil, pointer_length: 0)
11+
GoUtil.ruby_c_type_to_go_type(type, pos:, pointer:, pointer_length:)
12+
end
13+
end
14+
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/typeref_definition.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class TyperefDefinition
77

88
def_delegators :@definition, :==, :type, :type=, :pointer, :pointer=, :pointer?
99

10+
include TypeHelper
11+
1012
# @param definition [RubyHeaderParser::TyperefDefinition]
1113
def initialize(definition:)
1214
@definition = definition
@@ -16,14 +18,14 @@ def initialize(definition:)
1618
def go_function_typeref
1719
return "" if type == "void" && !pointer?
1820

19-
GoUtil.ruby_c_type_to_go_type(type, pos: :typeref, pointer:)
21+
ruby_c_type_to_go_type(pos: :typeref, pointer:)
2022
end
2123

2224
# @return [String]
2325
def cast_func_for_function_return
2426
return "" if type == "void" && !pointer?
2527

26-
cast_func = GoUtil.ruby_c_type_to_go_type(type, pos: :return, pointer:)
28+
cast_func = ruby_c_type_to_go_type(pos: :return, pointer:)
2729
return "(#{cast_func})" if cast_func.start_with?("*")
2830

2931
cast_func

0 commit comments

Comments
 (0)