@gtkx/native 0.20.0 → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":""}
package/index.ts DELETED
@@ -1,185 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import { arch, platform } from "node:os";
3
- import type { Arg, CallbackType, NativeHandle, Ref, Type } from "./types.js";
4
-
5
- const require = createRequire(import.meta.url);
6
-
7
- function loadNativeBinding() {
8
- const currentPlatform = platform();
9
- const currentArch = arch();
10
-
11
- if (currentPlatform !== "linux") {
12
- throw new Error(`Unsupported platform: ${currentPlatform}. Only Linux is supported.`);
13
- }
14
-
15
- if (currentArch !== "x64" && currentArch !== "arm64") {
16
- throw new Error(`Unsupported architecture: ${currentArch}. Only x64 and arm64 are supported.`);
17
- }
18
-
19
- const packageName = `@gtkx/native-linux-${currentArch}`;
20
-
21
- try {
22
- return require(packageName);
23
- } catch (error) {
24
- const originalError = error instanceof Error ? error.message : String(error);
25
- throw new Error(`Failed to load native binding for ${currentPlatform}-${currentArch}: ${originalError}`);
26
- }
27
- }
28
-
29
- const native = loadNativeBinding();
30
-
31
- /**
32
- * Creates a mutable reference wrapper.
33
- *
34
- * Used for out-parameters in FFI calls where the native function
35
- * needs to write a value back.
36
- *
37
- * @typeParam T - The type of the referenced value
38
- * @param value - Initial value
39
- * @returns A reference object containing the value
40
- *
41
- * @example
42
- * ```tsx
43
- * const errorRef = createRef<GError | null>(null);
44
- * const result = someFunction(errorRef);
45
- * if (errorRef.value) {
46
- * console.error(errorRef.value.message);
47
- * }
48
- * ```
49
- */
50
- export function createRef<T>(value: T): Ref<T> {
51
- return { value };
52
- }
53
-
54
- /**
55
- * Makes a low-level FFI call to a native library.
56
- *
57
- * This is the core FFI mechanism. Most code should use the generated
58
- * bindings in `@gtkx/ffi` instead of calling this directly.
59
- *
60
- * @param library - Shared library name (e.g., "libgtk-4.so.1")
61
- * @param symbol - Function symbol name
62
- * @param args - Function arguments with type information
63
- * @param returnType - Expected return type
64
- * @returns The function return value
65
- */
66
- export function call(library: string, symbol: string, args: Arg[], returnType: Type): unknown {
67
- return native.call(library, symbol, args, returnType);
68
- }
69
-
70
- /**
71
- * Starts the GTK runtime and creates an application.
72
- *
73
- * @param appId - Application ID in reverse domain notation
74
- * @param flags - Optional GIO application flags
75
- * @returns Native application pointer
76
- *
77
- * @internal Use `@gtkx/ffi` start() instead
78
- */
79
- export function start(appId: string, flags?: number): unknown {
80
- return native.start(appId, flags);
81
- }
82
-
83
- /**
84
- * Stops the GTK runtime.
85
- *
86
- * @internal Use `@gtkx/ffi` stop() instead
87
- */
88
- export function stop(): void {
89
- native.stop();
90
- }
91
-
92
- /**
93
- * Reads a value from native memory.
94
- *
95
- * @param handle - Native handle pointing to the memory
96
- * @param type - Type of value to read
97
- * @param offset - Byte offset from the handle pointer
98
- * @returns The read value
99
- */
100
- export function read(handle: unknown, type: Type, offset: number): unknown {
101
- return native.read(handle, type, offset);
102
- }
103
-
104
- /**
105
- * Writes a value to native memory.
106
- *
107
- * @param handle - Native handle pointing to the memory
108
- * @param type - Type of value to write
109
- * @param offset - Byte offset from the handle pointer
110
- * @param value - Value to write
111
- */
112
- export function write(handle: unknown, type: Type, offset: number, value: unknown): void {
113
- native.write(handle, type, offset, value);
114
- }
115
-
116
- /**
117
- * Allocates memory for a boxed type or plain struct.
118
- *
119
- * @param size - Size in bytes to allocate
120
- * @param glibTypeName - GLib type name for boxed types (optional for plain structs)
121
- * @param lib - Optional library containing the type
122
- * @returns Native pointer to allocated memory
123
- */
124
- export function alloc(size: number, glibTypeName?: string, lib?: string): unknown {
125
- return native.alloc(size, glibTypeName, lib);
126
- }
127
-
128
- /**
129
- * Gets the internal handle ID for a native pointer.
130
- *
131
- * Used for comparing object identity.
132
- *
133
- * @param handle - Native handle
134
- * @returns Internal handle ID
135
- */
136
- export function getNativeId(handle: unknown): number {
137
- return native.getNativeId(handle);
138
- }
139
-
140
- /**
141
- * Reads a value from memory pointed to by a pointer field.
142
- *
143
- * Used for accessing array elements or dereferencing pointer fields.
144
- * Reads the pointer at ptrOffset, then reads from that location plus elementOffset.
145
- *
146
- * @param handle - Native handle pointing to the parent struct
147
- * @param ptrOffset - Byte offset of the pointer field in the parent
148
- * @param elementOffset - Byte offset from the dereferenced pointer
149
- * @returns Native handle pointing to the element (borrowed, non-owning)
150
- */
151
- export function readPointer(handle: unknown, ptrOffset: number, elementOffset: number): unknown {
152
- return native.readPointer(handle, ptrOffset, elementOffset);
153
- }
154
-
155
- /**
156
- * Writes a struct value to memory pointed to by a pointer field.
157
- *
158
- * Used for setting array elements. Copies the data from source to the
159
- * destination array element.
160
- *
161
- * @param destHandle - Native handle pointing to the parent struct containing the pointer
162
- * @param ptrOffset - Byte offset of the pointer field in the parent
163
- * @param elementOffset - Byte offset from the dereferenced pointer (index * elementSize)
164
- * @param sourceHandle - Native handle of the struct to copy from
165
- * @param size - Size in bytes of the struct to copy
166
- */
167
- export function writePointer(
168
- destHandle: unknown,
169
- ptrOffset: number,
170
- elementOffset: number,
171
- sourceHandle: unknown,
172
- size: number,
173
- ): void {
174
- native.writePointer(destHandle, ptrOffset, elementOffset, sourceHandle, size);
175
- }
176
-
177
- export function freeze(): void {
178
- native.freeze();
179
- }
180
-
181
- export function unfreeze(): void {
182
- native.unfreeze();
183
- }
184
-
185
- export type { NativeHandle, Ref, Arg, Type, CallbackType };
package/types.ts DELETED
@@ -1,119 +0,0 @@
1
- /**
2
- * Opaque handle for a native value.
3
- *
4
- * Wraps GObject, Boxed, and Fundamental type instances.
5
- * This branded type ensures type safety for native object references.
6
- */
7
- export type NativeHandle = { readonly __brand: "NativeHandle" };
8
-
9
- type IntegerType = { type: "int"; size: 8 | 16 | 32 | 64; unsigned: boolean; library?: string; getTypeFn?: string };
10
-
11
- type FloatType = { type: "float"; size: 32 | 64 };
12
-
13
- type BooleanType = { type: "boolean" };
14
-
15
- type Ownership = "full" | "borrowed";
16
-
17
- type StringType = { type: "string"; ownership: Ownership; length?: number };
18
-
19
- type GObjectType = { type: "gobject"; ownership: Ownership };
20
-
21
- type BoxedType = { type: "boxed"; ownership: Ownership; innerType: string; library?: string; getTypeFn?: string };
22
-
23
- type StructType = { type: "struct"; ownership: Ownership; innerType: string; size?: number };
24
-
25
- type FundamentalType = {
26
- type: "fundamental";
27
- ownership: Ownership;
28
- library: string;
29
- refFn: string;
30
- unrefFn: string;
31
- };
32
-
33
- type ArrayType = {
34
- type: "array";
35
- itemType: Type;
36
- kind: "array" | "glist" | "gslist" | "gptrarray" | "garray" | "sized" | "fixed";
37
- ownership: Ownership;
38
- elementSize?: number;
39
- sizeParamIndex?: number;
40
- fixedSize?: number;
41
- };
42
-
43
- type HashTableType = {
44
- type: "hashtable";
45
- keyType: Type;
46
- valueType: Type;
47
- ownership: Ownership;
48
- };
49
-
50
- type RefType = { type: "ref"; innerType: Type };
51
-
52
- type NullType = { type: "null" };
53
-
54
- type UndefinedType = { type: "undefined" };
55
-
56
- export type CallbackType = {
57
- type: "callback";
58
- kind:
59
- | "animationTargetFunc"
60
- | "asyncReadyCallback"
61
- | "closure"
62
- | "destroyNotify"
63
- | "drawingAreaDrawFunc"
64
- | "pathIntersectionFunc"
65
- | "scaleFormatValueFunc"
66
- | "shapeRendererFunc"
67
- | "shortcutFunc"
68
- | "tickCallback"
69
- | "treeListModelCreateModelFunc";
70
- argTypes: Type[];
71
- returnType: Type;
72
- sourceType?: Type;
73
- resultType?: Type;
74
- };
75
-
76
- /**
77
- * Discriminated union of all FFI type descriptors.
78
- *
79
- * Describes how to marshal values between JavaScript and native code.
80
- */
81
- export type Type =
82
- | IntegerType
83
- | FloatType
84
- | BooleanType
85
- | StringType
86
- | GObjectType
87
- | BoxedType
88
- | StructType
89
- | FundamentalType
90
- | ArrayType
91
- | HashTableType
92
- | RefType
93
- | CallbackType
94
- | NullType
95
- | UndefinedType;
96
-
97
- /**
98
- * An argument for an FFI call.
99
- *
100
- * Combines a value with its type information for marshaling.
101
- */
102
- export type Arg = {
103
- /** Type descriptor for marshaling */
104
- type: Type;
105
- /** The argument value */
106
- value: unknown;
107
- /** Whether the argument can be null/undefined */
108
- optional?: boolean;
109
- };
110
-
111
- /**
112
- * A mutable reference wrapper for out-parameters.
113
- *
114
- * @typeParam T - The type of the referenced value
115
- */
116
- export type Ref<T> = {
117
- /** The current value */
118
- value: T;
119
- };