1 ;;;; package lock tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
16 ;;; callbacks only on ppc/darwin currently
17 #-darwin (quit :unix-status 104)
19 ;;; simple callback for a function
25 (sb-alien::alien-callback (function c-string) #'thunk))
27 (assert (equal (with-output-to-string (*standard-output*)
28 (alien-funcall *thunk*))
31 ;;; simple callback for a symbol
33 (defun add-two-ints (arg1 arg2)
36 (defvar *add-two-ints*
37 (sb-alien::alien-callback (function int int int) 'add-two-ints))
39 (assert (= (alien-funcall *add-two-ints* 555 444444) 444999))
41 ;;; actually using a callback with foreign code
43 (define-alien-routine qsort void
47 (compar (function int (* double) (* double))))
49 (sb-alien::define-alien-callback double*-cmp int ((arg1 (* double)) (arg2 (* double)))
50 (let ((a1 (deref arg1))
56 (let* ((vector (coerce '(0.1d0 0.5d0 0.2d0 1.2d0 1.5d0 2.5d0 0.0d0 0.1d0 0.2d0 0.3d0)
57 '(vector double-float)))
58 (sorted (sort (copy-seq vector) #'<)))
60 (sb-sys:with-pinned-objects (vector)
61 (qsort (sb-sys:vector-sap vector)
63 (alien-size double :bytes)
65 (assert (equalp vector sorted)))
69 (sb-alien::define-alien-callback redefined-fun int ()
73 '(sb-alien::define-alien-callback redefined-fun int ()
76 (assert (= 42 (alien-funcall redefined-fun)))
78 (sb-alien::define-alien-callback return-single float ((x float))
81 (sb-alien::define-alien-callback return-double double ((x double))
84 (defconstant spi (coerce pi 'single-float))
86 (assert (= spi (alien-funcall return-single spi)))
87 (assert (= pi (alien-funcall return-double pi)))
91 (sb-alien::define-alien-callback to-be-invalidated int ()
94 (assert (= 5 (alien-funcall to-be-invalidated)))
96 (multiple-value-bind (p valid) (sb-alien::alien-callback-p to-be-invalidated)
100 (sb-alien::invalidate-alien-callback to-be-invalidated)
102 (multiple-value-bind (p valid) (sb-alien::alien-callback-p to-be-invalidated)
104 (assert (not valid)))
106 (multiple-value-bind (res err)
107 (ignore-errors (alien-funcall to-be-invalidated))
108 (assert (and (not res) (typep err 'error))))
110 ;;; getting and setting the underlying function
112 (sb-alien::define-alien-callback foo int ()
117 (assert (eq #'foo (sb-alien::alien-callback-function foo)))
122 (setf (sb-alien::alien-callback-function foo) #'bar)
124 (assert (eq #'bar (sb-alien::alien-callback-function foo)))
126 (assert (= 26 (alien-funcall foo)))
128 (quit :unix-status 104)