Skip to content

Commit 3ba02af

Browse files
committed
Fix cast when return value is pointer
1 parent f2ddb93 commit 3ba02af

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

_tools/ruby_h_to_go/lib/ruby_h_to_go/function_definition.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ def generate_go_content
110110
go_function_lines << call_c_method
111111
go_function_lines.push(*after_call_function_lines)
112112
else
113-
go_function_lines << "ret := #{go_function_typeref}(#{call_c_method})"
113+
cast_func =
114+
if go_function_typeref.start_with?("*")
115+
"(#{go_function_typeref})"
116+
else
117+
go_function_typeref
118+
end
119+
120+
go_function_lines << "ret := #{cast_func}(#{call_c_method})"
114121
go_function_lines.push(*after_call_function_lines)
115122
go_function_lines << "return ret"
116123
end

_tools/ruby_h_to_go/spec/ruby_h_to_go/function_definition_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,36 @@
187187

188188
it { should eq go_content }
189189
end
190+
191+
context "rb_errno_ptr" do
192+
let(:definition) do
193+
RubyHeaderParser::FunctionDefinition.new(
194+
name: "rb_errno_ptr",
195+
definition: "int *rb_errno_ptr(void)",
196+
filepath: "/path/to/include/ruby/ruby.h",
197+
typeref: typedef(type: "int", pointer: :ref),
198+
args: [],
199+
)
200+
end
201+
202+
let(:go_content) do
203+
<<~GO
204+
// RbErrnoPtr calls `rb_errno_ptr` in C
205+
//
206+
// Original definition is following
207+
//
208+
// int *rb_errno_ptr(void)
209+
//
210+
// ref. https://github.com/ruby/ruby/blob/master/include/ruby/ruby.h
211+
func RbErrnoPtr() *int {
212+
ret := (*int)(C.rb_errno_ptr())
213+
return ret
214+
}
215+
216+
GO
217+
end
218+
219+
it { should eq go_content }
220+
end
190221
end
191222
end

_tools/ruby_h_to_go/spec/ruby_header_parser/parser_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@
136136
its(:typeref) { should eq typedef(type: "int") }
137137
its(:args) { should eq args }
138138
end
139+
140+
context "rb_errno_ptr" do
141+
subject { definitions.find { |d| d.name == "rb_errno_ptr" } }
142+
143+
let(:args) do
144+
[]
145+
end
146+
147+
its(:name) { should eq "rb_errno_ptr" }
148+
its(:definition) { should eq "int *rb_errno_ptr(void)" }
149+
its(:filepath) { should be_end_with "/ruby/ruby.h" }
150+
its(:typeref) { should eq typedef(type: "int", pointer: :ref) }
151+
its(:args) { should eq args }
152+
end
139153
end
140154

141155
describe "#extract_static_inline_function_definitions" do

0 commit comments

Comments
 (0)