Changeset 11856


Ignore:
Timestamp:
05/11/09 21:12:17 (14 years ago)
Author:
astalla
Message:
  • loading:

added a new primitive sys::load-returning-last-result which behaves like
load but returns the last value produced instead of T

  • JSR-223:
    • used the new load-returning-last-result to evaluate both interpreted and compiled code for consistency (with a caveat, see the wiki page on JSR-223)
    • bindings established through ScriptContext? are now declared special
    • compilation using the runtime compiler has been removed due to inconsistencies with evaluation and file-based compilation
    • updated the example as suggested on the ML to show both modes of getting the AbclScriptEngine?


Location:
trunk/abcl
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/examples/abcl/jsr-223/JSR223Example.java

    r11623 r11856  
    44
    55  public static void main(String[] args) {
    6     //Script Engine instantiation
    7     ScriptEngine lispEngine = new ScriptEngineManager().getEngineByExtension("lisp");
     6      //Script Engine instantiation using ServiceProvider - this will
     7      //look in the classpath for a file
     8      //  /META-INF/services/javax.script.ScriptEngineFactory
     9      //where the AbclScriptEngineFactory is registered
     10      ScriptEngine lispEngine = new ScriptEngineManager().getEngineByExtension("lisp");
     11
     12      //Alternatively, you can directly instantiate the script engine:
     13     
     14      //ScriptEngineManager scriptManager = new ScriptEngineManager();
     15      //scriptManager.registerEngineExtension("lisp", new AbclScriptEngineFactory());
     16      //ScriptEngine lispEngine = scriptManager.getEngineByExtension("lisp");
     17
     18      //(thanks to Peter Tsenter for suggesting this)
    819       
    920    //Accessing variables
  • trunk/abcl/src/org/armedbear/lisp/Load.java

    r11824 r11856  
    9191                                        boolean print,
    9292                                        boolean ifDoesNotExist)
     93        throws ConditionThrowable {
     94  return load(pathname, filename, verbose, print, ifDoesNotExist, false);
     95    }
     96
     97
     98    public static final LispObject load(Pathname pathname,
     99                                        String filename,
     100                                        boolean verbose,
     101                                        boolean print,
     102                                        boolean ifDoesNotExist,
     103          boolean returnLastResult)
    93104        throws ConditionThrowable
    94105    {
     
    154165            return loadFileFromStream(null, truename,
    155166                                      new Stream(in, Symbol.CHARACTER),
    156                                       verbose, print, false);
     167                                      verbose, print, false, returnLastResult);
    157168        }
    158169        catch (FaslVersionMismatch e) {
     
    381392                                                       boolean print,
    382393                                                       boolean auto)
     394  throws ConditionThrowable {
     395  return loadFileFromStream(pathname, truename, in, verbose, print, auto, false);
     396    }
     397
     398    private static final LispObject loadFileFromStream(LispObject pathname,
     399                                                       String truename,
     400                                                       Stream in,
     401                                                       boolean verbose,
     402                                                       boolean print,
     403                                                       boolean auto,
     404                   boolean returnLastResult)
    383405        throws ConditionThrowable
    384406    {
     
    416438                out._writeLine(" ...");
    417439                out._finishOutput();
    418                 LispObject result = loadStream(in, print, thread);
     440                LispObject result = loadStream(in, print, thread, returnLastResult);
    419441                long elapsed = System.currentTimeMillis() - start;
    420442                out.freshLine();
     
    428450                return result;
    429451            } else
    430                 return loadStream(in, print, thread);
     452                return loadStream(in, print, thread, returnLastResult);
    431453        }
    432454        finally {
     
    445467    private static final LispObject loadStream(Stream in, boolean print,
    446468                                               LispThread thread)
     469  throws ConditionThrowable {
     470  return loadStream(in, print, thread, false);
     471    }
     472
     473    private static final LispObject loadStream(Stream in, boolean print,
     474                                               LispThread thread, boolean returnLastResult)
    447475        throws ConditionThrowable
    448476    {
     
    455483        try {
    456484            final Environment env = new Environment();
     485      LispObject result = NIL;
    457486            while (true) {
    458487                sourcePositionBinding.value = Fixnum.getInstance(in.getOffset());
     
    460489                if (obj == EOF)
    461490                    break;
    462                 LispObject result = eval(obj, env, thread);
     491                result = eval(obj, env, thread);
    463492                if (print) {
    464493                    Stream out =
     
    468497                }
    469498            }
    470             return T;
     499      if(returnLastResult) {
     500    return result;
     501      } else {
     502    return T;
     503      }
    471504        }
    472505        finally {
     
    481514        final Environment env = new Environment();
    482515        final SpecialBinding lastSpecialBinding = thread.lastSpecialBinding;
     516  LispObject result = NIL;
    483517        try {
    484518            thread.bindSpecial(_FASL_ANONYMOUS_PACKAGE_, new Package());
     
    487521                if (obj == EOF)
    488522                    break;
    489                 eval(obj, env, thread);
     523                result = eval(obj, env, thread);
    490524            }
    491525        }
     
    493527            thread.lastSpecialBinding = lastSpecialBinding;
    494528        }
    495         return T;
     529        return result;
     530  //There's no point in using here the returnLastResult flag like in
     531  //loadStream(): this function is only called from init-fasl, which is
     532  //only called from load, which already has its own policy for choosing
     533  //whether to return T or the last value.
    496534    }
    497535
     
    563601        @Override
    564602        public LispObject execute(LispObject filespec, LispObject verbose,
    565                                   LispObject print, LispObject ifDoesNotExist)
    566             throws ConditionThrowable
    567         {
    568             if (filespec instanceof Stream) {
    569                 if (((Stream)filespec).isOpen()) {
    570                     LispObject pathname;
    571                     if (filespec instanceof FileStream)
    572                         pathname = ((FileStream)filespec).getPathname();
    573                     else
    574                         pathname = NIL;
    575                     String truename;
    576                     if (pathname instanceof Pathname)
    577                         truename = ((Pathname)pathname).getNamestring();
    578                     else
    579                         truename = null;
    580                     return loadFileFromStream(pathname,
    581                                               truename,
    582                                               (Stream) filespec,
    583                                               verbose != NIL,
    584                                               print != NIL,
    585                                               false);
    586                 }
    587                 // If stream is closed, fall through...
    588             }
    589             Pathname pathname = coerceToPathname(filespec);
    590             if (pathname instanceof LogicalPathname)
    591                 pathname = LogicalPathname.translateLogicalPathname((LogicalPathname)pathname);
    592             return load(pathname,
    593                         pathname.getNamestring(),
    594                         verbose != NIL,
    595                         print != NIL,
    596                         ifDoesNotExist != NIL);
    597         }
     603          LispObject print, LispObject ifDoesNotExist)
     604      throws ConditionThrowable {
     605      return load(filespec, verbose, print, ifDoesNotExist, NIL);
     606  }
    598607    };
     608
     609    // ### %load-returning-last-result filespec verbose print if-does-not-exist => object
     610    private static final Primitive _LOAD_RETURNING_LAST_RESULT =
     611        new Primitive("%load-returning-last-result", PACKAGE_SYS, false,
     612                      "filespec verbose print if-does-not-exist")
     613    {
     614        @Override
     615        public LispObject execute(LispObject filespec, LispObject verbose,
     616          LispObject print, LispObject ifDoesNotExist)
     617      throws ConditionThrowable {
     618      return load(filespec, verbose, print, ifDoesNotExist, T);
     619  }
     620    };
     621
     622    private static final LispObject load(LispObject filespec,
     623           LispObject verbose,
     624           LispObject print,
     625           LispObject ifDoesNotExist,
     626           LispObject returnLastResult)
     627  throws ConditionThrowable {
     628  if (filespec instanceof Stream) {
     629      if (((Stream)filespec).isOpen()) {
     630    LispObject pathname;
     631    if (filespec instanceof FileStream)
     632        pathname = ((FileStream)filespec).getPathname();
     633    else
     634        pathname = NIL;
     635    String truename;
     636    if (pathname instanceof Pathname)
     637        truename = ((Pathname)pathname).getNamestring();
     638    else
     639        truename = null;
     640    return loadFileFromStream(pathname,
     641            truename,
     642            (Stream) filespec,
     643            verbose != NIL,
     644            print != NIL,
     645            false,
     646            returnLastResult != NIL);
     647      }
     648      // If stream is closed, fall through...
     649  }
     650  Pathname pathname = coerceToPathname(filespec);
     651  if (pathname instanceof LogicalPathname)
     652      pathname = LogicalPathname.translateLogicalPathname((LogicalPathname)pathname);
     653  return load(pathname,
     654        pathname.getNamestring(),
     655        verbose != NIL,
     656        print != NIL,
     657        ifDoesNotExist != NIL,
     658        returnLastResult != NIL);
     659    }
    599660
    600661    // ### load-system-file
  • trunk/abcl/src/org/armedbear/lisp/load.lisp

    r11391 r11856  
    4343             (merge-pathnames (pathname filespec)))
    4444         verbose print if-does-not-exist))
     45
     46(defun load-returning-last-result (filespec
     47             &key
     48             (verbose *load-verbose*)
     49             (print *load-print*)
     50             (if-does-not-exist t)
     51             (external-format :default))
     52  (declare (ignore external-format)) ; FIXME
     53  (%load-returning-last-result (if (streamp filespec)
     54             filespec
     55             (merge-pathnames (pathname filespec)))
     56         verbose print if-does-not-exist))
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp

    r11839 r11856  
    5252      bindings)))
    5353
     54(defun generate-special-declarations (bindings)
     55  (let ((*package* (find-package :abcl-script-user)))
     56    `(declare (special
     57         ,@(mapcar (lambda (binding) (read-from-string (car binding)))
     58       bindings)))))
     59
    5460(defun generate-java-bindings (bindings-list actual-bindings java-bindings)
    5561  (loop :for binding  :in actual-bindings
     
    7379       (eval `(let (,@,actual-global-bindings)
    7480    (let (,@,actual-engine-bindings)
     81      ,(generate-special-declarations ,global-bindings)
     82      ,(generate-special-declarations ,engine-bindings)
    7583      (prog1
    7684          (progn ,@,body)
     
    8896        code-string script-context)
    8997  (eval-in-script-context (global-bindings engine-bindings stdin stdout script-context)
    90     (read-from-string
    91      (concatenate 'string "(" code-string ")"))))
     98    `((with-input-from-string (str ,code-string)
     99  (sys::load-returning-last-result str)))))
    92100
    93101(defun eval-compiled-script (global-bindings engine-bindings stdin stdout
     
    97105
    98106(defun compile-script (code-string)
    99   (if *compile-using-temp-files*
    100       (let* ((tmp-file (jstatic (jmethod "java.io.File" "createTempFile" "java.lang.String" "java.lang.String")
    101         nil "abcl-src-file-" ".lisp"))
    102        (tmp-file-path (jcall (jmethod "java.io.File" "getAbsolutePath") tmp-file)))
    103   (jcall (jmethod "java.io.File" "deleteOnExit") tmp-file) ;to be really-really-really sure...
    104   (unwind-protect
    105        (progn
    106          (with-open-file (stream tmp-file-path :direction :output)
    107      (princ "(in-package :abcl-script-user)" stream)
    108      (princ code-string stream)
    109      (finish-output stream))
    110          (let ((compiled-file (compile-file tmp-file-path)))
    111      (jcall (jmethod "java.io.File" "deleteOnExit")
    112       (jnew (jconstructor "java.io.File" "java.lang.String")
    113             (namestring compiled-file)))
    114      (lambda ()
    115        (let ((*package* (find-package :abcl-script-user)))
    116          (load compiled-file :verbose t :print t)))))
    117     (delete-file tmp-file-path)))
    118       (eval
    119        `(compile
    120    nil
    121    (lambda ()
    122      ,@(let ((*package* (find-package :abcl-script-user)))
    123          (read-from-string
    124     (concatenate 'string "(" code-string " cl:t)")))))))) ;return T in conformity of what LOAD does.
     107  (let* ((tmp-file (jstatic (jmethod "java.io.File" "createTempFile" "java.lang.String" "java.lang.String")
     108          nil "abcl-src-file-" ".lisp"))
     109   (tmp-file-path (jcall (jmethod "java.io.File" "getAbsolutePath") tmp-file)))
     110    (jcall (jmethod "java.io.File" "deleteOnExit") tmp-file) ;to be really-really-really sure...
     111    (unwind-protect
     112   (progn
     113     (with-open-file (stream tmp-file-path :direction :output)
     114       (princ "(in-package :abcl-script-user)" stream)
     115       (princ code-string stream)
     116       (finish-output stream))
     117     (let ((compiled-file (compile-file tmp-file-path)))
     118       (jcall (jmethod "java.io.File" "deleteOnExit")
     119        (jnew (jconstructor "java.io.File" "java.lang.String")
     120        (namestring compiled-file)))
     121       (lambda ()
     122         (let ((*package* (find-package :abcl-script-user)))
     123     (sys::load-returning-last-result compiled-file)))))
     124      (delete-file tmp-file-path))))
    125125
    126126;;Java interface implementation - TODO
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp

    r11839 r11856  
    4040(defparameter *use-throwing-debugger* t)
    4141
    42 (defparameter *compile-using-temp-files* t)
    43 
    4442(defun configure-abcl (abcl-script-engine)
    4543  (when *launch-swank-at-startup*
Note: See TracChangeset for help on using the changeset viewer.