MCPcopy
hub / github.com/gopherjs/gopherjs / MakeFunc

Function MakeFunc

compiler/natives/src/reflect/reflect.go:355–400  ·  view source on GitHub ↗

gopherjs:replace

(typ Type, fn func(args []Value) (results []Value))

Source from the content-addressed store, hash-verified

353
354//gopherjs:replace
355func 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
403func typedmemmove(t *abi.Type, dst, src unsafe.Pointer) {

Callers

nothing calls this directly

Calls 12

MakeFuncFunction · 0.92
toRTypeFunction · 0.85
PointerMethod · 0.80
OutSliceMethod · 0.80
UnsafeMethod · 0.80
makeValueFunction · 0.70
KindMethod · 0.65
TypeMethod · 0.65
GetMethod · 0.65
objectMethod · 0.45
NewMethod · 0.45
SetIndexMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…