@@ -36,6 +36,17 @@ def generate_initial_go_file(go_file_path)
36
36
GO
37
37
end
38
38
39
+ C_TYPE_TO_GO_TYPE = {
40
+ "RUBY_DATA_FUNC" => "unsafe.Pointer" ,
41
+ "long long" => "Longlong" ,
42
+ "rb_alloc_func_t" => "unsafe.Pointer" ,
43
+ "unsigned char" => "Uchar" ,
44
+ "unsigned int" => "uint" ,
45
+ "unsigned long" => "uint" ,
46
+ "unsigned long long" => "Ulonglong" ,
47
+ "unsigned short" => "Ushort" ,
48
+ } . freeze
49
+
39
50
# Convert C type to Go type. (used in wrapper function args and return type etc)
40
51
# @param typename [String]
41
52
# @param pos [Symbol,nil] :arg, :typeref, :return
@@ -45,19 +56,9 @@ def generate_initial_go_file(go_file_path)
45
56
def ruby_c_type_to_go_type ( typename , pos : nil , pointer : nil , pointer_length : 0 )
46
57
return ruby_pointer_c_type_to_go_type ( typename , pos :, pointer :, pointer_length :) if pointer
47
58
59
+ return C_TYPE_TO_GO_TYPE [ typename ] if C_TYPE_TO_GO_TYPE [ typename ]
60
+
48
61
case typename
49
- when "unsigned int" , "unsigned long"
50
- return "uint"
51
- when "unsigned short"
52
- return "Ushort"
53
- when "unsigned char"
54
- return "Uchar"
55
- when "long long"
56
- return "Longlong"
57
- when "unsigned long long"
58
- return "Ulonglong"
59
- when "RUBY_DATA_FUNC" , "rb_alloc_func_t"
60
- return "unsafe.Pointer"
61
62
when /^[A-Z]+$/ , "int"
62
63
# e.g. VALUE
63
64
return typename
@@ -68,36 +69,26 @@ def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
68
69
snake_to_camel ( typename )
69
70
end
70
71
72
+ C_TYPE_TO_CGO_TYPE = {
73
+ "RUBY_DATA_FUNC" => "toCFunctionPointer" ,
74
+ "long long" => "C.longlong" ,
75
+ "rb_io_wait_readwrite" => "C.enum_rb_io_wait_readwrite" ,
76
+ "ruby_value_type" => "C.enum_ruby_value_type" ,
77
+ "unsigned char" => "C.uchar" ,
78
+ "unsigned int" => "C.uint" ,
79
+ "unsigned long" => "C.ulong" ,
80
+ "unsigned long long" => "C.ulonglong" ,
81
+ "unsigned short" => "C.ushort" ,
82
+ "st_hash_type" => "C.struct_st_hash_type" ,
83
+ "timespec" => "C.struct_timespec" ,
84
+ "timeval" => "C.struct_timeval" ,
85
+ } . freeze
86
+
71
87
# Cast C type to cgo type. (Used in wrapper function)
72
88
# @param typename [String]
73
89
# @return [String]
74
90
def cast_to_cgo_type ( typename )
75
- case typename
76
- when "unsigned long"
77
- return "C.ulong"
78
- when "unsigned int"
79
- return "C.uint"
80
- when "unsigned char"
81
- return "C.uchar"
82
- when "unsigned short"
83
- return "C.ushort"
84
- when "long long"
85
- return "C.longlong"
86
- when "unsigned long long"
87
- return "C.ulonglong"
88
- when "timeval"
89
- return "C.struct_timeval"
90
- when "timespec"
91
- return "C.struct_timespec"
92
- when "st_hash_type"
93
- return "C.struct_st_hash_type"
94
- when "ruby_value_type"
95
- return "C.enum_ruby_value_type"
96
- when "rb_io_wait_readwrite"
97
- return "C.enum_rb_io_wait_readwrite"
98
- when "RUBY_DATA_FUNC"
99
- return "toCFunctionPointer"
100
- end
91
+ return C_TYPE_TO_CGO_TYPE [ typename ] if C_TYPE_TO_CGO_TYPE [ typename ]
101
92
102
93
"C.#{ typename } "
103
94
end
@@ -111,43 +102,40 @@ def cast_to_cgo_type(typename)
111
102
# @param pointer_length [Integer]
112
103
# @return [String]
113
104
def ruby_pointer_c_type_to_go_type ( typename , pos :, pointer :, pointer_length :)
105
+ go_type_name =
106
+ if typename == "int" && %i[ return typeref ] . include? ( pos )
107
+ "Int"
108
+ else
109
+ ruby_c_type_to_go_type ( typename , pos :, pointer : nil )
110
+ end
111
+
114
112
case pointer
115
113
when :sref
116
114
return "*unsafe.Pointer" if typename == "void" && pointer_length == 2
117
115
118
- go_type_name = ruby_c_type_to_go_type ( typename , pos :, pointer : nil )
119
116
return "#{ "*" * pointer_length } #{ go_type_name } "
117
+
120
118
when :str_array
121
119
return "[]string"
122
- end
123
120
124
- case typename
125
- when "char" , "const char"
126
- if pointer == :ref
121
+ when :array
122
+ return "[]#{ go_type_name } "
123
+
124
+ when :ref_array
125
+ return "[]*#{ go_type_name } "
126
+
127
+ when :ref
128
+ if typename == "char"
127
129
case pos
128
130
when :arg , :typeref
129
131
return "string"
130
132
else
131
133
return "char2String"
132
134
end
133
135
end
134
- when "void"
135
- return "unsafe.Pointer"
136
136
end
137
137
138
- go_type_name =
139
- if typename == "int" && %i[ return typeref ] . include? ( pos )
140
- "Int"
141
- else
142
- ruby_c_type_to_go_type ( typename , pos :, pointer : nil )
143
- end
144
-
145
- case pointer
146
- when :array
147
- return "[]#{ go_type_name } "
148
- when :ref_array
149
- return "[]*#{ go_type_name } "
150
- end
138
+ return "unsafe.Pointer" if typename == "void"
151
139
152
140
"*#{ go_type_name } "
153
141
end
0 commit comments