Changeset 11839
- Timestamp:
- 05/07/09 22:01:52 (15 years ago)
- 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 39 39 public class AbclScriptEngine extends AbstractScriptEngine implements Invocable, Compilable { 40 40 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 } 119 98 120 99 public LispObject loadFromClasspath(String classpathResource) throws ConditionThrowable { … … 245 224 } 246 225 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 } 267 247 268 248 @Override -
trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp
r11623 r11839 58 58 ,java-bindings ,(car jbinding) ,(car binding)))) 59 59 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." 62 63 (let ((actual-global-bindings (gensym)) 63 64 (actual-engine-bindings (gensym))) … … 65 66 (*standard-input* ,stdin) 66 67 (*standard-output* ,stdout) 68 (*debugger-hook* (if *use-throwing-debugger* 69 #'sys::%debugger-hook-function 70 *debugger-hook*)) 67 71 (,actual-global-bindings (generate-bindings ,global-bindings)) 68 72 (,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) 73 74 (let (,@,actual-engine-bindings) 74 75 (prog1 … … 82 83 ,engine-bindings 83 84 ,actual-engine-bindings 84 (jcall +get-bindings+ ,script-context +engine-scope+))))))))) )85 (jcall +get-bindings+ ,script-context +engine-scope+))))))))) 85 86 86 87 (defun eval-script (global-bindings engine-bindings stdin stdout 87 88 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) 89 90 (read-from-string 90 91 (concatenate 'string "(" code-string ")")))) … … 92 93 (defun eval-compiled-script (global-bindings engine-bindings stdin stdout 93 94 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) 95 96 `((funcall ,function)))) 96 97 … … 103 104 (unwind-protect 104 105 (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) 107 109 (finish-output stream)) 108 110 (let ((compiled-file (compile-file tmp-file-path))) … … 110 112 (jnew (jconstructor "java.io.File" "java.lang.String") 111 113 (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))))) 113 117 (delete-file tmp-file-path))) 114 118 (eval … … 117 121 (lambda () 118 122 ,@(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. 120 125 121 ;;Java interface implementation 126 ;;Java interface implementation - TODO 122 127 123 128 (defvar *interface-implementation-map* (make-hash-table :test #'equal)) … … 126 131 (gethash interface *interface-implementation-map*)) 127 132 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))) 130 136 131 137 (defun remove-java-interface-implementation (interface) 132 138 (remhash interface *interface-implementation-map*)) 133 139 134 (defun define-java-interface-implementation (interface implementation &optional lisp-this)135 (register-java-interface-implementation136 interface137 (jmake-proxy interface implementation lisp-this)))138 139 140 ;Let's load it so asdf package is already defined when loading config.lisp 140 141 (require 'asdf) -
trunk/abcl/src/org/armedbear/lisp/scripting/lisp/config.lisp
r11629 r11839 42 42 (defparameter *compile-using-temp-files* t) 43 43 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) 51 45 (when *launch-swank-at-startup* 52 46 (unless *swank-dir* 53 47 (error "Swank directory not specified, please set *swank-dir*")) 48 (when *use-throwing-debugger* 49 (setf *debugger-hook* #'sys::%debugger-hook-function)) 54 50 (pushnew *swank-dir* asdf:*central-registry* :test #'equal) 55 51 (asdf:oos 'asdf:load-op :swank)
Note: See TracChangeset
for help on using the changeset viewer.