1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Importable::symbol.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ttate 2002-06-27 15:09:47 +00:00
parent ad8c8144d4
commit 98007267bc

View file

@ -105,6 +105,29 @@ module DL
@types.typealias(*args) @types.typealias(*args)
end end
# example:
# symbol "foo_value"
# symbol "foo_func", "IIP"
#
def symbol(name, ty = nil)
sym = nil
@LIBS.each{|lib|
begin
if( ty )
sym = lib[name, ty]
else
sym = lib[name]
end
rescue
next
end
}
if( !sym )
raise(RuntimeError, "can't find the symbol `#{name}'")
end
return sym
end
# example: # example:
# import("get_length", "int", ["void*", "int"]) # import("get_length", "int", ["void*", "int"])
# #
@ -116,46 +139,39 @@ module DL
ty,enc,dec = encode_types(argtypes) ty,enc,dec = encode_types(argtypes)
symty = rty + ty symty = rty + ty
@LIBS.each{|lib| sym = symbol(name, symty)
begin
sym = lib[name, symty]
rescue
next
end
mname = name.dup mname = name.dup
if( ?A <= mname[0] && mname[0] <= ?Z ) if( ?A <= mname[0] && mname[0] <= ?Z )
mname[0,1] = mname[0,1].downcase mname[0,1] = mname[0,1].downcase
end end
@SYM[mname] = [sym,rdec,enc,dec] @SYM[mname] = [sym,rdec,enc,dec]
module_eval [ module_eval [
"def #{mname}(*args)", "def #{mname}(*args)",
" sym,rdec,enc,dec = @SYM['#{mname}']", " sym,rdec,enc,dec = @SYM['#{mname}']",
" args = enc.call(args) if enc", " args = enc.call(args) if enc",
if( $DEBUG ) if( $DEBUG )
" p \"[DL] call #{mname} with \#{args.inspect}\"" " p \"[DL] call #{mname} with \#{args.inspect}\""
else else
"" ""
end, end,
" r,rs = sym.call(*args)", " r,rs = sym.call(*args)",
if( $DEBUG ) if( $DEBUG )
" p \"[DL] retval=\#{r.inspect} args=\#{rs.inspect}\"" " p \"[DL] retval=\#{r.inspect} args=\#{rs.inspect}\""
else else
"" ""
end, end,
" r = rdec.call(r) if rdec", " r = rdec.call(r) if rdec",
" rs = dec.call(rs) if dec", " rs = dec.call(rs) if dec",
" @retval = r", " @retval = r",
" @args = rs", " @args = rs",
" return @retval", " return @retval",
"end", "end",
"module_function :#{mname}", "module_function :#{mname}",
].join("\n") ].join("\n")
return sym return sym
}
raise(RuntimeError, "can't find #{name}.")
end end
def _args_ def _args_