Changeset 11839


Ignore:
Timestamp:
05/07/09 22:01:52 (15 years ago)
Author:
astalla
Message:

Fixed compilation with temp files with JSR-223. Refactoring of AbclScriptEngine?
(mostly elimination of dead code). Changed policy of use of #'sys::%debugger-hook-function
in an attempt to have the throwing debugger cover more cases; it still doesn't
work always.

Location:
trunk/abcl/src/org/armedbear/lisp/scripting
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java

    r11724 r11839  
    3939public class AbclScriptEngine extends AbstractScriptEngine implements Invocable, Compilable {
    4040
    41   private Interpreter interpreter;
    42   private LispObject nonThrowingDebugHook;
    43   private Function evalScript;
    44   private Function compileScript;
    45   private Function evalCompiledScript;
    46 
    47   public AbclScriptEngine() {
    48     interpreter = Interpreter.getInstance();
    49     if(interpreter == null) {
    50         interpreter = Interpreter.createInstance();
    51     }
    52     this.nonThrowingDebugHook = Symbol.DEBUGGER_HOOK.getSymbolValue();
    53     try {
    54       loadFromClasspath("/org/armedbear/lisp/scripting/lisp/packages.lisp");
    55       loadFromClasspath("/org/armedbear/lisp/scripting/lisp/abcl-script.lisp");
    56       loadFromClasspath("/org/armedbear/lisp/scripting/lisp/config.lisp");
    57       if(getClass().getResource("/abcl-script-config.lisp") != null) {
    58           System.out.println("ABCL: loading configuration from " + getClass().getResource("/abcl-script-config.lisp"));
    59           loadFromClasspath("/abcl-script-config.lisp");
    60       }
    61       interpreter.eval("(abcl-script:configure-abcl)");
    62       System.out.println("ABCL: configured");
    63       evalScript = (Function) this.findSymbol("EVAL-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
    64       compileScript = (Function) this.findSymbol("COMPILE-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
    65       evalCompiledScript = (Function) this.findSymbol("EVAL-COMPILED-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
    66     } catch (ConditionThrowable e) {
    67       throw new RuntimeException(e);
    68     }
    69   }
    70    
    71   public Interpreter getInterpreter() {
    72     return interpreter;
    73   }
    74 
    75   public void installNonThrowingDebugHook() {
    76     installNonThrowingDebugHook(LispThread.currentThread());
    77   }
    78 
    79   public void installNonThrowingDebugHook(LispThread thread) {
    80     thread.setSpecialVariable(Symbol.DEBUGGER_HOOK, this.nonThrowingDebugHook);
    81   }
    82 
    83   public void installThrowingDebuggerHook(LispThread thread) throws ConditionThrowable {
    84     Symbol dbgrhkfunSym;
    85     dbgrhkfunSym = Lisp.PACKAGE_SYS.findAccessibleSymbol("%DEBUGGER-HOOK-FUNCTION");
    86     LispObject throwingDebugHook = dbgrhkfunSym.getSymbolFunction();
    87     thread.setSpecialVariable(Symbol.DEBUGGER_HOOK, throwingDebugHook);
    88   }
    89 
    90   public void installThrowingDebuggerHook() throws ConditionThrowable {
    91     installThrowingDebuggerHook(LispThread.currentThread());
    92   }
    93 
    94   public void setStandardInput(InputStream stream, LispThread thread) {
    95     thread.setSpecialVariable(Symbol.STANDARD_INPUT, new Stream(stream, Symbol.CHARACTER, true));
    96   }
    97 
    98   public void setStandardInput(InputStream stream) {
    99     setStandardInput(stream, LispThread.currentThread());
    100   }
    101 
    102   public void setInterpreter(Interpreter interpreter) {
    103     this.interpreter = interpreter;
    104   }
    105 
    106   public static String escape(String s) {
    107     StringBuffer b = new StringBuffer();
    108     int len = s.length();
    109     char c;
    110     for (int i = 0; i < len; ++i) {
    111       c = s.charAt(i);
    112       if (c == '\\' || c == '"') {
    113         b.append('\\');
    114       }
    115       b.append(c);
    116     }
    117     return b.toString();
    118   }
     41    private Interpreter interpreter;
     42    private Function evalScript;
     43    private Function compileScript;
     44    private Function evalCompiledScript;
     45
     46    protected AbclScriptEngine() {
     47  interpreter = Interpreter.getInstance();
     48  if(interpreter == null) {
     49      interpreter = Interpreter.createInstance();
     50  }
     51  try {
     52      loadFromClasspath("/org/armedbear/lisp/scripting/lisp/packages.lisp");
     53      loadFromClasspath("/org/armedbear/lisp/scripting/lisp/abcl-script.lisp");
     54      loadFromClasspath("/org/armedbear/lisp/scripting/lisp/config.lisp");
     55      if(getClass().getResource("/abcl-script-config.lisp") != null) {
     56    System.out.println("ABCL: loading configuration from " + getClass().getResource("/abcl-script-config.lisp"));
     57    loadFromClasspath("/abcl-script-config.lisp");
     58      }
     59      ((Function) interpreter.eval("#'abcl-script:configure-abcl")).execute(new JavaObject(this));
     60      System.out.println("ABCL: configured");
     61      evalScript = (Function) this.findSymbol("EVAL-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     62      compileScript = (Function) this.findSymbol("COMPILE-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     63      evalCompiledScript = (Function) this.findSymbol("EVAL-COMPILED-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     64  } catch (ConditionThrowable e) {
     65      throw new RuntimeException(e);
     66  }
     67    }
     68   
     69    public Interpreter getInterpreter() {
     70  return interpreter;
     71    }
     72
     73    public void setStandardInput(InputStream stream, LispThread thread) {
     74  thread.setSpecialVariable(Symbol.STANDARD_INPUT, new Stream(stream, Symbol.CHARACTER, true));
     75    }
     76   
     77    public void setStandardInput(InputStream stream) {
     78  setStandardInput(stream, LispThread.currentThread());
     79    }
     80   
     81    public void setInterpreter(Interpreter interpreter) {
     82  this.interpreter = interpreter;
     83    }
     84
     85    public static String escape(String s) {
     86  StringBuffer b = new StringBuffer();
     87  int len = s.length();
     88  char c;
     89  for (int i = 0; i < len; ++i) {
     90      c = s.charAt(i);
     91      if (c == '\\' || c == '"') {
     92    b.append('\\');
     93      }
     94      b.append(c);
     95  }
     96  return b.toString();
     97    }
    11998
    12099  public LispObject loadFromClasspath(String classpathResource) throws ConditionThrowable {
     
    245224  }
    246225
    247   private Object eval(Function evaluator, LispObject code, ScriptContext ctx) throws ScriptException {
    248     ReaderInputStream in = null;
    249     WriterOutputStream out = null;
    250     LispObject retVal = null;
    251     try {
    252       in = new ReaderInputStream(ctx.getReader());
    253       out = new WriterOutputStream(ctx.getWriter());
    254       Stream outStream = new Stream(out, Symbol.CHARACTER);
    255       Stream inStream  = new Stream(in,  Symbol.CHARACTER);
    256       retVal = evaluator.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)),
    257                      makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)),
    258                      inStream, outStream,
    259                      code, new JavaObject(ctx));
    260       return toJava(retVal);
    261     } catch (ConditionThrowable e) {
    262       throw new ScriptException(new Exception(e));
    263     } catch (IOException e) {
    264       throw new ScriptException(e);
    265     }
    266   }
     226    private Object eval(Function evaluator, LispObject code, ScriptContext ctx) throws ScriptException {
     227  ReaderInputStream in = null;
     228  WriterOutputStream out = null;
     229  LispObject retVal = null;
     230  try {
     231      in = new ReaderInputStream(ctx.getReader());
     232      out = new WriterOutputStream(ctx.getWriter());
     233      Stream outStream = new Stream(out, Symbol.CHARACTER);
     234      Stream inStream  = new Stream(in,  Symbol.CHARACTER);
     235
     236      retVal = evaluator.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)),
     237               makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)),
     238               inStream, outStream,
     239               code, new JavaObject(ctx));
     240      return toJava(retVal);
     241  } catch (ConditionThrowable e) {
     242      throw new ScriptException(new Exception(e));
     243  } catch (IOException e) {
     244      throw new ScriptException(e);
     245  }
     246    }
    267247 
    268248  @Override
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp

    r11623 r11839  
    5858       ,java-bindings ,(car jbinding) ,(car binding))))
    5959
    60 (defmacro with-script-context ((global-bindings engine-bindings stdin stdout script-context)
    61              body)
     60(defmacro eval-in-script-context ((global-bindings engine-bindings stdin stdout script-context)
     61          body)
     62  "Sets up an environment in which to evaluate a piece of code coming from Java through the JSR-223 methods."
    6263  (let ((actual-global-bindings (gensym))
    6364  (actual-engine-bindings (gensym)))
     
    6566     (*standard-input* ,stdin)
    6667     (*standard-output* ,stdout)
     68     (*debugger-hook* (if *use-throwing-debugger*
     69        #'sys::%debugger-hook-function
     70        *debugger-hook*))
    6771     (,actual-global-bindings (generate-bindings ,global-bindings))
    6872     (,actual-engine-bindings (generate-bindings ,engine-bindings)))
    69       (eval `(let ((*standard-input* ,,stdin)
    70        (*standard-output* ,,stdout)
    71        (*package* (find-package :abcl-script-user)))
    72         (let (,@,actual-global-bindings)
     73       (eval `(let (,@,actual-global-bindings)
    7374    (let (,@,actual-engine-bindings)
    7475      (prog1
     
    8283           ,engine-bindings
    8384           ,actual-engine-bindings
    84            (jcall +get-bindings+ ,script-context +engine-scope+))))))))))
     85           (jcall +get-bindings+ ,script-context +engine-scope+)))))))))
    8586 
    8687(defun eval-script (global-bindings engine-bindings stdin stdout
    8788        code-string script-context)
    88   (with-script-context (global-bindings engine-bindings stdin stdout script-context)
     89  (eval-in-script-context (global-bindings engine-bindings stdin stdout script-context)
    8990    (read-from-string
    9091     (concatenate 'string "(" code-string ")"))))
     
    9293(defun eval-compiled-script (global-bindings engine-bindings stdin stdout
    9394           function script-context)
    94   (with-script-context (global-bindings engine-bindings stdin stdout script-context)
     95  (eval-in-script-context (global-bindings engine-bindings stdin stdout script-context)
    9596    `((funcall ,function))))
    9697
     
    103104  (unwind-protect
    104105       (progn
    105          (with-open-file (stream tmp-file-path :direction :output :if-exists :overwrite)
    106      (prin1 code-string stream)
     106         (with-open-file (stream tmp-file-path :direction :output)
     107     (princ "(in-package :abcl-script-user)" stream)
     108     (princ code-string stream)
    107109     (finish-output stream))
    108110         (let ((compiled-file (compile-file tmp-file-path)))
     
    110112      (jnew (jconstructor "java.io.File" "java.lang.String")
    111113            (namestring compiled-file)))
    112      (lambda () (load compiled-file))))
     114     (lambda ()
     115       (let ((*package* (find-package :abcl-script-user)))
     116         (load compiled-file :verbose t :print t)))))
    113117    (delete-file tmp-file-path)))
    114118      (eval
     
    117121   (lambda ()
    118122     ,@(let ((*package* (find-package :abcl-script-user)))
    119       (read-from-string (concatenate 'string "(" code-string ")"))))))))
     123         (read-from-string
     124    (concatenate 'string "(" code-string " cl:t)")))))))) ;return T in conformity of what LOAD does.
    120125
    121 ;;Java interface implementation
     126;;Java interface implementation - TODO
    122127
    123128(defvar *interface-implementation-map* (make-hash-table :test #'equal))
     
    126131  (gethash interface *interface-implementation-map*))
    127132
    128 (defun register-java-interface-implementation (interface impl)
    129   (setf (gethash interface *interface-implementation-map*) impl))
     133(defun register-java-interface-implementation (interface implementation &optional lisp-this)
     134  (setf (gethash interface *interface-implementation-map*)
     135  (jmake-proxy interface implementation lisp-this)))
    130136
    131137(defun remove-java-interface-implementation (interface)
    132138  (remhash interface *interface-implementation-map*))
    133139
    134 (defun define-java-interface-implementation (interface implementation &optional lisp-this)
    135   (register-java-interface-implementation
    136    interface
    137    (jmake-proxy interface implementation lisp-this)))
    138 
    139140;Let's load it so asdf package is already defined when loading config.lisp
    140141(require 'asdf)
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp

    r11629 r11839  
    4242(defparameter *compile-using-temp-files* t)
    4343
    44 (defconstant +standard-debugger-hook+ *debugger-hook*)
    45 
    46 (defun configure-abcl ()
    47   (setq *debugger-hook*
    48   (if *use-throwing-debugger*
    49       #'sys::%debugger-hook-function
    50       +standard-debugger-hook+))
     44(defun configure-abcl (abcl-script-engine)
    5145  (when *launch-swank-at-startup*
    5246    (unless *swank-dir*
    5347      (error "Swank directory not specified, please set *swank-dir*"))
     48    (when *use-throwing-debugger*
     49      (setf *debugger-hook* #'sys::%debugger-hook-function))
    5450    (pushnew *swank-dir* asdf:*central-registry* :test #'equal)
    5551    (asdf:oos 'asdf:load-op :swank)
Note: See TracChangeset for help on using the changeset viewer.