github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/documentscan/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package documentscan 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/documentscan/bindings" 11 ) 12 13 type ScanCallbackFunc func(this js.Ref, result *ScanResults) js.Ref 14 15 func (fn ScanCallbackFunc) Register() js.Func[func(result *ScanResults)] { 16 return js.RegisterCallback[func(result *ScanResults)]( 17 fn, abi.FuncPCABIInternal(fn), 18 ) 19 } 20 21 func (fn ScanCallbackFunc) DispatchCallback( 22 targetPC uintptr, ctx *js.CallbackContext, 23 ) { 24 args := ctx.Args() 25 if len(args) != 1+1 /* js this */ || 26 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 27 js.ThrowInvalidCallbackInvocation() 28 } 29 var arg0 ScanResults 30 arg0.UpdateFrom(args[0+1]) 31 defer arg0.FreeMembers(true) 32 33 if ctx.Return(fn( 34 args[0], 35 36 mark.NoEscape(&arg0), 37 )) { 38 return 39 } 40 41 js.ThrowCallbackValueNotReturned() 42 } 43 44 type ScanCallback[T any] struct { 45 Fn func(arg T, this js.Ref, result *ScanResults) js.Ref 46 Arg T 47 } 48 49 func (cb *ScanCallback[T]) Register() js.Func[func(result *ScanResults)] { 50 return js.RegisterCallback[func(result *ScanResults)]( 51 cb, abi.FuncPCABIInternal(cb.Fn), 52 ) 53 } 54 55 func (cb *ScanCallback[T]) DispatchCallback( 56 targetPC uintptr, ctx *js.CallbackContext, 57 ) { 58 args := ctx.Args() 59 if len(args) != 1+1 /* js this */ || 60 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 61 js.ThrowInvalidCallbackInvocation() 62 } 63 var arg0 ScanResults 64 arg0.UpdateFrom(args[0+1]) 65 defer arg0.FreeMembers(true) 66 67 if ctx.Return(cb.Fn( 68 cb.Arg, 69 args[0], 70 71 mark.NoEscape(&arg0), 72 )) { 73 return 74 } 75 76 js.ThrowCallbackValueNotReturned() 77 } 78 79 type ScanResults struct { 80 // DataUrls is "ScanResults.dataUrls" 81 // 82 // Optional 83 DataUrls js.Array[js.String] 84 // MimeType is "ScanResults.mimeType" 85 // 86 // Optional 87 MimeType js.String 88 89 FFI_USE bool 90 } 91 92 // FromRef calls UpdateFrom and returns a ScanResults with all fields set. 93 func (p ScanResults) FromRef(ref js.Ref) ScanResults { 94 p.UpdateFrom(ref) 95 return p 96 } 97 98 // New creates a new ScanResults in the application heap. 99 func (p ScanResults) New() js.Ref { 100 return bindings.ScanResultsJSLoad( 101 js.Pointer(&p), js.True, 0, 102 ) 103 } 104 105 // UpdateFrom copies value of all fields of the heap object to p. 106 func (p *ScanResults) UpdateFrom(ref js.Ref) { 107 bindings.ScanResultsJSStore( 108 js.Pointer(p), ref, 109 ) 110 } 111 112 // Update writes all fields of the p to the heap object referenced by ref. 113 func (p *ScanResults) Update(ref js.Ref) { 114 bindings.ScanResultsJSLoad( 115 js.Pointer(p), js.False, ref, 116 ) 117 } 118 119 // FreeMembers frees fields with heap reference, if recursive is true 120 // free all heap references reachable from p. 121 func (p *ScanResults) FreeMembers(recursive bool) { 122 js.Free( 123 p.DataUrls.Ref(), 124 p.MimeType.Ref(), 125 ) 126 p.DataUrls = p.DataUrls.FromRef(js.Undefined) 127 p.MimeType = p.MimeType.FromRef(js.Undefined) 128 } 129 130 type ScanOptions struct { 131 // MimeTypes is "ScanOptions.mimeTypes" 132 // 133 // Optional 134 MimeTypes js.Array[js.String] 135 // MaxImages is "ScanOptions.maxImages" 136 // 137 // Optional 138 // 139 // NOTE: FFI_USE_MaxImages MUST be set to true to make this field effective. 140 MaxImages int32 141 142 FFI_USE_MaxImages bool // for MaxImages. 143 144 FFI_USE bool 145 } 146 147 // FromRef calls UpdateFrom and returns a ScanOptions with all fields set. 148 func (p ScanOptions) FromRef(ref js.Ref) ScanOptions { 149 p.UpdateFrom(ref) 150 return p 151 } 152 153 // New creates a new ScanOptions in the application heap. 154 func (p ScanOptions) New() js.Ref { 155 return bindings.ScanOptionsJSLoad( 156 js.Pointer(&p), js.True, 0, 157 ) 158 } 159 160 // UpdateFrom copies value of all fields of the heap object to p. 161 func (p *ScanOptions) UpdateFrom(ref js.Ref) { 162 bindings.ScanOptionsJSStore( 163 js.Pointer(p), ref, 164 ) 165 } 166 167 // Update writes all fields of the p to the heap object referenced by ref. 168 func (p *ScanOptions) Update(ref js.Ref) { 169 bindings.ScanOptionsJSLoad( 170 js.Pointer(p), js.False, ref, 171 ) 172 } 173 174 // FreeMembers frees fields with heap reference, if recursive is true 175 // free all heap references reachable from p. 176 func (p *ScanOptions) FreeMembers(recursive bool) { 177 js.Free( 178 p.MimeTypes.Ref(), 179 ) 180 p.MimeTypes = p.MimeTypes.FromRef(js.Undefined) 181 } 182 183 // HasFuncScan returns true if the function "WEBEXT.documentScan.scan" exists. 184 func HasFuncScan() bool { 185 return js.True == bindings.HasFuncScan() 186 } 187 188 // FuncScan returns the function "WEBEXT.documentScan.scan". 189 func FuncScan() (fn js.Func[func(options ScanOptions) js.Promise[ScanResults]]) { 190 bindings.FuncScan( 191 js.Pointer(&fn), 192 ) 193 return 194 } 195 196 // Scan calls the function "WEBEXT.documentScan.scan" directly. 197 func Scan(options ScanOptions) (ret js.Promise[ScanResults]) { 198 bindings.CallScan( 199 js.Pointer(&ret), 200 js.Pointer(&options), 201 ) 202 203 return 204 } 205 206 // TryScan calls the function "WEBEXT.documentScan.scan" 207 // in a try/catch block and returns (_, err, ok = false) when it went through 208 // the catch clause. 209 func TryScan(options ScanOptions) (ret js.Promise[ScanResults], exception js.Any, ok bool) { 210 ok = js.True == bindings.TryScan( 211 js.Pointer(&ret), js.Pointer(&exception), 212 js.Pointer(&options), 213 ) 214 215 return 216 }