node-gtk 0.14.0 → 2.0.0

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/src/value.h CHANGED
@@ -11,23 +11,45 @@ using v8::Local;
11
11
 
12
12
  namespace GNodeJS {
13
13
 
14
+ enum ResourceOwnership {
15
+ kNone,
16
+ kCopy,
17
+ kTransfer
18
+ };
19
+
14
20
  Local<Value> GListToV8 (GITypeInfo *info, GList *glist);
15
21
  Local<Value> GSListToV8 (GITypeInfo *info, GSList *glist);
16
22
  Local<Value> GHashToV8 (GITypeInfo *info, GHashTable *hash);
17
23
  Local<Value> ArrayToV8 (GITypeInfo *info, gpointer data, long length = -1);
18
- Local<Value> GErrorToV8 (GITypeInfo *type_info, GError *err);
19
- Local<Value> GIArgumentToV8 (GITypeInfo *type_info, GIArgument *argument, long length = -1, bool mustCopy = false);
24
+ Local<Value> GErrorToV8 (GITypeInfo *type_info, GError *err, ResourceOwnership ownership = kNone);
25
+ Local<Value> GIArgumentToV8 (GITypeInfo *type_info, GIArgument *argument, long length = -1, ResourceOwnership ownership = kNone);
20
26
  long GIArgumentToLength(GITypeInfo *type_info, GIArgument *arg, bool is_pointer);
21
27
 
22
- bool V8ToGIArgument (GITypeInfo *type_info, GIArgument *argument, Local<Value> value);
28
+ bool V8ToGIArgumentInterface (GIBaseInfo *gi_info, GIArgument *argument, Local<Value> value);
23
29
  bool V8ToGIArgument (GITypeInfo *type_info, GIArgument *argument, Local<Value> value, bool may_be_null);
24
30
  bool V8ToOutGIArgument(GITypeInfo *type_info, GIArgument *arg, Local<Value> value, bool may_be_null);
25
31
  void FreeGIArgument (GITypeInfo *type_info, GIArgument *argument, GITransfer transfer = GI_TRANSFER_EVERYTHING, GIDirection direction = GI_DIRECTION_OUT);
26
32
  void FreeGIArgumentArray (GITypeInfo *type_info, GIArgument *arg, GITransfer transfer = GI_TRANSFER_EVERYTHING, GIDirection direction = GI_DIRECTION_OUT, long length = -1);
33
+
34
+ // For a transfer-container IN GList/GSList/GHashTable, the callee frees the
35
+ // container structure itself, so node-gtk cannot walk it afterwards to free
36
+ // the (caller-owned) elements. These capture the element pointers *before*
37
+ // the call so they can be freed *after* it. See #399.
38
+ bool IsTransferContainerInList (GITypeInfo *type_info, GITransfer transfer, GIDirection direction);
39
+ gpointer CaptureTransferContainerElements (GITypeInfo *type_info, gpointer container);
40
+ void FreeTransferContainerElements (GITypeInfo *type_info, gpointer captured);
41
+
42
+ // For a transfer-full IN boxed argument (or a C array of boxed pointers) the
43
+ // callee frees the passed memory, but the JS wrapper still owns its own copy,
44
+ // which would then be double-freed when the wrapper is finalized. Replace each
45
+ // such pointer with a g_boxed_copy so only the copy is handed to the callee.
46
+ // See #409.
47
+ void CopyBoxedForTransferFullIn (GITypeInfo *type_info, GIArgument *arg, long length);
48
+
27
49
  bool CanConvertV8ToGIArgument (GITypeInfo *type_info, Local<Value> value, bool may_be_null);
28
50
 
29
- bool V8ToGValue(GValue *gvalue, Local<Value> value, bool mustCopy = false);
30
- Local<Value> GValueToV8(const GValue *gvalue, bool mustCopy = false);
51
+ bool V8ToGValue(GValue *gvalue, Local<Value> value, ResourceOwnership ownership = kNone);
52
+ Local<Value> GValueToV8(const GValue *gvalue, ResourceOwnership ownership = kNone);
31
53
  bool CanConvertV8ToGValue(GValue *gvalue, Local<Value> value);
32
54
 
33
55
  bool ValueHasInternalField (Local<Value> value);
@@ -0,0 +1,91 @@
1
+ # node-gtk TypeScript types — PROTOTYPE (Model B: generate-on-demand)
2
+
3
+ Types are generated **on the user's machine** from the GObject-Introspection
4
+ typelibs they actually have installed, using node-gtk's own runtime introspection
5
+ (`require('node-gtk')._GIRepository`, the libgirepository C API exposed to JS).
6
+ Because the generator reads the same typelibs and applies the same name/shape
7
+ rules as `lib/bootstrap.js`, the output matches what node-gtk produces at runtime
8
+ — and it matches *their* library versions, not a bundled snapshot.
9
+
10
+ ## User workflow
11
+
12
+ ```sh
13
+ # 1. generate types for the namespaces you use (+ their dependency closure).
14
+ # Output defaults to ./node_modules/.node-gtk-types (hidden, gitignored).
15
+ npx node-gtk generate-types Gtk-4.0
16
+
17
+ # 2. point tsconfig at the generated shim
18
+ ```
19
+ ```jsonc
20
+ // tsconfig.json
21
+ {
22
+ "compilerOptions": {
23
+ "skipLibCheck": true,
24
+ "paths": { "node-gtk": ["./node_modules/.node-gtk-types/node-gtk.d.ts"] }
25
+ }
26
+ }
27
+ ```
28
+ ```ts
29
+ // 3. write code — gi.require() is typed by string-literal overloads
30
+ import * as gi from 'node-gtk'
31
+ const Gtk = gi.require('Gtk', '4.0') // inferred as the Gtk-4.0 namespace
32
+ const win = new Gtk.ApplicationWindow({ title: 'Hi', defaultWidth: 400 })
33
+ win.on('close-request', () => false)
34
+ ```
35
+
36
+ `generate-types` emits one `<Namespace>-<version>.d.ts` per namespace plus a
37
+ `node-gtk.d.ts` module shim. The shim overloads `require()` so each
38
+ `gi.require('Ns','ver')` resolves to the matching generated namespace; namespaces
39
+ you didn't generate fall back to `any`. Because the default output lives under
40
+ `node_modules`, it's treated as a generated cache — wire it into a `postinstall`
41
+ script so it regenerates after install.
42
+
43
+ ## Pieces (prototype)
44
+
45
+ - `bin/node-gtk.js` — CLI entry (`package.json` `"bin"`); dispatches `generate-types`.
46
+ - `tools/generate-types.js` — the generator. `run(argv)` / `generate(roots, outdir)`.
47
+ - `examples/ts-demo/` — `app.ts` (valid, typechecks clean) and `app-errors.ts`
48
+ (4 deliberate mistakes, all caught). Generate types into `.node-gtk-types/` first
49
+ (see that dir's `.gitignore`).
50
+
51
+ ## Verify the demo
52
+
53
+ ```sh
54
+ node bin/node-gtk.js generate-types Gtk-4.0 --outdir examples/ts-demo/.node-gtk-types
55
+ node_modules/.bin/tsc -p examples/ts-demo/tsconfig.json # passes clean
56
+ sed 's/app.ts/app-errors.ts/' examples/ts-demo/tsconfig.json > examples/ts-demo/tsconfig.errors.json
57
+ node_modules/.bin/tsc -p examples/ts-demo/tsconfig.errors.json # 4 errors caught
58
+ ```
59
+
60
+ ## Fidelity
61
+
62
+ The generated `.d.ts` for the full Gtk-3.0, Gtk-4.0, and Adw/GtkSource stacks
63
+ type-check with **0 errors even without `skipLibCheck`**. Modelled faithfully:
64
+
65
+ - OUT/INOUT params surfaced via the return value as node-gtk does
66
+ (`getStartIter(): TextIter`, `getIterAtLine(n): [boolean, TextIter]`).
67
+ - Callback argument types expanded (e.g. `Gio.AsyncReadyCallback`).
68
+ - 64-bit ints return `bigint` (full precision, #323/#149); params accept
69
+ `number | bigint`.
70
+ - Enum methods and interface constants emitted (declaration-merged).
71
+ - GObject override conflicts reconciled as overloads, so subclass methods stay
72
+ assignable to inherited ones; multiple-interface signal/method conflicts
73
+ resolved with a unified, assignable-to-all declaration.
74
+ - Interfaces emit both a type and a value, so constructor functions and
75
+ constants work (`Gio.File.newForPath(...)`).
76
+ - Relative imports use `.js` extensions, so the output works under
77
+ `moduleResolution` node16/nodenext (and bundler). `skipLibCheck` is no longer
78
+ required for the GTK stack, though it remains a fine default.
79
+ - **JSDoc comments** from the `.gir` XML — class/method/property/signal/enum
80
+ docs, with `@param`/`@returns`/`@deprecated` — so editors show GNOME's API docs
81
+ on hover. The typelib doesn't carry docs, so this reads the matching
82
+ `<Namespace>-<version>.gir` from `$XDG_DATA_DIRS/gir-1.0` (shipped by the
83
+ library's `-dev`/`-devel` package). Best-effort: if the `.gir` is absent, types
84
+ still generate without comments. Pass `--no-docs` for leaner output (~5× smaller).
85
+
86
+ ## Remaining limitations
87
+
88
+ - **Overriding an inherited method that collides by name** in a user subclass
89
+ (e.g. a gutter renderer's `activate(iter, …)` vs `GtkWidget.activate()`)
90
+ requires the override to satisfy both signatures — an inherent consequence of
91
+ the GObject API reusing a name, not specific to these types.