wa-lang.org/wazero@v1.0.2/internal/integration_test/bench/testdata/case.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/base64"
     5  	"math/rand"
     6  	"reflect"
     7  	"strings"
     8  	"unsafe"
     9  )
    10  
    11  func main() {}
    12  
    13  //export allocate_buffer
    14  func allocateBuffer(size uint32) *byte {
    15  	// Allocate the in-Wasm memory region and returns its pointer to hosts.
    16  	// The region is supposed to store random strings generated in hosts,
    17  	// meaning that this is called "inside" of get_random_string.
    18  	buf := make([]byte, size)
    19  	return &buf[0]
    20  }
    21  
    22  //export get_random_string
    23  func getRandomStringRaw(retBufPtr **byte, retBufSize *int)
    24  
    25  // Get random string from the hosts.
    26  func getRandomString() string {
    27  	var bufPtr *byte
    28  	var bufSize int
    29  	getRandomStringRaw(&bufPtr, &bufSize)
    30  	//nolint
    31  	return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
    32  		Data: uintptr(unsafe.Pointer(bufPtr)),
    33  		Len:  uintptr(bufSize),
    34  		Cap:  uintptr(bufSize),
    35  	}))
    36  }
    37  
    38  //export base64
    39  func base64OnString(num uint32) {
    40  	// Get random strings from the host and
    41  	// do base64 encoding them for given times.
    42  	for i := uint32(0); i < num; i++ {
    43  		msg := getRandomString()
    44  		_ = base64.StdEncoding.EncodeToString([]byte(msg))
    45  	}
    46  }
    47  
    48  //export fibonacci
    49  func fibonacci(in uint32) uint32 {
    50  	if in <= 1 {
    51  		return in
    52  	}
    53  	return fibonacci(in-1) + fibonacci(in-2)
    54  }
    55  
    56  //export string_manipulation
    57  func stringManipulation(initialSize uint32) {
    58  	var str string
    59  	for i := 0; i < int(initialSize); i++ {
    60  		str += "a"
    61  		str += "b"
    62  	}
    63  	str2 := strings.Replace(str, "a", "b", -1)
    64  	str3 := str2[:3]
    65  	str4 := str2[3:]
    66  	lastStr := str4 + str3
    67  	lastStr = ""
    68  	print(lastStr)
    69  }
    70  
    71  //export reverse_array
    72  func reverseArray(size uint32) {
    73  	array := make([]int, size)
    74  	for i := 0; i < (len(array) >> 1); i++ {
    75  		j := len(array) - i - 1
    76  		array[j], array[i] = array[i], array[j]
    77  	}
    78  }
    79  
    80  //export random_mat_mul
    81  func randomMatMul(n uint32) {
    82  	var a, b, result [][]int
    83  	for i := uint32(0); i < n; i++ {
    84  		arow := make([]int, n)
    85  		brow := make([]int, n)
    86  		for j := uint32(0); j < n; j++ {
    87  			arow[j] = rand.Int()
    88  			brow[j] = rand.Int()
    89  		}
    90  		a = append(a, arow)
    91  		b = append(b, brow)
    92  		result = append(result, make([]int, n))
    93  	}
    94  
    95  	for i := uint32(0); i < n; i++ {
    96  		for j := uint32(0); j < n; j++ {
    97  			for k := uint32(0); k < n; k++ {
    98  				result[i][j] += a[i][k] * b[k][j]
    99  			}
   100  		}
   101  	}
   102  }