cstring is ONE pointer to a null-terminated u8 buffer, C's char*: thin (8 bytes, no length; cstring_len walks to the terminator), crossing #foreign boundaries verbatim in both directions, with ?cstring as the nullable case lowering to the same bare pointer (null = absent). Conversion discipline mirrors Odin: a string LITERAL coerces implicitly (its bytes are terminated constants); any other string is rejected with a diagnostic naming to_cstring (it may be an unterminated view); and cstring never coerces to string implicitly — from_cstring(c) is the explicit zero-copy view, pricing the strlen. Plumbing: TypeId/TypeInfo builtin slot 18 (first_user 19), name classifiers, size/align/name tables, LLVM ptr lowering, the ?T pointer niche, the xx pointer ladder, the literal-gated coercion plan (isConstString + data_ptr), and the reserved-spelling set. std gains cstring_len/from_cstring/to_cstring (fmt.sx, re-exported); the old cstring(size) allocator helper is renamed alloc_string everywhere; getenv migrates to (name: cstring) -> ?cstring as the canonical user and env() drops its manual strlen/memcpy. Pinned: examples/1222 (FFI both directions, literal coercion, ?cstring null paths, round trip) and examples/1173 (both coercion diagnostics); FAIL pre-feature. The alloc_string rename + getenv signature shift the .ir snapshots — regenerated. zig build test 426/426; run_examples 604/604. Spec: reserved spelling + cstring section + C-interop rows.
39 lines
1.5 KiB
Plaintext
39 lines
1.5 KiB
Plaintext
// The `cstring` type: ONE pointer to a null-terminated u8 buffer — C's
|
|
// `char *`. Crosses #foreign boundaries verbatim in both directions;
|
|
// `?cstring` is the nullable case (null pointer = absent); string
|
|
// LITERALS coerce implicitly (terminated constants); arbitrary strings
|
|
// materialize via to_cstring; from_cstring is the zero-copy view back.
|
|
#import "modules/std.sx";
|
|
|
|
libc :: #library "c";
|
|
strerror_c :: (code: i32) -> cstring #foreign libc "strerror";
|
|
getenv_c :: (name: cstring) -> ?cstring #foreign libc "getenv";
|
|
dlerror_c :: () -> ?cstring #foreign libc "dlerror";
|
|
|
|
main :: () -> i32 {
|
|
// literal -> cstring param; cstring return -> view
|
|
e := strerror_c(2);
|
|
v := from_cstring(e);
|
|
if v.len < 5 { print("BUG: strerror short\n"); return 1; }
|
|
print("strerror ok\n");
|
|
|
|
// ?cstring: present, absent, and null-returning C call
|
|
p := getenv_c("PATH");
|
|
if p == null { print("BUG: PATH null\n"); return 2; }
|
|
if cstring_len(p!) < 1 { print("BUG: PATH empty\n"); return 3; }
|
|
q := getenv_c("NO_SUCH_VAR_ZZZ");
|
|
if q != null { print("BUG: missing not null\n"); return 4; }
|
|
d := dlerror_c();
|
|
if d != null { print("BUG: dlerror non-null\n"); return 5; }
|
|
print("?cstring ok\n");
|
|
|
|
// round trip: built string -> owned cstring -> view -> equality
|
|
s := concat("hi-", "there");
|
|
c := to_cstring(s);
|
|
r := from_cstring(c);
|
|
if r != "hi-there" { print("BUG: round trip\n"); return 6; }
|
|
if cstring_len(c) != 8 { print("BUG: len\n"); return 7; }
|
|
print("round trip ok\n");
|
|
return 0;
|
|
}
|