Changeset 13634
- Timestamp:
- 10/20/11 08:29:05 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abcl/doc/manual/abcl.tex
r13628 r13634 6 6 \begin{document} 7 7 \title{A Manual for Armed Bear Common Lisp} 8 \date{October 13, 2011}8 \date{October 20, 2011} 9 9 \author{Mark~Evenson, Erik~Huelsmann, Alessio~Stalla, Ville~Voutilainen} 10 10 … … 27 27 executed under the control of a suitable JVM by using the ``-jar'' 28 28 option to parse the manifest, and select the named class 29 (\code{org.armedbear.lisp.Main}) for ex cution:29 (\code{org.armedbear.lisp.Main}) for execution: 30 30 31 31 \begin{listing-shell} … … 141 141 by their name ending with \code{-RAW}. 142 142 143 \subsection{Low level Java API}143 \subsection{Low-level Java API} 144 144 145 145 There's a higher level Java API defined in the … … 154 154 155 155 \begin{itemize} 156 \item Call a specific method reference ( pre-acquired)156 \item Call a specific method reference (which was previously acquired) 157 157 \item Dynamic dispatch using the method name and 158 158 the call-specific arguments provided by finding the … … 236 236 that is used first by \code{JAVA:JCALL} and similar to resolve methods; 237 237 the actual class of the object is only tried if the method is not found 238 in the intended class. Of course, the intended class is always a super class238 in the intended class. Of course, the intended class is always a super-class 239 239 of the actual class - in the worst case, they coincide. The intended class 240 240 is deduced by the return type of the method that originally returned … … 393 393 becomes considerably simpler. If we can locate the instance of 394 394 definition in the ABCL Java source, we can invoke the symbol directly. 395 For inst nace, to tell if a `LispObject` contains a reference to a symbol.395 For instance, to tell if a `LispObject` contains a reference to a symbol. 396 396 397 397 \begin{listing-java} 398 398 boolean nullp(LispObject object) { 399 399 LispObject result = Primitives.NULL.execute(object); 400 if (result == NIL) { // the symbol 'NIL' is explicit y named in the Java400 if (result == NIL) { // the symbol 'NIL' is explicitly named in the Java 401 401 // namespace at ``Symbol.NIL'' 402 402 // but is always present in the 403 // local namespace in its unadorned form for403 // local namespace in its unadorned form for 404 404 // the convenience of the User. 405 405 return false; … … 462 462 \subsection{Java Scripting API (JSR-223)} 463 463 464 ABCL can be built with support for JSR-223, which offers a language-agnostic 465 API to invoke other languages from Java. The binary distribution downloadable 466 from ABCL's common-lisp.net home is built with JSR-223 support. If you're building 467 ABCL from source on a pre-1.6 JVM, you need to have a JSR-223 implementation in your 468 CLASSPATH (such as Apache Commons BSF 3.x or greater) in order to build ABCL 469 with JSR-223 support; otherwise, this feature will not be built. 470 471 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. 464 ABCL can be built with support for JSR-223, which offers a 465 language-agnostic API to invoke other languages from Java. The binary 466 distribution downloadable from ABCL's common-lisp.net home is built 467 with JSR-223 support. If you're building ABCL from source on a pre-1.6 468 JVM, you need to have a JSR-223 implementation in your CLASSPATH (such 469 as Apache Commons BSF 3.x or greater) in order to build ABCL with 470 JSR-223 support; otherwise, this feature will not be built. 471 472 This section describes the design decisions behind the ABCL JSR-223 473 support. It is not a description of what JSR-223 is or a tutorial on 474 how to use it. See 475 http://trac.common-lisp.net/armedbear/browser/trunk/abcl/examples/jsr-223 476 for example usage. 472 477 473 478 \subsubsection{Conversions} 474 479 475 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. 480 In general, ABCL's implementation of the JSR-223 API performs implicit 481 conversion from Java objects to Lisp objects when invoking Lisp from 482 Java, and the opposite when returning values from Java to Lisp. This 483 potentially reduces coupling between user code and ABCL. To avoid such 484 conversions, wrap the relevant objects in \code{JavaObject} instances. 476 485 477 486 \subsubsection{Implemented JSR-223 interfaces} 478 487 479 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. 488 JSR-223 defines three main interfaces, of which two (Invocable and 489 Compilable) are optional. ABCL implements all the three interfaces - 490 ScriptEngine and the two optional ones - almost completely. While the 491 JSR-223 API is not specific to a single scripting language, it was 492 designed with languages with a more or less Java-like object model in 493 mind: languages such as Javascript, Python, Ruby, which have a concept 494 of "class" or "object" with "fields" and "methods". Lisp is a bit 495 different, so certain adaptations were made, and in one case a method 496 has been left unimplemented since it does not map at all to Lisp. 480 497 481 498 \subsubsection{The ScriptEngine} 482 499 483 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). 500 The main interface defined by JSR-223, javax.script.ScriptEngine, is 501 implemented by the class 502 \code{org.armedbear.lisp.scripting.AbclScriptEngine}. AbclScriptEngine 503 is a singleton, reflecting the fact that ABCL is a singleton as 504 well. You can obtain an instance of AbclScriptEngine using the 505 AbclScriptEngineFactory or by using the service provider mechanism 506 through ScriptEngineManager (refer to the javax.script documentation). 484 507 485 508 \subsubsection{Startup and configuration file} 486 509 487 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: 510 At startup (i.e. when its constructor is invoked, as part of the 511 static initialization phase of AbclScriptEngineFactory) the ABCL 512 script engine attempts to load an "init file" from the classpath 513 (/abcl-script-config.lisp). If present, this file can be used to 514 customize the behaviour of the engine, by setting a number of 515 variables in the ABCL-SCRIPT package. Here is a list of the available 516 variables: 488 517 489 518 \begin{itemize} 490 \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. 519 \item *use-throwing-debugger* Controls whether ABCL uses a 520 non-standard debugging hook function to throw a Java exception 521 instead of dropping into the debugger in case of unhandled error 522 conditions. 491 523 \begin{itemize} 492 524 \item Default value: T 493 \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. 494 \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. 525 \item Rationale: it is more convenient for Java programmers using 526 Lisp as a scripting language to have it return exceptions to Java 527 instead of handling them in the Lisp world. 528 \item Known Issues: the non-standard debugger hook has been reported 529 to misbehave in certain circumstances, so consider disabling it if 530 it doesn't work for you. 495 531 \end{itemize} 496 \item *launch-swank-at-startup* If true, Swank will be launched at startup. See *swank-dir* and *swank-port*. 532 \item *launch-swank-at-startup* If true, Swank will be launched at 533 startup. See *swank-dir* and *swank-port*. 497 534 \begin{itemize} 498 535 \item Default value: NIL 499 536 \end{itemize} 500 \item *swank-dir* The directory where Swank is installed. Must be set if *launch-swank-at-startup* is true. 501 \item *swank-port* The port where Swank will listen for connections. Must be set if *launch-swank-at-startup* is true. 537 \item *swank-dir* The directory where Swank is installed. Must be set 538 if *launch-swank-at-startup* is true. 539 \item *swank-port* The port where Swank will listen for 540 connections. Must be set if *launch-swank-at-startup* is true. 502 541 \begin{itemize} 503 542 \item Default value: 4005 … … 505 544 \end{itemize} 506 545 507 Additionally, at startup the AbclScriptEngine will \code{(require 'asdf)} - in fact, it uses asdf to load Swank. 546 Additionally, at startup the AbclScriptEngine will \code{(require 547 'asdf)} - in fact, it uses asdf to load Swank. 508 548 509 549 \subsubsection{Evaluation} 510 550 511 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. 512 513 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. 514 515 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. 551 Code is read and evaluated in the package ABCL-SCRIPT-USER. This 552 packages USEs the COMMON-LISP, JAVA and ABCL-SCRIPT packages. Future 553 versions of the script engine might make this default package 554 configurable. The \code{CL:LOAD} function is used under the hood for 555 evaluating code, and thus the same behavior of LOAD is 556 guaranteed. This allows, among other things, \code{IN-PACKAGE} forms 557 to change the package in which the loaded code is read. 558 559 It is possible to evaluate code in what JSR-223 calls a 560 "ScriptContext" (basically a flat environment of name->value 561 pairs). This context is used to establish special bindings for all the 562 variables defined in it; since variable names are strings from Java's 563 point of view, they are first interned using READ-FROM-STRING with, as 564 usual, ABCL-SCRIPT-USER as the default package. Variables are declared 565 special because CL's \code{LOAD}, \code{EVAL} and \code{COMPILE} 566 functions work in a null lexical environment and would ignore 567 non-special bindings. 568 569 Contrary to what the function \code{LOAD} does, evaluation of a series 570 of forms returns the value of the last form instead of T, so the 571 evaluation of short scripts does the Right Thing. 516 572 517 573 \subsubsection{Compilation} 518 574 519 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). 575 AbclScriptEngine implements the javax.script.Compilable 576 interface. Currently it only supports compilation using temporary 577 files. Compiled code, returned as an instance of 578 javax.script.CompiledScript, is read, compiled and executed by default 579 in the ABCL-SCRIPT-USER package, just like evaluated code. Differently 580 from evaluated code, though, due to the way the ABCL compiler works, 581 compiled code contains no reference to top-level self-evaluating 582 objects (like numbers or strings). Thus, when evaluated, a piece of 583 compiled code will return the value of the last non-self-evaluating 584 form: for example the code "(do-something) 42" will return 42 when 585 interpreted, but will return the result of (do-something) when 586 compiled and later evaluated. To ensure consistency of behavior 587 between interpreted and compiled code, make sure the last form is 588 always a compound form - at least (identity some-literal-object). Note 589 that this issue should not matter in real code, where it is unlikely a 590 top-level self-evaluating form will appear as the last form in a file 591 (in fact, the Common Lisp load function always returns T upon success; 592 with JSR-223 this policy has been changed to make evaluation of small 593 code snippets work as intended). 520 594 521 595 \subsubsection{Invocation of functions and methods} 522 596 523 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. 597 AbclScriptEngine implements the \code{javax.script.Invocable} 598 interface, which allows to directly call Lisp functions and methods, 599 and to obtain Lisp implementations of Java interfaces. This is only 600 partially possible with Lisp since it has functions, but not methods - 601 not in the traditional OO sense, at least, since Lisp methods are not 602 attached to objects but belong to generic functions. Thus, the method 603 \code{invokeMethod()} is not implemented and throws an 604 UnsupportedOperationException when called. The \code{invokeFunction()} 605 method should be used to call both regular and generic functions. 524 606 525 607 \subsubsection{Implementation of Java interfaces in Lisp} 526 608 527 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: 609 ABCL can use the Java reflection-based proxy feature to implement Java 610 interfaces in Lisp. It has several built-in ways to implement an 611 interface, and supports definition of new ones. The 612 \code{JAVA:JMAKE-PROXY} generic function is used to make such 613 proxies. It has the following signature: 528 614 529 615 \code{jmake-proxy interface implementation \&optional lisp-this ==> proxy} 530 616 531 \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}. 532 533 The returned proxy is an instance of the interface, with methods implemented with Lisp functions. 617 \code{interface} is a Java interface metaobject (e.g. obtained by 618 invoking \code{jclass}) or a string naming a Java 619 interface. \code{implementation} is the object used to implement the 620 interface - several built-in methods of jmake-proxy exist for various 621 types of implementations. \code{lisp-this} is an object passed to the 622 closures implementing the Lisp "methods" of the interface, and 623 defaults to \code{NIL}. 624 625 The returned proxy is an instance of the interface, with methods 626 implemented with Lisp functions. 534 627 535 628 Built-in interface-implementation types include: 536 629 537 630 \begin{itemize} 538 \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. 539 \item a hash-map of method-name -> Lisp function mappings. Function signature is \code{(lisp-this \&rest args)}. 540 \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. 631 \item a single Lisp function which upon invocation of any method in 632 the interface will be passed the method name, the Lisp-this object, 633 and all the parameters. Useful for interfaces with a single method, 634 or to implement custom interface-implementation strategies. 635 \item a hash-map of method-name -> Lisp function mappings. Function 636 signature is \code{(lisp-this \&rest args)}. 637 \item a Lisp package. The name of the Java method to invoke is first 638 transformed in an idiomatic Lisp name (\code{javaMethodName} becomes 639 \code{JAVA-METHOD-NAME}) and a symbol with that name is searched in 640 the package. If it exists and is fbound, the corresponding function 641 will be called. Function signature is as the hash-table case. 541 642 \end{itemize} 542 643 543 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. 644 This functionality is exposed by the AbclScriptEngine with the two 645 methods getInterface(Class) and getInterface(Object, Class). The 646 former returns an interface implemented with the current Lisp package, 647 the latter allows the programmer to pass an interface-implementation 648 object which will in turn be passed to the jmake-proxy generic 649 function. 544 650 545 651 \chapter{Implementation Dependent Extensions} … … 727 833 \section{ASDF} 728 834 729 asdf-2.017 is packaged as core component of ABCL. By default, ASDF is 730 not loaded, as it relies on the CLOS subsystem which can take a bit of 731 time to initialize. 835 asdf-2.017 is packaged as core component of ABCL, but not intialized 836 by default, as it relies on the CLOS subsystem which can take a bit of 837 time to initialize. It may be initialized by the ANSI 838 \textsc{REQUIRE} mechanism as follows: 732 839 733 840 \begin{listing-lisp} … … 739 846 \section{abcl-asdf} 740 847 741 Allow ASDF system definition which dynamically loads JVM artifacts 742 such as jar archives via a Maven encapsulation. 743 744 ASDF components added: JAR-FILE, JAR-DIRECTORY, CLASS-FILE-DIRECTORY 848 This contrib to ABCL enables an additional syntax for ASDF system 849 definition which dynamically loads JVM artifacts such as jar archives 850 via a Maven encapsulation. 851 852 The following ASDF components are added: JAR-FILE, JAR-DIRECTORY, CLASS-FILE-DIRECTORY 745 853 and MVN. 746 854 … … 758 866 \section{jss} 759 867 760 Java Syntax sucks, so we introduce the \#" macro. 761 868 To one used to a syntax that can construct macros, the Java syntax 869 sucks, so we introduce the \#" macro. 870 871 \subsection{JSS usage} 872 873 Example: 874 875 \begin{listing-lisp} 876 CL-USER> (require 'jss) 877 878 CL-USER) (#"getProperties" 'java.lang.System) 879 880 CL-USER) (#"propertyNames" (#"getProperties" 'java.lang.System)) 881 \end{listing-lisp} 762 882 763 883 \chapter{History}
Note: See TracChangeset
for help on using the changeset viewer.