Skip to content

Commit 34e0a0b

Browse files
authored
Merge pull request #53 from sue445/module_function
Add rb_define_module_function
2 parents 882439c + 89e4d65 commit 34e0a0b

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ ref. https://docs.ruby-lang.org/ja/latest/function/index.html
126126
* [x] `rb_define_method`
127127
* [ ] `rb_define_method_id`
128128
* [x] `rb_define_module`
129-
* [ ] `rb_define_module_function`
129+
* [x] `rb_define_module_function`
130130
* [ ] `rb_define_module_id`
131131
* [x] `rb_define_module_under`
132132
* [ ] `rb_define_private_method`

ruby-internal-method.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ func RbDefineMethod(klass VALUE, mid string, fun unsafe.Pointer, arity int) {
1919

2020
C.rb_define_method(C.VALUE(klass), midChar, toFunctionPointer(fun), C.int(arity))
2121
}
22+
23+
// RbDefineModuleFunction calls `rb_define_module_function` in C
24+
//
25+
// Original definition is following
26+
//
27+
// void rb_define_module_function(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity)
28+
func RbDefineModuleFunction(klass VALUE, mid string, fun unsafe.Pointer, arity int) {
29+
midChar, midCharClean := string2Char(mid)
30+
defer midCharClean()
31+
32+
C.rb_define_module_function(C.VALUE(klass), midChar, toFunctionPointer(fun), C.int(arity))
33+
}

testdata/dummy/ext/dummy/dummy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ VALUE rb_dummy_with_block(VALUE self, VALUE arg);
88
VALUE rb_dummy_hello(VALUE self, VALUE name);
99
VALUE rb_dummy_round_num(VALUE self, VALUE num, VALUE ndigits);
1010
VALUE rb_dummy_to_string(VALUE self, VALUE source);
11+
VALUE rb_dummy_max(VALUE self, VALUE a, VALUE b);
1112
1213
VALUE rb_dummy_unit_kilobyte(VALUE self);
1314
*/
@@ -71,6 +72,18 @@ func rb_dummy_unit_kilobyte(self C.VALUE) C.VALUE {
7172
return C.VALUE(ruby.INT2NUM(result))
7273
}
7374

75+
//export rb_dummy_max
76+
func rb_dummy_max(_ C.VALUE, a C.VALUE, b C.VALUE) C.VALUE {
77+
aLong := ruby.NUM2LONG(ruby.VALUE(a))
78+
bLong := ruby.NUM2LONG(ruby.VALUE(b))
79+
80+
if aLong > bLong {
81+
return C.VALUE(ruby.LONG2NUM(aLong))
82+
}
83+
84+
return C.VALUE(ruby.LONG2NUM(bLong))
85+
}
86+
7487
var rb_mDummy ruby.VALUE
7588

7689
//export Init_dummy
@@ -81,6 +94,7 @@ func Init_dummy() {
8194
ruby.RbDefineSingletonMethod(rb_mDummy, "hello", C.rb_dummy_hello, 1)
8295
ruby.RbDefineSingletonMethod(rb_mDummy, "round_num", C.rb_dummy_round_num, 2)
8396
ruby.RbDefineSingletonMethod(rb_mDummy, "to_string", C.rb_dummy_to_string, 1)
97+
ruby.RbDefineModuleFunction(rb_mDummy, "max", C.rb_dummy_max, 2)
8498

8599
// Create Dummy::InnerClass class
86100
ruby.RbDefineClassUnder(rb_mDummy, "InnerClass", ruby.VALUE(C.rb_cObject))

testdata/dummy/sig/dummy.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module Dummy
1414

1515
def self.to_string: (untyped source) -> String
1616

17+
def self.max: (Integer, Integer) -> Integer
18+
def max: (Integer, Integer) -> Integer
19+
1720
class InnerClass
1821
end
1922

testdata/dummy/spec/dummy_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,38 @@
4747
expect(actual).to eq "123"
4848
end
4949
end
50+
51+
describe ".max" do
52+
context "a > b" do
53+
it "works" do
54+
actual = Dummy.max(3, 2)
55+
expect(actual).to eq 3
56+
end
57+
end
58+
59+
context "a < b" do
60+
it "works" do
61+
actual = Dummy.max(1, 2)
62+
expect(actual).to eq 2
63+
end
64+
end
65+
end
66+
67+
describe "#max" do
68+
include Dummy
69+
70+
context "a > b" do
71+
it "works" do
72+
actual = max(3, 2)
73+
expect(actual).to eq 3
74+
end
75+
end
76+
77+
context "a < b" do
78+
it "works" do
79+
actual = max(1, 2)
80+
expect(actual).to eq 2
81+
end
82+
end
83+
end
5084
end

0 commit comments

Comments
 (0)