1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.*;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.util.Ary;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;
import static org.junit.Assert.*;
public class ReceiverTest {
@Test public void classAndInstanceFields() throws Exception {
String source="""
int total=1;
val forward={ -> late(); };
val early={ -> total; };
val late={ -> early(); };
val add_noInline={int n -> self.total=self.total+n; return total;};
val read_noInline={ -> self.total; };
val nested={ ->
val inc={ -> self.total++; };
inc();
return total;
};
struct Counter {
int total=100;
val read=read_noInline;
};
struct Writer {
int count=2;
val step={ -> self.count++; total=count; return total; };
};
u16[] !table=new u16[2];
val lookup={u8 c -> c<2 ? table[c] : 0;};
val main={ ->
total=1;
table=new u16[2]; table[0]=7; table[1]=9;
if( forward()!=1 ) return 4;
if( lookup(1)!=9 || lookup(2)!=0 ) return 5;
if( add_noInline(5)!=6 || nested()!=7 ) return 1;
val counter=new Counter;
if( counter.read()!=7 || counter.total!=100 ) return 2;
val a=new Writer, b=new Writer;
if( a.step()!=3 || total!=3 || a.count!=3 || b.count!=2 ) return 3;
return 0;
};
return main();
""";
assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.TypeCheck),0));
TestC.runSF(source,"class_instance_receivers","",-1);
}
@Test public void importedCallbackKeepsItsClass() throws Exception {
Path build=Path.of("build/objs/receivers");
Files.createDirectories(build);
Path lib=Files.createTempDirectory(build,"classes-");
new CodeGen(null,lib.toString(),null,"Owner",
"int value=7; val get_noInline={ -> self.value; };",123,true,TypeInteger.BOT)
.driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
new CodeGen(null,lib.toString(),null,"Forwarder",
"int value=99; val apply_noInline={ {int} action -> action()+self.value; };",123,true,TypeInteger.BOT)
.driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
var paths=new Ary<>(new String[]{lib.toString()});
String source="return Forwarder.apply_noInline(Owner.get_noInline)==106 ? 0 : 1;";
CodeGen code=new CodeGen(null,null,paths,"ReceiverClient",source,123,true,TypeInteger.BOT)
.driver(CodeGen.Phase.TypeCheck);
assertEquals("0",Eval2.eval(code,0));
TestC.run(source,"imported_receivers",paths,TestC.CALL_CONVENTION,null,null,"",-1);
}
@Test public void classReceiverHasNoNativeArgument() throws Exception {
String source="""
int base=10;
val calc={int a,int b,int c,int d,int e,int f,int g ->
self.base+a+2*b+3*c+4*d+5*e+6*f+7*g;
};
val callback_noInline={int n -> self.base+n;};
{ {int -> i64} int -> i64 } invoke="C";
val viaC={int n -> invoke(callback_noInline,n);};
""";
Path build=Path.of("build/objs/receivers");
Files.createDirectories(build);
Path driver=build.resolve("native_receiver.c");
Files.writeString(driver,"""
typedef long long (__attribute__((CALL_CONV)) *action_t)(int);
__attribute__((CALL_CONV)) long long invoke(action_t action, int n) { return action(n); }
__attribute__((CALL_CONV)) extern long long calc(int,int,int,int,int,int,int);
__attribute__((CALL_CONV)) extern long long viaC(int);
int main(void) { return calc(1,2,3,4,5,6,7)!=150 || viaC(5)!=15; }
""");
TestC.run(source,"native_receiver",null,TestC.CALL_CONVENTION,
TestC.CALL_CONVENTION.equals("win64") ? "ms_abi" : "sysv_abi",driver.toString(),"",-1);
}
}
|