github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/system/cpu/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package cpu 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/system/cpu/bindings" 11 ) 12 13 type CpuTime struct { 14 // User is "CpuTime.user" 15 // 16 // Optional 17 // 18 // NOTE: FFI_USE_User MUST be set to true to make this field effective. 19 User float64 20 // Kernel is "CpuTime.kernel" 21 // 22 // Optional 23 // 24 // NOTE: FFI_USE_Kernel MUST be set to true to make this field effective. 25 Kernel float64 26 // Idle is "CpuTime.idle" 27 // 28 // Optional 29 // 30 // NOTE: FFI_USE_Idle MUST be set to true to make this field effective. 31 Idle float64 32 // Total is "CpuTime.total" 33 // 34 // Optional 35 // 36 // NOTE: FFI_USE_Total MUST be set to true to make this field effective. 37 Total float64 38 39 FFI_USE_User bool // for User. 40 FFI_USE_Kernel bool // for Kernel. 41 FFI_USE_Idle bool // for Idle. 42 FFI_USE_Total bool // for Total. 43 44 FFI_USE bool 45 } 46 47 // FromRef calls UpdateFrom and returns a CpuTime with all fields set. 48 func (p CpuTime) FromRef(ref js.Ref) CpuTime { 49 p.UpdateFrom(ref) 50 return p 51 } 52 53 // New creates a new CpuTime in the application heap. 54 func (p CpuTime) New() js.Ref { 55 return bindings.CpuTimeJSLoad( 56 js.Pointer(&p), js.True, 0, 57 ) 58 } 59 60 // UpdateFrom copies value of all fields of the heap object to p. 61 func (p *CpuTime) UpdateFrom(ref js.Ref) { 62 bindings.CpuTimeJSStore( 63 js.Pointer(p), ref, 64 ) 65 } 66 67 // Update writes all fields of the p to the heap object referenced by ref. 68 func (p *CpuTime) Update(ref js.Ref) { 69 bindings.CpuTimeJSLoad( 70 js.Pointer(p), js.False, ref, 71 ) 72 } 73 74 // FreeMembers frees fields with heap reference, if recursive is true 75 // free all heap references reachable from p. 76 func (p *CpuTime) FreeMembers(recursive bool) { 77 } 78 79 type ProcessorInfo struct { 80 // Usage is "ProcessorInfo.usage" 81 // 82 // Optional 83 // 84 // NOTE: Usage.FFI_USE MUST be set to true to get Usage used. 85 Usage CpuTime 86 87 FFI_USE bool 88 } 89 90 // FromRef calls UpdateFrom and returns a ProcessorInfo with all fields set. 91 func (p ProcessorInfo) FromRef(ref js.Ref) ProcessorInfo { 92 p.UpdateFrom(ref) 93 return p 94 } 95 96 // New creates a new ProcessorInfo in the application heap. 97 func (p ProcessorInfo) New() js.Ref { 98 return bindings.ProcessorInfoJSLoad( 99 js.Pointer(&p), js.True, 0, 100 ) 101 } 102 103 // UpdateFrom copies value of all fields of the heap object to p. 104 func (p *ProcessorInfo) UpdateFrom(ref js.Ref) { 105 bindings.ProcessorInfoJSStore( 106 js.Pointer(p), ref, 107 ) 108 } 109 110 // Update writes all fields of the p to the heap object referenced by ref. 111 func (p *ProcessorInfo) Update(ref js.Ref) { 112 bindings.ProcessorInfoJSLoad( 113 js.Pointer(p), js.False, ref, 114 ) 115 } 116 117 // FreeMembers frees fields with heap reference, if recursive is true 118 // free all heap references reachable from p. 119 func (p *ProcessorInfo) FreeMembers(recursive bool) { 120 if recursive { 121 p.Usage.FreeMembers(true) 122 } 123 } 124 125 type CpuInfo struct { 126 // NumOfProcessors is "CpuInfo.numOfProcessors" 127 // 128 // Optional 129 // 130 // NOTE: FFI_USE_NumOfProcessors MUST be set to true to make this field effective. 131 NumOfProcessors int32 132 // ArchName is "CpuInfo.archName" 133 // 134 // Optional 135 ArchName js.String 136 // ModelName is "CpuInfo.modelName" 137 // 138 // Optional 139 ModelName js.String 140 // Features is "CpuInfo.features" 141 // 142 // Optional 143 Features js.Array[js.String] 144 // Processors is "CpuInfo.processors" 145 // 146 // Optional 147 Processors js.Array[ProcessorInfo] 148 // Temperatures is "CpuInfo.temperatures" 149 // 150 // Optional 151 Temperatures js.Array[float64] 152 153 FFI_USE_NumOfProcessors bool // for NumOfProcessors. 154 155 FFI_USE bool 156 } 157 158 // FromRef calls UpdateFrom and returns a CpuInfo with all fields set. 159 func (p CpuInfo) FromRef(ref js.Ref) CpuInfo { 160 p.UpdateFrom(ref) 161 return p 162 } 163 164 // New creates a new CpuInfo in the application heap. 165 func (p CpuInfo) New() js.Ref { 166 return bindings.CpuInfoJSLoad( 167 js.Pointer(&p), js.True, 0, 168 ) 169 } 170 171 // UpdateFrom copies value of all fields of the heap object to p. 172 func (p *CpuInfo) UpdateFrom(ref js.Ref) { 173 bindings.CpuInfoJSStore( 174 js.Pointer(p), ref, 175 ) 176 } 177 178 // Update writes all fields of the p to the heap object referenced by ref. 179 func (p *CpuInfo) Update(ref js.Ref) { 180 bindings.CpuInfoJSLoad( 181 js.Pointer(p), js.False, ref, 182 ) 183 } 184 185 // FreeMembers frees fields with heap reference, if recursive is true 186 // free all heap references reachable from p. 187 func (p *CpuInfo) FreeMembers(recursive bool) { 188 js.Free( 189 p.ArchName.Ref(), 190 p.ModelName.Ref(), 191 p.Features.Ref(), 192 p.Processors.Ref(), 193 p.Temperatures.Ref(), 194 ) 195 p.ArchName = p.ArchName.FromRef(js.Undefined) 196 p.ModelName = p.ModelName.FromRef(js.Undefined) 197 p.Features = p.Features.FromRef(js.Undefined) 198 p.Processors = p.Processors.FromRef(js.Undefined) 199 p.Temperatures = p.Temperatures.FromRef(js.Undefined) 200 } 201 202 type CpuInfoCallbackFunc func(this js.Ref, info *CpuInfo) js.Ref 203 204 func (fn CpuInfoCallbackFunc) Register() js.Func[func(info *CpuInfo)] { 205 return js.RegisterCallback[func(info *CpuInfo)]( 206 fn, abi.FuncPCABIInternal(fn), 207 ) 208 } 209 210 func (fn CpuInfoCallbackFunc) DispatchCallback( 211 targetPC uintptr, ctx *js.CallbackContext, 212 ) { 213 args := ctx.Args() 214 if len(args) != 1+1 /* js this */ || 215 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 216 js.ThrowInvalidCallbackInvocation() 217 } 218 var arg0 CpuInfo 219 arg0.UpdateFrom(args[0+1]) 220 defer arg0.FreeMembers(true) 221 222 if ctx.Return(fn( 223 args[0], 224 225 mark.NoEscape(&arg0), 226 )) { 227 return 228 } 229 230 js.ThrowCallbackValueNotReturned() 231 } 232 233 type CpuInfoCallback[T any] struct { 234 Fn func(arg T, this js.Ref, info *CpuInfo) js.Ref 235 Arg T 236 } 237 238 func (cb *CpuInfoCallback[T]) Register() js.Func[func(info *CpuInfo)] { 239 return js.RegisterCallback[func(info *CpuInfo)]( 240 cb, abi.FuncPCABIInternal(cb.Fn), 241 ) 242 } 243 244 func (cb *CpuInfoCallback[T]) DispatchCallback( 245 targetPC uintptr, ctx *js.CallbackContext, 246 ) { 247 args := ctx.Args() 248 if len(args) != 1+1 /* js this */ || 249 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 250 js.ThrowInvalidCallbackInvocation() 251 } 252 var arg0 CpuInfo 253 arg0.UpdateFrom(args[0+1]) 254 defer arg0.FreeMembers(true) 255 256 if ctx.Return(cb.Fn( 257 cb.Arg, 258 args[0], 259 260 mark.NoEscape(&arg0), 261 )) { 262 return 263 } 264 265 js.ThrowCallbackValueNotReturned() 266 } 267 268 // HasFuncGetInfo returns true if the function "WEBEXT.system.cpu.getInfo" exists. 269 func HasFuncGetInfo() bool { 270 return js.True == bindings.HasFuncGetInfo() 271 } 272 273 // FuncGetInfo returns the function "WEBEXT.system.cpu.getInfo". 274 func FuncGetInfo() (fn js.Func[func() js.Promise[CpuInfo]]) { 275 bindings.FuncGetInfo( 276 js.Pointer(&fn), 277 ) 278 return 279 } 280 281 // GetInfo calls the function "WEBEXT.system.cpu.getInfo" directly. 282 func GetInfo() (ret js.Promise[CpuInfo]) { 283 bindings.CallGetInfo( 284 js.Pointer(&ret), 285 ) 286 287 return 288 } 289 290 // TryGetInfo calls the function "WEBEXT.system.cpu.getInfo" 291 // in a try/catch block and returns (_, err, ok = false) when it went through 292 // the catch clause. 293 func TryGetInfo() (ret js.Promise[CpuInfo], exception js.Any, ok bool) { 294 ok = js.True == bindings.TryGetInfo( 295 js.Pointer(&ret), js.Pointer(&exception), 296 ) 297 298 return 299 }