Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions _scripts/dump_ruby_c_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ def parse_definition_args(definition)
type = parts[0]
name = "arg#{arg_pos}"
else
if parts[-1].start_with?("*")
parts[-1].delete_prefix!("*")
parts[-2] << "*"
end

unless parts[-1] =~ /^[0-9a-zA-Z_]+$/
# last elements isn't dummy argument
parts << "arg#{arg_pos}"
end

type = parts[0...-1].join(" ")
type = type.delete_prefix("const ")

name = parts[-1]
if name.start_with?("*")
name = name.delete_prefix("*")
type << "*"
end
end

{
Expand Down Expand Up @@ -202,7 +202,7 @@ def ruby_c_type_to_go_type(typename)
return "string"
when /^VALUE\s*\(\*func\)\s*\(ANYARGS\)$/
return "unsafe.Pointer"
when /^[A-Z]+$/
when /^[A-Z]+$/, "int"
# e.g. VALUE
return typename
end
Expand Down
17 changes: 17 additions & 0 deletions ruby-internal-intern-variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ruby

/*
#include "ruby.h"
*/
import "C"

// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/variable.h

// RbIvarGet calls `rb_ivar_get` in C
//
// Original definition is following
//
// VALUE rb_ivar_get(VALUE obj, ID name)
func RbIvarGet(obj VALUE, name ID) VALUE {
return VALUE(C.rb_ivar_get(C.VALUE(obj), C.ID(name)))
}
18 changes: 18 additions & 0 deletions ruby-internal-method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ruby

/*
#include "ruby.h"
*/
import "C"
import "unsafe"

// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/method.h

// RbDefineMethod calls `rb_define_method` in C
//
// Original definition is following
//
// void rb_define_method(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity)
func RbDefineMethod(klass VALUE, mid string, fun unsafe.Pointer, arity int) {
C.rb_define_method(C.VALUE(klass), string2Char(mid), toFunctionPointer(fun), C.int(arity))
}
17 changes: 17 additions & 0 deletions ruby-internal-symbol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ruby

/*
#include "ruby.h"
*/
import "C"

// c.f. https://github.com/ruby/ruby/blob/master/include/ruby/internal/symbol.h

// RbIntern calls `rb_intern` in C
//
// Original definition is following
//
// ID rb_intern(const char *name)
func RbIntern(name string) ID {
return ID(C.rb_intern(string2Char(name)))
}
3 changes: 3 additions & 0 deletions ruby-internal-value.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ import "C"

// VALUE is a type for passing `C.VALUE` in and out of package
type VALUE C.VALUE

// ID is a type for passing `C.ID` in and out of package
type ID C.ID
16 changes: 16 additions & 0 deletions testdata/dummy/ext/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
VALUE rb_dummy_sum(VALUE self, VALUE a, VALUE b);
VALUE rb_dummy_with_block(VALUE self, VALUE arg);
VALUE rb_dummy_hello(VALUE self, VALUE name);
VALUE rb_dummy_unit_kilobyte(VALUE self);
*/
import "C"

Expand Down Expand Up @@ -40,6 +41,17 @@ func rb_dummy_hello(_ C.VALUE, name C.VALUE) C.VALUE {
return C.VALUE(ruby.String2Value(result))
}

//export rb_dummy_unit_kilobyte
func rb_dummy_unit_kilobyte(self C.VALUE) C.VALUE {
sourceID := ruby.RbIntern("@source")
sourceValue := ruby.RbIvarGet(ruby.VALUE(self), sourceID)

sourceLong := ruby.RbNum2long(sourceValue)
result := sourceLong * 1024

return C.VALUE(ruby.RbLong2numInline(result))
}

var rb_mDummy ruby.VALUE

//export Init_dummy
Expand All @@ -57,6 +69,10 @@ func Init_dummy() {

// Create OuterClass class
ruby.RbDefineClass("OuterClass", ruby.VALUE(C.rb_cObject))

// Dummy::Unit
rb_cUnit := ruby.RbDefineClassUnder(rb_mDummy, "Unit", ruby.VALUE(C.rb_cObject))
ruby.RbDefineMethod(rb_cUnit, "kilobyte", C.rb_dummy_unit_kilobyte, 0)
}

func main() {
Expand Down
1 change: 1 addition & 0 deletions testdata/dummy/lib/dummy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "dummy/version"
require_relative "dummy/dummy"
require_relative "dummy/unit"

module Dummy
class Error < StandardError; end
Expand Down
10 changes: 10 additions & 0 deletions testdata/dummy/lib/dummy/unit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Dummy
class Unit # rubocop:disable Style/Documentation
# @param source [Integer]
def initialize(source)
@source = source
end
end
end
8 changes: 8 additions & 0 deletions testdata/dummy/sig/dummy.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ module Dummy
module InnerModule
end

class Unit
@source: Integer

def initialize: (Integer source) -> void

def kilobyte: () -> Integer
end

# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
10 changes: 10 additions & 0 deletions testdata/dummy/spec/dummy/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

RSpec.describe Dummy::Unit do
describe "#kilobyte" do
it "works" do
unit = Dummy::Unit.new(2)
expect(unit.kilobyte).to eq 2048
end
end
end