summaryrefslogtreecommitdiff
path: root/ext/dl/mkcallback.rb
blob: 32c7b451d794d27a5ce12378abe3304dbae8358e (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
# -*- ruby -*-

require 'mkmf'
$:.unshift File.dirname(__FILE__)
require 'type'
require 'dlconfig'

$int_eq_long = try_run(<<EOF)
int main() {
  return sizeof(int) == sizeof(long) ? 0 : 1;
}
EOF

def func_arg(x,i)
  ctype = DLTYPE[x][:ctype]
  "#{ctype} arg#{i}"
end

def func_args(types)
  t = []
  types[1..-1].each_with_index{|x,i| t.push(func_arg(x,i))}
  t.join(", ")
end

def funcall_args(types)
  num = types.length - 1
  if( num > 0 )
    t = []
    types[1..-1].each_with_index{|x,i| t.push(DLTYPE[x][:c2rb].call("arg#{i}"))}
    return num.to_s + ", " + t.join(", ")
  else
    return num.to_s
  end
end

def output_func(types, n = 0)
  func_name = "rb_dl_func#{types2num(types)}_#{n}"
  code =
    "#{func_name}(#{func_args(types)}) /* #{types2ctypes(types).inspect} */\n" +
    "{\n" +
    "  VALUE val, obj;\n" +
    "#ifdef DEBUG\n" +
    "  printf(\"#{func_name}()\\n\");\n" +
    "#endif\n" +
    "  obj = rb_hash_aref(DLFuncTable, INT2NUM(#{types2num(types)}));\n" +
    "  obj = rb_hash_aref(obj,INT2NUM(#{n}));\n" +
    "  val = rb_funcall(obj, id_call,\n" +
    "                   #{funcall_args(types)});\n"

  rtype = DLTYPE[types[0]][:ctype]
  rcode = DLTYPE[types[0]][:rb2c]
  if( rcode )
    code += "  return #{rcode.call('val')};\n"
  end

  code =
    rtype + "\n" +
    code +
    "}\n\n"
  if( n < MAX_CBENT - 1)
    return code + output_func(types, n+1)
  else
    return code
  end
end


def rec_output(types = [VOID])
  print output_func(types)
  if( types.length <= MAX_CBARG )
    DLTYPE.keys.sort.each{|t|
      if( t != VOID && DLTYPE[t][:cb] )
	rec_output(types + [t])
      end
    }
  end
end

DLTYPE.keys.sort.each{|t|
  if( DLTYPE[t][:cb] )
    rec_output([t])
  end
}