summaryrefslogtreecommitdiff
path: root/chapter31/src/main/smp/sys/rt/simple_args.smp
blob: 6f857d912a2d49b18bbb1396f6e82a2e9ec30cfa (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
// Native startup support, compiled independently and linked with the CRT.
// Raw native addresses stay i64; only the arrays allocated here are Simple objects.
// This startup object is built without sys imports.
u32 _MAX_U32 = (1<<32)-1;
{ i64 i64 -> i64 } simple_argv = "C";
{ i64 -> i64 } strlen = "C";
{ i64 i64 i64 -> i64 } memcpy = "C";

// Returns a constant argc-length array, filled with not-null constant u8
// arrays.
val simple_args = { i64 argc, i64 argv ->
    if( argc < 0 || argc > _MAX_U32 ) return null;
    // Build an initialized
    return new u8[][argc] { int i ->
        i64 src = simple_argv(argv,i);
        i64 len = strlen(src);
        if( !(0 < len <= _MAX_U32) ) // If not the u32 range, just use zero-length
            len = 0;
        val bytes = new u8[len];
        i64 dst = bytes;
        memcpy(dst,src,len);    // Copy from C memory to Simple memory
        return bytes;
    };
};