@octane-xplat/build 0.0.0-nightly.g1b95ef7
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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/add-worker.js +66 -0
- package/dist/build-worker.js +287 -0
- package/dist/compatibility-check.js +155 -0
- package/dist/compatibility-data/compatibility.js +109 -0
- package/dist/compatibility-data/compatibility.json +298 -0
- package/dist/compatibility.js +240 -0
- package/dist/css-platform.js +236 -0
- package/dist/css-platform.types.d.ts +11 -0
- package/dist/dev-inspector.web.js +125 -0
- package/dist/dev-log-client.web.js +79 -0
- package/dist/dev-log-server.js +13 -0
- package/dist/devtools-worker.js +94 -0
- package/dist/doctor-checks.js +414 -0
- package/dist/doctor-tool.js +27 -0
- package/dist/env.js +3 -0
- package/dist/fmt-plan.js +149 -0
- package/dist/fonts-worker.js +31 -0
- package/dist/fonts.js +614 -0
- package/dist/inline-stylesheets.js +44 -0
- package/dist/linux/config.js +43 -0
- package/dist/linux/gtk-runtime.js +86 -0
- package/dist/linux/package.js +189 -0
- package/dist/linux-gtk/bindings.mjs +675 -0
- package/dist/linux-gtk/native-gi-projection.js +36 -0
- package/dist/linux-gtk/vite.d.ts +39 -0
- package/dist/linux-gtk/vite.js +281 -0
- package/dist/macos/config.js +136 -0
- package/dist/macos/core-ui.js +59 -0
- package/dist/macos/core-vite.js +122 -0
- package/dist/macos/doctor-checks.js +58 -0
- package/dist/macos/executables.js +39 -0
- package/dist/macos/jsc-host/runtime.js +134 -0
- package/dist/macos/native.js +286 -0
- package/dist/macos/runtime-package.js +30 -0
- package/dist/macos/tab-item.js +62 -0
- package/dist/macos/vite.d.ts +13 -0
- package/dist/macos/vite.js +22 -0
- package/dist/markdown.js +11 -0
- package/dist/mason-engine.js +20 -0
- package/dist/mcp/contracts/actions.js +133 -0
- package/dist/mcp/dev-bridge.js +363 -0
- package/dist/mcp/dev-socket.js +203 -0
- package/dist/mcp/dev-stack.js +134 -0
- package/dist/mcp/inspection/dom.web.js +46 -0
- package/dist/native-regex.js +123 -0
- package/dist/package-manager.js +106 -0
- package/dist/package-root.js +48 -0
- package/dist/patches.js +356 -0
- package/dist/route-config.js +53 -0
- package/dist/route-data.js +42 -0
- package/dist/route-generator.js +514 -0
- package/dist/route-path.js +29 -0
- package/dist/route-source.js +67 -0
- package/dist/targets.js +37 -0
- package/dist/tsconfig-envs.js +400 -0
- package/dist/tsconfig-pin.js +31 -0
- package/dist/tsrx-leaf.js +57 -0
- package/dist/typecheck-globals/macos.d.ts +21 -0
- package/dist/typecheck-globals/native.d.ts +42 -0
- package/dist/ui/src/markdown/markdown-ast.js +33 -0
- package/dist/ui/src/markdown/markdown-character-references.js +79 -0
- package/dist/ui/src/markdown/markdown-parse.js +1930 -0
- package/dist/ui/src/markdown/markdown-plugins.js +564 -0
- package/dist/ui/src/markdown/markdown-url.js +17 -0
- package/dist/updates-worker.js +34 -0
- package/dist/upgrade-worker.js +63 -0
- package/dist/vite-target.js +57 -0
- package/dist/vite.d.ts +125 -0
- package/dist/vite.js +797 -0
- package/dist/workshop-worker.js +181 -0
- package/package.json +74 -0
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
// Experimental GTK object projection over NativeScript's generated C API.
|
|
2
|
+
// It intentionally fails on unsupported calls; it is not a general GJS replacement.
|
|
3
|
+
// v2 targets runtimes >= be71019d8: C-returned GObjects arrive as runtime-owned
|
|
4
|
+
// class objects that hold their own reference — the runtime owns GObject
|
|
5
|
+
// lifetimes; this layer only projects and observes.
|
|
6
|
+
import schema from 'virtual:gtk-schema'
|
|
7
|
+
const data = schema.namespaces
|
|
8
|
+
const t = interop.types
|
|
9
|
+
const C = globalThis
|
|
10
|
+
// `GObject *` as an interop type: callbacks/reads produce the runtime wrapper.
|
|
11
|
+
// v2 hard-requires runtimes >= be71019d8, which expose this type.
|
|
12
|
+
const OBJ = globalThis.__nsGObject?.object
|
|
13
|
+
if(!OBJ){throw Error('bindings v2 requires __nsGObject.object (runtime >= be71019d8)')}
|
|
14
|
+
const pointer = value => value?.__native ?? value
|
|
15
|
+
const key = value => { const p = pointer(value); return p?.toBigInt ? p.toBigInt().toString() : p?.toString() }
|
|
16
|
+
const constructors = new Map(), byTypeName = new Map(), byGType = new Map()
|
|
17
|
+
const nativeMethods = new WeakSet(), classProperties = new Map()
|
|
18
|
+
const GI_SCOPE_NOTIFIED = 3
|
|
19
|
+
const proxies = new WeakMap()
|
|
20
|
+
// ptrKey -> GType name: every GObject proxy we create, until its weak notify.
|
|
21
|
+
export const pendingFinalizers = new Map()
|
|
22
|
+
const liveSignals = new Set()
|
|
23
|
+
const registry = new FinalizationRegistry((entry) => {
|
|
24
|
+
if (entry === 'proxy') { bridgeStats.objects--; return }
|
|
25
|
+
if (entry?.kind === 'glib-bytes') {
|
|
26
|
+
C.g_bytes_unref(entry.native)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
if (entry?.kind === 'gsk-transform') {
|
|
30
|
+
C.gsk_transform_unref(entry.native)
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
if (entry?.kind === 'gtk-accessible-list') {
|
|
34
|
+
C.g_boxed_free(C.gtk_accessible_list_get_type(),entry.native)
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
if (entry.live) { entry.live = false; bridgeStats.signals--; liveSignals.delete(entry) }
|
|
38
|
+
})
|
|
39
|
+
const scopes = new Set(), permanent = { callbacks: new Set(), timers: new Set() }
|
|
40
|
+
let activeScope = null
|
|
41
|
+
export const bridgeStats = { objects: 0, finalized: 0, signals: 0, callbacks: 0, sources: 0 }
|
|
42
|
+
const fatal = error => { console.error('GTK_BINDING_ERROR', error.stack ?? String(error)); throw error }
|
|
43
|
+
const decode = p => typeof p==='string'?p:p?interop.stringFromCString(p):null
|
|
44
|
+
const snake = name => name.replace(/([a-z0-9])([A-Z])/g,'$1_$2').toLowerCase()
|
|
45
|
+
const fundamental = type => Number(g_type_fundamental(type))
|
|
46
|
+
const nativeType = p => new interop.Reference(t.ulong, new interop.Reference(t.pointer,p).value).value
|
|
47
|
+
const rawCtor = (ns,name,record=data[ns]?.[name]) => {
|
|
48
|
+
const names = [record?.typeName, '_'+record?.typeName, '_'+ns+name, ns+name, snake(ns)+'_'+snake(name)+'_t', name]
|
|
49
|
+
if(ns==='Gdk'&&name==='Rectangle'){names.unshift('cairo_rectangle_int_t')}
|
|
50
|
+
for(const candidate of names){if(typeof C[candidate]==='function'){return C[candidate]}}
|
|
51
|
+
throw Error(`No C struct metadata for ${ns}.${name}: ${names.join(',')}`)
|
|
52
|
+
}
|
|
53
|
+
const scalar = type => {
|
|
54
|
+
if(type.pointer){return t.pointer}
|
|
55
|
+
if(type.tag===16){if([5,6].includes(type.interface.kind)){return t.int32;}return t.pointer}
|
|
56
|
+
return ({0:t.void,1:t.int32,2:t.int8,3:t.uint8,4:t.int16,5:t.uint16,6:t.int32,7:t.uint32,8:t.int64,9:t.uint64,10:t.float,11:t.double,12:t.ulong,13:t.pointer,14:t.pointer,15:t.pointer,21:t.int32})[type.tag]??t.pointer
|
|
57
|
+
}
|
|
58
|
+
const typeForRef = type => type.pointer||type.tag===16||type.tag===15?t.pointer:scalar(type)
|
|
59
|
+
const isObject = ref => ref && [7,8].includes(ref.kind)
|
|
60
|
+
const ptrSlot = interop.alloc(8)
|
|
61
|
+
// Bare pointer or runtime wrapper -> the runtime's JSObject wrapper. The
|
|
62
|
+
// wrapper cache makes this an identity: one GObject, one JS object.
|
|
63
|
+
const toWrapper = (p) => {
|
|
64
|
+
if(!p){return null}
|
|
65
|
+
new interop.Reference(t.pointer, ptrSlot).value = p.__native ?? p
|
|
66
|
+
return new interop.Reference(OBJ, ptrSlot).value
|
|
67
|
+
}
|
|
68
|
+
function fromNative(value,type,transfer=0){
|
|
69
|
+
if(value==null){return value}
|
|
70
|
+
if(type.tag===1){return !!value}
|
|
71
|
+
if(type.tag===20){
|
|
72
|
+
// GError callback arguments are borrowed; copy their fields before the
|
|
73
|
+
// callback returns so the JS value cannot retain a dangling C pointer.
|
|
74
|
+
try{
|
|
75
|
+
const error=new interop.Reference(_GError,value).value
|
|
76
|
+
const domain=Number(error.domain),code=Number(error.code),message=decode(error.message)
|
|
77
|
+
return {domain,code,message,matches:(otherDomain,otherCode)=>domain===Number(otherDomain)&&code===Number(otherCode)}
|
|
78
|
+
}finally{if(transfer===2){g_error_free(value)}}
|
|
79
|
+
}
|
|
80
|
+
if(type.tag===13||type.tag===14){if(typeof value==='string'){return value;}const str=decode(value);if(transfer===2){g_free(value);}return str}
|
|
81
|
+
if(type.tag===15&&type.zero){const result=[];for(let i=0;;i++){const p=new interop.Reference(t.pointer,value.add(i*8)).value;if(!p){break;}result.push(fromNative(p,type.element))}return result}
|
|
82
|
+
if(type.tag===16){
|
|
83
|
+
const ref=type.interface
|
|
84
|
+
if([5,6].includes(ref.kind)){return Number(value)}
|
|
85
|
+
if(ref.ns==='GObject'&&ref.name.startsWith('ParamSpec')){return value}
|
|
86
|
+
if(isObject(ref)){return wrapObject(value,ref,transfer)}
|
|
87
|
+
if([3,4].includes(ref.kind)){
|
|
88
|
+
if(ref.ns==='GLib'&&ref.name==='Bytes'){
|
|
89
|
+
if(typeof C.g_bytes_unref!=='function'||(transfer!==2&&typeof C.g_bytes_ref!=='function')){throw Error('GLib.Bytes reference functions are unavailable')}
|
|
90
|
+
return wrapStruct(transfer===2?value:C.g_bytes_ref(value),ref,true,true)
|
|
91
|
+
}
|
|
92
|
+
if(ref.ns==='Gsk'&&ref.name==='Transform'){return wrapStruct(transfer===2?value:C.gsk_transform_ref(value),ref,true,true)}
|
|
93
|
+
if(ref.ns==='Gtk'&&ref.name==='AccessibleList'){return wrapStruct(value,ref,transfer===2,true)}
|
|
94
|
+
return wrapStruct(value,ref)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return value
|
|
98
|
+
}
|
|
99
|
+
function projectGdkPopupInterface(target,type){
|
|
100
|
+
const popup=getClass('Gdk','Popup')
|
|
101
|
+
if(!popup.__gtype||!g_type_is_a(type,popup.__gtype)){return}
|
|
102
|
+
// GtkNative.get_surface() is introspected as Gdk.Surface, while popup
|
|
103
|
+
// surfaces implement the separate Gdk.Popup interface. GJS resolves the
|
|
104
|
+
// instance's dynamic interface methods; mirror that for the probe wrapper.
|
|
105
|
+
for(const name of Object.getOwnPropertyNames(popup.prototype)){
|
|
106
|
+
if(name==='constructor'||Reflect.has(target,name)){continue}
|
|
107
|
+
Object.defineProperty(target,name,Object.getOwnPropertyDescriptor(popup.prototype,name))
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const weakFinalized = (data, _dead) => {
|
|
111
|
+
pendingFinalizers.delete(key(data))
|
|
112
|
+
bridgeStats.finalized++
|
|
113
|
+
}
|
|
114
|
+
// GWeakNotify observes a dying instance. Its second pointer must stay raw:
|
|
115
|
+
// creating a GObject wrapper here would add a toggle ref during disposal.
|
|
116
|
+
const weakNotification = interop.callback(weakFinalized, {returns:t.void,args:[t.pointer,t.pointer]})
|
|
117
|
+
// The runtime wrapper always holds its own reference (#25), so for a
|
|
118
|
+
// transfer-full GObject return/out the caller's ref must be dropped after
|
|
119
|
+
// wrapping — except initially-unowned types, where the wrapper merely sank
|
|
120
|
+
// the floating ref and there is no extra ref to release.
|
|
121
|
+
const dropTransfer = (w, type) => {
|
|
122
|
+
if(!g_type_is_a(type,g_initially_unowned_get_type())){g_object_unref(w)}
|
|
123
|
+
}
|
|
124
|
+
function objectPropertySpec(w,name){
|
|
125
|
+
const klass=new interop.Reference(t.pointer,w).value,id=key(klass)
|
|
126
|
+
let properties=classProperties.get(id)
|
|
127
|
+
if(!properties){properties=new Map();classProperties.set(id,properties)}
|
|
128
|
+
if(!properties.has(name)){properties.set(name,g_object_class_find_property(klass,name)??null)}
|
|
129
|
+
return properties.get(name)
|
|
130
|
+
}
|
|
131
|
+
function wrapObject(p,ref,transfer=0,prototype){
|
|
132
|
+
if(!p){return null}
|
|
133
|
+
p = p.__native ?? p
|
|
134
|
+
const type=nativeType(p)
|
|
135
|
+
if(!g_type_is_a(type,g_object_get_type())){
|
|
136
|
+
// Non-GObject fundamentals (GdkEvent): adopt native ownership of the ref.
|
|
137
|
+
if(!g_type_is_a(type,gdk_event_get_type())){throw Error(`Unsupported fundamental ${ref.ns}.${ref.name}`)}
|
|
138
|
+
const owned=transfer===2?p:gdk_event_ref(p)
|
|
139
|
+
const name = decode(g_type_name(type))
|
|
140
|
+
const projected = byTypeName.get(name)
|
|
141
|
+
const eventClass = projected?.__lazy
|
|
142
|
+
? getClass(...projected.__lazy)
|
|
143
|
+
: projected ?? getClass(ref.ns, ref.name)
|
|
144
|
+
const target = Object.create(eventClass.prototype)
|
|
145
|
+
Object.assign(target, {
|
|
146
|
+
__native: interop.adopt(owned, gdk_event_unref),
|
|
147
|
+
__ref: eventClass.__ref ?? ref,
|
|
148
|
+
__gobject: false,
|
|
149
|
+
})
|
|
150
|
+
bridgeStats.objects++;registry.register(target,'proxy',target);return target
|
|
151
|
+
}
|
|
152
|
+
const w=toWrapper(p)
|
|
153
|
+
// Transfer-full returns/outs reach us at caller-ref + wrapper-ref (#25);
|
|
154
|
+
// drop the caller's ref once regardless of whether the proxy is fresh.
|
|
155
|
+
if(transfer===2){dropTransfer(w,type)}
|
|
156
|
+
const old=proxies.get(w)
|
|
157
|
+
if(old){return old}
|
|
158
|
+
const name=decode(g_type_name(type))
|
|
159
|
+
let selected=byGType.get(Number(type))??byTypeName.get(name)??getClass(ref.ns,ref.name)
|
|
160
|
+
if(selected.__lazy){selected=getClass(...selected.__lazy)}
|
|
161
|
+
const resolvedRef=selected.__ref??ref
|
|
162
|
+
const target=Object.create(prototype??selected.prototype)
|
|
163
|
+
Object.assign(target,{__native:w,__ref:resolvedRef,__gobject:true,__conn:new Map()})
|
|
164
|
+
projectGdkPopupInterface(target,type)
|
|
165
|
+
const proxy=new Proxy(target,{
|
|
166
|
+
get(obj,property,receiver){
|
|
167
|
+
const present=Reflect.has(obj,property)
|
|
168
|
+
const existing=present?Reflect.get(obj,property,receiver):undefined
|
|
169
|
+
if(typeof property!=='string'||(present&&!nativeMethods.has(existing))){return existing}
|
|
170
|
+
// GJS resolves an introspected property before a same-named native method
|
|
171
|
+
// (for example WebKit.WebView.is_loading).
|
|
172
|
+
const propertyName=property.replaceAll('_','-')
|
|
173
|
+
const spec=objectPropertySpec(w,propertyName)
|
|
174
|
+
if(spec){return propertyGet(proxy,propertyName,spec)}
|
|
175
|
+
return existing
|
|
176
|
+
},
|
|
177
|
+
set(obj,property,value,receiver){
|
|
178
|
+
const existing=Reflect.has(obj,property)?Reflect.get(obj,property,receiver):undefined
|
|
179
|
+
if(typeof property==='string'&&!property.startsWith('__')&&(!Reflect.has(obj,property)||nativeMethods.has(existing))){
|
|
180
|
+
const spec=objectPropertySpec(w,property.replaceAll('_','-'))
|
|
181
|
+
if(spec){propertySet(proxy,property.replaceAll('_','-'),value,spec);return true}
|
|
182
|
+
}
|
|
183
|
+
return Reflect.set(obj,property,value,receiver)
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
proxies.set(w,proxy)
|
|
187
|
+
// We own no reference; a weak ref just lets teardown name whatever survives.
|
|
188
|
+
pendingFinalizers.set(key(w),name)
|
|
189
|
+
g_object_weak_ref(w,weakNotification,w)
|
|
190
|
+
activeScope?.pending.set(key(w),name)
|
|
191
|
+
bridgeStats.objects++;registry.register(proxy,'proxy',proxy);return proxy
|
|
192
|
+
}
|
|
193
|
+
function wrapStruct(native,ref,owned=false,force=false){
|
|
194
|
+
if(!force&&native?.__native){return native}
|
|
195
|
+
const klass=getClass(ref.ns,ref.name),target=Object.create(klass.prototype)
|
|
196
|
+
target.__native=native;target.__ref=ref
|
|
197
|
+
const proxy=new Proxy(target,{
|
|
198
|
+
get(obj,p,receiver){
|
|
199
|
+
if(Reflect.has(obj,p)){return Reflect.get(obj,p,receiver)}
|
|
200
|
+
// GJS's GBytes.toArray convenience — copies the buffer, then drops the
|
|
201
|
+
// transfer-full reference the facade handed over.
|
|
202
|
+
if(ref.ns==='GLib'&&ref.name==='Bytes'&&p==='toArray'){return()=>{
|
|
203
|
+
const out=obj.get_data().slice()
|
|
204
|
+
if(!obj.__bytesFreed){obj.__bytesFreed=true;obj.unref()}
|
|
205
|
+
return out
|
|
206
|
+
}}
|
|
207
|
+
return obj.__native[p]
|
|
208
|
+
},
|
|
209
|
+
set(obj,p,value,receiver){if(typeof p==='string'&&!p.startsWith('__')&&p in obj.__native){obj.__native[p]=pointer(value);return true}return Reflect.set(obj,p,value,receiver)}
|
|
210
|
+
})
|
|
211
|
+
if(owned&&ref.ns==='GLib'&&ref.name==='Bytes'){
|
|
212
|
+
const token={}
|
|
213
|
+
const unref=proxy.unref
|
|
214
|
+
target.unref=function(...args){registry.unregister(token);target.__bytesFreed=true;return unref.apply(proxy,args)}
|
|
215
|
+
registry.register(proxy,{kind:'glib-bytes',native:pointer(native)},token)
|
|
216
|
+
}
|
|
217
|
+
if(owned&&ref.ns==='Gsk'&&ref.name==='Transform'){
|
|
218
|
+
const token={}
|
|
219
|
+
target.ref=()=>{
|
|
220
|
+
if(target.__transformFreed){throw Error('Gsk.Transform already released')}
|
|
221
|
+
return wrapStruct(C.gsk_transform_ref(pointer(native)),ref,true,true)
|
|
222
|
+
}
|
|
223
|
+
target.unref=()=>{
|
|
224
|
+
if(target.__transformFreed){throw Error('Gsk.Transform already released')}
|
|
225
|
+
target.__transformFreed=true;registry.unregister(token);C.gsk_transform_unref(pointer(native))
|
|
226
|
+
}
|
|
227
|
+
registry.register(proxy,{kind:'gsk-transform',native:pointer(native)},token)
|
|
228
|
+
}
|
|
229
|
+
if(owned&&ref.ns==='Gtk'&&ref.name==='AccessibleList'){
|
|
230
|
+
registry.register(proxy,{kind:'gtk-accessible-list',native:pointer(native)})
|
|
231
|
+
}
|
|
232
|
+
return proxy
|
|
233
|
+
}
|
|
234
|
+
export const typeNameOf = value => decode(g_type_name(nativeType(pointer(value))))
|
|
235
|
+
function valueType(spec){return new interop.Reference(_GParamSpec,spec).value.value_type}
|
|
236
|
+
function fillValue(memory,value,type){
|
|
237
|
+
if(value?.__ref?.ns==='GObject'&&value.__ref.name==='Value'){
|
|
238
|
+
// Copy the initialized GValue, preserving ref-counted contents.
|
|
239
|
+
g_value_init(memory,pointer(value).g_type);g_value_copy(pointer(value),memory);return
|
|
240
|
+
}
|
|
241
|
+
if(value?.__ref?.ns==='Gtk'&&value.__ref.name==='AccessibleList'){
|
|
242
|
+
g_value_init(memory,C.gtk_accessible_list_get_type())
|
|
243
|
+
// The GValue owns a copy; the returned boxed wrapper keeps its own copy.
|
|
244
|
+
g_value_set_boxed(memory,pointer(value));return
|
|
245
|
+
}
|
|
246
|
+
type??=typeof value==='string'?64:typeof value==='boolean'?20:typeof value==='number'?24:80
|
|
247
|
+
g_value_init(memory,type)
|
|
248
|
+
const kind=fundamental(type)
|
|
249
|
+
const setter=({20:g_value_set_boolean,24:g_value_set_int,28:g_value_set_uint,32:g_value_set_long,36:g_value_set_ulong,40:g_value_set_int64,44:g_value_set_uint64,48:g_value_set_enum,52:g_value_set_flags,56:g_value_set_float,60:g_value_set_double,64:g_value_set_string,68:g_value_set_pointer,72:g_value_set_boxed,80:g_value_set_object})[kind]
|
|
250
|
+
if(!setter){throw Error(`Unsupported GValue fundamental ${kind}`)}
|
|
251
|
+
setter(memory,pointer(value))
|
|
252
|
+
}
|
|
253
|
+
function readValue(memory,type){
|
|
254
|
+
const kind=fundamental(type)
|
|
255
|
+
const getter=({20:g_value_get_boolean,24:g_value_get_int,28:g_value_get_uint,32:g_value_get_long,36:g_value_get_ulong,40:g_value_get_int64,44:g_value_get_uint64,48:g_value_get_enum,52:g_value_get_flags,56:g_value_get_float,60:g_value_get_double,64:g_value_get_string,68:g_value_get_pointer,72:g_value_get_boxed,80:g_value_get_object})[kind]
|
|
256
|
+
if(!getter){throw Error(`Unsupported GValue read ${kind}`)}
|
|
257
|
+
if(kind===64&&Number(memory.g_type)!==Number(type)){throw Error(`GValue mismatch expected ${type}, actual ${memory.g_type}`)}
|
|
258
|
+
const value=getter(memory)
|
|
259
|
+
if(kind===64){return decode(value)}
|
|
260
|
+
if(kind===20){return !!value}
|
|
261
|
+
if(kind===80){return value?wrapObject(value,{ns:'GObject',name:'Object',kind:7}):null}
|
|
262
|
+
return value
|
|
263
|
+
}
|
|
264
|
+
function propertyGet(object,name,spec){const memory=new _GValue();const type=valueType(spec);g_value_init(memory,type);try{g_object_get_property(pointer(object),name,memory);return readValue(memory,type)}finally{g_value_unset(memory)}}
|
|
265
|
+
function propertySet(object,name,value,spec){const memory=new _GValue();fillValue(memory,value,valueType(spec));try{g_object_set_property(pointer(object),name,memory)}finally{g_value_unset(memory)}}
|
|
266
|
+
function makeObject(klass,props={}){
|
|
267
|
+
const type=klass.__gtype,propertyNames=Object.keys(props),values=interop.alloc(propertyNames.length*24),strings=[]
|
|
268
|
+
try{
|
|
269
|
+
const nativeClass=g_type_class_ref(type)
|
|
270
|
+
for(let i=0;i<propertyNames.length;i++){
|
|
271
|
+
const name=propertyNames[i].replaceAll('_','-'),spec=g_object_class_find_property(nativeClass,name)
|
|
272
|
+
if(!spec){throw Error(`Unknown constructor property ${klass.__ref.name}.${name}`)}
|
|
273
|
+
strings.push(interop.cstring(name));fillValue(values.add(i*24),props[propertyNames[i]],valueType(spec))
|
|
274
|
+
}
|
|
275
|
+
const names=new BigUint64Array(strings.map(p=>p.toBigInt()))
|
|
276
|
+
const p=g_object_new_with_properties(type,propertyNames.length,names,values)
|
|
277
|
+
g_type_class_unref(nativeClass)
|
|
278
|
+
// GtkWindow is initially-unowned and presented windows are GTK-held;
|
|
279
|
+
// transfer=0 asks wrapObject to leave the wrapper's sunk ref alone.
|
|
280
|
+
return wrapObject(p,klass.__ref,g_type_is_a(type,gtk_window_get_type())?0:2,klass.prototype)
|
|
281
|
+
}finally{for(let i=0;i<strings.length;i++){g_value_unset(values.add(i*24));}interop.free(values)}
|
|
282
|
+
}
|
|
283
|
+
function allMethods(ns,name,result=new Map()){
|
|
284
|
+
const info=data[ns][name]
|
|
285
|
+
if(info.parent){allMethods(info.parent.ns,info.parent.name,result)}
|
|
286
|
+
for(const iface of info.interfaces??[]){allMethods(iface.ns,iface.name,result)}
|
|
287
|
+
for(const [key,value]of Object.entries(info.methods??{})){result.set(key,value)}
|
|
288
|
+
return result
|
|
289
|
+
}
|
|
290
|
+
function installGType(klass,gtype){
|
|
291
|
+
Object.defineProperties(klass,{
|
|
292
|
+
__gtype:{value:gtype},
|
|
293
|
+
$gtype:{value:{name:decode(g_type_name(gtype))}},
|
|
294
|
+
})
|
|
295
|
+
byGType.set(Number(gtype),klass)
|
|
296
|
+
}
|
|
297
|
+
function getClass(ns,name){
|
|
298
|
+
const id=ns+'.'+name;if(constructors.has(id)){return constructors.get(id)}
|
|
299
|
+
const record=data[ns]?.[name];if(!record){throw Error(`Unexported GI type ${id}`)}
|
|
300
|
+
if(record.values){return record.values}
|
|
301
|
+
const klass=function(...args){
|
|
302
|
+
if([7,8].includes(record.kind)){return makeObject(new.target,args[0])}
|
|
303
|
+
if((ns==='GLib'&&name==='Bytes')||(ns==='Gsk'&&name==='Transform')){
|
|
304
|
+
if(typeof klass.new!=='function'){throw Error(`${ns}.${name}.new is not available in the native projection`)}
|
|
305
|
+
return klass.new(...args)
|
|
306
|
+
}
|
|
307
|
+
const native=new (rawCtor(ns,name))()
|
|
308
|
+
if(args[0]&&typeof args[0]==='object'){Object.assign(native,args[0])}
|
|
309
|
+
return wrapStruct(native,{ns,name,kind:record.kind})
|
|
310
|
+
}
|
|
311
|
+
constructors.set(id,klass);klass.__ref={ns,name,kind:record.kind}
|
|
312
|
+
if(record.typeName){byTypeName.set(record.typeName,klass)}
|
|
313
|
+
if(record.typeInit&&typeof C[record.typeInit]==='function'){installGType(klass,C[record.typeInit]())}
|
|
314
|
+
// GObject interfaces are implemented through GType, outside JS's prototype chain.
|
|
315
|
+
if(record.kind===8&&klass.__gtype){Object.defineProperty(klass,Symbol.hasInstance,{value:value=>
|
|
316
|
+
value?.__gobject===true&&!!g_type_is_a(nativeType(pointer(value)),klass.__gtype)})}
|
|
317
|
+
if(record.parent){Object.setPrototypeOf(klass.prototype,getClass(record.parent.ns,record.parent.name).prototype)}
|
|
318
|
+
for(const [methodName,method]of allMethods(ns,name)){
|
|
319
|
+
// Projected matrices own their aligned allocation through interop.adopt.
|
|
320
|
+
if(method.symbol==='graphene_matrix_alloc'||method.symbol==='graphene_matrix_free'){continue}
|
|
321
|
+
if(typeof C[method.symbol]!=='function'){continue}
|
|
322
|
+
if(method.flags&1){
|
|
323
|
+
const projected=function(...args){return invoke(method,args,this)}
|
|
324
|
+
nativeMethods.add(projected)
|
|
325
|
+
Object.defineProperty(klass.prototype,methodName,{value:projected,writable:true})
|
|
326
|
+
}else {Object.defineProperty(klass,methodName,{value:(...args)=>invoke(method,args),configurable:true})}
|
|
327
|
+
}
|
|
328
|
+
// Signal/property sugar on top of the runtime's own wrapper methods; the
|
|
329
|
+
// runtime owns the closure — it is freed on disconnect or instance finalize.
|
|
330
|
+
if([7,8].includes(record.kind)){
|
|
331
|
+
Object.assign(klass.prototype,{
|
|
332
|
+
connect(signal,fn){return connect(this,signal,fn)},connect_after(signal,fn){return connect(this,signal,fn,true)},
|
|
333
|
+
disconnect(id){const tok=this.__conn.get(id);this.__native.disconnect(id);if(tok?.live){tok.live=false;bridgeStats.signals--;liveSignals.delete(tok);this.__conn.delete(id)}},
|
|
334
|
+
emit(signal,...args){return this.__native.emit(signal,...args.map(pointer))},
|
|
335
|
+
get_property(name){const spec=g_object_class_find_property(new interop.Reference(t.pointer,this.__native).value,name);return propertyGet(this,name,spec)},
|
|
336
|
+
set_property(name,value){const spec=g_object_class_find_property(new interop.Reference(t.pointer,this.__native).value,name);propertySet(this,name,value,spec)},
|
|
337
|
+
})
|
|
338
|
+
}
|
|
339
|
+
if(record.kind===7&&klass.__gtype&&!Object.hasOwn(klass,'find_property')){
|
|
340
|
+
Object.defineProperty(klass,'find_property',{value:name=>{
|
|
341
|
+
const nativeClass=g_type_class_ref(klass.__gtype)
|
|
342
|
+
try{
|
|
343
|
+
const spec=g_object_class_find_property(nativeClass,name)
|
|
344
|
+
return spec?interop.adopt(g_param_spec_ref(spec),g_param_spec_unref):null
|
|
345
|
+
}finally{g_type_class_unref(nativeClass)}
|
|
346
|
+
}})
|
|
347
|
+
}
|
|
348
|
+
return klass
|
|
349
|
+
}
|
|
350
|
+
// Out-structs that rawCtor's name candidates miss (the runtime exposes
|
|
351
|
+
// graphene structs only by tag, e.g. `_graphene_rect_t`) get raw memory plus
|
|
352
|
+
// an explicit float field map — `fields` carry absolute byte offsets.
|
|
353
|
+
const outStructLayouts={
|
|
354
|
+
'Graphene.Rect':{size:16,fields:{origin:{x:0,y:4},size:{width:8,height:12}}},
|
|
355
|
+
'Graphene.Point':{size:8,fields:{x:0,y:4}},
|
|
356
|
+
'Graphene.Size':{size:8,fields:{width:0,height:4}},
|
|
357
|
+
}
|
|
358
|
+
function outStructFallback(ref){
|
|
359
|
+
const layout=outStructLayouts[ref.ns+'.'+ref.name]
|
|
360
|
+
if(!layout){throw Error(`No out-struct fallback for ${ref.ns}.${ref.name}`)}
|
|
361
|
+
const build=(buffer,fields)=>{
|
|
362
|
+
const out={}
|
|
363
|
+
for(const [name,value]of Object.entries(fields)){
|
|
364
|
+
if(typeof value==='number'){out[name]=new interop.Reference(t.float,buffer.add(value)).value}
|
|
365
|
+
else {out[name]=build(buffer,value)}
|
|
366
|
+
}
|
|
367
|
+
return out
|
|
368
|
+
}
|
|
369
|
+
return {alloc:()=>interop.alloc(layout.size),read:(buffer)=>build(buffer,layout.fields)}
|
|
370
|
+
}
|
|
371
|
+
function prepareArray(value,type,cleanup,hints){
|
|
372
|
+
if(typeof value==='string'){value=new TextEncoder().encode(value)}
|
|
373
|
+
if(ArrayBuffer.isView(value)){return value}
|
|
374
|
+
if(type.element?.interface?.ns==='GObject'&&type.element.interface.name==='Value'){
|
|
375
|
+
const memory=interop.alloc(value.length*24)
|
|
376
|
+
value.forEach((v,i)=>fillValue(memory.add(i*24),v,hints?.[i]))
|
|
377
|
+
cleanup.push(()=>{value.forEach((_,i)=>g_value_unset(memory.add(i*24)));interop.free(memory)})
|
|
378
|
+
return memory
|
|
379
|
+
}
|
|
380
|
+
if([6,7].includes(type.element.tag)||[5,6].includes(type.element.interface?.kind)){return new Int32Array(value)}
|
|
381
|
+
if(type.element.tag===3){return new Uint8Array(value)}
|
|
382
|
+
// guint64/gint64 element arrays (XplatMason child handles) carry plain numbers.
|
|
383
|
+
if(type.element.tag===8){return new BigInt64Array(value.map(v=>BigInt(v)))}
|
|
384
|
+
if(type.element.tag===9){return new BigUint64Array(value.map(v=>BigInt(v)))}
|
|
385
|
+
const strings=[]
|
|
386
|
+
const array=new BigUint64Array([...value.map(v=>{
|
|
387
|
+
if(typeof v==='string'){const s=interop.cstring(v);strings.push(s);return s.toBigInt()}
|
|
388
|
+
if(typeof v==='number'){return BigInt(v)}
|
|
389
|
+
return pointer(v)?.toBigInt()??0n
|
|
390
|
+
}),...(type.zero?[0n]:[])])
|
|
391
|
+
cleanup.push(()=>void strings.length)
|
|
392
|
+
return array
|
|
393
|
+
}
|
|
394
|
+
function callbackNative(fn,info,owner){
|
|
395
|
+
const wrapped=(...args)=>{
|
|
396
|
+
try{return pointer(fn(...info.args.map((arg,i)=>fromNative(args[i],arg.type))))}catch(e){fatal(e)}
|
|
397
|
+
}
|
|
398
|
+
// Keep args as raw pointers: object-typed callback args are marshalled
|
|
399
|
+
// inside the trampoline; see triniwiz/linux-runtime#29. wrapObject upgrades
|
|
400
|
+
// them in JS.
|
|
401
|
+
const raw=interop.callback(wrapped,{returns:scalar(info.returns),args:info.args.map(a=>scalar(a.type))})
|
|
402
|
+
owner.push(wrapped);bridgeStats.callbacks++
|
|
403
|
+
return raw
|
|
404
|
+
}
|
|
405
|
+
function notifiedCallbackNative(fn,info){
|
|
406
|
+
const owned=[]
|
|
407
|
+
const callback=callbackNative(fn,info,owned)
|
|
408
|
+
let released=false
|
|
409
|
+
const destroy=()=>{
|
|
410
|
+
if(released){return}
|
|
411
|
+
released=true
|
|
412
|
+
// Native code is still executing the destroy trampoline on this stack.
|
|
413
|
+
deferRelease(owned[0])
|
|
414
|
+
deferRelease(destroy)
|
|
415
|
+
}
|
|
416
|
+
try{
|
|
417
|
+
const notify=interop.callback(destroy,{returns:t.void,args:[t.pointer]})
|
|
418
|
+
bridgeStats.callbacks++
|
|
419
|
+
return {callback,notify,destroy}
|
|
420
|
+
}catch(error){
|
|
421
|
+
interop.release(owned[0]);bridgeStats.callbacks--
|
|
422
|
+
throw error
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
function invoke(info,values,self){
|
|
426
|
+
const hidden=new Set(),out=[],args=[],cleanup=[],callbacks=[],retain=[]
|
|
427
|
+
const notified=new Map()
|
|
428
|
+
let nativeCallStarted=false
|
|
429
|
+
const byteReturn=info.returns.tag===15&&info.returns.element?.tag===3&&info.returns.length>=0
|
|
430
|
+
let byteLength
|
|
431
|
+
try{
|
|
432
|
+
for(const arg of info.args){if(arg.type.tag===15&&arg.type.length>=0){hidden.add(arg.type.length);}if(arg.closure>=0){hidden.add(arg.closure);}if(arg.destroy>=0){hidden.add(arg.destroy)}}
|
|
433
|
+
const lengths=new Map();let index=0
|
|
434
|
+
let inputIndex=0
|
|
435
|
+
info.args.forEach((arg,i)=>{
|
|
436
|
+
if(hidden.has(i)||arg.direction===1){return}
|
|
437
|
+
const value=values[inputIndex++]
|
|
438
|
+
if(arg.type.tag===15&&arg.type.length>=0&&value!=null){lengths.set(arg.type.length,typeof value==='string'?new TextEncoder().encode(value).length:value.length)}
|
|
439
|
+
})
|
|
440
|
+
if(self){
|
|
441
|
+
if(self.__ref?.ns==='Gsk'&&self.__ref.name==='Transform'){
|
|
442
|
+
if(info.instanceTransfer===undefined){throw Error('Re-export GI schema with instance ownership metadata')}
|
|
443
|
+
if(self.__transformFreed){throw Error('Gsk.Transform already released')}
|
|
444
|
+
if(info.instanceTransfer===2){retain.push(()=>C.gsk_transform_ref(pointer(self)))}
|
|
445
|
+
}else if(info.instanceTransfer===2&&self.__gobject){retain.push(()=>g_object_ref(pointer(self)))}
|
|
446
|
+
args.push(pointer(self))
|
|
447
|
+
}
|
|
448
|
+
for(let i=0;i<info.args.length;i++){
|
|
449
|
+
const arg=info.args[i],type=arg.type
|
|
450
|
+
if(byteReturn&&i===info.returns.length){
|
|
451
|
+
if(arg.direction!==1){throw Error('Byte array return length must be an out parameter')}
|
|
452
|
+
byteLength=new interop.Reference(typeForRef(type))
|
|
453
|
+
args.push(byteLength);continue
|
|
454
|
+
}
|
|
455
|
+
if(hidden.has(i)){args.push(lengths.get(i)??null);continue}
|
|
456
|
+
if(arg.direction===1){
|
|
457
|
+
let memory,read
|
|
458
|
+
if(type.tag===16&&[3,4].includes(type.interface.kind)){
|
|
459
|
+
const ref=type.interface
|
|
460
|
+
if(ref.ns==='Graphene'&&ref.name==='Matrix'){
|
|
461
|
+
// Graphene owns the SIMD layout and alignment on each architecture.
|
|
462
|
+
memory=interop.adopt(C.graphene_matrix_alloc(),C.graphene_matrix_free)
|
|
463
|
+
if(!memory){throw Error('Graphene could not allocate a matrix')}
|
|
464
|
+
read=()=>wrapStruct(memory,ref)
|
|
465
|
+
}else{
|
|
466
|
+
try{memory=new (rawCtor(ref.ns,ref.name))();read=()=>wrapStruct(memory,ref)}
|
|
467
|
+
catch{const flat=outStructFallback(ref);memory=flat.alloc();read=()=>flat.read(memory);cleanup.push(()=>interop.free(memory))}
|
|
468
|
+
}
|
|
469
|
+
}else{memory=new interop.Reference(typeForRef(type));read=()=>fromNative(memory.value,type,arg.transfer)}
|
|
470
|
+
args.push(memory);out.push({type,read});continue
|
|
471
|
+
}
|
|
472
|
+
let value=values[index++]
|
|
473
|
+
if(arg.direction===2&&!type.pointer&&type.tag>=1&&type.tag<=12){
|
|
474
|
+
const memory=new interop.Reference(typeForRef(type))
|
|
475
|
+
memory.value=value
|
|
476
|
+
args.push(memory)
|
|
477
|
+
out.push({type,read:()=>fromNative(memory.value,type,arg.transfer)})
|
|
478
|
+
continue
|
|
479
|
+
}
|
|
480
|
+
if(type.tag===15&&value!=null){if(type.length>=0){lengths.set(type.length,typeof value==='string'?new TextEncoder().encode(value).length:value.length);}const properties=data.Gtk.AccessibleProperty.values
|
|
481
|
+
const stringProperties=['DESCRIPTION','KEY_SHORTCUTS','LABEL','ROLE_DESCRIPTION','VALUE_TEXT','PLACEHOLDER'].map(k=>properties[k])
|
|
482
|
+
const doubleProperties=['VALUE_MIN','VALUE_MAX','VALUE_NOW'].map(k=>properties[k])
|
|
483
|
+
const hints=info.symbol==='gtk_accessible_update_property_value'&&arg.name==='values'?values[0].map(p=>stringProperties.includes(p)?64:doubleProperties.includes(p)?60:undefined):undefined
|
|
484
|
+
value=prepareArray(value,type,cleanup,hints)}
|
|
485
|
+
else if(type.tag===17&&Array.isArray(value)){
|
|
486
|
+
// GList input parameters borrow a native list, not the JS array.
|
|
487
|
+
if(arg.transfer!==0){throw Error('Ownership-taking GList inputs are not projected')}
|
|
488
|
+
let list=null
|
|
489
|
+
cleanup.push(()=>{if(list){g_list_free(list)}})
|
|
490
|
+
for(const item of value){
|
|
491
|
+
if(!item?.__ref||!isObject(item.__ref)){throw Error('GList inputs require projected GObjects')}
|
|
492
|
+
list=g_list_append(list,pointer(item))
|
|
493
|
+
}
|
|
494
|
+
value=list
|
|
495
|
+
}
|
|
496
|
+
else if(type.tag===16&&type.interface.kind===2&&typeof value==='function'){
|
|
497
|
+
const callbackInfo=data[type.interface.ns][type.interface.name]
|
|
498
|
+
if(arg.scope===GI_SCOPE_NOTIFIED&&arg.destroy>=0){
|
|
499
|
+
const owned=notifiedCallbackNative(value,callbackInfo)
|
|
500
|
+
notified.set(arg.destroy,owned)
|
|
501
|
+
value=owned.callback
|
|
502
|
+
}else {value=callbackNative(value,callbackInfo,callbacks)}
|
|
503
|
+
}
|
|
504
|
+
else if(type.tag===16&&type.interface.ns==='GObject'&&type.interface.name==='Value'&&arg.transfer===0&&typeof value==='string'){
|
|
505
|
+
const boxed=new _GValue()
|
|
506
|
+
fillValue(boxed,value)
|
|
507
|
+
cleanup.push(()=>g_value_unset(boxed))
|
|
508
|
+
value=boxed
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
// A transfer-full parameter consumes a native reference; add one for the
|
|
512
|
+
// callee's consumption — the wrapper's own ref stays ours to release.
|
|
513
|
+
if(arg.transfer===2&&value?.__ref){
|
|
514
|
+
if(isObject(value.__ref)){retain.push(()=>g_object_ref(pointer(value)))}
|
|
515
|
+
else if(value.__ref.ns==='Gsk'&&value.__ref.name==='Transform'){
|
|
516
|
+
if(value.__transformFreed){throw Error('Gsk.Transform already released')}
|
|
517
|
+
retain.push(()=>C.gsk_transform_ref(pointer(value)))
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
value=pointer(value)
|
|
521
|
+
}
|
|
522
|
+
args.push(value??null)
|
|
523
|
+
}
|
|
524
|
+
const error=info.throws?new interop.Reference(t.pointer):null
|
|
525
|
+
if(error){args.push(error)}
|
|
526
|
+
for(const ref of retain){ref()}
|
|
527
|
+
for(const [index,owned] of notified){args[index+(self?1:0)]=owned.notify}
|
|
528
|
+
nativeCallStarted=true
|
|
529
|
+
const raw=C[info.symbol](...args)
|
|
530
|
+
if(error?.value){const native=new interop.Reference(_GError,error.value).value;const message=decode(native.message);g_error_free(error.value);throw Error(message)}
|
|
531
|
+
const results=out.map(o=>o.read())
|
|
532
|
+
const hasReturn=!(info.returns.tag===0&&!info.returns.pointer)&&!info.skipReturn
|
|
533
|
+
if(hasReturn){
|
|
534
|
+
if(byteReturn){
|
|
535
|
+
// GI hides the length parameter and presents a copied Uint8Array.
|
|
536
|
+
// Transfer-full C buffers belong to the caller; borrowed ones stay native.
|
|
537
|
+
try{
|
|
538
|
+
const length=Number(byteLength.value)
|
|
539
|
+
if(!Number.isSafeInteger(length)||length<0){throw Error('Invalid byte array return length')}
|
|
540
|
+
if(length&&!raw){throw Error('Null byte array with nonzero length')}
|
|
541
|
+
results.unshift(length?new Uint8Array(interop.bufferFromData(raw,length)).slice():new Uint8Array())
|
|
542
|
+
}finally{if(info.transfer===2&&raw){g_free(raw)}}
|
|
543
|
+
}else {results.unshift(fromNative(raw,info.returns,info.transfer))}
|
|
544
|
+
}
|
|
545
|
+
return results.length>1?results:results[0]
|
|
546
|
+
}catch(error){throw Error(`${info.symbol}: ${error.message}`,{cause:error})}
|
|
547
|
+
finally{
|
|
548
|
+
for(const fn of cleanup){fn()}
|
|
549
|
+
if(!nativeCallStarted){for(const owned of notified.values()){owned.destroy()}}
|
|
550
|
+
// Only call-scoped callbacks are safe to release immediately. Longer-lived
|
|
551
|
+
// callbacks remain associated with the current explicit ownership scope.
|
|
552
|
+
for(const fn of callbacks){(activeScope??permanent).callbacks?.add(fn)}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function signalInfo(ref,name){
|
|
556
|
+
const record=data[ref.ns]?.[ref.name]
|
|
557
|
+
if(record?.signals?.[name]){return record.signals[name]}
|
|
558
|
+
for(const iface of record?.interfaces??[]){const found=signalInfo(iface,name);if(found){return found}}
|
|
559
|
+
return record?.parent?signalInfo(record.parent,name):null
|
|
560
|
+
}
|
|
561
|
+
function connect(object,signal,handler,after=false){
|
|
562
|
+
const info=signalInfo(object.__ref,signal.split('::')[0])
|
|
563
|
+
const ref=object.__ref
|
|
564
|
+
const converted=(self,...args)=>{
|
|
565
|
+
try{
|
|
566
|
+
// Runtime signal marshalling already converts scalars and wraps GObjects;
|
|
567
|
+
// fromNative turns those into our proxies and passes the rest through.
|
|
568
|
+
// Unknown signals keep the v1 shape: drop the trailing data pointer.
|
|
569
|
+
const mapped=info?info.args.map((a,i)=>fromNative(args[i],a.type)):args.slice(0,-1)
|
|
570
|
+
// The C closure keeps `converted` alive for as long as the connection
|
|
571
|
+
// exists — capturing `object` here would pin the widget's own wrapper
|
|
572
|
+
// and leak it. Recover the proxy from `self` instead.
|
|
573
|
+
return pointer(handler(proxies.get(self)??wrapObject(self,ref),...mapped))
|
|
574
|
+
}catch(e){fatal(e)}
|
|
575
|
+
}
|
|
576
|
+
const id=object.__native.connect(signal,converted,after)
|
|
577
|
+
const token={live:true,type:typeNameOf(object),signal}
|
|
578
|
+
object.__conn.set(id,token);liveSignals.add(token);registry.register(converted,token,converted)
|
|
579
|
+
bridgeStats.signals++;return id
|
|
580
|
+
}
|
|
581
|
+
function registerClass(options,klass){
|
|
582
|
+
if(typeof options==='function'){klass=options;options={}}
|
|
583
|
+
const parent=Object.getPrototypeOf(klass),ref=parent.__ref,record=data[ref.ns][ref.name]
|
|
584
|
+
const query=new _GTypeQuery();g_type_query(parent.__gtype,query)
|
|
585
|
+
const owned=[]
|
|
586
|
+
const init=classPointer=>{
|
|
587
|
+
for(const name of Object.getOwnPropertyNames(klass.prototype)){
|
|
588
|
+
if(!name.startsWith('vfunc_')){continue}
|
|
589
|
+
const virtual=name.slice(6);let owner=record;while(!owner.vfuncs?.[virtual]&&owner.parent){owner=data[owner.parent.ns][owner.parent.name];}const info=owner.vfuncs?.[virtual]
|
|
590
|
+
if(!info){throw Error(`Unsupported inherited vfunc ${ref.ns}.${ref.name}.${virtual}`)}
|
|
591
|
+
const cb=(self,...args)=>{
|
|
592
|
+
const instance=wrapObject(self,klass.__ref)
|
|
593
|
+
const inputs=[],outputs=[]
|
|
594
|
+
info.args.forEach((arg,i)=>{if(arg.direction===1){outputs.push([arg,args[i]]);}else {inputs.push(fromNative(args[i],arg.type))}})
|
|
595
|
+
const result=klass.prototype[name].apply(instance,inputs)
|
|
596
|
+
if(outputs.length){const values=Array.isArray(result)?result:[result];outputs.forEach(([arg,p],i)=>{new interop.Reference(typeForRef(arg.type),p).value=values[i]})}
|
|
597
|
+
return outputs.length?undefined:pointer(result)
|
|
598
|
+
}
|
|
599
|
+
owned.push(cb)
|
|
600
|
+
const slot=new interop.Reference(rawCtor(owner.classStruct.ns,owner.classStruct.name),classPointer).value
|
|
601
|
+
slot[virtual]=interop.callback(cb,{returns:scalar(info.returns),args:[t.pointer,...info.args.map(a=>a.direction===1?t.pointer:scalar(a.type))]})
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
const gtype=g_type_register_static_simple(parent.__gtype,options.GTypeName??klass.name,query.class_size,init,query.instance_size,null,0)
|
|
605
|
+
installGType(klass,gtype);Object.defineProperty(klass,'__ref',{value:ref})
|
|
606
|
+
permanent.callbacks.add(init);owned.forEach(f=>permanent.callbacks.add(f))
|
|
607
|
+
// super.vfunc_focus must call the actual parent's slot rather than recurse.
|
|
608
|
+
if(record.classStruct){const parentPointer=g_type_class_ref(parent.__gtype);const native=new interop.Reference(rawCtor(record.classStruct.ns,record.classStruct.name),parentPointer).value
|
|
609
|
+
for(const [name,info]of Object.entries(record.vfuncs??{})){if(!parent.prototype['vfunc_'+name]){parent.prototype['vfunc_'+name]=function(...args){return fromNative(native[name](this.__native,...args.map(pointer)),info.returns)}}}
|
|
610
|
+
}
|
|
611
|
+
return klass
|
|
612
|
+
}
|
|
613
|
+
for(const [ns,records]of Object.entries(data)){for(const [name,record]of Object.entries(records)){if(record.typeName){byTypeName.set(record.typeName,{__lazy:[ns,name]})}}}
|
|
614
|
+
// Resolve lazy constructor lookup before instance projection.
|
|
615
|
+
for(const value of byTypeName.values()){if(value.__lazy){Object.defineProperty(value,'prototype',{get(){return getClass(...value.__lazy).prototype}})}}
|
|
616
|
+
export const namespaces={}
|
|
617
|
+
for(const [ns,records]of Object.entries(data)){namespaces[ns]=new Proxy({}, {get(_,name){
|
|
618
|
+
const record=records[name]
|
|
619
|
+
if(!record){return undefined}
|
|
620
|
+
if(record.values){return record.values}
|
|
621
|
+
if('value'in record){return record.value}
|
|
622
|
+
if(record.symbol){return typeof C[record.symbol]==='function'? (...args)=>invoke(record,args):undefined}
|
|
623
|
+
return getClass(ns,name)
|
|
624
|
+
}})}
|
|
625
|
+
// Constants not included in typelib exports or represented as fundamental macros.
|
|
626
|
+
namespaces.GObject=new Proxy(namespaces.GObject,{get(target,p){if(p==='registerClass'){return registerClass;}if(p==='TYPE_INT'){return 24;}if(p==='TYPE_BOOLEAN'){return 20;}if(p==='TYPE_DOUBLE'){return 60;}if(p==='TYPE_STRING'){return 64;}return target[p]}})
|
|
627
|
+
const timers=new Map()
|
|
628
|
+
const deferRelease=callback=>setTimeout(()=>{interop.release(callback);bridgeStats.callbacks--},0)
|
|
629
|
+
const addSource=(priority,ms,fn,idle=false)=>{
|
|
630
|
+
let id
|
|
631
|
+
const callback=()=>{
|
|
632
|
+
try {
|
|
633
|
+
const again=!!fn()
|
|
634
|
+
if(!again){timers.delete(id);bridgeStats.sources--;deferRelease(callback)}
|
|
635
|
+
return again
|
|
636
|
+
}catch(error){fatal(error);return false}
|
|
637
|
+
}
|
|
638
|
+
id=idle?g_idle_add_full(priority,callback,null,null):g_timeout_add_full(priority,ms,callback,null,null)
|
|
639
|
+
timers.set(id,callback);bridgeStats.sources++;bridgeStats.callbacks++
|
|
640
|
+
activeScope?.timers.add(id)
|
|
641
|
+
return id
|
|
642
|
+
}
|
|
643
|
+
const glibExtras={PRIORITY_DEFAULT:0,PRIORITY_DEFAULT_IDLE:200,SOURCE_REMOVE:false,SOURCE_CONTINUE:true,
|
|
644
|
+
timeout_add:(priority,ms,fn)=>addSource(priority,ms,fn),
|
|
645
|
+
idle_add:(priority,fn)=>addSource(priority,0,fn,true),
|
|
646
|
+
source_remove:id=>{const callback=timers.get(id);if(!callback){return false;}g_source_remove(id);timers.delete(id);bridgeStats.sources--;deferRelease(callback);return true},
|
|
647
|
+
}
|
|
648
|
+
namespaces.GLib=new Proxy(namespaces.GLib,{get(target,p){return p in glibExtras?glibExtras[p]:target[p]}})
|
|
649
|
+
C.imports={gi:new Proxy({versions:{}},{get(target,p){
|
|
650
|
+
if(p in target){return target[p]}
|
|
651
|
+
// The facade "typelib" only exists when its compiled module is present —
|
|
652
|
+
// report it missing like a real GIRepository lookup would.
|
|
653
|
+
if(p==='XplatMason'&&data.XplatMason){return typeof C.xplat_mason_engine_new==='function'?namespaces.XplatMason:null}
|
|
654
|
+
return namespaces[p]??null
|
|
655
|
+
}}),system:{gc:()=>C.gc?.()}}
|
|
656
|
+
export function beginScope(){const scope=new Set();scope.pending=new Map();scope.callbacks=new Set();scope.timers=new Set();scopes.add(scope);activeScope=scope;return scope}
|
|
657
|
+
// Report what survives full teardown so probes can tell process-global
|
|
658
|
+
// anchors (Gtk.Settings watches, registered class init) from real leaks.
|
|
659
|
+
export function describeResidual(){
|
|
660
|
+
const signals=[...liveSignals].filter(t=>t.live).map(t=>({type:t.type,signal:t.signal}))
|
|
661
|
+
return {objects:[...pendingFinalizers.values()],signals,permanentCallbacks:permanent.callbacks.size,timers:[...timers.keys()]}
|
|
662
|
+
}
|
|
663
|
+
export function releaseScope(scope){
|
|
664
|
+
activeScope=null
|
|
665
|
+
for(const id of scope.timers){glibExtras.source_remove(id)}
|
|
666
|
+
for(const fn of scope.callbacks){interop.release(fn);bridgeStats.callbacks--}
|
|
667
|
+
scope.callbacks.clear();scope.timers.clear()
|
|
668
|
+
scopes.delete(scope);scope.clear()
|
|
669
|
+
}
|
|
670
|
+
// GType names of objects created under `scope` that have not finalized yet —
|
|
671
|
+
// scope.pending is a creation-time snapshot, liveness lives in
|
|
672
|
+
// pendingFinalizers.
|
|
673
|
+
export function pendingInScope(scope){
|
|
674
|
+
return [...scope.pending.entries()].filter(([k])=>pendingFinalizers.has(k)).map(([,n])=>n)
|
|
675
|
+
}
|