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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package commands
     5  
     6  import (
     7  	"github.com/primecitizens/pcz/std/core/abi"
     8  	"github.com/primecitizens/pcz/std/core/mark"
     9  	"github.com/primecitizens/pcz/std/ffi/js"
    10  	"github.com/primecitizens/pcz/std/plat/js/webext/commands/bindings"
    11  	"github.com/primecitizens/pcz/std/plat/js/webext/tabs"
    12  )
    13  
    14  type Command struct {
    15  	// Description is "Command.description"
    16  	//
    17  	// Optional
    18  	Description js.String
    19  	// Name is "Command.name"
    20  	//
    21  	// Optional
    22  	Name js.String
    23  	// Shortcut is "Command.shortcut"
    24  	//
    25  	// Optional
    26  	Shortcut js.String
    27  
    28  	FFI_USE bool
    29  }
    30  
    31  // FromRef calls UpdateFrom and returns a Command with all fields set.
    32  func (p Command) FromRef(ref js.Ref) Command {
    33  	p.UpdateFrom(ref)
    34  	return p
    35  }
    36  
    37  // New creates a new Command in the application heap.
    38  func (p Command) New() js.Ref {
    39  	return bindings.CommandJSLoad(
    40  		js.Pointer(&p), js.True, 0,
    41  	)
    42  }
    43  
    44  // UpdateFrom copies value of all fields of the heap object to p.
    45  func (p *Command) UpdateFrom(ref js.Ref) {
    46  	bindings.CommandJSStore(
    47  		js.Pointer(p), ref,
    48  	)
    49  }
    50  
    51  // Update writes all fields of the p to the heap object referenced by ref.
    52  func (p *Command) Update(ref js.Ref) {
    53  	bindings.CommandJSLoad(
    54  		js.Pointer(p), js.False, ref,
    55  	)
    56  }
    57  
    58  // FreeMembers frees fields with heap reference, if recursive is true
    59  // free all heap references reachable from p.
    60  func (p *Command) FreeMembers(recursive bool) {
    61  	js.Free(
    62  		p.Description.Ref(),
    63  		p.Name.Ref(),
    64  		p.Shortcut.Ref(),
    65  	)
    66  	p.Description = p.Description.FromRef(js.Undefined)
    67  	p.Name = p.Name.FromRef(js.Undefined)
    68  	p.Shortcut = p.Shortcut.FromRef(js.Undefined)
    69  }
    70  
    71  // HasFuncGetAll returns true if the function "WEBEXT.commands.getAll" exists.
    72  func HasFuncGetAll() bool {
    73  	return js.True == bindings.HasFuncGetAll()
    74  }
    75  
    76  // FuncGetAll returns the function "WEBEXT.commands.getAll".
    77  func FuncGetAll() (fn js.Func[func() js.Promise[js.Array[Command]]]) {
    78  	bindings.FuncGetAll(
    79  		js.Pointer(&fn),
    80  	)
    81  	return
    82  }
    83  
    84  // GetAll calls the function "WEBEXT.commands.getAll" directly.
    85  func GetAll() (ret js.Promise[js.Array[Command]]) {
    86  	bindings.CallGetAll(
    87  		js.Pointer(&ret),
    88  	)
    89  
    90  	return
    91  }
    92  
    93  // TryGetAll calls the function "WEBEXT.commands.getAll"
    94  // in a try/catch block and returns (_, err, ok = false) when it went through
    95  // the catch clause.
    96  func TryGetAll() (ret js.Promise[js.Array[Command]], exception js.Any, ok bool) {
    97  	ok = js.True == bindings.TryGetAll(
    98  		js.Pointer(&ret), js.Pointer(&exception),
    99  	)
   100  
   101  	return
   102  }
   103  
   104  type OnCommandEventCallbackFunc func(this js.Ref, command js.String, tab *tabs.Tab) js.Ref
   105  
   106  func (fn OnCommandEventCallbackFunc) Register() js.Func[func(command js.String, tab *tabs.Tab)] {
   107  	return js.RegisterCallback[func(command js.String, tab *tabs.Tab)](
   108  		fn, abi.FuncPCABIInternal(fn),
   109  	)
   110  }
   111  
   112  func (fn OnCommandEventCallbackFunc) DispatchCallback(
   113  	targetPC uintptr, ctx *js.CallbackContext,
   114  ) {
   115  	args := ctx.Args()
   116  	if len(args) != 2+1 /* js this */ ||
   117  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
   118  		js.ThrowInvalidCallbackInvocation()
   119  	}
   120  	var arg1 tabs.Tab
   121  	arg1.UpdateFrom(args[1+1])
   122  	defer arg1.FreeMembers(true)
   123  
   124  	if ctx.Return(fn(
   125  		args[0],
   126  
   127  		js.String{}.FromRef(args[0+1]),
   128  		mark.NoEscape(&arg1),
   129  	)) {
   130  		return
   131  	}
   132  
   133  	js.ThrowCallbackValueNotReturned()
   134  }
   135  
   136  type OnCommandEventCallback[T any] struct {
   137  	Fn  func(arg T, this js.Ref, command js.String, tab *tabs.Tab) js.Ref
   138  	Arg T
   139  }
   140  
   141  func (cb *OnCommandEventCallback[T]) Register() js.Func[func(command js.String, tab *tabs.Tab)] {
   142  	return js.RegisterCallback[func(command js.String, tab *tabs.Tab)](
   143  		cb, abi.FuncPCABIInternal(cb.Fn),
   144  	)
   145  }
   146  
   147  func (cb *OnCommandEventCallback[T]) DispatchCallback(
   148  	targetPC uintptr, ctx *js.CallbackContext,
   149  ) {
   150  	args := ctx.Args()
   151  	if len(args) != 2+1 /* js this */ ||
   152  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   153  		js.ThrowInvalidCallbackInvocation()
   154  	}
   155  	var arg1 tabs.Tab
   156  	arg1.UpdateFrom(args[1+1])
   157  	defer arg1.FreeMembers(true)
   158  
   159  	if ctx.Return(cb.Fn(
   160  		cb.Arg,
   161  		args[0],
   162  
   163  		js.String{}.FromRef(args[0+1]),
   164  		mark.NoEscape(&arg1),
   165  	)) {
   166  		return
   167  	}
   168  
   169  	js.ThrowCallbackValueNotReturned()
   170  }
   171  
   172  // HasFuncOnCommand returns true if the function "WEBEXT.commands.onCommand.addListener" exists.
   173  func HasFuncOnCommand() bool {
   174  	return js.True == bindings.HasFuncOnCommand()
   175  }
   176  
   177  // FuncOnCommand returns the function "WEBEXT.commands.onCommand.addListener".
   178  func FuncOnCommand() (fn js.Func[func(callback js.Func[func(command js.String, tab *tabs.Tab)])]) {
   179  	bindings.FuncOnCommand(
   180  		js.Pointer(&fn),
   181  	)
   182  	return
   183  }
   184  
   185  // OnCommand calls the function "WEBEXT.commands.onCommand.addListener" directly.
   186  func OnCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret js.Void) {
   187  	bindings.CallOnCommand(
   188  		js.Pointer(&ret),
   189  		callback.Ref(),
   190  	)
   191  
   192  	return
   193  }
   194  
   195  // TryOnCommand calls the function "WEBEXT.commands.onCommand.addListener"
   196  // in a try/catch block and returns (_, err, ok = false) when it went through
   197  // the catch clause.
   198  func TryOnCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret js.Void, exception js.Any, ok bool) {
   199  	ok = js.True == bindings.TryOnCommand(
   200  		js.Pointer(&ret), js.Pointer(&exception),
   201  		callback.Ref(),
   202  	)
   203  
   204  	return
   205  }
   206  
   207  // HasFuncOffCommand returns true if the function "WEBEXT.commands.onCommand.removeListener" exists.
   208  func HasFuncOffCommand() bool {
   209  	return js.True == bindings.HasFuncOffCommand()
   210  }
   211  
   212  // FuncOffCommand returns the function "WEBEXT.commands.onCommand.removeListener".
   213  func FuncOffCommand() (fn js.Func[func(callback js.Func[func(command js.String, tab *tabs.Tab)])]) {
   214  	bindings.FuncOffCommand(
   215  		js.Pointer(&fn),
   216  	)
   217  	return
   218  }
   219  
   220  // OffCommand calls the function "WEBEXT.commands.onCommand.removeListener" directly.
   221  func OffCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret js.Void) {
   222  	bindings.CallOffCommand(
   223  		js.Pointer(&ret),
   224  		callback.Ref(),
   225  	)
   226  
   227  	return
   228  }
   229  
   230  // TryOffCommand calls the function "WEBEXT.commands.onCommand.removeListener"
   231  // in a try/catch block and returns (_, err, ok = false) when it went through
   232  // the catch clause.
   233  func TryOffCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret js.Void, exception js.Any, ok bool) {
   234  	ok = js.True == bindings.TryOffCommand(
   235  		js.Pointer(&ret), js.Pointer(&exception),
   236  		callback.Ref(),
   237  	)
   238  
   239  	return
   240  }
   241  
   242  // HasFuncHasOnCommand returns true if the function "WEBEXT.commands.onCommand.hasListener" exists.
   243  func HasFuncHasOnCommand() bool {
   244  	return js.True == bindings.HasFuncHasOnCommand()
   245  }
   246  
   247  // FuncHasOnCommand returns the function "WEBEXT.commands.onCommand.hasListener".
   248  func FuncHasOnCommand() (fn js.Func[func(callback js.Func[func(command js.String, tab *tabs.Tab)]) bool]) {
   249  	bindings.FuncHasOnCommand(
   250  		js.Pointer(&fn),
   251  	)
   252  	return
   253  }
   254  
   255  // HasOnCommand calls the function "WEBEXT.commands.onCommand.hasListener" directly.
   256  func HasOnCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret bool) {
   257  	bindings.CallHasOnCommand(
   258  		js.Pointer(&ret),
   259  		callback.Ref(),
   260  	)
   261  
   262  	return
   263  }
   264  
   265  // TryHasOnCommand calls the function "WEBEXT.commands.onCommand.hasListener"
   266  // in a try/catch block and returns (_, err, ok = false) when it went through
   267  // the catch clause.
   268  func TryHasOnCommand(callback js.Func[func(command js.String, tab *tabs.Tab)]) (ret bool, exception js.Any, ok bool) {
   269  	ok = js.True == bindings.TryHasOnCommand(
   270  		js.Pointer(&ret), js.Pointer(&exception),
   271  		callback.Ref(),
   272  	)
   273  
   274  	return
   275  }