summaryrefslogtreecommitdiff
path: root/ext/dl/lib
diff options
context:
space:
mode:
authorttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-24 16:38:38 +0000
committerttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-24 16:38:38 +0000
commit733a3adc796907415f642a92510d6ccd050dc4a9 (patch)
tree32ad1b083ac2017eed20413ddf022983a6be375e /ext/dl/lib
parent0f2fdc6978fb05e0149473a79d6083a1dd63e743 (diff)
improved encode_type() in types.rb for [ruby-talk:121175].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7363 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dl/lib')
-rw-r--r--ext/dl/lib/dl/import.rb6
-rw-r--r--ext/dl/lib/dl/struct.rb2
-rw-r--r--ext/dl/lib/dl/types.rb187
3 files changed, 128 insertions, 67 deletions
diff --git a/ext/dl/lib/dl/import.rb b/ext/dl/lib/dl/import.rb
index 1ab5145deff..f145c0fedf2 100644
--- a/ext/dl/lib/dl/import.rb
+++ b/ext/dl/lib/dl/import.rb
@@ -70,7 +70,7 @@ module DL
init_types()
init_sym()
- rty,renc,rdec = @types.encode_type(ret)
+ rty,renc,rdec,_,_,_ = @types.encode_type(ret)
ty,enc,dec = encode_types(args)
symty = rty + ty
@@ -131,7 +131,7 @@ module DL
init_types()
init_sym()
- rty,_,rdec = @types.encode_type(rettype)
+ rty,_,rdec,_,_,_ = @types.encode_type(rettype)
ty,enc,dec = encode_types(argtypes)
symty = rty + ty
@@ -184,7 +184,7 @@ module DL
enc = nil
dec = nil
tys.each_with_index{|ty,idx|
- ty,c1,c2,_,_ = @types.encode_type(ty)
+ ty,c1,c2,_,_,_ = @types.encode_type(ty)
encty.push(ty)
if( enc )
if( c1 )
diff --git a/ext/dl/lib/dl/struct.rb b/ext/dl/lib/dl/struct.rb
index 2c52d5040d6..638b0953125 100644
--- a/ext/dl/lib/dl/struct.rb
+++ b/ext/dl/lib/dl/struct.rb
@@ -128,7 +128,7 @@ module DL
else
raise(RuntimeError, "invalid element: #{elem}")
end
- ty,_,_,enc,dec = @types.encode_type(ty)
+ _,_,_,ty,enc,dec = @types.encode_type(ty)
return [name,ty,num,enc,dec]
end
end # class Struct
diff --git a/ext/dl/lib/dl/types.rb b/ext/dl/lib/dl/types.rb
index 139426473a7..24595b748a3 100644
--- a/ext/dl/lib/dl/types.rb
+++ b/ext/dl/lib/dl/types.rb
@@ -6,110 +6,167 @@ module DL
class Types
TYPES = [
# FORMAT:
- # ["alias name", "type name",
- # encoding_method, decoding_method, for function prototypes
- # encoding_method, decoding_method] for structures (not implemented)
+ # ["alias name",
+ # "type name", encoding_method, decoding_method, for function prototypes
+ # "type name", encoding_method, decoding_method] for structures (not implemented)
# for Windows
- ["DWORD", "unsigned long", nil, nil, nil, nil],
- ["PDWORD", "unsigned long *", nil, nil, nil, nil],
- ["WORD", "unsigned short", nil, nil, nil, nil],
- ["PWORD", "unsigned int *", nil, nil, nil, nil],
- ["BYTE", "unsigned char", nil, nil, nil, nil],
- ["PBYTE", "unsigned char *", nil, nil, nil, nil],
- ["BOOL", "ibool", nil, nil, nil, nil],
- ["ATOM", "int", nil, nil, nil, nil],
- ["BYTE", "unsigned char", nil, nil, nil, nil],
- ["PBYTE", "unsigned char *", nil, nil, nil, nil],
- ["UINT", "unsigned int", nil, nil, nil, nil],
- ["ULONG", "unsigned long", nil, nil, nil, nil],
- ["UCHAR", "unsigned char", nil, nil, nil, nil],
- ["HANDLE", "unsigned long", nil, nil, nil, nil],
- ["PHANDLE","void*", nil, nil, nil, nil],
- ["PVOID", "void*", nil, nil, nil, nil],
- ["LPCSTR", "char*", nil, nil, nil, nil],
- ["HDC", "unsigned int", nil, nil, nil, nil],
- ["HWND", "unsigned int", nil, nil, nil, nil],
+ ["DWORD", "unsigned long", nil, nil,
+ "unsigned long", nil, nil],
+ ["PDWORD", "unsigned long *", nil, nil,
+ "unsigned long *", nil, nil],
+ ["WORD", "unsigned short", nil, nil,
+ "unsigned short", nil, nil],
+ ["PWORD", "unsigned int *", nil, nil,
+ "unsigned int *", nil, nil],
+ ["BYTE", "unsigned char", nil, nil,
+ "unsigned char", nil, nil],
+ ["PBYTE", "unsigned char *", nil, nil,
+ "unsigned char *", nil, nil],
+ ["BOOL", "ibool", nil, nil,
+ "ibool", nil, nil],
+ ["ATOM", "int", nil, nil,
+ "int", nil, nil],
+ ["BYTE", "unsigned char", nil, nil,
+ "unsigned char", nil, nil],
+ ["PBYTE", "unsigned char *", nil, nil,
+ "unsigned char *", nil, nil],
+ ["UINT", "unsigned int", nil, nil,
+ "unsigned int", nil, nil],
+ ["ULONG", "unsigned long", nil, nil,
+ "unsigned long", nil, nil],
+ ["UCHAR", "unsigned char", nil, nil,
+ "unsigned char", nil, nil],
+ ["HANDLE", "unsigned long", nil, nil,
+ "unsigned long", nil, nil],
+ ["PHANDLE","void*", nil, nil,
+ "void*", nil, nil],
+ ["PVOID", "void*", nil, nil,
+ "void*", nil, nil],
+ ["LPCSTR", "char*", nil, nil,
+ "char*", nil, nil],
+ ["HDC", "unsigned int", nil, nil,
+ "unsigned int", nil, nil],
+ ["HWND", "unsigned int", nil, nil,
+ "unsigned int", nil, nil],
# Others
- ["uint", "unsigned int", nil, nil, nil, nil],
- ["u_int", "unsigned int", nil, nil, nil, nil],
- ["ulong", "unsigned long", nil, nil, nil, nil],
- ["u_long", "unsigned long", nil, nil, nil, nil],
+ ["uint", "unsigned int", nil, nil,
+ "unsigned int", nil, nil],
+ ["u_int", "unsigned int", nil, nil,
+ "unsigned int", nil, nil],
+ ["ulong", "unsigned long", nil, nil,
+ "unsigned long", nil, nil],
+ ["u_long", "unsigned long", nil, nil,
+ "unsigned long", nil, nil],
# DL::Importable primitive types
- ["ibool", "I",
+ ["ibool",
+ "I",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
+ "I",
proc{|v| v ? 1 : 0 },
proc{|v| (v != 0) ? true : false} ],
- ["cbool", "C",
+ ["cbool",
+ "C",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
+ "C",
proc{|v,len| v ? 1 : 0},
proc{|v,len| (v != 0) ? true : false}],
- ["lbool", "L",
+ ["lbool",
+ "L",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
+ "L",
proc{|v,len| v ? 1 : 0},
proc{|v,len| (v != 0) ? true : false}],
- ["unsigned char", "C",
+ ["unsigned char",
+ "C",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]},
+ "C",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]}],
- ["unsigned short", "H",
+ ["unsigned short",
+ "H",
proc{|v| [v].pack("S").unpack("s")[0]},
proc{|v| [v].pack("s").unpack("S")[0]},
+ "H",
proc{|v| [v].pack("S").unpack("s")[0]},
proc{|v| [v].pack("s").unpack("S")[0]}],
- ["unsigned int", "I",
+ ["unsigned int",
+ "I",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]},
+ "I",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]}],
- ["unsigned long", "L",
+ ["unsigned long",
+ "L",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]},
+ "L",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]}],
- ["unsigned char ref", "c",
+ ["unsigned char ref",
+ "c",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]},
- nil, nil],
- ["unsigned int ref", "i",
+ nil, nil, nil],
+ ["unsigned int ref",
+ "i",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]},
- nil, nil],
- ["unsigned long ref", "l",
+ nil, nil, nil],
+ ["unsigned long ref",
+ "l",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]},
- nil, nil],
- ["char ref", "c", nil, nil, nil, nil],
- ["short ref", "h", nil, nil, nil, nil],
- ["int ref", "i", nil, nil, nil, nil],
- ["long ref", "l", nil, nil, nil, nil],
- ["float ref", "f", nil, nil, nil, nil],
- ["double ref","d", nil, nil, nil, nil],
- ["char", "C", nil, nil, nil, nil],
- ["short", "H", nil, nil, nil, nil],
- ["int", "I", nil, nil, nil, nil],
- ["long", "L", nil, nil, nil, nil],
- ["float", "F", nil, nil, nil, nil],
- ["double", "D", nil, nil, nil, nil],
- [/^char\s*\*$/,"s",nil, nil, nil, nil],
- [/^const char\s*\*$/,"S",nil, nil, nil, nil],
- [/^.+\*$/, "p", nil, nil, nil, nil],
- [/^.+\[\]$/, "a", nil, nil, nil, nil],
- ["void", "0", nil, nil, nil, nil],
+ nil, nil, nil],
+ ["char ref", "c", nil, nil,
+ nil, nil, nil],
+ ["short ref", "h", nil, nil,
+ nil, nil, nil],
+ ["int ref", "i", nil, nil,
+ nil, nil, nil],
+ ["long ref", "l", nil, nil,
+ nil, nil, nil],
+ ["float ref", "f", nil, nil,
+ nil, nil, nil],
+ ["double ref","d", nil, nil,
+ nil, nil, nil],
+ ["char", "C", nil, nil,
+ "C", nil, nil],
+ ["short", "H", nil, nil,
+ "H", nil, nil],
+ ["int", "I", nil, nil,
+ "I", nil, nil],
+ ["long", "L", nil, nil,
+ "L", nil, nil],
+ ["float", "F", nil, nil,
+ "F", nil, nil],
+ ["double", "D", nil, nil,
+ "D", nil, nil],
+ [/^char\s*\*$/,"s",nil, nil,
+ "S",nil, nil],
+ [/^const char\s*\*$/,"S",nil, nil,
+ "S",nil, nil],
+ [/^.+\*$/, "p", nil, nil,
+ "P", nil, nil],
+ [/^.+\[\]$/, "a", nil, nil,
+ "a", nil, nil],
+ ["void", "0", nil, nil,
+ nil, nil, nil],
]
def initialize
init_types()
end
- def typealias(ty1, ty2, enc=nil, dec=nil, senc=nil, sdec=nil)
- @TYDEFS.unshift([ty1,ty2, enc,dec, senc, sdec])
+ def typealias(ty1, ty2, enc=nil, dec=nil, ty3=nil, senc=nil, sdec=nil)
+ @TYDEFS.unshift([ty1, ty2, enc, dec, ty3, senc, sdec])
end
def init_types
@@ -122,12 +179,17 @@ module DL
dec = nil
senc = nil
sdec = nil
- @TYDEFS.each{|t1,t2,c1,c2,c3,c4|
+ ty1 = nil
+ ty2 = nil
+ @TYDEFS.each{|t1,t2,c1,c2,t3,c3,c4|
# if( t1.is_a?(String) )
# t1 = Regexp.new("^" + t1 + "$")
# end
if( (t1.is_a?(Regexp) && (t1 =~ ty)) || (t1 == ty) )
- ty = ty.gsub(t1,t2)
+ ty1 = ty.gsub(t1,t2)
+ ty2 = ty.gsub(t1,t3)
+ ty1.strip! if ty1
+ ty2.strip! if ty2
if( enc )
if( c1 )
conv1 = enc
@@ -170,11 +232,10 @@ module DL
end
end
}
- ty = ty.strip
- if( ty.length != 1 )
+ if( ty1.length != 1 && ty2.length != 1 )
raise(TypeError, "unknown type: #{orig_ty}.")
end
- return [ty,enc,dec,senc,sdec]
+ return [ty1,enc,dec,ty2,senc,sdec]
end
end # end of Types
end