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