summaryrefslogtreecommitdiff
path: root/ext/dl/lib
diff options
context:
space:
mode:
authorttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-06-27 15:09:47 +0000
committerttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-06-27 15:09:47 +0000
commit98007267bce98147a1259ded452e0c5109580877 (patch)
tree857f8c1e86e21cfbd1dd81be831238fa85dd4c93 /ext/dl/lib
parentad8c8144d4abcfa3c263add80b1b33013a1cb8ba (diff)
Add Importable::symbol.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dl/lib')
-rw-r--r--ext/dl/lib/dl/import.rb94
1 files changed, 55 insertions, 39 deletions
diff --git a/ext/dl/lib/dl/import.rb b/ext/dl/lib/dl/import.rb
index 5d221f28160..16ecf9c904f 100644
--- a/ext/dl/lib/dl/import.rb
+++ b/ext/dl/lib/dl/import.rb
@@ -106,6 +106,29 @@ module DL
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"])
#
def import(name, rettype, argtypes = nil)
@@ -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")
-
- return sym
- }
- raise(RuntimeError, "can't find #{name}.")
+ 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
end
def _args_