Added spec for Kernel#eval with binding from method defined by #eval.
[rbx.git] / stdlib / ext / dl / mkcallback.rb
blobc9f92e4a0defa8297c927d4c01ebcadbb69d5333
1 # -*- ruby -*-
3 require 'mkmf'
4 $:.unshift File.dirname(__FILE__)
5 require 'type'
6 require 'dlconfig'
8 def mkfunc(rettype, fnum, argc)
9   args = (0..(argc-1)).collect{|i| "long arg#{i}"}.join(", ")
11   subst_code = (0..(argc-1)).collect{|i|
12     "  buff[#{i.to_s}] = arg#{i.to_s};"
13   }.join("\n")
15   ret_code =
16     if( DLTYPE[rettype][:c2rb] )
17       "  return #{DLTYPE[rettype][:rb2c]['retval']};"
18     else
19       "  /* no return value */"
20     end
22   code = [
23     "static #{DLTYPE[rettype][:ctype]}",
24     "rb_dl_callback_func_#{rettype.to_s}_#{fnum.to_s}(#{args})",
25     "{",
26     "  VALUE retval, proto, proc, obj;",
27     "  VALUE argv[#{argc.to_s}];",
28     "  int  argc;",
29     "  long buff[#{argc.to_s}];",
30     "",
31     subst_code,
32     "",
33     "  obj = rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(#{rettype.to_s}),INT2NUM(#{fnum.to_s})));",
34     "  if(NIL_P(obj))",
35     "    rb_raise(rb_eDLError, \"callback function does not exist in DL::FuncTable\");",
36     "  Check_Type(obj, T_ARRAY);",
37     "  proto = rb_ary_entry(obj, 0);",
38     "  proc  = rb_ary_entry(obj, 1);",
39     "  Check_Type(proto, T_STRING);",
40     "  if( RSTRING(proto)->len >= #{argc.to_s} )",
41     "    rb_raise(rb_eArgError, \"too many arguments\");",
42     "  rb_dl_scan_callback_args(buff, RSTRING(proto)->ptr, &argc, argv);",
43     "  retval = rb_funcall2(proc, id_call, argc, argv);",
44     "",
45     ret_code,
46     "}",
47   ].join("\n")
49   return code
50 end
52 DLTYPE.keys.sort.each{|t|
53   for n in 0..(MAX_CALLBACK - 1)
54     print(mkfunc(t, n, 15), "\n\n")
55   end