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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package offscreen
     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/offscreen/bindings"
    10  )
    11  
    12  type BooleanCallbackFunc func(this js.Ref, result bool) js.Ref
    13  
    14  func (fn BooleanCallbackFunc) Register() js.Func[func(result bool)] {
    15  	return js.RegisterCallback[func(result bool)](
    16  		fn, abi.FuncPCABIInternal(fn),
    17  	)
    18  }
    19  
    20  func (fn BooleanCallbackFunc) 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  		args[0+1] == js.True,
    33  	)) {
    34  		return
    35  	}
    36  
    37  	js.ThrowCallbackValueNotReturned()
    38  }
    39  
    40  type BooleanCallback[T any] struct {
    41  	Fn  func(arg T, this js.Ref, result bool) js.Ref
    42  	Arg T
    43  }
    44  
    45  func (cb *BooleanCallback[T]) Register() js.Func[func(result bool)] {
    46  	return js.RegisterCallback[func(result bool)](
    47  		cb, abi.FuncPCABIInternal(cb.Fn),
    48  	)
    49  }
    50  
    51  func (cb *BooleanCallback[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  		args[0+1] == js.True,
    65  	)) {
    66  		return
    67  	}
    68  
    69  	js.ThrowCallbackValueNotReturned()
    70  }
    71  
    72  type Reason uint32
    73  
    74  const (
    75  	_ Reason = iota
    76  
    77  	Reason_TESTING
    78  	Reason_AUDIO_PLAYBACK
    79  	Reason_IFRAME_SCRIPTING
    80  	Reason_DOM_SCRAPING
    81  	Reason_BLOBS
    82  	Reason_DOM_PARSER
    83  	Reason_USER_MEDIA
    84  	Reason_DISPLAY_MEDIA
    85  	Reason_WEB_RTC
    86  	Reason_CLIPBOARD
    87  	Reason_LOCAL_STORAGE
    88  	Reason_WORKERS
    89  	Reason_BATTERY_STATUS
    90  	Reason_MATCH_MEDIA
    91  	Reason_GEOLOCATION
    92  )
    93  
    94  func (Reason) FromRef(str js.Ref) Reason {
    95  	return Reason(bindings.ConstOfReason(str))
    96  }
    97  
    98  func (x Reason) String() (string, bool) {
    99  	switch x {
   100  	case Reason_TESTING:
   101  		return "TESTING", true
   102  	case Reason_AUDIO_PLAYBACK:
   103  		return "AUDIO_PLAYBACK", true
   104  	case Reason_IFRAME_SCRIPTING:
   105  		return "IFRAME_SCRIPTING", true
   106  	case Reason_DOM_SCRAPING:
   107  		return "DOM_SCRAPING", true
   108  	case Reason_BLOBS:
   109  		return "BLOBS", true
   110  	case Reason_DOM_PARSER:
   111  		return "DOM_PARSER", true
   112  	case Reason_USER_MEDIA:
   113  		return "USER_MEDIA", true
   114  	case Reason_DISPLAY_MEDIA:
   115  		return "DISPLAY_MEDIA", true
   116  	case Reason_WEB_RTC:
   117  		return "WEB_RTC", true
   118  	case Reason_CLIPBOARD:
   119  		return "CLIPBOARD", true
   120  	case Reason_LOCAL_STORAGE:
   121  		return "LOCAL_STORAGE", true
   122  	case Reason_WORKERS:
   123  		return "WORKERS", true
   124  	case Reason_BATTERY_STATUS:
   125  		return "BATTERY_STATUS", true
   126  	case Reason_MATCH_MEDIA:
   127  		return "MATCH_MEDIA", true
   128  	case Reason_GEOLOCATION:
   129  		return "GEOLOCATION", true
   130  	default:
   131  		return "", false
   132  	}
   133  }
   134  
   135  type CreateParameters struct {
   136  	// Reasons is "CreateParameters.reasons"
   137  	//
   138  	// Optional
   139  	Reasons js.Array[Reason]
   140  	// Url is "CreateParameters.url"
   141  	//
   142  	// Optional
   143  	Url js.String
   144  	// Justification is "CreateParameters.justification"
   145  	//
   146  	// Optional
   147  	Justification js.String
   148  
   149  	FFI_USE bool
   150  }
   151  
   152  // FromRef calls UpdateFrom and returns a CreateParameters with all fields set.
   153  func (p CreateParameters) FromRef(ref js.Ref) CreateParameters {
   154  	p.UpdateFrom(ref)
   155  	return p
   156  }
   157  
   158  // New creates a new CreateParameters in the application heap.
   159  func (p CreateParameters) New() js.Ref {
   160  	return bindings.CreateParametersJSLoad(
   161  		js.Pointer(&p), js.True, 0,
   162  	)
   163  }
   164  
   165  // UpdateFrom copies value of all fields of the heap object to p.
   166  func (p *CreateParameters) UpdateFrom(ref js.Ref) {
   167  	bindings.CreateParametersJSStore(
   168  		js.Pointer(p), ref,
   169  	)
   170  }
   171  
   172  // Update writes all fields of the p to the heap object referenced by ref.
   173  func (p *CreateParameters) Update(ref js.Ref) {
   174  	bindings.CreateParametersJSLoad(
   175  		js.Pointer(p), js.False, ref,
   176  	)
   177  }
   178  
   179  // FreeMembers frees fields with heap reference, if recursive is true
   180  // free all heap references reachable from p.
   181  func (p *CreateParameters) FreeMembers(recursive bool) {
   182  	js.Free(
   183  		p.Reasons.Ref(),
   184  		p.Url.Ref(),
   185  		p.Justification.Ref(),
   186  	)
   187  	p.Reasons = p.Reasons.FromRef(js.Undefined)
   188  	p.Url = p.Url.FromRef(js.Undefined)
   189  	p.Justification = p.Justification.FromRef(js.Undefined)
   190  }
   191  
   192  type VoidCallbackFunc func(this js.Ref) js.Ref
   193  
   194  func (fn VoidCallbackFunc) Register() js.Func[func()] {
   195  	return js.RegisterCallback[func()](
   196  		fn, abi.FuncPCABIInternal(fn),
   197  	)
   198  }
   199  
   200  func (fn VoidCallbackFunc) DispatchCallback(
   201  	targetPC uintptr, ctx *js.CallbackContext,
   202  ) {
   203  	args := ctx.Args()
   204  	if len(args) != 0+1 /* js this */ ||
   205  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
   206  		js.ThrowInvalidCallbackInvocation()
   207  	}
   208  
   209  	if ctx.Return(fn(
   210  		args[0],
   211  	)) {
   212  		return
   213  	}
   214  
   215  	js.ThrowCallbackValueNotReturned()
   216  }
   217  
   218  type VoidCallback[T any] struct {
   219  	Fn  func(arg T, this js.Ref) js.Ref
   220  	Arg T
   221  }
   222  
   223  func (cb *VoidCallback[T]) Register() js.Func[func()] {
   224  	return js.RegisterCallback[func()](
   225  		cb, abi.FuncPCABIInternal(cb.Fn),
   226  	)
   227  }
   228  
   229  func (cb *VoidCallback[T]) DispatchCallback(
   230  	targetPC uintptr, ctx *js.CallbackContext,
   231  ) {
   232  	args := ctx.Args()
   233  	if len(args) != 0+1 /* js this */ ||
   234  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   235  		js.ThrowInvalidCallbackInvocation()
   236  	}
   237  
   238  	if ctx.Return(cb.Fn(
   239  		cb.Arg,
   240  		args[0],
   241  	)) {
   242  		return
   243  	}
   244  
   245  	js.ThrowCallbackValueNotReturned()
   246  }
   247  
   248  // HasFuncCloseDocument returns true if the function "WEBEXT.offscreen.closeDocument" exists.
   249  func HasFuncCloseDocument() bool {
   250  	return js.True == bindings.HasFuncCloseDocument()
   251  }
   252  
   253  // FuncCloseDocument returns the function "WEBEXT.offscreen.closeDocument".
   254  func FuncCloseDocument() (fn js.Func[func() js.Promise[js.Void]]) {
   255  	bindings.FuncCloseDocument(
   256  		js.Pointer(&fn),
   257  	)
   258  	return
   259  }
   260  
   261  // CloseDocument calls the function "WEBEXT.offscreen.closeDocument" directly.
   262  func CloseDocument() (ret js.Promise[js.Void]) {
   263  	bindings.CallCloseDocument(
   264  		js.Pointer(&ret),
   265  	)
   266  
   267  	return
   268  }
   269  
   270  // TryCloseDocument calls the function "WEBEXT.offscreen.closeDocument"
   271  // in a try/catch block and returns (_, err, ok = false) when it went through
   272  // the catch clause.
   273  func TryCloseDocument() (ret js.Promise[js.Void], exception js.Any, ok bool) {
   274  	ok = js.True == bindings.TryCloseDocument(
   275  		js.Pointer(&ret), js.Pointer(&exception),
   276  	)
   277  
   278  	return
   279  }
   280  
   281  // HasFuncCreateDocument returns true if the function "WEBEXT.offscreen.createDocument" exists.
   282  func HasFuncCreateDocument() bool {
   283  	return js.True == bindings.HasFuncCreateDocument()
   284  }
   285  
   286  // FuncCreateDocument returns the function "WEBEXT.offscreen.createDocument".
   287  func FuncCreateDocument() (fn js.Func[func(parameters CreateParameters) js.Promise[js.Void]]) {
   288  	bindings.FuncCreateDocument(
   289  		js.Pointer(&fn),
   290  	)
   291  	return
   292  }
   293  
   294  // CreateDocument calls the function "WEBEXT.offscreen.createDocument" directly.
   295  func CreateDocument(parameters CreateParameters) (ret js.Promise[js.Void]) {
   296  	bindings.CallCreateDocument(
   297  		js.Pointer(&ret),
   298  		js.Pointer(&parameters),
   299  	)
   300  
   301  	return
   302  }
   303  
   304  // TryCreateDocument calls the function "WEBEXT.offscreen.createDocument"
   305  // in a try/catch block and returns (_, err, ok = false) when it went through
   306  // the catch clause.
   307  func TryCreateDocument(parameters CreateParameters) (ret js.Promise[js.Void], exception js.Any, ok bool) {
   308  	ok = js.True == bindings.TryCreateDocument(
   309  		js.Pointer(&ret), js.Pointer(&exception),
   310  		js.Pointer(&parameters),
   311  	)
   312  
   313  	return
   314  }
   315  
   316  // HasFuncHasDocument returns true if the function "WEBEXT.offscreen.hasDocument" exists.
   317  func HasFuncHasDocument() bool {
   318  	return js.True == bindings.HasFuncHasDocument()
   319  }
   320  
   321  // FuncHasDocument returns the function "WEBEXT.offscreen.hasDocument".
   322  func FuncHasDocument() (fn js.Func[func() js.Promise[js.Boolean]]) {
   323  	bindings.FuncHasDocument(
   324  		js.Pointer(&fn),
   325  	)
   326  	return
   327  }
   328  
   329  // HasDocument calls the function "WEBEXT.offscreen.hasDocument" directly.
   330  func HasDocument() (ret js.Promise[js.Boolean]) {
   331  	bindings.CallHasDocument(
   332  		js.Pointer(&ret),
   333  	)
   334  
   335  	return
   336  }
   337  
   338  // TryHasDocument calls the function "WEBEXT.offscreen.hasDocument"
   339  // in a try/catch block and returns (_, err, ok = false) when it went through
   340  // the catch clause.
   341  func TryHasDocument() (ret js.Promise[js.Boolean], exception js.Any, ok bool) {
   342  	ok = js.True == bindings.TryHasDocument(
   343  		js.Pointer(&ret), js.Pointer(&exception),
   344  	)
   345  
   346  	return
   347  }