Changeset 11393


Ignore:
Timestamp:
11/19/08 20:57:04 (13 years ago)
Author:
astalla
Message:

Implemented the Compilable interface and refactored the script evaluation code in the process.

Location:
branches/scripting/j/src/org/armedbear/lisp/scripting
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/scripting/j/src/org/armedbear/lisp/scripting/AbclScriptEngine.java

    r11389 r11393  
    3131import javax.script.AbstractScriptEngine;
    3232import javax.script.Bindings;
     33import javax.script.Compilable;
     34import javax.script.CompiledScript;
    3335import javax.script.Invocable;
    3436import javax.script.ScriptContext;
     37import javax.script.ScriptEngine;
    3538import javax.script.ScriptEngineFactory;
    3639import javax.script.ScriptException;
     
    5962
    6063
    61 public class AbclScriptEngine extends AbstractScriptEngine implements Invocable {
     64public class AbclScriptEngine extends AbstractScriptEngine implements Invocable, Compilable {
    6265
    6366  private Interpreter interpreter;
    6467  private LispObject nonThrowingDebugHook;
    6568  private Function evalScript;
     69  private Function compileScript;
     70  private Function evalCompiledScript;
    6671
    6772  public AbclScriptEngine(Interpreter interpreter, boolean enableThrowingDebugger) {
     73   
    6874    this.interpreter = interpreter;
    6975    Interpreter.initializeLisp();
     
    8187      loadFromClasspath("/org/armedbear/lisp/scripting/lisp/abcl-script.lisp");
    8288      evalScript = (Function) this.findSymbol("EVAL-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     89      compileScript = (Function) this.findSymbol("COMPILE-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     90      evalCompiledScript = (Function) this.findSymbol("EVAL-COMPILED-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
    8391    } catch (ConditionThrowable e) {
    84       e.printStackTrace();
     92      throw new Error(e);
    8593    }
    8694  }
     
    270278  }
    271279
    272   @Override
    273   public Object eval(String code, ScriptContext ctx) throws ScriptException {
     280  private Object eval(Function evaluator, LispObject code, ScriptContext ctx) throws ScriptException {
    274281    ReaderInputStream in = null;
    275282    WriterOutputStream out = null;
     
    280287      Stream outStream = new Stream(out, Symbol.CHARACTER);
    281288      Stream inStream  = new Stream(in,  Symbol.CHARACTER);
    282       retVal = evalScript.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)),
    283                     makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)),
    284                     inStream, outStream,
    285                     new SimpleString(code), new JavaObject(ctx));
     289      retVal = evaluator.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)),
     290                     makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)),
     291                     inStream, outStream,
     292                     code, new JavaObject(ctx));
    286293      return toJava(retVal);
    287294    } catch (ConditionThrowable e) {
     
    291298    }
    292299  }
    293 
    294   @Override
    295   public Object eval(Reader code, ScriptContext ctx) throws ScriptException {
     300 
     301  @Override
     302  public Object eval(String code, ScriptContext ctx) throws ScriptException {
     303    return eval(evalScript, new SimpleString(code), ctx);
     304  }
     305
     306  private static String toString(Reader reader) throws IOException {
    296307    StringWriter w = new StringWriter();
    297308    int i;
    298     try {
    299       i = code.read();
    300       while (i != -1) {
    301         w.write(i);
    302         i = code.read();
    303       }
    304       return eval(w.toString(), ctx);
     309    i = reader.read();
     310    while (i != -1) {
     311      w.write(i);
     312      i = reader.read();
     313    }
     314    return w.toString();
     315  }
     316 
     317  @Override
     318  public Object eval(Reader code, ScriptContext ctx) throws ScriptException {
     319    try {
     320      return eval(toString(code), ctx);
    305321    } catch (IOException e) {
    306322      return new ScriptException(e);
     
    351367          v.aset(i, new JavaObject(array[i]));
    352368        } catch (ConditionThrowable e) {
    353           throw new Error("Can't set simplevector index " + i, e);
     369          throw new Error("Can't set SimpleVector index " + i, e);
    354370        }
    355371            }
     
    432448  }
    433449
     450  public class AbclCompiledScript extends CompiledScript {
     451
     452    private LispObject function;
     453   
     454    public AbclCompiledScript(LispObject function) {
     455      this.function = function;
     456    }
     457   
     458    @Override
     459    public Object eval(ScriptContext context) throws ScriptException {
     460      return AbclScriptEngine.this.eval(evalCompiledScript, function, context);
     461    }
     462
     463    @Override
     464    public ScriptEngine getEngine() {
     465      return AbclScriptEngine.this;
     466    }
     467
     468  }
     469
     470 
     471  @Override
     472  public CompiledScript compile(String script) throws ScriptException {
     473    try {
     474      Function f = (Function) compileScript.execute(new SimpleString(script));
     475      return new AbclCompiledScript(f);
     476    } catch (ConditionThrowable e) {
     477      throw new ScriptException(new Exception(e));
     478    } catch(ClassCastException e) {
     479      throw new ScriptException(e);
     480    }
     481  }
     482
     483  @Override
     484  public CompiledScript compile(Reader script) throws ScriptException {
     485    try {
     486      return compile(toString(script));
     487    } catch (IOException e) {
     488      throw new ScriptException(e);
     489    }
     490  }
     491
    434492}
  • branches/scripting/j/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp

    r11389 r11393  
    4646       ,java-bindings ,(car jbinding) ,(car binding))))
    4747
     48(defmacro with-script-context ((global-bindings engine-bindings stdin stdout script-context)
     49             body)
     50  (let ((actual-global-bindings (gensym))
     51  (actual-engine-bindings (gensym)))
     52    `(let ((*package* (find-package :abcl-script-user))
     53     (*standard-input* ,stdin)
     54     (*standard-output* ,stdout)
     55     (,actual-global-bindings (generate-bindings ,global-bindings))
     56     (,actual-engine-bindings (generate-bindings ,engine-bindings)))
     57      (eval `(let ((*standard-input* ,,stdin)
     58       (*standard-output* ,,stdout)
     59       (*package* (find-package :abcl-script-user)))
     60        (let (,@,actual-global-bindings)
     61    (let (,@,actual-engine-bindings)
     62      (prog1
     63          (progn ,@,body)
     64        (finish-output *standard-output*)
     65        ,@(generate-java-bindings
     66           ,global-bindings
     67           ,actual-global-bindings
     68           (jcall +get-bindings+ ,script-context +global-scope+))
     69        ,@(generate-java-bindings
     70           ,engine-bindings
     71           ,actual-engine-bindings
     72           (jcall +get-bindings+ ,script-context +engine-scope+))))))))))
     73 
    4874(defun eval-script (global-bindings engine-bindings stdin stdout
    4975        code-string script-context)
    50   (let ((*package* (find-package :abcl-script-user))
    51   (*standard-input* stdin)
    52   (*standard-output* stdout)
    53   (actual-global-bindings (generate-bindings global-bindings))
    54   (actual-engine-bindings (generate-bindings engine-bindings)))
    55     (eval `(let ((*standard-input* ,stdin)
    56      (*standard-output* ,stdout)
    57      (*package* (find-package :abcl-script-user)))
    58       (let (,@actual-global-bindings)
    59         (let (,@actual-engine-bindings)
    60     (prog1
    61         (progn
    62           ,@(read-from-string
    63        (concatenate 'string "(" code-string ")")))
    64       (finish-output *standard-output*)
    65       ,@(generate-java-bindings
    66          global-bindings
    67          actual-global-bindings
    68          (jcall +get-bindings+ script-context +global-scope+))
    69       ,@(generate-java-bindings
    70          engine-bindings
    71          actual-engine-bindings
    72          (jcall +get-bindings+ script-context +engine-scope+)))))))))
     76  (with-script-context (global-bindings engine-bindings stdin stdout script-context)
     77    (read-from-string
     78     (concatenate 'string "(" code-string ")"))))
     79
     80(defun eval-compiled-script (global-bindings engine-bindings stdin stdout
     81           function script-context)
     82  (with-script-context (global-bindings engine-bindings stdin stdout script-context)
     83    `((funcall ,function))))
     84
     85(defun compile-script (code-string)
     86  (let ((*package* (find-package :abcl-script-user)))
     87    (eval `(compile nil
     88      (lambda ()
     89        ,@(read-from-string (concatenate 'string "(" code-string ")")))))))
  • branches/scripting/j/src/org/armedbear/lisp/scripting/lisp/packages.lisp

    r11360 r11393  
    2020  (:use :cl :java)
    2121  (:export #:eval-script
     22     #:compile-script
     23     #:eval-compiled-script
    2224     #:define-java-interface-implementation
    2325     #:find-java-interface-implementation
Note: See TracChangeset for help on using the changeset viewer.