美文网首页
3.reflect.ChanDir()

3.reflect.ChanDir()

作者: 浩玥当空 | 来源:发表于2019-06-16 11:26 被阅读0次

    2. 接口方法实现-ChanDir() 方法

    1. 将Type接口的值,即动态类型入栈
    2. 调用Type接口方法ChanDir(),参入即动态类型。
    3. 判断动态类型是否chan,否则panic
    4. 将动态类型转换为chanType类型
    5. 返回chanType类型中的chan方向
    // chanType represents a channel type.
    type chanType struct {
        rtype
        elem *rtype  // channel element type
        dir  uintptr // channel direction (ChanDir)
    }
    
    

    方法调用示例

    var var_chan chan(int)
    var var_reflect_chan reflect.Type = reflect.TypeOf(var_chan)
    
    0x043c 01084 (reflect.go:31)    MOVQ    CX, "".var_reflect_chan+968(SP) // cx是Type接口类型
    0x0444 01092 (reflect.go:31)    PCDATA  $2, $0
    0x0444 01092 (reflect.go:31)    MOVQ    AX, "".var_reflect_chan+976(SP) // AX是chan(int) 类型
    
    var_reflect_chan.ChanDir();
    
    0x0454 01108 (reflect.go:33)    MOVQ    "".var_reflect_chan+968(SP), AX
    0x045c 01116 (reflect.go:33)    TESTB   AL, (AX)
    0x045e 01118 (reflect.go:33)    MOVQ    48(AX), AX  //ChanDir函数偏移位置48
    0x0462 01122 (reflect.go:33)    PCDATA  $2, $2
    0x0462 01122 (reflect.go:33)    MOVQ    "".var_reflect_chan+976(SP), CX
    0x046a 01130 (reflect.go:33)    PCDATA  $2, $0
    0x046a 01130 (reflect.go:33)    MOVQ    CX, (SP)    //将类型 chan(int)入栈
    0x046e 01134 (reflect.go:33)    CALL    AX  //调用运行时函数  ChanDir()
    
    //
    go.itab.*reflect.rtype,reflect.Type SRODATA dupok size=272
    0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0010 d6 c9 33 e3 00 00 00 00 00 00 00 00 00 00 00 00  ..3.............
    0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    rel 0+8 t=1 type.reflect.Type+0
    rel 8+8 t=1 type.*reflect.rtype+0
    rel 24+8 t=1 reflect.(*rtype).Align+0
    rel 32+8 t=1 reflect.(*rtype).AssignableTo+0
    rel 40+8 t=1 reflect.(*rtype).Bits+0
    rel 48+8 t=1 reflect.(*rtype).ChanDir+0
    

    2. ChanDir()实现

    src\reflect\type.go

    func (t *rtype) ChanDir() ChanDir {
        if t.Kind() != Chan {
            panic("reflect: ChanDir of non-chan type")
        }
        tt := (*chanType)(unsafe.Pointer(t))
        return ChanDir(tt.dir)
    }
    

    2.1. 类型判断

    1. rtype/_type有kind属性。uint8类型共32个字节。 kind uint8 // enumeration for C
    2. 实际只用了31位来描述类型,kindMask=1<<5-1=31.
    3. Kind种类个数总共27种。
    4. Kind()方法就是获取kind属性。
    5. 类型不匹配时,就panic。
    type Kind uint
    const (
        kindDirectIface = 1 << 5
        kindGCProg      = 1 << 6 // Type.gc points to GC program
        kindNoPointers  = 1 << 7
        kindMask        = (1 << 5) - 1
    )
    
    func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }
    
    
    const (
        Invalid Kind = iota
        Bool
        Int
        Int8
        Int16
        Int32
        Int64
        Uint
        Uint8
        Uint16
        Uint32
        Uint64
        Uintptr
        Float32
        Float64
        Complex64
        Complex128
        Array
        Chan
        Func
        Interface
        Map
        Ptr
        Slice
        String
        Struct
        UnsafePointer
    )
    

    2.2. 将类型强制转换为目标类型。

        tt := (*chanType)(unsafe.Pointer(t))
    

    2.3. 调用目标类型对应的方法、属性。

    ChanDir(tt.dir)
    
    

    相关文章

      网友评论

          本文标题:3.reflect.ChanDir()

          本文链接:https://www.haomeiwen.com/subject/rqnwfctx.html