Skip to content

Commit 22f2228

Browse files
authored
Merge pull request #38 from sue445/feature/instance_method
Add `Dummy::Unit#kilobyte`
2 parents e3fbab5 + 562d89e commit 22f2228

File tree

10 files changed

+106
-6
lines changed

10 files changed

+106
-6
lines changed

_scripts/dump_ruby_c_functions.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ def parse_definition_args(definition)
8181
type = parts[0]
8282
name = "arg#{arg_pos}"
8383
else
84+
if parts[-1].start_with?("*")
85+
parts[-1].delete_prefix!("*")
86+
parts[-2] << "*"
87+
end
88+
8489
unless parts[-1] =~ /^[0-9a-zA-Z_]+$/
8590
# last elements isn't dummy argument
8691
parts << "arg#{arg_pos}"
8792
end
8893

8994
type = parts[0...-1].join(" ")
9095
type = type.delete_prefix("const ")
91-
9296
name = parts[-1]
93-
if name.start_with?("*")
94-
name = name.delete_prefix("*")
95-
type << "*"
96-
end
9797
end
9898

9999
{
@@ -202,7 +202,7 @@ def ruby_c_type_to_go_type(typename)
202202
return "string"
203203
when /^VALUE\s*\(\*func\)\s*\(ANYARGS\)$/
204204
return "unsafe.Pointer"
205-
when /^[A-Z]+$/
205+
when /^[A-Z]+$/, "int"
206206
# e.g. VALUE
207207
return typename
208208
end

ruby-internal-intern-variable.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ruby
2+
3+
/*
4+
#include "ruby.h"
5+
*/
6+
import "C"
7+
8+
// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/variable.h
9+
10+
// RbIvarGet calls `rb_ivar_get` in C
11+
//
12+
// Original definition is following
13+
//
14+
// VALUE rb_ivar_get(VALUE obj, ID name)
15+
func RbIvarGet(obj VALUE, name ID) VALUE {
16+
return VALUE(C.rb_ivar_get(C.VALUE(obj), C.ID(name)))
17+
}

ruby-internal-method.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ruby
2+
3+
/*
4+
#include "ruby.h"
5+
*/
6+
import "C"
7+
import "unsafe"
8+
9+
// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/method.h
10+
11+
// RbDefineMethod calls `rb_define_method` in C
12+
//
13+
// Original definition is following
14+
//
15+
// void rb_define_method(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity)
16+
func RbDefineMethod(klass VALUE, mid string, fun unsafe.Pointer, arity int) {
17+
C.rb_define_method(C.VALUE(klass), string2Char(mid), toFunctionPointer(fun), C.int(arity))
18+
}

ruby-internal-symbol.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ruby
2+
3+
/*
4+
#include "ruby.h"
5+
*/
6+
import "C"
7+
8+
// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/symbol.h
9+
10+
// RbIntern calls `rb_intern` in C
11+
//
12+
// Original definition is following
13+
//
14+
// ID rb_intern(const char *name)
15+
func RbIntern(name string) ID {
16+
return ID(C.rb_intern(string2Char(name)))
17+
}

ruby-internal-value.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ import "C"
99

1010
// VALUE is a type for passing `C.VALUE` in and out of package
1111
type VALUE C.VALUE
12+
13+
// ID is a type for passing `C.ID` in and out of package
14+
type ID C.ID

testdata/dummy/ext/dummy/dummy.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66
VALUE rb_dummy_sum(VALUE self, VALUE a, VALUE b);
77
VALUE rb_dummy_with_block(VALUE self, VALUE arg);
88
VALUE rb_dummy_hello(VALUE self, VALUE name);
9+
VALUE rb_dummy_unit_kilobyte(VALUE self);
910
*/
1011
import "C"
1112

@@ -40,6 +41,17 @@ func rb_dummy_hello(_ C.VALUE, name C.VALUE) C.VALUE {
4041
return C.VALUE(ruby.String2Value(result))
4142
}
4243

44+
//export rb_dummy_unit_kilobyte
45+
func rb_dummy_unit_kilobyte(self C.VALUE) C.VALUE {
46+
sourceID := ruby.RbIntern("@source")
47+
sourceValue := ruby.RbIvarGet(ruby.VALUE(self), sourceID)
48+
49+
sourceLong := ruby.RbNum2long(sourceValue)
50+
result := sourceLong * 1024
51+
52+
return C.VALUE(ruby.RbLong2numInline(result))
53+
}
54+
4355
var rb_mDummy ruby.VALUE
4456

4557
//export Init_dummy
@@ -57,6 +69,10 @@ func Init_dummy() {
5769

5870
// Create OuterClass class
5971
ruby.RbDefineClass("OuterClass", ruby.VALUE(C.rb_cObject))
72+
73+
// Dummy::Unit
74+
rb_cUnit := ruby.RbDefineClassUnder(rb_mDummy, "Unit", ruby.VALUE(C.rb_cObject))
75+
ruby.RbDefineMethod(rb_cUnit, "kilobyte", C.rb_dummy_unit_kilobyte, 0)
6076
}
6177

6278
func main() {

testdata/dummy/lib/dummy.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "dummy/version"
44
require_relative "dummy/dummy"
5+
require_relative "dummy/unit"
56

67
module Dummy
78
class Error < StandardError; end

testdata/dummy/lib/dummy/unit.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module Dummy
4+
class Unit # rubocop:disable Style/Documentation
5+
# @param source [Integer]
6+
def initialize(source)
7+
@source = source
8+
end
9+
end
10+
end

testdata/dummy/sig/dummy.rbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@ module Dummy
1616
module InnerModule
1717
end
1818

19+
class Unit
20+
@source: Integer
21+
22+
def initialize: (Integer source) -> void
23+
24+
def kilobyte: () -> Integer
25+
end
26+
1927
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
2028
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Dummy::Unit do
4+
describe "#kilobyte" do
5+
it "works" do
6+
unit = Dummy::Unit.new(2)
7+
expect(unit.kilobyte).to eq 2048
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)