Skip to content

Commit c336711

Browse files
authored
Merge pull request #54 from sue445/rb_ivar_set
Add rb_ivar_set
2 parents 34e0a0b + 82724cd commit c336711

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ ref. https://docs.ruby-lang.org/ja/latest/function/index.html
239239
* [ ] `rb_iv_set`
240240
* [ ] `rb_ivar_defined`
241241
* [x] `rb_ivar_get`
242-
* [ ] `rb_ivar_set`
242+
* [x] `rb_ivar_set`
243243
* [ ] `rb_jump_tag`
244244
* [ ] `rb_lastline_get`
245245
* [ ] `rb_lastline_set`

ruby-internal-intern-variable.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ import "C"
1515
func RbIvarGet(obj VALUE, name ID) VALUE {
1616
return VALUE(C.rb_ivar_get(C.VALUE(obj), C.ID(name)))
1717
}
18+
19+
// RbIvarSet calls `rb_ivar_set` in C
20+
//
21+
// Original definition is following
22+
//
23+
// VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
24+
func RbIvarSet(obj VALUE, name ID, val VALUE) VALUE {
25+
return VALUE(C.rb_ivar_set(C.VALUE(obj), C.ID(name), C.VALUE(val)))
26+
}

testdata/dummy/ext/dummy/dummy.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ VALUE rb_dummy_to_string(VALUE self, VALUE source);
1111
VALUE rb_dummy_max(VALUE self, VALUE a, VALUE b);
1212
1313
VALUE rb_dummy_unit_kilobyte(VALUE self);
14+
VALUE rb_dummy_unit_increment(VALUE self);
1415
*/
1516
import "C"
1617

@@ -72,6 +73,20 @@ func rb_dummy_unit_kilobyte(self C.VALUE) C.VALUE {
7273
return C.VALUE(ruby.INT2NUM(result))
7374
}
7475

76+
//export rb_dummy_unit_increment
77+
func rb_dummy_unit_increment(self C.VALUE) C.VALUE {
78+
sourceID := ruby.RbIntern("@source")
79+
sourceValue := ruby.RbIvarGet(ruby.VALUE(self), sourceID)
80+
81+
sourceInt := ruby.NUM2INT(sourceValue)
82+
sourceInt++
83+
84+
result := ruby.INT2NUM(sourceInt)
85+
ruby.RbIvarSet(ruby.VALUE(self), sourceID, result)
86+
87+
return C.VALUE(result)
88+
}
89+
7590
//export rb_dummy_max
7691
func rb_dummy_max(_ C.VALUE, a C.VALUE, b C.VALUE) C.VALUE {
7792
aLong := ruby.NUM2LONG(ruby.VALUE(a))
@@ -108,6 +123,7 @@ func Init_dummy() {
108123
// Dummy::Unit
109124
rb_cUnit := ruby.RbDefineClassUnder(rb_mDummy, "Unit", ruby.VALUE(C.rb_cObject))
110125
ruby.RbDefineMethod(rb_cUnit, "kilobyte", C.rb_dummy_unit_kilobyte, 0)
126+
ruby.RbDefineMethod(rb_cUnit, "increment", C.rb_dummy_unit_increment, 0)
111127
}
112128

113129
func main() {

testdata/dummy/lib/dummy/unit.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Dummy
44
class Unit # rubocop:disable Style/Documentation
5+
attr_accessor :source
6+
57
# @param source [Integer]
68
def initialize(source)
79
@source = source

testdata/dummy/sig/dummy.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Dummy
2626
class Unit
2727
@source: Integer
2828

29+
attr_accessor source: Integer
30+
2931
def initialize: (Integer source) -> void
3032

3133
def kilobyte: () -> Integer

testdata/dummy/spec/dummy/unit_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@
77
expect(unit.kilobyte).to eq 2048
88
end
99
end
10+
11+
describe "#increment" do
12+
it "works" do
13+
unit = Dummy::Unit.new(1)
14+
actual = unit.increment
15+
16+
expect(actual).to eq 2
17+
expect(unit.source).to eq 2
18+
end
19+
end
1020
end

0 commit comments

Comments
 (0)