Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • dl/callback/mkcallback.rb

Object

Public Instance Methods

foreach_proc_entry() click to toggle source
 
               # File dl/callback/mkcallback.rb, line 109
def foreach_proc_entry
  for calltype in CALLTYPES
    case calltype
    when CDECL
      proc_entry = "rb_DLCdeclCallbackProcs"
    when STDCALL
      proc_entry = "rb_DLStdcallCallbackProcs"
    else
      raise "unknown calltype: #{calltype}"
    end
    yield calltype, proc_entry
  end
end
            
func_name(ty, argc, n, calltype) click to toggle source
 
               # File dl/callback/mkcallback.rb, line 91
def func_name(ty, argc, n, calltype)
  "rb_dl_callback_#{DLTYPE[ty][:name]}_#{argc}_#{n}_#{calltype}"
end
            
gen_callback_file(ty) click to toggle source
 
               # File dl/callback/mkcallback.rb, line 169
def gen_callback_file(ty)
  filename = "#{$output}-#{ty}.c"
  initname = "rb_dl_init_callbacks_#{ty}"
  body = <<-EOS
#include "dl.h"

extern VALUE rb_DLCdeclCallbackAddrs, rb_DLCdeclCallbackProcs;
#ifdef FUNC_STDCALL
extern VALUE rb_DLStdcallCallbackAddrs, rb_DLStdcallCallbackProcs;
#endif
extern ID   rb_dl_cb_call;
    EOS
  yield body
  body << <<-EOS
void
#{initname}()
{
#{gen_push_proc_ary(ty, "rb_DLCdeclCallbackProcs")}
#{gen_push_addr_ary(ty, "rb_DLCdeclCallbackAddrs", CDECL)}
#ifdef FUNC_STDCALL
#{gen_push_proc_ary(ty, "rb_DLStdcallCallbackProcs")}
#{gen_push_addr_ary(ty, "rb_DLStdcallCallbackAddrs", STDCALL)}
#endif
}
    EOS
  [filename, initname, body]
end
            
gen_push_addr_ary(ty, aryname, calltype) click to toggle source
 
               # File dl/callback/mkcallback.rb, line 159
def gen_push_addr_ary(ty, aryname, calltype)
  sprintf("    rb_ary_push(#{aryname}, rb_ary_new3(%d,%s));",
          MAX_CALLBACK * DLSTACK_SIZE,
          (0...MAX_CALLBACK).collect{|i|
            (0...DLSTACK_SIZE).collect{|argc|
              "PTR2NUM(%s)" % func_name(ty,argc,i,calltype)
            }.join(",")
          }.join(","))
end
            
gen_push_proc_ary(ty, aryname) click to toggle source
 
               # File dl/callback/mkcallback.rb, line 151
def gen_push_proc_ary(ty, aryname)
  sprintf("    rb_ary_push(#{aryname}, rb_ary_new3(%d,%s));",
          MAX_CALLBACK * DLSTACK_SIZE,
          (0...MAX_CALLBACK).collect{
            (0...DLSTACK_SIZE).collect{ "Qnil" }.join(",")
          }.join(","))
end
            
gencallback(ty, calltype, proc_entry, argc, n) click to toggle source
 
               # File dl/callback/mkcallback.rb, line 123
def gencallback(ty, calltype, proc_entry, argc, n)
  <<-EOS
#{calltype == STDCALL ? "\n#ifdef FUNC_STDCALL" : ""}
static #{DLTYPE[ty][:type]}
FUNC_#{calltype.upcase}(#{func_name(ty,argc,n,calltype)})(#{(0...argc).collect{|i| "DLSTACK_TYPE stack" + i.to_s}.join(", ")})
{
    VALUE ret, cb#{argc > 0 ? ", args[#{argc}]" : ""};
#{
      sizeof_voidp = [""].pack('p').size
      sizeof_long = [0].pack('l!').size
      (0...argc).collect{|i|
        if sizeof_voidp == sizeof_long
          "    args[%d] = LONG2NUM(stack%d);" % [i,i]
        elsif sizeof_voidp == 8 # should get sizeof_long_long...
          "    args[%d] = LL2NUM(stack%d);" % [i,i]
        else
          raise "unknown size of void*"
        end
      }.join("\n")
}
    cb = rb_ary_entry(rb_ary_entry(#{proc_entry}, #{ty}), #{(n * DLSTACK_SIZE) + argc});
    ret = rb_funcall2(cb, rb_dl_cb_call, #{argc}, #{argc > 0 ? 'args' : 'NULL'});
    return #{DLTYPE[ty][:conv] ? DLTYPE[ty][:conv] % "ret" : ""};
}
#{calltype == STDCALL ? "#endif\n" : ""}
  EOS
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.