@@ -29,12 +29,74 @@ func goobj_free(obj unsafe.Pointer) {
29
29
30
30
// NewGoStruct create Ruby object from Go object
31
31
//
32
- // See also [RbDefineAllocFunc]
32
+ // Example
33
+ //
34
+ // /*
35
+ // package main
36
+ //
37
+ // #include "example.h"
38
+ //
39
+ // VALUE go_struct_alloc(VALUE klass);
40
+ // void rb_example_go_struct_set(VALUE self, VALUE x, VALUE y);
41
+ // VALUE rb_example_go_struct_get(VALUE self);
42
+ // */
43
+ // import "C"
44
+ //
45
+ // import (
46
+ // "github.com/sue445/go-gem-wrapper/ruby"
47
+ // "unsafe"
48
+ // )
49
+ //
50
+ // // GoStruct is internal reality of Ruby `Example::GoStruct`
51
+ // type GoStruct struct {
52
+ // x int
53
+ // y int
54
+ // }
55
+ //
56
+ // //export go_struct_alloc
57
+ // func go_struct_alloc(klass C.VALUE) C.VALUE {
58
+ // data := GoStruct{}
59
+ // return C.VALUE(ruby.NewGoStruct(ruby.VALUE(klass), unsafe.Pointer(&data)))
60
+ // }
61
+ //
62
+ // //export rb_example_go_struct_set
63
+ // func rb_example_go_struct_set(self C.VALUE, x C.VALUE, y C.VALUE) {
64
+ // data := (*GoStruct)(ruby.GetGoStruct(ruby.VALUE(self)))
65
+ // data.x = ruby.NUM2INT(ruby.VALUE(x))
66
+ // data.y = ruby.NUM2INT(ruby.VALUE(y))
67
+ // }
68
+ //
69
+ // //export rb_example_go_struct_get
70
+ // func rb_example_go_struct_get(self C.VALUE) C.VALUE {
71
+ // data := (*GoStruct)(ruby.GetGoStruct(ruby.VALUE(self)))
72
+ //
73
+ // ret := []ruby.VALUE{
74
+ // ruby.INT2NUM(data.x),
75
+ // ruby.INT2NUM(data.y),
76
+ // }
77
+ //
78
+ // return C.VALUE(ruby.Slice2rbAry(ret))
79
+ // }
80
+ //
81
+ // //export Init_example
82
+ // func Init_example() {
83
+ // rb_mExample := ruby.RbDefineModule("Example")
84
+ //
85
+ // // Create Example::GoStruct class
86
+ // rb_cGoStruct := ruby.RbDefineClassUnder(rb_mExample, "GoStruct", ruby.VALUE(C.rb_cObject))
87
+ //
88
+ // ruby.RbDefineAllocFunc(rb_cGoStruct, C.go_struct_alloc)
89
+ //
90
+ // ruby.RbDefineMethod(rb_cGoStruct, "set", C.rb_example_go_struct_set, 2)
91
+ // ruby.RbDefineMethod(rb_cGoStruct, "get", C.rb_example_go_struct_get, 0)
92
+ // }
33
93
func NewGoStruct (klass VALUE , goobj unsafe.Pointer ) VALUE {
34
94
return VALUE (C .NewGoStruct (C .VALUE (klass ), goobj ))
35
95
}
36
96
37
97
// GetGoStruct returns Go object from Ruby object
98
+ //
99
+ // See also [NewGoStruct]
38
100
func GetGoStruct (obj VALUE ) unsafe.Pointer {
39
101
return C .GetGoStruct (C .VALUE (obj ))
40
102
}
0 commit comments