Scoop API

scoop:scoop.core:0.1.0

src/foreign_callback.scoop

Selected production source · src/foreign_callback.scoop

1// GC-aware managed callback registration (spec 14.3, M13).
3/**
4 * Controls whether a managed callback registration accepts repeated native invocation.
5 */
6public enum ForeignCallbackMode {
7 /**
8 * Allows repeated invocations until the registration is released or fails.
9 */
10 Reusable,
11 /**
12 * Allows one terminal invocation of the managed callback.
13 */
14 OneShot
17/**
18 * The observable lifecycle of a registered managed callback.
19 */
20public enum ForeignCallbackState {
21 /**
22 * The registration exists and has not begun invocation.
23 */
24 Registered,
25 /**
26 * A native invocation is executing the managed callback.
27 */
28 Active,
29 /**
30 * The one-shot callback completed successfully.
31 */
32 Completed,
33 /**
34 * A callback invocation failed; its exception can be queried.
35 */
36 Failed
39/**
40 * Pairs a callable native entry with the context pointer that must be passed at its declared
41 * context argument position.
42 */
43public struct ForeignCallback<F>(
44 val function: FunPtr<F>,
45 val context: Ptr<Unit>
48/**
49 * Registers a managed non-suspending function behind a native trampoline. F must be an explicit
50 * concrete C-safe signature; contextIndex and mode are compile-time constants, and the context
51 * parameter is Ptr<Unit>. Reusable owners release after native unregister. A successful one-shot
52 * launch transfers its worker ownership to the callback; retain a separate observer before that
53 * transfer when needed.
54 */
55@Unsafe
56@Intrinsic("foreign_callback_register")
57public fun <F> foreignCallback(
58 callback: Any,
59 contextIndex: Long,
60 mode: ForeignCallbackMode
61): ForeignCallback<F>
63/**
64 * Acquires another reference to the callback registration for independent native ownership.
65 */
66@Unsafe
67@Intrinsic("foreign_callback_retain")
68public fun <F> retainForeignCallback(
69 callback: ForeignCallback<F>
70): ForeignCallback<F>
72/**
73 * Releases one acquired registration reference. Coordinate with native owners so no invocation can
74 * use a freed registration.
75 */
76@Unsafe
77@Intrinsic("foreign_callback_release")
78public fun <F> releaseForeignCallback(callback: ForeignCallback<F>)
80/**
81 * Reads the lifecycle state of a live callback registration.
82 */
83@Unsafe
84@Intrinsic("foreign_callback_state")
85public fun <F> foreignCallbackState(
86 callback: ForeignCallback<F>
87): ForeignCallbackState
89/**
90 * Returns the captured callback exception when invocation failed, or None when no failure is
91 * recorded.
92 */
93@Unsafe
94@Intrinsic("foreign_callback_failure")
95public fun <F> foreignCallbackFailure(callback: ForeignCallback<F>): Throwable?