Repository navigation
Description
What I did
The guide says that each effect may import a .c and .js implementation and that users can add effects the same way as Base. I used that path for a native program whose custom effects own resources and park on file descriptors.
The native effect path works on Bend 2.0.7 after I reused Base handle types, but I had to derive the C interface from comp.ts, the Base effect sources, and the IO tests. The guide specifies the generated host-function name and stops there.
Missing native documentation
A native custom effect must answer questions that the guide does not cover:
- Which C function signature and registration call should an imported effect use?
- Which result constructors may it return, including
io_failwith custom text? - Which
IoWorkfields may it own acrossio_workorio_wait_on, and who releases their memory after success or failure? - Can a package rely on
io_eff,io_hand,io_hand_v,io_work, andio_wait_on, or are those helpers private compiler details? - Does Bend support a nominal opaque handle type for a custom resource?
- What compatibility, if any, does Bend promise for imported C across releases?
The handle question has no documented answer. This declaration follows the guide's affine-handle description and compiles:
import Base
type Handle is Type:
def Handle.open() -> IO(Result<&1, &1, U32 & String, Handle>):
import "./handle.c"
def Handle.close(handle: Handle) -> IO(Unit):
import "./handle.c"
def main() -> IO(Unit):
do IO<Unit>:
handle : Handle <- IO.try(Handle, Handle.open())
Handle.close(handle)handle.c:
static Term handle_open_run(Env e, Term* f, IoWork* w) {
return io_done(e, io_hand(42));
}
static Term handle_close_run(Env e, Term* f, IoWork* w) {
fprintf(stderr, "handle=%llu\n", (unsigned long long)io_hand_v(f[0]));
return term_pak(CID_UNIT, 0);
}
static void __attribute__((constructor)) handle_effects_use(void) {
io_eff(CID_HANDLE_OPEN, handle_open_run, 0);
io_eff(CID_HANDLE_CLOSE, handle_close_run, 0);
}$ bend main.bend -o main && ./main
handle=0Handle.close receives a zero word rather than 42. An unfilled law Handle: Type fails with 1 TODO found. The working fallback I found defines Handle as File, Socket, or Listener, which loses nominal separation between unrelated resources.
This probe does not assume that an empty datatype should carry a foreign word. It shows that the guide does not name a supported custom-handle mechanism or state that none exists.
Requested documentation
Please document the native C side of custom effects, or state that packages must treat it as unsupported compiler internals. A small checked-in example or a guide link to an existing test would be enough. It should cover:
- one synchronous effect and its registration;
- the supported value and error representations;
- the supported pattern, if any, for an opaque affine resource handle;
- one worker or file-descriptor park/resume path and its memory ownership;
- the compatibility policy for imported C across Bend releases.
This request does not require a stable C ABI. Documentation may state that a custom effect must track its exact Bend compiler version.
References
- The current guide claim:
Lines 371 to 375 in b4929c2
Every effect in Base is a def whose body is `import "./x.js"` plus a `.c` twin, implemented by a host function named after the def, lowercased, dots to underscores. You can add your own effects the same way. Only the event loop runs them, so proofs, termination and the GPU never touch host code. In the other direction, a JS file may `import Game from "./game.bend"` (with `bend2/main.ts` - Existing foreign-value test: https://github.com/bendlang/bend/blob/b4929c2d0d9d1e9598954aa37758954bab4d8a77/tests/io/foreign_types.c
- Existing worker-effect test: https://github.com/bendlang/bend/blob/b4929c2d0d9d1e9598954aa37758954bab4d8a77/tests/io/help_flood.c
- A built-in parked effect: https://github.com/bendlang/bend/blob/b4929c2d0d9d1e9598954aa37758954bab4d8a77/bend2/effs/tcp_recv.c
Environment
bend 2.0.7 (b4929c2d0d9d1e9598954aa37758954bab4d8a77)
Linux x86_64
Bun 1.3.13
Ubuntu clang 18.1.3
Activity
WONTFIX: user handle types are a design entry, the native library tar…
The guide gains "Effects in Bend", an AI-written note on the C and JS…
VictorTaelin commentedon Sep 19, 2026
2.0.13 adds
bend guide effects(guide/EFFECTS.md): the effect def, the C side (signature,io_effand the three needs,io_done/io_fail/io_hand,io_workandio_wait_on, who ownsw,fandw->data), the JS side, a complete example on both lanes, and the compatibility policy (the C side tracks the exact compiler version; no ABI promise). It is AI-written from the runtime's source and a human will revise it; report anything wrong. Your second finding, that a user cannot declare a handle type (a userlaw Handle: Typecounts as open; only Base's handle laws are exempt), is now a WONTFIX DESIGN entry: opening it touches consistency, and we will address the gap later.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
luccahuguet commentedon Sep 19, 2026
I checked the new C-effects guide against a resource-owning native effect and found one cleanup caveat to document.
Our effect opened a Unix listener and PTY child. In a disposable Bend 2.0.7 build on x86_64 Linux, I forced
err_failafter both resources existed. Bend exited 1, left the socket pathname, and left a child that ignored SIGHUP running. At the same point, returningio_fail(EIO)let ouratexitcleanup reap the child and remove the socket (exit 5).err_failstill calls_exit(1)incomp.tsat 01e13b72. Couldguide/EFFECTS.mdstate that this fatal path bypassesatexitcleanup? Authors of resource-owning effects need that boundary when they choose their cleanup strategy. I am not asking to change fail-stop behavior or reopen this issue.WONTFIX: user handle types are a design entry, the native library tar…
The guide gains "Effects in Bend", an AI-written note on the C and JS…