Skip to content

Commit 347b678

Browse files
authored
Merge pull request #152 from sue445/feature/fix_rstring_char
Fix RSTRING_PTR
2 parents 57be41c + 54392e8 commit 347b678

File tree

8 files changed

+77
-5
lines changed

8 files changed

+77
-5
lines changed

_tools/ruby_h_to_go/lib/ruby_h_to_go/generator_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def generate_initial_go_file(go_file_path)
3939
# Convert C type to Go type. (used in wrapper function args and return type etc)
4040
# @param typename [String]
4141
# @param pos [Symbol,nil] :arg, :typeref, :return
42-
# @param pointer [Symbol,nil] Whether pointer hint
42+
# @param pointer [Symbol,nil] pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)
4343
# @param pointer_length [Integer]
4444
# @return [String]
4545
def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
@@ -107,7 +107,7 @@ def cast_to_cgo_type(typename)
107107
# Convert pointer C type to Go type. (used in wrapper function args and return type etc)
108108
# @param typename [String]
109109
# @param pos [Symbol,nil] :arg, :typeref, :return
110-
# @param pointer [Symbol,nil] Whether pointer hint
110+
# @param pointer [Symbol,nil] pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)
111111
# @param pointer_length [Integer]
112112
# @return [String]
113113
def ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:)

_tools/ruby_h_to_go/lib/ruby_header_parser/data.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def function_arg_pointer_hint(function_name:, pos:)
2222
:ref
2323
end
2424

25+
# @param function_name [String]
26+
# @return [Symbol] :ref, :raw
27+
def function_self_pointer_hint(function_name)
28+
pointer_hint = data["function"]["pointer_hint"].dig(function_name, "self")
29+
return pointer_hint.to_sym if pointer_hint
30+
31+
:ref
32+
end
33+
2534
# rubocop:disable Style/CaseEquality
2635

2736
# Whether generate C function to go

_tools/ruby_h_to_go/lib/ruby_header_parser/data.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ function:
6868
- rb_class_descendants
6969

7070
pointer_hint:
71+
RSTRING_PTR:
72+
self: raw
7173
rb_data_object_make:
7274
4: sref
7375
rb_data_typed_object_make:

_tools/ruby_h_to_go/lib/ruby_header_parser/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def create_typeref(definition:, function_name:, typeref_field:, filepath:, line_
265265
typeref_pointer = nil
266266
if typeref_type.match?(/\*+$/)
267267
typeref_type = typeref_type.gsub(/\*+$/, "").strip
268-
typeref_pointer = :ref
268+
typeref_pointer = data.function_self_pointer_hint(function_name)
269269
end
270270

271271
TyperefDefinition.new(type: typeref_type, pointer: typeref_pointer)

_tools/ruby_h_to_go/spec/ruby_h_to_go/function_definition_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,36 @@
502502

503503
it { should eq go_content }
504504
end
505+
506+
context "RSTRING_PTR" do
507+
let(:definition) do
508+
RubyHeaderParser::FunctionDefinition.new(
509+
name: "RSTRING_PTR",
510+
definition: "RSTRING_PTR(VALUE str)",
511+
typeref: typeref(type: "char", pointer: :raw),
512+
args: [
513+
argument(type: "VALUE", name: "str"),
514+
],
515+
)
516+
end
517+
518+
let(:go_content) do
519+
<<~GO
520+
// RSTRING_PTR calls `RSTRING_PTR` in C
521+
//
522+
// Original definition is following
523+
//
524+
// RSTRING_PTR(VALUE str)
525+
func RSTRING_PTR(str VALUE) *Char {
526+
ret := (*Char)(C.RSTRING_PTR(C.VALUE(str)))
527+
return ret
528+
}
529+
530+
GO
531+
end
532+
533+
it { should eq go_content }
534+
end
505535
end
506536

507537
describe "#go_function_name" do

_tools/ruby_h_to_go/spec/ruby_header_parser/data_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121
end
2222
end
2323

24+
describe "#function_self_pointer_hint" do
25+
subject { data.function_self_pointer_hint(function_name) }
26+
27+
context "found in data.yml" do
28+
let(:function_name) { "RSTRING_PTR" }
29+
30+
it { should eq :raw }
31+
end
32+
33+
context "not found in data.yml" do
34+
let(:function_name) { "rb_class2name" }
35+
36+
it { should eq :ref }
37+
end
38+
end
39+
2440
describe "#should_generate_function?" do
2541
subject { data.should_generate_function?(function_name) }
2642

_tools/ruby_h_to_go/spec/ruby_header_parser/parser_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,21 @@
301301
its(:typeref) { should eq typeref(type: "VALUE") }
302302
its(:args) { should eq args }
303303
end
304+
305+
context "RSTRING_PTR" do
306+
subject { definitions.find { |d| d.name == "RSTRING_PTR" } }
307+
308+
let(:args) do
309+
[
310+
argument(type: "VALUE", name: "str"),
311+
]
312+
end
313+
314+
its(:name) { should eq "RSTRING_PTR" }
315+
its(:definition) { should eq "RSTRING_PTR(VALUE str)" }
316+
its(:typeref) { should eq typeref(type: "char", pointer: :raw) }
317+
its(:args) { should eq args }
318+
end
304319
end
305320

306321
describe "#extract_struct_definitions" do

ruby/function_generated.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)