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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package resourcesprivate
     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/resourcesprivate/bindings"
    10  )
    11  
    12  type Component uint32
    13  
    14  const (
    15  	_ Component = iota
    16  
    17  	Component_IDENTITY
    18  	Component_PDF
    19  )
    20  
    21  func (Component) FromRef(str js.Ref) Component {
    22  	return Component(bindings.ConstOfComponent(str))
    23  }
    24  
    25  func (x Component) String() (string, bool) {
    26  	switch x {
    27  	case Component_IDENTITY:
    28  		return "identity", true
    29  	case Component_PDF:
    30  		return "pdf", true
    31  	default:
    32  		return "", false
    33  	}
    34  }
    35  
    36  type GetStringsCallbackFunc func(this js.Ref, result js.Object) js.Ref
    37  
    38  func (fn GetStringsCallbackFunc) Register() js.Func[func(result js.Object)] {
    39  	return js.RegisterCallback[func(result js.Object)](
    40  		fn, abi.FuncPCABIInternal(fn),
    41  	)
    42  }
    43  
    44  func (fn GetStringsCallbackFunc) DispatchCallback(
    45  	targetPC uintptr, ctx *js.CallbackContext,
    46  ) {
    47  	args := ctx.Args()
    48  	if len(args) != 1+1 /* js this */ ||
    49  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    50  		js.ThrowInvalidCallbackInvocation()
    51  	}
    52  
    53  	if ctx.Return(fn(
    54  		args[0],
    55  
    56  		js.Object{}.FromRef(args[0+1]),
    57  	)) {
    58  		return
    59  	}
    60  
    61  	js.ThrowCallbackValueNotReturned()
    62  }
    63  
    64  type GetStringsCallback[T any] struct {
    65  	Fn  func(arg T, this js.Ref, result js.Object) js.Ref
    66  	Arg T
    67  }
    68  
    69  func (cb *GetStringsCallback[T]) Register() js.Func[func(result js.Object)] {
    70  	return js.RegisterCallback[func(result js.Object)](
    71  		cb, abi.FuncPCABIInternal(cb.Fn),
    72  	)
    73  }
    74  
    75  func (cb *GetStringsCallback[T]) DispatchCallback(
    76  	targetPC uintptr, ctx *js.CallbackContext,
    77  ) {
    78  	args := ctx.Args()
    79  	if len(args) != 1+1 /* js this */ ||
    80  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
    81  		js.ThrowInvalidCallbackInvocation()
    82  	}
    83  
    84  	if ctx.Return(cb.Fn(
    85  		cb.Arg,
    86  		args[0],
    87  
    88  		js.Object{}.FromRef(args[0+1]),
    89  	)) {
    90  		return
    91  	}
    92  
    93  	js.ThrowCallbackValueNotReturned()
    94  }
    95  
    96  // HasFuncGetStrings returns true if the function "WEBEXT.resourcesPrivate.getStrings" exists.
    97  func HasFuncGetStrings() bool {
    98  	return js.True == bindings.HasFuncGetStrings()
    99  }
   100  
   101  // FuncGetStrings returns the function "WEBEXT.resourcesPrivate.getStrings".
   102  func FuncGetStrings() (fn js.Func[func(component Component) js.Promise[js.Object]]) {
   103  	bindings.FuncGetStrings(
   104  		js.Pointer(&fn),
   105  	)
   106  	return
   107  }
   108  
   109  // GetStrings calls the function "WEBEXT.resourcesPrivate.getStrings" directly.
   110  func GetStrings(component Component) (ret js.Promise[js.Object]) {
   111  	bindings.CallGetStrings(
   112  		js.Pointer(&ret),
   113  		uint32(component),
   114  	)
   115  
   116  	return
   117  }
   118  
   119  // TryGetStrings calls the function "WEBEXT.resourcesPrivate.getStrings"
   120  // in a try/catch block and returns (_, err, ok = false) when it went through
   121  // the catch clause.
   122  func TryGetStrings(component Component) (ret js.Promise[js.Object], exception js.Any, ok bool) {
   123  	ok = js.True == bindings.TryGetStrings(
   124  		js.Pointer(&ret), js.Pointer(&exception),
   125  		uint32(component),
   126  	)
   127  
   128  	return
   129  }