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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package sharedstorageprivate
     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/sharedstorageprivate/bindings"
    10  )
    11  
    12  type GetCallbackFunc func(this js.Ref, items js.Object) js.Ref
    13  
    14  func (fn GetCallbackFunc) Register() js.Func[func(items js.Object)] {
    15  	return js.RegisterCallback[func(items js.Object)](
    16  		fn, abi.FuncPCABIInternal(fn),
    17  	)
    18  }
    19  
    20  func (fn GetCallbackFunc) DispatchCallback(
    21  	targetPC uintptr, ctx *js.CallbackContext,
    22  ) {
    23  	args := ctx.Args()
    24  	if len(args) != 1+1 /* js this */ ||
    25  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    26  		js.ThrowInvalidCallbackInvocation()
    27  	}
    28  
    29  	if ctx.Return(fn(
    30  		args[0],
    31  
    32  		js.Object{}.FromRef(args[0+1]),
    33  	)) {
    34  		return
    35  	}
    36  
    37  	js.ThrowCallbackValueNotReturned()
    38  }
    39  
    40  type GetCallback[T any] struct {
    41  	Fn  func(arg T, this js.Ref, items js.Object) js.Ref
    42  	Arg T
    43  }
    44  
    45  func (cb *GetCallback[T]) Register() js.Func[func(items js.Object)] {
    46  	return js.RegisterCallback[func(items js.Object)](
    47  		cb, abi.FuncPCABIInternal(cb.Fn),
    48  	)
    49  }
    50  
    51  func (cb *GetCallback[T]) DispatchCallback(
    52  	targetPC uintptr, ctx *js.CallbackContext,
    53  ) {
    54  	args := ctx.Args()
    55  	if len(args) != 1+1 /* js this */ ||
    56  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
    57  		js.ThrowInvalidCallbackInvocation()
    58  	}
    59  
    60  	if ctx.Return(cb.Fn(
    61  		cb.Arg,
    62  		args[0],
    63  
    64  		js.Object{}.FromRef(args[0+1]),
    65  	)) {
    66  		return
    67  	}
    68  
    69  	js.ThrowCallbackValueNotReturned()
    70  }
    71  
    72  type RemoveCallbackFunc func(this js.Ref) js.Ref
    73  
    74  func (fn RemoveCallbackFunc) Register() js.Func[func()] {
    75  	return js.RegisterCallback[func()](
    76  		fn, abi.FuncPCABIInternal(fn),
    77  	)
    78  }
    79  
    80  func (fn RemoveCallbackFunc) DispatchCallback(
    81  	targetPC uintptr, ctx *js.CallbackContext,
    82  ) {
    83  	args := ctx.Args()
    84  	if len(args) != 0+1 /* js this */ ||
    85  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
    86  		js.ThrowInvalidCallbackInvocation()
    87  	}
    88  
    89  	if ctx.Return(fn(
    90  		args[0],
    91  	)) {
    92  		return
    93  	}
    94  
    95  	js.ThrowCallbackValueNotReturned()
    96  }
    97  
    98  type RemoveCallback[T any] struct {
    99  	Fn  func(arg T, this js.Ref) js.Ref
   100  	Arg T
   101  }
   102  
   103  func (cb *RemoveCallback[T]) Register() js.Func[func()] {
   104  	return js.RegisterCallback[func()](
   105  		cb, abi.FuncPCABIInternal(cb.Fn),
   106  	)
   107  }
   108  
   109  func (cb *RemoveCallback[T]) DispatchCallback(
   110  	targetPC uintptr, ctx *js.CallbackContext,
   111  ) {
   112  	args := ctx.Args()
   113  	if len(args) != 0+1 /* js this */ ||
   114  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   115  		js.ThrowInvalidCallbackInvocation()
   116  	}
   117  
   118  	if ctx.Return(cb.Fn(
   119  		cb.Arg,
   120  		args[0],
   121  	)) {
   122  		return
   123  	}
   124  
   125  	js.ThrowCallbackValueNotReturned()
   126  }
   127  
   128  type SetCallbackFunc func(this js.Ref) js.Ref
   129  
   130  func (fn SetCallbackFunc) Register() js.Func[func()] {
   131  	return js.RegisterCallback[func()](
   132  		fn, abi.FuncPCABIInternal(fn),
   133  	)
   134  }
   135  
   136  func (fn SetCallbackFunc) DispatchCallback(
   137  	targetPC uintptr, ctx *js.CallbackContext,
   138  ) {
   139  	args := ctx.Args()
   140  	if len(args) != 0+1 /* js this */ ||
   141  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
   142  		js.ThrowInvalidCallbackInvocation()
   143  	}
   144  
   145  	if ctx.Return(fn(
   146  		args[0],
   147  	)) {
   148  		return
   149  	}
   150  
   151  	js.ThrowCallbackValueNotReturned()
   152  }
   153  
   154  type SetCallback[T any] struct {
   155  	Fn  func(arg T, this js.Ref) js.Ref
   156  	Arg T
   157  }
   158  
   159  func (cb *SetCallback[T]) Register() js.Func[func()] {
   160  	return js.RegisterCallback[func()](
   161  		cb, abi.FuncPCABIInternal(cb.Fn),
   162  	)
   163  }
   164  
   165  func (cb *SetCallback[T]) DispatchCallback(
   166  	targetPC uintptr, ctx *js.CallbackContext,
   167  ) {
   168  	args := ctx.Args()
   169  	if len(args) != 0+1 /* js this */ ||
   170  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   171  		js.ThrowInvalidCallbackInvocation()
   172  	}
   173  
   174  	if ctx.Return(cb.Fn(
   175  		cb.Arg,
   176  		args[0],
   177  	)) {
   178  		return
   179  	}
   180  
   181  	js.ThrowCallbackValueNotReturned()
   182  }
   183  
   184  // HasFuncGet returns true if the function "WEBEXT.sharedStoragePrivate.get" exists.
   185  func HasFuncGet() bool {
   186  	return js.True == bindings.HasFuncGet()
   187  }
   188  
   189  // FuncGet returns the function "WEBEXT.sharedStoragePrivate.get".
   190  func FuncGet() (fn js.Func[func() js.Promise[js.Object]]) {
   191  	bindings.FuncGet(
   192  		js.Pointer(&fn),
   193  	)
   194  	return
   195  }
   196  
   197  // Get calls the function "WEBEXT.sharedStoragePrivate.get" directly.
   198  func Get() (ret js.Promise[js.Object]) {
   199  	bindings.CallGet(
   200  		js.Pointer(&ret),
   201  	)
   202  
   203  	return
   204  }
   205  
   206  // TryGet calls the function "WEBEXT.sharedStoragePrivate.get"
   207  // in a try/catch block and returns (_, err, ok = false) when it went through
   208  // the catch clause.
   209  func TryGet() (ret js.Promise[js.Object], exception js.Any, ok bool) {
   210  	ok = js.True == bindings.TryGet(
   211  		js.Pointer(&ret), js.Pointer(&exception),
   212  	)
   213  
   214  	return
   215  }
   216  
   217  // HasFuncRemove returns true if the function "WEBEXT.sharedStoragePrivate.remove" exists.
   218  func HasFuncRemove() bool {
   219  	return js.True == bindings.HasFuncRemove()
   220  }
   221  
   222  // FuncRemove returns the function "WEBEXT.sharedStoragePrivate.remove".
   223  func FuncRemove() (fn js.Func[func(keys js.Array[js.String]) js.Promise[js.Void]]) {
   224  	bindings.FuncRemove(
   225  		js.Pointer(&fn),
   226  	)
   227  	return
   228  }
   229  
   230  // Remove calls the function "WEBEXT.sharedStoragePrivate.remove" directly.
   231  func Remove(keys js.Array[js.String]) (ret js.Promise[js.Void]) {
   232  	bindings.CallRemove(
   233  		js.Pointer(&ret),
   234  		keys.Ref(),
   235  	)
   236  
   237  	return
   238  }
   239  
   240  // TryRemove calls the function "WEBEXT.sharedStoragePrivate.remove"
   241  // in a try/catch block and returns (_, err, ok = false) when it went through
   242  // the catch clause.
   243  func TryRemove(keys js.Array[js.String]) (ret js.Promise[js.Void], exception js.Any, ok bool) {
   244  	ok = js.True == bindings.TryRemove(
   245  		js.Pointer(&ret), js.Pointer(&exception),
   246  		keys.Ref(),
   247  	)
   248  
   249  	return
   250  }
   251  
   252  // HasFuncSet returns true if the function "WEBEXT.sharedStoragePrivate.set" exists.
   253  func HasFuncSet() bool {
   254  	return js.True == bindings.HasFuncSet()
   255  }
   256  
   257  // FuncSet returns the function "WEBEXT.sharedStoragePrivate.set".
   258  func FuncSet() (fn js.Func[func(items js.Object) js.Promise[js.Void]]) {
   259  	bindings.FuncSet(
   260  		js.Pointer(&fn),
   261  	)
   262  	return
   263  }
   264  
   265  // Set calls the function "WEBEXT.sharedStoragePrivate.set" directly.
   266  func Set(items js.Object) (ret js.Promise[js.Void]) {
   267  	bindings.CallSet(
   268  		js.Pointer(&ret),
   269  		items.Ref(),
   270  	)
   271  
   272  	return
   273  }
   274  
   275  // TrySet calls the function "WEBEXT.sharedStoragePrivate.set"
   276  // in a try/catch block and returns (_, err, ok = false) when it went through
   277  // the catch clause.
   278  func TrySet(items js.Object) (ret js.Promise[js.Void], exception js.Any, ok bool) {
   279  	ok = js.True == bindings.TrySet(
   280  		js.Pointer(&ret), js.Pointer(&exception),
   281  		items.Ref(),
   282  	)
   283  
   284  	return
   285  }