github.com/gotranspile/cxgo@v0.3.7/funcs_test.go (about)

     1  package cxgo
     2  
     3  import "testing"
     4  
     5  var casesTranslateFuncs = []parseCase{
     6  	{
     7  		name: "func ptr",
     8  		src: `
     9  int foo() {
    10  	int (*a)();
    11  	a = &foo;
    12  	return a != 0;
    13  }
    14  `,
    15  		exp: `
    16  func foo() int32 {
    17  	var a func() int32
    18  	a = foo
    19  	return libc.BoolToInt(a != nil)
    20  }
    21  `,
    22  	},
    23  	{
    24  		name: "wrong func ptr",
    25  		src: `
    26  void foo() {
    27  	int (*a)();
    28  	a = &foo;
    29  	return 1;
    30  }
    31  `,
    32  		exp: `
    33  func foo() {
    34  	var a func() int32
    35  	_ = a
    36  	a = foo
    37  	return 1
    38  }
    39  `,
    40  	},
    41  	{
    42  		name: "call func from array",
    43  		src: `
    44  void foo(void) {}
    45  
    46  static void (*functions[1])(void) = {
    47      foo,
    48  };
    49  
    50  void foo2() {
    51      functions[0]();
    52  }
    53  `,
    54  		exp: `
    55  func foo() {
    56  }
    57  
    58  var functions [1]*func() = [1]*func(){(*func())(unsafe.Pointer(libc.FuncAddr(foo)))}
    59  
    60  func foo2() {
    61  	libc.AsFunc(functions[0], (*func())(nil)).(func())()
    62  }
    63  `,
    64  	},
    65  }
    66  
    67  func TestFunctions(t *testing.T) {
    68  	runTestTranslate(t, casesTranslateFuncs)
    69  }