Skip to content

Commit 76aed9c

Browse files
committed
Copy from go-gem-wrapper
1 parent c335053 commit 76aed9c

17 files changed

+1476
-1
lines changed

.rubocop.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
AllCops:
2+
TargetRubyVersion: 3.3
3+
NewCops: enable
4+
SuggestExtensions: false
5+
6+
Exclude:
7+
- "_benchmark/**/*"
8+
- "ruby/testdata/example/vendor/**/*"
9+
10+
# ref. https://github.com/rubocop/rubocop/blob/v1.66.1/config/default.yml
11+
- 'node_modules/**/*'
12+
- 'tmp/**/*'
13+
- 'vendor/**/*'
14+
- '.git/**/*'
15+
16+
Gemspec/RequireMFA:
17+
Exclude:
18+
- ruby/testdata/example/example.gemspec
19+
20+
Layout/DotPosition:
21+
EnforcedStyle: trailing
22+
23+
Layout/HashAlignment:
24+
EnforcedColonStyle: table
25+
EnforcedHashRocketStyle: table
26+
27+
Layout/SpaceAroundOperators:
28+
Exclude:
29+
# Suppress line breaks in spec files (e.g `it { should xxxx }`, `its(:method) { should xxxx }` )
30+
- "**/*_spec.rb"
31+
32+
Lint/BinaryOperatorWithIdenticalOperands:
33+
Exclude:
34+
- "**/*_spec.rb" # for rspec-parameterized
35+
36+
Metrics/AbcSize:
37+
Max: 20
38+
39+
Metrics/BlockLength:
40+
Exclude:
41+
- "*.gemspec"
42+
- "**/*_spec.rb"
43+
44+
Metrics/ClassLength:
45+
Exclude:
46+
- "**/*_test.rb"
47+
48+
Metrics/CyclomaticComplexity:
49+
Max: 13
50+
51+
Metrics/MethodLength:
52+
Max: 28
53+
54+
Metrics/PerceivedComplexity:
55+
Max: 11
56+
57+
Style/Documentation:
58+
Exclude:
59+
- "**/*_test.rb"
60+
61+
Style/FetchEnvVar:
62+
Enabled: false
63+
64+
Style/NumericPredicate:
65+
EnforcedStyle: comparison
66+
67+
Style/SingleLineMethods:
68+
Enabled: false
69+
70+
Style/StringLiterals:
71+
EnforcedStyle: double_quotes
72+
73+
Style/StringLiteralsInInterpolation:
74+
EnforcedStyle: double_quotes
75+
76+
Style/TrailingCommaInArguments:
77+
EnforcedStyleForMultiline: comma
78+
79+
Style/TrailingCommaInArrayLiteral:
80+
EnforcedStyleForMultiline: comma
81+
82+
Style/TrailingCommaInHashLiteral:
83+
EnforcedStyleForMultiline: comma

lib/ruby_header_parser.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
# frozen_string_literal: true
22

3+
require "yaml"
4+
35
require_relative "ruby_header_parser/version"
46

7+
# Parser for ruby.h
58
module RubyHeaderParser
69
class Error < StandardError; end
7-
# Your code goes here...
10+
11+
autoload :ArgumentDefinition, "ruby_header_parser/argument_definition"
12+
autoload :Data, "ruby_header_parser/data"
13+
autoload :EnumDefinition, "ruby_header_parser/enum_definition"
14+
autoload :FunctionDefinition, "ruby_header_parser/function_definition"
15+
autoload :Parser, "ruby_header_parser/parser"
16+
autoload :StructDefinition, "ruby_header_parser/struct_definition"
17+
autoload :TypeDefinition, "ruby_header_parser/type_definition"
18+
autoload :TyperefDefinition, "ruby_header_parser/typeref_definition"
19+
autoload :Util, "ruby_header_parser/util"
820
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module RubyHeaderParser
4+
# argument definition for {RubyHeaderParser::FunctionDefinition}
5+
class ArgumentDefinition
6+
# @!attribute type
7+
# @return [String]
8+
attr_accessor :type
9+
10+
# @!attribute name
11+
# @return [String]
12+
attr_accessor :name
13+
14+
# @!attribute pointer
15+
# @return [Symbol,nil] :ref, :array
16+
attr_accessor :pointer
17+
18+
# @!attribute length
19+
# @return [Integer]
20+
attr_accessor :length
21+
22+
# @param type [String]
23+
# @param name [String]
24+
# @param pointer [Symbol,nil] :ref, :array
25+
# @param length [String]
26+
def initialize(type:, name:, pointer: nil, length: 0)
27+
@type = type
28+
@name = name
29+
@pointer = pointer
30+
@length = length
31+
end
32+
33+
# @param other [ArgumentDefinition]
34+
# @return [Boolean]
35+
def ==(other)
36+
other.is_a?(ArgumentDefinition) && type == other.type && name == other.name && pointer == other.pointer &&
37+
length == other.length
38+
end
39+
40+
# @return [Boolean]
41+
def pointer?
42+
!!pointer
43+
end
44+
end
45+
end

lib/ruby_header_parser/data.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
module RubyHeaderParser
4+
# Manager for `data.yml`
5+
class Data
6+
# @!attribute [r] data
7+
# @return [Hash]
8+
attr_reader :data
9+
10+
def initialize
11+
yaml = File.read(File.join(__dir__, "data.yml"))
12+
@data = YAML.safe_load(yaml, aliases: true, permitted_classes: [Regexp])
13+
end
14+
15+
# @param function_name [String]
16+
# @param pos [Integer] arg position (1 origin)
17+
# @return [Symbol] :ref, :array, :ref_array, :function, :sref, :str_array, :in_ref
18+
def function_arg_pointer_hint(function_name:, pos:)
19+
pointer_hint = data["function"]["pointer_hint"].dig(function_name, pos)
20+
return pointer_hint.to_sym if pointer_hint
21+
22+
:ref
23+
end
24+
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+
34+
# rubocop:disable Style/CaseEquality
35+
36+
# Whether generate C function to go
37+
# @param function_name [String]
38+
# @return [Boolean]
39+
def should_generate_function?(function_name)
40+
return false if data["function"]["exclude_name"].any? { |format| format === function_name }
41+
42+
data["function"]["include_name"].any? { |format| format === function_name }
43+
end
44+
45+
# Whether generate C struct to go
46+
# @param struct_name [String]
47+
# @return [Boolean]
48+
def should_generate_struct?(struct_name)
49+
return false if data["struct"]["exclude_name"].any? { |format| format === struct_name }
50+
51+
data["struct"]["include_name"].any? { |format| format === struct_name }
52+
end
53+
54+
# Whether generate C type to go
55+
# @param type_name [String]
56+
# @return [Boolean]
57+
def should_generate_type?(type_name)
58+
return false if data["type"]["exclude_name"].any? { |format| format === type_name }
59+
60+
data["type"]["include_name"].any? { |format| format === type_name }
61+
end
62+
63+
# Whether generate C enum to go
64+
# @param enum_name [String]
65+
# @return [Boolean]
66+
def should_generate_enum?(enum_name)
67+
return false if data["enum"]["exclude_name"].any? { |format| format === enum_name }
68+
69+
data["enum"]["include_name"].any? { |format| format === enum_name }
70+
end
71+
72+
# rubocop:enable Style/CaseEquality
73+
end
74+
end

lib/ruby_header_parser/data.yml

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
function:
2+
include_name:
3+
- !ruby/regexp /^rb_/i
4+
- !ruby/regexp /^rstring_/i
5+
6+
exclude_name:
7+
# deprecated functions
8+
- !ruby/regexp /^rb_check_safe_str$/i
9+
- !ruby/regexp /^rb_clear_constant_cache$/i
10+
- !ruby/regexp /^rb_clone_setup$/i
11+
- !ruby/regexp /^rb_complex_polar$/i
12+
- !ruby/regexp /^rb_data_object_alloc$/i
13+
- !ruby/regexp /^rb_data_object_get_warning$/i
14+
- !ruby/regexp /^rb_data_object_wrap_warning$/i
15+
- !ruby/regexp /^rb_data_typed_object_alloc$/i
16+
- !ruby/regexp /^rb_dup_setup$/i
17+
- !ruby/regexp /^rb_gc_force_recycle$/i
18+
- !ruby/regexp /^rb_iterate$/i
19+
- !ruby/regexp /^rb_obj_infect$/i
20+
- !ruby/regexp /^rb_obj_infect_raw$/i
21+
- !ruby/regexp /^rb_obj_taint$/i
22+
- !ruby/regexp /^rb_obj_taint_raw$/i
23+
- !ruby/regexp /^rb_obj_taintable$/i
24+
- !ruby/regexp /^rb_obj_tainted$/i
25+
- !ruby/regexp /^rb_obj_tainted_raw$/i
26+
- !ruby/regexp /^rb_scan_args_length_mismatch$/i
27+
- !ruby/regexp /^rb_varargs_bad_length$/i
28+
29+
# internal functions in ruby.h
30+
- rb_scan_args_bad_format
31+
32+
# internal macros
33+
- !ruby/regexp /^rb_define_global_function_(m?[0-9]+|notimpl)$/i
34+
- !ruby/regexp /^rb_define_method_(m?[0-9]+|notimpl)$/i
35+
- !ruby/regexp /^rb_define_method_id_(m?[0-9]+|notimpl)$/i
36+
- !ruby/regexp /^rb_define_module_function_(m?[0-9]+|notimpl)$/i
37+
- !ruby/regexp /^rb_define_private_method_(m?[0-9]+|notimpl)$/i
38+
- !ruby/regexp /^rb_define_protected_method_(m?[0-9]+|notimpl)$/i
39+
- !ruby/regexp /^rb_define_singleton_method_(m?[0-9]+|notimpl)$/i
40+
41+
# FIXME: Both `rb_hash` and `rb_Hash` are defined in ruby.h
42+
# Converting to Go function names would result in both being `RbHash`
43+
# Exclude one of the function names because duplicate function names will result in an error.
44+
- rb_Hash
45+
46+
# Available only in certain platform
47+
- !ruby/regexp /^rb_w32_/i
48+
49+
# FIXME: Workaround for "could not determine kind of name for C.rb_gc_unprotect_logging"
50+
- rb_gc_unprotect_logging
51+
52+
# FIXME: Workaround for "could not determine kind of name for C.rb_iterate_deprecated"
53+
- rb_iterate_deprecated
54+
55+
# FIXME: Workaround for "could not determine kind of name for C.rb_gc_guarded_ptr_val"
56+
- rb_gc_guarded_ptr_val
57+
58+
# FIXME: Workaround for "cannot use _cgo2 (variable of type _Ctype_va_list) as *_Ctype_struct___va_list_tag value in argument to _Cfunc_rb_str_vcatf" on GitHub Actions (Ubuntu 22.04)
59+
- rb_str_vcatf
60+
61+
# FIXME: Workaround for "cannot use _cgo4 (variable of type _Ctype_va_list) as *_Ctype_struct___va_list_tag value in argument to _Cfunc_rb_vrescue2" on GitHub Actions (Ubuntu 22.04)
62+
- rb_vrescue2
63+
64+
# FIXME: Workaround for "cannot use _cgo1 (variable of type _Ctype_va_list) as *_Ctype_struct___va_list_tag value in argument to _Cfunc_rb_vsprintf" on GitHub Actions (Ubuntu 22.04)
65+
- rb_vsprintf
66+
67+
# FIXME: Workaround for "undefined reference to `rb_class_descendants'" on GitHub Actions (Ubuntu 22.04)
68+
- rb_class_descendants
69+
70+
pointer_hint:
71+
RSTRING_PTR:
72+
self: raw
73+
rb_data_object_make:
74+
4: sref
75+
rb_data_typed_object_make:
76+
3: sref
77+
rb_define_global_function:
78+
2: function
79+
rb_define_method:
80+
3: function
81+
rb_define_method_id:
82+
3: function
83+
rb_define_module_function:
84+
3: function
85+
rb_define_private_method:
86+
3: function
87+
rb_define_protected_method:
88+
3: function
89+
rb_define_singleton_method:
90+
3: function
91+
rb_define_variable:
92+
2: in_ref
93+
rb_ensure:
94+
1: function
95+
3: function
96+
rb_exec_recursive:
97+
1: function
98+
rb_exec_recursive_outer:
99+
1: function
100+
rb_exec_recursive_paired:
101+
1: function
102+
rb_exec_recursive_paired_outer:
103+
1: function
104+
rb_feature_provided:
105+
2: sref
106+
rb_find_file_ext:
107+
2: str_array
108+
rb_funcallv:
109+
4: array
110+
rb_funcallv_public:
111+
4: array
112+
rb_get_values_at:
113+
5: function
114+
rb_glob:
115+
2: function
116+
rb_hash_foreach:
117+
2: function
118+
rb_ivar_foreach:
119+
2: function
120+
rb_marshal_define_compat:
121+
3: function
122+
4: function
123+
rb_mutex_synchronize:
124+
2: function
125+
rb_protect:
126+
1: function
127+
rb_rescue:
128+
1: function
129+
3: function
130+
rb_scan_args_set:
131+
10: ref_array
132+
rb_set_end_proc:
133+
1: function
134+
rb_st_insert2:
135+
4: function
136+
rb_thread_call_with_gvl:
137+
1: function
138+
rb_thread_create:
139+
1: function
140+
rb_vrescue2:
141+
1: function
142+
3: function
143+
144+
struct:
145+
include_name:
146+
- !ruby/regexp /^rb_/i
147+
- re_registers
148+
149+
exclude_name:
150+
- rb_data_type_struct
151+
152+
type:
153+
include_name:
154+
- !ruby/regexp /^rb_/i
155+
- !ruby/regexp /^st_/i
156+
- ID
157+
- VALUE
158+
- regex_t
159+
- OnigPosition
160+
- pid_t
161+
- off_t
162+
163+
exclude_name: []
164+
165+
enum:
166+
include_name:
167+
- ruby_value_type
168+
- rb_io_wait_readwrite
169+
170+
exclude_name: []

0 commit comments

Comments
 (0)