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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package js
     5  
     6  import (
     7  	"github.com/primecitizens/pcz/std/core/assert"
     8  	"github.com/primecitizens/pcz/std/ffi/js/bindings"
     9  )
    10  
    11  const smallIntCacheSize = 128
    12  
    13  const (
    14  	Undefined   Ref = iota // undefined
    15  	Null                   // null
    16  	Zero                   // 0
    17  	False                  // false
    18  	True                   // true
    19  	NaN                    // NaN
    20  	Inf                    // Infinity
    21  	NegativeInf            // -Infinity
    22  	Global                 // globalThis
    23  
    24  	firstSmallIntCache                                          // = 1
    25  	lastSmallIntCache  = firstSmallIntCache + smallIntCacheSize // = 128
    26  )
    27  
    28  type Ref bindings.Ref
    29  
    30  func (r Ref) Undefined() bool { return r == Undefined }
    31  
    32  // Falsy returns true when r is the reference to one of following values:
    33  //   - Undefined
    34  //   - Null
    35  //   - 0
    36  //   - False
    37  //
    38  // Otherwise, it returns false.
    39  func (r Ref) Falsy() bool { return r < True }
    40  
    41  // Truthy returns true when r.Falsy() returns false.
    42  func (r Ref) Truthy() bool { return r > False }
    43  
    44  func (r Ref) Once() Ref {
    45  	bindings.Once(bindings.Ref(r))
    46  	return r
    47  }
    48  
    49  // Clone returns a new heap reference to the same object referenced by r.
    50  func (r Ref) Clone() Ref {
    51  	return Ref(bindings.Clone(bindings.Ref(r)))
    52  }
    53  
    54  // Free the referenced js value.
    55  func (r Ref) Free() {
    56  	bindings.Free(bindings.Ref(r))
    57  }
    58  
    59  func (r Ref) Any() Any {
    60  	return Any{
    61  		ref: bindings.Ref(r),
    62  	}
    63  }
    64  
    65  func Free[T ~uint32](refs ...T) {
    66  	if len(refs) == 0 {
    67  		return
    68  	}
    69  
    70  	bindings.BatchFree(
    71  		SliceData(refs),
    72  		SizeU(len(refs)),
    73  	)
    74  }
    75  
    76  type Any struct {
    77  	ref bindings.Ref
    78  }
    79  
    80  func (x Any) FromRef(ref Ref) Any {
    81  	return Any{ref: bindings.Ref(ref)}
    82  }
    83  
    84  func (x Any) Ref() Ref {
    85  	return Ref(x.ref)
    86  }
    87  
    88  func (x Any) Once() Any {
    89  	bindings.Once(x.ref)
    90  	return x
    91  }
    92  
    93  func (x Any) Free() {
    94  	bindings.Free(x.ref)
    95  }
    96  
    97  func (x Any) AsNumber() Number[float64] {
    98  	return Number[float64]{ref: x.ref}
    99  }
   100  
   101  func (x Any) AsFunc() Func[Any] {
   102  	return Func[Any]{ref: x.ref}
   103  }
   104  
   105  func (x Any) AsArray() Array[Any] {
   106  	return Array[Any]{ref: x.ref}
   107  }
   108  
   109  func (x Any) AsString() String {
   110  	return String{ref: x.ref}
   111  }
   112  
   113  func (x Any) AsObject() Object {
   114  	return Object{ref: x.ref}
   115  }
   116  
   117  func (x Any) InstanceOf(o Ref) bool {
   118  	return bindings.Ref(True) == bindings.Instanceof(
   119  		x.ref,
   120  		bindings.Ref(o),
   121  	)
   122  }
   123  
   124  type Void struct{}
   125  
   126  func (Void) FromRef(Ref) Void { return Void{} }
   127  func (Void) Ref() Ref         { return Undefined }
   128  func (Void) Once() Void       { return Void{} }
   129  func (Void) Free()            {}
   130  
   131  type Boolean struct {
   132  	ref Ref
   133  }
   134  
   135  func (b Boolean) FromRef(ref Ref) Boolean {
   136  	if ref != True && ref != False {
   137  		assert.Throw("not", "a", "boolean", "ref")
   138  	}
   139  
   140  	return Boolean{
   141  		ref: ref,
   142  	}
   143  }
   144  
   145  func (b Boolean) Ref() Ref {
   146  	return b.ref
   147  }
   148  
   149  func (b Boolean) Once() Boolean {
   150  	return b
   151  }
   152  
   153  func (b Boolean) Free() {}
   154  
   155  func (b Boolean) Get() bool {
   156  	return b.ref == True
   157  }