def generate_ext
ext = []
if @include_ruby_first
@inc.unshift "#include \"ruby.h\""
else
@inc.push "#include \"ruby.h\""
end
ext << @inc
ext << nil
ext << @src.join("\n\n")
ext << nil
ext << nil
ext << "#ifdef __cplusplus"
ext << "extern \"C\" {"
ext << "#endif"
ext << " __declspec(dllexport)" if WINDOZE
ext << " void Init_#{module_name}() {"
ext << " VALUE c = rb_cObject;"
ext << @mod.name.split("::").map { |n|
" c = rb_const_get(c, rb_intern(\"#{n}\"));"
}.join("\n")
ext << nil
@sig.keys.sort.each do |name|
method = ''
arity, singleton, method_name = @sig[name]
if singleton then
if method_name == 'allocate' then
raise "#{@mod}::allocate must have an arity of zero" if arity > 0
ext << " rb_define_alloc_func(c, (VALUE(*)(VALUE))#{name});"
next
end
method << " rb_define_singleton_method(c, \"#{method_name}\", "
else
method << " rb_define_method(c, \"#{method_name}\", "
end
method << "(VALUE(*)(ANYARGS))#{name}, #{arity});"
ext << method
end
ext << @init_extra.join("\n") unless @init_extra.empty?
ext << nil
ext << " }"
ext << "#ifdef __cplusplus"
ext << "}"
ext << "#endif"
ext << nil
ext.join "\n"
end