Files
sx/library/modules/std.sx
agra 45d869da41 fibers B1.2: Io capability + context.io + blocking impl + Future/async/await/cancel
Threads an `Io` capability onto `Context` exactly like `Allocator`: a
`protocol #inline` whose process-wide default is a stateless `CBlockingIo`
(the mirror of `CAllocator`), installed in `__sx_default_context`.

Library (library/modules/std):
- core.sx: `Io` protocol (spawn_raw / suspend_raw / ready / poll / now_ms /
  arm_timer) + `SpawnOpts` / `PinTarget` / `ParkToken`; `Context` gains an
  `io: Io` field LAST (allocator stays index 0, data stays index 1).
- io.sx (new): `CBlockingIo` + `impl Io` (blocking M:1 semantics — now_ms is
  a real monotonic clock, the rest are no-ops/0; suspend never called);
  `Future($R)` { value; state: FutureState; err: IoErr; park; task; canceled:
  Atomic(bool) } with `Value :: R`; the async ergonomic layer
  `async` / `async_void` / `await` (value-carrying `(R, !IoErr)`) / `cancel`.
  Built with the verified `= ---` + field-assign + `Closure(..$args) -> $R` +
  `..$args` idiom (NON-void $R only — Future(void) is deferred per issue 0150).
- std.sx: re-export the Io surface + the io.sx tail.

Compiler (src/ir):
- protocol.zig `emitDefaultContextGlobal` + comptime_vm.zig
  `materializeDefaultContext`: both materializers of `__sx_default_context`
  now build the inline CBlockingIo->Io vtable (7 words) at the new field.
- stmt.zig `lowerPush`: `push Context.{...}` now INHERITS omitted fields from
  the ambient context (seed the slot from current_ctx_ref, overwrite only the
  literal's named fields) — correct capability-bag semantics, so the partial
  `push Context.{ allocator = X }` sites don't zero a null `io` vtable.
- protocols.zig + lower.zig + error_analysis.zig: record protocol-impl method
  names so the "declared `!` but never errors" lint skips a conforming impl
  whose `!` is dictated by the protocol contract (e.g. Io.suspend_raw).

37 `.ir` snapshots regenerated: layout-only (the Context type now carries the
Io field, shifting type-table numbering); no stdout/stderr/exit changes.

The blocking Io + now_ms + Future/async work when `async` is called with the
receiver passed explicitly; the user-facing UFCS form `context.io.async(...)`
is blocked on a separate UFCS generic-inference bug (filed next).

Suite: 726 ran, 0 failed.
2026-06-20 22:21:27 +03:00

122 lines
4.1 KiB
Plaintext

// The prelude facade: every name here is a RE-EXPORT. The implementations
// live in the part-files (std/core.sx — compiler-coupled primitives,
// std/fmt.sx — formatting + string helpers, std/list.sx — List); each
// alias below is an ordinary OWN declaration of this file, so a flat
// `#import "modules/std.sx"` sees the whole prelude bare, one hop —
// visibility never chains, aliases are the re-export mechanism. The
// part-file namespaces (`core` / `fmt` / `list`) and the namespace tail at
// the bottom are carried to flat importers the same one level.
core :: #import "modules/std/core.sx";
fmt :: #import "modules/std/fmt.sx";
list :: #import "modules/std/list.sx";
// --- Compiler-resolved types ---
// (`string` has no alias here: it is a reserved type name — its #builtin
// declaration in core.sx resolves program-wide and cannot be re-bound.)
Context :: core.Context;
Allocator :: core.Allocator;
Io :: core.Io;
SpawnOpts :: core.SpawnOpts;
PinTarget :: core.PinTarget;
ParkToken :: core.ParkToken;
Into :: core.Into;
Source_Location :: core.Source_Location;
// --- Type system & reflection builtins ---
Vector :: core.Vector;
size_of :: core.size_of;
align_of :: core.align_of;
type_of :: core.type_of;
type_name :: core.type_name;
type_is_unsigned :: core.type_is_unsigned;
field_count :: core.field_count;
field_name :: core.field_name;
field_value :: core.field_value;
field_value_int :: core.field_value_int;
field_index :: core.field_index;
is_flags :: core.is_flags;
error_tag_name :: core.error_tag_name;
// --- Output & libc escape hatch ---
out :: core.out;
libc_malloc :: core.libc_malloc;
libc_free :: core.libc_free;
memcpy :: core.memcpy;
memset :: core.memset;
// --- Formatting ---
print :: fmt.print;
format :: fmt.format;
any_to_string :: fmt.any_to_string;
int_to_string :: fmt.int_to_string;
uint_to_string :: fmt.uint_to_string;
int_to_hex_string :: fmt.int_to_hex_string;
float_to_string :: fmt.float_to_string;
bool_to_string :: fmt.bool_to_string;
struct_to_string :: fmt.struct_to_string;
enum_to_string :: fmt.enum_to_string;
flags_to_string :: fmt.flags_to_string;
vector_to_string :: fmt.vector_to_string;
array_to_string :: fmt.array_to_string;
slice_to_string :: fmt.slice_to_string;
pointer_to_string :: fmt.pointer_to_string;
optional_to_string :: fmt.optional_to_string;
// --- String ops & allocation helpers ---
concat :: fmt.concat;
substr :: fmt.substr;
path_join :: fmt.path_join;
alloc_string :: fmt.alloc_string;
cstring_len :: fmt.cstring_len;
from_cstring :: fmt.from_cstring;
to_cstring :: fmt.to_cstring;
alloc_slice :: fmt.alloc_slice;
// fmt internals, re-exported only because they were always part of the
// flat prelude surface.
build_format :: fmt.build_format;
hex_group :: fmt.hex_group;
decompose_u16x4 :: fmt.decompose_u16x4;
// --- Containers ---
List :: list.List;
// --- Async / Io capability (impls in std/io.sx) ---
io_mod :: #import "modules/std/io.sx";
CBlockingIo :: io_mod.CBlockingIo;
Future :: io_mod.Future;
FutureState :: io_mod.FutureState;
IoErr :: io_mod.IoErr;
async :: io_mod.async;
async_void :: io_mod.async_void;
await :: io_mod.await;
cancel :: io_mod.cancel;
// `timeout` / `Future(void)` are DEFERRED (B1.4) pending issue 0150
// (a `void` struct field SIGTRAPs the compiler). Re-export once it lands.
// --- The stdlib namespace tail: flat-importing std.sx carries these ---
mem :: #import "modules/std/mem.sx";
fs :: #import "modules/std/fs.sx";
process :: #import "modules/std/process.sx";
socket :: #import "modules/std/socket.sx";
json :: #import "modules/std/json.sx";
xml :: #import "modules/std/xml.sx";
cli :: #import "modules/std/cli.sx";
hash :: #import "modules/std/hash.sx";
log :: #import "modules/std/log.sx";
test :: #import "modules/std/test.sx";
time :: #import "modules/std/time.sx";
event :: #import "modules/std/event.sx";
http :: #import "modules/std/http.sx";
thread :: #import "modules/std/thread.sx";