github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/tests/testdata/linkname/method/method.go (about) 1 package method 2 3 import ( 4 "sort" 5 "strings" 6 "testing" 7 _ "unsafe" 8 ) 9 10 type Point struct { 11 X int 12 Y int 13 } 14 15 func (pt *Point) Set(x, y int) { 16 pt.X, pt.Y = x, y 17 } 18 19 func (pt Point) Get() (int, int) { 20 return pt.X, pt.Y 21 } 22 23 //go:linkname struct_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Point).Set 24 func struct_Set(pt *point, x int, y int) 25 26 //go:linkname struct_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Point.Get 27 func struct_Get(pt point) (int, int) 28 29 type point struct { 30 X int 31 Y int 32 } 33 34 func testStruct(t *testing.T) { 35 var pt point 36 struct_Set(&pt, 1, 2) 37 x, y := struct_Get(pt) 38 if x != 1 || y != 2 { 39 t.Fatalf("Got: struct_Get(pt) = (%v,%v). Want: (1,2).", x, y) 40 } 41 } 42 43 type List []string 44 45 func (t *List) Append(s ...string) { 46 *t = append(*t, s...) 47 } 48 49 func (t List) Get() string { 50 return strings.Join(t, ",") 51 } 52 53 type list []string 54 55 //go:linkname slice_Append github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*List).Append 56 func slice_Append(*list, ...string) 57 58 //go:linkname slice_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.List.Get 59 func slice_Get(list) string 60 61 func testSlice(t *testing.T) { 62 var v list 63 v = append(v, "one") 64 slice_Append(&v, "two", "three") 65 got := slice_Get(v) 66 want := "one,two,three" 67 if got != want { 68 t.Fatalf("Got: slice_Get(v) = %q. Want: %q.", got, want) 69 } 70 } 71 72 type Array [5]string 73 74 func (t *Array) Set(i int, s string) { 75 (*t)[i] = s 76 } 77 78 func (t Array) Get() string { 79 return strings.Join(t[:], ",") 80 } 81 82 type array [5]string 83 84 //go:linkname array_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Array).Set 85 func array_Set(*array, int, string) 86 87 //go:linkname array_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Array.Get 88 func array_Get(array) string 89 90 func testArray(t *testing.T) { 91 var a array 92 a[0] = "one" 93 array_Set(&a, 1, "two") 94 array_Set(&a, 4, "five") 95 got := array_Get(a) 96 want := "one,two,,,five" 97 if got != want { 98 t.Fatalf("Got: array_Get(a) = %q. Want: %q.", got, want) 99 } 100 } 101 102 type Map map[int]string 103 104 func (m Map) Set(key int, value string) { 105 m[key] = value 106 } 107 108 func (m *Map) SetPtr(key int, value string) { 109 (*m)[key] = value 110 } 111 112 func (m Map) Get() string { 113 var list []string 114 for _, v := range m { 115 list = append(list, v) 116 } 117 sort.Strings(list) 118 return strings.Join(list, ",") 119 } 120 121 type _map map[int]string 122 123 //go:linkname map_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Map.Set 124 func map_Set(_map, int, string) 125 126 //go:linkname map_SetPtr github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Map).SetPtr 127 func map_SetPtr(*_map, int, string) 128 129 //go:linkname map_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Map.Get 130 func map_Get(_map) string 131 132 func testMap(t *testing.T) { 133 m := make(_map) 134 map_Set(m, 1, "one") 135 map_SetPtr(&m, 2, "two") 136 got := map_Get(m) 137 want := "one,two" 138 if got != want { 139 t.Fatalf("Got: map_Get(m) = %q. Want: %q.", got, want) 140 } 141 } 142 143 type Func func(int, int) int 144 145 func (f Func) Call(a, b int) int { 146 return f(a, b) 147 } 148 149 func (f *Func) CallPtr(a, b int) int { 150 return (*f)(a, b) 151 } 152 153 type _func func(int, int) int 154 155 //go:linkname func_Call github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Func.Call 156 func func_Call(_func, int, int) int 157 158 //go:linkname func_CallPtr github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Func).CallPtr 159 func func_CallPtr(*_func, int, int) int 160 161 func testFunc(t *testing.T) { 162 var fn _func = func(a, b int) int { 163 return a + b 164 } 165 r := func_Call(fn, 100, 200) 166 if r != 300 { 167 t.Fatalf("Got: func_Call(fn,100,200) = %v. Want: 300.", r) 168 } 169 r2 := func_CallPtr(&fn, 100, 200) 170 if r2 != 300 { 171 t.Fatalf("Got: func_CallPtr(fn,100,200) = %v. Want: 300.", r2) 172 } 173 } 174 175 type Chan chan int 176 177 func (c Chan) Send(n int) { 178 c <- n 179 } 180 181 func (c *Chan) SendPtr(n int) { 182 *c <- n 183 } 184 185 func (c Chan) Recv() int { 186 return <-c 187 } 188 189 type _chan chan int 190 191 //go:linkname chan_Send github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Chan.Send 192 func chan_Send(_chan, int) 193 194 //go:linkname chan_SendPtr github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Chan).SendPtr 195 func chan_SendPtr(*_chan, int) 196 197 //go:linkname chan_Recv github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Chan.Recv 198 func chan_Recv(_chan) int 199 200 func testChan(t *testing.T) { 201 c := make(_chan) 202 go func() { 203 chan_Send(c, 100) 204 }() 205 r := chan_Recv(c) 206 if r != 100 { 207 t.Fatalf("Got: chan_Recv(c) = %v. Want: 100.", r) 208 } 209 go func() { 210 chan_SendPtr(&c, 200) 211 }() 212 r = chan_Recv(c) 213 if r != 200 { 214 t.Fatalf("Got: chan_Recv(c) = %v. Want: 200.", r) 215 } 216 } 217 218 type Int int 219 220 func (m *Int) Set(v int) { 221 *m = Int(v) 222 } 223 224 func (m Int) Get() int { 225 return int(m) 226 } 227 228 type _int int 229 230 //go:linkname int_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Int).Set 231 func int_Set(*_int, int) int 232 233 //go:linkname int_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Int.Get 234 func int_Get(_int) int 235 236 func testInt(t *testing.T) { 237 var i _int 238 int_Set(&i, 100) 239 r := int_Get(i) 240 if r != 100 { 241 t.Fatalf("Got: int_Get(i) = %v. Want: 100.", r) 242 } 243 } 244 245 type Uint uint 246 247 func (m *Uint) Set(v uint) { 248 *m = Uint(v) 249 } 250 251 func (m Uint) Get() uint { 252 return uint(m) 253 } 254 255 type _uint uint 256 257 //go:linkname uint_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Uint).Set 258 func uint_Set(*_uint, uint) uint 259 260 //go:linkname uint_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uint.Get 261 func uint_Get(_uint) uint 262 263 func testUint(t *testing.T) { 264 var i _uint 265 uint_Set(&i, 100) 266 r := uint_Get(i) 267 if r != 100 { 268 t.Fatalf("Got: uint_Get(i) = %v. Want: 100.", r) 269 } 270 } 271 272 type Float64 float64 273 274 func (m *Float64) Set(v float64) { 275 *m = Float64(v) 276 } 277 278 func (m Float64) Get() float64 { 279 return float64(m) 280 } 281 282 type _float64 float64 283 284 //go:linkname float64_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Float64).Set 285 func float64_Set(*_float64, float64) float64 286 287 //go:linkname float64_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Float64.Get 288 func float64_Get(_float64) float64 289 290 func testFloat64(t *testing.T) { 291 var i _float64 292 float64_Set(&i, 3.14) 293 r := float64_Get(i) 294 if r != 3.14 { 295 t.Fatalf("Got: float64_Get(i) = %v. Want: 3.14.", r) 296 } 297 } 298 299 type Complex128 complex128 300 301 func (m *Complex128) Set(v complex128) { 302 *m = Complex128(v) 303 } 304 305 func (m Complex128) Get() complex128 { 306 return complex128(m) 307 } 308 309 type _complex128 complex128 310 311 //go:linkname complex128_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Complex128).Set 312 func complex128_Set(*_complex128, complex128) complex128 313 314 //go:linkname complex128_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Complex128.Get 315 func complex128_Get(_complex128) complex128 316 317 func testComplex128(t *testing.T) { 318 var i _complex128 319 want := 1 + 2i 320 complex128_Set(&i, want) 321 got := complex128_Get(i) 322 if got != want { 323 t.Fatalf("Got: complex128_Get(i) = %v. Want: %v.", got, want) 324 } 325 } 326 327 type Uintptr uintptr 328 329 func (m *Uintptr) Set(v uintptr) { 330 *m = Uintptr(v) 331 } 332 333 func (m Uintptr) Get() uintptr { 334 return uintptr(m) 335 } 336 337 type _uintptr uintptr 338 339 //go:linkname uintptr_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Uintptr).Set 340 func uintptr_Set(*_uintptr, uintptr) uintptr 341 342 //go:linkname uintptr_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uintptr.Get 343 func uintptr_Get(_uintptr) uintptr 344 345 func testUintptr(t *testing.T) { 346 var i _uintptr 347 uintptr_Set(&i, 0x1234) 348 r := uintptr_Get(i) 349 if r != 0x1234 { 350 t.Fatalf("Got: uintptr_Get(i) = %v. Want: 0x1234.", r) 351 } 352 } 353 354 type Bool bool 355 356 func (m *Bool) Set(v bool) { 357 *m = Bool(v) 358 } 359 360 func (m Bool) Get() bool { 361 return bool(m) 362 } 363 364 type _bool bool 365 366 //go:linkname bool_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Bool).Set 367 func bool_Set(*_bool, bool) bool 368 369 //go:linkname bool_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Bool.Get 370 func bool_Get(_bool) bool 371 372 func testBool(t *testing.T) { 373 var i _bool 374 bool_Set(&i, true) 375 r := bool_Get(i) 376 if r != true { 377 t.Fatalf("Got: bool_Get(i) = %v. Want: true.", r) 378 } 379 } 380 381 type Byte byte 382 383 func (m *Byte) Set(v byte) { 384 *m = Byte(v) 385 } 386 387 func (m Byte) Get() byte { 388 return byte(m) 389 } 390 391 type _byte byte 392 393 //go:linkname byte_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Byte).Set 394 func byte_Set(*_byte, byte) byte 395 396 //go:linkname byte_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Byte.Get 397 func byte_Get(_byte) byte 398 399 func testByte(t *testing.T) { 400 var i _byte 401 byte_Set(&i, 0x7f) 402 r := byte_Get(i) 403 if r != 0x7f { 404 t.Fatalf("Got: byte_Get(i) = %v. Want: 0x7f.", r) 405 } 406 } 407 408 type String string 409 410 func (m *String) Set(v string) { 411 *m = String(v) 412 } 413 414 func (m String) Get() string { 415 return string(m) 416 } 417 418 type _string string 419 420 //go:linkname string_Set github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*String).Set 421 func string_Set(*_string, string) string 422 423 //go:linkname string_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.String.Get 424 func string_Get(_string) string 425 426 func testString(t *testing.T) { 427 var i _string 428 want := "hello world" 429 string_Set(&i, want) 430 got := string_Get(i) 431 if got != want { 432 t.Fatalf("Got: string_Get(i) = %q. Want: %q.", got, want) 433 } 434 } 435 436 func TestLinkname(t *testing.T) { 437 testStruct(t) 438 testSlice(t) 439 testArray(t) 440 testMap(t) 441 testFunc(t) 442 testChan(t) 443 testBool(t) 444 testByte(t) 445 testInt(t) 446 testUint(t) 447 testFloat64(t) 448 testComplex128(t) 449 testString(t) 450 }