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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package printerproviderinternal
     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/printerprovider"
    10  	"github.com/primecitizens/pcz/std/plat/js/webext/printerproviderinternal/bindings"
    11  )
    12  
    13  type BlobCallbackFunc func(this js.Ref, blob js.Object) js.Ref
    14  
    15  func (fn BlobCallbackFunc) Register() js.Func[func(blob js.Object)] {
    16  	return js.RegisterCallback[func(blob js.Object)](
    17  		fn, abi.FuncPCABIInternal(fn),
    18  	)
    19  }
    20  
    21  func (fn BlobCallbackFunc) 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  
    30  	if ctx.Return(fn(
    31  		args[0],
    32  
    33  		js.Object{}.FromRef(args[0+1]),
    34  	)) {
    35  		return
    36  	}
    37  
    38  	js.ThrowCallbackValueNotReturned()
    39  }
    40  
    41  type BlobCallback[T any] struct {
    42  	Fn  func(arg T, this js.Ref, blob js.Object) js.Ref
    43  	Arg T
    44  }
    45  
    46  func (cb *BlobCallback[T]) Register() js.Func[func(blob js.Object)] {
    47  	return js.RegisterCallback[func(blob js.Object)](
    48  		cb, abi.FuncPCABIInternal(cb.Fn),
    49  	)
    50  }
    51  
    52  func (cb *BlobCallback[T]) DispatchCallback(
    53  	targetPC uintptr, ctx *js.CallbackContext,
    54  ) {
    55  	args := ctx.Args()
    56  	if len(args) != 1+1 /* js this */ ||
    57  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
    58  		js.ThrowInvalidCallbackInvocation()
    59  	}
    60  
    61  	if ctx.Return(cb.Fn(
    62  		cb.Arg,
    63  		args[0],
    64  
    65  		js.Object{}.FromRef(args[0+1]),
    66  	)) {
    67  		return
    68  	}
    69  
    70  	js.ThrowCallbackValueNotReturned()
    71  }
    72  
    73  type PrintError uint32
    74  
    75  const (
    76  	_ PrintError = iota
    77  
    78  	PrintError_OK
    79  	PrintError_FAILED
    80  	PrintError_INVALID_TICKET
    81  	PrintError_INVALID_DATA
    82  )
    83  
    84  func (PrintError) FromRef(str js.Ref) PrintError {
    85  	return PrintError(bindings.ConstOfPrintError(str))
    86  }
    87  
    88  func (x PrintError) String() (string, bool) {
    89  	switch x {
    90  	case PrintError_OK:
    91  		return "OK", true
    92  	case PrintError_FAILED:
    93  		return "FAILED", true
    94  	case PrintError_INVALID_TICKET:
    95  		return "INVALID_TICKET", true
    96  	case PrintError_INVALID_DATA:
    97  		return "INVALID_DATA", true
    98  	default:
    99  		return "", false
   100  	}
   101  }
   102  
   103  // HasFuncGetPrintData returns true if the function "WEBEXT.printerProviderInternal.getPrintData" exists.
   104  func HasFuncGetPrintData() bool {
   105  	return js.True == bindings.HasFuncGetPrintData()
   106  }
   107  
   108  // FuncGetPrintData returns the function "WEBEXT.printerProviderInternal.getPrintData".
   109  func FuncGetPrintData() (fn js.Func[func(requestId int32) js.Promise[js.Object]]) {
   110  	bindings.FuncGetPrintData(
   111  		js.Pointer(&fn),
   112  	)
   113  	return
   114  }
   115  
   116  // GetPrintData calls the function "WEBEXT.printerProviderInternal.getPrintData" directly.
   117  func GetPrintData(requestId int32) (ret js.Promise[js.Object]) {
   118  	bindings.CallGetPrintData(
   119  		js.Pointer(&ret),
   120  		int32(requestId),
   121  	)
   122  
   123  	return
   124  }
   125  
   126  // TryGetPrintData calls the function "WEBEXT.printerProviderInternal.getPrintData"
   127  // in a try/catch block and returns (_, err, ok = false) when it went through
   128  // the catch clause.
   129  func TryGetPrintData(requestId int32) (ret js.Promise[js.Object], exception js.Any, ok bool) {
   130  	ok = js.True == bindings.TryGetPrintData(
   131  		js.Pointer(&ret), js.Pointer(&exception),
   132  		int32(requestId),
   133  	)
   134  
   135  	return
   136  }
   137  
   138  // HasFuncReportPrintResult returns true if the function "WEBEXT.printerProviderInternal.reportPrintResult" exists.
   139  func HasFuncReportPrintResult() bool {
   140  	return js.True == bindings.HasFuncReportPrintResult()
   141  }
   142  
   143  // FuncReportPrintResult returns the function "WEBEXT.printerProviderInternal.reportPrintResult".
   144  func FuncReportPrintResult() (fn js.Func[func(request_id int32, err PrintError)]) {
   145  	bindings.FuncReportPrintResult(
   146  		js.Pointer(&fn),
   147  	)
   148  	return
   149  }
   150  
   151  // ReportPrintResult calls the function "WEBEXT.printerProviderInternal.reportPrintResult" directly.
   152  func ReportPrintResult(request_id int32, err PrintError) (ret js.Void) {
   153  	bindings.CallReportPrintResult(
   154  		js.Pointer(&ret),
   155  		int32(request_id),
   156  		uint32(err),
   157  	)
   158  
   159  	return
   160  }
   161  
   162  // TryReportPrintResult calls the function "WEBEXT.printerProviderInternal.reportPrintResult"
   163  // in a try/catch block and returns (_, err, ok = false) when it went through
   164  // the catch clause.
   165  func TryReportPrintResult(request_id int32, err PrintError) (ret js.Void, exception js.Any, ok bool) {
   166  	ok = js.True == bindings.TryReportPrintResult(
   167  		js.Pointer(&ret), js.Pointer(&exception),
   168  		int32(request_id),
   169  		uint32(err),
   170  	)
   171  
   172  	return
   173  }
   174  
   175  // HasFuncReportPrinterCapability returns true if the function "WEBEXT.printerProviderInternal.reportPrinterCapability" exists.
   176  func HasFuncReportPrinterCapability() bool {
   177  	return js.True == bindings.HasFuncReportPrinterCapability()
   178  }
   179  
   180  // FuncReportPrinterCapability returns the function "WEBEXT.printerProviderInternal.reportPrinterCapability".
   181  func FuncReportPrinterCapability() (fn js.Func[func(request_id int32, capability js.Object)]) {
   182  	bindings.FuncReportPrinterCapability(
   183  		js.Pointer(&fn),
   184  	)
   185  	return
   186  }
   187  
   188  // ReportPrinterCapability calls the function "WEBEXT.printerProviderInternal.reportPrinterCapability" directly.
   189  func ReportPrinterCapability(request_id int32, capability js.Object) (ret js.Void) {
   190  	bindings.CallReportPrinterCapability(
   191  		js.Pointer(&ret),
   192  		int32(request_id),
   193  		capability.Ref(),
   194  	)
   195  
   196  	return
   197  }
   198  
   199  // TryReportPrinterCapability calls the function "WEBEXT.printerProviderInternal.reportPrinterCapability"
   200  // in a try/catch block and returns (_, err, ok = false) when it went through
   201  // the catch clause.
   202  func TryReportPrinterCapability(request_id int32, capability js.Object) (ret js.Void, exception js.Any, ok bool) {
   203  	ok = js.True == bindings.TryReportPrinterCapability(
   204  		js.Pointer(&ret), js.Pointer(&exception),
   205  		int32(request_id),
   206  		capability.Ref(),
   207  	)
   208  
   209  	return
   210  }
   211  
   212  // HasFuncReportPrinters returns true if the function "WEBEXT.printerProviderInternal.reportPrinters" exists.
   213  func HasFuncReportPrinters() bool {
   214  	return js.True == bindings.HasFuncReportPrinters()
   215  }
   216  
   217  // FuncReportPrinters returns the function "WEBEXT.printerProviderInternal.reportPrinters".
   218  func FuncReportPrinters() (fn js.Func[func(requestId int32, printers js.Array[printerprovider.PrinterInfo])]) {
   219  	bindings.FuncReportPrinters(
   220  		js.Pointer(&fn),
   221  	)
   222  	return
   223  }
   224  
   225  // ReportPrinters calls the function "WEBEXT.printerProviderInternal.reportPrinters" directly.
   226  func ReportPrinters(requestId int32, printers js.Array[printerprovider.PrinterInfo]) (ret js.Void) {
   227  	bindings.CallReportPrinters(
   228  		js.Pointer(&ret),
   229  		int32(requestId),
   230  		printers.Ref(),
   231  	)
   232  
   233  	return
   234  }
   235  
   236  // TryReportPrinters calls the function "WEBEXT.printerProviderInternal.reportPrinters"
   237  // in a try/catch block and returns (_, err, ok = false) when it went through
   238  // the catch clause.
   239  func TryReportPrinters(requestId int32, printers js.Array[printerprovider.PrinterInfo]) (ret js.Void, exception js.Any, ok bool) {
   240  	ok = js.True == bindings.TryReportPrinters(
   241  		js.Pointer(&ret), js.Pointer(&exception),
   242  		int32(requestId),
   243  		printers.Ref(),
   244  	)
   245  
   246  	return
   247  }
   248  
   249  // HasFuncReportUsbPrinterInfo returns true if the function "WEBEXT.printerProviderInternal.reportUsbPrinterInfo" exists.
   250  func HasFuncReportUsbPrinterInfo() bool {
   251  	return js.True == bindings.HasFuncReportUsbPrinterInfo()
   252  }
   253  
   254  // FuncReportUsbPrinterInfo returns the function "WEBEXT.printerProviderInternal.reportUsbPrinterInfo".
   255  func FuncReportUsbPrinterInfo() (fn js.Func[func(requestId int32, printerInfo printerprovider.PrinterInfo)]) {
   256  	bindings.FuncReportUsbPrinterInfo(
   257  		js.Pointer(&fn),
   258  	)
   259  	return
   260  }
   261  
   262  // ReportUsbPrinterInfo calls the function "WEBEXT.printerProviderInternal.reportUsbPrinterInfo" directly.
   263  func ReportUsbPrinterInfo(requestId int32, printerInfo printerprovider.PrinterInfo) (ret js.Void) {
   264  	bindings.CallReportUsbPrinterInfo(
   265  		js.Pointer(&ret),
   266  		int32(requestId),
   267  		js.Pointer(&printerInfo),
   268  	)
   269  
   270  	return
   271  }
   272  
   273  // TryReportUsbPrinterInfo calls the function "WEBEXT.printerProviderInternal.reportUsbPrinterInfo"
   274  // in a try/catch block and returns (_, err, ok = false) when it went through
   275  // the catch clause.
   276  func TryReportUsbPrinterInfo(requestId int32, printerInfo printerprovider.PrinterInfo) (ret js.Void, exception js.Any, ok bool) {
   277  	ok = js.True == bindings.TryReportUsbPrinterInfo(
   278  		js.Pointer(&ret), js.Pointer(&exception),
   279  		int32(requestId),
   280  		js.Pointer(&printerInfo),
   281  	)
   282  
   283  	return
   284  }