summaryrefslogtreecommitdiffhomepage
path: root/lib/FelBind.rb
blob: 7e404110edad7194b4f0dbe2f0d0b99e4ae22983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# frozen_string_literal: true

=begin
require_relative "FelBind/version"
require_relative "FelBind/backend/uctags.rb"
require_relative "FelBind/frontend/mruby.rb"
=end

module FelBind
  class Error < StandardError; end
  # Your code goes here...

  # Binding C to mruby
  class BindGem
    attr_accessor :gem_name

    def initialize(gem_name:)
      self.gem_name = gem_name
    end

    def add_class(name)
      class_names.push name
    end

    def add_function(class_name:, function_name:, &block)
      functions.push Function.new(class_name: class_name, function_name: function_name)
      block.call(functions.last)
    end

    def add_struct(class_name:, cstruct_name:, &block)
      structs.push StructObj.new(class_name, cstruct_name)
      block.call(structs.last)
    end

    # structs
    class StructObj
      attr_accessor :class_name, :cstruct_name, :initializer

      # declaring the C struct
      def build_struct_init
        "static const struct mrb_data_type felbind_struct_#{class_name} = { \"#{class_name}\", mrb_free };\n"
      end

      # building the C functions
      def build_funcs
        init_result = ""
        init_vars = ""
        init_get_args_types = ""
        init_get_args_addresses = ""
        init_set_vars = ""
        accessor_result = ""
        if initializer
          init_result += "static mrb_value felbind_struct_init_#{class_name}(mrb_state* mrb, mrb_value self) {\n"
          init_result += "#{cstruct_name} *felbind_struct_wrapped_#{class_name} = (#{cstruct_name} *)DATA_PTR(self);\n"
          init_result += "if(felbind_struct_wrapped_#{class_name}) { mrb_free(mrb, felbind_struct_wrapped_#{class_name}); }\n"
          init_result += "mrb_data_init(self, NULL, &felbind_struct_#{class_name});\n"
          init_result += "felbind_struct_wrapped_#{class_name} = (#{cstruct_name} *)mrb_malloc(mrb, sizeof(#{cstruct_name}));\n"
        end
        members.each do |mem|
          next if !mem.accessor
          next if !initializer

          if mem.rtype == "int"
            init_vars += "mrb_int felbind_param_#{mem.name};\n"
            init_get_args_types += "i"
            init_get_args_addresses += ", &felbind_param_#{mem.name}"
            init_set_vars += "felbind_struct_wrapped_#{class_name}->#{mem.name} = (#{mem.ctype})felbind_param_#{mem.name};\n"

            accessor_result += "static mrb_value felbind_getter_#{class_name}_#{mem.name}(mrb_state *mrb, mrb_value self) {\n"
            accessor_result += "struct #{cstruct_name} *felbind_struct_get = DATA_GET_PTR(mrb, self, &felbind_struct_#{class_name}, #{cstruct_name});\n"
            #accessor_result += "return mrb_fixnum_value(felbind_getter_#{class_name}_#{mem.name});\n"
            accessor_result += "return mrb_fixnum_value(felbind_struct_get->#{mem.name});\n"
            accessor_result += "}\n"

            accessor_result += "static mrb_value felbind_setter_#{class_name}_#{mem.name}(mrb_state *mrb, mrb_value self) {\n"
            accessor_result += "mrb_int felbind_param_#{mem.name};\n"
            accessor_result += "mrb_get_args(mrb, \"i\", &felbind_param_#{mem.name});\n"
            accessor_result += "struct #{cstruct_name} *felbind_struct_set = DATA_GET_PTR(mrb, self, &felbind_struct_#{class_name}, #{cstruct_name});\n"
            accessor_result += "felbind_struct_set->#{mem.name} = (#{mem.ctype})felbind_param_#{mem.name};\n"
            accessor_result += "return mrb_fixnum_value(felbind_struct_set->#{mem.name});\n"
            accessor_result += "}\n"
          end
        end
        result = init_result
        result += init_vars
        result += "mrb_get_args(mrb, \"#{init_get_args_types}\"#{init_get_args_addresses});\n"
        result += init_set_vars
        result += "mrb_data_init(self, felbind_struct_wrapped_#{class_name}, &felbind_struct_#{class_name});\n"
        result += "return self;\n"
        result += "}\n"
        result + accessor_result
      end

      # binding instance after class is defined
      def build_set_instance(def_class_name)
        "MRB_SET_INSTANCE_TT(#{def_class_name}, MRB_TT_DATA);\n"
      end

      # binding the C funcs to Ruby
      def build_def_funcs(def_class_name)
        result = ""
        if initializer
          result += "mrb_define_method(mrb, #{def_class_name}, \"initialize\", felbind_struct_init_#{class_name}, MRB_ARGS_ANY());\n"
        end
        members.each do |mem|
          next if !initializer
          next if !mem.accessor

          result += "mrb_define_method(mrb, #{def_class_name}, \"#{mem.name}\", felbind_getter_#{class_name}_#{mem.name}, MRB_ARGS_NONE());\n"
          result += "mrb_define_method(mrb, #{def_class_name}, \"#{mem.name}=\", felbind_setter_#{class_name}_#{mem.name}, MRB_ARGS_ANY());\n"
        end
        result
      end

      def initialize(class_name, cstruct_name)
        self.class_name = class_name
        self.cstruct_name = cstruct_name
      end

      def members
        @members ||= []
      end

      def member(name:, ctype:, rtype:, accessor:)
        members.push MemberObj.new(name, ctype, rtype, accessor)
      end

      # members
      class MemberObj
        attr_accessor :name, :ctype, :rtype, :accessor

        def initialize(name, ctype, rtype, accessor)
          self.name = name
          self.ctype = ctype
          self.rtype = rtype
          self.accessor = accessor
        end
      end
    end

    private

    def functions
      @functions ||= []
    end

    def class_names
      @class_names ||= []
    end

    def structs
      @structs ||= []
    end

    # function
    class Function
      attr_accessor :content, :name, :class_name, :return_call_val, :args

      def initialize(class_name:, function_name:)
        self.class_name = class_name
        self.name = function_name
      end

      def return_call(&block)
        self.return_call_val = ReturnCall.new
        block.call(return_call_val)
      end

      def build_get_vars
        result = ""
        expect = ""
        addresses = ""
        args.arguments.each do |param|
          if param.first == :int
            result += "mrb_int #{arg(param.last)};\n"
            expect += "i"
            addresses += ", &#{arg(param.last)}"
          end
        end
        addresses.delete_prefix! ", "
        result += "mrb_get_args(mrb, \"#{expect}\", #{addresses});\n"
      end

      def build
        function = "static mrb_value\n"
        function += "felbind_#{name}(mrb_state *mrb, mrb_value self){\n"
        function += build_get_vars
        function += "#{content}\n"
        function += "#{return_call_val.build}"
        function += "}\n"
        function
      end

      def arg(name)
        "felbind_var_#{name}"
      end

      def get_args(&block)
        self.args = Args.new
        block.call(args)
      end

      # args
      class Args
        def arguments
          @arguments ||= []
        end

        def int(name)
          arguments.push [:int, name]
        end
      end

      # return call
      class ReturnCall
        attr_accessor :type, :val

        def build
          if type == "nil"
            "return mrb_nil_value();\n"
          elsif type == "int"
            "return mrb_fixnum_value(#{val});\n"
          end
        end
      end
    end
  end

  # bind gem
  class BindGem
    def build
      result = ""
      result += insert_includes

      structs.each do |strct|
        result += strct.build_struct_init
        result += strct.build_funcs
      end

      functions.each do |func_obj|
        result += func_obj.build
      end

      result += build_init
      result += build_final
      result
    end

    private

    def insert_includes
      "#include <mruby.h>\n" +
        "#include <mruby/data.h>\n" +
        "#include <mruby/class.h>\n" +
        "#include <mruby/compile.h>\n" +
        "#include <stdio.h>\n"
    end

    def build_init
      result = ""
      result += "void mrb_#{gem_name}_gem_init(mrb_state* mrb) {\n"
      class_names.each do |class_name|
        result += "struct RClass *#{class_name}_class = mrb_define_class(mrb, \"#{class_name}\", mrb->object_class);\n"
      end
      structs.each do |strct|
        result += strct.build_set_instance("#{strct.class_name}_class")
        result += strct.build_def_funcs("#{strct.class_name}_class")
      end

      functions.each do |func|
        result += "mrb_define_class_method(mrb, #{func.class_name}_class, \"#{func.name}\", felbind_#{func.name},"
        if(func.args.arguments.size.zero?)
          result += " MRB_ARGS_NONE()"
        else
          result += " MRB_ARGS_REQ(#{func.args.arguments.size})"
        end
        result += ");\n"
      end
      result += "}\n"
      result
    end

    def build_final
      "void mrb_#{gem_name}_gem_final(mrb_state* mrb) {}\n"
    end
  end
end