gopherjs:replace
(typ Type, fn func(args []Value) (results []Value))
| 353 | |
| 354 | //gopherjs:replace |
| 355 | func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value { |
| 356 | if typ.Kind() != Func { |
| 357 | panic("reflect: call of MakeFunc with non-Func type") |
| 358 | } |
| 359 | |
| 360 | t := typ.common() |
| 361 | ftyp := (*funcType)(unsafe.Pointer(t)) |
| 362 | |
| 363 | fv := js.MakeFunc(func(this *js.Object, arguments []*js.Object) any { |
| 364 | // Convert raw JS arguments into []Value the user-supplied function expects. |
| 365 | args := make([]Value, ftyp.NumIn()) |
| 366 | for i := range args { |
| 367 | argType := ftyp.In(i) |
| 368 | args[i] = makeValue(argType, arguments[i], 0) |
| 369 | } |
| 370 | |
| 371 | // Call the user-supplied function. |
| 372 | resultsSlice := fn(args) |
| 373 | |
| 374 | // Verify that returned value types are compatible with the function type specified by the caller. |
| 375 | if want, got := ftyp.NumOut(), len(resultsSlice); want != got { |
| 376 | panic("reflect: expected " + strconv.Itoa(want) + " return values, got " + strconv.Itoa(got)) |
| 377 | } |
| 378 | for i, rtyp := range ftyp.OutSlice() { |
| 379 | if !resultsSlice[i].Type().AssignableTo(toRType(rtyp)) { |
| 380 | panic("reflect: " + strconv.Itoa(i) + " return value type is not compatible with the function declaration") |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Rearrange return values according to the expected function signature. |
| 385 | switch ftyp.NumOut() { |
| 386 | case 0: |
| 387 | return nil |
| 388 | case 1: |
| 389 | return resultsSlice[0].object() |
| 390 | default: |
| 391 | results := js.Global.Get("Array").New(ftyp.NumOut()) |
| 392 | for i, r := range resultsSlice { |
| 393 | results.SetIndex(i, r.object()) |
| 394 | } |
| 395 | return results |
| 396 | } |
| 397 | }) |
| 398 | |
| 399 | return Value{t, unsafe.Pointer(fv.Unsafe()), flag(Func)} |
| 400 | } |
| 401 | |
| 402 | //gopherjs:replace |
| 403 | func typedmemmove(t *abi.Type, dst, src unsafe.Pointer) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…