github.com/hsfzxjy/dgo/go@v0.2.0/special_int.go (about)

     1  package dgo
     2  
     3  type _SpecialIntKind uint64
     4  
     5  const (
     6  	sikDartCallback _SpecialIntKind = iota
     7  	sikGoCallback
     8  	sikGoObject
     9  	sikGoMethod
    10  	sikDartFutureCallback
    11  	sikDartStreamCallback
    12  	sikDartCallbackGroup
    13  	sikPreservedGoCall
    14  )
    15  
    16  const (
    17  	specialIntMagic uint64 = 0b0111_1111_1111_1
    18  )
    19  
    20  type _SpecialInt interface {
    21  	specialInt()
    22  }
    23  
    24  type _Serializable interface {
    25  	_SpecialInt
    26  	getPayload() uint64
    27  	getKind() _SpecialIntKind
    28  }
    29  
    30  type _Handlable interface {
    31  	_SpecialInt
    32  	handleCObjects(objs []*Dart_CObject)
    33  }
    34  
    35  func _buildSpecialInt(kind _SpecialIntKind, port *Port, payload uint64) _SpecialInt {
    36  	switch kind {
    37  	case sikDartCallback:
    38  		return DartCallback{payload, port}
    39  	case sikDartFutureCallback:
    40  		return DartFutureCallback{payload, port}
    41  	case sikDartStreamCallback:
    42  		return DartStreamCallback{payload, port}
    43  	default:
    44  		panic("unreachable")
    45  	}
    46  }
    47  
    48  func _buildHandlable(kind _SpecialIntKind, port *Port, payload uint64) _Handlable {
    49  	switch kind {
    50  	case sikGoCallback:
    51  		return invokingGoCallback{payload, port}
    52  	case sikGoMethod:
    53  		return invokingGoMethod{payload, port}
    54  	case sikPreservedGoCall:
    55  		return _PreservedGoCall{payload, port}
    56  	default:
    57  		panic("unreachable")
    58  	}
    59  }
    60  
    61  func parseSpecialInt(port *Port, value uint64, handlable bool) (result _SpecialInt, success bool) {
    62  	if value>>(64-13) != specialIntMagic {
    63  		return nil, false
    64  	}
    65  	kind := _SpecialIntKind((value >> 48) & 0b111)
    66  	payload := value & ((1 << 48) - 1)
    67  
    68  	if handlable {
    69  		result = _buildHandlable(kind, port, payload)
    70  	} else {
    71  		result = _buildSpecialInt(kind, port, payload)
    72  	}
    73  	return result, true
    74  }
    75  
    76  func serialize(kind _SpecialIntKind, payload uint64) uint64 {
    77  	return (specialIntMagic << (64 - 13)) |
    78  		(uint64(kind) << 48) |
    79  		payload
    80  }
    81  
    82  func serializeSpecialInt(i _Serializable) uint64 {
    83  	return serialize(i.getKind(), i.getPayload())
    84  }