Repository navigation
@cdecl C callbacks (TSLANG 29); a generic sees no local of its caller (TSLANG 30) - #788
Merged
Merged
Conversation
… (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>
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.
TSLANG 29: C callbacks
A function-typed parameter of a
declare functionwas 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 nullarg. A callback in the last position (qsort's comparator) only worked by luck.@cdecl declare function ...now takes each function-typed parameter as a thints.func, which is a C function pointer (MLIRGenFunctions.cpp,mlirGenParameters).mlirGenAdjustOperandTypes). The check does not go in the general cast, because object-literal and interface methods legitimately castbound_functofuncthere; putting it in the cast broke 94 tests.@cdeclparameter, 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 = 5inmainhid the module'stotal()fromBag<T>.sum(), and a local namedvaluesbroke the default library'sArray<T>.GenericFunctionInfoandGenericClassInforecorddeclaredInFunction.lexicalFuncOpslist.resolveIdentifierAsVariable, a local of a function outside that list (other than module code) is not visible.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.check_declarationsmatches all 336 declarations, suite 193/193.Docs: README (tsbindgen section),
docs/c-cpp-header-import.md, TSLANG_ISSUES.md.Not fixed here:
<Opaque><any>0reads 8 bytes from a 4-byte box, so it is a garbage pointer under rc/none/own. The tests useOpaque | nulland passnullinstead.🤖 Generated with Claude Code