Skip to content

JS lane: a Nat inside a value passed between two loaded .bend libraries stays a BigInt (constructor tags depend on the importing root), since 2.0.29 (still in 2.0.30) #1105

Closed

Description

@moorbrook

Bend 2.0.29 (c8691aa) and 2.0.30 (56330b2), bun loader (bun --preload bend2/main.ts), macOS arm64, Bun 1.3.14.

a.bend
import Base
type Counter is Data:
Counter{count: Nat}
def Counter.make() -> Counter:
Counter{5n}

b.bend
import Base
import ./a.bend as A
def dec(c: A.Counter) -> Nat:
A.Counter{count} = c
Nat.sub(count, 1n)

t.ts
import A from './a.bend';
import B from './b.bend';
const c = A'Counter.make'; // c.$ === "Counter", c.count === 5n
B'dec'; // 2.0.29: TypeError: Invalid mix of BigInt and other type in subtraction
B['dec']({$: 'Counter', count: 5n}); // same TypeError
B['dec']({$: 'a.Counter', count: 5n}); // 4n

Results:

The cross-library case has no host-side fix: two libraries compiled from the same source disagree on one constructor's tag, so a value one library returns is not recognised by the other. Either the tag should not depend on which file is the root of the book (a file's namespace being its real path, as #1042 says, for the root too), or the in-marshal should convert by type rather than by tag for a single-constructor type, and an unrecognised tag should fail at the boundary instead of passing through.

Seen in a real program: a record built by one module (a jitter buffer inside a bridge) returned to TS and passed into a function of another module throws in Jitter.emit_ready on count - 1; another returns false from a validity check because a BigInt count compares unequal to a number.

Activity

  1. added
    later
    on Sep 27, 2026
  2. moorbrook commented on Oct 4, 2026

    @moorbrook
    ContributorAuthor

    Update on current main (565d7fd): the repro above no longer returns a BigInt silently; the host-boundary dispatch now refuses it, as the TODO(#1105) in comp.ts describes:

    tag "Counter" count bigint 5
    from A        -> bend: a.Counter has no tag Counter (its tags: a.Counter); a tag names its constructor as the loading file sees it, which a later version will make the same everywhere (#1105)
    tag Counter   -> (same error)
    tag a.Counter -> 4
    

    So a value one library returns still cannot be passed to another library that imports the same type.

    In case it helps with that later version: we have been running an approach downstream that tags a constructor with its name in its own file, as the loader's header promises ({$: "Name", field: value}), so the tag no longer depends on which file is the root of the book.

    • bend.ts records each constructor's namespace, as a Def already does (we know bend.ts is human-maintained, so this is only a suggestion):

      -export type Ctr  = { k: Name; n: number; T: HTerm }
      +export type Ctr  = { k: Name; n: number; T: HTerm; m?: string }
      ...
      -        const ctr = { k: c, n: fs.length, T: term_higher(tele_bind(params.concat(fs), tip)) };
      +        const ctr = { k: c, n: fs.length, T: term_higher(tele_bind(params.concat(fs), tip)), m: p.ns };
    • comp.ts spells every JS-lane tag through one function: where the lane builds a constructor, matches one, converts one at the host boundary, and resolves CID(Name) in a .js effect source. main's printed value keeps the full key, as the C lane does. Dispatch is per type and a file's constructor names are unique, so dropping the namespace from the tag cannot make two arms of one match collide.

    Evidence from when we introduced it (on 2.0.31; the same approach is carried on 2.0.34): the repro returns 4n both for the value from the other library and for a host value with the short tag; and the 1,343 tests under tests/ that define main, built with -o t.js and run under bun, print identical output with and without the change. Our comp.ts side is a 46-line change against 2.0.34; happy to rebase it onto main and share it if that is useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    later

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions