forked from commonqt/commonqt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.lisp
420 lines (357 loc) · 15.5 KB
/
call.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2009 David Lichteblau. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :qt)
(named-readtables:in-readtable :qt)
(defun pointer->cached-object (ptr)
(gethash (cffi:pointer-address ptr) *weakly-cached-objects*))
(defun (setf pointer->cached-object) (newval ptr)
(setf (gethash (cffi:pointer-address ptr) *weakly-cached-objects*)
newval))
(defun %deletion-callback (obj)
(restart-case
(let ((object (pointer->cached-object obj)))
(when object
(note-deleted object)))
(abort ()
:report (lambda (stream) (write-string "Abort smoke callback" stream)))))
(defun %method-invocation-callback (smoke method-idx obj stack abstractp)
(declare (ignore abstractp))
(restart-case
(let* ((<module> (module-number smoke))
(object (pointer->cached-object obj))
(<method> (bash method-idx <module> +method+))
(fun (and object (find-method-override object <method>))))
(if fun
(let* ((args
(loop for type in (list-qmethod-argument-types <method>)
for i from 1
for item = (cffi:mem-aref stack
'|union StackItem|
i)
collect (unmarshal type item)))
(result (override fun object <method> args))
(rtype (qmethod-return-type <method>)))
(unless (qtype-void-p rtype)
(marshal result rtype stack (lambda ())))
1)
0))
(abort ()
:report (lambda (stream) (write-string "Abort smoke callback" stream))
0)))
(defun %child-callback (added obj)
(restart-case
(let ((object (pointer->cached-object obj)))
(when object
(if (zerop added)
(note-child-removed object)
(note-child-added object))))
(abort ()
:report (lambda (stream) (write-string "Abort smoke callback" stream)))))
(defclass abstract-qobject ()
((class :initarg :class
:accessor qobject-class)))
(defclass null-qobject (abstract-qobject)
())
(defun null-qobject (class)
(make-instance 'null-qobject :class (find-qclass class)))
(defgeneric qobject-pointer (qobject))
(defmethod qobject-pointer ((object null-qobject))
(cffi:null-pointer))
(defclass qobject (abstract-qobject)
((pointer :initarg :pointer
:initform :unborn
:accessor qobject-pointer)
(deleted :initform nil
:accessor qobject-deleted)))
(defmethod print-object ((instance qobject) stream)
(print-unreadable-object (instance stream :type nil :identity nil)
(cond
((not (slot-boundp instance 'class))
(format stream "uninitialized"))
((cffi:pointerp (qobject-pointer instance))
(format stream "~A 0x~8,'0X"
(qclass-name (qobject-class instance))
(cffi:pointer-address (qobject-pointer instance))))
(t
(format stream "~A ~A"
(qclass-name (qobject-class instance))
(qobject-pointer instance))))))
(defmethod print-object ((instance null-qobject) stream)
(print-unreadable-object (instance stream :type nil :identity nil)
(format stream "~A NULL"
(qclass-name (qobject-class instance)))))
(defclass primitive ()
((value :initarg :value :accessor primitive-value)))
(defmethod print-object ((instance primitive) stream)
(print-unreadable-object (instance stream :type t :identity nil)
(format stream "~A" (primitive-value instance))))
(defmacro defprimitive (name (superclass) type)
`(progn
(defclass ,name (,superclass) ())
(defun ,name (value)
(check-type value ,type)
(make-instance ',name :value value))))
(defclass $ (primitive) ())
(defclass ? (primitive) ())
;;; (defprimitive int ($) (signed-byte 32))
;;; (defprimitive uint ($) (unsigned-byte 32))
;;; (defprimitive bool ($) (signed-byte 32))
;;; (defprimitive char* ($) (satisfies cffi:pointerp))
;;; (defprimitive char** (?) (satisfies cffi:pointerp))
;;; (defprimitive qstring ($) string)
;;; (defprimitive qstringlist (?) (satisfies cffi:pointerp))
;;; (defprimitive int& ($) (satisfies cffi:pointerp))
;;; (defprimitive void** (?) (satisfies cffi:pointerp))
;;; (defprimitive bool* ($) (satisfies cffi:pointerp))
;;; (defprimitive quintptr (?) (satisfies cffi:pointerp))
(defclass enum ($)
((type-name :initarg :type-name
:accessor enum-type-name)))
(defun enum (value type-name)
(check-type value (signed-byte 32))
(make-instance 'enum :type-name type-name :value value))
#+nil
(defmethod print-object ((instance primitive) stream)
(print-unreadable-object (instance stream :type t :identity nil)
(format stream "~A"
(primitive-value instance))))
(defmethod print-object ((instance enum) stream)
(print-unreadable-object (instance stream :type t :identity nil)
(format stream "~A ~A"
(enum-type-name instance)
(primitive-value instance))))
(defun enum= (a b)
(and (eq (enum-type-name a) (enum-type-name b))
(eql (primitive-value a) (primitive-value b))))
(defun enum-or (&rest enums)
(reduce #'logior enums
:key (lambda (x) (if (integerp x) x (primitive-value x)))))
(defclass qthread ()
((pointer :initarg :pointer
:accessor qthread-pointer)))
(defmethod print-object ((instance qthread) stream)
(print-unreadable-object (instance stream :type t :identity nil)
(format stream "~X" (cffi:pointer-address (qthread-pointer instance)))))
(defun qobject= (x y)
(cffi-sys:pointer-eq (qobject-pointer x) (qobject-pointer y)))
(defun %qobject (class ptr)
(let ((cached (pointer->cached-object ptr)))
(if (and cached
(qsubclassp (qobject-class cached) class))
cached
(if (cffi:null-pointer-p ptr)
(make-instance 'null-qobject :class class)
(let ((actual-class (or (when (qsubclassp class (find-qclass "QObject"))
(instance-qclass ptr nil))
class)))
(make-instance 'qobject :class actual-class :pointer ptr))))))
(flet ((note-lisp-type-for-stack-slot (slot type)
(setf (get slot 'lisp-type-for-stack-slot) type)))
#+nil (note-lisp-type-for-stack-slot 'class 'abstract-qobject)
(note-lisp-type-for-stack-slot 'ptr '(or (satisfies cffi-sys:pointerp)
string))
(note-lisp-type-for-stack-slot 'bool 't #+nil boolean)
(note-lisp-type-for-stack-slot 'enum '(or (unsigned-byte 32) enum))
(note-lisp-type-for-stack-slot 'uint '(or (unsigned-byte 32) enum))
(note-lisp-type-for-stack-slot 'int '(or (signed-byte 32) enum))
(note-lisp-type-for-stack-slot 'ulong '(unsigned-byte 64)) ;fixme
(note-lisp-type-for-stack-slot 'long '(signed-byte 64)) ;fixme
(note-lisp-type-for-stack-slot 'ushort '(unsigned-byte 16))
(note-lisp-type-for-stack-slot 'short '(signed-byte 16))
(note-lisp-type-for-stack-slot 'float 'number)
(note-lisp-type-for-stack-slot 'double 'number))
#+nil
(flet ((note-lisp-type-for-qtype (interned-type-name type)
(setf (get interned-type-name 'lisp-type-for-qtype) type)))
(note-lisp-type-for-qtype :|const QString&| 'string)
(note-lisp-type-for-qtype :|const char*| 'string)
(note-lisp-type-for-qtype :|const QList<int>&| 'qlist<int>))
(defvar *marshalling-tests* (make-hash-table))
(defmacro define-marshalling-test ((var type) &body body)
`(setf (gethash ,type *marshalling-tests*)
#'(lambda (,var) ,@body)))
(defun can-marshal-p (lisp-object <type>)
(let ((slot (qtype-stack-item-slot <type>)))
(alexandria:if-let ((test (gethash (qtype-interned-name
(qtype-deconstify <type>))
*marshalling-tests*)))
(funcall test lisp-object)
(cond ((typep lisp-object 'abstract-qobject)
(and (eq slot 'class)
(qtypep lisp-object (qtype-class <type>))))
((let ((element-type (qlist-element-type <type>)))
(and element-type
(alexandria:proper-list-p lisp-object)
(iter (for item in lisp-object)
(always (can-marshal-p item element-type))))))
((and (not (eq slot 'class))
(typep lisp-object
`(and ,(or (get slot 'lisp-type-for-stack-slot)
(progn
(warn "slot ~A not implemented"
(qtype-stack-item-slot <type>))
nil))
#+nil
,(get (qtype-interned-name <type>)
'lisp-type-for-qtype t)))))
((type= (qtype-deconstify <type>)
(with-cache () (qt::find-qtype "QByteArray")))
(typep lisp-object 'string))))))
(defun find-applicable-method (object name args fix-types)
(qclass-find-applicable-method (if (integerp object)
object
(qobject-class object))
name
args
fix-types))
(defun type= (x y)
(and (eq (qtype-kind x) (qtype-kind y))
(eq (qtype-interned-name x) (qtype-interned-name y))
(eq (qtype-stack-item-slot x) (qtype-stack-item-slot y))))
(defun method-signature= (a b)
(let ((r (list-qmethod-argument-types a))
(s (list-qmethod-argument-types b)))
(and (eql (length r) (length s))
(every #'type= r s))))
(defun qclass-find-applicable-method (class method-name args fix-types)
(labels ((recurse (c)
(append (list-class-methods-named c method-name)
(iter (for super in (list-qclass-superclasses c))
(appending (recurse super))))))
(let ((methods #+nil (remove-duplicates (recurse class)
:from-end t
:test #'method-signature=)
(recurse class)))
(cond
((null methods)
nil)
((cdr methods)
(find-if (lambda (method)
(method-applicable-p method args fix-types))
methods))
(t
(car methods))))))
(defun method-applicable-p (method args &optional fix-types)
(let ((argtypes (list-qmethod-argument-types method)))
(and (iter (for method-argtype in argtypes)
(for fix-type in fix-types)
(always (or (null fix-type)
(eq (qtype-interned-name method-argtype)
fix-type))))
(eql (length argtypes) (length args))
(every #'can-marshal-p args argtypes))))
(defun qtypep (instance thing)
(when (stringp thing)
(setf thing (find-qtype thing)))
(let ((kind (nth-value 2 (unbash thing))))
(cond
((not (typep instance 'abstract-qobject)) nil)
((eql kind +class+) (qsubclassp (qobject-class instance) thing))
((eql kind +type+) (qtypep instance (qtype-class thing)))
(t (error "not a type or class: ~A" thing)))))
(defun qsubclassp (a b)
(or (eq a b)
(some (lambda (super) (qsubclassp super b))
(list-qclass-superclasses a))))
;; for reference results, return new values as multiple return values
(defun splice-reference-result (result-list newval)
(destructuring-bind (primary-return-value &rest rest) result-list
(list* primary-return-value newval rest)))
(defun string-vector-to-char**! (ptr vector)
(loop
for i from 0
for elt across vector
do
(setf (cffi:mem-aref ptr :pointer i)
(cffi:foreign-string-alloc elt))))
(defun string-vector-to-char** (vector)
(let ((ptr (cffi:foreign-alloc :pointer :count (length vector))))
(string-vector-to-char**! ptr vector)
ptr))
(defun char**-to-string-vector! (vector ptr n freep)
(loop
for i from 0 below n
do
(setf (elt vector i)
(cffi:mem-aref ptr :string i))
(when freep
(cffi:foreign-free (cffi:mem-aref ptr :pointer i)))))
(defun char**-to-string-vector (ptr n freep)
(let ((vector (make-array n)))
(char**-to-string-vector! vector ptr n freep)
vector))
(defun unmarshal (type stack-item)
(unmarshal-using-type type stack-item))
(defun null-qobject-p (object)
(typep object 'null-qobject))
#+(or)
(defun run-pending ()
(setf *pending-finalizations*
(remove-if #'funcall *pending-finalizations*)))
(defun note-deleted (object)
(check-type object abstract-qobject)
(unless (qobject-deleted object)
(cancel-finalization object)
(let ((addr (cffi:pointer-address (qobject-pointer object))))
(remhash addr *weakly-cached-objects*)
(remhash addr *strongly-cached-objects*))
(setf (qobject-deleted object) t)))
(defun cancel-finalization (object)
(check-type object abstract-qobject)
(tg:cancel-finalization object))
;; like the OPTIMIZED macros, this cannot be a function because that would
;; foil caching
(defmacro %maybe-delete (object)
`(let ((object ,object))
(unless (or (typep object 'null-qobject)
(qobject-deleted object))
(optimized-delete object))))
(defmacro with-object ((var &optional value) &body body)
;; must not be implemented using a call-with-object function
(if value
`(let ((,var ,value))
(unwind-protect
(progn ,@body)
(%maybe-delete ,var)))
`(let ((,var nil))
(flet ((,var (x) (push x ,var) x))
(unwind-protect
(progn ,@body)
(dolist (object ,var)
(%maybe-delete object)))))))
(defmacro with-objects ((&rest clauses) &body body)
(if clauses
`(with-object ,(car clauses) (with-objects ,(rest clauses) ,@body))
`(progn ,@body)))
(defmethod note-child-added ((object qobject))
(setf (gethash object *keep-alive*) t))
(defmethod note-child-removed ((object qobject))
(remhash object *keep-alive*))
(defun map-cpl (fun class)
(labels ((recurse (c)
(funcall fun c)
(map-qclass-superclasses #'recurse c)))
(recurse class)))