github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/enterprise/networkingattributes/apis_js_wasm.go (about)

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