github.com/goplus/gossa@v0.3.25/internal/xtype/xtype.go (about) 1 package xtype 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 type eface struct { 9 typ unsafe.Pointer 10 word unsafe.Pointer 11 } 12 13 type Type unsafe.Pointer 14 15 func TypeOf(i interface{}) Type { 16 p := (*eface)(unsafe.Pointer(&i)) 17 return Type(p.typ) 18 } 19 20 func TypeOfType(typ reflect.Type) Type { 21 e := (*eface)(unsafe.Pointer(&typ)) 22 return Type(e.word) 23 } 24 25 func Bytes(i interface{}) []byte { 26 return *(*[]byte)((*eface)(unsafe.Pointer(&i)).word) 27 } 28 29 func Runes(i interface{}) []rune { 30 return *(*[]rune)((*eface)(unsafe.Pointer(&i)).word) 31 } 32 33 func Bool(i interface{}) bool { 34 return *(*bool)((*eface)(unsafe.Pointer(&i)).word) 35 } 36 37 func Int(i interface{}) int { 38 return *(*int)((*eface)(unsafe.Pointer(&i)).word) 39 } 40 41 func Int8(i interface{}) int8 { 42 return *(*int8)((*eface)(unsafe.Pointer(&i)).word) 43 } 44 45 func Int16(i interface{}) int16 { 46 return *(*int16)((*eface)(unsafe.Pointer(&i)).word) 47 } 48 49 func Int32(i interface{}) int32 { 50 return *(*int32)((*eface)(unsafe.Pointer(&i)).word) 51 } 52 53 func Int64(i interface{}) int64 { 54 return *(*int64)((*eface)(unsafe.Pointer(&i)).word) 55 } 56 57 func Uint(i interface{}) uint { 58 return *(*uint)((*eface)(unsafe.Pointer(&i)).word) 59 } 60 61 func Uint8(i interface{}) uint8 { 62 return *(*uint8)((*eface)(unsafe.Pointer(&i)).word) 63 } 64 65 func Uint16(i interface{}) uint16 { 66 return *(*uint16)((*eface)(unsafe.Pointer(&i)).word) 67 } 68 69 func Uint32(i interface{}) uint32 { 70 return *(*uint32)((*eface)(unsafe.Pointer(&i)).word) 71 } 72 73 func Uint64(i interface{}) uint64 { 74 return *(*uint64)((*eface)(unsafe.Pointer(&i)).word) 75 } 76 77 func Uintptr(i interface{}) uintptr { 78 return *(*uintptr)((*eface)(unsafe.Pointer(&i)).word) 79 } 80 81 func Float32(i interface{}) float32 { 82 return *(*float32)((*eface)(unsafe.Pointer(&i)).word) 83 } 84 85 func Float64(i interface{}) float64 { 86 return *(*float64)((*eface)(unsafe.Pointer(&i)).word) 87 } 88 89 func Complex64(i interface{}) complex64 { 90 return *(*complex64)((*eface)(unsafe.Pointer(&i)).word) 91 } 92 93 func Complex128(i interface{}) complex128 { 94 return *(*complex128)((*eface)(unsafe.Pointer(&i)).word) 95 } 96 97 func String(i interface{}) string { 98 return *(*string)((*eface)(unsafe.Pointer(&i)).word) 99 } 100 101 func Pointer(i interface{}) unsafe.Pointer { 102 return (*eface)(unsafe.Pointer(&i)).word 103 } 104 105 // Make change interface type and return 106 func Make(typ Type, i interface{}) interface{} { 107 p := (*eface)(unsafe.Pointer(&i)) 108 p.typ = unsafe.Pointer(typ) 109 return i 110 } 111 112 func ConvertPtr(typ Type, i interface{}) interface{} { 113 p := (*eface)(unsafe.Pointer(&i)) 114 return *(*interface{})(unsafe.Pointer(&eface{ 115 typ: unsafe.Pointer(typ), 116 word: p.word, 117 })) 118 } 119 120 //go:linkname typedmemmove reflect.typedmemmove 121 func typedmemmove(t Type, dst unsafe.Pointer, src unsafe.Pointer) 122 123 //go:linkname unsafe_New reflect.unsafe_New 124 func unsafe_New(t Type) unsafe.Pointer 125 126 // convert copy 127 func ConvertDirect(typ Type, i interface{}) interface{} { 128 p := (*eface)(unsafe.Pointer(&i)) 129 c := unsafe_New(typ) 130 typedmemmove(typ, c, p.word) 131 return *(*interface{})(unsafe.Pointer(&eface{ 132 typ: unsafe.Pointer(typ), 133 word: c, 134 })) 135 } 136 137 func ConvertBool(typ Type, i interface{}) interface{} { 138 p := (*eface)(unsafe.Pointer(&i)) 139 v := *(*bool)(p.word) 140 return *(*interface{})(unsafe.Pointer(&eface{ 141 typ: unsafe.Pointer(typ), 142 word: unsafe.Pointer(&v), 143 })) 144 } 145 146 func ConvertInt(typ Type, i interface{}) interface{} { 147 p := (*eface)(unsafe.Pointer(&i)) 148 v := *(*int)(p.word) 149 return *(*interface{})(unsafe.Pointer(&eface{ 150 typ: unsafe.Pointer(typ), 151 word: unsafe.Pointer(&v), 152 })) 153 } 154 155 func ConvertInt8(typ Type, i interface{}) interface{} { 156 p := (*eface)(unsafe.Pointer(&i)) 157 v := *(*int8)(p.word) 158 return *(*interface{})(unsafe.Pointer(&eface{ 159 typ: unsafe.Pointer(typ), 160 word: unsafe.Pointer(&v), 161 })) 162 } 163 164 func ConvertInt16(typ Type, i interface{}) interface{} { 165 p := (*eface)(unsafe.Pointer(&i)) 166 v := *(*int16)(p.word) 167 return *(*interface{})(unsafe.Pointer(&eface{ 168 typ: unsafe.Pointer(typ), 169 word: unsafe.Pointer(&v), 170 })) 171 } 172 173 func ConvertInt32(typ Type, i interface{}) interface{} { 174 p := (*eface)(unsafe.Pointer(&i)) 175 v := *(*int32)(p.word) 176 return *(*interface{})(unsafe.Pointer(&eface{ 177 typ: unsafe.Pointer(typ), 178 word: unsafe.Pointer(&v), 179 })) 180 } 181 182 func ConvertInt64(typ Type, i interface{}) interface{} { 183 p := (*eface)(unsafe.Pointer(&i)) 184 v := *(*int64)(p.word) 185 return *(*interface{})(unsafe.Pointer(&eface{ 186 typ: unsafe.Pointer(typ), 187 word: unsafe.Pointer(&v), 188 })) 189 } 190 191 func ConvertUint(typ Type, i interface{}) interface{} { 192 p := (*eface)(unsafe.Pointer(&i)) 193 v := *(*uint)(p.word) 194 return *(*interface{})(unsafe.Pointer(&eface{ 195 typ: unsafe.Pointer(typ), 196 word: unsafe.Pointer(&v), 197 })) 198 } 199 200 func ConvertUint8(typ Type, i interface{}) interface{} { 201 p := (*eface)(unsafe.Pointer(&i)) 202 v := *(*uint8)(p.word) 203 return *(*interface{})(unsafe.Pointer(&eface{ 204 typ: unsafe.Pointer(typ), 205 word: unsafe.Pointer(&v), 206 })) 207 } 208 209 func ConvertUint16(typ Type, i interface{}) interface{} { 210 p := (*eface)(unsafe.Pointer(&i)) 211 v := *(*uint16)(p.word) 212 return *(*interface{})(unsafe.Pointer(&eface{ 213 typ: unsafe.Pointer(typ), 214 word: unsafe.Pointer(&v), 215 })) 216 } 217 218 func ConvertUint32(typ Type, i interface{}) interface{} { 219 p := (*eface)(unsafe.Pointer(&i)) 220 v := *(*uint32)(p.word) 221 return *(*interface{})(unsafe.Pointer(&eface{ 222 typ: unsafe.Pointer(typ), 223 word: unsafe.Pointer(&v), 224 })) 225 } 226 227 func ConvertUint64(typ Type, i interface{}) interface{} { 228 p := (*eface)(unsafe.Pointer(&i)) 229 v := *(*uint64)(p.word) 230 return *(*interface{})(unsafe.Pointer(&eface{ 231 typ: unsafe.Pointer(typ), 232 word: unsafe.Pointer(&v), 233 })) 234 } 235 236 func ConvertUintptr(typ Type, i interface{}) interface{} { 237 p := (*eface)(unsafe.Pointer(&i)) 238 v := *(*uintptr)(p.word) 239 return *(*interface{})(unsafe.Pointer(&eface{ 240 typ: unsafe.Pointer(typ), 241 word: unsafe.Pointer(&v), 242 })) 243 } 244 245 func ConvertFloat32(typ Type, i interface{}) interface{} { 246 p := (*eface)(unsafe.Pointer(&i)) 247 v := *(*float32)(p.word) 248 return *(*interface{})(unsafe.Pointer(&eface{ 249 typ: unsafe.Pointer(typ), 250 word: unsafe.Pointer(&v), 251 })) 252 } 253 254 func ConvertFloat64(typ Type, i interface{}) interface{} { 255 p := (*eface)(unsafe.Pointer(&i)) 256 v := *(*float64)(p.word) 257 return *(*interface{})(unsafe.Pointer(&eface{ 258 typ: unsafe.Pointer(typ), 259 word: unsafe.Pointer(&v), 260 })) 261 } 262 263 func ConvertComplex64(typ Type, i interface{}) interface{} { 264 p := (*eface)(unsafe.Pointer(&i)) 265 v := *(*complex64)(p.word) 266 return *(*interface{})(unsafe.Pointer(&eface{ 267 typ: unsafe.Pointer(typ), 268 word: unsafe.Pointer(&v), 269 })) 270 } 271 272 func ConvertComplex128(typ Type, i interface{}) interface{} { 273 p := (*eface)(unsafe.Pointer(&i)) 274 v := *(*complex128)(p.word) 275 return *(*interface{})(unsafe.Pointer(&eface{ 276 typ: unsafe.Pointer(typ), 277 word: unsafe.Pointer(&v), 278 })) 279 } 280 281 func ConvertString(typ Type, i interface{}) interface{} { 282 p := (*eface)(unsafe.Pointer(&i)) 283 v := *(*string)(p.word) 284 return *(*interface{})(unsafe.Pointer(&eface{ 285 typ: unsafe.Pointer(typ), 286 word: unsafe.Pointer(&v), 287 })) 288 } 289 290 func Not(i interface{}) interface{} { 291 p := (*eface)(unsafe.Pointer(&i)) 292 v := !*(*bool)(p.word) 293 return *(*interface{})(unsafe.Pointer(&eface{ 294 typ: p.typ, 295 word: unsafe.Pointer(&v), 296 })) 297 } 298 299 func NegInt(i interface{}) interface{} { 300 p := (*eface)(unsafe.Pointer(&i)) 301 v := -*(*int)(p.word) 302 return *(*interface{})(unsafe.Pointer(&eface{ 303 typ: p.typ, 304 word: unsafe.Pointer(&v), 305 })) 306 } 307 308 func NegInt8(i interface{}) interface{} { 309 p := (*eface)(unsafe.Pointer(&i)) 310 v := -*(*int8)(p.word) 311 return *(*interface{})(unsafe.Pointer(&eface{ 312 typ: p.typ, 313 word: unsafe.Pointer(&v), 314 })) 315 } 316 317 func NegInt16(i interface{}) interface{} { 318 p := (*eface)(unsafe.Pointer(&i)) 319 v := -*(*int16)(p.word) 320 return *(*interface{})(unsafe.Pointer(&eface{ 321 typ: p.typ, 322 word: unsafe.Pointer(&v), 323 })) 324 } 325 326 func NegInt32(i interface{}) interface{} { 327 p := (*eface)(unsafe.Pointer(&i)) 328 v := -*(*int32)(p.word) 329 return *(*interface{})(unsafe.Pointer(&eface{ 330 typ: p.typ, 331 word: unsafe.Pointer(&v), 332 })) 333 } 334 335 func NegInt64(i interface{}) interface{} { 336 p := (*eface)(unsafe.Pointer(&i)) 337 v := -*(*int64)(p.word) 338 return *(*interface{})(unsafe.Pointer(&eface{ 339 typ: p.typ, 340 word: unsafe.Pointer(&v), 341 })) 342 } 343 344 func NegUint(i interface{}) interface{} { 345 p := (*eface)(unsafe.Pointer(&i)) 346 v := -*(*uint)(p.word) 347 return *(*interface{})(unsafe.Pointer(&eface{ 348 typ: p.typ, 349 word: unsafe.Pointer(&v), 350 })) 351 } 352 353 func NegUint8(i interface{}) interface{} { 354 p := (*eface)(unsafe.Pointer(&i)) 355 v := -*(*uint8)(p.word) 356 return *(*interface{})(unsafe.Pointer(&eface{ 357 typ: p.typ, 358 word: unsafe.Pointer(&v), 359 })) 360 } 361 362 func NegUint16(i interface{}) interface{} { 363 p := (*eface)(unsafe.Pointer(&i)) 364 v := -*(*uint16)(p.word) 365 return *(*interface{})(unsafe.Pointer(&eface{ 366 typ: p.typ, 367 word: unsafe.Pointer(&v), 368 })) 369 } 370 371 func NegUint32(i interface{}) interface{} { 372 p := (*eface)(unsafe.Pointer(&i)) 373 v := -*(*uint32)(p.word) 374 return *(*interface{})(unsafe.Pointer(&eface{ 375 typ: p.typ, 376 word: unsafe.Pointer(&v), 377 })) 378 } 379 380 func NegUint64(i interface{}) interface{} { 381 p := (*eface)(unsafe.Pointer(&i)) 382 v := -*(*uint64)(p.word) 383 return *(*interface{})(unsafe.Pointer(&eface{ 384 typ: p.typ, 385 word: unsafe.Pointer(&v), 386 })) 387 } 388 389 func NegUintptr(i interface{}) interface{} { 390 p := (*eface)(unsafe.Pointer(&i)) 391 v := -*(*uintptr)(p.word) 392 return *(*interface{})(unsafe.Pointer(&eface{ 393 typ: p.typ, 394 word: unsafe.Pointer(&v), 395 })) 396 } 397 398 func NegFloat32(i interface{}) interface{} { 399 p := (*eface)(unsafe.Pointer(&i)) 400 v := -*(*float32)(p.word) 401 return *(*interface{})(unsafe.Pointer(&eface{ 402 typ: p.typ, 403 word: unsafe.Pointer(&v), 404 })) 405 } 406 407 func NegFloat64(i interface{}) interface{} { 408 p := (*eface)(unsafe.Pointer(&i)) 409 v := -*(*float64)(p.word) 410 return *(*interface{})(unsafe.Pointer(&eface{ 411 typ: p.typ, 412 word: unsafe.Pointer(&v), 413 })) 414 } 415 416 func NegComplex64(i interface{}) interface{} { 417 p := (*eface)(unsafe.Pointer(&i)) 418 v := -*(*complex64)(p.word) 419 return *(*interface{})(unsafe.Pointer(&eface{ 420 typ: p.typ, 421 word: unsafe.Pointer(&v), 422 })) 423 } 424 425 func NegComplex128(i interface{}) interface{} { 426 p := (*eface)(unsafe.Pointer(&i)) 427 v := -*(*complex128)(p.word) 428 return *(*interface{})(unsafe.Pointer(&eface{ 429 typ: p.typ, 430 word: unsafe.Pointer(&v), 431 })) 432 } 433 434 func XorInt(i interface{}) interface{} { 435 p := (*eface)(unsafe.Pointer(&i)) 436 v := ^*(*int)(p.word) 437 return *(*interface{})(unsafe.Pointer(&eface{ 438 typ: p.typ, 439 word: unsafe.Pointer(&v), 440 })) 441 } 442 443 func XorInt8(i interface{}) interface{} { 444 p := (*eface)(unsafe.Pointer(&i)) 445 v := ^*(*int8)(p.word) 446 return *(*interface{})(unsafe.Pointer(&eface{ 447 typ: p.typ, 448 word: unsafe.Pointer(&v), 449 })) 450 } 451 452 func XorInt16(i interface{}) interface{} { 453 p := (*eface)(unsafe.Pointer(&i)) 454 v := ^*(*int16)(p.word) 455 return *(*interface{})(unsafe.Pointer(&eface{ 456 typ: p.typ, 457 word: unsafe.Pointer(&v), 458 })) 459 } 460 461 func XorInt32(i interface{}) interface{} { 462 p := (*eface)(unsafe.Pointer(&i)) 463 v := ^*(*int32)(p.word) 464 return *(*interface{})(unsafe.Pointer(&eface{ 465 typ: p.typ, 466 word: unsafe.Pointer(&v), 467 })) 468 } 469 470 func XorInt64(i interface{}) interface{} { 471 p := (*eface)(unsafe.Pointer(&i)) 472 v := ^*(*int64)(p.word) 473 return *(*interface{})(unsafe.Pointer(&eface{ 474 typ: p.typ, 475 word: unsafe.Pointer(&v), 476 })) 477 } 478 479 func XorUint(i interface{}) interface{} { 480 p := (*eface)(unsafe.Pointer(&i)) 481 v := ^*(*uint)(p.word) 482 return *(*interface{})(unsafe.Pointer(&eface{ 483 typ: p.typ, 484 word: unsafe.Pointer(&v), 485 })) 486 } 487 488 func XorUint8(i interface{}) interface{} { 489 p := (*eface)(unsafe.Pointer(&i)) 490 v := ^*(*uint8)(p.word) 491 return *(*interface{})(unsafe.Pointer(&eface{ 492 typ: p.typ, 493 word: unsafe.Pointer(&v), 494 })) 495 } 496 497 func XorUint16(i interface{}) interface{} { 498 p := (*eface)(unsafe.Pointer(&i)) 499 v := ^*(*uint16)(p.word) 500 return *(*interface{})(unsafe.Pointer(&eface{ 501 typ: p.typ, 502 word: unsafe.Pointer(&v), 503 })) 504 } 505 506 func XorUint32(i interface{}) interface{} { 507 p := (*eface)(unsafe.Pointer(&i)) 508 v := ^*(*uint32)(p.word) 509 return *(*interface{})(unsafe.Pointer(&eface{ 510 typ: p.typ, 511 word: unsafe.Pointer(&v), 512 })) 513 } 514 515 func XorUint64(i interface{}) interface{} { 516 p := (*eface)(unsafe.Pointer(&i)) 517 v := ^*(*uint64)(p.word) 518 return *(*interface{})(unsafe.Pointer(&eface{ 519 typ: p.typ, 520 word: unsafe.Pointer(&v), 521 })) 522 } 523 524 func XorUintptr(i interface{}) interface{} { 525 p := (*eface)(unsafe.Pointer(&i)) 526 v := ^*(*uintptr)(p.word) 527 return *(*interface{})(unsafe.Pointer(&eface{ 528 typ: p.typ, 529 word: unsafe.Pointer(&v), 530 })) 531 } 532 533 func MakeBool(typ Type, v bool) interface{} { 534 return *(*interface{})(unsafe.Pointer(&eface{ 535 typ: unsafe.Pointer(typ), 536 word: unsafe.Pointer(&v), 537 })) 538 } 539 540 func MakeInt(typ Type, v int) interface{} { 541 return *(*interface{})(unsafe.Pointer(&eface{ 542 typ: unsafe.Pointer(typ), 543 word: unsafe.Pointer(&v), 544 })) 545 } 546 547 func MakeInt8(typ Type, v int8) interface{} { 548 return *(*interface{})(unsafe.Pointer(&eface{ 549 typ: unsafe.Pointer(typ), 550 word: unsafe.Pointer(&v), 551 })) 552 } 553 554 func MakeInt16(typ Type, v int16) interface{} { 555 return *(*interface{})(unsafe.Pointer(&eface{ 556 typ: unsafe.Pointer(typ), 557 word: unsafe.Pointer(&v), 558 })) 559 } 560 561 func MakeInt32(typ Type, v int32) interface{} { 562 return *(*interface{})(unsafe.Pointer(&eface{ 563 typ: unsafe.Pointer(typ), 564 word: unsafe.Pointer(&v), 565 })) 566 } 567 568 func MakeInt64(typ Type, v int64) interface{} { 569 return *(*interface{})(unsafe.Pointer(&eface{ 570 typ: unsafe.Pointer(typ), 571 word: unsafe.Pointer(&v), 572 })) 573 } 574 575 func MakeUint(typ Type, v uint) interface{} { 576 return *(*interface{})(unsafe.Pointer(&eface{ 577 typ: unsafe.Pointer(typ), 578 word: unsafe.Pointer(&v), 579 })) 580 } 581 582 func MakeUint8(typ Type, v uint8) interface{} { 583 return *(*interface{})(unsafe.Pointer(&eface{ 584 typ: unsafe.Pointer(typ), 585 word: unsafe.Pointer(&v), 586 })) 587 } 588 589 func MakeUint16(typ Type, v uint16) interface{} { 590 return *(*interface{})(unsafe.Pointer(&eface{ 591 typ: unsafe.Pointer(typ), 592 word: unsafe.Pointer(&v), 593 })) 594 } 595 596 func MakeUint32(typ Type, v uint32) interface{} { 597 return *(*interface{})(unsafe.Pointer(&eface{ 598 typ: unsafe.Pointer(typ), 599 word: unsafe.Pointer(&v), 600 })) 601 } 602 603 func MakeUint64(typ Type, v uint64) interface{} { 604 return *(*interface{})(unsafe.Pointer(&eface{ 605 typ: unsafe.Pointer(typ), 606 word: unsafe.Pointer(&v), 607 })) 608 } 609 610 func MakeUintptr(typ Type, v uintptr) interface{} { 611 return *(*interface{})(unsafe.Pointer(&eface{ 612 typ: unsafe.Pointer(typ), 613 word: unsafe.Pointer(&v), 614 })) 615 } 616 617 func MakeFloat32(typ Type, v float32) interface{} { 618 return *(*interface{})(unsafe.Pointer(&eface{ 619 typ: unsafe.Pointer(typ), 620 word: unsafe.Pointer(&v), 621 })) 622 } 623 624 func MakeFloat64(typ Type, v float64) interface{} { 625 return *(*interface{})(unsafe.Pointer(&eface{ 626 typ: unsafe.Pointer(typ), 627 word: unsafe.Pointer(&v), 628 })) 629 } 630 631 func MakeComplex64(typ Type, v complex64) interface{} { 632 return *(*interface{})(unsafe.Pointer(&eface{ 633 typ: unsafe.Pointer(typ), 634 word: unsafe.Pointer(&v), 635 })) 636 } 637 638 func MakeComplex128(typ Type, v complex128) interface{} { 639 return *(*interface{})(unsafe.Pointer(&eface{ 640 typ: unsafe.Pointer(typ), 641 word: unsafe.Pointer(&v), 642 })) 643 } 644 645 func MakeString(typ Type, v string) interface{} { 646 return *(*interface{})(unsafe.Pointer(&eface{ 647 typ: unsafe.Pointer(typ), 648 word: unsafe.Pointer(&v), 649 })) 650 } 651 652 func Alloc(typ Type) interface{} { 653 ptr := unsafe_New(typ) 654 return *(*interface{})(unsafe.Pointer(&eface{ 655 typ: unsafe.Pointer(typ), 656 word: ptr, 657 })) 658 } 659 660 func New(typ, ptrto Type) interface{} { 661 ptr := unsafe_New(typ) 662 return *(*interface{})(unsafe.Pointer(&eface{ 663 typ: unsafe.Pointer(ptrto), 664 word: ptr, 665 })) 666 } 667 668 func NewPointer(typ Type) unsafe.Pointer { 669 return unsafe_New(typ) 670 } 671 672 func SetPointer(i interface{}, word unsafe.Pointer) interface{} { 673 p := (*eface)(unsafe.Pointer(&i)) 674 p.word = word 675 return i 676 } 677 678 func SetType(i interface{}, typ Type) interface{} { 679 p := (*eface)(unsafe.Pointer(&i)) 680 p.typ = unsafe.Pointer(typ) 681 return i 682 }