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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package platformkeysinternal
     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/enterprise/platformkeysinternal/bindings"
    10  )
    11  
    12  type Hash struct {
    13  	// Name is "Hash.name"
    14  	//
    15  	// Optional
    16  	Name js.String
    17  
    18  	FFI_USE bool
    19  }
    20  
    21  // FromRef calls UpdateFrom and returns a Hash with all fields set.
    22  func (p Hash) FromRef(ref js.Ref) Hash {
    23  	p.UpdateFrom(ref)
    24  	return p
    25  }
    26  
    27  // New creates a new Hash in the application heap.
    28  func (p Hash) New() js.Ref {
    29  	return bindings.HashJSLoad(
    30  		js.Pointer(&p), js.True, 0,
    31  	)
    32  }
    33  
    34  // UpdateFrom copies value of all fields of the heap object to p.
    35  func (p *Hash) UpdateFrom(ref js.Ref) {
    36  	bindings.HashJSStore(
    37  		js.Pointer(p), ref,
    38  	)
    39  }
    40  
    41  // Update writes all fields of the p to the heap object referenced by ref.
    42  func (p *Hash) Update(ref js.Ref) {
    43  	bindings.HashJSLoad(
    44  		js.Pointer(p), js.False, ref,
    45  	)
    46  }
    47  
    48  // FreeMembers frees fields with heap reference, if recursive is true
    49  // free all heap references reachable from p.
    50  func (p *Hash) FreeMembers(recursive bool) {
    51  	js.Free(
    52  		p.Name.Ref(),
    53  	)
    54  	p.Name = p.Name.FromRef(js.Undefined)
    55  }
    56  
    57  type Algorithm struct {
    58  	// Name is "Algorithm.name"
    59  	//
    60  	// Optional
    61  	Name js.String
    62  	// ModulusLength is "Algorithm.modulusLength"
    63  	//
    64  	// Optional
    65  	//
    66  	// NOTE: FFI_USE_ModulusLength MUST be set to true to make this field effective.
    67  	ModulusLength int32
    68  	// PublicExponent is "Algorithm.publicExponent"
    69  	//
    70  	// Optional
    71  	PublicExponent js.ArrayBuffer
    72  	// Hash is "Algorithm.hash"
    73  	//
    74  	// Optional
    75  	//
    76  	// NOTE: Hash.FFI_USE MUST be set to true to get Hash used.
    77  	Hash Hash
    78  	// NamedCurve is "Algorithm.namedCurve"
    79  	//
    80  	// Optional
    81  	NamedCurve js.String
    82  
    83  	FFI_USE_ModulusLength bool // for ModulusLength.
    84  
    85  	FFI_USE bool
    86  }
    87  
    88  // FromRef calls UpdateFrom and returns a Algorithm with all fields set.
    89  func (p Algorithm) FromRef(ref js.Ref) Algorithm {
    90  	p.UpdateFrom(ref)
    91  	return p
    92  }
    93  
    94  // New creates a new Algorithm in the application heap.
    95  func (p Algorithm) New() js.Ref {
    96  	return bindings.AlgorithmJSLoad(
    97  		js.Pointer(&p), js.True, 0,
    98  	)
    99  }
   100  
   101  // UpdateFrom copies value of all fields of the heap object to p.
   102  func (p *Algorithm) UpdateFrom(ref js.Ref) {
   103  	bindings.AlgorithmJSStore(
   104  		js.Pointer(p), ref,
   105  	)
   106  }
   107  
   108  // Update writes all fields of the p to the heap object referenced by ref.
   109  func (p *Algorithm) Update(ref js.Ref) {
   110  	bindings.AlgorithmJSLoad(
   111  		js.Pointer(p), js.False, ref,
   112  	)
   113  }
   114  
   115  // FreeMembers frees fields with heap reference, if recursive is true
   116  // free all heap references reachable from p.
   117  func (p *Algorithm) FreeMembers(recursive bool) {
   118  	js.Free(
   119  		p.Name.Ref(),
   120  		p.PublicExponent.Ref(),
   121  		p.NamedCurve.Ref(),
   122  	)
   123  	p.Name = p.Name.FromRef(js.Undefined)
   124  	p.PublicExponent = p.PublicExponent.FromRef(js.Undefined)
   125  	p.NamedCurve = p.NamedCurve.FromRef(js.Undefined)
   126  	if recursive {
   127  		p.Hash.FreeMembers(true)
   128  	}
   129  }
   130  
   131  type GenerateKeyCallbackFunc func(this js.Ref, publicKey js.ArrayBuffer) js.Ref
   132  
   133  func (fn GenerateKeyCallbackFunc) Register() js.Func[func(publicKey js.ArrayBuffer)] {
   134  	return js.RegisterCallback[func(publicKey js.ArrayBuffer)](
   135  		fn, abi.FuncPCABIInternal(fn),
   136  	)
   137  }
   138  
   139  func (fn GenerateKeyCallbackFunc) DispatchCallback(
   140  	targetPC uintptr, ctx *js.CallbackContext,
   141  ) {
   142  	args := ctx.Args()
   143  	if len(args) != 1+1 /* js this */ ||
   144  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
   145  		js.ThrowInvalidCallbackInvocation()
   146  	}
   147  
   148  	if ctx.Return(fn(
   149  		args[0],
   150  
   151  		js.ArrayBuffer{}.FromRef(args[0+1]),
   152  	)) {
   153  		return
   154  	}
   155  
   156  	js.ThrowCallbackValueNotReturned()
   157  }
   158  
   159  type GenerateKeyCallback[T any] struct {
   160  	Fn  func(arg T, this js.Ref, publicKey js.ArrayBuffer) js.Ref
   161  	Arg T
   162  }
   163  
   164  func (cb *GenerateKeyCallback[T]) Register() js.Func[func(publicKey js.ArrayBuffer)] {
   165  	return js.RegisterCallback[func(publicKey js.ArrayBuffer)](
   166  		cb, abi.FuncPCABIInternal(cb.Fn),
   167  	)
   168  }
   169  
   170  func (cb *GenerateKeyCallback[T]) DispatchCallback(
   171  	targetPC uintptr, ctx *js.CallbackContext,
   172  ) {
   173  	args := ctx.Args()
   174  	if len(args) != 1+1 /* js this */ ||
   175  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   176  		js.ThrowInvalidCallbackInvocation()
   177  	}
   178  
   179  	if ctx.Return(cb.Fn(
   180  		cb.Arg,
   181  		args[0],
   182  
   183  		js.ArrayBuffer{}.FromRef(args[0+1]),
   184  	)) {
   185  		return
   186  	}
   187  
   188  	js.ThrowCallbackValueNotReturned()
   189  }
   190  
   191  type GetTokensCallbackFunc func(this js.Ref, tokenIds js.Array[js.String]) js.Ref
   192  
   193  func (fn GetTokensCallbackFunc) Register() js.Func[func(tokenIds js.Array[js.String])] {
   194  	return js.RegisterCallback[func(tokenIds js.Array[js.String])](
   195  		fn, abi.FuncPCABIInternal(fn),
   196  	)
   197  }
   198  
   199  func (fn GetTokensCallbackFunc) DispatchCallback(
   200  	targetPC uintptr, ctx *js.CallbackContext,
   201  ) {
   202  	args := ctx.Args()
   203  	if len(args) != 1+1 /* js this */ ||
   204  		targetPC != uintptr(abi.FuncPCABIInternal(fn)) {
   205  		js.ThrowInvalidCallbackInvocation()
   206  	}
   207  
   208  	if ctx.Return(fn(
   209  		args[0],
   210  
   211  		js.Array[js.String]{}.FromRef(args[0+1]),
   212  	)) {
   213  		return
   214  	}
   215  
   216  	js.ThrowCallbackValueNotReturned()
   217  }
   218  
   219  type GetTokensCallback[T any] struct {
   220  	Fn  func(arg T, this js.Ref, tokenIds js.Array[js.String]) js.Ref
   221  	Arg T
   222  }
   223  
   224  func (cb *GetTokensCallback[T]) Register() js.Func[func(tokenIds js.Array[js.String])] {
   225  	return js.RegisterCallback[func(tokenIds js.Array[js.String])](
   226  		cb, abi.FuncPCABIInternal(cb.Fn),
   227  	)
   228  }
   229  
   230  func (cb *GetTokensCallback[T]) DispatchCallback(
   231  	targetPC uintptr, ctx *js.CallbackContext,
   232  ) {
   233  	args := ctx.Args()
   234  	if len(args) != 1+1 /* js this */ ||
   235  		targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) {
   236  		js.ThrowInvalidCallbackInvocation()
   237  	}
   238  
   239  	if ctx.Return(cb.Fn(
   240  		cb.Arg,
   241  		args[0],
   242  
   243  		js.Array[js.String]{}.FromRef(args[0+1]),
   244  	)) {
   245  		return
   246  	}
   247  
   248  	js.ThrowCallbackValueNotReturned()
   249  }
   250  
   251  // HasFuncGenerateKey returns true if the function "WEBEXT.enterprise.platformKeysInternal.generateKey" exists.
   252  func HasFuncGenerateKey() bool {
   253  	return js.True == bindings.HasFuncGenerateKey()
   254  }
   255  
   256  // FuncGenerateKey returns the function "WEBEXT.enterprise.platformKeysInternal.generateKey".
   257  func FuncGenerateKey() (fn js.Func[func(tokenId js.String, algorithm Algorithm, softwareBacked bool, callback js.Func[func(publicKey js.ArrayBuffer)])]) {
   258  	bindings.FuncGenerateKey(
   259  		js.Pointer(&fn),
   260  	)
   261  	return
   262  }
   263  
   264  // GenerateKey calls the function "WEBEXT.enterprise.platformKeysInternal.generateKey" directly.
   265  func GenerateKey(tokenId js.String, algorithm Algorithm, softwareBacked bool, callback js.Func[func(publicKey js.ArrayBuffer)]) (ret js.Void) {
   266  	bindings.CallGenerateKey(
   267  		js.Pointer(&ret),
   268  		tokenId.Ref(),
   269  		js.Pointer(&algorithm),
   270  		js.Bool(bool(softwareBacked)),
   271  		callback.Ref(),
   272  	)
   273  
   274  	return
   275  }
   276  
   277  // TryGenerateKey calls the function "WEBEXT.enterprise.platformKeysInternal.generateKey"
   278  // in a try/catch block and returns (_, err, ok = false) when it went through
   279  // the catch clause.
   280  func TryGenerateKey(tokenId js.String, algorithm Algorithm, softwareBacked bool, callback js.Func[func(publicKey js.ArrayBuffer)]) (ret js.Void, exception js.Any, ok bool) {
   281  	ok = js.True == bindings.TryGenerateKey(
   282  		js.Pointer(&ret), js.Pointer(&exception),
   283  		tokenId.Ref(),
   284  		js.Pointer(&algorithm),
   285  		js.Bool(bool(softwareBacked)),
   286  		callback.Ref(),
   287  	)
   288  
   289  	return
   290  }
   291  
   292  // HasFuncGetTokens returns true if the function "WEBEXT.enterprise.platformKeysInternal.getTokens" exists.
   293  func HasFuncGetTokens() bool {
   294  	return js.True == bindings.HasFuncGetTokens()
   295  }
   296  
   297  // FuncGetTokens returns the function "WEBEXT.enterprise.platformKeysInternal.getTokens".
   298  func FuncGetTokens() (fn js.Func[func(callback js.Func[func(tokenIds js.Array[js.String])])]) {
   299  	bindings.FuncGetTokens(
   300  		js.Pointer(&fn),
   301  	)
   302  	return
   303  }
   304  
   305  // GetTokens calls the function "WEBEXT.enterprise.platformKeysInternal.getTokens" directly.
   306  func GetTokens(callback js.Func[func(tokenIds js.Array[js.String])]) (ret js.Void) {
   307  	bindings.CallGetTokens(
   308  		js.Pointer(&ret),
   309  		callback.Ref(),
   310  	)
   311  
   312  	return
   313  }
   314  
   315  // TryGetTokens calls the function "WEBEXT.enterprise.platformKeysInternal.getTokens"
   316  // in a try/catch block and returns (_, err, ok = false) when it went through
   317  // the catch clause.
   318  func TryGetTokens(callback js.Func[func(tokenIds js.Array[js.String])]) (ret js.Void, exception js.Any, ok bool) {
   319  	ok = js.True == bindings.TryGetTokens(
   320  		js.Pointer(&ret), js.Pointer(&exception),
   321  		callback.Ref(),
   322  	)
   323  
   324  	return
   325  }