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
This commit is contained in:
ttate 2004-12-14 12:42:42 +00:00
parent 2f35e1e146
commit ba2fce1507
3 changed files with 64 additions and 56 deletions

View File

@ -70,11 +70,11 @@ module DL
init_types() init_types()
init_sym() init_sym()
rty,renc,rdec,_,_,_ = @types.encode_type(ret) rty,renc,rdec = @types.encode_return_type(ret)
if( !rty ) if( !rty )
raise(TypeError, "unsupported type: #{ret}") raise(TypeError, "unsupported type: #{ret}")
end end
ty,enc,dec = encode_types(args) ty,enc,dec = encode_argument_types(args)
symty = rty + ty symty = rty + ty
module_eval("module_function :#{func}") module_eval("module_function :#{func}")
@ -134,11 +134,11 @@ module DL
init_types() init_types()
init_sym() init_sym()
rty,_,rdec,_,_,_ = @types.encode_type(rettype) rty,_,rdec = @types.encode_return_type(rettype)
if( !rty ) if( !rty )
raise(TypeError, "unsupported type: #{rettype}") raise(TypeError, "unsupported type: #{rettype}")
end end
ty,enc,dec = encode_types(argtypes) ty,enc,dec = encode_argument_types(argtypes)
symty = rty + ty symty = rty + ty
sym = symbol(name, symty) sym = symbol(name, symty)
@ -184,13 +184,13 @@ module DL
return @retval return @retval
end end
def encode_types(tys) def encode_argument_types(tys)
init_types() init_types()
encty = [] encty = []
enc = nil enc = nil
dec = nil dec = nil
tys.each_with_index{|ty,idx| tys.each_with_index{|ty,idx|
ty,c1,c2,_,_,_ = @types.encode_type(ty) ty,c1,c2 = @types.encode_argument_type(ty)
if( !ty ) if( !ty )
raise(TypeError, "unsupported type: #{ty}") raise(TypeError, "unsupported type: #{ty}")
end end

View File

@ -128,7 +128,7 @@ module DL
else else
raise(RuntimeError, "invalid element: #{elem}") raise(RuntimeError, "invalid element: #{elem}")
end end
_,_,_,ty,enc,dec = @types.encode_type(ty) ty,enc,dec = @types.encode_struct_type(ty)
if( !ty ) if( !ty )
raise(TypeError, "unsupported type: #{ty}") raise(TypeError, "unsupported type: #{ty}")
end end

View File

@ -173,65 +173,73 @@ module DL
@TYDEFS = TYPES.dup @TYDEFS = TYPES.dup
end end
def encode_type(ty) def encode_argument_type(alias_type)
orig_ty = ty proc_encode = nil
enc = nil proc_decode = nil
dec = nil @TYDEFS.each{|aty,ty,enc,dec,_,_,_|
senc = nil if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) )
sdec = nil alias_type = alias_type.gsub(aty,ty) if ty
ty1 = ty alias_type.strip! if alias_type
ty2 = ty if( proc_encode )
@TYDEFS.each{|t1,t2,c1,c2,t3,c3,c4| if( enc )
if( (t1.is_a?(Regexp) && (t1 =~ ty1)) || (t1 == ty1) ) conv1 = proc_encode
ty1 = ty1.gsub(t1,t2) if t2 proc_encode = proc{|v| enc.call(conv1.call(v))}
ty1.strip! if ty1
if( enc )
if( c1 )
conv1 = enc
enc = proc{|v| c1.call(conv1.call(v))}
end end
else else
if( c1 ) if( enc )
enc = c1 proc_encode = enc
end end
end end
if( dec ) if( proc_decode )
if( c2 ) if( dec )
conv2 = dec conv2 = proc_decode
dec = proc{|v| c2.call(conv2.call(v))} proc_decode = proc{|v| dec.call(conv2.call(v))}
end end
else else
if( c2 ) if( dec )
dec = c2 proc_decode = dec
end end
end 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))}
end
else
if( c3 )
senc = c3
end
end
if( sdec )
if( c4 )
conv4 = sdec
sdec = proc{|v| c4.call(conv4.call(v))}
end
else
if( c4 )
sdec = c4
end
end
end
} }
return [ty1,enc,dec,ty2,senc,sdec] 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( enc )
proc_encode = enc
end
end
if( proc_decode )
if( dec )
conv2 = proc_decode
proc_decode = proc{|v| dec.call(conv2.call(v))}
end
else
if( dec )
proc_decode = dec
end
end
end
}
return [alias_type, proc_encode, proc_decode]
end end
end # end of Types end # end of Types
end end