Skip to content

Thread.spawn passes its entry point as a C function pointer (@cdecl) - #71

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cdecl-thread-entry
Nov 8, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cdecl-thread-entry

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Drops the Opaque workaround in Thread.spawn. That workaround was needed because a function-typed parameter of a declared function reached C as three pointers (TSLANG 29). The compiler's new @cdecl decorator, added in ASDAlexander77/TypeScriptCompiler#788, makes such a parameter a single C function pointer.

  • lib.native.d.ts: @cdecl @nodrops declare function tslang_thread_spawn(entry: (arg: Opaque) => u32, arg: Opaque): Opaque;
  • lib.generics.ts: tslang_thread_spawn(__thread_entry<R>, <Opaque>this.start), with no cast on the function.
  • thread.cpp: the parameter is typed ThreadEntry instead of void * plus a reinterpret_cast.

The generated IR is unchanged in substance: call ptr @tslang_thread_spawn(ptr @"___thread_entry<si32>", ptr %17).

Merge order: merge this after ASDAlexander77/TypeScriptCompiler#788. A compiler without @cdecl ignores the decorator and passes the three-pointer struct, so Thread.spawn would crash.

Tests

These were run on Windows with the #788 compiler, release build.

  • gc: check_declarations.ps1 matches all 336 declarations; thread_spawn, thread_move, thread_gc_collect and atomics_wait pass under both JIT and compile.
  • rc: thread_spawn, thread_move and atomics_wait pass.
  • own: thread_move passes. thread_spawn and atomics_wait are // not own by design, unchanged from Thread.spawn / join and Atomics.wait / notify (workers tier 1) #70.

🤖 Generated with Claude Code

tslang_thread_spawn took the entry point as an Opaque and the caller cast the
function to one, because a function-typed parameter of a declared function
reached C as three pointers. The compiler's @cdecl (TypeScriptCompiler #788,
TSLANG 29) makes it one: the declaration says `entry: (arg: Opaque) => u32`
and the C side takes a ThreadEntry.

Needs a compiler with @cdecl: an older one ignores the decorator and passes
the struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit a333739 into main Nov 8, 2026
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