github.com/visualfc/xtype@v0.2.0/xtype_js.go (about) 1 //go:build js && !wasm 2 // +build js,!wasm 3 4 package xtype 5 6 import ( 7 "reflect" 8 "unsafe" 9 10 "github.com/gopherjs/gopherjs/js" 11 ) 12 13 type eface struct { 14 typ unsafe.Pointer 15 word unsafe.Pointer 16 } 17 18 type Type = *js.Object 19 20 func TypeOf(i interface{}) Type { 21 return js.InternalObject(i).Get("constructor") 22 } 23 24 func TypeOfType(typ reflect.Type) Type { 25 return js.InternalObject(typ).Get("jsType") 26 } 27 28 func Bytes(i interface{}) []byte { 29 v1 := js.InternalObject(i) 30 b := make([]byte, v1.Get("$length").Int(), v1.Get("$capacity").Int()) 31 v2 := js.InternalObject(b) 32 v2.Set("$array", v1.Get("$array")) 33 v2.Set("$offset", v1.Get("$offset")) 34 return b 35 } 36 37 func Runes(i interface{}) []rune { 38 v1 := js.InternalObject(i) 39 b := make([]rune, v1.Get("$length").Int(), v1.Get("$capacity").Int()) 40 v2 := js.InternalObject(b) 41 v2.Set("$array", v1.Get("$array")) 42 v2.Set("$offset", v1.Get("$offset")) 43 return b 44 } 45 46 func Bool(i interface{}) bool { 47 v := js.InternalObject(i).Get("$val") 48 return v.Bool() 49 } 50 51 func Int(i interface{}) int { 52 v := js.InternalObject(i).Get("$val") 53 return v.Int() 54 } 55 56 func Int8(i interface{}) int8 { 57 v := js.InternalObject(i).Get("$val") 58 return int8(v.Int64()) 59 } 60 61 func Int16(i interface{}) int16 { 62 v := js.InternalObject(i).Get("$val") 63 return int16(v.Int64()) 64 } 65 66 func Int32(i interface{}) int32 { 67 v := js.InternalObject(i).Get("$val") 68 return int32(v.Int64()) 69 } 70 71 func Int64(i interface{}) int64 { 72 v := js.InternalObject(i).Get("$val") 73 return v.Int64() 74 } 75 76 func Uint(i interface{}) uint { 77 v := js.InternalObject(i).Get("$val") 78 return uint(v.Uint64()) 79 } 80 81 func Uint8(i interface{}) uint8 { 82 v := js.InternalObject(i).Get("$val") 83 return uint8(v.Uint64()) 84 } 85 86 func Uint16(i interface{}) uint16 { 87 v := js.InternalObject(i).Get("$val") 88 return uint16(v.Uint64()) 89 } 90 91 func Uint32(i interface{}) uint32 { 92 v := js.InternalObject(i).Get("$val") 93 return uint32(v.Uint64()) 94 } 95 96 func Uint64(i interface{}) uint64 { 97 v := js.InternalObject(i).Get("$val") 98 return v.Uint64() 99 } 100 101 func Uintptr(i interface{}) uintptr { 102 v := js.InternalObject(i).Get("$val") 103 return uintptr(v.Uint64()) 104 } 105 106 func Float32(i interface{}) float32 { 107 v := js.InternalObject(i).Get("$val") 108 return float32(v.Float()) 109 } 110 111 func Float64(i interface{}) float64 { 112 v := js.InternalObject(i).Get("$val") 113 return v.Float() 114 } 115 116 func Complex64(i interface{}) complex64 { 117 v := js.InternalObject(i) 118 return complex(float32(v.Get("$real").Float()), float32(v.Get("$imag").Float())) 119 } 120 121 func Complex128(i interface{}) complex128 { 122 v := js.InternalObject(i) 123 return complex(v.Get("$real").Float(), v.Get("$imag").Float()) 124 } 125 126 func String(i interface{}) string { 127 v := js.InternalObject(i) 128 return v.Get("$val").String() 129 } 130 131 func Pointer(i interface{}) unsafe.Pointer { 132 v := js.InternalObject(i) 133 return unsafe.Pointer(v.Get("$val").Unsafe()) 134 } 135 136 // Make change interface type and return 137 func Make(typ Type, i interface{}) interface{} { 138 v := js.InternalObject(i) 139 v.Set("constructor", typ) 140 return i 141 } 142 143 func ConvertPtr(typ Type, i interface{}) interface{} { 144 rt := toReflectType(typ) 145 return reflect.ValueOf(i).Convert(rt).Interface() 146 } 147 148 // //go:linkname typedmemmove reflect.typedmemmove 149 // func typedmemmove(t Type, dst unsafe.Pointer, src unsafe.Pointer) 150 151 // //go:linkname unsafe_New reflect.unsafe_New 152 // func unsafe_New(t Type) unsafe.Pointer 153 154 func ptrTo(typ Type) Type { 155 return js.Global.Call("$ptrType", typ) 156 } 157 158 // func unsafe_New(typ Type) unsafe.Pointer { 159 // switch reflect.Kind(typ.Get("kind").Int()) { 160 // case reflect.Struct: 161 // return unsafe.Pointer(typ.Get("ptr").New().Unsafe()) 162 // case reflect.Array: 163 // return unsafe.Pointer(typ.Call("zero").Unsafe()) 164 // default: 165 // return unsafe.Pointer(js.Global.Call("$newDataPointer", typ.Call("zero"), ptrTo(typ)).Unsafe()) 166 // } 167 // } 168 169 // func newObject(typ Type) *js.Object { 170 // switch reflect.Kind(typ.Get("kind").Int()) { 171 // case reflect.Struct: 172 // return typ.Get("ptr").New() 173 // case reflect.Array: 174 // return typ.Call("zero") 175 // default: 176 // return js.Global.Call("$newDataPointer", typ.Call("zero"), ptrTo(typ)) 177 // } 178 // } 179 180 // func typedmemmove(t *rtype, dst, src unsafe.Pointer) { 181 // js.InternalObject(dst).Call("$set", js.InternalObject(src).Call("$get")) 182 // } 183 184 // convert copy 185 func ConvertDirect(typ Type, i interface{}) interface{} { 186 rt := toReflectType(typ) 187 return reflect.ValueOf(i).Convert(rt).Interface() 188 } 189 190 func toReflectType(typ Type) reflect.Type { 191 return toType(reflectType(typ)) 192 } 193 194 //go:linkname toType reflect.toType 195 func toType(t *_type) reflect.Type 196 197 //go:linkname reflectType reflect.reflectType 198 func reflectType(typ *js.Object) *_type 199 200 func ConvertBool(typ Type, i interface{}) interface{} { 201 return Make(typ, i) 202 } 203 204 func ConvertInt(typ Type, i interface{}) interface{} { 205 return Make(typ, i) 206 } 207 208 func ConvertInt8(typ Type, i interface{}) interface{} { 209 return Make(typ, i) 210 } 211 212 func ConvertInt16(typ Type, i interface{}) interface{} { 213 return Make(typ, i) 214 } 215 216 func ConvertInt32(typ Type, i interface{}) interface{} { 217 return Make(typ, i) 218 } 219 220 func ConvertInt64(typ Type, i interface{}) interface{} { 221 return Make(typ, i) 222 } 223 224 func ConvertUint(typ Type, i interface{}) interface{} { 225 return Make(typ, i) 226 } 227 228 func ConvertUint8(typ Type, i interface{}) interface{} { 229 return Make(typ, i) 230 } 231 232 func ConvertUint16(typ Type, i interface{}) interface{} { 233 return Make(typ, i) 234 } 235 236 func ConvertUint32(typ Type, i interface{}) interface{} { 237 return Make(typ, i) 238 } 239 240 func ConvertUint64(typ Type, i interface{}) interface{} { 241 return Make(typ, i) 242 } 243 244 func ConvertUintptr(typ Type, i interface{}) interface{} { 245 return Make(typ, i) 246 } 247 248 func ConvertFloat32(typ Type, i interface{}) interface{} { 249 return Make(typ, i) 250 } 251 252 func ConvertFloat64(typ Type, i interface{}) interface{} { 253 return Make(typ, i) 254 } 255 256 func ConvertComplex64(typ Type, i interface{}) interface{} { 257 return Make(typ, i) 258 } 259 260 func ConvertComplex128(typ Type, i interface{}) interface{} { 261 return Make(typ, i) 262 } 263 264 func ConvertString(typ Type, i interface{}) interface{} { 265 return Make(typ, i) 266 } 267 268 func Not(i interface{}) interface{} { 269 v := js.InternalObject(i) 270 return Make(v.Get("constructor"), !v.Get("$val").Bool()) 271 } 272 273 func NegInt(i interface{}) interface{} { 274 v := js.InternalObject(i) 275 return Make(v.Get("constructor"), -v.Get("$val").Int()) 276 } 277 278 func NegInt8(i interface{}) interface{} { 279 v := js.InternalObject(i) 280 return Make(v.Get("constructor"), -int8(v.Get("$val").Int64())) 281 } 282 283 func NegInt16(i interface{}) interface{} { 284 v := js.InternalObject(i) 285 return Make(v.Get("constructor"), -int16(v.Get("$val").Int64())) 286 } 287 288 func NegInt32(i interface{}) interface{} { 289 v := js.InternalObject(i) 290 return Make(v.Get("constructor"), -int32(v.Get("$val").Int64())) 291 } 292 293 func NegInt64(i interface{}) interface{} { 294 v := js.InternalObject(i) 295 return Make(v.Get("constructor"), -int64(v.Get("$val").Int64())) 296 } 297 298 func NegUint(i interface{}) interface{} { 299 v := js.InternalObject(i) 300 return Make(v.Get("constructor"), -uint(v.Get("$val").Uint64())) 301 } 302 303 func NegUint8(i interface{}) interface{} { 304 v := js.InternalObject(i) 305 return Make(v.Get("constructor"), -uint8(v.Get("$val").Uint64())) 306 } 307 308 func NegUint16(i interface{}) interface{} { 309 v := js.InternalObject(i) 310 return Make(v.Get("constructor"), -uint16(v.Get("$val").Uint64())) 311 } 312 313 func NegUint32(i interface{}) interface{} { 314 v := js.InternalObject(i) 315 return Make(v.Get("constructor"), -uint32(v.Get("$val").Uint64())) 316 } 317 318 func NegUint64(i interface{}) interface{} { 319 v := js.InternalObject(i) 320 return Make(v.Get("constructor"), -uint64(v.Get("$val").Uint64())) 321 } 322 323 func NegUintptr(i interface{}) interface{} { 324 v := js.InternalObject(i) 325 return Make(v.Get("constructor"), -uintptr(v.Get("$val").Uint64())) 326 } 327 328 func NegFloat32(i interface{}) interface{} { 329 v := js.InternalObject(i) 330 return Make(v.Get("constructor"), -float32(v.Get("$val").Float())) 331 } 332 333 func NegFloat64(i interface{}) interface{} { 334 v := js.InternalObject(i) 335 return Make(v.Get("constructor"), -float64(v.Get("$val").Float())) 336 } 337 338 func NegComplex64(i interface{}) interface{} { 339 v := js.InternalObject(i) 340 return Make(v.Get("constructor"), -complex(float32(v.Get("$real").Float()), float32(v.Get("$imag").Float()))) 341 } 342 343 func NegComplex128(i interface{}) interface{} { 344 v := js.InternalObject(i) 345 return Make(v.Get("constructor"), -complex(v.Get("$real").Float(), v.Get("$imag").Float())) 346 } 347 348 func XorInt(i interface{}) interface{} { 349 v := js.InternalObject(i) 350 return Make(v.Get("constructor"), ^int(v.Get("$val").Int())) 351 } 352 353 func XorInt8(i interface{}) interface{} { 354 v := js.InternalObject(i) 355 return Make(v.Get("constructor"), ^int8(v.Get("$val").Int())) 356 } 357 358 func XorInt16(i interface{}) interface{} { 359 v := js.InternalObject(i) 360 return Make(v.Get("constructor"), ^int16(v.Get("$val").Int())) 361 } 362 363 func XorInt32(i interface{}) interface{} { 364 v := js.InternalObject(i) 365 return Make(v.Get("constructor"), ^int32(v.Get("$val").Int())) 366 } 367 368 func XorInt64(i interface{}) interface{} { 369 v := js.InternalObject(i) 370 return Make(v.Get("constructor"), ^int64(v.Get("$val").Int64())) 371 } 372 373 func XorUint(i interface{}) interface{} { 374 v := js.InternalObject(i) 375 return Make(v.Get("constructor"), ^uint(v.Get("$val").Uint64())) 376 } 377 378 func XorUint8(i interface{}) interface{} { 379 v := js.InternalObject(i) 380 return Make(v.Get("constructor"), ^uint8(v.Get("$val").Uint64())) 381 } 382 383 func XorUint16(i interface{}) interface{} { 384 v := js.InternalObject(i) 385 return Make(v.Get("constructor"), ^uint16(v.Get("$val").Uint64())) 386 } 387 388 func XorUint32(i interface{}) interface{} { 389 v := js.InternalObject(i) 390 return Make(v.Get("constructor"), ^uint32(v.Get("$val").Uint64())) 391 } 392 393 func XorUint64(i interface{}) interface{} { 394 v := js.InternalObject(i) 395 return Make(v.Get("constructor"), ^uint64(v.Get("$val").Uint64())) 396 } 397 398 func XorUintptr(i interface{}) interface{} { 399 v := js.InternalObject(i) 400 return Make(v.Get("constructor"), ^uintptr(v.Get("$val").Uint64())) 401 } 402 403 func MakeBool(typ Type, v bool) interface{} { 404 return Make(typ, v) 405 } 406 407 func MakeInt(typ Type, v int) interface{} { 408 return Make(typ, v) 409 } 410 411 func MakeInt8(typ Type, v int8) interface{} { 412 return Make(typ, v) 413 } 414 415 func MakeInt16(typ Type, v int16) interface{} { 416 return Make(typ, v) 417 } 418 419 func MakeInt32(typ Type, v int32) interface{} { 420 return Make(typ, v) 421 } 422 423 func MakeInt64(typ Type, v int64) interface{} { 424 return Make(typ, v) 425 } 426 427 func MakeUint(typ Type, v uint) interface{} { 428 return Make(typ, v) 429 } 430 431 func MakeUint8(typ Type, v uint8) interface{} { 432 return Make(typ, v) 433 } 434 435 func MakeUint16(typ Type, v uint16) interface{} { 436 return Make(typ, v) 437 } 438 439 func MakeUint32(typ Type, v uint32) interface{} { 440 return Make(typ, v) 441 } 442 443 func MakeUint64(typ Type, v uint64) interface{} { 444 return Make(typ, v) 445 } 446 447 func MakeUintptr(typ Type, v uintptr) interface{} { 448 return Make(typ, v) 449 } 450 451 func MakeFloat32(typ Type, v float32) interface{} { 452 return Make(typ, v) 453 } 454 455 func MakeFloat64(typ Type, v float64) interface{} { 456 return Make(typ, v) 457 } 458 459 func MakeComplex64(typ Type, v complex64) interface{} { 460 return Make(typ, v) 461 } 462 463 func MakeComplex128(typ Type, v complex128) interface{} { 464 return Make(typ, v) 465 } 466 467 func MakeString(typ Type, v string) interface{} { 468 return Make(typ, v) 469 } 470 471 func Alloc(typ Type) interface{} { 472 t := toReflectType(typ) 473 if t.Kind() == reflect.Ptr { 474 return reflect.New(t.Elem()).Interface() 475 } 476 return reflect.New(t).Elem().Interface() 477 } 478 479 func New(typ, ptrto Type) interface{} { 480 t := toReflectType(typ) 481 return reflect.New(t).Interface() 482 } 483 484 // func NewPointer(typ Type) unsafe.Pointer { 485 // return unsafe_New(typ) 486 // } 487 488 // func SetPointer(i interface{}, word unsafe.Pointer) interface{} { 489 // p := (*eface)(unsafe.Pointer(&i)) 490 // p.word = word 491 // return i 492 // } 493 494 // func SetType(i interface{}, typ Type) interface{} { 495 // p := (*eface)(unsafe.Pointer(&i)) 496 // p.typ = unsafe.Pointer(typ) 497 // return i 498 // } 499 500 func ConvertFunc(fn reflect.Value, typ Type) reflect.Value { 501 (*struct { 502 typ *_type 503 ptr unsafe.Pointer 504 })(unsafe.Pointer(&fn)).typ = reflectType(typ) 505 return fn 506 }