github.com/goplusjs/reflectx@v0.5.4/icall_gen.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"strconv"
    11  	"strings"
    12  )
    13  
    14  var head = `// +build !js js,wasm
    15  // +build !go1.17 go1.17,!goexperiment.regabireflect
    16  
    17  package reflectx
    18  
    19  import (
    20  	"log"
    21  )
    22  
    23  var (
    24  	check_max_itype = true
    25  	check_max_index = true
    26  )
    27  
    28  func icall(t int, i int, max int, ptrto bool, output bool) interface{} {
    29  	if t >= max_itype_index {
    30  		if check_max_itype {
    31  			check_max_itype = false
    32  			log.Println("warning, too many types interface call >", t)
    33  		}
    34  		return func(p, a unsafeptr) {}
    35  	}
    36  	if i >= max_icall_index {
    37  		if check_max_index {
    38  			check_max_index = false
    39  			log.Println("warning, too many methods interface call >", i)
    40  		}
    41  		return func(p, a unsafeptr) {}
    42  	}
    43  	if ptrto {
    44  		return icall_ptr[t*max_icall_index+i]
    45  	} else {
    46  		return icall_typ[t*max_icall_index+i]
    47  	}
    48  }
    49  
    50  const max_itype_index = $max_itype
    51  const max_icall_index = $max_index
    52  `
    53  
    54  var templ_fn = `	func(p, a unsafeptr) { i_x($itype, $index, p, unsafeptr(&a), $ptr) },
    55  `
    56  
    57  func main() {
    58  	writeFile("./icall.go", 64, 256)
    59  }
    60  
    61  func writeFile(filename string, max_itype int, max_index int) {
    62  	var buf bytes.Buffer
    63  	r := strings.NewReplacer("$max_itype", strconv.Itoa(max_itype),
    64  		"$max_index", strconv.Itoa(max_index))
    65  	buf.WriteString(r.Replace(head))
    66  
    67  	fnWrite := func(name string, t string, ptr string) {
    68  		buf.WriteString(fmt.Sprintf("\nvar %v = []interface{}{\n", name))
    69  		for i := 0; i < max_itype; i++ {
    70  			for j := 0; j < max_index; j++ {
    71  				r := strings.NewReplacer("$itype", strconv.Itoa(i),
    72  					"$index", strconv.Itoa(j),
    73  					"$ptr", ptr)
    74  				buf.WriteString(r.Replace(t))
    75  			}
    76  		}
    77  		buf.WriteString("}\n")
    78  	}
    79  	fnWrite("icall_typ", templ_fn, "false")
    80  	fnWrite("icall_ptr", templ_fn, "true")
    81  
    82  	ioutil.WriteFile(filename, buf.Bytes(), 0666)
    83  }