Skip to content

@cdecl C callbacks (TSLANG 29); a generic sees no local of its caller (TSLANG 30) - #788

Merged
ASDAlexander77 merged 1 commit into
mainfrom
generic-scope-c-callbacks
Nov 8, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
generic-scope-c-callbacks

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

TSLANG 29: C callbacks

A function-typed parameter of a declare function was lowered as a TypeScript function value, { ptr, ptr, ptr }. A C function taking a callback therefore got every argument after the callback shifted. For example, _beginthreadex(..., entry, arg, ...) handed the thread a null arg. A callback in the last position (qsort's comparator) only worked by luck.

  • @cdecl declare function ... now takes each function-typed parameter as a thin ts.func, which is a C function pointer (MLIRGenFunctions.cpp, mlirGenParameters).
  • You can pass a function, or an arrow that captures nothing.
  • Passing a capturing arrow or a bound method is a compile error, raised while the call's arguments are adjusted (mlirGenAdjustOperandTypes). The check does not go in the general cast, because object-literal and interface methods legitimately cast bound_func to func there; putting it in the cast broke 94 tests.
  • Known gap (noted in TSLANG_ISSUES): a closure already stored in a variable of function type, then passed to a @cdecl parameter, still loses its captures at run time. Catching that needs a run-time check.

TSLANG 30: generic name resolution

A generic body is compiled where it is first instantiated, so a name in it resolved to a local of the instantiating function. const total = 5 in main hid the module's total() from Bag<T>.sum(), and a local named values broke the default library's Array<T>.

  • GenericFunctionInfo and GenericClassInfo record declaredInFunction.
  • While a generic declared outside every function is instantiated, its generated bodies start a new lexicalFuncOps list.
  • In resolveIdentifierAsVariable, a local of a function outside that list (other than module code) is not visible.
  • The list includes every function body generated within the generic, because lambda instances redeclare the variables they capture.
  • A generic declared inside a function still reads that function's locals.

Tests

  • 00tslang_issue_29.ts (qsort), 00tslang_issue_29_thread.ts (Windows, _beginthreadex), 00tslang_issue_29_pthread.ts (Linux, pthread_create), all 4 memory models, JIT and compiled. The thread tests crash without @cdecl.
  • cdecl_closure_error.ts, the error test. It is excluded from the ownership verifier, like the other error tests.
  • 00tslang_issue_30.ts, all 4 memory models.
  • Windows: full ctest green (6750 tests).
  • Linux (WSL): the 29/30 tests pass in all 4 memory models.
  • DefaultLib gc, rebuilt with this compiler: check_declarations matches all 336 declarations, suite 193/193.

Docs: README (tsbindgen section), docs/c-cpp-header-import.md, TSLANG_ISSUES.md.

Not fixed here: <Opaque><any>0 reads 8 bytes from a 4-byte box, so it is a garbage pointer under rc/none/own. The tests use Opaque | null and pass null instead.

🤖 Generated with Claude Code

… (TSLANG 30)

29: a function-typed parameter of a declared function was a TypeScript function
value ({ ptr, ptr, ptr }), so a C function taking a callback got every argument
after it shifted. A function declared @cdecl takes each function-typed parameter
as a C function pointer; a closure or bound method passed to it is a compile error.

30: a generic body is compiled where it is first instantiated, and a name in it
resolved to a local of the instantiating function (`const total = 5` in main hid
the module's total() from Bag<T>.sum()). A generic declared outside every function
now sees only its own locals and those of functions generated within it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 94b8bdb into main Nov 8, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the generic-scope-c-callbacks branch November 8, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant