lang: introduce cstring — the C-boundary string (Odin model)
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.
This commit is contained in:
@@ -24,8 +24,7 @@ popen :: (cmd: [:0]u8, mode: [:0]u8) -> *void #foreign libc;
|
||||
pclose :: (stream: *void) -> i32 #foreign libc;
|
||||
fread :: (ptr: [*]u8, size: usize, nmemb: usize, stream: *void) -> usize #foreign libc;
|
||||
feof :: (stream: *void) -> i32 #foreign libc;
|
||||
getenv :: (name: [:0]u8) -> *u8 #foreign libc;
|
||||
strlen :: (s: *u8) -> usize #foreign libc;
|
||||
getenv :: (name: cstring) -> ?cstring #foreign libc;
|
||||
system :: (cmd: [:0]u8) -> i32 #foreign libc;
|
||||
|
||||
// ── Public types ─────────────────────────────────────────────────────
|
||||
@@ -53,7 +52,7 @@ run :: (cmd: [:0]u8) -> ?ProcessResult {
|
||||
if cast(i64) f == 0 { return null; }
|
||||
|
||||
out := "";
|
||||
buf := cstring(4096);
|
||||
buf := alloc_string(4096);
|
||||
loop := true;
|
||||
while loop {
|
||||
n := fread(buf.ptr, 1, 4096, f);
|
||||
@@ -84,14 +83,11 @@ run :: (cmd: [:0]u8) -> ?ProcessResult {
|
||||
// Read an environment variable. Returns null if unset; an empty
|
||||
// string if set to "".
|
||||
env :: (name: [:0]u8) -> ?string {
|
||||
p := getenv(name);
|
||||
addr : i64 = xx p;
|
||||
if addr == 0 { return null; }
|
||||
n := strlen(p);
|
||||
if n == 0 { return ""; }
|
||||
buf := cstring(cast(i64) n);
|
||||
memcpy(buf.ptr, xx p, cast(i64) n);
|
||||
buf
|
||||
p := getenv(to_cstring(name));
|
||||
if p == null { return null; }
|
||||
v := from_cstring(p!);
|
||||
if v.len == 0 { return ""; }
|
||||
substr(v, 0, v.len)
|
||||
}
|
||||
|
||||
// Locate an executable by walking `$PATH`. Returns the absolute path
|
||||
@@ -103,7 +99,7 @@ find_executable :: (name: [:0]u8) -> ?string {
|
||||
// (executable names like `codesign`, `javac`, `aapt2`).
|
||||
cmd := concat("command -v ", name);
|
||||
// Need null-terminated for popen.
|
||||
cmd_z := cstring(cmd.len);
|
||||
cmd_z := alloc_string(cmd.len);
|
||||
memcpy(cmd_z.ptr, cmd.ptr, cmd.len);
|
||||
if r := run(cmd_z) {
|
||||
if r.exit_code != 0 { return null; }
|
||||
|
||||
Reference in New Issue
Block a user