github.com/primecitizens/pcz/std@v0.2.1/ffi/js/func.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 func ThrowInvalidCallbackInvocation() { 12 assert.Throw("invalid", "callback", "invocation") 13 } 14 15 func ThrowCallbackValueNotReturned() { 16 assert.Throw( 17 "value", "not", "returned:", "internal", "callback", "bindings", "error", 18 ) 19 } 20 21 type Func[T any] struct { 22 ref bindings.Ref 23 } 24 25 func (fn Func[T]) FromRef(ref Ref) Func[T] { 26 return Func[T]{ 27 ref: bindings.Ref(ref), 28 } 29 } 30 31 func (fn Func[T]) Ref() Ref { 32 return Ref(fn.ref) 33 } 34 35 func (fn Func[T]) Once() Func[T] { 36 bindings.Once(fn.ref) 37 return fn 38 } 39 40 func (fn Func[T]) Free() { 41 bindings.Free(fn.ref) 42 } 43 44 // Try runs the fn in a trycatch block, return heap reference of the result 45 // of either fn or catch. 46 // 47 // When free is true, all args after it (catch, finally, args...) are freed 48 // after the call. 49 // 50 // The bool return value indicates whether the try was successful (catch was 51 // not called) 52 func (fn Func[T]) Try( 53 this Ref, 54 take bool, 55 catch Func[func(err Any, args []Ref) Ref], 56 finally Func[func(args []Ref)], 57 args ...Ref, 58 ) (Ref, bool) { 59 var caught bool 60 ret := bindings.Try( 61 fn.ref, 62 bindings.Ref(this), 63 bindings.Ref(Bool(take)), 64 catch.ref, 65 finally.ref, 66 SliceData(args), 67 SizeU(len(args)), 68 Pointer(&caught), 69 ) 70 71 return Ref(ret), !caught 72 } 73 74 // Call calls js function fn and returns the heap reference of the js return 75 // value. 76 // 77 // When free is ture, all args after it are freed after the call. 78 func (fn Func[T]) Call(this Ref, take bool, args ...Ref) Ref { 79 return Ref(bindings.Call( 80 fn.ref, 81 bindings.Ref(this), 82 bindings.Ref(Bool(take)), 83 SliceData(args), 84 SizeU(len(args)), 85 )) 86 } 87 88 // CallVoid calls js function fn and discard any return value. 89 // 90 // When free is ture, all args after it are freed after the call. 91 func (fn Func[T]) CallVoid(this Ref, take bool, args ...Ref) { 92 bindings.CallVoid( 93 fn.ref, 94 bindings.Ref(this), 95 bindings.Ref(Bool(take)), 96 SliceData(args), 97 SizeU(len(args)), 98 ) 99 } 100 101 // CallNum calls js function fn and treate the return value as a js number. 102 // 103 // When free is ture, all args after it are freed after the call. 104 func (fn Func[T]) CallNum(this Ref, take bool, args ...Ref) float64 { 105 return bindings.CallNum( 106 fn.ref, 107 bindings.Ref(this), 108 bindings.Ref(Bool(take)), 109 SliceData(args), 110 SizeU(len(args)), 111 ) 112 } 113 114 // CallBool calls js function fn and treate the return value as a js boolean. 115 // 116 // When free is ture, all args after it are freed after the call. 117 func (fn Func[T]) CallBool(this Ref, take bool, args ...Ref) bool { 118 return bindings.Ref(True) == bindings.CallBool( 119 fn.ref, 120 bindings.Ref(this), 121 bindings.Ref(Bool(take)), 122 SliceData(args), 123 SizeU(len(args)), 124 ) 125 }