Scoop API

scoop:scoop.core:0.1.0

src/ffi.scoop

Selected production source · src/ffi.scoop

1// Typed raw-pointer core contract (spec 13.10, M12 DESIGN section 4.1).
2// HIR validates these declarations exactly and normalizes applications to
3// dedicated Ptr/FunPtr types; downstream stages never identify them by name.
5/**
6 * A typed native data address. Unsafe users must ensure valid lifetime, alignment, bounds, and
7 * access permissions; pointer values do not own or root their pointees.
8 */
9@Intrinsic("core_ptr")
10public struct Ptr<T : value> : Equals<Ptr<T>> {
11 /**
12 * Compares the stored addresses without dereferencing either pointer.
13 */
14 public override operator fun equals(other: Ptr<T>): Boolean {
15 @Unsafe {
16 return this.toULong() == other.toULong()
17 }
18 }
21 /**
22 * Returns the address as an unsigned integer without extending the pointee's lifetime.
23 */
24 @NoGC
25 @Unsafe
26 @Intrinsic("ptr_to_ulong")
27 public fun toULong(): ULong
29 /**
30 * Reinterprets the same address with another pointee type. The caller must establish the new
31 * type's alignment and access validity.
32 */
33 @NoGC
34 @Unsafe
35 @Intrinsic("ptr_cast")
36 public fun <U : value> cast(): Ptr<U>
38 /**
39 * Reads a value at this address or the specified element offset. The caller must provide
40 * readable, properly aligned initialized storage.
41 */
42 @NoGC
43 @Unsafe
44 @Intrinsic("ptr_load")
45 public fun load(): T
47 /**
48 * Reads a value at this address or the specified element offset. The caller must provide
49 * readable, properly aligned initialized storage.
50 */
51 @NoGC
52 @Unsafe
53 @Intrinsic("ptr_load_offset")
54 public fun load(offset: Long): T
56 /**
57 * Writes a value at this address or the specified element offset. The caller must provide
58 * writable, properly aligned storage.
59 */
60 @NoGC
61 @Unsafe
62 @Intrinsic("ptr_store")
63 public fun store(value: T)
65 /**
66 * Writes a value at this address or the specified element offset. The caller must provide
67 * writable, properly aligned storage.
68 */
69 @NoGC
70 @Unsafe
71 @Intrinsic("ptr_store_offset")
72 public fun store(offset: Long, value: T)
74 /**
75 * Moves forward by `offset` elements of T, not bytes. It does not check allocation bounds.
76 */
77 @NoGC
78 @Unsafe
79 @Intrinsic("ptr_plus")
80 public operator fun plus(offset: Long): Ptr<T>
82 /**
83 * Moves backward by `offset` elements of T, not bytes. It does not check allocation bounds.
84 */
85 @NoGC
86 @Unsafe
87 @Intrinsic("ptr_minus")
88 public operator fun minus(offset: Long): Ptr<T>
91/**
92 * A native function address with a statically checked function signature; it does not own callback
93 * state.
94 */
95@Intrinsic("core_fun_ptr")
96public struct FunPtr<F>
98/**
99 * A native function address whose caller promises the GC-leaf contract. Its machine
100 * representation matches FunPtr, but the call mode is part of its static type.
101 */
102@Intrinsic("core_gc_leaf_fun_ptr")
103public struct GCLeafFunPtr<F>
105/**
106 * Returns the same native function address with the NativeSafe calling boundary.
107 */
108@NoGC
109@Intrinsic("gc_leaf_fun_ptr_as_fun_ptr")
110public fun <F> GCLeafFunPtr<F>.asFunPtr(): FunPtr<F>
112/**
113 * Treats the same native function address as a GC-leaf call target. The caller must ensure that
114 * every invocation satisfies the GC-leaf contract.
115 */
116@NoGC
117@Unsafe
118@Intrinsic("fun_ptr_as_gc_leaf")
119public fun <F> FunPtr<F>.asGCLeaf(): GCLeafFunPtr<F>
121/**
122 * Borrows the address of addressable value storage. The unsafe caller must not let the pointer
123 * outlive that storage.
124 */
125@Unsafe
126@Intrinsic("address_of")
127public fun <T : value> addressOf(value: T): Ptr<T>
129/**
130 * Returns the target ABI size in bytes of a concrete value type.
131 */
132@NoGC
133@Intrinsic("size_of")
134public fun <T : value> sizeOf(): ULong
136/**
137 * Returns the target ABI alignment in bytes of a concrete value type.
138 */
139@NoGC
140@Intrinsic("align_of")
141public fun <T : value> alignOf(): ULong