Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
cmucl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Carl Shapiro
cmucl
Commits
33b3b3c0
Commit
33b3b3c0
authored
21 years ago
by
toy
Browse files
Options
Downloads
Patches
Plain Diff
Replace the section "Calling Lisp from C" with rudimentary info about
callback support.
parent
c7bc67be
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/cmu-user/aliens.tex
+106
-14
106 additions, 14 deletions
docs/cmu-user/aliens.tex
with
106 additions
and
14 deletions
docs/cmu-user/aliens.tex
+
106
−
14
View file @
33b3b3c0
...
...
@@ -727,20 +727,112 @@ and two return values (\var{a} and \var{i}).
\subsection
{
Calling Lisp from C
}
Calling Lisp functions from C is sometimes possible, but is rather hackish.
See
\code
{
funcall0
}
...
\code
{
funcall3
}
in the
\file
{
lisp/arch.h
}
. The
arguments must be valid
\cmucl
{}
object descriptors (e.g. fixnums must be
left-shifted by 2.) See
\file
{
compiler/generic/objdef.lisp
}
or the derived
file
\file
{
lisp/internals.h
}
for details of the object representation.
\file
{
lisp/internals.h
}
is mechanically generated, and is not part of the
source distribution. It is distributed in the
\file
{
docs/
}
directory of the
binary distribution.
Note that the garbage collector moves objects, and won't be able to fix up any
references in C variables, so either turn GC off or don't keep Lisp pointers
in C data unless they are to statically allocated objects. You can use
\funref
{
purify
}
to place live data structures in static space so that they
won't move during GC.
% Calling Lisp functions from C is sometimes possible, but is rather hackish.
% See \code{funcall0} ... \code{funcall3} in the \file{lisp/arch.h}. The
% arguments must be valid \cmucl{} object descriptors (e.g. fixnums must be
% left-shifted by 2.) See \file{compiler/generic/objdef.lisp} or the derived
% file \file{lisp/internals.h} for details of the object representation.
% \file{lisp/internals.h} is mechanically generated, and is not part of the
% source distribution. It is distributed in the \file{docs/} directory of the
% binary distribution.
% Note that the garbage collector moves objects, and won't be able to fix up any
% references in C variables, so either turn GC off or don't keep Lisp pointers
% in C data unless they are to statically allocated objects. You can use
% \funref{purify} to place live data structures in static space so that they
% won't move during GC.
\cmucl
{}
supports calling Lisp from C via the
\funref
{
def-callback
}
macro:
\begin{defmac}
{
alien:
}{
def-callback
}{
\var
{
name
}
(
\var
{
return-type
}
\mstar
{
(arg-name arg-type)
}
)
}
This macro defines a Lisp function that can be called from C and a
Lisp variable. The arguments to the function must be alien types,
and the return type must also be an alien type. This Lisp function
can be accessed via the
\funref
{
callback
}
macro.
\var
{
name
}
is the name of the Lisp function. It is also the name of
a variable to be used by the
\code
{
callback
}
macro.
\var
{
return-type
}
is the return type of the function. This must be
a recognized alien type.
\var
{
arg-name
}
specifies the name of the argument to the function,
and the argument has type
\var
{
arg-type
}
, which must be an alien type.
\end{defmac}
\begin{defmac}
{
alien:
}{
callback
}{
\var
{
callback-symbol
}}
This macro extracts the appropriate information for the function
named
\var
{
callback-symbol
}
so that it can be called by a C
function.
\var
{
callback-symbol
}
must be a symbol created by the
\code
{
def-callback
}
macro.
\end{defmac}
\subsection
{
Callback Example
}
Here is a simple example of using callbacks.
\begin{lisp}
(use-package :alien)
(use-package :c-call)
(def-callback foo (int (arg1 int) (arg2 int))
(format t "~
&
foo: ~S, ~S~
%" arg1 arg2)
(+ arg1 arg2))
(defun test-foo ()
(alien-funcall (sap-alien (callback foo) (function int int int))
555 444444))
\end{lisp}
In this example, the callback function
\code
{
foo
}
is defined which
takes two C
\code
{
int
}
parameters and returns a
\code
{
int
}
. As this
shows, we can use arbitrary Lisp inside the function.
The function
\code
{
test-foo
}
shows how we can call this callback
function from Lisp. The macro
\code
{
callback
}
extracts the necessary
information for the callback function
\code
{
foo
}
which can be
converted into a pointer which we can call via
\code
{
alien-funcall
}
.
The following code is a more complete example where a foreign routine
calls our Lisp routine.
\begin{lisp}
(use-package :alien)
(use-package :c-call)
(def-alien-routine qsort void
(base (* t))
(nmemb int)
(size int)
(compar (* (function int (* t) (* t)))))
(def-callback my< (int (arg1 (* double))
(arg2 (* double)))
(let ((a1 (deref arg1))
(a2 (deref arg2)))
(cond ((= a1 a2) 0)
((< a1 a2) -1)
(t +1))))
(defun test-qsort ()
(let ((a (make-array 10 :element-type 'double-float
:initial-contents '(0.1d0 0.5d0 0.2d0 1.2d0 1.5d0
2.5d0 0.0d0 0.1d0 0.2d0 0.3d0))))
(print a)
(qsort (sys:vector-sap a)
(length a)
(alien-size double :bytes)
(alien:callback my<))
(print a)))
\end{lisp}
We define the alien routine,
\code
{
qsort
}
, and a callback,
\code
{
my<
}
,
to determine whether two
\code
{
double
}
's are less than, greater than
or equal to each other.
The test function
\code
{
test-qsort
}
shows how we can call the alien
sort routine with our Lisp comparison routine to produce a sorted
array.
\subsection
{
Accessing Lisp Arrays
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment