github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/system/network/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package network 5 6 import ( 7 "github.com/primecitizens/pcz/std/core/abi" 8 "github.com/primecitizens/pcz/std/ffi/js" 9 "github.com/primecitizens/pcz/std/plat/js/webext/system/network/bindings" 10 ) 11 12 type GetNetworkInterfacesCallbackFunc func(this js.Ref, networkInterfaces js.Array[NetworkInterface]) js.Ref 13 14 func (fn GetNetworkInterfacesCallbackFunc) Register() js.Func[func(networkInterfaces js.Array[NetworkInterface])] { 15 return js.RegisterCallback[func(networkInterfaces js.Array[NetworkInterface])]( 16 fn, abi.FuncPCABIInternal(fn), 17 ) 18 } 19 20 func (fn GetNetworkInterfacesCallbackFunc) DispatchCallback( 21 targetPC uintptr, ctx *js.CallbackContext, 22 ) { 23 args := ctx.Args() 24 if len(args) != 1+1 /* js this */ || 25 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 26 js.ThrowInvalidCallbackInvocation() 27 } 28 29 if ctx.Return(fn( 30 args[0], 31 32 js.Array[NetworkInterface]{}.FromRef(args[0+1]), 33 )) { 34 return 35 } 36 37 js.ThrowCallbackValueNotReturned() 38 } 39 40 type GetNetworkInterfacesCallback[T any] struct { 41 Fn func(arg T, this js.Ref, networkInterfaces js.Array[NetworkInterface]) js.Ref 42 Arg T 43 } 44 45 func (cb *GetNetworkInterfacesCallback[T]) Register() js.Func[func(networkInterfaces js.Array[NetworkInterface])] { 46 return js.RegisterCallback[func(networkInterfaces js.Array[NetworkInterface])]( 47 cb, abi.FuncPCABIInternal(cb.Fn), 48 ) 49 } 50 51 func (cb *GetNetworkInterfacesCallback[T]) DispatchCallback( 52 targetPC uintptr, ctx *js.CallbackContext, 53 ) { 54 args := ctx.Args() 55 if len(args) != 1+1 /* js this */ || 56 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 57 js.ThrowInvalidCallbackInvocation() 58 } 59 60 if ctx.Return(cb.Fn( 61 cb.Arg, 62 args[0], 63 64 js.Array[NetworkInterface]{}.FromRef(args[0+1]), 65 )) { 66 return 67 } 68 69 js.ThrowCallbackValueNotReturned() 70 } 71 72 type NetworkInterface struct { 73 // Name is "NetworkInterface.name" 74 // 75 // Optional 76 Name js.String 77 // Address is "NetworkInterface.address" 78 // 79 // Optional 80 Address js.String 81 // PrefixLength is "NetworkInterface.prefixLength" 82 // 83 // Optional 84 // 85 // NOTE: FFI_USE_PrefixLength MUST be set to true to make this field effective. 86 PrefixLength int32 87 88 FFI_USE_PrefixLength bool // for PrefixLength. 89 90 FFI_USE bool 91 } 92 93 // FromRef calls UpdateFrom and returns a NetworkInterface with all fields set. 94 func (p NetworkInterface) FromRef(ref js.Ref) NetworkInterface { 95 p.UpdateFrom(ref) 96 return p 97 } 98 99 // New creates a new NetworkInterface in the application heap. 100 func (p NetworkInterface) New() js.Ref { 101 return bindings.NetworkInterfaceJSLoad( 102 js.Pointer(&p), js.True, 0, 103 ) 104 } 105 106 // UpdateFrom copies value of all fields of the heap object to p. 107 func (p *NetworkInterface) UpdateFrom(ref js.Ref) { 108 bindings.NetworkInterfaceJSStore( 109 js.Pointer(p), ref, 110 ) 111 } 112 113 // Update writes all fields of the p to the heap object referenced by ref. 114 func (p *NetworkInterface) Update(ref js.Ref) { 115 bindings.NetworkInterfaceJSLoad( 116 js.Pointer(p), js.False, ref, 117 ) 118 } 119 120 // FreeMembers frees fields with heap reference, if recursive is true 121 // free all heap references reachable from p. 122 func (p *NetworkInterface) FreeMembers(recursive bool) { 123 js.Free( 124 p.Name.Ref(), 125 p.Address.Ref(), 126 ) 127 p.Name = p.Name.FromRef(js.Undefined) 128 p.Address = p.Address.FromRef(js.Undefined) 129 } 130 131 // HasFuncGetNetworkInterfaces returns true if the function "WEBEXT.system.network.getNetworkInterfaces" exists. 132 func HasFuncGetNetworkInterfaces() bool { 133 return js.True == bindings.HasFuncGetNetworkInterfaces() 134 } 135 136 // FuncGetNetworkInterfaces returns the function "WEBEXT.system.network.getNetworkInterfaces". 137 func FuncGetNetworkInterfaces() (fn js.Func[func() js.Promise[js.Array[NetworkInterface]]]) { 138 bindings.FuncGetNetworkInterfaces( 139 js.Pointer(&fn), 140 ) 141 return 142 } 143 144 // GetNetworkInterfaces calls the function "WEBEXT.system.network.getNetworkInterfaces" directly. 145 func GetNetworkInterfaces() (ret js.Promise[js.Array[NetworkInterface]]) { 146 bindings.CallGetNetworkInterfaces( 147 js.Pointer(&ret), 148 ) 149 150 return 151 } 152 153 // TryGetNetworkInterfaces calls the function "WEBEXT.system.network.getNetworkInterfaces" 154 // in a try/catch block and returns (_, err, ok = false) when it went through 155 // the catch clause. 156 func TryGetNetworkInterfaces() (ret js.Promise[js.Array[NetworkInterface]], exception js.Any, ok bool) { 157 ok = js.True == bindings.TryGetNetworkInterfaces( 158 js.Pointer(&ret), js.Pointer(&exception), 159 ) 160 161 return 162 }