github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekaerr/unsafe.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekaerr
     7  
     8  import (
     9  	"unsafe"
    10  
    11  	"github.com/qioalice/ekago/v3/internal/ekaletter"
    12  )
    13  
    14  // -----
    15  // Functions with names started with "bridge..." are not used in this package
    16  // but assigns to the bridge
    17  // ( https://github.com/qioalice/ekago/internal/letter/bridge.go )
    18  // and used at the ekaunsafe package's ekaerr related functions
    19  // ( https://github.com/qioalice/ekago/ekaunsafe/ekaerr.go )
    20  // -----
    21  
    22  // bridgeGetLetter return *ekaletter.Letter object from the *ekaerr.Error object
    23  // assuming that 'err' is an untyped pointer to the ekaerr.Error.
    24  // Returns nil if 'err' == nil.
    25  func bridgeGetLetter(err unsafe.Pointer) *ekaletter.Letter {
    26  	if err := (*Error)(err); err.IsValid() {
    27  		return err.letter
    28  	} else {
    29  		return nil
    30  	}
    31  }
    32  
    33  // bridgeGetStackIdx returns current stackIdx of the *ekaerr.Error object
    34  // assuming that 'err' is an untyped pointer to the ekaerr.Error.
    35  func bridgeGetStackIdx(err unsafe.Pointer) int16 {
    36  	if err := (*Error)(err); err.IsValid() {
    37  		return ekaletter.LGetStackIdx(err.letter)
    38  	} else {
    39  		return -1
    40  	}
    41  }
    42  
    43  // bridgeSetStackIdx sets the new value of the *ekaerr.Error object's 'stackIdx' field
    44  // assuming that 'err' is an untyped pointer to the ekaerr.Error.
    45  // New stack index must be 0 or greater.
    46  //
    47  // IT MUST BE USED CAREFULLY!
    48  // YOU CAN GET PANIC IF YOUR STACK IDX WILL BE GREATER THAN AN EMBEDDED STACKTRACE'S LENGTH.
    49  func bridgeSetStackIdx(err unsafe.Pointer, newStackIdx int16) {
    50  	if err := (*Error)(err); err.IsValid() && newStackIdx >= 0 {
    51  		ekaletter.LSetStackIdx(err.letter, newStackIdx)
    52  	}
    53  }