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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package certificateproviderinternal
     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/certificateprovider"
    10  	"github.com/primecitizens/pcz/std/plat/js/webext/certificateproviderinternal/bindings"
    11  )
    12  
    13  type DoneCallbackFunc func(this js.Ref) js.Ref
    14  
    15  func (fn DoneCallbackFunc) Register() js.Func[func()] {
    16  	return js.RegisterCallback[func()](
    17  		fn, abi.FuncPCABIInternal(fn),
    18  	)
    19  }
    20  
    21  func (fn DoneCallbackFunc) DispatchCallback(
    22  	targetPC uintptr, ctx *js.CallbackContext,
    23  ) {
    24  	args := ctx.Args()
    25  	if len(args) != 0+1 /* js this */ ||
    26  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    27  		js.ThrowInvalidCallbackInvocation()
    28  	}
    29  
    30  	if ctx.Return(fn(
    31  		args[0],
    32  	)) {
    33  		return
    34  	}
    35  
    36  	js.ThrowCallbackValueNotReturned()
    37  }
    38  
    39  type DoneCallback[T any] struct {
    40  	Fn  func(arg T, this js.Ref) js.Ref
    41  	Arg T
    42  }
    43  
    44  func (cb *DoneCallback[T]) Register() js.Func[func()] {
    45  	return js.RegisterCallback[func()](
    46  		cb, abi.FuncPCABIInternal(cb.Fn),
    47  	)
    48  }
    49  
    50  func (cb *DoneCallback[T]) DispatchCallback(
    51  	targetPC uintptr, ctx *js.CallbackContext,
    52  ) {
    53  	args := ctx.Args()
    54  	if len(args) != 0+1 /* js this */ ||
    55  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
    56  		js.ThrowInvalidCallbackInvocation()
    57  	}
    58  
    59  	if ctx.Return(cb.Fn(
    60  		cb.Arg,
    61  		args[0],
    62  	)) {
    63  		return
    64  	}
    65  
    66  	js.ThrowCallbackValueNotReturned()
    67  }
    68  
    69  type ResultCallbackFunc func(this js.Ref, rejectedCertificates js.Array[js.ArrayBuffer]) js.Ref
    70  
    71  func (fn ResultCallbackFunc) Register() js.Func[func(rejectedCertificates js.Array[js.ArrayBuffer])] {
    72  	return js.RegisterCallback[func(rejectedCertificates js.Array[js.ArrayBuffer])](
    73  		fn, abi.FuncPCABIInternal(fn),
    74  	)
    75  }
    76  
    77  func (fn ResultCallbackFunc) DispatchCallback(
    78  	targetPC uintptr, ctx *js.CallbackContext,
    79  ) {
    80  	args := ctx.Args()
    81  	if len(args) != 1+1 /* js this */ ||
    82  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    83  		js.ThrowInvalidCallbackInvocation()
    84  	}
    85  
    86  	if ctx.Return(fn(
    87  		args[0],
    88  
    89  		js.Array[js.ArrayBuffer]{}.FromRef(args[0+1]),
    90  	)) {
    91  		return
    92  	}
    93  
    94  	js.ThrowCallbackValueNotReturned()
    95  }
    96  
    97  type ResultCallback[T any] struct {
    98  	Fn  func(arg T, this js.Ref, rejectedCertificates js.Array[js.ArrayBuffer]) js.Ref
    99  	Arg T
   100  }
   101  
   102  func (cb *ResultCallback[T]) Register() js.Func[func(rejectedCertificates js.Array[js.ArrayBuffer])] {
   103  	return js.RegisterCallback[func(rejectedCertificates js.Array[js.ArrayBuffer])](
   104  		cb, abi.FuncPCABIInternal(cb.Fn),
   105  	)
   106  }
   107  
   108  func (cb *ResultCallback[T]) DispatchCallback(
   109  	targetPC uintptr, ctx *js.CallbackContext,
   110  ) {
   111  	args := ctx.Args()
   112  	if len(args) != 1+1 /* js this */ ||
   113  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   114  		js.ThrowInvalidCallbackInvocation()
   115  	}
   116  
   117  	if ctx.Return(cb.Fn(
   118  		cb.Arg,
   119  		args[0],
   120  
   121  		js.Array[js.ArrayBuffer]{}.FromRef(args[0+1]),
   122  	)) {
   123  		return
   124  	}
   125  
   126  	js.ThrowCallbackValueNotReturned()
   127  }
   128  
   129  // HasFuncReportCertificates returns true if the function "WEBEXT.certificateProviderInternal.reportCertificates" exists.
   130  func HasFuncReportCertificates() bool {
   131  	return js.True == bindings.HasFuncReportCertificates()
   132  }
   133  
   134  // FuncReportCertificates returns the function "WEBEXT.certificateProviderInternal.reportCertificates".
   135  func FuncReportCertificates() (fn js.Func[func(requestId int32, certificates js.Array[certificateprovider.CertificateInfo]) js.Promise[js.Array[js.ArrayBuffer]]]) {
   136  	bindings.FuncReportCertificates(
   137  		js.Pointer(&fn),
   138  	)
   139  	return
   140  }
   141  
   142  // ReportCertificates calls the function "WEBEXT.certificateProviderInternal.reportCertificates" directly.
   143  func ReportCertificates(requestId int32, certificates js.Array[certificateprovider.CertificateInfo]) (ret js.Promise[js.Array[js.ArrayBuffer]]) {
   144  	bindings.CallReportCertificates(
   145  		js.Pointer(&ret),
   146  		int32(requestId),
   147  		certificates.Ref(),
   148  	)
   149  
   150  	return
   151  }
   152  
   153  // TryReportCertificates calls the function "WEBEXT.certificateProviderInternal.reportCertificates"
   154  // in a try/catch block and returns (_, err, ok = false) when it went through
   155  // the catch clause.
   156  func TryReportCertificates(requestId int32, certificates js.Array[certificateprovider.CertificateInfo]) (ret js.Promise[js.Array[js.ArrayBuffer]], exception js.Any, ok bool) {
   157  	ok = js.True == bindings.TryReportCertificates(
   158  		js.Pointer(&ret), js.Pointer(&exception),
   159  		int32(requestId),
   160  		certificates.Ref(),
   161  	)
   162  
   163  	return
   164  }
   165  
   166  // HasFuncReportSignature returns true if the function "WEBEXT.certificateProviderInternal.reportSignature" exists.
   167  func HasFuncReportSignature() bool {
   168  	return js.True == bindings.HasFuncReportSignature()
   169  }
   170  
   171  // FuncReportSignature returns the function "WEBEXT.certificateProviderInternal.reportSignature".
   172  func FuncReportSignature() (fn js.Func[func(requestId int32, signature js.ArrayBuffer) js.Promise[js.Void]]) {
   173  	bindings.FuncReportSignature(
   174  		js.Pointer(&fn),
   175  	)
   176  	return
   177  }
   178  
   179  // ReportSignature calls the function "WEBEXT.certificateProviderInternal.reportSignature" directly.
   180  func ReportSignature(requestId int32, signature js.ArrayBuffer) (ret js.Promise[js.Void]) {
   181  	bindings.CallReportSignature(
   182  		js.Pointer(&ret),
   183  		int32(requestId),
   184  		signature.Ref(),
   185  	)
   186  
   187  	return
   188  }
   189  
   190  // TryReportSignature calls the function "WEBEXT.certificateProviderInternal.reportSignature"
   191  // in a try/catch block and returns (_, err, ok = false) when it went through
   192  // the catch clause.
   193  func TryReportSignature(requestId int32, signature js.ArrayBuffer) (ret js.Promise[js.Void], exception js.Any, ok bool) {
   194  	ok = js.True == bindings.TryReportSignature(
   195  		js.Pointer(&ret), js.Pointer(&exception),
   196  		int32(requestId),
   197  		signature.Ref(),
   198  	)
   199  
   200  	return
   201  }