Commit 8c6f5ec5 authored by astalla@1c010e3e-69d0-11dd-93a8-456734b0d56f's avatar astalla@1c010e3e-69d0-11dd-93a8-456734b0d56f
Browse files

Manual: documented the JSR-223 implementation.

parent 6e3c85a6
......@@ -443,7 +443,7 @@ iteration.
...
}
\end{listing-java}
A more Lispy way to iterated down a list is to use the `cdr()` access
function just as like one would traverse a list in Lisp:;
......@@ -455,6 +455,89 @@ function just as like one would traverse a list in Lisp:;
}
\end{listing-java}
\subsection{Java Scripting API (JSR-223)}
ABCL can be built with support for JSR-223, which offers a language-agnostic
API to invoke other languages from Java. The binary distribution downloadable
from ABCL's common-lisp.net home is built with JSR-223 support. If you're building
ABCL from source on a pre-1.6 JVM, you need to have a JSR-223 implementation in your
CLASSPATH (such as Apache Commons BSF 3.x or greater) in order to build ABCL
with JSR-223 support; otherwise, this feature will not be built.
This section describes the design decisions behind the ABCL JSR-223 support. It is not a description of what JSR-223 is or a tutorial on how to use it. See http://trac.common-lisp.net/armedbear/browser/trunk/abcl/examples/jsr-223 for example usage.
\subsubsection{Conversions}
In general, ABCL's implementation of the JSR-223 API performs implicit conversion from Java objects to Lisp objects when invoking Lisp from Java, and the opposite when returning values from Java to Lisp. This potentially reduces coupling between user code and ABCL. To avoid such conversions, wrap the relevant objects in \code{JavaObject} instances.
\subsubsection{Implemented JSR-223 interfaces}
JSR-223 defines three main interfaces, of which two (Invocable and Compilable) are optional. ABCL implements all the three interfaces - ScriptEngine and the two optional ones - almost completely. While the JSR-223 API is not specific to a single scripting language, it was designed with languages with a more or less Java-like object model in mind: languages such as Javascript, Python, Ruby, which have a concept of "class" or "object" with "fields" and "methods". Lisp is a bit different, so certain adaptations were made, and in one case a method has been left unimplemented since it does not map at all to Lisp.
\subsubsection{The ScriptEngine}
The main interface defined by JSR-223, javax.script.ScriptEngine, is implemented by the class \code{org.armedbear.lisp.scripting.AbclScriptEngine}. AbclScriptEngine is a singleton, reflecting the fact that ABCL is a singleton as well. You can obtain an instance of AbclScriptEngine using the AbclScriptEngineFactory or by using the service provider mechanism through ScriptEngineManager (refer to the javax.script documentation).
\subsubsection{Startup and configuration file}
At startup (i.e. when its constructor is invoked, as part of the static initialization phase of AbclScriptEngineFactory) the ABCL script engine attempts to load an "init file" from the classpath (/abcl-script-config.lisp). If present, this file can be used to customize the behaviour of the engine, by setting a number of variables in the ABCL-SCRIPT package. Here is a list of the available variables:
\begin{itemize}
\item *use-throwing-debugger* Controls whether ABCL uses a non-standard debugging hook function to throw a Java exception instead of dropping into the debugger in case of unhandled error conditions.
\begin{itemize}
\item Default value: T
\item Rationale: it is more convenient for Java programmers using Lisp as a scripting language to have it return exceptions to Java instead of handling them in the Lisp world.
\item Known Issues: the non-standard debugger hook has been reported to misbehave in certain circumstances, so consider disabling it if it doesn't work for you.
\end{itemize}
\item *launch-swank-at-startup* If true, Swank will be launched at startup. See *swank-dir* and *swank-port*.
\begin{itemize}
\item Default value: NIL
\end{itemize}
\item *swank-dir* The directory where Swank is installed. Must be set if *launch-swank-at-startup* is true.
\item *swank-port* The port where Swank will listen for connections. Must be set if *launch-swank-at-startup* is true.
\begin{itemize}
\item Default value: 4005
\end{itemize}
\end{itemize}
Additionally, at startup the AbclScriptEngine will \code{(require 'asdf)} - in fact, it uses asdf to load Swank.
\subsubsection{Evaluation}
Code is read and evaluated in the package ABCL-SCRIPT-USER. This packages USEs the COMMON-LISP, JAVA and ABCL-SCRIPT packages. Future versions of the script engine might make this default package configurable. The \code{CL:LOAD} function is used under the hood for evaluating code, and thus the same behavior of LOAD is guaranteed. This allows, among other things, \code{IN-PACKAGE} forms to change the package in which the loaded code is read.
It is possible to evaluate code in what JSR-223 calls a "ScriptContext" (basically a flat environment of name->value pairs). This context is used to establish special bindings for all the variables defined in it; since variable names are strings from Java's point of view, they are first interned using READ-FROM-STRING with, as usual, ABCL-SCRIPT-USER as the default package. Variables are declared special because CL's \code{LOAD}, \code{EVAL} and \code{COMPILE} functions work in a null lexical environment and would ignore non-special bindings.
Contrary to what the function \code{LOAD} does, evaluation of a series of forms returns the value of the last form instead of T, so the evaluation of short scripts does the Right Thing.
\subsubsection{Compilation}
AbclScriptEngine implements the javax.script.Compilable interface. Currently it only supports compilation using temporary files. Compiled code, returned as an instance of javax.script.CompiledScript, is read, compiled and executed by default in the ABCL-SCRIPT-USER package, just like evaluated code. Differently from evaluated code, though, due to the way the ABCL compiler works, compiled code contains no reference to top-level self-evaluating objects (like numbers or strings). Thus, when evaluated, a piece of compiled code will return the value of the last non-self-evaluating form: for example the code "(do-something) 42" will return 42 when interpreted, but will return the result of (do-something) when compiled and later evaluated. To ensure consistency of behavior between interpreted and compiled code, make sure the last form is always a compound form - at least (identity some-literal-object). Note that this issue should not matter in real code, where it is unlikely a top-level self-evaluating form will appear as the last form in a file (in fact, the Common Lisp load function always returns T upon success; with JSR-223 this policy has been changed to make evaluation of small code snippets work as intended).
\subsubsection{Invocation of functions and methods}
AbclScriptEngine implements the \code{javax.script.Invocable} interface, which allows to directly call Lisp functions and methods, and to obtain Lisp implementations of Java interfaces. This is only partially possible with Lisp since it has functions, but not methods - not in the traditional OO sense, at least, since Lisp methods are not attached to objects but belong to generic functions. Thus, the method \code{invokeMethod()} is not implemented and throws an UnsupportedOperationException when called. The \code{invokeFunction()} method should be used to call both regular and generic functions.
\subsubsection{Implementation of Java interfaces in Lisp}
ABCL can use the Java reflection-based proxy feature to implement Java interfaces in Lisp. It has several built-in ways to implement an interface, and supports definition of new ones. The \code{JAVA:JMAKE-PROXY} generic function is used to make such proxies. It has the following signature:
\code{jmake-proxy interface implementation \&optional lisp-this ==> proxy}
\code{interface} is a Java interface metaobject (e.g. obtained by invoking \code{jclass}) or a string naming a Java interface. \code{implementation} is the object used to implement the interface - several built-in methods of jmake-proxy exist for various types of implementations. \code{lisp-this} is an object passed to the closures implementing the Lisp "methods" of the interface, and defaults to \code{NIL}.
The returned proxy is an instance of the interface, with methods implemented with Lisp functions.
Built-in interface-implementation types include:
\begin{itemize}
\item a single Lisp function which upon invocation of any method in the interface will be passed the method name, the Lisp-this object, and all the parameters. Useful for interfaces with a single method, or to implement custom interface-implementation strategies.
\item a hash-map of method-name -> Lisp function mappings. Function signature is \code{(lisp-this \&rest args)}.
\item a Lisp package. The name of the Java method to invoke is first transformed in an idiomatic Lisp name (\code{javaMethodName} becomes \code{JAVA-METHOD-NAME}) and a symbol with that name is searched in the package. If it exists and is fbound, the corresponding function will be called. Function signature is as the hash-table case.
\end{itemize}
This functionality is exposed by the AbclScriptEngine with the two methods getInterface(Class) and getInterface(Object, Class). The former returns an interface implemented with the current Lisp package, the latter allows the programmer to pass an interface-implementation object which will in turn be passed to the jmake-proxy generic function.
\chapter{Implementation Dependent Extensions}
As outlined by the CLHS ANSI conformance guidelines, we document the
......@@ -467,13 +550,17 @@ or EXTENSIONS packages.
\subsection{Modifying the JVM CLASSPATH}
The JAVA:ADD-TO-CLASSPATH generic functions allows one to add the
specified pathname or list of pathnames to the current JVM classpath
allowing the dynamic loading of JVM objects:
specified pathname or list of pathnames to the current classpath
used by ABCL, allowing the dynamic loading of JVM objects:
\begin{listing-lisp}
CL-USER> (add-to-classpath "/path/to/some.jar")
\end{listing-lisp}
NB \code{add-to-classpath} only affects the classloader used by ABCL
(the value of the special variable \code{JAVA:*CLASSLOADER*}. It has
no effect on Java code outside ABCL.
\subsection{API}
% include autogen docs for the JAVA package.
......@@ -569,10 +656,24 @@ abstractions on the standard Java collection classes as defined by the
This extension is not automatically loaded by the implementation. It
may be loaded via:
\begin{listing-lisp}
CL-USER> (require 'java-collections)
\end{listing-lisp}
if both extensible sequences and their application to Java collections
is required, or
\begin{listing-lisp}
CL-USER> (require 'extensible-sequences)
\end{listing-lisp}
if only the extensible sequences API as specified in \ref{RHODES2007} is
required.
Note that \code{(require 'java-collections)} must be issued before
\code{java.util.List} or any subclass is used as a specializer in a CLOS
method definition (see the section below).
\section{Extensions to CLOS}
There is an additional syntax for specializing the parameter of a
......@@ -631,7 +732,7 @@ CL-USER> (require 'asdf)
\chapter{Contrib}
\section{abcl-asdf}
\section{abcl-asdf}
Allow ASDF system definition which dynamically loads JVM artifacts
such as jar archives via a Maven encapsulation.
......@@ -661,7 +762,7 @@ ABCL was originally the extension language for the J editor, which was
started in 1998 by Peter Graves. Sometime in 2003, it seems that a
lot of code that had previously not been released publically was
suddenly committed that enabled ABCL to be plausibly termed an ANSI
Common Lisp implementation.
Common Lisp implementation.
In 2006, the implementation was transferred to the current
maintainers, who have strived to improve its usability as a
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment