github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/loginscreenui/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package loginscreenui 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/loginscreenui/bindings" 10 ) 11 12 type ShowOptions struct { 13 // Url is "ShowOptions.url" 14 // 15 // Optional 16 Url js.String 17 // UserCanClose is "ShowOptions.userCanClose" 18 // 19 // Optional 20 // 21 // NOTE: FFI_USE_UserCanClose MUST be set to true to make this field effective. 22 UserCanClose bool 23 24 FFI_USE_UserCanClose bool // for UserCanClose. 25 26 FFI_USE bool 27 } 28 29 // FromRef calls UpdateFrom and returns a ShowOptions with all fields set. 30 func (p ShowOptions) FromRef(ref js.Ref) ShowOptions { 31 p.UpdateFrom(ref) 32 return p 33 } 34 35 // New creates a new ShowOptions in the application heap. 36 func (p ShowOptions) New() js.Ref { 37 return bindings.ShowOptionsJSLoad( 38 js.Pointer(&p), js.True, 0, 39 ) 40 } 41 42 // UpdateFrom copies value of all fields of the heap object to p. 43 func (p *ShowOptions) UpdateFrom(ref js.Ref) { 44 bindings.ShowOptionsJSStore( 45 js.Pointer(p), ref, 46 ) 47 } 48 49 // Update writes all fields of the p to the heap object referenced by ref. 50 func (p *ShowOptions) Update(ref js.Ref) { 51 bindings.ShowOptionsJSLoad( 52 js.Pointer(p), js.False, ref, 53 ) 54 } 55 56 // FreeMembers frees fields with heap reference, if recursive is true 57 // free all heap references reachable from p. 58 func (p *ShowOptions) FreeMembers(recursive bool) { 59 js.Free( 60 p.Url.Ref(), 61 ) 62 p.Url = p.Url.FromRef(js.Undefined) 63 } 64 65 type SimpleCallbackFunc func(this js.Ref) js.Ref 66 67 func (fn SimpleCallbackFunc) Register() js.Func[func()] { 68 return js.RegisterCallback[func()]( 69 fn, abi.FuncPCABIInternal(fn), 70 ) 71 } 72 73 func (fn SimpleCallbackFunc) DispatchCallback( 74 targetPC uintptr, ctx *js.CallbackContext, 75 ) { 76 args := ctx.Args() 77 if len(args) != 0+1 /* js this */ || 78 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 79 js.ThrowInvalidCallbackInvocation() 80 } 81 82 if ctx.Return(fn( 83 args[0], 84 )) { 85 return 86 } 87 88 js.ThrowCallbackValueNotReturned() 89 } 90 91 type SimpleCallback[T any] struct { 92 Fn func(arg T, this js.Ref) js.Ref 93 Arg T 94 } 95 96 func (cb *SimpleCallback[T]) Register() js.Func[func()] { 97 return js.RegisterCallback[func()]( 98 cb, abi.FuncPCABIInternal(cb.Fn), 99 ) 100 } 101 102 func (cb *SimpleCallback[T]) DispatchCallback( 103 targetPC uintptr, ctx *js.CallbackContext, 104 ) { 105 args := ctx.Args() 106 if len(args) != 0+1 /* js this */ || 107 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 108 js.ThrowInvalidCallbackInvocation() 109 } 110 111 if ctx.Return(cb.Fn( 112 cb.Arg, 113 args[0], 114 )) { 115 return 116 } 117 118 js.ThrowCallbackValueNotReturned() 119 } 120 121 // HasFuncClose returns true if the function "WEBEXT.loginScreenUi.close" exists. 122 func HasFuncClose() bool { 123 return js.True == bindings.HasFuncClose() 124 } 125 126 // FuncClose returns the function "WEBEXT.loginScreenUi.close". 127 func FuncClose() (fn js.Func[func() js.Promise[js.Void]]) { 128 bindings.FuncClose( 129 js.Pointer(&fn), 130 ) 131 return 132 } 133 134 // Close calls the function "WEBEXT.loginScreenUi.close" directly. 135 func Close() (ret js.Promise[js.Void]) { 136 bindings.CallClose( 137 js.Pointer(&ret), 138 ) 139 140 return 141 } 142 143 // TryClose calls the function "WEBEXT.loginScreenUi.close" 144 // in a try/catch block and returns (_, err, ok = false) when it went through 145 // the catch clause. 146 func TryClose() (ret js.Promise[js.Void], exception js.Any, ok bool) { 147 ok = js.True == bindings.TryClose( 148 js.Pointer(&ret), js.Pointer(&exception), 149 ) 150 151 return 152 } 153 154 // HasFuncShow returns true if the function "WEBEXT.loginScreenUi.show" exists. 155 func HasFuncShow() bool { 156 return js.True == bindings.HasFuncShow() 157 } 158 159 // FuncShow returns the function "WEBEXT.loginScreenUi.show". 160 func FuncShow() (fn js.Func[func(options ShowOptions) js.Promise[js.Void]]) { 161 bindings.FuncShow( 162 js.Pointer(&fn), 163 ) 164 return 165 } 166 167 // Show calls the function "WEBEXT.loginScreenUi.show" directly. 168 func Show(options ShowOptions) (ret js.Promise[js.Void]) { 169 bindings.CallShow( 170 js.Pointer(&ret), 171 js.Pointer(&options), 172 ) 173 174 return 175 } 176 177 // TryShow calls the function "WEBEXT.loginScreenUi.show" 178 // in a try/catch block and returns (_, err, ok = false) when it went through 179 // the catch clause. 180 func TryShow(options ShowOptions) (ret js.Promise[js.Void], exception js.Any, ok bool) { 181 ok = js.True == bindings.TryShow( 182 js.Pointer(&ret), js.Pointer(&exception), 183 js.Pointer(&options), 184 ) 185 186 return 187 }