mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add DL::Importable::Internal::callback().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
be87030ea8
commit
5c1bdda10c
1 changed files with 47 additions and 9 deletions
|
@ -37,10 +37,7 @@ module DL
|
||||||
end
|
end
|
||||||
alias dllink :dlload
|
alias dllink :dlload
|
||||||
|
|
||||||
# example:
|
def parse_cproto(proto)
|
||||||
# extern "int strlen(char*)"
|
|
||||||
#
|
|
||||||
def extern(proto)
|
|
||||||
proto = proto.gsub(/\s+/, " ").strip
|
proto = proto.gsub(/\s+/, " ").strip
|
||||||
case proto
|
case proto
|
||||||
when /^([\d\w\*_\s]+)\(([\d\w\*_\s\,\[\]]*)\)$/
|
when /^([\d\w\*_\s]+)\(([\d\w\*_\s\,\[\]]*)\)$/
|
||||||
|
@ -54,12 +51,52 @@ module DL
|
||||||
ret.push("*")
|
ret.push("*")
|
||||||
end
|
end
|
||||||
ret = ret.join(" ")
|
ret = ret.join(" ")
|
||||||
return import(func, ret, args)
|
return [func, ret, args]
|
||||||
else
|
else
|
||||||
raise(RuntimeError,"can't parse the function prototype: #{proto}")
|
raise(RuntimeError,"can't parse the function prototype: #{proto}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# example:
|
||||||
|
# extern "int strlen(char*)"
|
||||||
|
#
|
||||||
|
def extern(proto)
|
||||||
|
func,ret,args = parse_cproto(proto)
|
||||||
|
return import(func, ret, args)
|
||||||
|
end
|
||||||
|
|
||||||
|
# example:
|
||||||
|
# callback "int method_name(int, char*)"
|
||||||
|
#
|
||||||
|
def callback(proto)
|
||||||
|
func,ret,args = parse_cproto(proto)
|
||||||
|
|
||||||
|
init_types()
|
||||||
|
init_sym()
|
||||||
|
|
||||||
|
rty,_,rdec = @types.encode_type(ret)
|
||||||
|
ty,enc,dec = encode_types(args)
|
||||||
|
symty = rty + ty
|
||||||
|
|
||||||
|
module_eval("module_function :#{func}")
|
||||||
|
sym = module_eval [
|
||||||
|
"DL::callback(\"#{symty}\"){|*args|",
|
||||||
|
" sym,rdec,enc,dec = @SYM['#{func}']",
|
||||||
|
" args = enc.call(args) if enc",
|
||||||
|
" r,rs = #{func}(*args)",
|
||||||
|
" r = rdec.call(r) if rdec",
|
||||||
|
" rs = dec.call(rs) if dec",
|
||||||
|
" @retval = r",
|
||||||
|
" @args = rs",
|
||||||
|
" @retval",
|
||||||
|
"}",
|
||||||
|
].join("\n")
|
||||||
|
|
||||||
|
@SYM[func] = [sym,rdec,enc,dec]
|
||||||
|
|
||||||
|
return sym
|
||||||
|
end
|
||||||
|
|
||||||
# example:
|
# example:
|
||||||
# typealias("uint", "unsigned int")
|
# typealias("uint", "unsigned int")
|
||||||
#
|
#
|
||||||
|
@ -74,11 +111,12 @@ module DL
|
||||||
def import(name, rettype, argtypes = nil)
|
def import(name, rettype, argtypes = nil)
|
||||||
init_types()
|
init_types()
|
||||||
init_sym()
|
init_sym()
|
||||||
@LIBS.each{|lib|
|
|
||||||
rty,_,rdec = @types.encode_type(rettype)
|
|
||||||
ty,enc,dec = encode_types(argtypes)
|
|
||||||
symty = rty + ty
|
|
||||||
|
|
||||||
|
rty,_,rdec = @types.encode_type(rettype)
|
||||||
|
ty,enc,dec = encode_types(argtypes)
|
||||||
|
symty = rty + ty
|
||||||
|
|
||||||
|
@LIBS.each{|lib|
|
||||||
begin
|
begin
|
||||||
sym = lib[name, symty]
|
sym = lib[name, symty]
|
||||||
rescue
|
rescue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue