mirror of
https://github.com/golang/go
synced 2024-11-21 19:54:41 -07:00
reflect: add Kind, remove Int8Type, Int8Value, etc.
update other code to match. R=r CC=golang-dev https://golang.org/cl/1680044
This commit is contained in:
parent
9f002f6892
commit
45bdf0367e
@ -390,6 +390,9 @@ enum {
|
|||||||
KindFloat,
|
KindFloat,
|
||||||
KindFloat32,
|
KindFloat32,
|
||||||
KindFloat64,
|
KindFloat64,
|
||||||
|
KindComplex,
|
||||||
|
KindComplex64,
|
||||||
|
KindComplex128,
|
||||||
KindArray,
|
KindArray,
|
||||||
KindChan,
|
KindChan,
|
||||||
KindFunc,
|
KindFunc,
|
||||||
@ -431,6 +434,9 @@ kinds[] =
|
|||||||
[TMAP] = KindMap,
|
[TMAP] = KindMap,
|
||||||
[TARRAY] = KindArray,
|
[TARRAY] = KindArray,
|
||||||
[TFUNC] = KindFunc,
|
[TFUNC] = KindFunc,
|
||||||
|
[TCOMPLEX] = KindComplex,
|
||||||
|
[TCOMPLEX64] = KindComplex64,
|
||||||
|
[TCOMPLEX128] = KindComplex128,
|
||||||
};
|
};
|
||||||
|
|
||||||
static char*
|
static char*
|
||||||
@ -438,21 +444,21 @@ structnames[] =
|
|||||||
{
|
{
|
||||||
[TINT] = "*runtime.IntType",
|
[TINT] = "*runtime.IntType",
|
||||||
[TUINT] = "*runtime.UintType",
|
[TUINT] = "*runtime.UintType",
|
||||||
[TINT8] = "*runtime.Int8Type",
|
[TINT8] = "*runtime.IntType",
|
||||||
[TUINT8] = "*runtime.Uint8Type",
|
[TUINT8] = "*runtime.UintType",
|
||||||
[TINT16] = "*runtime.Int16Type",
|
[TINT16] = "*runtime.IntType",
|
||||||
[TUINT16] = "*runtime.Uint16Type",
|
[TUINT16] = "*runtime.UintType",
|
||||||
[TINT32] = "*runtime.Int32Type",
|
[TINT32] = "*runtime.IntType",
|
||||||
[TUINT32] = "*runtime.Uint32Type",
|
[TUINT32] = "*runtime.UintType",
|
||||||
[TINT64] = "*runtime.Int64Type",
|
[TINT64] = "*runtime.IntType",
|
||||||
[TUINT64] = "*runtime.Uint64Type",
|
[TUINT64] = "*runtime.UintType",
|
||||||
[TUINTPTR] = "*runtime.UintptrType",
|
[TUINTPTR] = "*runtime.UintType",
|
||||||
[TCOMPLEX] = "*runtime.ComplexType",
|
[TCOMPLEX] = "*runtime.ComplexType",
|
||||||
[TCOMPLEX64] = "*runtime.Complex64Type",
|
[TCOMPLEX64] = "*runtime.ComplexType",
|
||||||
[TCOMPLEX128] = "*runtime.Complex128Type",
|
[TCOMPLEX128] = "*runtime.ComplexType",
|
||||||
[TFLOAT] = "*runtime.FloatType",
|
[TFLOAT] = "*runtime.FloatType",
|
||||||
[TFLOAT32] = "*runtime.Float32Type",
|
[TFLOAT32] = "*runtime.FloatType",
|
||||||
[TFLOAT64] = "*runtime.Float64Type",
|
[TFLOAT64] = "*runtime.FloatType",
|
||||||
[TBOOL] = "*runtime.BoolType",
|
[TBOOL] = "*runtime.BoolType",
|
||||||
[TSTRING] = "*runtime.StringType",
|
[TSTRING] = "*runtime.StringType",
|
||||||
|
|
||||||
|
@ -647,19 +647,22 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
|
|||||||
err = err1
|
err = err1
|
||||||
return
|
return
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
parsedInt, err1 := parseInt(innerBytes)
|
switch val.Type().Kind() {
|
||||||
if err1 == nil {
|
case reflect.Int:
|
||||||
val.Set(parsedInt)
|
parsedInt, err1 := parseInt(innerBytes)
|
||||||
|
if err1 == nil {
|
||||||
|
val.Set(int64(parsedInt))
|
||||||
|
}
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
case reflect.Int64:
|
||||||
|
parsedInt, err1 := parseInt64(innerBytes)
|
||||||
|
if err1 == nil {
|
||||||
|
val.Set(parsedInt)
|
||||||
|
}
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
}
|
}
|
||||||
err = err1
|
|
||||||
return
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
parsedInt, err1 := parseInt64(innerBytes)
|
|
||||||
if err1 == nil {
|
|
||||||
val.Set(parsedInt)
|
|
||||||
}
|
|
||||||
err = err1
|
|
||||||
return
|
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
structType := fieldType.(*reflect.StructType)
|
structType := fieldType.(*reflect.StructType)
|
||||||
|
|
||||||
@ -686,7 +689,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
|
|||||||
return
|
return
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
sliceType := fieldType.(*reflect.SliceType)
|
sliceType := fieldType.(*reflect.SliceType)
|
||||||
if _, ok := sliceType.Elem().(*reflect.Uint8Type); ok {
|
if sliceType.Elem().Kind() == reflect.Uint8 {
|
||||||
val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
|
val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
|
||||||
reflect.ArrayCopy(val, reflect.NewValue(innerBytes).(reflect.ArrayOrSliceValue))
|
reflect.ArrayCopy(val, reflect.NewValue(innerBytes).(reflect.ArrayOrSliceValue))
|
||||||
return
|
return
|
||||||
@ -729,9 +732,7 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
|
|||||||
}
|
}
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
val.Set(int(*params.defaultValue))
|
val.Set(*params.defaultValue)
|
||||||
case *reflect.Int64Value:
|
|
||||||
val.Set(int64(*params.defaultValue))
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -122,17 +122,15 @@ func getUniversalType(t reflect.Type) (tagNumber int, isCompound, ok bool) {
|
|||||||
case timeType:
|
case timeType:
|
||||||
return tagUTCTime, false, true
|
return tagUTCTime, false, true
|
||||||
}
|
}
|
||||||
switch i := t.(type) {
|
switch t := t.(type) {
|
||||||
case *reflect.BoolType:
|
case *reflect.BoolType:
|
||||||
return tagBoolean, false, true
|
return tagBoolean, false, true
|
||||||
case *reflect.IntType:
|
case *reflect.IntType:
|
||||||
return tagInteger, false, true
|
return tagInteger, false, true
|
||||||
case *reflect.Int64Type:
|
|
||||||
return tagInteger, false, true
|
|
||||||
case *reflect.StructType:
|
case *reflect.StructType:
|
||||||
return tagSequence, true, true
|
return tagSequence, true, true
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
if _, ok := t.(*reflect.SliceType).Elem().(*reflect.Uint8Type); ok {
|
if t.Elem().Kind() == reflect.Uint8 {
|
||||||
return tagOctetString, false, true
|
return tagOctetString, false, true
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(t.Name(), "SET") {
|
if strings.HasSuffix(t.Name(), "SET") {
|
||||||
|
@ -333,8 +333,6 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
|
|||||||
}
|
}
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
return marshalInt64(out, int64(v.Get()))
|
return marshalInt64(out, int64(v.Get()))
|
||||||
case *reflect.Int64Value:
|
|
||||||
return marshalInt64(out, v.Get())
|
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
t := v.Type().(*reflect.StructType)
|
t := v.Type().(*reflect.StructType)
|
||||||
|
|
||||||
@ -347,7 +345,7 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
|
|||||||
if s.Len() > 0 {
|
if s.Len() > 0 {
|
||||||
bytes := make([]byte, s.Len())
|
bytes := make([]byte, s.Len())
|
||||||
for i := 0; i < s.Len(); i++ {
|
for i := 0; i < s.Len(); i++ {
|
||||||
bytes[i] = s.Elem(i).(*reflect.Uint8Value).Get()
|
bytes[i] = uint8(s.Elem(i).(*reflect.UintValue).Get())
|
||||||
}
|
}
|
||||||
/* The RawContents will contain the tag and
|
/* The RawContents will contain the tag and
|
||||||
* length fields but we'll also be writing
|
* length fields but we'll also be writing
|
||||||
@ -371,10 +369,10 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
|
|||||||
return
|
return
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
sliceType := v.Type().(*reflect.SliceType)
|
sliceType := v.Type().(*reflect.SliceType)
|
||||||
if _, ok := sliceType.Elem().(*reflect.Uint8Type); ok {
|
if sliceType.Elem().Kind() == reflect.Uint8 {
|
||||||
bytes := make([]byte, v.Len())
|
bytes := make([]byte, v.Len())
|
||||||
for i := 0; i < v.Len(); i++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
bytes[i] = v.Elem(i).(*reflect.Uint8Value).Get()
|
bytes[i] = uint8(v.Elem(i).(*reflect.UintValue).Get())
|
||||||
}
|
}
|
||||||
_, err = out.Write(bytes)
|
_, err = out.Write(bytes)
|
||||||
return
|
return
|
||||||
|
@ -194,26 +194,8 @@ func sizeof(v reflect.Type) int {
|
|||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
|
|
||||||
case *reflect.Uint8Type:
|
case *reflect.UintType, *reflect.IntType, *reflect.FloatType:
|
||||||
return 1
|
return int(v.Size())
|
||||||
case *reflect.Uint16Type:
|
|
||||||
return 2
|
|
||||||
case *reflect.Uint32Type:
|
|
||||||
return 4
|
|
||||||
case *reflect.Uint64Type:
|
|
||||||
return 8
|
|
||||||
case *reflect.Int8Type:
|
|
||||||
return 1
|
|
||||||
case *reflect.Int16Type:
|
|
||||||
return 2
|
|
||||||
case *reflect.Int32Type:
|
|
||||||
return 4
|
|
||||||
case *reflect.Int64Type:
|
|
||||||
return 8
|
|
||||||
case *reflect.Float32Type:
|
|
||||||
return 4
|
|
||||||
case *reflect.Float64Type:
|
|
||||||
return 8
|
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
@ -307,26 +289,37 @@ func (d *decoder) value(v reflect.Value) {
|
|||||||
d.value(v.Elem(i))
|
d.value(v.Elem(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
case *reflect.Uint8Value:
|
case *reflect.IntValue:
|
||||||
v.Set(d.uint8())
|
switch v.Type().Kind() {
|
||||||
case *reflect.Uint16Value:
|
case reflect.Int8:
|
||||||
v.Set(d.uint16())
|
v.Set(int64(d.int8()))
|
||||||
case *reflect.Uint32Value:
|
case reflect.Int16:
|
||||||
v.Set(d.uint32())
|
v.Set(int64(d.int16()))
|
||||||
case *reflect.Uint64Value:
|
case reflect.Int32:
|
||||||
v.Set(d.uint64())
|
v.Set(int64(d.int32()))
|
||||||
case *reflect.Int8Value:
|
case reflect.Int64:
|
||||||
v.Set(d.int8())
|
v.Set(d.int64())
|
||||||
case *reflect.Int16Value:
|
}
|
||||||
v.Set(d.int16())
|
|
||||||
case *reflect.Int32Value:
|
case *reflect.UintValue:
|
||||||
v.Set(d.int32())
|
switch v.Type().Kind() {
|
||||||
case *reflect.Int64Value:
|
case reflect.Uint8:
|
||||||
v.Set(d.int64())
|
v.Set(uint64(d.uint8()))
|
||||||
case *reflect.Float32Value:
|
case reflect.Uint16:
|
||||||
v.Set(math.Float32frombits(d.uint32()))
|
v.Set(uint64(d.uint16()))
|
||||||
case *reflect.Float64Value:
|
case reflect.Uint32:
|
||||||
v.Set(math.Float64frombits(d.uint64()))
|
v.Set(uint64(d.uint32()))
|
||||||
|
case reflect.Uint64:
|
||||||
|
v.Set(d.uint64())
|
||||||
|
}
|
||||||
|
|
||||||
|
case *reflect.FloatValue:
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case reflect.Float32:
|
||||||
|
v.Set(float64(math.Float32frombits(d.uint32())))
|
||||||
|
case reflect.Float64:
|
||||||
|
v.Set(math.Float64frombits(d.uint64()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,25 +341,36 @@ func (e *encoder) value(v reflect.Value) {
|
|||||||
e.value(v.Elem(i))
|
e.value(v.Elem(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
case *reflect.Uint8Value:
|
case *reflect.IntValue:
|
||||||
e.uint8(v.Get())
|
switch v.Type().Kind() {
|
||||||
case *reflect.Uint16Value:
|
case reflect.Int8:
|
||||||
e.uint16(v.Get())
|
e.int8(int8(v.Get()))
|
||||||
case *reflect.Uint32Value:
|
case reflect.Int16:
|
||||||
e.uint32(v.Get())
|
e.int16(int16(v.Get()))
|
||||||
case *reflect.Uint64Value:
|
case reflect.Int32:
|
||||||
e.uint64(v.Get())
|
e.int32(int32(v.Get()))
|
||||||
case *reflect.Int8Value:
|
case reflect.Int64:
|
||||||
e.int8(v.Get())
|
e.int64(v.Get())
|
||||||
case *reflect.Int16Value:
|
}
|
||||||
e.int16(v.Get())
|
|
||||||
case *reflect.Int32Value:
|
case *reflect.UintValue:
|
||||||
e.int32(v.Get())
|
switch v.Type().Kind() {
|
||||||
case *reflect.Int64Value:
|
case reflect.Uint8:
|
||||||
e.int64(v.Get())
|
e.uint8(uint8(v.Get()))
|
||||||
case *reflect.Float32Value:
|
case reflect.Uint16:
|
||||||
e.uint32(math.Float32bits(v.Get()))
|
e.uint16(uint16(v.Get()))
|
||||||
case *reflect.Float64Value:
|
case reflect.Uint32:
|
||||||
e.uint64(math.Float64bits(v.Get()))
|
e.uint32(uint32(v.Get()))
|
||||||
|
case reflect.Uint64:
|
||||||
|
e.uint64(v.Get())
|
||||||
|
}
|
||||||
|
|
||||||
|
case *reflect.FloatValue:
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case reflect.Float32:
|
||||||
|
e.uint32(math.Float32bits(float32(v.Get())))
|
||||||
|
case reflect.Float64:
|
||||||
|
e.uint64(math.Float64bits(v.Get()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,37 +37,45 @@ func TypeFromNative(t reflect.Type) Type {
|
|||||||
switch t := t.(type) {
|
switch t := t.(type) {
|
||||||
case *reflect.BoolType:
|
case *reflect.BoolType:
|
||||||
et = BoolType
|
et = BoolType
|
||||||
case *reflect.Float32Type:
|
|
||||||
et = Float32Type
|
|
||||||
case *reflect.Float64Type:
|
|
||||||
et = Float64Type
|
|
||||||
case *reflect.FloatType:
|
case *reflect.FloatType:
|
||||||
et = FloatType
|
switch t.Kind() {
|
||||||
case *reflect.Int16Type:
|
case reflect.Float32:
|
||||||
et = Int16Type
|
et = Float32Type
|
||||||
case *reflect.Int32Type:
|
case reflect.Float64:
|
||||||
et = Int32Type
|
et = Float64Type
|
||||||
case *reflect.Int64Type:
|
case reflect.Float:
|
||||||
et = Int64Type
|
et = FloatType
|
||||||
case *reflect.Int8Type:
|
}
|
||||||
et = Int8Type
|
|
||||||
case *reflect.IntType:
|
case *reflect.IntType:
|
||||||
et = IntType
|
switch t.Kind() {
|
||||||
|
case reflect.Int16:
|
||||||
|
et = Int16Type
|
||||||
|
case reflect.Int32:
|
||||||
|
et = Int32Type
|
||||||
|
case reflect.Int64:
|
||||||
|
et = Int64Type
|
||||||
|
case reflect.Int8:
|
||||||
|
et = Int8Type
|
||||||
|
case reflect.Int:
|
||||||
|
et = IntType
|
||||||
|
}
|
||||||
|
case *reflect.UintType:
|
||||||
|
switch t.Kind() {
|
||||||
|
case reflect.Uint16:
|
||||||
|
et = Uint16Type
|
||||||
|
case reflect.Uint32:
|
||||||
|
et = Uint32Type
|
||||||
|
case reflect.Uint64:
|
||||||
|
et = Uint64Type
|
||||||
|
case reflect.Uint8:
|
||||||
|
et = Uint8Type
|
||||||
|
case reflect.Uint:
|
||||||
|
et = UintType
|
||||||
|
case reflect.Uintptr:
|
||||||
|
et = UintptrType
|
||||||
|
}
|
||||||
case *reflect.StringType:
|
case *reflect.StringType:
|
||||||
et = StringType
|
et = StringType
|
||||||
case *reflect.Uint16Type:
|
|
||||||
et = Uint16Type
|
|
||||||
case *reflect.Uint32Type:
|
|
||||||
et = Uint32Type
|
|
||||||
case *reflect.Uint64Type:
|
|
||||||
et = Uint64Type
|
|
||||||
case *reflect.Uint8Type:
|
|
||||||
et = Uint8Type
|
|
||||||
case *reflect.UintType:
|
|
||||||
et = UintType
|
|
||||||
case *reflect.UintptrType:
|
|
||||||
et = UintptrType
|
|
||||||
|
|
||||||
case *reflect.ArrayType:
|
case *reflect.ArrayType:
|
||||||
et = NewArrayType(int64(t.Len()), TypeFromNative(t.Elem()))
|
et = NewArrayType(int64(t.Len()), TypeFromNative(t.Elem()))
|
||||||
case *reflect.ChanType:
|
case *reflect.ChanType:
|
||||||
|
@ -237,7 +237,7 @@ func (p *Process) bootstrap() {
|
|||||||
if sym == nil {
|
if sym == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rtv.Field(i).(*reflect.Uint64Value).Set(sym.Value)
|
rtv.Field(i).(*reflect.UintValue).Set(sym.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get runtime field indexes
|
// Get runtime field indexes
|
||||||
|
@ -265,7 +265,7 @@ func fillRuntimeIndexes(runtime *runtimeValues, out *runtimeIndexes) {
|
|||||||
for j := 0; j < outStructt.NumField(); j++ {
|
for j := 0; j < outStructt.NumField(); j++ {
|
||||||
f := outStructv.Field(j).(*reflect.IntValue)
|
f := outStructv.Field(j).(*reflect.IntValue)
|
||||||
name := outStructt.Field(j).Name
|
name := outStructt.Field(j).Name
|
||||||
f.Set(indexes[name])
|
f.Set(int64(indexes[name]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -468,8 +468,6 @@ func (p *pp) fmtUint64(v uint64, verb int, sharp bool, value interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var floatBits = reflect.Typeof(float(0)).Size() * 8
|
|
||||||
|
|
||||||
func (p *pp) fmtFloat32(v float32, verb int, value interface{}) {
|
func (p *pp) fmtFloat32(v float32, verb int, value interface{}) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'b':
|
case 'b':
|
||||||
@ -508,8 +506,6 @@ func (p *pp) fmtFloat64(v float64, verb int, value interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var complexBits = reflect.Typeof(complex(0i)).Size() * 8
|
|
||||||
|
|
||||||
func (p *pp) fmtComplex64(v complex64, verb int, value interface{}) {
|
func (p *pp) fmtComplex64(v complex64, verb int, value interface{}) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'e', 'E', 'f', 'F', 'g', 'G':
|
case 'e', 'E', 'f', 'F', 'g', 'G':
|
||||||
@ -615,6 +611,13 @@ func (p *pp) fmtUintptrGetter(field interface{}, value reflect.Value, verb int,
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
intBits = uintptr(reflect.Typeof(int(0)).Size() * 8)
|
||||||
|
floatBits = uintptr(reflect.Typeof(float(0)).Size() * 8)
|
||||||
|
complexBits = uintptr(reflect.Typeof(complex(0+0i)).Size() * 8)
|
||||||
|
uintptrBits = uintptr(reflect.Typeof(uintptr(0)).Size() * 8)
|
||||||
|
)
|
||||||
|
|
||||||
func (p *pp) printField(field interface{}, verb int, plus, sharp bool, depth int) (was_string bool) {
|
func (p *pp) printField(field interface{}, verb int, plus, sharp bool, depth int) (was_string bool) {
|
||||||
if field != nil {
|
if field != nil {
|
||||||
switch {
|
switch {
|
||||||
@ -724,47 +727,21 @@ BigSwitch:
|
|||||||
case *reflect.BoolValue:
|
case *reflect.BoolValue:
|
||||||
p.fmtBool(f.Get(), verb, field)
|
p.fmtBool(f.Get(), verb, field)
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
p.fmtInt64(int64(f.Get()), verb, field)
|
|
||||||
case *reflect.Int8Value:
|
|
||||||
p.fmtInt64(int64(f.Get()), verb, field)
|
|
||||||
case *reflect.Int16Value:
|
|
||||||
p.fmtInt64(int64(f.Get()), verb, field)
|
|
||||||
case *reflect.Int32Value:
|
|
||||||
p.fmtInt64(int64(f.Get()), verb, field)
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
p.fmtInt64(f.Get(), verb, field)
|
p.fmtInt64(f.Get(), verb, field)
|
||||||
case *reflect.UintValue:
|
case *reflect.UintValue:
|
||||||
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
||||||
case *reflect.Uint8Value:
|
|
||||||
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
|
||||||
case *reflect.Uint16Value:
|
|
||||||
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
|
||||||
case *reflect.Uint64Value:
|
|
||||||
p.fmtUint64(f.Get(), verb, sharp, field)
|
|
||||||
case *reflect.UintptrValue:
|
|
||||||
p.fmtUint64(uint64(f.Get()), verb, sharp, field)
|
|
||||||
case *reflect.FloatValue:
|
case *reflect.FloatValue:
|
||||||
if floatBits == 32 {
|
if f.Type().Size() == 4 {
|
||||||
p.fmtFloat32(float32(f.Get()), verb, field)
|
p.fmtFloat32(float32(f.Get()), verb, field)
|
||||||
} else {
|
} else {
|
||||||
p.fmtFloat64(float64(f.Get()), verb, field)
|
p.fmtFloat64(float64(f.Get()), verb, field)
|
||||||
}
|
}
|
||||||
case *reflect.Float32Value:
|
|
||||||
p.fmtFloat64(float64(f.Get()), verb, field)
|
|
||||||
case *reflect.Float64Value:
|
|
||||||
p.fmtFloat64(f.Get(), verb, field)
|
|
||||||
case *reflect.ComplexValue:
|
case *reflect.ComplexValue:
|
||||||
if complexBits == 64 {
|
if f.Type().Size() == 8 {
|
||||||
p.fmtComplex64(complex64(f.Get()), verb, field)
|
p.fmtComplex64(complex64(f.Get()), verb, field)
|
||||||
} else {
|
} else {
|
||||||
p.fmtComplex128(complex128(f.Get()), verb, field)
|
p.fmtComplex128(complex128(f.Get()), verb, field)
|
||||||
}
|
}
|
||||||
case *reflect.Complex64Value:
|
|
||||||
p.fmtComplex64(f.Get(), verb, field)
|
|
||||||
case *reflect.Complex128Value:
|
|
||||||
p.fmtComplex128(f.Get(), verb, field)
|
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
p.fmtString(f.Get(), verb, sharp, field)
|
p.fmtString(f.Get(), verb, sharp, field)
|
||||||
case *reflect.MapValue:
|
case *reflect.MapValue:
|
||||||
|
@ -353,8 +353,6 @@ func (s *ss) typeError(field interface{}, expected string) {
|
|||||||
s.errorString("expected field of type pointer to " + expected + "; found " + reflect.Typeof(field).String())
|
s.errorString("expected field of type pointer to " + expected + "; found " + reflect.Typeof(field).String())
|
||||||
}
|
}
|
||||||
|
|
||||||
var intBits = uint(reflect.Typeof(int(0)).Size() * 8)
|
|
||||||
var uintptrBits = uint(reflect.Typeof(int(0)).Size() * 8)
|
|
||||||
var complexError = os.ErrorString("syntax error scanning complex number")
|
var complexError = os.ErrorString("syntax error scanning complex number")
|
||||||
var boolError = os.ErrorString("syntax error scanning boolean")
|
var boolError = os.ErrorString("syntax error scanning boolean")
|
||||||
|
|
||||||
@ -458,7 +456,7 @@ func (s *ss) scanNumber(digits string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// scanRune returns the next rune value in the input.
|
// scanRune returns the next rune value in the input.
|
||||||
func (s *ss) scanRune(bitSize uint) int64 {
|
func (s *ss) scanRune(bitSize uintptr) int64 {
|
||||||
rune := int64(s.mustGetRune())
|
rune := int64(s.mustGetRune())
|
||||||
x := (rune << (64 - bitSize)) >> (64 - bitSize)
|
x := (rune << (64 - bitSize)) >> (64 - bitSize)
|
||||||
if x != rune {
|
if x != rune {
|
||||||
@ -469,7 +467,7 @@ func (s *ss) scanRune(bitSize uint) int64 {
|
|||||||
|
|
||||||
// scanInt returns the value of the integer represented by the next
|
// scanInt returns the value of the integer represented by the next
|
||||||
// token, checking for overflow. Any error is stored in s.err.
|
// token, checking for overflow. Any error is stored in s.err.
|
||||||
func (s *ss) scanInt(verb int, bitSize uint) int64 {
|
func (s *ss) scanInt(verb int, bitSize uintptr) int64 {
|
||||||
if verb == 'c' {
|
if verb == 'c' {
|
||||||
return s.scanRune(bitSize)
|
return s.scanRune(bitSize)
|
||||||
}
|
}
|
||||||
@ -490,7 +488,7 @@ func (s *ss) scanInt(verb int, bitSize uint) int64 {
|
|||||||
|
|
||||||
// scanUint returns the value of the unsigned integer represented
|
// scanUint returns the value of the unsigned integer represented
|
||||||
// by the next token, checking for overflow. Any error is stored in s.err.
|
// by the next token, checking for overflow. Any error is stored in s.err.
|
||||||
func (s *ss) scanUint(verb int, bitSize uint) uint64 {
|
func (s *ss) scanUint(verb int, bitSize uintptr) uint64 {
|
||||||
if verb == 'c' {
|
if verb == 'c' {
|
||||||
return uint64(s.scanRune(bitSize))
|
return uint64(s.scanRune(bitSize))
|
||||||
}
|
}
|
||||||
@ -559,27 +557,9 @@ func (s *ss) complexTokens() (real, imag string) {
|
|||||||
return real, imagSign + imag
|
return real, imagSign + imag
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertFloat converts the string to a float value.
|
// convertFloat converts the string to a float64value.
|
||||||
func (s *ss) convertFloat(str string) float64 {
|
func (s *ss) convertFloat(str string, n int) float64 {
|
||||||
f, err := strconv.Atof(str)
|
f, err := strconv.AtofN(str, n)
|
||||||
if err != nil {
|
|
||||||
s.error(err)
|
|
||||||
}
|
|
||||||
return float64(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertFloat32 converts the string to a float32 value.
|
|
||||||
func (s *ss) convertFloat32(str string) float64 {
|
|
||||||
f, err := strconv.Atof32(str)
|
|
||||||
if err != nil {
|
|
||||||
s.error(err)
|
|
||||||
}
|
|
||||||
return float64(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertFloat64 converts the string to a float64 value.
|
|
||||||
func (s *ss) convertFloat64(str string) float64 {
|
|
||||||
f, err := strconv.Atof64(str)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err)
|
s.error(err)
|
||||||
}
|
}
|
||||||
@ -590,14 +570,14 @@ func (s *ss) convertFloat64(str string) float64 {
|
|||||||
// The atof argument is a type-specific reader for the underlying type.
|
// The atof argument is a type-specific reader for the underlying type.
|
||||||
// If we're reading complex64, atof will parse float32s and convert them
|
// If we're reading complex64, atof will parse float32s and convert them
|
||||||
// to float64's to avoid reproducing this code for each complex type.
|
// to float64's to avoid reproducing this code for each complex type.
|
||||||
func (s *ss) scanComplex(verb int, atof func(*ss, string) float64) complex128 {
|
func (s *ss) scanComplex(verb int, n int) complex128 {
|
||||||
if !s.okVerb(verb, floatVerbs, "complex") {
|
if !s.okVerb(verb, floatVerbs, "complex") {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
s.skipSpace()
|
s.skipSpace()
|
||||||
sreal, simag := s.complexTokens()
|
sreal, simag := s.complexTokens()
|
||||||
real := atof(s, sreal)
|
real := s.convertFloat(sreal, n/2)
|
||||||
imag := atof(s, simag)
|
imag := s.convertFloat(simag, n/2)
|
||||||
return cmplx(real, imag)
|
return cmplx(real, imag)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -725,11 +705,11 @@ func (s *ss) scanOne(verb int, field interface{}) {
|
|||||||
case *bool:
|
case *bool:
|
||||||
*v = s.scanBool(verb)
|
*v = s.scanBool(verb)
|
||||||
case *complex:
|
case *complex:
|
||||||
*v = complex(s.scanComplex(verb, (*ss).convertFloat))
|
*v = complex(s.scanComplex(verb, int(complexBits)))
|
||||||
case *complex64:
|
case *complex64:
|
||||||
*v = complex64(s.scanComplex(verb, (*ss).convertFloat32))
|
*v = complex64(s.scanComplex(verb, 64))
|
||||||
case *complex128:
|
case *complex128:
|
||||||
*v = s.scanComplex(verb, (*ss).convertFloat64)
|
*v = s.scanComplex(verb, 128)
|
||||||
case *int:
|
case *int:
|
||||||
*v = int(s.scanInt(verb, intBits))
|
*v = int(s.scanInt(verb, intBits))
|
||||||
case *int8:
|
case *int8:
|
||||||
@ -757,17 +737,17 @@ func (s *ss) scanOne(verb int, field interface{}) {
|
|||||||
case *float:
|
case *float:
|
||||||
if s.okVerb(verb, floatVerbs, "float") {
|
if s.okVerb(verb, floatVerbs, "float") {
|
||||||
s.skipSpace()
|
s.skipSpace()
|
||||||
*v = float(s.convertFloat(s.floatToken()))
|
*v = float(s.convertFloat(s.floatToken(), int(floatBits)))
|
||||||
}
|
}
|
||||||
case *float32:
|
case *float32:
|
||||||
if s.okVerb(verb, floatVerbs, "float32") {
|
if s.okVerb(verb, floatVerbs, "float32") {
|
||||||
s.skipSpace()
|
s.skipSpace()
|
||||||
*v = float32(s.convertFloat32(s.floatToken()))
|
*v = float32(s.convertFloat(s.floatToken(), 32))
|
||||||
}
|
}
|
||||||
case *float64:
|
case *float64:
|
||||||
if s.okVerb(verb, floatVerbs, "float64") {
|
if s.okVerb(verb, floatVerbs, "float64") {
|
||||||
s.skipSpace()
|
s.skipSpace()
|
||||||
*v = s.convertFloat64(s.floatToken())
|
*v = s.convertFloat(s.floatToken(), 64)
|
||||||
}
|
}
|
||||||
case *string:
|
case *string:
|
||||||
*v = s.convertString(verb)
|
*v = s.convertString(verb)
|
||||||
@ -786,55 +766,27 @@ func (s *ss) scanOne(verb int, field interface{}) {
|
|||||||
case *reflect.BoolValue:
|
case *reflect.BoolValue:
|
||||||
v.Set(s.scanBool(verb))
|
v.Set(s.scanBool(verb))
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
v.Set(int(s.scanInt(verb, intBits)))
|
v.Set(s.scanInt(verb, v.Type().Size()*8))
|
||||||
case *reflect.Int8Value:
|
|
||||||
v.Set(int8(s.scanInt(verb, 8)))
|
|
||||||
case *reflect.Int16Value:
|
|
||||||
v.Set(int16(s.scanInt(verb, 16)))
|
|
||||||
case *reflect.Int32Value:
|
|
||||||
v.Set(int32(s.scanInt(verb, 32)))
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
v.Set(s.scanInt(verb, 64))
|
|
||||||
case *reflect.UintValue:
|
case *reflect.UintValue:
|
||||||
v.Set(uint(s.scanUint(verb, intBits)))
|
v.Set(s.scanUint(verb, v.Type().Size()*8))
|
||||||
case *reflect.Uint8Value:
|
|
||||||
v.Set(uint8(s.scanUint(verb, 8)))
|
|
||||||
case *reflect.Uint16Value:
|
|
||||||
v.Set(uint16(s.scanUint(verb, 16)))
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
v.Set(uint32(s.scanUint(verb, 32)))
|
|
||||||
case *reflect.Uint64Value:
|
|
||||||
v.Set(s.scanUint(verb, 64))
|
|
||||||
case *reflect.UintptrValue:
|
|
||||||
v.Set(uintptr(s.scanUint(verb, uintptrBits)))
|
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
v.Set(s.convertString(verb))
|
v.Set(s.convertString(verb))
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
// For now, can only handle (renamed) []byte.
|
// For now, can only handle (renamed) []byte.
|
||||||
typ := v.Type().(*reflect.SliceType)
|
typ := v.Type().(*reflect.SliceType)
|
||||||
if _, ok := typ.Elem().(*reflect.Uint8Type); !ok {
|
if typ.Elem().Kind() != reflect.Uint8 {
|
||||||
goto CantHandle
|
goto CantHandle
|
||||||
}
|
}
|
||||||
str := s.convertString(verb)
|
str := s.convertString(verb)
|
||||||
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
|
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
|
||||||
for i := 0; i < len(str); i++ {
|
for i := 0; i < len(str); i++ {
|
||||||
v.Elem(i).(*reflect.Uint8Value).Set(str[i])
|
v.Elem(i).(*reflect.UintValue).Set(uint64(str[i]))
|
||||||
}
|
}
|
||||||
case *reflect.FloatValue:
|
case *reflect.FloatValue:
|
||||||
s.skipSpace()
|
s.skipSpace()
|
||||||
v.Set(float(s.convertFloat(s.floatToken())))
|
v.Set(s.convertFloat(s.floatToken(), int(v.Type().Size()*8)))
|
||||||
case *reflect.Float32Value:
|
|
||||||
s.skipSpace()
|
|
||||||
v.Set(float32(s.convertFloat(s.floatToken())))
|
|
||||||
case *reflect.Float64Value:
|
|
||||||
s.skipSpace()
|
|
||||||
v.Set(s.convertFloat(s.floatToken()))
|
|
||||||
case *reflect.ComplexValue:
|
case *reflect.ComplexValue:
|
||||||
v.Set(complex(s.scanComplex(verb, (*ss).convertFloat)))
|
v.Set(s.scanComplex(verb, int(v.Type().Size()*8)))
|
||||||
case *reflect.Complex64Value:
|
|
||||||
v.Set(complex64(s.scanComplex(verb, (*ss).convertFloat32)))
|
|
||||||
case *reflect.Complex128Value:
|
|
||||||
v.Set(s.scanComplex(verb, (*ss).convertFloat64))
|
|
||||||
default:
|
default:
|
||||||
CantHandle:
|
CantHandle:
|
||||||
s.errorString("Scan: can't handle type: " + val.Type().String())
|
s.errorString("Scan: can't handle type: " + val.Type().String())
|
||||||
|
@ -360,7 +360,7 @@ func TestScalarDecInstructions(t *testing.T) {
|
|||||||
var data struct {
|
var data struct {
|
||||||
a int
|
a int
|
||||||
}
|
}
|
||||||
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
instr := &decInstr{decOpMap[reflect.Int], 6, 0, 0, ovfl}
|
||||||
state := newDecodeStateFromData(signedResult)
|
state := newDecodeStateFromData(signedResult)
|
||||||
execDec("int", instr, state, t, unsafe.Pointer(&data))
|
execDec("int", instr, state, t, unsafe.Pointer(&data))
|
||||||
if data.a != 17 {
|
if data.a != 17 {
|
||||||
@ -373,7 +373,7 @@ func TestScalarDecInstructions(t *testing.T) {
|
|||||||
var data struct {
|
var data struct {
|
||||||
a uint
|
a uint
|
||||||
}
|
}
|
||||||
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
instr := &decInstr{decOpMap[reflect.Uint], 6, 0, 0, ovfl}
|
||||||
state := newDecodeStateFromData(unsignedResult)
|
state := newDecodeStateFromData(unsignedResult)
|
||||||
execDec("uint", instr, state, t, unsafe.Pointer(&data))
|
execDec("uint", instr, state, t, unsafe.Pointer(&data))
|
||||||
if data.a != 17 {
|
if data.a != 17 {
|
||||||
@ -464,7 +464,7 @@ func TestScalarDecInstructions(t *testing.T) {
|
|||||||
var data struct {
|
var data struct {
|
||||||
a uintptr
|
a uintptr
|
||||||
}
|
}
|
||||||
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
instr := &decInstr{decOpMap[reflect.Uintptr], 6, 0, 0, ovfl}
|
||||||
state := newDecodeStateFromData(unsignedResult)
|
state := newDecodeStateFromData(unsignedResult)
|
||||||
execDec("uintptr", instr, state, t, unsafe.Pointer(&data))
|
execDec("uintptr", instr, state, t, unsafe.Pointer(&data))
|
||||||
if data.a != 17 {
|
if data.a != 17 {
|
||||||
@ -503,7 +503,7 @@ func TestScalarDecInstructions(t *testing.T) {
|
|||||||
var data struct {
|
var data struct {
|
||||||
a float
|
a float
|
||||||
}
|
}
|
||||||
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
instr := &decInstr{decOpMap[reflect.Float], 6, 0, 0, ovfl}
|
||||||
state := newDecodeStateFromData(floatResult)
|
state := newDecodeStateFromData(floatResult)
|
||||||
execDec("float", instr, state, t, unsafe.Pointer(&data))
|
execDec("float", instr, state, t, unsafe.Pointer(&data))
|
||||||
if data.a != 17 {
|
if data.a != 17 {
|
||||||
|
@ -540,19 +540,19 @@ func ignoreSlice(state *decodeState, elemOp decOp) os.Error {
|
|||||||
return ignoreArrayHelper(state, elemOp, int(decodeUint(state)))
|
return ignoreArrayHelper(state, elemOp, int(decodeUint(state)))
|
||||||
}
|
}
|
||||||
|
|
||||||
var decOpMap = map[reflect.Type]decOp{
|
var decOpMap = map[reflect.Kind]decOp{
|
||||||
valueKind(false): decBool,
|
reflect.Bool: decBool,
|
||||||
valueKind(int8(0)): decInt8,
|
reflect.Int8: decInt8,
|
||||||
valueKind(int16(0)): decInt16,
|
reflect.Int16: decInt16,
|
||||||
valueKind(int32(0)): decInt32,
|
reflect.Int32: decInt32,
|
||||||
valueKind(int64(0)): decInt64,
|
reflect.Int64: decInt64,
|
||||||
valueKind(uint8(0)): decUint8,
|
reflect.Uint8: decUint8,
|
||||||
valueKind(uint16(0)): decUint16,
|
reflect.Uint16: decUint16,
|
||||||
valueKind(uint32(0)): decUint32,
|
reflect.Uint32: decUint32,
|
||||||
valueKind(uint64(0)): decUint64,
|
reflect.Uint64: decUint64,
|
||||||
valueKind(float32(0)): decFloat32,
|
reflect.Float32: decFloat32,
|
||||||
valueKind(float64(0)): decFloat64,
|
reflect.Float64: decFloat64,
|
||||||
valueKind("x"): decString,
|
reflect.String: decString,
|
||||||
}
|
}
|
||||||
|
|
||||||
var decIgnoreOpMap = map[typeId]decOp{
|
var decIgnoreOpMap = map[typeId]decOp{
|
||||||
@ -568,7 +568,7 @@ var decIgnoreOpMap = map[typeId]decOp{
|
|||||||
// the indirection count to reach it.
|
// the indirection count to reach it.
|
||||||
func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string) (decOp, int, os.Error) {
|
func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string) (decOp, int, os.Error) {
|
||||||
typ, indir := indirect(rt)
|
typ, indir := indirect(rt)
|
||||||
op, ok := decOpMap[reflect.Typeof(typ)]
|
op, ok := decOpMap[typ.Kind()]
|
||||||
if !ok {
|
if !ok {
|
||||||
// Special cases
|
// Special cases
|
||||||
switch t := typ.(type) {
|
switch t := typ.(type) {
|
||||||
@ -604,7 +604,7 @@ func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string) (decOp
|
|||||||
|
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
name = "element of " + name
|
name = "element of " + name
|
||||||
if _, ok := t.Elem().(*reflect.Uint8Type); ok {
|
if t.Elem().Kind() == reflect.Uint8 {
|
||||||
op = decUint8Array
|
op = decUint8Array
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -718,11 +718,11 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId) bool {
|
|||||||
return false
|
return false
|
||||||
case *reflect.BoolType:
|
case *reflect.BoolType:
|
||||||
return fw == tBool
|
return fw == tBool
|
||||||
case *reflect.IntType, *reflect.Int8Type, *reflect.Int16Type, *reflect.Int32Type, *reflect.Int64Type:
|
case *reflect.IntType:
|
||||||
return fw == tInt
|
return fw == tInt
|
||||||
case *reflect.UintType, *reflect.Uint8Type, *reflect.Uint16Type, *reflect.Uint32Type, *reflect.Uint64Type, *reflect.UintptrType:
|
case *reflect.UintType:
|
||||||
return fw == tUint
|
return fw == tUint
|
||||||
case *reflect.FloatType, *reflect.Float32Type, *reflect.Float64Type:
|
case *reflect.FloatType:
|
||||||
return fw == tFloat
|
return fw == tFloat
|
||||||
case *reflect.StringType:
|
case *reflect.StringType:
|
||||||
return fw == tString
|
return fw == tString
|
||||||
@ -742,8 +742,7 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId) bool {
|
|||||||
return dec.compatibleType(t.Key(), mapType.Key) && dec.compatibleType(t.Elem(), mapType.Elem)
|
return dec.compatibleType(t.Key(), mapType.Key) && dec.compatibleType(t.Elem(), mapType.Elem)
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
// Is it an array of bytes?
|
// Is it an array of bytes?
|
||||||
et := t.Elem()
|
if t.Elem().Kind() == reflect.Uint8 {
|
||||||
if _, ok := et.(*reflect.Uint8Type); ok {
|
|
||||||
return fw == tBytes
|
return fw == tBytes
|
||||||
}
|
}
|
||||||
// Extract and compare element types.
|
// Extract and compare element types.
|
||||||
@ -874,7 +873,7 @@ func init() {
|
|||||||
default:
|
default:
|
||||||
panic("gob: unknown size of float")
|
panic("gob: unknown size of float")
|
||||||
}
|
}
|
||||||
decOpMap[valueKind(float(0))] = op
|
decOpMap[reflect.Float] = op
|
||||||
|
|
||||||
// A similar assumption about int and uint. Also assume int and uint have the same size.
|
// A similar assumption about int and uint. Also assume int and uint have the same size.
|
||||||
var uop decOp
|
var uop decOp
|
||||||
@ -888,8 +887,8 @@ func init() {
|
|||||||
default:
|
default:
|
||||||
panic("gob: unknown size of int/uint")
|
panic("gob: unknown size of int/uint")
|
||||||
}
|
}
|
||||||
decOpMap[valueKind(int(0))] = op
|
decOpMap[reflect.Int] = op
|
||||||
decOpMap[valueKind(uint(0))] = uop
|
decOpMap[reflect.Uint] = uop
|
||||||
|
|
||||||
// Finally uintptr
|
// Finally uintptr
|
||||||
switch unsafe.Sizeof(uintptr(0)) {
|
switch unsafe.Sizeof(uintptr(0)) {
|
||||||
@ -900,5 +899,5 @@ func init() {
|
|||||||
default:
|
default:
|
||||||
panic("gob: unknown size of uintptr")
|
panic("gob: unknown size of uintptr")
|
||||||
}
|
}
|
||||||
decOpMap[valueKind(uintptr(0))] = uop
|
decOpMap[reflect.Uintptr] = uop
|
||||||
}
|
}
|
||||||
|
@ -601,35 +601,35 @@ func encodeMap(b *bytes.Buffer, rt reflect.Type, p uintptr, keyOp, elemOp encOp,
|
|||||||
return state.err
|
return state.err
|
||||||
}
|
}
|
||||||
|
|
||||||
var encOpMap = map[reflect.Type]encOp{
|
var encOpMap = map[reflect.Kind]encOp{
|
||||||
valueKind(false): encBool,
|
reflect.Bool: encBool,
|
||||||
valueKind(int(0)): encInt,
|
reflect.Int: encInt,
|
||||||
valueKind(int8(0)): encInt8,
|
reflect.Int8: encInt8,
|
||||||
valueKind(int16(0)): encInt16,
|
reflect.Int16: encInt16,
|
||||||
valueKind(int32(0)): encInt32,
|
reflect.Int32: encInt32,
|
||||||
valueKind(int64(0)): encInt64,
|
reflect.Int64: encInt64,
|
||||||
valueKind(uint(0)): encUint,
|
reflect.Uint: encUint,
|
||||||
valueKind(uint8(0)): encUint8,
|
reflect.Uint8: encUint8,
|
||||||
valueKind(uint16(0)): encUint16,
|
reflect.Uint16: encUint16,
|
||||||
valueKind(uint32(0)): encUint32,
|
reflect.Uint32: encUint32,
|
||||||
valueKind(uint64(0)): encUint64,
|
reflect.Uint64: encUint64,
|
||||||
valueKind(uintptr(0)): encUintptr,
|
reflect.Uintptr: encUintptr,
|
||||||
valueKind(float(0)): encFloat,
|
reflect.Float: encFloat,
|
||||||
valueKind(float32(0)): encFloat32,
|
reflect.Float32: encFloat32,
|
||||||
valueKind(float64(0)): encFloat64,
|
reflect.Float64: encFloat64,
|
||||||
valueKind("x"): encString,
|
reflect.String: encString,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the encoding op for the base type under rt and
|
// Return the encoding op for the base type under rt and
|
||||||
// the indirection count to reach it.
|
// the indirection count to reach it.
|
||||||
func encOpFor(rt reflect.Type) (encOp, int, os.Error) {
|
func encOpFor(rt reflect.Type) (encOp, int, os.Error) {
|
||||||
typ, indir := indirect(rt)
|
typ, indir := indirect(rt)
|
||||||
op, ok := encOpMap[reflect.Typeof(typ)]
|
op, ok := encOpMap[typ.Kind()]
|
||||||
if !ok {
|
if !ok {
|
||||||
// Special cases
|
// Special cases
|
||||||
switch t := typ.(type) {
|
switch t := typ.(type) {
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
if _, ok := t.Elem().(*reflect.Uint8Type); ok {
|
if t.Elem().Kind() == reflect.Uint8 {
|
||||||
op = encUint8Array
|
op = encUint8Array
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ func (enc *Encoder) sendType(origt reflect.Type) {
|
|||||||
return
|
return
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
// If it's []uint8, don't send; it's considered basic.
|
// If it's []uint8, don't send; it's considered basic.
|
||||||
if _, ok := rt.Elem().(*reflect.Uint8Type); ok {
|
if rt.Elem().Kind() == reflect.Uint8 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Otherwise we do send.
|
// Otherwise we do send.
|
||||||
|
@ -11,8 +11,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type kind reflect.Type
|
|
||||||
|
|
||||||
// Reflection types are themselves interface values holding structs
|
// Reflection types are themselves interface values holding structs
|
||||||
// describing the type. Each type has a different struct so that struct can
|
// describing the type. Each type has a different struct so that struct can
|
||||||
// be the kind. For example, if typ is the reflect type for an int8, typ is
|
// be the kind. For example, if typ is the reflect type for an int8, typ is
|
||||||
@ -20,17 +18,6 @@ type kind reflect.Type
|
|||||||
// function, typ is a pointer to a reflect.FuncType struct; we use the type
|
// function, typ is a pointer to a reflect.FuncType struct; we use the type
|
||||||
// of that pointer as the kind.
|
// of that pointer as the kind.
|
||||||
|
|
||||||
// typeKind returns a reflect.Type representing typ's kind. The kind is the
|
|
||||||
// general kind of type:
|
|
||||||
// int8, int16, int, uint, float, func, chan, struct, and so on.
|
|
||||||
// That is, all struct types have the same kind, all func types have the same
|
|
||||||
// kind, all int8 types have the same kind, and so on.
|
|
||||||
func typeKind(typ reflect.Type) kind { return kind(reflect.Typeof(typ)) }
|
|
||||||
|
|
||||||
// valueKind returns the kind of the value type
|
|
||||||
// stored inside the interface v.
|
|
||||||
func valueKind(v interface{}) reflect.Type { return typeKind(reflect.Typeof(v)) }
|
|
||||||
|
|
||||||
// A typeId represents a gob Type as an integer that can be passed on the wire.
|
// A typeId represents a gob Type as an integer that can be passed on the wire.
|
||||||
// Internally, typeIds are used as keys to a map to recover the underlying type info.
|
// Internally, typeIds are used as keys to a map to recover the underlying type info.
|
||||||
type typeId int32
|
type typeId int32
|
||||||
@ -245,13 +232,13 @@ func newTypeObject(name string, rt reflect.Type) (gobType, os.Error) {
|
|||||||
case *reflect.BoolType:
|
case *reflect.BoolType:
|
||||||
return tBool.gobType(), nil
|
return tBool.gobType(), nil
|
||||||
|
|
||||||
case *reflect.IntType, *reflect.Int8Type, *reflect.Int16Type, *reflect.Int32Type, *reflect.Int64Type:
|
case *reflect.IntType:
|
||||||
return tInt.gobType(), nil
|
return tInt.gobType(), nil
|
||||||
|
|
||||||
case *reflect.UintType, *reflect.Uint8Type, *reflect.Uint16Type, *reflect.Uint32Type, *reflect.Uint64Type, *reflect.UintptrType:
|
case *reflect.UintType:
|
||||||
return tUint.gobType(), nil
|
return tUint.gobType(), nil
|
||||||
|
|
||||||
case *reflect.FloatType, *reflect.Float32Type, *reflect.Float64Type:
|
case *reflect.FloatType:
|
||||||
return tFloat.gobType(), nil
|
return tFloat.gobType(), nil
|
||||||
|
|
||||||
case *reflect.StringType:
|
case *reflect.StringType:
|
||||||
@ -277,7 +264,7 @@ func newTypeObject(name string, rt reflect.Type) (gobType, os.Error) {
|
|||||||
|
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
// []byte == []uint8 is a special case
|
// []byte == []uint8 is a special case
|
||||||
if _, ok := t.Elem().(*reflect.Uint8Type); ok {
|
if t.Elem().Kind() == reflect.Uint8 {
|
||||||
return tBytes.gobType(), nil
|
return tBytes.gobType(), nil
|
||||||
}
|
}
|
||||||
gt, err := getType(t.Elem().Name(), t.Elem())
|
gt, err := getType(t.Elem().Name(), t.Elem())
|
||||||
@ -413,7 +400,7 @@ func getTypeInfo(rt reflect.Type) (*typeInfo, os.Error) {
|
|||||||
info.wire = &wireType{mapT: t.(*mapType)}
|
info.wire = &wireType{mapT: t.(*mapType)}
|
||||||
case *reflect.SliceType:
|
case *reflect.SliceType:
|
||||||
// []byte == []uint8 is a special case handled separately
|
// []byte == []uint8 is a special case handled separately
|
||||||
if _, ok := typ.Elem().(*reflect.Uint8Type); !ok {
|
if typ.Elem().Kind() != reflect.Uint8 {
|
||||||
info.wire = &wireType{sliceT: t.(*sliceType)}
|
info.wire = &wireType{sliceT: t.(*sliceType)}
|
||||||
}
|
}
|
||||||
case *reflect.StructType:
|
case *reflect.StructType:
|
||||||
|
@ -572,101 +572,24 @@ func (d *decodeState) literal(v reflect.Value) {
|
|||||||
v.Set(reflect.NewValue(n))
|
v.Set(reflect.NewValue(n))
|
||||||
|
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
n, err := strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(n)
|
|
||||||
case *reflect.Int8Value:
|
|
||||||
n, err := strconv.Atoi(s)
|
|
||||||
if err != nil || int(int8(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(int8(n))
|
|
||||||
case *reflect.Int16Value:
|
|
||||||
n, err := strconv.Atoi(s)
|
|
||||||
if err != nil || int(int16(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(int16(n))
|
|
||||||
case *reflect.Int32Value:
|
|
||||||
n, err := strconv.Atoi(s)
|
|
||||||
if err != nil || int(int32(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(int32(n))
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
n, err := strconv.Atoi64(s)
|
n, err := strconv.Atoi64(s)
|
||||||
if err != nil {
|
if err != nil || v.Overflow(n) {
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
v.Set(n)
|
v.Set(n)
|
||||||
|
|
||||||
case *reflect.UintValue:
|
case *reflect.UintValue:
|
||||||
n, err := strconv.Atoui(s)
|
n, err := strconv.Atoui64(s)
|
||||||
if err != nil {
|
if err != nil || v.Overflow(n) {
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
v.Set(n)
|
v.Set(n)
|
||||||
case *reflect.Uint8Value:
|
|
||||||
n, err := strconv.Atoui(s)
|
|
||||||
if err != nil || uint(uint8(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(uint8(n))
|
|
||||||
case *reflect.Uint16Value:
|
|
||||||
n, err := strconv.Atoui(s)
|
|
||||||
if err != nil || uint(uint16(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(uint16(n))
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
n, err := strconv.Atoui(s)
|
|
||||||
if err != nil || uint(uint32(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(uint32(n))
|
|
||||||
case *reflect.Uint64Value:
|
|
||||||
n, err := strconv.Atoui64(s)
|
|
||||||
if err != nil {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(n)
|
|
||||||
case *reflect.UintptrValue:
|
|
||||||
n, err := strconv.Atoui64(s)
|
|
||||||
if err != nil || uint64(uintptr(n)) != n {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(uintptr(n))
|
|
||||||
|
|
||||||
case *reflect.FloatValue:
|
case *reflect.FloatValue:
|
||||||
n, err := strconv.Atof(s)
|
n, err := strconv.AtofN(s, int(v.Type().Size()*8))
|
||||||
if err != nil {
|
if err != nil || v.Overflow(n) {
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(n)
|
|
||||||
case *reflect.Float32Value:
|
|
||||||
n, err := strconv.Atof32(s)
|
|
||||||
if err != nil {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
v.Set(n)
|
|
||||||
case *reflect.Float64Value:
|
|
||||||
n, err := strconv.Atof64(s)
|
|
||||||
if err != nil {
|
|
||||||
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -156,35 +156,13 @@ func (e *encodeState) reflectValue(v reflect.Value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
e.WriteString(strconv.Itoa(v.Get()))
|
|
||||||
case *reflect.Int8Value:
|
|
||||||
e.WriteString(strconv.Itoa(int(v.Get())))
|
|
||||||
case *reflect.Int16Value:
|
|
||||||
e.WriteString(strconv.Itoa(int(v.Get())))
|
|
||||||
case *reflect.Int32Value:
|
|
||||||
e.WriteString(strconv.Itoa(int(v.Get())))
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
e.WriteString(strconv.Itoa64(v.Get()))
|
e.WriteString(strconv.Itoa64(v.Get()))
|
||||||
|
|
||||||
case *reflect.UintValue:
|
case *reflect.UintValue:
|
||||||
e.WriteString(strconv.Uitoa(v.Get()))
|
|
||||||
case *reflect.Uint8Value:
|
|
||||||
e.WriteString(strconv.Uitoa(uint(v.Get())))
|
|
||||||
case *reflect.Uint16Value:
|
|
||||||
e.WriteString(strconv.Uitoa(uint(v.Get())))
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
e.WriteString(strconv.Uitoa(uint(v.Get())))
|
|
||||||
case *reflect.Uint64Value:
|
|
||||||
e.WriteString(strconv.Uitoa64(v.Get()))
|
e.WriteString(strconv.Uitoa64(v.Get()))
|
||||||
case *reflect.UintptrValue:
|
|
||||||
e.WriteString(strconv.Uitoa64(uint64(v.Get())))
|
|
||||||
|
|
||||||
case *reflect.FloatValue:
|
case *reflect.FloatValue:
|
||||||
e.WriteString(strconv.Ftoa(v.Get(), 'g', -1))
|
e.WriteString(strconv.FtoaN(v.Get(), 'g', -1, int(v.Type().Size()*8)))
|
||||||
case *reflect.Float32Value:
|
|
||||||
e.WriteString(strconv.Ftoa32(v.Get(), 'g', -1))
|
|
||||||
case *reflect.Float64Value:
|
|
||||||
e.WriteString(strconv.Ftoa64(v.Get(), 'g', -1))
|
|
||||||
|
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
e.string(v.Get())
|
e.string(v.Get())
|
||||||
|
@ -369,28 +369,33 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
|
|||||||
f := val.Type().(*reflect.StructType).Field(i)
|
f := val.Type().(*reflect.StructType).Field(i)
|
||||||
switch fv := val.Field(i).(type) {
|
switch fv := val.Field(i).(type) {
|
||||||
default:
|
default:
|
||||||
|
BadType:
|
||||||
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
|
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
|
||||||
return len(msg), false
|
return len(msg), false
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
off, ok = packStructValue(fv, msg, off)
|
off, ok = packStructValue(fv, msg, off)
|
||||||
case *reflect.Uint16Value:
|
case *reflect.UintValue:
|
||||||
i := fv.Get()
|
i := fv.Get()
|
||||||
if off+2 > len(msg) {
|
switch fv.Type().Kind() {
|
||||||
return len(msg), false
|
default:
|
||||||
|
goto BadType
|
||||||
|
case reflect.Uint16:
|
||||||
|
if off+2 > len(msg) {
|
||||||
|
return len(msg), false
|
||||||
|
}
|
||||||
|
msg[off] = byte(i >> 8)
|
||||||
|
msg[off+1] = byte(i)
|
||||||
|
off += 2
|
||||||
|
case reflect.Uint32:
|
||||||
|
if off+4 > len(msg) {
|
||||||
|
return len(msg), false
|
||||||
|
}
|
||||||
|
msg[off] = byte(i >> 24)
|
||||||
|
msg[off+1] = byte(i >> 16)
|
||||||
|
msg[off+2] = byte(i >> 8)
|
||||||
|
msg[off+3] = byte(i)
|
||||||
|
off += 4
|
||||||
}
|
}
|
||||||
msg[off] = byte(i >> 8)
|
|
||||||
msg[off+1] = byte(i)
|
|
||||||
off += 2
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
i := fv.Get()
|
|
||||||
if off+4 > len(msg) {
|
|
||||||
return len(msg), false
|
|
||||||
}
|
|
||||||
msg[off] = byte(i >> 24)
|
|
||||||
msg[off+1] = byte(i >> 16)
|
|
||||||
msg[off+2] = byte(i >> 8)
|
|
||||||
msg[off+3] = byte(i)
|
|
||||||
off += 4
|
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
// There are multiple string encodings.
|
// There are multiple string encodings.
|
||||||
// The tag distinguishes ordinary strings from domain names.
|
// The tag distinguishes ordinary strings from domain names.
|
||||||
@ -438,24 +443,30 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
|
|||||||
f := val.Type().(*reflect.StructType).Field(i)
|
f := val.Type().(*reflect.StructType).Field(i)
|
||||||
switch fv := val.Field(i).(type) {
|
switch fv := val.Field(i).(type) {
|
||||||
default:
|
default:
|
||||||
|
BadType:
|
||||||
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
|
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
|
||||||
return len(msg), false
|
return len(msg), false
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
off, ok = unpackStructValue(fv, msg, off)
|
off, ok = unpackStructValue(fv, msg, off)
|
||||||
case *reflect.Uint16Value:
|
case *reflect.UintValue:
|
||||||
if off+2 > len(msg) {
|
switch fv.Type().Kind() {
|
||||||
return len(msg), false
|
default:
|
||||||
|
goto BadType
|
||||||
|
case reflect.Uint16:
|
||||||
|
if off+2 > len(msg) {
|
||||||
|
return len(msg), false
|
||||||
|
}
|
||||||
|
i := uint16(msg[off])<<8 | uint16(msg[off+1])
|
||||||
|
fv.Set(uint64(i))
|
||||||
|
off += 2
|
||||||
|
case reflect.Uint32:
|
||||||
|
if off+4 > len(msg) {
|
||||||
|
return len(msg), false
|
||||||
|
}
|
||||||
|
i := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
|
||||||
|
fv.Set(uint64(i))
|
||||||
|
off += 4
|
||||||
}
|
}
|
||||||
i := uint16(msg[off])<<8 | uint16(msg[off+1])
|
|
||||||
fv.Set(i)
|
|
||||||
off += 2
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
if off+4 > len(msg) {
|
|
||||||
return len(msg), false
|
|
||||||
}
|
|
||||||
i := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
|
|
||||||
fv.Set(i)
|
|
||||||
off += 4
|
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
var s string
|
var s string
|
||||||
switch f.Tag {
|
switch f.Tag {
|
||||||
@ -508,7 +519,7 @@ func printStructValue(val *reflect.StructValue) string {
|
|||||||
fval := val.Field(i)
|
fval := val.Field(i)
|
||||||
if fv, ok := fval.(*reflect.StructValue); ok {
|
if fv, ok := fval.(*reflect.StructValue); ok {
|
||||||
s += printStructValue(fv)
|
s += printStructValue(fv)
|
||||||
} else if fv, ok := fval.(*reflect.Uint32Value); ok && f.Tag == "ipv4" {
|
} else if fv, ok := fval.(*reflect.UintValue); ok && f.Tag == "ipv4" {
|
||||||
i := fv.Get()
|
i := fv.Get()
|
||||||
s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)).String()
|
s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)).String()
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,8 +164,8 @@ var valueTests = []pair{
|
|||||||
pair{(uint16)(0), "16"},
|
pair{(uint16)(0), "16"},
|
||||||
pair{(uint32)(0), "32"},
|
pair{(uint32)(0), "32"},
|
||||||
pair{(uint64)(0), "64"},
|
pair{(uint64)(0), "64"},
|
||||||
pair{(float32)(0), "32.1"},
|
pair{(float32)(0), "256.25"},
|
||||||
pair{(float64)(0), "64.2"},
|
pair{(float64)(0), "512.125"},
|
||||||
pair{(string)(""), "stringy cheese"},
|
pair{(string)(""), "stringy cheese"},
|
||||||
pair{(bool)(false), "true"},
|
pair{(bool)(false), "true"},
|
||||||
pair{(*int8)(nil), "*int8(0)"},
|
pair{(*int8)(nil), "*int8(0)"},
|
||||||
@ -219,31 +219,49 @@ func TestSet(t *testing.T) {
|
|||||||
v := NewValue(tt.i)
|
v := NewValue(tt.i)
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case *IntValue:
|
case *IntValue:
|
||||||
v.Set(132)
|
switch v.Type().Kind() {
|
||||||
case *Int8Value:
|
case Int:
|
||||||
v.Set(8)
|
v.Set(132)
|
||||||
case *Int16Value:
|
case Int8:
|
||||||
v.Set(16)
|
v.Set(8)
|
||||||
case *Int32Value:
|
case Int16:
|
||||||
v.Set(32)
|
v.Set(16)
|
||||||
case *Int64Value:
|
case Int32:
|
||||||
v.Set(64)
|
v.Set(32)
|
||||||
|
case Int64:
|
||||||
|
v.Set(64)
|
||||||
|
}
|
||||||
case *UintValue:
|
case *UintValue:
|
||||||
v.Set(132)
|
switch v.Type().Kind() {
|
||||||
case *Uint8Value:
|
case Uint:
|
||||||
v.Set(8)
|
v.Set(132)
|
||||||
case *Uint16Value:
|
case Uint8:
|
||||||
v.Set(16)
|
v.Set(8)
|
||||||
case *Uint32Value:
|
case Uint16:
|
||||||
v.Set(32)
|
v.Set(16)
|
||||||
case *Uint64Value:
|
case Uint32:
|
||||||
v.Set(64)
|
v.Set(32)
|
||||||
|
case Uint64:
|
||||||
|
v.Set(64)
|
||||||
|
}
|
||||||
case *FloatValue:
|
case *FloatValue:
|
||||||
v.Set(3200.0)
|
switch v.Type().Kind() {
|
||||||
case *Float32Value:
|
case Float:
|
||||||
v.Set(32.1)
|
v.Set(128.5)
|
||||||
case *Float64Value:
|
case Float32:
|
||||||
v.Set(64.2)
|
v.Set(256.25)
|
||||||
|
case Float64:
|
||||||
|
v.Set(512.125)
|
||||||
|
}
|
||||||
|
case *ComplexValue:
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case Complex:
|
||||||
|
v.Set(53200.0 + 100i)
|
||||||
|
case Complex64:
|
||||||
|
v.Set(532.125 + 10i)
|
||||||
|
case Complex128:
|
||||||
|
v.Set(564.25 + 1i)
|
||||||
|
}
|
||||||
case *StringValue:
|
case *StringValue:
|
||||||
v.Set("stringy cheese")
|
v.Set("stringy cheese")
|
||||||
case *BoolValue:
|
case *BoolValue:
|
||||||
@ -261,31 +279,50 @@ func TestSetValue(t *testing.T) {
|
|||||||
v := NewValue(tt.i)
|
v := NewValue(tt.i)
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case *IntValue:
|
case *IntValue:
|
||||||
v.SetValue(NewValue(int(132)))
|
switch v.Type().Kind() {
|
||||||
case *Int8Value:
|
case Int:
|
||||||
v.SetValue(NewValue(int8(8)))
|
v.SetValue(NewValue(int(132)))
|
||||||
case *Int16Value:
|
case Int8:
|
||||||
v.SetValue(NewValue(int16(16)))
|
v.SetValue(NewValue(int8(8)))
|
||||||
case *Int32Value:
|
case Int16:
|
||||||
v.SetValue(NewValue(int32(32)))
|
v.SetValue(NewValue(int16(16)))
|
||||||
case *Int64Value:
|
case Int32:
|
||||||
v.SetValue(NewValue(int64(64)))
|
v.SetValue(NewValue(int32(32)))
|
||||||
|
case Int64:
|
||||||
|
v.SetValue(NewValue(int64(64)))
|
||||||
|
}
|
||||||
case *UintValue:
|
case *UintValue:
|
||||||
v.SetValue(NewValue(uint(132)))
|
switch v.Type().Kind() {
|
||||||
case *Uint8Value:
|
case Uint:
|
||||||
v.SetValue(NewValue(uint8(8)))
|
v.SetValue(NewValue(uint(132)))
|
||||||
case *Uint16Value:
|
case Uint8:
|
||||||
v.SetValue(NewValue(uint16(16)))
|
v.SetValue(NewValue(uint8(8)))
|
||||||
case *Uint32Value:
|
case Uint16:
|
||||||
v.SetValue(NewValue(uint32(32)))
|
v.SetValue(NewValue(uint16(16)))
|
||||||
case *Uint64Value:
|
case Uint32:
|
||||||
v.SetValue(NewValue(uint64(64)))
|
v.SetValue(NewValue(uint32(32)))
|
||||||
|
case Uint64:
|
||||||
|
v.SetValue(NewValue(uint64(64)))
|
||||||
|
}
|
||||||
case *FloatValue:
|
case *FloatValue:
|
||||||
v.SetValue(NewValue(float(3200.0)))
|
switch v.Type().Kind() {
|
||||||
case *Float32Value:
|
case Float:
|
||||||
v.SetValue(NewValue(float32(32.1)))
|
v.SetValue(NewValue(float(128.5)))
|
||||||
case *Float64Value:
|
case Float32:
|
||||||
v.SetValue(NewValue(float64(64.2)))
|
v.SetValue(NewValue(float32(256.25)))
|
||||||
|
case Float64:
|
||||||
|
v.SetValue(NewValue(float64(512.125)))
|
||||||
|
}
|
||||||
|
case *ComplexValue:
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case Complex:
|
||||||
|
v.SetValue(NewValue(complex(53200.0 + 100i)))
|
||||||
|
case Complex64:
|
||||||
|
v.SetValue(NewValue(complex64(532.125 + 10i)))
|
||||||
|
case Complex128:
|
||||||
|
v.SetValue(NewValue(complex128(564.25 + 1i)))
|
||||||
|
}
|
||||||
|
|
||||||
case *StringValue:
|
case *StringValue:
|
||||||
v.SetValue(NewValue("stringy cheese"))
|
v.SetValue(NewValue("stringy cheese"))
|
||||||
case *BoolValue:
|
case *BoolValue:
|
||||||
@ -302,7 +339,7 @@ var _i = 7
|
|||||||
|
|
||||||
var valueToStringTests = []pair{
|
var valueToStringTests = []pair{
|
||||||
pair{123, "123"},
|
pair{123, "123"},
|
||||||
pair{123.4, "123.4"},
|
pair{123.5, "123.5"},
|
||||||
pair{byte(123), "123"},
|
pair{byte(123), "123"},
|
||||||
pair{"abc", "abc"},
|
pair{"abc", "abc"},
|
||||||
pair{T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
|
pair{T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
|
||||||
@ -606,9 +643,9 @@ func TestDeepEqualRecursiveStruct(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Complex struct {
|
type _Complex struct {
|
||||||
a int
|
a int
|
||||||
b [3]*Complex
|
b [3]*_Complex
|
||||||
c *string
|
c *string
|
||||||
d map[float]float
|
d map[float]float
|
||||||
}
|
}
|
||||||
@ -616,9 +653,9 @@ type Complex struct {
|
|||||||
func TestDeepEqualComplexStruct(t *testing.T) {
|
func TestDeepEqualComplexStruct(t *testing.T) {
|
||||||
m := make(map[float]float)
|
m := make(map[float]float)
|
||||||
stra, strb := "hello", "hello"
|
stra, strb := "hello", "hello"
|
||||||
a, b := new(Complex), new(Complex)
|
a, b := new(_Complex), new(_Complex)
|
||||||
*a = Complex{5, [3]*Complex{a, b, a}, &stra, m}
|
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
|
||||||
*b = Complex{5, [3]*Complex{b, a, a}, &strb, m}
|
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
|
||||||
if !DeepEqual(a, b) {
|
if !DeepEqual(a, b) {
|
||||||
t.Error("DeepEqual(complex same) = false, want true")
|
t.Error("DeepEqual(complex same) = false, want true")
|
||||||
}
|
}
|
||||||
@ -627,9 +664,9 @@ func TestDeepEqualComplexStruct(t *testing.T) {
|
|||||||
func TestDeepEqualComplexStructInequality(t *testing.T) {
|
func TestDeepEqualComplexStructInequality(t *testing.T) {
|
||||||
m := make(map[float]float)
|
m := make(map[float]float)
|
||||||
stra, strb := "hello", "helloo" // Difference is here
|
stra, strb := "hello", "helloo" // Difference is here
|
||||||
a, b := new(Complex), new(Complex)
|
a, b := new(_Complex), new(_Complex)
|
||||||
*a = Complex{5, [3]*Complex{a, b, a}, &stra, m}
|
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
|
||||||
*b = Complex{5, [3]*Complex{b, a, a}, &strb, m}
|
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
|
||||||
if DeepEqual(a, b) {
|
if DeepEqual(a, b) {
|
||||||
t.Error("DeepEqual(complex different) = true, want false")
|
t.Error("DeepEqual(complex different) = true, want false")
|
||||||
}
|
}
|
||||||
@ -830,7 +867,7 @@ func TestMap(t *testing.T) {
|
|||||||
|
|
||||||
// Check that value lookup is correct.
|
// Check that value lookup is correct.
|
||||||
vv := mv.Elem(NewValue(k))
|
vv := mv.Elem(NewValue(k))
|
||||||
if vi := vv.(*IntValue).Get(); vi != v {
|
if vi := vv.(*IntValue).Get(); vi != int64(v) {
|
||||||
t.Errorf("Key %q: have value %d, want %d", vi, v)
|
t.Errorf("Key %q: have value %d, want %d", vi, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -982,9 +1019,9 @@ func TestFunc(t *testing.T) {
|
|||||||
t.Fatalf("Call returned %d values, want 3", len(ret))
|
t.Fatalf("Call returned %d values, want 3", len(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
i := ret[0].(*Uint8Value).Get()
|
i := ret[0].(*UintValue).Get()
|
||||||
j := ret[1].(*IntValue).Get()
|
j := ret[1].(*IntValue).Get()
|
||||||
k := ret[2].(*Uint8Value).Get()
|
k := ret[2].(*UintValue).Get()
|
||||||
if i != 10 || j != 20 || k != 30 {
|
if i != 10 || j != 20 || k != 30 {
|
||||||
t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
|
t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
|
||||||
}
|
}
|
||||||
|
@ -23,35 +23,14 @@ func valueToString(val Value) string {
|
|||||||
typ := val.Type()
|
typ := val.Type()
|
||||||
switch val := val.(type) {
|
switch val := val.(type) {
|
||||||
case *IntValue:
|
case *IntValue:
|
||||||
return strconv.Uitoa64(uint64(val.Get()))
|
return strconv.Itoa64(val.Get())
|
||||||
case *Int8Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Int16Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Int32Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Int64Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *UintValue:
|
case *UintValue:
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
return strconv.Uitoa64(val.Get())
|
||||||
case *Uint8Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Uint16Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Uint32Value:
|
|
||||||
return strconv.Itoa64(int64(val.Get()))
|
|
||||||
case *Uint64Value:
|
|
||||||
return strconv.Uitoa64(uint64(val.Get()))
|
|
||||||
case *FloatValue:
|
case *FloatValue:
|
||||||
if strconv.FloatSize == 32 {
|
return strconv.Ftoa64(float64(val.Get()), 'g', -1)
|
||||||
return strconv.Ftoa32(float32(val.Get()), 'g', -1)
|
case *ComplexValue:
|
||||||
} else {
|
c := val.Get()
|
||||||
return strconv.Ftoa64(float64(val.Get()), 'g', -1)
|
return strconv.Ftoa64(float64(real(c)), 'g', -1) + "+" + strconv.Ftoa64(float64(imag(c)), 'g', -1) + "i"
|
||||||
}
|
|
||||||
case *Float32Value:
|
|
||||||
return strconv.Ftoa32(val.Get(), 'g', -1)
|
|
||||||
case *Float64Value:
|
|
||||||
return strconv.Ftoa64(val.Get(), 'g', -1)
|
|
||||||
case *StringValue:
|
case *StringValue:
|
||||||
return val.Get()
|
return val.Get()
|
||||||
case *BoolValue:
|
case *BoolValue:
|
||||||
|
@ -40,6 +40,7 @@ type commonType struct {
|
|||||||
alg uint8
|
alg uint8
|
||||||
align uint8
|
align uint8
|
||||||
fieldAlign uint8
|
fieldAlign uint8
|
||||||
|
kind uint8
|
||||||
string *string
|
string *string
|
||||||
*uncommonType
|
*uncommonType
|
||||||
}
|
}
|
||||||
@ -64,81 +65,21 @@ type BoolType struct {
|
|||||||
commonType
|
commonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float32Type represents a float32 type.
|
|
||||||
type Float32Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Float64Type represents a float64 type.
|
|
||||||
type Float64Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// FloatType represents a float type.
|
// FloatType represents a float type.
|
||||||
type FloatType struct {
|
type FloatType struct {
|
||||||
commonType
|
commonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex64Type represents a complex64 type.
|
|
||||||
type Complex64Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complex128Type represents a complex128 type.
|
|
||||||
type Complex128Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// ComplexType represents a complex type.
|
// ComplexType represents a complex type.
|
||||||
type ComplexType struct {
|
type ComplexType struct {
|
||||||
commonType
|
commonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int16Type represents an int16 type.
|
// IntType represents a signed integer type.
|
||||||
type Int16Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Int32Type represents an int32 type.
|
|
||||||
type Int32Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Int64Type represents an int64 type.
|
|
||||||
type Int64Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Int8Type represents an int8 type.
|
|
||||||
type Int8Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// IntType represents an int type.
|
|
||||||
type IntType struct {
|
type IntType struct {
|
||||||
commonType
|
commonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint16Type represents a uint16 type.
|
|
||||||
type Uint16Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint32Type represents a uint32 type.
|
|
||||||
type Uint32Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint64Type represents a uint64 type.
|
|
||||||
type Uint64Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint8Type represents a uint8 type.
|
|
||||||
type Uint8Type struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// UintType represents a uint type.
|
// UintType represents a uint type.
|
||||||
type UintType struct {
|
type UintType struct {
|
||||||
commonType
|
commonType
|
||||||
@ -149,11 +90,6 @@ type StringType struct {
|
|||||||
commonType
|
commonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// UintptrType represents a uintptr type.
|
|
||||||
type UintptrType struct {
|
|
||||||
commonType
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnsafePointerType represents an unsafe.Pointer type.
|
// UnsafePointerType represents an unsafe.Pointer type.
|
||||||
type UnsafePointerType struct {
|
type UnsafePointerType struct {
|
||||||
commonType
|
commonType
|
||||||
@ -286,6 +222,9 @@ type Type interface {
|
|||||||
// when used as a field in a struct.
|
// when used as a field in a struct.
|
||||||
FieldAlign() int
|
FieldAlign() int
|
||||||
|
|
||||||
|
// Kind returns the specific kind of this type.
|
||||||
|
Kind() Kind
|
||||||
|
|
||||||
// For non-interface types, Method returns the i'th method with receiver T.
|
// For non-interface types, Method returns the i'th method with receiver T.
|
||||||
// For interface types, Method returns the i'th method in the interface.
|
// For interface types, Method returns the i'th method in the interface.
|
||||||
// NumMethod returns the number of such methods.
|
// NumMethod returns the number of such methods.
|
||||||
@ -294,6 +233,84 @@ type Type interface {
|
|||||||
uncommon() *uncommonType
|
uncommon() *uncommonType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Kind represents the specific kind of type that a Type represents.
|
||||||
|
// For numeric types, the Kind gives more information than the Type's
|
||||||
|
// dynamic type. For example, the Type of a float32 is FloatType, but
|
||||||
|
// the Kind is Float32.
|
||||||
|
//
|
||||||
|
// The zero Kind is not a valid kind.
|
||||||
|
type Kind uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
Bool Kind = 1 + iota
|
||||||
|
Int
|
||||||
|
Int8
|
||||||
|
Int16
|
||||||
|
Int32
|
||||||
|
Int64
|
||||||
|
Uint
|
||||||
|
Uint8
|
||||||
|
Uint16
|
||||||
|
Uint32
|
||||||
|
Uint64
|
||||||
|
Uintptr
|
||||||
|
Float
|
||||||
|
Float32
|
||||||
|
Float64
|
||||||
|
Complex
|
||||||
|
Complex64
|
||||||
|
Complex128
|
||||||
|
Array
|
||||||
|
Chan
|
||||||
|
Func
|
||||||
|
Interface
|
||||||
|
Map
|
||||||
|
Ptr
|
||||||
|
Slice
|
||||||
|
String
|
||||||
|
Struct
|
||||||
|
UnsafePointer
|
||||||
|
)
|
||||||
|
|
||||||
|
// High bit says whether type has
|
||||||
|
// embedded pointers,to help garbage collector.
|
||||||
|
const kindMask = 0x7f
|
||||||
|
|
||||||
|
func (k Kind) String() string {
|
||||||
|
if int(k) < len(kindNames) {
|
||||||
|
return kindNames[k]
|
||||||
|
}
|
||||||
|
return "kind" + strconv.Itoa(int(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
var kindNames = []string{
|
||||||
|
Bool: "bool",
|
||||||
|
Int: "int",
|
||||||
|
Int8: "int8",
|
||||||
|
Int16: "int16",
|
||||||
|
Int32: "int32",
|
||||||
|
Int64: "int64",
|
||||||
|
Uint: "uint",
|
||||||
|
Uint8: "uint8",
|
||||||
|
Uint16: "uint16",
|
||||||
|
Uint32: "uint32",
|
||||||
|
Uint64: "uint64",
|
||||||
|
Uintptr: "uintptr",
|
||||||
|
Float: "float",
|
||||||
|
Float32: "float32",
|
||||||
|
Float64: "float64",
|
||||||
|
Array: "array",
|
||||||
|
Chan: "chan",
|
||||||
|
Func: "func",
|
||||||
|
Interface: "interface",
|
||||||
|
Map: "map",
|
||||||
|
Ptr: "ptr",
|
||||||
|
Slice: "slice",
|
||||||
|
String: "string",
|
||||||
|
Struct: "struct",
|
||||||
|
UnsafePointer: "unsafe.Pointer",
|
||||||
|
}
|
||||||
|
|
||||||
func (t *uncommonType) uncommon() *uncommonType {
|
func (t *uncommonType) uncommon() *uncommonType {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
@ -320,6 +337,8 @@ func (t *commonType) Align() int { return int(t.align) }
|
|||||||
|
|
||||||
func (t *commonType) FieldAlign() int { return int(t.fieldAlign) }
|
func (t *commonType) FieldAlign() int { return int(t.fieldAlign) }
|
||||||
|
|
||||||
|
func (t *commonType) Kind() Kind { return Kind(t.kind & kindMask) }
|
||||||
|
|
||||||
func (t *uncommonType) Method(i int) (m Method) {
|
func (t *uncommonType) Method(i int) (m Method) {
|
||||||
if t == nil || i < 0 || i >= len(t.methods) {
|
if t == nil || i < 0 || i >= len(t.methods) {
|
||||||
return
|
return
|
||||||
@ -603,40 +622,14 @@ func toType(i interface{}) Type {
|
|||||||
return (*BoolType)(unsafe.Pointer(v))
|
return (*BoolType)(unsafe.Pointer(v))
|
||||||
case *runtime.FloatType:
|
case *runtime.FloatType:
|
||||||
return (*FloatType)(unsafe.Pointer(v))
|
return (*FloatType)(unsafe.Pointer(v))
|
||||||
case *runtime.Float32Type:
|
|
||||||
return (*Float32Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Float64Type:
|
|
||||||
return (*Float64Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.ComplexType:
|
case *runtime.ComplexType:
|
||||||
return (*ComplexType)(unsafe.Pointer(v))
|
return (*ComplexType)(unsafe.Pointer(v))
|
||||||
case *runtime.Complex64Type:
|
|
||||||
return (*Complex64Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Complex128Type:
|
|
||||||
return (*Complex128Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.IntType:
|
case *runtime.IntType:
|
||||||
return (*IntType)(unsafe.Pointer(v))
|
return (*IntType)(unsafe.Pointer(v))
|
||||||
case *runtime.Int8Type:
|
|
||||||
return (*Int8Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Int16Type:
|
|
||||||
return (*Int16Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Int32Type:
|
|
||||||
return (*Int32Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Int64Type:
|
|
||||||
return (*Int64Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.StringType:
|
case *runtime.StringType:
|
||||||
return (*StringType)(unsafe.Pointer(v))
|
return (*StringType)(unsafe.Pointer(v))
|
||||||
case *runtime.UintType:
|
case *runtime.UintType:
|
||||||
return (*UintType)(unsafe.Pointer(v))
|
return (*UintType)(unsafe.Pointer(v))
|
||||||
case *runtime.Uint8Type:
|
|
||||||
return (*Uint8Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Uint16Type:
|
|
||||||
return (*Uint16Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Uint32Type:
|
|
||||||
return (*Uint32Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.Uint64Type:
|
|
||||||
return (*Uint64Type)(unsafe.Pointer(v))
|
|
||||||
case *runtime.UintptrType:
|
|
||||||
return (*UintptrType)(unsafe.Pointer(v))
|
|
||||||
case *runtime.UnsafePointerType:
|
case *runtime.UnsafePointerType:
|
||||||
return (*UnsafePointerType)(unsafe.Pointer(v))
|
return (*UnsafePointerType)(unsafe.Pointer(v))
|
||||||
case *runtime.ArrayType:
|
case *runtime.ArrayType:
|
||||||
@ -656,6 +649,7 @@ func toType(i interface{}) Type {
|
|||||||
case *runtime.StructType:
|
case *runtime.StructType:
|
||||||
return (*StructType)(unsafe.Pointer(v))
|
return (*StructType)(unsafe.Pointer(v))
|
||||||
}
|
}
|
||||||
|
println(i)
|
||||||
panic("toType")
|
panic("toType")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package reflect
|
package reflect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"runtime"
|
"runtime"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
@ -134,72 +135,83 @@ type FloatValue struct {
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the underlying float value.
|
// Get returns the underlying int value.
|
||||||
func (v *FloatValue) Get() float { return *(*float)(v.addr) }
|
func (v *FloatValue) Get() float64 {
|
||||||
|
switch v.typ.(*FloatType).Kind() {
|
||||||
|
case Float:
|
||||||
|
return float64(*(*float)(v.addr))
|
||||||
|
case Float32:
|
||||||
|
return float64(*(*float32)(v.addr))
|
||||||
|
case Float64:
|
||||||
|
return *(*float64)(v.addr)
|
||||||
|
}
|
||||||
|
panic("reflect: invalid float kind")
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *FloatValue) Set(x float) {
|
func (v *FloatValue) Set(x float64) {
|
||||||
if !v.canSet {
|
if !v.canSet {
|
||||||
panic(cannotSet)
|
panic(cannotSet)
|
||||||
}
|
}
|
||||||
*(*float)(v.addr) = x
|
switch v.typ.(*FloatType).Kind() {
|
||||||
|
default:
|
||||||
|
panic("reflect: invalid float kind")
|
||||||
|
case Float:
|
||||||
|
*(*float)(v.addr) = float(x)
|
||||||
|
case Float32:
|
||||||
|
*(*float32)(v.addr) = float32(x)
|
||||||
|
case Float64:
|
||||||
|
*(*float64)(v.addr) = x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overflow returns true if x cannot be represented by the type of v.
|
||||||
|
func (v *FloatValue) Overflow(x float64) bool {
|
||||||
|
if v.typ.Size() == 8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if x < 0 {
|
||||||
|
x = -x
|
||||||
|
}
|
||||||
|
return math.MaxFloat32 < x && x <= math.MaxFloat64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
|
func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
|
||||||
|
|
||||||
// Float32Value represents a float32 value.
|
|
||||||
type Float32Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying float32 value.
|
|
||||||
func (v *Float32Value) Get() float32 { return *(*float32)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Float32Value) Set(x float32) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*float32)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Float32Value) SetValue(x Value) { v.Set(x.(*Float32Value).Get()) }
|
|
||||||
|
|
||||||
// Float64Value represents a float64 value.
|
|
||||||
type Float64Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying float64 value.
|
|
||||||
func (v *Float64Value) Get() float64 { return *(*float64)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Float64Value) Set(x float64) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*float64)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Float64Value) SetValue(x Value) { v.Set(x.(*Float64Value).Get()) }
|
|
||||||
|
|
||||||
// ComplexValue represents a complex value.
|
// ComplexValue represents a complex value.
|
||||||
type ComplexValue struct {
|
type ComplexValue struct {
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the underlying complex value.
|
// Get returns the underlying complex value.
|
||||||
func (v *ComplexValue) Get() complex { return *(*complex)(v.addr) }
|
func (v *ComplexValue) Get() complex128 {
|
||||||
|
switch v.typ.(*ComplexType).Kind() {
|
||||||
|
case Complex:
|
||||||
|
return complex128(*(*complex)(v.addr))
|
||||||
|
case Complex64:
|
||||||
|
return complex128(*(*complex64)(v.addr))
|
||||||
|
case Complex128:
|
||||||
|
return *(*complex128)(v.addr)
|
||||||
|
}
|
||||||
|
panic("reflect: invalid complex kind")
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *ComplexValue) Set(x complex) {
|
func (v *ComplexValue) Set(x complex128) {
|
||||||
if !v.canSet {
|
if !v.canSet {
|
||||||
panic(cannotSet)
|
panic(cannotSet)
|
||||||
}
|
}
|
||||||
*(*complex)(v.addr) = x
|
switch v.typ.(*ComplexType).Kind() {
|
||||||
|
default:
|
||||||
|
panic("reflect: invalid complex kind")
|
||||||
|
case Complex:
|
||||||
|
*(*complex)(v.addr) = complex(x)
|
||||||
|
case Complex64:
|
||||||
|
*(*complex64)(v.addr) = complex64(x)
|
||||||
|
case Complex128:
|
||||||
|
*(*complex128)(v.addr) = x
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
@ -249,95 +261,53 @@ type IntValue struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the underlying int value.
|
// Get returns the underlying int value.
|
||||||
func (v *IntValue) Get() int { return *(*int)(v.addr) }
|
func (v *IntValue) Get() int64 {
|
||||||
|
switch v.typ.(*IntType).Kind() {
|
||||||
|
case Int:
|
||||||
|
return int64(*(*int)(v.addr))
|
||||||
|
case Int8:
|
||||||
|
return int64(*(*int8)(v.addr))
|
||||||
|
case Int16:
|
||||||
|
return int64(*(*int16)(v.addr))
|
||||||
|
case Int32:
|
||||||
|
return int64(*(*int32)(v.addr))
|
||||||
|
case Int64:
|
||||||
|
return *(*int64)(v.addr)
|
||||||
|
}
|
||||||
|
panic("reflect: invalid int kind")
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *IntValue) Set(x int) {
|
func (v *IntValue) Set(x int64) {
|
||||||
if !v.canSet {
|
if !v.canSet {
|
||||||
panic(cannotSet)
|
panic(cannotSet)
|
||||||
}
|
}
|
||||||
*(*int)(v.addr) = x
|
switch v.typ.(*IntType).Kind() {
|
||||||
|
default:
|
||||||
|
panic("reflect: invalid int kind")
|
||||||
|
case Int:
|
||||||
|
*(*int)(v.addr) = int(x)
|
||||||
|
case Int8:
|
||||||
|
*(*int8)(v.addr) = int8(x)
|
||||||
|
case Int16:
|
||||||
|
*(*int16)(v.addr) = int16(x)
|
||||||
|
case Int32:
|
||||||
|
*(*int32)(v.addr) = int32(x)
|
||||||
|
case Int64:
|
||||||
|
*(*int64)(v.addr) = x
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) }
|
func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) }
|
||||||
|
|
||||||
// Int8Value represents an int8 value.
|
// Overflow returns true if x cannot be represented by the type of v.
|
||||||
type Int8Value struct {
|
func (v *IntValue) Overflow(x int64) bool {
|
||||||
value
|
bitSize := v.typ.Size() * 8
|
||||||
|
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
|
||||||
|
return x != trunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the underlying int8 value.
|
|
||||||
func (v *Int8Value) Get() int8 { return *(*int8)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int8Value) Set(x int8) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*int8)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int8Value) SetValue(x Value) { v.Set(x.(*Int8Value).Get()) }
|
|
||||||
|
|
||||||
// Int16Value represents an int16 value.
|
|
||||||
type Int16Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying int16 value.
|
|
||||||
func (v *Int16Value) Get() int16 { return *(*int16)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int16Value) Set(x int16) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*int16)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int16Value) SetValue(x Value) { v.Set(x.(*Int16Value).Get()) }
|
|
||||||
|
|
||||||
// Int32Value represents an int32 value.
|
|
||||||
type Int32Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying int32 value.
|
|
||||||
func (v *Int32Value) Get() int32 { return *(*int32)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int32Value) Set(x int32) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*int32)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int32Value) SetValue(x Value) { v.Set(x.(*Int32Value).Get()) }
|
|
||||||
|
|
||||||
// Int64Value represents an int64 value.
|
|
||||||
type Int64Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying int64 value.
|
|
||||||
func (v *Int64Value) Get() int64 { return *(*int64)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int64Value) Set(x int64) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*int64)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Int64Value) SetValue(x Value) { v.Set(x.(*Int64Value).Get()) }
|
|
||||||
|
|
||||||
// StringHeader is the runtime representation of a string.
|
// StringHeader is the runtime representation of a string.
|
||||||
type StringHeader struct {
|
type StringHeader struct {
|
||||||
Data uintptr
|
Data uintptr
|
||||||
@ -368,115 +338,58 @@ type UintValue struct {
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the underlying uint value.
|
// Get returns the underlying uuint value.
|
||||||
func (v *UintValue) Get() uint { return *(*uint)(v.addr) }
|
func (v *UintValue) Get() uint64 {
|
||||||
|
switch v.typ.(*UintType).Kind() {
|
||||||
|
case Uint:
|
||||||
|
return uint64(*(*uint)(v.addr))
|
||||||
|
case Uint8:
|
||||||
|
return uint64(*(*uint8)(v.addr))
|
||||||
|
case Uint16:
|
||||||
|
return uint64(*(*uint16)(v.addr))
|
||||||
|
case Uint32:
|
||||||
|
return uint64(*(*uint32)(v.addr))
|
||||||
|
case Uint64:
|
||||||
|
return *(*uint64)(v.addr)
|
||||||
|
case Uintptr:
|
||||||
|
return uint64(*(*uintptr)(v.addr))
|
||||||
|
}
|
||||||
|
panic("reflect: invalid uint kind")
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *UintValue) Set(x uint) {
|
func (v *UintValue) Set(x uint64) {
|
||||||
if !v.canSet {
|
if !v.canSet {
|
||||||
panic(cannotSet)
|
panic(cannotSet)
|
||||||
}
|
}
|
||||||
*(*uint)(v.addr) = x
|
switch v.typ.(*UintType).Kind() {
|
||||||
|
default:
|
||||||
|
panic("reflect: invalid uint kind")
|
||||||
|
case Uint:
|
||||||
|
*(*uint)(v.addr) = uint(x)
|
||||||
|
case Uint8:
|
||||||
|
*(*uint8)(v.addr) = uint8(x)
|
||||||
|
case Uint16:
|
||||||
|
*(*uint16)(v.addr) = uint16(x)
|
||||||
|
case Uint32:
|
||||||
|
*(*uint32)(v.addr) = uint32(x)
|
||||||
|
case Uint64:
|
||||||
|
*(*uint64)(v.addr) = x
|
||||||
|
case Uintptr:
|
||||||
|
*(*uintptr)(v.addr) = uintptr(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overflow returns true if x cannot be represented by the type of v.
|
||||||
|
func (v *UintValue) Overflow(x uint64) bool {
|
||||||
|
bitSize := v.typ.Size() * 8
|
||||||
|
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
|
||||||
|
return x != trunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets v to the value x.
|
// Set sets v to the value x.
|
||||||
func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) }
|
func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) }
|
||||||
|
|
||||||
// Uint8Value represents a uint8 value.
|
|
||||||
type Uint8Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying uint8 value.
|
|
||||||
func (v *Uint8Value) Get() uint8 { return *(*uint8)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint8Value) Set(x uint8) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*uint8)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint8Value) SetValue(x Value) { v.Set(x.(*Uint8Value).Get()) }
|
|
||||||
|
|
||||||
// Uint16Value represents a uint16 value.
|
|
||||||
type Uint16Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying uint16 value.
|
|
||||||
func (v *Uint16Value) Get() uint16 { return *(*uint16)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint16Value) Set(x uint16) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*uint16)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint16Value) SetValue(x Value) { v.Set(x.(*Uint16Value).Get()) }
|
|
||||||
|
|
||||||
// Uint32Value represents a uint32 value.
|
|
||||||
type Uint32Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying uint32 value.
|
|
||||||
func (v *Uint32Value) Get() uint32 { return *(*uint32)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint32Value) Set(x uint32) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*uint32)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint32Value) SetValue(x Value) { v.Set(x.(*Uint32Value).Get()) }
|
|
||||||
|
|
||||||
// Uint64Value represents a uint64 value.
|
|
||||||
type Uint64Value struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying uint64 value.
|
|
||||||
func (v *Uint64Value) Get() uint64 { return *(*uint64)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint64Value) Set(x uint64) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*uint64)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *Uint64Value) SetValue(x Value) { v.Set(x.(*Uint64Value).Get()) }
|
|
||||||
|
|
||||||
// UintptrValue represents a uintptr value.
|
|
||||||
type UintptrValue struct {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the underlying uintptr value.
|
|
||||||
func (v *UintptrValue) Get() uintptr { return *(*uintptr)(v.addr) }
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *UintptrValue) Set(x uintptr) {
|
|
||||||
if !v.canSet {
|
|
||||||
panic(cannotSet)
|
|
||||||
}
|
|
||||||
*(*uintptr)(v.addr) = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets v to the value x.
|
|
||||||
func (v *UintptrValue) SetValue(x Value) { v.Set(x.(*UintptrValue).Get()) }
|
|
||||||
|
|
||||||
// UnsafePointerValue represents an unsafe.Pointer value.
|
// UnsafePointerValue represents an unsafe.Pointer value.
|
||||||
type UnsafePointerValue struct {
|
type UnsafePointerValue struct {
|
||||||
value
|
value
|
||||||
@ -1329,26 +1242,10 @@ func newValue(typ Type, addr addr, canSet bool) Value {
|
|||||||
return (*ChanValue)(v)
|
return (*ChanValue)(v)
|
||||||
case *FloatType:
|
case *FloatType:
|
||||||
return (*FloatValue)(v)
|
return (*FloatValue)(v)
|
||||||
case *Float32Type:
|
|
||||||
return (*Float32Value)(v)
|
|
||||||
case *Float64Type:
|
|
||||||
return (*Float64Value)(v)
|
|
||||||
case *ComplexType:
|
case *ComplexType:
|
||||||
return (*ComplexValue)(v)
|
return (*ComplexValue)(v)
|
||||||
case *Complex64Type:
|
|
||||||
return (*Complex64Value)(v)
|
|
||||||
case *Complex128Type:
|
|
||||||
return (*Complex128Value)(v)
|
|
||||||
case *IntType:
|
case *IntType:
|
||||||
return (*IntValue)(v)
|
return (*IntValue)(v)
|
||||||
case *Int8Type:
|
|
||||||
return (*Int8Value)(v)
|
|
||||||
case *Int16Type:
|
|
||||||
return (*Int16Value)(v)
|
|
||||||
case *Int32Type:
|
|
||||||
return (*Int32Value)(v)
|
|
||||||
case *Int64Type:
|
|
||||||
return (*Int64Value)(v)
|
|
||||||
case *InterfaceType:
|
case *InterfaceType:
|
||||||
return (*InterfaceValue)(v)
|
return (*InterfaceValue)(v)
|
||||||
case *MapType:
|
case *MapType:
|
||||||
@ -1363,16 +1260,6 @@ func newValue(typ Type, addr addr, canSet bool) Value {
|
|||||||
return (*StructValue)(v)
|
return (*StructValue)(v)
|
||||||
case *UintType:
|
case *UintType:
|
||||||
return (*UintValue)(v)
|
return (*UintValue)(v)
|
||||||
case *Uint8Type:
|
|
||||||
return (*Uint8Value)(v)
|
|
||||||
case *Uint16Type:
|
|
||||||
return (*Uint16Value)(v)
|
|
||||||
case *Uint32Type:
|
|
||||||
return (*Uint32Value)(v)
|
|
||||||
case *Uint64Type:
|
|
||||||
return (*Uint64Value)(v)
|
|
||||||
case *UintptrType:
|
|
||||||
return (*UintptrValue)(v)
|
|
||||||
case *UnsafePointerType:
|
case *UnsafePointerType:
|
||||||
return (*UnsafePointerValue)(v)
|
return (*UnsafePointerValue)(v)
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,9 @@ enum {
|
|||||||
KindFloat,
|
KindFloat,
|
||||||
KindFloat32,
|
KindFloat32,
|
||||||
KindFloat64,
|
KindFloat64,
|
||||||
|
KindComplex,
|
||||||
|
KindComplex64,
|
||||||
|
KindComplex128,
|
||||||
KindArray,
|
KindArray,
|
||||||
KindChan,
|
KindChan,
|
||||||
KindFunc,
|
KindFunc,
|
||||||
|
@ -60,26 +60,41 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
|
|||||||
switch concrete := t.(type) {
|
switch concrete := t.(type) {
|
||||||
case *reflect.BoolType:
|
case *reflect.BoolType:
|
||||||
return reflect.NewValue(rand.Int()&1 == 0), true
|
return reflect.NewValue(rand.Int()&1 == 0), true
|
||||||
case *reflect.Float32Type:
|
case *reflect.FloatType, *reflect.IntType, *reflect.UintType:
|
||||||
return reflect.NewValue(randFloat32(rand)), true
|
switch t.Kind() {
|
||||||
case *reflect.Float64Type:
|
case reflect.Float32:
|
||||||
return reflect.NewValue(randFloat64(rand)), true
|
return reflect.NewValue(randFloat32(rand)), true
|
||||||
case *reflect.FloatType:
|
case reflect.Float64:
|
||||||
if t.Size() == 4 {
|
return reflect.NewValue(randFloat64(rand)), true
|
||||||
return reflect.NewValue(float(randFloat32(rand))), true
|
case reflect.Float:
|
||||||
} else {
|
if t.Size() == 4 {
|
||||||
return reflect.NewValue(float(randFloat64(rand))), true
|
return reflect.NewValue(float(randFloat32(rand))), true
|
||||||
|
} else {
|
||||||
|
return reflect.NewValue(float(randFloat64(rand))), true
|
||||||
|
}
|
||||||
|
case reflect.Int16:
|
||||||
|
return reflect.NewValue(int16(randInt64(rand))), true
|
||||||
|
case reflect.Int32:
|
||||||
|
return reflect.NewValue(int32(randInt64(rand))), true
|
||||||
|
case reflect.Int64:
|
||||||
|
return reflect.NewValue(randInt64(rand)), true
|
||||||
|
case reflect.Int8:
|
||||||
|
return reflect.NewValue(int8(randInt64(rand))), true
|
||||||
|
case reflect.Int:
|
||||||
|
return reflect.NewValue(int(randInt64(rand))), true
|
||||||
|
case reflect.Uint16:
|
||||||
|
return reflect.NewValue(uint16(randInt64(rand))), true
|
||||||
|
case reflect.Uint32:
|
||||||
|
return reflect.NewValue(uint32(randInt64(rand))), true
|
||||||
|
case reflect.Uint64:
|
||||||
|
return reflect.NewValue(uint64(randInt64(rand))), true
|
||||||
|
case reflect.Uint8:
|
||||||
|
return reflect.NewValue(uint8(randInt64(rand))), true
|
||||||
|
case reflect.Uint:
|
||||||
|
return reflect.NewValue(uint(randInt64(rand))), true
|
||||||
|
case reflect.Uintptr:
|
||||||
|
return reflect.NewValue(uintptr(randInt64(rand))), true
|
||||||
}
|
}
|
||||||
case *reflect.Int16Type:
|
|
||||||
return reflect.NewValue(int16(randInt64(rand))), true
|
|
||||||
case *reflect.Int32Type:
|
|
||||||
return reflect.NewValue(int32(randInt64(rand))), true
|
|
||||||
case *reflect.Int64Type:
|
|
||||||
return reflect.NewValue(randInt64(rand)), true
|
|
||||||
case *reflect.Int8Type:
|
|
||||||
return reflect.NewValue(int8(randInt64(rand))), true
|
|
||||||
case *reflect.IntType:
|
|
||||||
return reflect.NewValue(int(randInt64(rand))), true
|
|
||||||
case *reflect.MapType:
|
case *reflect.MapType:
|
||||||
numElems := rand.Intn(complexSize)
|
numElems := rand.Intn(complexSize)
|
||||||
m := reflect.MakeMap(concrete)
|
m := reflect.MakeMap(concrete)
|
||||||
@ -128,18 +143,6 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
|
|||||||
s.Field(i).SetValue(v)
|
s.Field(i).SetValue(v)
|
||||||
}
|
}
|
||||||
return s, true
|
return s, true
|
||||||
case *reflect.Uint16Type:
|
|
||||||
return reflect.NewValue(uint16(randInt64(rand))), true
|
|
||||||
case *reflect.Uint32Type:
|
|
||||||
return reflect.NewValue(uint32(randInt64(rand))), true
|
|
||||||
case *reflect.Uint64Type:
|
|
||||||
return reflect.NewValue(uint64(randInt64(rand))), true
|
|
||||||
case *reflect.Uint8Type:
|
|
||||||
return reflect.NewValue(uint8(randInt64(rand))), true
|
|
||||||
case *reflect.UintType:
|
|
||||||
return reflect.NewValue(uint(randInt64(rand))), true
|
|
||||||
case *reflect.UintptrType:
|
|
||||||
return reflect.NewValue(uintptr(randInt64(rand))), true
|
|
||||||
default:
|
default:
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
@ -218,7 +218,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
|
|||||||
|
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
typ := v.Type().(*reflect.SliceType)
|
typ := v.Type().(*reflect.SliceType)
|
||||||
if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
|
if typ.Elem().Kind() == reflect.Uint8 {
|
||||||
// []byte
|
// []byte
|
||||||
saveData = v
|
saveData = v
|
||||||
break
|
break
|
||||||
@ -245,11 +245,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case *reflect.StringValue,
|
case *reflect.BoolValue, *reflect.FloatValue, *reflect.IntValue, *reflect.UintValue, *reflect.StringValue:
|
||||||
*reflect.IntValue, *reflect.UintValue, *reflect.UintptrValue,
|
|
||||||
*reflect.Int8Value, *reflect.Int16Value, *reflect.Int32Value, *reflect.Int64Value,
|
|
||||||
*reflect.Uint8Value, *reflect.Uint16Value, *reflect.Uint32Value, *reflect.Uint64Value,
|
|
||||||
*reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value, *reflect.BoolValue:
|
|
||||||
saveData = v
|
saveData = v
|
||||||
|
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
@ -431,71 +427,16 @@ Loop:
|
|||||||
default:
|
default:
|
||||||
return os.ErrorString("cannot happen: unknown type " + t.Type().String())
|
return os.ErrorString("cannot happen: unknown type " + t.Type().String())
|
||||||
case *reflect.IntValue:
|
case *reflect.IntValue:
|
||||||
if !getInt64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(int(itmp))
|
|
||||||
case *reflect.Int8Value:
|
|
||||||
if !getInt64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(int8(itmp))
|
|
||||||
case *reflect.Int16Value:
|
|
||||||
if !getInt64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(int16(itmp))
|
|
||||||
case *reflect.Int32Value:
|
|
||||||
if !getInt64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(int32(itmp))
|
|
||||||
case *reflect.Int64Value:
|
|
||||||
if !getInt64() {
|
if !getInt64() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.Set(itmp)
|
t.Set(itmp)
|
||||||
case *reflect.UintValue:
|
case *reflect.UintValue:
|
||||||
if !getUint64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(uint(utmp))
|
|
||||||
case *reflect.Uint8Value:
|
|
||||||
if !getUint64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(uint8(utmp))
|
|
||||||
case *reflect.Uint16Value:
|
|
||||||
if !getUint64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(uint16(utmp))
|
|
||||||
case *reflect.Uint32Value:
|
|
||||||
if !getUint64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(uint32(utmp))
|
|
||||||
case *reflect.Uint64Value:
|
|
||||||
if !getUint64() {
|
if !getUint64() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.Set(utmp)
|
t.Set(utmp)
|
||||||
case *reflect.UintptrValue:
|
|
||||||
if !getUint64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(uintptr(utmp))
|
|
||||||
case *reflect.FloatValue:
|
case *reflect.FloatValue:
|
||||||
if !getFloat64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(float(ftmp))
|
|
||||||
case *reflect.Float32Value:
|
|
||||||
if !getFloat64() {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Set(float32(ftmp))
|
|
||||||
case *reflect.Float64Value:
|
|
||||||
if !getFloat64() {
|
if !getFloat64() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user