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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package accessibilityserviceprivate
     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/accessibilityserviceprivate/bindings"
    10  )
    11  
    12  type VoidCallbackFunc func(this js.Ref) js.Ref
    13  
    14  func (fn VoidCallbackFunc) Register() js.Func[func()] {
    15  	return js.RegisterCallback[func()](
    16  		fn, abi.FuncPCABIInternal(fn),
    17  	)
    18  }
    19  
    20  func (fn VoidCallbackFunc) DispatchCallback(
    21  	targetPC uintptr, ctx *js.CallbackContext,
    22  ) {
    23  	args := ctx.Args()
    24  	if len(args) != 0+1 /* js this */ ||
    25  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    26  		js.ThrowInvalidCallbackInvocation()
    27  	}
    28  
    29  	if ctx.Return(fn(
    30  		args[0],
    31  	)) {
    32  		return
    33  	}
    34  
    35  	js.ThrowCallbackValueNotReturned()
    36  }
    37  
    38  type VoidCallback[T any] struct {
    39  	Fn  func(arg T, this js.Ref) js.Ref
    40  	Arg T
    41  }
    42  
    43  func (cb *VoidCallback[T]) Register() js.Func[func()] {
    44  	return js.RegisterCallback[func()](
    45  		cb, abi.FuncPCABIInternal(cb.Fn),
    46  	)
    47  }
    48  
    49  func (cb *VoidCallback[T]) DispatchCallback(
    50  	targetPC uintptr, ctx *js.CallbackContext,
    51  ) {
    52  	args := ctx.Args()
    53  	if len(args) != 0+1 /* js this */ ||
    54  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
    55  		js.ThrowInvalidCallbackInvocation()
    56  	}
    57  
    58  	if ctx.Return(cb.Fn(
    59  		cb.Arg,
    60  		args[0],
    61  	)) {
    62  		return
    63  	}
    64  
    65  	js.ThrowCallbackValueNotReturned()
    66  }
    67  
    68  // HasFuncSpeakSelectedText returns true if the function "WEBEXT.accessibilityServicePrivate.speakSelectedText" exists.
    69  func HasFuncSpeakSelectedText() bool {
    70  	return js.True == bindings.HasFuncSpeakSelectedText()
    71  }
    72  
    73  // FuncSpeakSelectedText returns the function "WEBEXT.accessibilityServicePrivate.speakSelectedText".
    74  func FuncSpeakSelectedText() (fn js.Func[func() js.Promise[js.Void]]) {
    75  	bindings.FuncSpeakSelectedText(
    76  		js.Pointer(&fn),
    77  	)
    78  	return
    79  }
    80  
    81  // SpeakSelectedText calls the function "WEBEXT.accessibilityServicePrivate.speakSelectedText" directly.
    82  func SpeakSelectedText() (ret js.Promise[js.Void]) {
    83  	bindings.CallSpeakSelectedText(
    84  		js.Pointer(&ret),
    85  	)
    86  
    87  	return
    88  }
    89  
    90  // TrySpeakSelectedText calls the function "WEBEXT.accessibilityServicePrivate.speakSelectedText"
    91  // in a try/catch block and returns (_, err, ok = false) when it went through
    92  // the catch clause.
    93  func TrySpeakSelectedText() (ret js.Promise[js.Void], exception js.Any, ok bool) {
    94  	ok = js.True == bindings.TrySpeakSelectedText(
    95  		js.Pointer(&ret), js.Pointer(&exception),
    96  	)
    97  
    98  	return
    99  }