From ba2fce15075685e92d5d8be13df4df69dc56f63b Mon Sep 17 00:00:00 2001 From: ttate Date: Tue, 14 Dec 2004 12:42:42 +0000 Subject: devided DL::Types#encode_type into three methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/dl/lib/dl/import.rb | 12 +++---- ext/dl/lib/dl/struct.rb | 2 +- ext/dl/lib/dl/types.rb | 90 +++++++++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 48 deletions(-) (limited to 'ext/dl/lib') diff --git a/ext/dl/lib/dl/import.rb b/ext/dl/lib/dl/import.rb index 99978c6a14..2a98ace8ab 100644 --- a/ext/dl/lib/dl/import.rb +++ b/ext/dl/lib/dl/import.rb @@ -70,11 +70,11 @@ module DL init_types() init_sym() - rty,renc,rdec,_,_,_ = @types.encode_type(ret) + rty,renc,rdec = @types.encode_return_type(ret) if( !rty ) raise(TypeError, "unsupported type: #{ret}") end - ty,enc,dec = encode_types(args) + ty,enc,dec = encode_argument_types(args) symty = rty + ty module_eval("module_function :#{func}") @@ -134,11 +134,11 @@ module DL init_types() init_sym() - rty,_,rdec,_,_,_ = @types.encode_type(rettype) + rty,_,rdec = @types.encode_return_type(rettype) if( !rty ) raise(TypeError, "unsupported type: #{rettype}") end - ty,enc,dec = encode_types(argtypes) + ty,enc,dec = encode_argument_types(argtypes) symty = rty + ty sym = symbol(name, symty) @@ -184,13 +184,13 @@ module DL return @retval end - def encode_types(tys) + def encode_argument_types(tys) init_types() encty = [] enc = nil dec = nil tys.each_with_index{|ty,idx| - ty,c1,c2,_,_,_ = @types.encode_type(ty) + ty,c1,c2 = @types.encode_argument_type(ty) if( !ty ) raise(TypeError, "unsupported type: #{ty}") end diff --git a/ext/dl/lib/dl/struct.rb b/ext/dl/lib/dl/struct.rb index d3107fcf50..2f54019294 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_struct_type(ty) if( !ty ) raise(TypeError, "unsupported type: #{ty}") end diff --git a/ext/dl/lib/dl/types.rb b/ext/dl/lib/dl/types.rb index cc73893049..1144917dae 100644 --- a/ext/dl/lib/dl/types.rb +++ b/ext/dl/lib/dl/types.rb @@ -173,65 +173,73 @@ module DL @TYDEFS = TYPES.dup end - def encode_type(ty) - orig_ty = ty - enc = nil - dec = nil - senc = nil - sdec = nil - ty1 = ty - ty2 = ty - @TYDEFS.each{|t1,t2,c1,c2,t3,c3,c4| - if( (t1.is_a?(Regexp) && (t1 =~ ty1)) || (t1 == ty1) ) - ty1 = ty1.gsub(t1,t2) if t2 - ty1.strip! if ty1 - if( enc ) - if( c1 ) - conv1 = enc - enc = proc{|v| c1.call(conv1.call(v))} + def encode_argument_type(alias_type) + proc_encode = nil + proc_decode = nil + @TYDEFS.each{|aty,ty,enc,dec,_,_,_| + if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) ) + alias_type = alias_type.gsub(aty,ty) if ty + alias_type.strip! if alias_type + if( proc_encode ) + if( enc ) + conv1 = proc_encode + proc_encode = proc{|v| enc.call(conv1.call(v))} end else - if( c1 ) - enc = c1 + if( enc ) + proc_encode = enc end end - if( dec ) - if( c2 ) - conv2 = dec - dec = proc{|v| c2.call(conv2.call(v))} + if( proc_decode ) + if( dec ) + conv2 = proc_decode + proc_decode = proc{|v| dec.call(conv2.call(v))} end else - if( c2 ) - dec = c2 + if( dec ) + proc_decode = dec end end end - if( (t1.is_a?(Regexp) && (t1 =~ ty2)) || (t1 == ty2) ) - ty2 = ty2.gsub(t1,t3) if t3 - ty2.strip! if ty2 - if( senc ) - if( c3 ) - conv3 = senc - senc = proc{|v| c3.call(conv3.call(v))} + } + return [alias_type, proc_encode, proc_decode] + end + + def encode_return_type(ty) + ty, enc, dec = encode_argument_type(ty) + return [ty, enc, dec] + end + + def encode_struct_type(alias_type) + proc_encode = nil + proc_decode = nil + @TYDEFS.each{|aty,_,_,_,ty,enc,dec| + if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) ) + alias_type = alias_type.gsub(aty,ty) if ty + alias_type.strip! if alias_type + if( proc_encode ) + if( enc ) + conv1 = proc_encode + proc_encode = proc{|v| enc.call(conv1.call(v))} end else - if( c3 ) - senc = c3 + if( enc ) + proc_encode = enc end end - if( sdec ) - if( c4 ) - conv4 = sdec - sdec = proc{|v| c4.call(conv4.call(v))} + if( proc_decode ) + if( dec ) + conv2 = proc_decode + proc_decode = proc{|v| dec.call(conv2.call(v))} end else - if( c4 ) - sdec = c4 + if( dec ) + proc_decode = dec end end - end + end } - return [ty1,enc,dec,ty2,senc,sdec] + return [alias_type, proc_encode, proc_decode] end end # end of Types end -- cgit v1.2.3