summaryrefslogtreecommitdiff
path: root/chapter28a/src/test/java/com/seaofnodes/simple/ReceiverTest.java
blob: 9384b42a60b89bfecb8b4e1d2f46d63c80623eb3 (plain)
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
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 sourceCallbackKeepsItsClass() throws Exception {
        Path build=Path.of("build/objs/receivers");
        Files.createDirectories(build);
        Path source=Files.createTempDirectory(build,"sources-");
        Files.createDirectories(source.resolve("Root"));
        Files.writeString(source.resolve("Root.smp"),"");
        Files.writeString(source.resolve("Root/Owner.smp"),
            "int value=7; val get_noInline={ -> self.value; };");
        Files.writeString(source.resolve("Root/Forwarder.smp"),
            "int value=99; val apply_noInline={ {int} action -> action()+self.value; };");
        Files.writeString(source.resolve("Root/Main.smp"),
            "return Forwarder.apply_noInline(Owner.get_noInline)==106 ? 0 : 1;");
        CodeGen code=new CodeGen(source.toString(),source.toString(),"Root/Main",null,123,true,TypeInteger.BOT)
            .driver(CodeGen.Phase.TypeCheck);
        assertEquals("0",Eval2.eval(code,0));
    }

    @Test public void classReceiverHasNoNativeArgument() throws Exception {
        String source="""
            int base=10;
            val calc={i64 a,i64 b,i64 c,i64 d,i64 e,i64 f,i64 g ->
                self.base+a+2*b+3*c+4*d+5*e+6*f+7*g;
            };
            val callback_noInline={i64 n -> self.base+n;};
            { {i64 -> i64} i64 -> i64 } invoke="C";
            val viaC={i64 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)(long long);
            __attribute__((CALL_CONV)) long long invoke(action_t action, long long n) { return action(n); }
            __attribute__((CALL_CONV)) extern long long calc(long long,long long,long long,long long,long long,long long,long long);
            __attribute__((CALL_CONV)) extern long long viaC(long long);
            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);
    }
}