[armedbear-cvs] r11623 - in trunk/abcl: examples/abcl/jsr-223 src/org/armedbear/lisp/scripting src/org/armedbear/lisp/scripting/lisp
Alessio Stalla
astalla at common-lisp.net
Wed Feb 4 21:58:28 UTC 2009
Author: astalla
Date: Wed Feb 4 21:58:25 2009
New Revision: 11623
Log:
Perfectioned ABCL auto-configuration when using JSR-223; added option to
compile scripts using temp files (default) or using the run-time
compiler; added example of usage of ABCL with JSR-223.
Added:
trunk/abcl/examples/abcl/jsr-223/
trunk/abcl/examples/abcl/jsr-223/JSR223Example.java
Modified:
trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java
trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngineFactory.java
trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp
trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp
Added: trunk/abcl/examples/abcl/jsr-223/JSR223Example.java
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/abcl/jsr-223/JSR223Example.java Wed Feb 4 21:58:25 2009
@@ -0,0 +1,58 @@
+import javax.script.*;
+
+public class JSR223Example {
+
+ public static void main(String[] args) {
+ //Script Engine instantiation
+ ScriptEngine lispEngine = new ScriptEngineManager().getEngineByExtension("lisp");
+
+ //Accessing variables
+ System.out.println();
+ System.out.println("*package* = " + lispEngine.get("*package*"));
+ Object someValue = new Object();
+ lispEngine.put("someVariable", someValue);
+ System.out.println("someVariable = " + lispEngine.get("someVariable"));
+ try {
+ //Interpretation (also from streams)
+ lispEngine.eval("(defun hello (arg) (print (list arg someVariable)) (terpri))");
+
+ //Direct function invocation
+ ((Invocable) lispEngine).invokeFunction("hello", "world");
+
+ //Implementing a Java interface in Lisp
+ lispEngine.eval("(defun compare-to (&rest args) 42)");
+ Comparable c = ((Invocable) lispEngine).getInterface(java.lang.Comparable.class);
+ System.out.println("compareTo: " + c.compareTo(null));
+
+ //Compilation!
+ lispEngine.eval("(defmacro slow-compiling-macro (arg) (dotimes (i 1000000) (incf i)) `(print ,arg))");
+
+ long millis = System.currentTimeMillis();
+ lispEngine.eval("(slow-compiling-macro 42)");
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("interpretation took " + millis);
+
+ millis = System.currentTimeMillis();
+ CompiledScript cs = ((Compilable) lispEngine).compile("(slow-compiling-macro 42)");
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("compilation took " + millis);
+
+ millis = System.currentTimeMillis();
+ cs.eval();
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("evaluation took " + millis);
+
+ millis = System.currentTimeMillis();
+ cs.eval();
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("evaluation took " + millis);
+
+ //Ecc. ecc.
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ } catch (ScriptException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
Modified: trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java (original)
+++ trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java Wed Feb 4 21:58:25 2009
@@ -43,18 +43,6 @@
private Function evalScript;
private Function compileScript;
private Function evalCompiledScript;
- private boolean configured = false;
-
- public AbclScriptEngine(boolean enableThrowingDebugger) {
- this();
- if (enableThrowingDebugger) {
- try {
- installThrowingDebuggerHook(LispThread.currentThread());
- } catch (ConditionThrowable e) {
- throw new InternalError("Can't set throwing debugger hook!");
- }
- }
- }
public AbclScriptEngine() {
interpreter = Interpreter.createInstance();
@@ -76,11 +64,7 @@
throw new RuntimeException(e);
}
}
-
- public boolean isConfigured() {
- return configured;
- }
-
+
public Interpreter getInterpreter() {
return interpreter;
}
Modified: trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngineFactory.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngineFactory.java (original)
+++ trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngineFactory.java Wed Feb 4 21:58:25 2009
@@ -28,7 +28,7 @@
public class AbclScriptEngineFactory implements ScriptEngineFactory {
- private static final AbclScriptEngine THE_ONLY_ONE_ENGINE = new AbclScriptEngine(true);
+ private static final AbclScriptEngine THE_ONLY_ONE_ENGINE = new AbclScriptEngine();
@Override
public String getEngineName() {
Modified: trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp (original)
+++ trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp Wed Feb 4 21:58:25 2009
@@ -95,13 +95,28 @@
`((funcall ,function))))
(defun compile-script (code-string)
- (eval
- `(compile
- nil
- (lambda ()
- ,@(let ((*package* (find-package :abcl-script-user)))
- (read-from-string (concatenate 'string "(" code-string ")")))))))
-
+ (if *compile-using-temp-files*
+ (let* ((tmp-file (jstatic (jmethod "java.io.File" "createTempFile" "java.lang.String" "java.lang.String")
+ nil "abcl-src-file-" ".lisp"))
+ (tmp-file-path (jcall (jmethod "java.io.File" "getAbsolutePath") tmp-file)))
+ (jcall (jmethod "java.io.File" "deleteOnExit") tmp-file) ;to be really-really-really sure...
+ (unwind-protect
+ (progn
+ (with-open-file (stream tmp-file-path :direction :output :if-exists :overwrite)
+ (prin1 code-string stream)
+ (finish-output stream))
+ (let ((compiled-file (compile-file tmp-file-path)))
+ (jcall (jmethod "java.io.File" "deleteOnExit")
+ (jnew (jconstructor "java.io.File" "java.lang.String")
+ (namestring compiled-file)))
+ (lambda () (load compiled-file))))
+ (delete-file tmp-file-path)))
+ (eval
+ `(compile
+ nil
+ (lambda ()
+ ,@(let ((*package* (find-package :abcl-script-user)))
+ (read-from-string (concatenate 'string "(" code-string ")"))))))))
;;Java interface implementation
Modified: trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp (original)
+++ trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp Wed Feb 4 21:58:25 2009
@@ -31,7 +31,7 @@
(in-package :abcl-script)
-(defparameter *abcl-debug* nil)
+(defparameter *launch-swank-at-startup* nil)
(defparameter *swank-dir* nil)
@@ -39,7 +39,9 @@
(defparameter *use-throwing-debugger* t)
-(defparameter *compile-using-temp-files* nil)
+(defparameter *compile-using-temp-files* t)
+
+;(defparameter *compiler-temp-dir* #P".abcl.d/tmp/")
(defconstant +standard-debugger-hook+ *debugger-hook*)
@@ -48,7 +50,7 @@
(if *use-throwing-debugger*
#'sys::%debugger-hook-function
+standard-debugger-hook+))
- (when *abcl-debug*
+ (when *launch-swank-at-startup*
(unless *swank-dir*
(error "Swank directory not specified, please set *swank-dir*"))
(pushnew *swank-dir* asdf:*central-registry* :test #'equal)
@@ -57,4 +59,4 @@
(symbol-name '#:create-server)
:swank)
:port *swank-port*))
- :name "ABCL script - Swank thread")))
\ No newline at end of file
+ :name "ABCL script - Swank thread")))
More information about the armedbear-cvs
mailing list