github.com/primecitizens/pcz/std@v0.2.1/ffi/js/utils.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package js
     5  
     6  import (
     7  	"unsafe"
     8  
     9  	"github.com/primecitizens/pcz/std/core/assert"
    10  	"github.com/primecitizens/pcz/std/core/mark"
    11  	"github.com/primecitizens/pcz/std/core/num"
    12  )
    13  
    14  func Must[T any](x T, ok bool) T {
    15  	if ok {
    16  		return x
    17  	}
    18  
    19  	assert.Throw("value", "not", "present")
    20  	return x
    21  }
    22  
    23  func Bool(b bool) Ref {
    24  	if b {
    25  		return True
    26  	}
    27  
    28  	return False
    29  }
    30  
    31  func Pointer[T any](p *T) unsafe.Pointer {
    32  	return mark.NoEscapeUnsafePointer(unsafe.Pointer(p))
    33  }
    34  
    35  func StringData(s string) unsafe.Pointer {
    36  	return mark.NoEscapeStringDataPointer(s)
    37  }
    38  
    39  func SliceData[T any](s []T) unsafe.Pointer {
    40  	return mark.NoEscapeSliceDataPointer(s)
    41  }
    42  
    43  func SizeU[T num.Integer | num.Pointer](v T) (x uint32) {
    44  	if x = uint32(uintptr(v)); uintptr(x) != uintptr(v) {
    45  		assert.Throw("overflow")
    46  	}
    47  
    48  	return x
    49  }
    50  
    51  func SizeI[T num.Integer | num.Pointer](v T) (x int32) {
    52  	if x = int32(uintptr(v)); uintptr(x) != uintptr(v) {
    53  		assert.Throw("overflow")
    54  	}
    55  
    56  	return x
    57  }