Repository navigation
A JS effect gets and answers Bend's own text and converts it itself; an export converts all of its text - #1825
Open
nicolas-abril wants to merge 10 commits into
Open
nicolas-abril wants to merge 10 commits into
nicolas-abril wants to merge 10 commits into
Conversation
This was referenced Oct 28, 2026
… as a C effect does The foreign-effect wrapper no longer runs str_out on top-level text arguments or str_in on text answers. An effect converts its text with the runtime's helpers: io_bytes or str_out on the way out (as io_cstr in C), io_text or str_in on the way in (as io_str). The base effects already did, so text no longer goes through str_out twice, and UDP.send_to's unsent text comes back exact. Module exports still convert their text, since no effect stands between them and the host. Nat and F32 answers keep their checks. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…CTS.md says so for Char too The base effects that answered host strings raw (IO.args, IO.get_env, Process.run, UDP.recv_from's host, Window.open's message) now answer through str_in, and those that hand Bend's host text to io_addr pass it through str_out. bend-lint's effects convert every String field both ways. marshal_string_scalar answers each host literal through str_in. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ost, an export's crossing with its text Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…for an effect answer, so comp.ts fits its 66k ttok cap Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ttok cap, to be made up elsewhere This reverts commit 6c3dc0a. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
force-pushed
the
ai/effect-text-explicit
branch
from
October 28, 2026 15:52
c6d3ab1 to
a23de92
Compare
…ffects drop conversions that cannot change their text
A Char that is no Unicode scalar is Bend text that holds zeros, so the JS
title check read Chr{55296} as a NUL; it now runs on str_out(title), as
window.c's io_cstr then io_nul. io_addr already rejects whatever str_out
would change, Process.run's output is decoded UTF-8, a peer's host is a
dotted quad, and Window.open's message is ASCII: none converts.
marshal_string_scalar's valid answers go raw again; only torn calls str_in.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t cannot change their text; EFFECTS.md says what Bend's text is host_in(A, x, text) is any value in from a host: a Nat through nat_in, an F32 rounded, and, for an export's caller (text), a String or Char through str_in; an effect answers Bend's own text, so its answer passes text=false. host_out is an export's answer, its text through str_out. The direction is the name, as with str_in and str_out, in place of host_in plus a lib_host taking an out flag. args.js and get_env.js drop str_in: Node and Bun decode argv and env as UTF-8, a bad byte as U+FFFD (checked with ED A0 80 and ED B0 80 on Bun 1.3 and Node 26), so neither holds a lone surrogate. bend-lint passes a source's path and text as they are, read as UTF-8, instead of scanning every source's whole text on each rule run. EFFECTS.md's JS section says that Bend's text is not a host string (a non-scalar Char is \uDC00 and three 11-bit parts, zeros included), so a check on it tests the converted text, as window_open.js does; that a Char answer is char_new(code), not io_text; and which host text needs str_in (what may hold a lone surrogate) and which does not. Tests: text_host_export pins an export's str_in and str_out (each, removed, fails it); marshal_string_scalar's torn_low pins str_in on a lone DC00 (counts 2 without it, 5 with); marshal_char_scalar's echo_list pins that nested text crosses an effect as it is. window_open_args' header is reflowed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ctly in C too; get_env and bend-lint keep str_in
An export now passes text as it is, both ways and at any depth, as an
effect does: only its caller knows which strings are host text and which
are Bend's, so it converts them itself. host_out goes, and host_in is
Nat and F32 only, for an effect's answer and an export's argument alike.
EFFECTS.md says so, with str_in's and str_out's one-line rules, and
GUIDE.md points there.
udp_send_to.c reads its datagram without taking it (udp_send_buf, as
io_cbuf but by term_peek) and keeps it in w->item: Fail and Wait hand
back exactly what was given, Chr{55296} as 55296, as the JS lane does,
and a sent one is dropped. udp_bad_address pins it (65533 on C before).
get_env.js runs str_in again: process.env may be set from JS, and Bun
keeps a lone surrogate set there. bend-lint runs str_in on a source's
path and text again: a source may be an editor's unsaved buffer.
EFFECTS.md no longer calls every literal safe, only one of valid text.
marshal_string_scalar's torn prints its codes, so a missing str_in shows
(97;55296; for 97;65533;).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
An export's arguments and answer are walked at runtime: every JS string in them is Bend text (only Char and String are strings), so the walk needs no type and covers a polymorphic def, nested data, arrays and closures in either direction. A value with no text to change stays itself, so an Array argument is still updated in place, and a shared value is walked once. The packing of a non-scalar Char no longer reaches a caller, which gets U+FFFD, as for a lone surrogate going in. Effects still get and answer Bend's own text. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A C effect gets a
Stringas a Bend term and turns it into bytes itself:io_cstrvalidates and writes U+FFFD for a Char that is no Unicode scalar,io_cbufcopies raw bytes, andio_strbuilds a text answer. The JS side now follows the same rule: an effect converts its own text, in both directions. Before this, the compiler's foreign-effect wrapper also ranstr_outon top-level text arguments andstr_inon text answers. That converted text twice on most paths (the wrapper, thenio_bytes, and a third time inget_env.js), and it left nested text, such asProcess.run'sList(String)args, to the effect anyway.Change
comp.ts: an effect's wrapper no longer converts text: an effect gets and answers Bend's own text, at any depth, and converts it itself. A module export converts all of its text, at any depth.host()becomeshost_in(A, x):nat_infor aNat,Math.froundfor anF32, applied to an effect's answer and to an export's arguments.run_libruns an export's arguments throughhost_text(·, str_in, str_out)and its answer throughhost_text(·, str_out, str_in). The walk goes by value, not type: in the JS runtime onlyCharandStringare strings, so every string it meets is Bend text. It therefore covers what a type-directed conversion can't: a polymorphic def (poly(-A, x: A) -> A), text in a ctor, list or array, and a function in either direction, which gets wrapped with the directions swapped for its arguments. Onmainonly a top-levelStringorCharwas converted.===), so GUIDE.md's "anArrayargument is the caller's own array, updated in place" still holds. A shared value is walked once (memoized by identity), so sharing can't make the walk exponential. Cost: a 1M-elementArray<U32>takes 25 ms on Bun and 32 ms on Node per crossing.args.jsneeds nostr_in, and says why: Node and Bun decode argv as UTF-8, a bad byte as U+FFFD, so it can't hold a lone surrogate (checked with CESU-8ED A0 80andED B0 80, on Bun 1.3.14 and Node 26.3.1).get_env.jsanswers throughstr_in, becauseprocess.envcan be set from JS, and Bun keeps a lone surrogate set there.window_open.jschecks for a NUL instr_out(title), aswindow.crunsio_cstrthenio_nul. Bend's text for a non-scalar Char holds zeros (Chr{55296}is"\udc00\^@\^[\^@"), so a check on the raw title would read a NUL that isn't there.io_addr, which already rejects whateverstr_outwould change;Process.run's output is decoded UTF-8; a UDP peer's host is a dotted quad;Window.open's message is ASCII.UDP.send_tohands its unsent text back inFail(and thetry_twin inWait) exactly, on both lanes. In C,udp_send_to.creads the datagram without consuming it (udp_send_buf,io_cbuf's walk done withterm_peek) and keeps the value inw->item, which hands it back on Fail or Wait and drops it after a send. Before, the C lane rebuilt the value from the UTF-8 copy, soChr{55296}came back as U+FFFD while JS gave 55296.tools/bend-lint/src/bend/effects.js): every String crossing the lint channel goes in withstr_inand out withstr_out, nested text included. That includes a source's text, which can be an editor's unsaved buffer.guide/EFFECTS.md: the JS section's text guidance, nested text included:io_bytes/str_out; text coming in goes throughstr_in/io_text.\uDC00and three 11-bit parts, which may be 0, so a check for a NUL, a length or a pattern tests the converted text, aswindow_open.jsdoes.Charanswer ischar_new(code), notio_text, which can give""or several characters.JSON.parse, text built from code units,process.env) needsstr_in, because a lone\uDC00would read as a packed Char with no error. Text that cannot hold one (a literal of valid text,io_text, argv) goes in as it is.Charcoming out reads as U+FFFD. A function that crosses converts its own arguments and answer.GUIDE.mdsays text crosses as a host string, and that a value with no text to change crosses without a copy.marshal_char_scalarhasecho(returns the Char, which reads back as 55296) andseen(returnsstr_in(str_out(c)), which reads back as 65533).marshal_string_scalar's valid answers stay raw host strings, which need nostr_in;torn(a lone surrogate) callsstr_initself.window_open_argsadds a title holdingChr{55296}:acceptedon every lane. Without thewindow_open.jsfix, the JS lane and the interpreter answered EILSEQ.marshal_string_scalaraddstorn_low(str_in("a\uDC00bcd")), which counts 5. It counts 2 withoutstr_in, the packed-Char misread.tornnow prints its codes,97;65533;, so a missingstr_inshows as97;55296;; its old count was 2 either way.udp_bad_addresssendsSCon{Chr{55296}, "x"}to a bad host and reads the kept text's first code: 55296 on every lane (65533 on C before). It then sends the same datagram successfully.marshal_char_scalaraddsecho_list: aList<String>holdingChr{55296}crosses an effect and back as it is, 55296.text_host_exportbuilds a.mjsand calls its exports:"\uDC00\0\^[\0") is just host text with a lone surrogate: it becomes"\uFFFD\0\^[\0", counts 5 Chars plus 1 for"x", and a lone"\uDC00"Char reads 65533.Chr{55296}reaches the caller as U+FFFD in a String, a Char, a list, an array, a tuple through a polymorphic def, a returned closure's answer, and a host callback's argument. The callback's lone-surrogate answer comes back cleaned.Arraywith no text comes back===and updated in place, and so does a ctor with no text to change.Breaking for third-party
.jseffects and for JS callers of exports. An effect that passed a top-levelStringstraight to a host API now callsstr_outorio_bytes, and one that answers a host string that may hold a lone surrogate callsstr_in. A caller of an export now gets nested text converted too (onmainonly a top-levelString/Charwas), and a function it gets or passes converts on each call. Valid text behaves the same either way; only non-scalar Chars and lone surrogates differ.This replaces most of #1549, which moves the extra
str_outcalls the other way (out of the effects, kept in the wrapper).Checks
comp.tsis 66,118 ttok, 118 over its 66,000 cap, sogates/repo.tspasses 53 / 54.GUIDE.mdis 7,999 of 8,000. The export walk costs about 200 ttok, and it needs a simplification elsewhere incomp.tsbefore merging.run_libis only used by exports, so other programs' output is unchanged. The suite-wide numbers below predate the walk.tools/bend-lint'sbun test: 134 / 134. The tests that build a module run their JS lane from the repo root; all 13 pass on both trees. The 3 C failures are this machine's:gfx/windowandgfx/clicksexpect "no display", andio/stack_fault_trapexits 138 under the runner.clicksandwindowexpect "no display",tcp_recv_max_hugediffers by a trailing space, andprocess_run_node_module's JS lane.🤖 Generated with Claude Code