From 2cf3590d204acc44024ea76a1107ac0c183cd2a6 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 8 Nov 2026 20:53:34 +0000 Subject: [PATCH] Thread.spawn passes its entry point as a C function pointer (@cdecl) 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 --- src/generics/lib.generics.ts | 2 +- src/native/lib.native.d.ts | 9 ++++----- src/wrappers/thread.cpp | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index a46f453..2c91523 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -873,7 +873,7 @@ class ThreadHandle { // thread does too, so their counts change atomically from here on (erased under the // other models) Shared.share(this.start); - this.handle = tslang_thread_spawn(__thread_entry, this.start); + this.handle = tslang_thread_spawn(__thread_entry, this.start); } join(): R { diff --git a/src/native/lib.native.d.ts b/src/native/lib.native.d.ts index 8ec1a2d..75c1858 100644 --- a/src/native/lib.native.d.ts +++ b/src/native/lib.native.d.ts @@ -126,11 +126,10 @@ declare function GC_gcollect(): void; // thread @nodrops declare function std_this_thread_sleep_for(milliseconds: u32): void; -// Threads (wrappers/thread.cpp). `entry` is a function `(arg: Opaque) => u32` cast to an Opaque: a -// function-typed parameter of a declared function reaches C as a three-pointer struct, not a code -// pointer. The new thread calls back into TypeScript, but only into the ThreadStart `arg` names, -// which the Thread keeps until join: nothing the caller lends is dropped. -@nodrops declare function tslang_thread_spawn(entry: Opaque, arg: Opaque): Opaque; +// Threads (wrappers/thread.cpp). @cdecl: `entry` reaches C as a function pointer. The new thread calls +// back into TypeScript, but only into the ThreadStart `arg` names, which the Thread keeps until join: +// nothing the caller lends is dropped. +@cdecl @nodrops declare function tslang_thread_spawn(entry: (arg: Opaque) => u32, arg: Opaque): Opaque; @nodrops declare function tslang_thread_join(handle: Opaque): void; @nodrops declare function tslang_thread_hardware_concurrency(): u32; // Atomics.wait / notify: 0 "ok", 1 "not-equal", 2 "timed-out"; notify answers how many it woke diff --git a/src/wrappers/thread.cpp b/src/wrappers/thread.cpp index 8198709..fc89abc 100644 --- a/src/wrappers/thread.cpp +++ b/src/wrappers/thread.cpp @@ -69,9 +69,8 @@ struct CollectorRegistration typedef uint32_t (*ThreadEntry)(void *); -extern "C" TSLANG_EXPORT void *tslang_thread_spawn(void *entry, void *arg) +extern "C" TSLANG_EXPORT void *tslang_thread_spawn(ThreadEntry start, void *arg) { - auto start = reinterpret_cast(entry); return new std::thread([start, arg]() { #ifdef TSLANG_MM_GC CollectorRegistration registration;