source: trunk/abcl/doc/manual/abcl.tex @ 15047

Last change on this file since 15047 was 15047, checked in by Mark Evenson, 6 years ago

Corrections to User Manual

File size: 65.8 KB
Line 
1% -*- mode: latex; -*-
2% http://en.wikibooks.org/wiki/LaTeX/
3\documentclass[10pt]{book}
4% also need to have cm-super installed for high quality rendering
5\usepackage[T1]{fontenc}
6\usepackage{abcl}
7
8\usepackage{hyperref} % Put this one last, it redefines lots of internals
9
10
11\begin{document}
12\title{Armed Bear Common Lisp User Manual}
13\date{Version 1.5.0-rc-0\\
14\smallskip
15June 2017}
16\author{Mark Evenson \and Erik H\"{u}lsmann \and Rudolf Schlatte \and
17  Alessio Stalla \and Ville Voutilainen}
18
19\maketitle
20
21\tableofcontents
22%%Preface to the Sixth edition, abcl-1.5.0
23\subsection{Preface to the Sixth Edition}
24
25With the sixth major release of the implementation, we make the
26following explicit revision of our compatibility to the underlying
27JVM.  Since we are an open source implementation, we insist on
28possible open access to the sources from with an JDK may both be built
29and run upon.  This requirement is no longer met by Java 5, so
30henceforth with the release of \textsc{ABCL} 1.5 ABCL will support
31Java 6, Java 7 and Java 8 runtimes.
32
33%% We should probably not release a 1.5.0, go straight for abcl-1.6.0?
34%% Not worth the effort: Java 9 is the current abcl-1.6.0 release driver.
35
36%%Preface to the Fifth edition, abcl-1.4.0
37\subsection{Preface to the Fifth Edition}
38
39\textsc{ABCL} 1.4 consolidates eighteen months of production bugfixes,
40and substantially improves the support for invoking external
41processes via \code{SYS:RUN-PROGRAM}.
42
43%%Preface to the Fourth edition, abcl-1.3.
44\subsection{Preface to the Fourth Edition}
45
46\textsc{ABCL} 1.3 now implements an optimized implementation of the
47\code{org.armedbare.lisp.LispStack} abstraction thanks to Dmitry
48Nadezhin which runs on ORCL JVMs from 1.[5-8] conformantly.
49
50%%Preface to the Third edition, abcl-1.2.
51\subsection{Preface to the Third Edition}
52The implementation now contains a performant and conformant
53implementation of (A)MOP to the point of inclusion in CLOSER-MOP's
54test suite.
55
56%%Preface to the second edition, abcl-1.1.
57
58\subsection{Preface to the Second Edition}
59
60\textsc{ABCL} 1.1 now contains \textsc{(A)MOP}.  We hope you enjoy!  --The Mgmt.
61
62\chapter{Introduction}
63
64Armed Bear Common Lisp (\textsc{ABCL}) is an implementation of Common
65Lisp that runs on the Java Virtual Machine.  It compiles Common Lisp
66to Java 5 bytecode \footnote{The class file version is ``49.0''.},
67providing the following integration methods for interfacing with Java
68code and libraries:
69\begin{itemize}
70\item Lisp code can create Java objects and call their methods (see
71  Section~\ref{sec:lisp-java}, page~\pageref{sec:lisp-java}).
72\item Java code can call Lisp functions and generic functions, either
73  directly (Section~\ref{sec:calling-lisp-from-java},
74  page~\pageref{sec:calling-lisp-from-java}) or via \texttt{JSR-223}
75  (Section~\ref{sec:java-scripting-api},
76  page~\pageref{sec:java-scripting-api}).
77\item \code{jinterface-implementation} creates Lisp-side implementations
78  of Java interfaces that can be used as listeners for Swing classes and
79  similar.
80\item \code{java:jnew-runtime-class} can inject fully synthetic Java
81  classes--and their objects-- into the current JVM process whose
82  behavior is specified via closures expressed in Common Lisp.. \footnote{Parts of
83    the current implementation are not fully finished, so the status
84    of some interfaces here should be treated with skepticism if you
85    run into problems.}
86
87\end{itemize}
88\textsc{ABCL} is supported by the Lisp library manager
89\textsc{QuickLisp}\footnote{\url{http://quicklisp.org/}} and can run many of the
90programs and libraries provided therein out-of-the-box.
91
92\section{Conformance}
93\label{section:conformance}
94
95\subsection{ANSI Common Lisp}
96\textsc{ABCL} is currently a (non)-conforming \textsc{ANSI} Common Lisp
97implementation due to the following known issues:
98
99\begin{itemize}
100\item The generic function signatures of the \code{CL:DOCUMENTATION} symbol
101  do not match the specification.
102\item The \code{CL:TIME} form does not return a proper \code{CL:VALUES}
103  environment to its caller.
104\item When merging pathnames and the defaults point to a \code{EXT:JAR-PATHNAME},
105  we set the \code{DEVICE} of the result to \code{:UNSPECIFIC} if the pathname to be
106  be merged does not contain a specified \code{DEVICE}, does not contain a
107  specified \code{HOST}, does contain a relative \code{DIRECTORY}, and we are
108  not running on a \textsc{MSFT} Windows platform.\footnote{The intent of this
109    rather arcane sounding deviation from conformance is so that the
110    result of a merge won't fill in a \code{DEVICE} with the wrong "default
111    device for the host" in the sense of the fourth paragraph in the
112    \textsc{CLHS} description of MERGE-PATHNAMES (see in \cite{CLHS} the paragraph beginning
113    "If the PATHNAME explicitly specifies a host and not a device
").
114    A future version of the implementation may return to conformance
115    by using the \code{HOST} value to reflect the type explicitly.
116  }
117
118\end{itemize}
119
120Somewhat confusingly, this statement of non-conformance in the
121accompanying user documentation fulfills the requirements that
122\textsc{ABCL} is a conforming ANSI Common Lisp implementation according
123to the Common Lisp HyperSpec~\cite{CLHS}.  Clarifications to this point
124are solicited.
125
126\textsc{ABCL} aims to be be a fully conforming \textsc{ANSI} Common Lisp implementation.
127Any other behavior should be reported as a bug.
128
129\subsection{Contemporary Common Lisp}
130In addition to \textsc{ANSI} conformance, \textsc{ABCL} strives to implement
131features expected of a contemporary Common Lisp, i.e. a Lisp of the
132post-2005 Renaissance.
133
134The following known problems detract from \textsc{ABCL} being a proper
135contemporary Common Lisp.
136\begin{itemize}
137\item An incomplete implementation of interactive debugging mechanisms,
138  namely a no-op version of \code{STEP} \footnote{Somewhat surprisingly
139    allowed by \textsc{ANSI}}, the inability to inspect local variables
140  in a given call frame, and the inability to resume a halted
141  computation at an arbitrarily selected call frame.
142\item Incomplete streams abstraction, in that \textsc{ABCL} needs a
143  suitable abstraction between \textsc{ANSI} and \textsc{Gray streams}
144  with a runtime switch for the beyond conforming
145  behavior.  \footnote{The streams could be optimized to the
146    \textsc{JVM} NIO \cite{nio} abstractions at great profit for
147    binary byte-level manipulations.}
148\item Incomplete documentation: source code is missing docstrings from
149  all exported symbols from the \code{EXTENSIONS}, \code{SYSTEM},
150  \code{JAVA}, \code{MOP}, and \code{THREADS} packages.  This user
151  manual is currently in draft status.
152\end{itemize}
153
154
155
156\section{License}
157
158\textsc{ABCL} is licensed under the terms of the \textsc{GPL} v2 of
159June 1991 with the ``classpath-exception'' (see the file
160\texttt{COPYING} in the source distribution \footnote{See
161  \url{http://abcl.org/svn/trunk/tags/1.5.0/COPYING}} for
162the license, term 13 in the same file for the classpath exception).
163This license broadly means that you must distribute the sources to
164\textsc{ABCL}, including any changes you make, together with a program that
165includes \textsc{ABCL}, but that you are not required to distribute the sources
166of the whole program.  Submitting your changes upstream to the ABCL
167development team is actively encouraged and very much appreciated, of
168course.
169
170\section{Contributors}
171
172\begin{itemize}
173\item Philipp Marek \texttt{Thanks for the markup}
174\item Douglas Miles \texttt{Thanks for the whacky IKVM stuff and keeping the flame alive
175  in the dark years.}
176\item Alan Ruttenberg \texttt{Thanks for JSS.}
177\item Olof-Joachim Frahm 
178\item piso
179\item and of course
180\emph{Peter Graves}
181\end{itemize}
182
183
184\chapter{Running ABCL}
185
186
187\textsc{ABCL} is packaged as a single jar file usually named either
188\texttt{abcl.jar} or possibly something like \texttt{abcl-1.5.0.jar} if
189using a versioned package on the local filesystem from your system
190vendor.  This jar file can be executed from the command line to obtain a
191\textsc{REPL}\footnote{Read-Eval Print Loop, a Lisp command-line}, viz:
192
193\index{REPL}
194
195\begin{listing-shell}
196  cmd$ java -jar abcl.jar
197\end{listing-shell} %$ unconfuse Emacs syntax highlighting
198
199\emph{N.b.} for the proceeding command to work, the \texttt{java}
200executable needs to be in your path.
201
202To facilitate the use of ABCL in tool chains such as SLIME~\cite{slime}
203(the Superior Lisp Interaction Mode for Emacs), we provide both a Bourne
204shell script and a \textsc{DOS} batch file.  If you or your
205administrator adjusted the path properly, ABCL may be executed simply
206as:
207
208\begin{listing-shell}
209  cmd$ abcl
210\end{listing-shell}%$
211
212Probably the easiest way of setting up an editing environment using the
213\textsc{Emacs} editor is to use \textsc{Quicklisp} and follow the instructions at
214\url{http://www.quicklisp.org/beta/#slime}.
215
216\section{Options}
217
218ABCL supports the following command line options:
219
220\index{Command Line Options}
221
222\begin{description}
223\item[\texttt{  --help}] displays a help message.
224\item[\texttt{  --noinform}] Suppresses the printing of startup information and banner.
225\item[\texttt{  --noinit}] suppresses the loading of the \verb+~/.abclrc+ startup file.
226\item[\texttt{  --nosystem}] suppresses loading the \texttt{system.lisp} customization file.
227\item[\texttt{  --eval FORM}] evaluates FORM before initializing the REPL.
228\item[\texttt{  --load FILE}] loads the file FILE before initializing the REPL.
229\item[\texttt{  --load-system-file FILE}] loads the system file FILE before initializing the REPL.
230\item[\texttt{  --batch}] evaluates forms specified by arguments and in
231  the initialization file \verb+~/.abclrc+, and then exits without
232  starting a \textsc{REPL}.
233\end{description}
234
235All of the command line arguments following the occurrence of \verb+--+
236are passed unprocessed into a list of strings accessible via the
237variable \code{EXT:*COMMAND-LINE-ARGUMENT-LIST*} from within ABCL.
238
239\section{Initialization}
240
241If the \textsc{ABCL} process is started without the \code{--noinit}
242flag, it attempts to load a file named \code{.abclrc} in the user's home
243directory and then interpret its contents.
244
245The user's home directory is determined by the value of the JVM system
246property \texttt{user.home}.  This value may or may not correspond
247to the value of the \texttt{HOME} system environment variable, at the
248discretion of the JVM implementation that \textsc{ABCL} finds itself
249hosted upon.
250
251\chapter{Interaction with the Hosting JVM}
252
253%  Plan of Attack
254%
255% describe calling Java from Lisp, and calling Lisp from Java,
256% probably in two separate sections.  Presumably, we can partition our
257% audience into those who are more comfortable with Java, and those
258% that are more comforable with Lisp
259
260The Armed Bear Common Lisp implementation is hosted on a Java Virtual
261Machine.  This chapter describes the mechanisms by which the
262implementation interacts with that hosting mechanism.
263
264\section{Lisp to Java}
265\label{sec:lisp-java}
266
267\textsc{ABCL} offers a number of mechanisms to interact with Java from its
268Lisp environment. It allows calling both instance and static methods
269of Java objects, manipulation of instance and static fields on Java
270objects, and construction of new Java objects.
271
272When calling Java routines, some values will automatically be
273converted by the FFI\footnote{Foreign Function Interface, is the term
274  of art for the part of a Lisp implementation which implements
275  calling code written in other languages, typically normalized to the
276  local C compiler calling conventions.}  from Lisp values to Java
277values. These conversions typically apply to strings, integers and
278floats. Other values need to be converted to their Java equivalents by
279the programmer before calling the Java object method. Java values
280returned to Lisp are also generally converted back to their Lisp
281counterparts. Some operators make an exception to this rule and do not
282perform any conversion; those are the ``raw'' counterparts of certain
283FFI functions and are recognizable by their name ending with
284\code{-RAW}.
285
286\subsection{Low-level Java API}
287
288This subsection covers the low-level API available after evaluating
289\code{(require :java)}.  A higher level Java API, developed by Alan
290Ruttenberg, is available in the \code{contrib/} directory and described
291later in this document, see Section~\ref{section:jss} on page
292\pageref{section:jss}.
293
294\subsubsection{Calling Java Object Methods}
295
296There are two ways to call a Java object method in the low-level (basic) API:
297
298\begin{itemize}
299\item Call a specific method reference (which was previously acquired)
300\item Dynamic dispatch using the method name and the call-specific
301  arguments provided by finding the best match (see
302  Section~\ref{sec:param-matching-for-ffi}).
303\end{itemize}
304
305\code{JAVA:JMETHOD} is used to acquire a specific method reference.  The
306function takes two or more arguments. The first is a Java class
307designator (a \code{JAVA:JAVA-CLASS} object returned by
308\code{JAVA:JCLASS} or a string naming a Java class). The second is a
309string naming the method.
310
311Any arguments beyond the first two should be strings naming Java
312classes, with one exception as listed in the next paragraph. These
313classes specify the types of the arguments for the method.
314
315When \code{JAVA:JMETHOD} is called with three parameters and the last
316parameter is an integer, the first method by that name and matching
317number of parameters is returned.
318
319Once a method reference has been acquired, it can be invoked using
320\code{JAVA:JCALL}, which takes the method as the first argument. The
321second argument is the object instance to call the method on, or
322\code{NIL} in case of a static method.  Any remaining parameters are
323used as the remaining arguments for the call.
324
325\subsubsection{Calling Java object methods: dynamic dispatch}
326
327The second way of calling Java object methods is by using dynamic dispatch.
328In this case \code{JAVA:JCALL} is used directly without acquiring a method
329reference first. In this case, the first argument provided to \code{JAVA:JCALL}
330is a string naming the method to be called. The second argument is the instance
331on which the method should be called and any further arguments are used to
332select the best matching method and dispatch the call.
333
334\subsubsection{Dynamic dispatch: Caveats}
335
336Dynamic dispatch is performed by using the Java reflection
337API \footnote{The Java reflection API is found in the
338  \code{java.lang.reflect} package}. Generally the dispatch works
339fine, but there are corner cases where the API does not correctly
340reflect all the details involved in calling a Java method. An example
341is the following Java code:
342
343\begin{listing-java}
344ZipFile jar = new ZipFile("/path/to/some.jar");
345Object els = jar.entries();
346Method method = els.getClass().getMethod("hasMoreElements");
347method.invoke(els);
348\end{listing-java}
349
350Even though the method \code{hasMoreElements()} is public in
351\code{Enumeration}, the above code fails with
352
353\begin{listing-java}
354java.lang.IllegalAccessException: Class ... can
355not access a member of class java.util.zip.ZipFile\$2 with modifiers
356"public"
357       at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
358       at java.lang.reflect.Method.invoke(Method.java:583)
359       at ...
360\end{listing-java}
361
362This is because the method has been overridden by a non-public class and
363the reflection API, unlike \texttt{javac}, is not able to handle such a case.
364
365While code like that is uncommon in Java, it is typical of ABCL's FFI
366calls. The code above corresponds to the following Lisp code:
367
368\begin{listing-lisp}
369(let ((jar (jnew "java.util.zip.ZipFile" "/path/to/some.jar")))
370  (let ((els (jcall "entries" jar)))
371    (jcall "hasMoreElements" els)))
372\end{listing-lisp}
373
374except that the dynamic dispatch part is not shown.
375
376To avoid such pitfalls, all Java objects in \textsc{ABCL} carry an extra
377field representing the ``intended class'' of the object. That class is
378used first by \code{JAVA:JCALL} and similar to resolve methods; the
379actual class of the object is only tried if the method is not found in
380the intended class. Of course, the intended class is always a
381super-class of the actual class -- in the worst case, they coincide. The
382intended class is deduced by the return type of the method that
383originally returned the Java object; in the case above, the intended
384class of \code{ELS} is \code{java.util.Enumeration} because that is the
385return type of the \code{entries} method.
386
387While this strategy is generally effective, there are cases where the
388intended class becomes too broad to be useful. The typical example
389is the extraction of an element from a collection, since methods in
390the collection API erase all types to \code{Object}. The user can
391always force a more specific intended class by using the \code{JAVA:JCOERCE}
392operator.
393
394% \begin{itemize}
395% \item Java values are accessible as objects of type JAVA:JAVA-OBJECT.
396% \item The Java FFI presents a Lisp package (JAVA) with many useful
397%   symbols for manipulating the artifacts of expectation on the JVM,
398%   including creation of new objects \ref{JAVA:JNEW}, \ref{JAVA:JMETHOD}), the
399%   introspection of values \ref{JAVA:JFIELD}, the execution of methods
400%   (\ref{JAVA:JCALL}, \ref{JAVA:JCALL-RAW}, \ref{JAVA:JSTATIC})
401% \item The JSS package (\ref{JSS}) in contrib introduces a convenient macro
402%   syntax \ref{JSS:SHARPSIGN_DOUBLEQUOTE_MACRO} for accessing Java
403%   methods, and additional convenience functions.
404% \item Java classes and libraries may be dynamically added to the
405%   classpath at runtime (JAVA:ADD-TO-CLASSPATH).
406% \end{itemize}
407
408\subsubsection{Calling Java class static methods}
409
410Like non-static methods, references to static methods can be acquired by
411using the \code{JAVA:JMETHOD} primitive. Static methods are called with
412\code{JAVA:JSTATIC} instead of \code{JAVA:JCALL}.
413
414Like \code{JAVA:JCALL}, \code{JAVA:JSTATIC} supports dynamic dispatch by
415passing the name of the method as a string instead of passing a method reference.
416The parameter values should be values to pass in the function call instead of
417a specification of classes for each parameter.
418
419\subsubsection{Parameter matching for FFI dynamic dispatch}
420\label{sec:param-matching-for-ffi}
421
422The algorithm used to resolve the best matching method given the name
423and the arguments' types is the same as described in the Java Language
424Specification. Any deviation should be reported as a bug.
425
426% ###TODO reference to correct JLS section
427
428\subsubsection{Instantiating Java objects}
429
430Java objects can be instantiated (created) from Lisp by calling
431a constructor from the class of the object to be created. The
432\code{JAVA:JCONSTRUCTOR} primitive is used to acquire a constructor
433reference. It's arguments specify the types of arguments of the constructor
434method the same way as with \code{JAVA:JMETHOD}.
435
436The obtained constructor is passed as an argument to \code{JAVA:JNEW},
437together with any arguments.  \code{JAVA:JNEW} can also be invoked with
438a string naming the class as its first argument.
439
440\subsubsection{Accessing Java object and class fields}
441
442Fields in Java objects can be accessed using the getter and setter
443functions \code{JAVA:JFIELD} and \code{(SETF JAVA:JFIELD)}.  Static
444(class) fields are accessed the same way, but with a class object or
445string naming a class as first argument.
446
447Like \code{JAVA:JCALL} and friends, values returned from these accessors carry
448an intended class around, and values which can be converted to Lisp values will
449be converted.
450
451\section{Java to Lisp}
452
453This section describes the various ways that one interacts with Lisp
454from Java code.  In order to access the Lisp world from Java, one needs
455to be aware of a few things, the most important ones being listed below:
456
457\begin{itemize}
458\item All Lisp values are descendants of \code{LispObject}.
459\item Lisp symbols are accessible either via static members of the
460  \code{Symbol} class, or by dynamically introspecting a \code{Package}
461  object.
462\item The Lisp dynamic environment may be saved via
463  \code{LispThread.bindSpecial(Binding)} and restored via
464  \code{LispThread.resetSpecialBindings(Mark)}.
465\item Functions can be executed by invoking \code{LispObject.execute(args
466    [...])}
467\end{itemize}
468
469\subsection{Calling Lisp from Java}
470\label{sec:calling-lisp-from-java}
471
472Note: the entire ABCL Lisp system resides in the
473\texttt{org.armedbear.lisp} package, but the following code snippets do
474not show the relevant import statements in the interest of brevity.  An
475example of the import statement would be
476\begin{listing-java}
477  import org.armedbear.lisp.*;
478\end{listing-java}
479to potentially import all the JVM symbol from the `org.armedbear.lisp'
480namespace.
481
482There can only ever be a single Lisp interpreter per JVM instance.  A
483reference to this interpreter is obtained by calling the static method
484\code{Interpreter.createInstance()}.
485
486\begin{listing-java}
487  Interpreter interpreter = Interpreter.createInstance();
488\end{listing-java}
489
490If this method has already been invoked in the lifetime of the current
491Java process it will return \texttt{null}, so if you are writing Java
492whose life-cycle is a bit out of your control (like in a Java servlet),
493a safer invocation pattern might be:
494
495\begin{listing-java}
496  Interpreter interpreter = Interpreter.getInstance();
497  if (interpreter == null) {
498    interpreter = Interpreter.createInstance();
499  }
500\end{listing-java}
501
502
503The Lisp \code{eval} primitive may simply be passed strings for evaluation:
504
505\begin{listing-java}
506  String line = "(load \"file.lisp\")";
507  LispObject result = interpreter.eval(line);
508\end{listing-java}
509
510Notice that all possible return values from an arbitrary Lisp
511computation are collapsed into a single return value.  Doing useful
512further computation on the \code{LispObject} depends on knowing what the
513result of the computation might be.  This usually involves some amount
514of \code{instanceof} introspection, and forms a whole topic to itself
515(see Section~\ref{topic:Introspecting a LispObject},
516page~\pageref{topic:Introspecting a LispObject}).
517
518Using \code{eval} involves the Lisp interpreter.  Lisp functions may
519also be directly invoked by Java method calls as follows.  One simply
520locates the package containing the symbol, obtains a reference to the
521symbol, and then invokes the \code{execute()} method with the desired
522parameters.
523
524\begin{listing-java}
525  interpreter.eval("(defun foo (msg)" +
526    "(format nil \"You told me '~A'~%\" msg))");
527  Package pkg = Packages.findPackage("CL-USER");
528  Symbol foo = pkg.findAccessibleSymbol("FOO");
529  Function fooFunction = (Function)foo.getSymbolFunction();
530  JavaObject parameter = new JavaObject("Lisp is fun!");
531  LispObject result = fooFunction.execute(parameter);
532  // How to get the "naked string value"?
533  System.out.println("The result was " + result.writeToString());
534\end{listing-java}
535
536If one is calling a function in the CL package, the syntax can become
537considerably simpler.  If we can locate the instance of definition in
538the ABCL Java source, we can invoke the symbol directly.  For instance,
539to tell if a \code{LispObject} is (Lisp) \texttt{NIL}, we can invoke the
540CL function \code{NULL} in the following way:
541
542\begin{listing-java}
543  boolean nullp(LispObject object) {
544    LispObject result = Primitives.NULL.execute(object);
545    if (result == NIL) { // the symbol 'NIL' is explicitly named in the Java
546                         // namespace at ``Symbol.NIL''
547                         // but is always present in the
548                         // local namespace in its unadorned form for
549                         // the convenience of the User.
550      return false;
551    }
552    return true;
553 }
554\end{listing-java}
555
556\subsubsection{Multiple Values}
557
558After a call to a function that returns Lisp multiple values, the
559values are associated with the executing \code{LispThread} until the
560next call into Lisp.  One may access the values object as a list of
561\code{LispObject} instances via a call to \code{getValues()} on that
562thread reference
563as evidenced by the following code:
564
565\begin{listing-java}
566
567  org.armedbear.lisp.Package cl = Packages.findPackage("CL");
568  Symbol valuesSymbol = cl.findAccessibleSymbol("VALUES");
569  LispObject[] valuesArgs = {
570    LispInteger.getInstance(1), LispInteger.getInstance(2)
571  };
572  // equivalent to ``(values 1 2)''
573  LispObject result = valuesSymbol.execute(valuesArgs);
574  LispObject[] values = LispThread.currentThread().getValues();
575  for (LispObject value: values) {
576    System.out.println("value ==> " + value.printObject());
577  }
578\end{listing-java}
579
580\subsubsection{Introspecting a LispObject}
581\label{topic:Introspecting a LispObject}
582
583We present various patterns for introspecting an arbitrary
584\code{LispObject} which can hold the result of every Lisp evaluation
585into semantics that Java can meaningfully deal with.
586
587\paragraph{LispObject as \code{boolean}}
588
589If the \code{LispObject} is to be interpreted as a generalized boolean
590value, one can use \code{getBooleanValue()} to convert to Java:
591
592\begin{listing-java}
593   LispObject object = Symbol.NIL;
594   boolean javaValue = object.getBooleanValue();
595\end{listing-java}
596
597Since in Lisp any value other than \code{NIL} means "true", Java
598equality can also be used, which is a bit easier to type and better in
599terms of information it conveys to the compiler:
600
601\begin{listing-java}
602    boolean javaValue = (object != Symbol.NIL);
603\end{listing-java}
604
605\paragraph{LispObject as a list}
606
607If \code{LispObject} is a list, it will have the type \code{Cons}.  One
608can then use the \code{copyToArray} method to make things a bit more
609suitable for Java iteration.
610
611\begin{listing-java}
612  LispObject result = interpreter.eval("'(1 2 4 5)");
613  if (result instanceof Cons) {
614    LispObject array[] = ((Cons)result.copyToArray());
615    ...
616  }
617\end{listing-java}
618
619A more Lispy way to iterate down a list is to use the `cdr()` access
620function just as like one would traverse a list in Lisp:;
621
622\begin{listing-java}
623  LispObject result = interpreter.eval("'(1 2 4 5)");
624  while (result != Symbol.NIL) {
625    doSomething(result.car());
626    result = result.cdr();
627  }
628\end{listing-java}
629
630\section{Java Scripting API (JSR-223)}
631\label{sec:java-scripting-api}
632
633ABCL can be built with support for JSR-223~\cite{jsr-223}, which offers
634a language-agnostic API to invoke other languages from Java. The binary
635distribution download-able from ABCL's homepage is built with JSR-223
636support. If you're building ABCL from source on a pre-1.6 JVM, you need
637to have a JSR-223 implementation in your classpath (such as Apache
638Commons BSF 3.x or greater) in order to build ABCL with JSR-223 support;
639otherwise, this feature will not be built.
640
641This section describes the design decisions behind the ABCL JSR-223
642support. It is not a description of what JSR-223 is or a tutorial on
643how to use it. See
644\url{http://abcl.org/trac/browser/trunk/abcl/examples/jsr-223}
645for example usage.
646
647\subsection{Conversions}
648
649In general, ABCL's implementation of the JSR-223 API performs implicit
650conversion from Java objects to Lisp objects when invoking Lisp from
651Java, and the opposite when returning values from Java to Lisp. This
652potentially reduces coupling between user code and ABCL. To avoid such
653conversions, wrap the relevant objects in \code{JavaObject} instances.
654
655\subsection{Implemented JSR-223 interfaces}
656
657JSR-223 defines three main interfaces, of which two (\code{Invocable}
658and \code{Compilable}) are optional. ABCL implements all the three
659interfaces - \code{ScriptEngine} and the two optional ones - almost
660completely. While the JSR-223 API is not specific to a single scripting
661language, it was designed with languages with a more or less Java-like
662object model in mind: languages such as Javascript, Python, Ruby, which
663have a concept of "class" or "object" with "fields" and "methods". Lisp
664is a bit different, so certain adaptations were made, and in one case a
665method has been left unimplemented since it does not map at all to Lisp.
666
667\subsubsection{The ScriptEngine}
668
669The main interface defined by JSR-223, \code{javax.script.ScriptEngine},
670is implemented by the class
671\code{org.armedbear.lisp.scripting.AbclScriptEngine}. \code{AbclScriptEngine}
672is a singleton, reflecting the fact that ABCL is a singleton as
673well. You can obtain an instance of \code{AbclScriptEngine} using the
674\code{AbclScriptEngineFactory} or by using the service provider
675mechanism through \code{ScriptEngineManager} (refer to the
676\texttt{javax.script} documentation).
677
678\subsection{Start-up and configuration file}
679
680At start-up (i.e. when its constructor is invoked, as part of the
681static initialization phase of \code{AbclScriptEngineFactory}) the ABCL
682script engine attempts to load an "init file" from the classpath
683(\texttt{/abcl-script-config.lisp}). If present, this file can be used to
684customize the behavior of the engine, by setting a number of
685variables in the \code{ABCL-SCRIPT} package. Here is a list of the available
686variables:
687
688\begin{description}
689\item[\texttt{*use-throwing-debugger*}] controls whether ABCL uses a
690  non-standard debugging hook function to throw a Java exception
691  instead of dropping into the debugger in case of unhandled error
692  conditions.
693  \begin{itemize}
694  \item Default value: \texttt{T}
695  \item Rationale: it is more convenient for Java programmers using
696    Lisp as a scripting language to have it return exceptions to Java
697    instead of handling them in the Lisp world.
698  \item Known Issues: the non-standard debugger hook has been reported
699    to misbehave in certain circumstances, so consider disabling it if
700    it doesn't work for you.
701  \end{itemize}
702\item[\texttt{*launch-swank-at-startup*}] If true, Swank will be launched at
703  startup. See \texttt{*swank-dir*} and \texttt{*swank-port*}.
704  \begin{itemize}
705  \item Default value: \texttt{NIL}
706  \end{itemize}
707\item[\texttt{*swank-dir*}] The directory where Swank is installed. Must be set
708  if \texttt{*launch-swank-at-startup*} is true.
709\item[\texttt{*swank-port*}] The port where Swank will listen for
710  connections. Must be set if \texttt{*launch-swank-at-startup*} is
711  true.
712  \begin{itemize}
713  \item Default value: 4005
714  \end{itemize}
715\end{description}
716
717Additionally, at startup the AbclScriptEngine will \code{(require
718  'asdf)} - in fact, it uses asdf to load Swank.
719
720\subsection{Evaluation}
721
722Code is read and evaluated in the package \code{ABCL-SCRIPT-USER}. This
723packages \texttt{USE}s the \code{COMMON-LISP}, \code{JAVA} and
724\code{ABCL-SCRIPT} packages. Future versions of the script engine might
725make this default package configurable. The \code{CL:LOAD} function is
726used under the hood for evaluating code, and thus the behavior of
727\code{LOAD} is guaranteed. This allows, among other things,
728\code{IN-PACKAGE} forms to change the package in which the loaded code
729is read.
730
731It is possible to evaluate code in what JSR-223 calls a
732``ScriptContext'' (basically a flat environment of name$\rightarrow$value
733pairs). This context is used to establish special bindings for all the
734variables defined in it; since variable names are strings from Java's
735point of view, they are first interned using \code{READ-FROM-STRING} with, as
736usual, \code{ABCL-SCRIPT-USER} as the default package. Variables are declared
737special because CL's \code{LOAD}, \code{EVAL} and \code{COMPILE}
738functions work in a null lexical environment and would ignore
739non-special bindings.
740
741Contrary to what the function \code{LOAD} does, evaluation of a series
742of forms returns the value of the last form instead of T, so the
743evaluation of short scripts does the Right Thing.
744
745\subsection{Compilation}
746
747AbclScriptEngine implements the \code{javax.script.Compilable}
748interface. Currently it only supports compilation using temporary
749files. Compiled code, returned as an instance of
750\texttt{javax.script.CompiledScript}, is read, compiled and executed by
751default in the \texttt{ABCL-SCRIPT-USER} package, just like evaluated
752code.  In contrast to evaluated code, though, due to the way the ABCL
753compiler works, compiled code contains no reference to top-level
754self-evaluating objects (like numbers or strings). Thus, when evaluated,
755a piece of compiled code will return the value of the last
756non-self-evaluating form: for example the code ``\code{(do-something)
757  42}'' will return 42 when interpreted, but will return the result of
758(do-something) when compiled and later evaluated. To ensure consistency
759of behavior between interpreted and compiled code, make sure the last
760form is always a compound form - at least \code{(identity
761some-literal-object)}. Note that this issue should not matter in real
762code, where it is unlikely a top-level self-evaluating form will appear
763as the last form in a file (in fact, the Common Lisp load function
764always returns \texttt{T} upon success; with JSR-223 this policy has been changed
765to make evaluation of small code snippets work as intended).
766
767\subsection{Invocation of functions and methods}
768
769AbclScriptEngine implements the \code{javax.script.Invocable}
770interface, which allows to directly call Lisp functions and methods,
771and to obtain Lisp implementations of Java interfaces. This is only
772partially possible with Lisp since it has functions, but not methods -
773not in the traditional OO sense, at least, since Lisp methods are not
774attached to objects but belong to generic functions. Thus, the method
775\code{invokeMethod()} is not implemented and throws an
776\texttt{UnsupportedOperationException} when called. The \code{invokeFunction()}
777method should be used to call both regular and generic functions.
778
779\subsection{Implementation of Java interfaces in Lisp}
780
781ABCL can use the Java reflection-based proxy feature to implement Java
782interfaces in Lisp. It has several built-in ways to implement an
783interface, and supports definition of new ones. The
784\code{JAVA:JMAKE-PROXY} generic function is used to make such
785proxies. It has the following signature:
786
787\code{jmake-proxy interface implementation \&optional lisp-this ==> proxy}
788
789\code{interface} is a Java interface metaobject (e.g. obtained by
790invoking \code{jclass}) or a string naming a Java
791interface. \code{implementation} is the object used to implement the
792interface - several built-in methods of jmake-proxy exist for various
793types of implementations. \code{lisp-this} is an object passed to the
794closures implementing the Lisp "methods" of the interface, and
795defaults to \code{NIL}.
796
797The returned proxy is an instance of the interface, with methods
798implemented with Lisp functions.
799
800Built-in interface-implementation types include:
801
802\begin{itemize}
803\item a single Lisp function which upon invocation of any method in
804  the interface will be passed the method name, the Lisp-this object,
805  and all the parameters. Useful for interfaces with a single method,
806  or to implement custom interface-implementation strategies.
807\item a hash-map of method-name $\rightarrow$ Lisp function mappings. Function
808  signature is \code{(lisp-this \&rest args)}.
809\item a Lisp package. The name of the Java method to invoke is first
810  transformed in an idiomatic Lisp name (\code{javaMethodName} becomes
811  \code{JAVA-METHOD-NAME}) and a symbol with that name is searched in
812  the package. If it exists and is fbound, the corresponding function
813  will be called. Function signature is as the hash-table case.
814\end{itemize}
815
816This functionality is exposed by the class \code{AbclScriptEngine} via
817the two methods \code{getInterface(Class)} and
818\code{getInterface(Object, Class)}. The former returns an interface
819implemented with the current Lisp package, the latter allows the
820programmer to pass an interface-implementation object which will in turn
821be passed to the \code{jmake-proxy} generic function.
822
823\subsection{Implementation of Java classes in Lisp}
824
825See \code{JAVA:JNEW-RUNTIME-CLASS} on \ref{JAVA:JNEW-RUNTIME-CLASS}.
826
827
828\chapter{Implementation Dependent Extensions}
829
830As outlined by the CLHS ANSI conformance guidelines, we document the
831extensions to the Armed Bear Lisp implementation made accessible to
832the user by virtue of being an exported symbol in the JAVA, THREADS,
833or EXTENSIONS packages.
834
835\section{JAVA}
836
837\subsection{Modifying the JVM CLASSPATH}
838
839The \code{JAVA:ADD-TO-CLASSPATH} generic functions allows one to add the
840specified pathname or list of pathnames to the current classpath
841used by ABCL, allowing the dynamic loading of \textsc{JVM} objects:
842
843\begin{listing-lisp}
844CL-USER> (add-to-classpath "/path/to/some.jar")
845\end{listing-lisp}
846
847N.b \code{ADD-TO-CLASSPATH} only affects the classloader used by \textsc{ABCL}
848(the value of the special variable \code{JAVA:*CLASSLOADER*}. It has
849no effect on \textsc{Java} code outside \textsc{ABCL}.
850
851\subsection{Creating a synthetic Java Class at Runtime}
852
853See \code{JAVA:JNEW-RUNTIME-CLASS} on \ref{JAVA:JNEW-RUNTIME-CLASS}.
854
855% include autogen docs for the JAVA package.
856\include{java}
857
858\section{THREADS}
859
860The extensions for handling multithreaded execution are collected in
861the \code{THREADS} package.  Most of the abstractions in Doug Lea's
862excellent \code{java.util.concurrent} packages may be manipulated
863directly via the JSS contrib to great effect \cite{lea-1998}
864
865% include autogen docs for the THREADS package.
866\include{threads}
867
868\section{EXTENSIONS}
869
870The symbols in the EXTENSIONS package (nicknamed ``EXT'') constitutes
871extensions to the \textsc{ANSI} standard that are potentially useful to the
872user.  They include functions for manipulating network sockets,
873running external programs, registering object finalizers, constructing
874reference weakly held by the garbage collector and others.
875
876See \cite{RHODES2007} for a generic function interface to the native
877\textsc{JVM} contract for \code{java.util.List}.
878
879% include autogen docs for the EXTENSIONS package.
880\include{extensions}
881
882\chapter{Beyond ANSI}
883
884Naturally, in striving to be a useful contemporary Common Lisp
885implementation, ABCL endeavors to include extensions beyond the ANSI
886specification which are either widely adopted or are especially useful
887in working with the hosting \textsc{JVM}.
888
889\section{Compiler to Java 5 Bytecode}
890
891The \code{CL:COMPILE-FILE} interface emits a packed fasl format whose
892Pathname has the type  ``abcl''.  These fasls are operating system neutral
893byte archives packaged by the zip compression format which contain
894artifacts whose loading \code{CL:LOAD} understands.
895
896\section{Pathname}
897
898We implement an extension to the \code{CL:PATHNAME} that allows for
899the description and retrieval of resources named in a
900\textsc{URI} \footnote{A \textsc{URI} is essentially a superset of
901  what is commonly understood as a \textsc{URL} We sometime suse the
902  term URL as shorthand in describing the URL Pathnames, even though
903  the corresponding encoding is more akin to a URI as described in
904  RFC3986 \cite{rfc3986}.}  scheme that the \textsc{JVM}
905``understands''.  By definition, support is built-in into the JVM to
906access the ``http'' and ``https'' schemes but additional protocol
907handlers may be installed at runtime by having \textsc{JVM} symbols
908present in the \code{sun.net.protocol.dynamic} package. See
909\cite{maso2000} for more details.
910
911\textsc{ABCL} has created specializations of the ANSI
912\code{CL:PATHNAME} object to enable to use of \textsc{URI}s to address
913dynamically loaded resources for the JVM.  The \code{EXT:URL-PATHNAME}
914specialization has a corresponding \textsc{URI} whose canonical
915representation is defined to be the \code{NAMESTRING} of the
916\code{CL:PATHNAME}. The \code{EXT:JAR-PATHNAME} extension further
917specializes the the \code{EXT:URL-PATHNAME} to provide access to
918components of zip archives. 
919
920% RDF description of type hierarchy
921% TODO Render via some LaTeX mode for graphviz?
922\begin{verbatim}
923  @prefix ext:   <http://abcl.not.org/cl-packages/extensions/> .
924  @prefix cl:    <http://abcl.not.org/cl-packages/common-lisp/> .
925 
926  <ext:jar-pathname> a <ext:url-pathname>.
927  <ext:url-pathname> a <cl:pathname>.
928  <cl:logical-pathname> a <cl:pathname> .
929\end{verbatim}
930
931\label{EXTENSIONS:URL-PATHNAME}
932\index{URL-PATHNAME}
933
934\label{EXTENSIONS:JAR-PATHNAME}
935\index{JAR-PATHNAME}
936
937Both the \code{EXT:URL-PATHNAME} and \code{EXT:JAR-PATHNAME} objects
938may be used anywhere a \code{CL:PATHNAME} is accepted with the
939following caveats:
940
941\begin{itemize}
942
943\item A stream obtained via \code{CL:OPEN} on a \code{CL:URL-PATHNAME}
944  cannot be the target of write operations.
945
946\index{URI}
947\item Any results of canonicalization procedures performed on the
948  \textsc{URI} via local or network resolutions are discarded between
949  attempts (i.e. the implementation does not attempt to cache the
950  results of current name resolution of the URI for underlying
951  resource unless it is requested to be resolved.)  Upon resolution,
952  any canonicalization procedures followed in resolving the resource
953  (e.g. following redirects) are discarded.  Users may programatically
954  initiate a new, local computation of the resolution of the resource
955  by applying the \code{CL:TRUENAME} function to a
956  \code{EXT:URL-PATHNAME} object.  Depending on the reliability and
957  properties of your local \textsc{REST} infrastructure, these results
958  may not necessarily be idempotent over time\footnote {See
959    \cite{uri-pathname} for the design and implementation notes for
960    the technical details}.  A future implementation may attempt to
961  expose an API to observer/customize the content negotiation
962  initiated during retrieval of the representation of a given
963  resource, which is currently handled at the application level.
964
965\end{itemize}
966
967The implementation of \code{EXT:URL-PATHNAME} allows the \textsc{ABCL}
968user to dynamically load code from the network.  For example,
969\textsc{Quicklisp} (\cite{quicklisp}) may be completely installed from
970the \textsc{REPL} as the single form:
971
972\begin{listing-lisp}
973  CL-USER> (load "http://beta.quicklisp.org/quicklisp.lisp")
974\end{listing-lisp}
975
976will load and execute the Quicklisp setup code.
977
978The implementation currently breaks \textsc{ANSI} conformance by allowing the
979types able to be \code{CL:READ} for the \code{DEVICE} to return a possible \code{CONS} of
980\code{CL:PATHNAME} objects.  %% citation from CLHS needed.
981
982In order to ``smooth over'' the bit about types being \code{CL:READ}
983from \code{CL:PATHNAME} components, we extend the semantics for the
984usual PATHNAME merge semantics when \code{*DEFAULT-PATHNAME-DEFAULTS*}
985contains a \code{EXT:JAR-PATHNAME} with the ``do what I mean''
986algorithm described in \ref{section:conformance} on page
987\pageref{section:conformance}.
988
989%See \ref{_:quicklisp} on page \pageref{_:quicklisp}.
990
991\subsubsection{Implementation}
992
993The implementation of these extensions stores all the additional
994information in the \code{CL:PATHNAME} object itself in ways that while strictly
995speaking are conformant, nonetheless may trip up libraries that don't
996expect the following:
997
998\begin{itemize}
999\item \code{DEVICE} can be either a string denoting a drive letter
1000  under \textsc{DOS} or a list of exactly one or two elements.  If
1001  \code{DEVICE} is a list, it denotes a \code{EXT:JAR-PATHNAME}, with
1002  the entries containing \code{CL:PATHNAME} objects which describe the
1003  outer and (possibly inner) locations of the jar
1004  archive \footnote{The case of inner and outer
1005    \code{EXT:JAR-PATHNAME} \ref{EXT:JAR-PATHNAME} arises when zip
1006    archives themselves contain zip archives which is the case when
1007    the ABCL fasl is included in the abcl.jar zip archive.}.
1008
1009\item A \code{EXT:URL-PATHNAME} always has a \code{HOST} component that is a
1010  property list.  The values of the \code{HOST} property list are
1011  always character strings.  The allowed keys have the following meanings:
1012  \begin{description}
1013  \item[:SCHEME] Scheme of URI ("http", "ftp", "bundle", etc.)
1014  \item[:AUTHORITY] Valid authority according to the URI scheme.  For
1015    "http" this could be "example.org:8080".
1016  \item[:QUERY] The query of the \textsc{URI} 
1017  \item[:FRAGMENT] The fragment portion of the \textsc{URI}
1018  \end{description}
1019
1020\item In order to encapsulate the implementation decisions for these
1021  meanings, the following functions provide a SETF-able API for
1022  reading and writing such values: \code{URL-PATHNAME-QUERY},
1023  \code{URL-PATHNAME-FRAGMENT}, \code{URL-PATHNAME-AUTHORITY}, and
1024  \code{URL-PATHNAME-SCHEME}.  The specific subtype of a Pathname may
1025  be determined with the predicates \code{PATHNAME-URL-P} and
1026  \code{PATHNAME-JAR-P}.
1027
1028\label{EXTENSIONS:URL-PATHNAME-SCHEME}
1029\index{URL-PATHNAME-SCHEME}
1030
1031\label{EXTENSIONS:URL-PATHNAME-FRAGMENT}
1032\index{URL-PATHNAME-FRAGMENT}
1033
1034\label{EXTENSIONS:URL-PATHNAME-AUTHORITY}
1035\index{URL-PATHNAME-AUTHORITY}
1036
1037\label{EXTENSIONS:PATHNAME-URL-P}
1038\index{PATHNAME-URL-P}
1039
1040\label{EXTENSIONS:URL-PATHNAME-QUERY}
1041\index{URL-PATHNAME-QUERY}
1042
1043\end{itemize}
1044
1045\section{Package-Local Nicknames}
1046\label{sec:pack-local-nickn}
1047
1048ABCL allows giving packages local nicknames: they allow short and
1049easy-to-use names to be used without fear of name conflict associated
1050with normal nicknames.\footnote{Package-local nicknames were originally
1051developed in SBCL.}
1052
1053A local nickname is valid only when inside the package for which it
1054has been specified. Different packages can use same local nickname for
1055different global names, or different local nickname for same global
1056name.
1057
1058Symbol \code{:package-local-nicknames} in \code{*features*} denotes the
1059support for this feature.
1060
1061\index{DEFPACKAGE}
1062The options to \code{defpackage} are extended with a new option
1063\code{:local-nicknames (local-nickname actual-package-name)*}.
1064
1065The new package has the specified local nicknames for the corresponding
1066actual packages.
1067
1068Example:
1069\begin{listing-lisp}
1070(defpackage :bar (:intern "X"))
1071(defpackage :foo (:intern "X"))
1072(defpackage :quux (:use :cl)
1073  (:local-nicknames (:bar :foo) (:foo :bar)))
1074(find-symbol "X" :foo) ; => FOO::X
1075(find-symbol "X" :bar) ; => BAR::X
1076(let ((*package* (find-package :quux)))
1077  (find-symbol "X" :foo))               ; => BAR::X
1078(let ((*package* (find-package :quux)))
1079  (find-symbol "X" :bar))               ; => FOO::X
1080\end{listing-lisp}
1081
1082\index{PACKAGE-LOCAL-NICKNAMES}
1083--- Function: \textbf{package-local-nicknames} [\textbf{ext}] \textit{package-designator}
1084
1085\begin{adjustwidth}{5em}{5em}
1086  Returns an ALIST of \code{(local-nickname . actual-package)}
1087  describing the nicknames local to the designated package.
1088
1089  When in the designated package, calls to \code{find-package} with any
1090  of the local-nicknames will return the corresponding actual-package
1091  instead. This also affects all implied calls to \code{find-package},
1092  including those performed by the reader.
1093
1094  When printing a package prefix for a symbol with a package local
1095  nickname, the local nickname is used instead of the real name in order
1096  to preserve print-read consistency.
1097\end{adjustwidth}
1098
1099\index{PACKAGE-LOCALLY-NICKNAMED-BY-LIST}
1100--- Function: \textbf{package-locally-nicknamed-by-list} [\textbf{ext}] \textit{package-designator}
1101
1102\begin{adjustwidth}{5em}{5em}
1103Returns a list of packages which have a local nickname for the
1104designated package.
1105\end{adjustwidth}
1106
1107\index{ADD-PACKAGE-LOCAL-NICKNAME}
1108--- Function: \textbf{add-package-local-nickname} [\textbf{ext}] \textit{local-nickname actual-package \&optional package-designator}
1109
1110\begin{adjustwidth}{5em}{5em}
1111  Adds \code{local-nickname} for \code{actual-package} in the designated
1112  package, defaulting to current package. \code{local-nickname} must be
1113  a string designator, and \code{actual-package} must be a package
1114  designator.
1115
1116  Returns the designated package.
1117
1118  Signals an error if \code{local-nickname} is already a package local
1119  nickname for a different package, or if \code{local-nickname} is one
1120  of "CL", "COMMON-LISP", or, "KEYWORD", or if \code{local-nickname} is
1121  a global name or nickname for the package to which the nickname would
1122  be added.
1123
1124  When in the designated package, calls to \code{find-package} with the
1125  \code{local-nickname} will return the package the designated
1126  \code{actual-package} instead. This also affects all implied calls to
1127  \code{find-package}, including those performed by the reader.
1128
1129  When printing a package prefix for a symbol with a package local
1130  nickname, local nickname is used instead of the real name in order to
1131  preserve print-read consistency.
1132\end{adjustwidth}
1133
1134\index{REMOVE-PACKAGE-LOCAL-NICKNAME}
1135--- Function: \textbf{remove-package-local-nickname} [\textbf{ext}] \textit{old-nickname \&optional package-designator}
1136
1137\begin{adjustwidth}{5em}{5em}
1138  If the designated package had \code{old-nickname} as a local nickname
1139  for another package, it is removed. Returns true if the nickname
1140  existed and was removed, and \code{nil} otherwise.
1141\end{adjustwidth}
1142
1143
1144         
1145\section{Extensible Sequences}
1146
1147The SEQUENCE package fully implements Christopher Rhodes' proposal for
1148extensible sequences.  These user extensible sequences are used
1149directly in \code{java-collections.lisp} provide these CLOS
1150abstractions on the standard Java collection classes as defined by the
1151\code{java.util.List} contract.
1152
1153
1154%% an Example of using java.util.Lisp in Lisp would be nice
1155
1156This extension is not automatically loaded by the implementation.   It
1157may be loaded via:
1158
1159\begin{listing-lisp}
1160CL-USER> (require :java-collections)
1161\end{listing-lisp}
1162
1163if both extensible sequences and their application to Java collections
1164is required, or
1165
1166\begin{listing-lisp}
1167CL-USER> (require :extensible-sequences)
1168\end{listing-lisp}
1169
1170if only the extensible sequences API as specified in \cite{RHODES2007} is
1171required.
1172
1173Note that \code{(require :java-collections)} must be issued before
1174\code{java.util.List} or any subclass is used as a specializer in a \textsc{CLOS}
1175method definition (see the section below).
1176
1177See Rhodes2007 \cite{RHODES2007} for the an overview of the
1178abstractions of the \code{java.util.collection} package afforded by
1179\code{JAVA-COLLECTIONS}.
1180
1181\section{Extensions to CLOS}
1182
1183\subsection{Metaobject Protocol}
1184
1185\textsc{ABCL} implements the metaobject protocol for \textsc{CLOS} as
1186specified in \textsc{(A)MOP}.  The symbols are exported from the
1187package \code{MOP}.
1188
1189Contrary to the AMOP specification and following \textsc{SBCL}'s lead,
1190the metaclass \code{funcallable-standard-object} has
1191\code{funcallable-standard-class} as metaclass instead of
1192\code{standard-class}.
1193
1194\textsc{ABCL}'s fidelity to the AMOP specification is codified as part
1195of Pascal Costanza's \code{closer-mop} \ref{closer-mop} \cite{closer-mop}.
1196
1197\subsection{Specializing on Java classes}
1198
1199There is an additional syntax for specializing the parameter of a
1200generic function on a java class, viz. \code{(java:jclass CLASS-STRING)}
1201where \code{CLASS-STRING} is a string naming a Java class in dotted package
1202form.
1203
1204For instance the following specialization would perhaps allow one to
1205print more information about the contents of a \code{java.util.Collection}
1206object
1207
1208\begin{listing-lisp}
1209(defmethod print-object ((coll (java:jclass "java.util.Collection"))
1210                         stream)
1211  ;;; ...
1212)
1213\end{listing-lisp}
1214
1215If the class had been loaded via a classloader other than the original
1216the class you wish to specialize on, one needs to specify the
1217classloader as an optional third argument.
1218
1219\begin{listing-lisp}
1220
1221(defparameter *other-classloader*
1222  (jcall "getBaseLoader" cl-user::*classpath-manager*))
1223 
1224(defmethod print-object
1225   ((device-id (java:jclass "dto.nbi.service.hdm.alcatel.com.NBIDeviceID"
1226                            *other-classloader*))
1227    stream)
1228  ;;; ...
1229)
1230\end{listing-lisp}
1231
1232\section{Extensions to the Reader}
1233
1234We implement a special hexadecimal escape sequence for specifying 32
1235bit characters to the Lisp reader\footnote{This represents a
1236  compromise with contemporary in 2011 32bit hosting architecures for
1237  which we wish to make text processing efficient.  Should the User
1238  require more control over \textsc{UNICODE} processing we recommend Edi Weisz'
1239  excellent work with \textsc|{FLEXI-STREAMS}  which we fully support}, namely we
1240allow a sequences of the form \verb~#\U~\emph{\texttt{xxxx}} to be processed
1241by the reader as character whose code is specified by the hexadecimal
1242digits \emph{\texttt{xxxx}}.  The hexadecimal sequence may be one to four digits
1243long.
1244
1245% Why doesn't ALEXANDRIA work?  Good question: Alexandria from
1246% Quicklisp 2010-10-07 fails a number of tests:
1247%% Form: (ALEXANDRIA.0.DEV:TYPE= 'LIST '(OR NULL CONS))
1248%% Expected values: T
1249%%                  T
1250%% Actual values: NIL
1251%%                T.
1252%% Test ALEXANDRIA-TESTS::TYPE=.3 failed
1253%% Form: (ALEXANDRIA.0.DEV:TYPE= 'NULL '(AND SYMBOL LIST))
1254%% Expected values: T
1255%%                  T
1256%% Actual values: NIL
1257%%                NIL.
1258
1259
1260Note that this sequence is never output by the implementation.  Instead,
1261the corresponding Unicode character is output for characters whose
1262code is greater than 0x00ff.
1263
1264\section{Overloading of the CL:REQUIRE Mechanism}
1265
1266The \code{CL:REQUIRE} mechanism is overloaded by attaching these
1267semantics to the execution of \code{REQUIRE} on the following symbols:
1268
1269\begin{description}
1270
1271  \item{\code{ASDF}} 
1272    Loads the \textsc{ASDF} implementation shipped
1273    with the implementation.  After \textsc{ASDF} has been loaded in
1274    this manner, symbols passed to \code{CL:REQUIRE} which are
1275    otherwise unresolved, are passed to ASDF for a chance for
1276    resolution.  This means, for instance if \code{CL-PPCRE} can be
1277    located as a loadable \textsc{ASDF} system \code{(require
1278      :cl-ppcre)} is equivalent to \code{(asdf:load-system
1279      :cl-ppcre)}.
1280
1281  \item{\code{ABCL-CONTRIB}} 
1282    Locates and pushes the toplevel contents of
1283    ``abcl-contrib.jar'' into the \textsc{ASDF} central registry. 
1284
1285    \begin{enumerate}
1286      \item \code{abcl-asdf} 
1287        Functions for loading JVM artifacts
1288        dynamically, hooking into ASDF 3 objects where possible.
1289      \item \code{asdf-jar} 
1290        Package addressable JVM artifacts via
1291        \code{abcl-asdf} descriptions as a single binary artifact
1292        including recursive dependencies.
1293      \item \code{mvn} 
1294        These systems name common JVM artifacts from
1295        the distributed pom.xml graph of Maven Aether:
1296        \begin{enumerate}
1297          \item \code{jna} 
1298            Dynamically load 'jna.jar' version 4.2.2
1299            from the network \footnote{This loading can be inhibited
1300              if, at runtime, the Java class corresponding
1301              ``:classname'' clause of the system defition is present.}
1302        \end{enumerate}
1303      \item \code{quicklisp-abcl} Boot a local Quicklisp installation
1304        via the ASDF:IRI type introduced via ABCL-ASDF.
1305
1306      \item \code{jfli} A descendent of Rich Hickey's pre-Clojure work
1307        on the JVM.
1308      \item \code{jss} Introduces dynamic inspection of present
1309        symbols via the \code{SHARPSIGN-DOUBLE-QUOTE} macros as Java Syntax
1310        Sucks
1311      \item \code{abcl-introspect} Provides a framework for
1312        introspecting runtime Java and Lisp object values.
1313      \item \code{abcl-build} Provides a toolkit for building ABCL
1314        from source, as well as installing the necessary tools for
1315        such builds.
1316       
1317\end{enumerate}
1318
1319\end{description}
1320
1321The user may extend the \code{CL:REQUIRE} mechanism by pushing
1322function hooks into \code{SYSTEM:*MODULE-PROVIDER-FUNCTIONS*}.  Each
1323such hook function takes a single argument containing the symbol
1324passed to \code{CL:REQUIRE} and returns a non-\code{NIL} value if it
1325can successful resolve the symbol.
1326
1327\section{JSS extension of the Reader by SHARPSIGN-DOUBLE-QUOTE}
1328
1329The JSS contrib consitutes an additional, optional extension to the
1330reader in the definition of the \code{SHARPSIGN-DOUBLE-QUOTE}
1331(``\#\"'') reader macro.  See section \ref{section:jss} on page
1332\pageref{section:jss} for more information.
1333
1334\section{ASDF}
1335
1336asdf-3.2.1 (see \cite{asdf}) is packaged as core component of \textsc{ABCL},
1337but not initialized by default, as it relies on the \textsc{CLOS} subsystem
1338which can take a bit of time to start \footnote{While this time is
1339  ``merely'' on the order of seconds for contemporary 2011 machines,
1340  for applications that need to initialize quickly, for example a web
1341  server, this time might be unnecessarily long}.  The packaged \textsc{ASDF}
1342may be loaded by the \textsc{ANSI} \code{REQUIRE} mechanism as
1343follows:
1344
1345\begin{listing-lisp}
1346CL-USER> (require :asdf)
1347\end{listing-lisp}
1348
1349\chapter{Contrib}
1350
1351The \textsc{ABCL} contrib is packaged as a separate jar archive usually named
1352\code{abcl-contrib.jar} or possibly something like
1353\code{abcl-contrib-1.5.0.jar}.  The contrib jar is not loaded by the
1354implementation by default, and must be first intialized by the
1355\code{REQUIRE} mechanism before using any specific contrib:
1356
1357\begin{listing-lisp}
1358CL-USER> (require :abcl-contrib)
1359\end{listing-lisp}
1360
1361\section{abcl-asdf}
1362
1363This contrib enables an additional syntax for \textsc{ASDF} system
1364definition which dynamically loads \textsc{JVM} artifacts such as jar
1365archives via encapsulation of the Maven build tool.  The Maven Aether
1366component can also be directly manipulated by the function associated
1367with the \code{ABCL-ASDF:RESOLVE-DEPENDENCIES} symbol.
1368
1369%ABCL specific contributions to ASDF system definition mainly
1370%concerned with finding JVM artifacts such as jar archives to be
1371%dynamically loaded.
1372
1373
1374When loaded, abcl-asdf adds the following objects to \textsc{ASDF}:
1375\code{JAR-FILE}, \code{JAR-DIRECTORY}, \code{CLASS-FILE-DIRECTORY} and
1376\code{MVN}, exporting them (and others) as public symbols.
1377
1378\subsection{Referencing Maven Artifacts via ASDF}
1379
1380Maven artifacts may be referenced within \textsc{ASDF} system
1381definitions, as the following example references the
1382\code{log4j-1.4.9.jar} JVM artifact which provides a widely-used
1383abstraction for handling logging systems:
1384
1385\begin{listing-lisp}
1386  ;;;; -*- Mode: LISP -*-
1387  (require :asdf)
1388  (in-package :cl-user)
1389
1390  (asdf:defsystem :log4j
1391     :defsystem-depends-on (abcl-asdf)
1392     :components ((:mvn "log4j/log4j" :version "1.4.9")))
1393\end{listing-lisp}
1394
1395\subsection{API}
1396
1397We define an API for \textsc{ABCL-ASDF} as consisting of the following
1398ASDF classes:
1399
1400\code{JAR-DIRECTORY}, \code{JAR-FILE}, and
1401\code{CLASS-FILE-DIRECTORY} for JVM artifacts that have a currently
1402valid pathname representation.
1403
1404Both the \code{MVN} and \code{IRI} classes descend from
1405\code{ASDF-COMPONENT}, but do not directly have a filesystem location.
1406
1407For use outside of ASDF system definitions, we currently define one
1408method, \code{ABCL-ASDF:RESOLVE-DEPENDENCIES} which locates,
1409downloads, caches, and then loads into the currently executing JVM
1410process all recursive dependencies annotated in the Maven pom.xml
1411graph.
1412
1413\subsection{Directly Instructing Maven to Download JVM Artifacts}
1414
1415Bypassing \textsc{ASDF}, one can directly issue requests for the Maven
1416artifacts to be downloaded
1417
1418\begin{listing-lisp}
1419CL-USER> (abcl-asdf:resolve-dependencies "com.google.gwt"
1420                                         "gwt-user")
1421WARNING: Using LATEST for unspecified version.
1422"/Users/evenson/.m2/repository/com/google/gwt/gwt-user/2.4.0-rc1
1423/gwt-user-2.4.0-rc1.jar:/Users/evenson/.m2/repository/javax/vali
1424dation/validation-api/1.0.0.GA/validation-api-1.0.0.GA.jar:/User
1425s/evenson/.m2/repository/javax/validation/validation-api/1.0.0.G
1426A/validation-api-1.0.0.GA-sources.jar"
1427\end{listing-lisp}
1428
1429To actually load the dependency, use the \code{JAVA:ADD-TO-CLASSPATH} generic
1430function:
1431
1432\begin{listing-lisp}
1433CL-USER> (java:add-to-classpath
1434          (abcl-asdf:resolve-dependencies "com.google.gwt"
1435                                          "gwt-user"))
1436\end{listing-lisp}
1437
1438Notice that all recursive dependencies have been located and installed
1439locally from the network as well.
1440
1441More extensive documentations and examples can be found at
1442\url{http://abcl.org/svn/tags/1.5.0/contrib/abcl-asdf/README.markdown}.
1443
1444\section{asdf-jar}
1445
1446The asdf-jar contrib provides a system for packaging \textsc{ASDF}
1447systems into jar archives for \textsc{ABCL}.  Given a running
1448\textsc{ABCL} image with loadable \textsc{ASDF} systems the code in
1449this package will recursively package all the required source and
1450fasls in a jar archive.
1451
1452The documentation for this contrib can be found at
1453\url{http://abcl.org/svn/tags/1.5.0/contrib/asdf-jar/README.markdown}.
1454
1455\section{jss}
1456\label{section:jss}
1457
1458To one used to the more universal syntax of Lisp pairs upon which the
1459definition of read and compile time macros is quite
1460natural \footnote{See Graham's ``On Lisp''
1461  http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf.}, the Java
1462syntax available to the Java programmer may be said to suck.  To
1463alleviate this situation, the JSS contrib introduces the
1464\code{SHARPSIGN-DOUBLE-QUOTE} (\code{\#"}) reader macro, which allows
1465the the specification of the name of invoking function as the first
1466element of the relevant s-expr which tends to be more congruent to how
1467Lisp programmers seem to be wired to think.
1468
1469While quite useful, we don't expect that the JSS contrib will be the
1470last experiment in wrangling Java from Common Lisp.
1471
1472\subsection{JSS usage}
1473
1474Example:
1475
1476\begin{listing-lisp}
1477CL-USER> (require :abcl-contrib)
1478==> ("ABCL-CONTRIB")
1479CL-USER> (require :jss)
1480==> ("JSS")
1481CL-USER) (#"getProperties" 'java.lang.System)
1482==> #<java.util.Properties {java.runtime.name=Java.... {2FA21ACF}>
1483CL-USER) (#"propertyNames" (#"getProperties" 'java.lang.System))
1484==> #<java.util.Hashtable$Enumerator java.util.Has.... {36B4361A}>
1485\end{listing-lisp} %$ <-- un-confuse Emacs font-lock
1486
1487Some more information on jss can be found in its documentation at
1488\url{http://abcl.org/svn/tags/1.5.0/contrib/jss/README.markdown}
1489
1490\section{jfli}
1491\label{section:jfli}
1492
1493The contrib contains a pure-Java version of JFLI.
1494
1495\url{http://abcl.org/svn/tags/1.5.0/contrib/jfli/README}.
1496
1497\section{abcl-introspect}
1498
1499\textsc{ABCL-INTROSPECT} offers more extensive systems for inspecting the state
1500of the implementation, most notably in integration with SLIME, where
1501the backtrace mechanism is augmented to the poiont that local
1502variables are inspectable.
1503
1504A compiled function is an instance of a class - This class has
1505multiple instances if it represents a closure, or a single instance if
1506it represents a non-closed-over function.
1507
1508The \textsc{ABCL} compiler stores constants that are used in function
1509execution as private java fields. This includes symbols used to invoke
1510function, locally-defined functions (such as via \code{LABEL} or
1511\code{flet}) and string and other literal objects.
1512\textsc{ABCL-INTROSPECT} implements a ``do what I mean'' API for
1513introspecting these constants.
1514
1515ABCL-INTROSPECT provides access to those internal values, and
1516uses them in at least two ways. First, to annotate locally defined
1517functions with the top-level function they are defined within, and
1518second to search for callers of a give function \footnote{ Since java
1519  functions are strings, local fields also have these strings. In the
1520  context of looking for callers of a function you can also give a
1521  string that names a java method. Same caveat re: false positives.}
1522. This may yield some false positives, such as when a symbol that
1523names a function is also used for some other purpose. It can also have
1524false negatives, as when a function is inlined. Still, it's pretty
1525useful. The second use to to find source locations for frames in the
1526debugger. If the source location for a local function is asked for the
1527location of its 'owner' is instead returns.
1528
1529In order to record information about local functions, ABCL defines a
1530function-plist, which is for the most part unused, but is used here
1531with set of keys indicating where the local function was defined and
1532in what manner, i.e. as normal local function, as a method function,
1533or as an initarg function. There may be other places functions are
1534stashed away (defstructs come to mind) and this file should be added
1535to to take them into account as they are discovered.
1536
1537ABCL-INTROSPECT does not depend on jss, but provides a bit of
1538jss-specific functionality if jss *is* loaded.
1539
1540\url{http://abcl.org/svn/tags/1.5.0/contrib/abcl-introspect/}.
1541
1542\section{abcl-build}
1543
1544\textsc{ABCL-BUILD} constitutes a new implementation for the original
1545Lisp-hosted ABCL build system API in the package \code{ABCL-BUILD}
1546that uses the same build artifacts as all of the other current builds.
1547
1548\subsection{Utilities}
1549
1550\textsc{ABCL-BUILD} consolidates various utilities that are useful
1551for system construction, namely
1552\begin{itemize}
1553
1554\item The ability to introspect the invocation of given executable in
1555  the current implementation process PATH.
1556
1557\item Downloading and unpackages selected JVM artifacts, namely the
1558  Ant and Maven build tools.  The \code{ABCL-BUILD:WITH-ANT} and
1559  \code{ABCL-BUILD:WITH-MVN} macros abstracts this installation
1560  procedure conveniently away from the User.
1561
1562\item The beginings of a generic framework to download arbitrary
1563    archives from the network.
1564\end{itemize}
1565
1566\url{http://abcl.org/svn/tags/1.5.0/contrib/abcl-build/}.
1567
1568\section{named-readtables}
1569
1570\code{NAMED-READTABLES} is a library that provides a namespace for
1571readtables akin to the already-existing namespace of packages.
1572
1573\url{https://github.com/melisgl/named-readtables/}.
1574\url{http://abcl.org/svn/tags/1.5.0/contrib/named-readtables/}.
1575
1576\chapter{History}
1577\index{History}
1578
1579\textsc{ABCL} was originally the extension language for the J editor, which was
1580started in 1998 by Peter Graves.  Sometime in 2003, a whole lot of
1581code that had previously not been released publically was suddenly
1582committed that enabled ABCL to be plausibly termed an emergent ANSI
1583Common Lisp implementation candidate.
1584
1585From 2006 to 2008, Peter manned the development lists, incorporating
1586patches as made sense.  After a suitable search, Peter nominated Erik
1587H\"{u}lsmann to take over the project.
1588
1589In 2008, the implementation was transferred to the current
1590maintainers, who have strived to improve its usability as a
1591contemporary Common Lisp implementation.
1592
1593On October 22, 2011, with the publication of this Manual explicitly
1594stating the conformance of Armed Bear Common Lisp to \textsc{ANSI}, we
1595released abcl-1.0.0.  We released abcl-1.0.1 as a maintainence release
1596on January 10, 2012.
1597
1598In December 2012, we revised the implementation by adding
1599\textsc{(A)MOP} with the release of abcl-1.1.0.  We released
1600abcl-1.1.1 as a maintainence release on Feburary 14, 2013.
1601
1602At the beginning of June 2013, we enhanced the stability of the
1603implementation with the release of abcl-1.2.1.
1604
1605In March 2014, we introduced the Fourth Edition of the implementation
1606with abcl-1.3.0.  At the end of April 2014, we released abcl-1.3.1 as
1607a maintainence release.
1608
1609In October 2016 we blessed the current \textsc{svn} trunk
1610\url{http://abcl.org/svn/trunk/} as 1.4.0, which includes the
1611community contributions from Vihbu, Olof, Pipping, and Cyrus.  We
1612gingerly stepped into current century by establishing \textsc{git}
1613bridges to the source repository at
1614\url{https://github.com/easye/abcl/} and
1615\url{https://gitlab.common-lisp.net/abcl/abcl/} so that pull
1616requests for enhancements to the implementation many be more easily
1617facilitated.
1618
1619In June 2017, we released the Sixth Edition of the implementation
1620which dropped support for running on Java 5.
1621
1622\appendix 
1623
1624\chapter{The MOP Dictionary}
1625
1626\include{mop}
1627
1628\chapter{The SYSTEM Dictionary}
1629
1630The public interfaces in this package are subject to change with
1631\textsc{ABCL} 1.6.
1632
1633\include{system}
1634
1635\chapter{The JSS Dictionary}
1636
1637These public interfaces are provided by the JSS contrib.
1638
1639\include{jss}
1640
1641\bibliography{abcl}
1642\bibliographystyle{alpha}
1643
1644\printindex
1645
1646\end{document}
Note: See TracBrowser for help on using the repository browser.