Changeset 11894


Ignore:
Timestamp:
05/18/09 19:37:43 (14 years ago)
Author:
astalla
Message:

Fixed function evaluation using invokeFunction. It was broken since last
commit on JSR-223. Now invokeFunction uses the same "eval-in-script-context"
macro that is used to evaluate interpreted and compiled code in the right
environment, including special variables from the ScriptContext?.
In passing, the invokeFunction() method has also been fixed so that
javaInstance() is called on its return value, like it happens in all other
kinds of Lisp calls from Java.

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

Legend:

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

    r11839 r11894  
    4040
    4141    private Interpreter interpreter;
     42    /**
     43     * The function used to evaluate a string of code.
     44     */
    4245    private Function evalScript;
     46    /**
     47     * The function used to evaluate a Lisp function.
     48     */
     49    private Function evalFunction;
     50    /**
     51     * The function used to compile Lisp code.
     52     */
    4353    private Function compileScript;
     54    /**
     55     * The function used to evaluate a compiled script.
     56     */
    4457    private Function evalCompiledScript;
    4558
     
    6275      compileScript = (Function) this.findSymbol("COMPILE-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
    6376      evalCompiledScript = (Function) this.findSymbol("EVAL-COMPILED-SCRIPT", "ABCL-SCRIPT").getSymbolFunction();
     77      evalFunction = (Function) this.findSymbol("EVAL-FUNCTION", "ABCL-SCRIPT").getSymbolFunction();
    6478  } catch (ConditionThrowable e) {
    6579      throw new RuntimeException(e);
     
    219233  }
    220234
    221   @Override
    222   public ScriptContext getContext() {
    223     return super.getContext();
    224   }
    225 
    226235    private Object eval(Function evaluator, LispObject code, ScriptContext ctx) throws ScriptException {
    227236  ReaderInputStream in = null;
     
    233242      Stream outStream = new Stream(out, Symbol.CHARACTER);
    234243      Stream inStream  = new Stream(in,  Symbol.CHARACTER);
    235 
    236244      retVal = evaluator.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)),
    237245               makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)),
    238246               inStream, outStream,
    239247               code, new JavaObject(ctx));
    240       return toJava(retVal);
     248      return retVal.javaInstance();
    241249  } catch (ConditionThrowable e) {
    242250      throw new ScriptException(new Exception(e));
     
    274282  public ScriptEngineFactory getFactory() {
    275283    return new AbclScriptEngineFactory();
    276   }
    277 
    278   private static Object toJava(LispObject lispObject) throws ConditionThrowable {
    279     return lispObject.javaInstance();
    280284  }
    281285 
     
    342346  }
    343347 
    344   @Override
    345   public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
    346       try {
    347     Symbol s;
    348     if(name.indexOf(':') >= 0) {
    349         s = findSymbol(name);
    350     } else {
    351         s = findSymbol(name, "ABCL-SCRIPT-USER");
    352     }
    353     if(s != null) {
    354         LispObject f = s.getSymbolFunction();
    355         if(f != null && f instanceof Function) {
    356       LispObject[] wrappedArgs = new LispObject[args.length];
    357       for(int i = 0; i < args.length; ++i) {
    358           wrappedArgs[i] = toLisp(args[i]);
    359       }
    360       switch(args.length) {
    361       case 0:
    362           return LispThread.currentThread().execute(f);
    363       case 1:
    364           return LispThread.currentThread().execute(f, wrappedArgs[0]);
    365       case 2:
    366           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1]);
    367       case 3:
    368           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2]);             
    369       case 4:
    370           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2], wrappedArgs[3]);
    371       case 5:
    372           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2], wrappedArgs[3], wrappedArgs[4]);
    373       case 6:
    374           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2], wrappedArgs[3], wrappedArgs[4], wrappedArgs[5]);
    375       case 7:
    376           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2], wrappedArgs[3], wrappedArgs[4], wrappedArgs[5], wrappedArgs[6]);
    377       case 8:
    378           return LispThread.currentThread().execute(f, wrappedArgs[0], wrappedArgs[1], wrappedArgs[2], wrappedArgs[3], wrappedArgs[4], wrappedArgs[5], wrappedArgs[6], wrappedArgs[7]);
    379       default:
    380           return LispThread.currentThread().execute(f, wrappedArgs);
    381       }
    382         } else {
    383       throw new NoSuchMethodException(name);
     348    @Override
     349    public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
     350  try {
     351      Symbol s;
     352      if(name.indexOf(':') >= 0) {
     353    s = findSymbol(name);
     354      } else {
     355    s = findSymbol(name, "ABCL-SCRIPT-USER");
     356      }
     357      if(s != null) {
     358    LispObject f = s.getSymbolFunction();
     359    if(f != null && f instanceof Function) {
     360        LispObject functionAndArgs = Lisp.NIL.push(f);
     361        for(int i = 0; i < args.length; ++i) {
     362      functionAndArgs = functionAndArgs.push(toLisp(args[i]));
    384363        }
     364        functionAndArgs = functionAndArgs.reverse();
     365        return eval(evalFunction, functionAndArgs, getContext());
    385366    } else {
    386367        throw new NoSuchMethodException(name);
    387368    }
    388       } catch (ConditionThrowable e) {
    389     throw new ScriptException(new RuntimeException(e));
     369      } else {
     370    throw new NoSuchMethodException(name);
    390371      }
    391   }
    392 
    393   @Override
    394   public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
    395     throw new UnsupportedOperationException("Common Lisp does not have methods in the Java sense.");
    396   }
    397 
    398   public class AbclCompiledScript extends CompiledScript {
    399 
    400     private LispObject function;
    401    
    402     public AbclCompiledScript(LispObject function) {
    403       this.function = function;
    404     }
    405    
    406     @Override
    407     public Object eval(ScriptContext context) throws ScriptException {
    408       return AbclScriptEngine.this.eval(evalCompiledScript, function, context);
    409     }
    410 
    411     @Override
    412     public ScriptEngine getEngine() {
    413       return AbclScriptEngine.this;
    414     }
    415 
    416   }
     372  } catch (ConditionThrowable e) {
     373      throw new ScriptException(new RuntimeException(e));
     374  }
     375    }
     376
     377    @Override
     378    public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
     379  throw new UnsupportedOperationException("Common Lisp does not have methods in the Java sense.");
     380    }
     381
     382    public class AbclCompiledScript extends CompiledScript {
     383
     384  private LispObject function;
     385 
     386  public AbclCompiledScript(LispObject function) {
     387      this.function = function;
     388  }
     389 
     390  @Override
     391  public Object eval(ScriptContext context) throws ScriptException {
     392      return AbclScriptEngine.this.eval(evalCompiledScript, function, context);
     393  }
     394 
     395  @Override
     396  public ScriptEngine getEngine() {
     397      return AbclScriptEngine.this;
     398  }
     399 
     400    }
    417401
    418402 
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp

    r11856 r11894  
    9393           (jcall +get-bindings+ ,script-context +engine-scope+)))))))))
    9494 
     95(defun eval-function (global-bindings engine-bindings stdin stdout function-and-args script-context)
     96  (eval-in-script-context (global-bindings engine-bindings stdin stdout script-context)
     97    `((funcall ,@(mapcar (lambda (arg) `(quote ,arg))
     98       function-and-args)))))
     99
    95100(defun eval-script (global-bindings engine-bindings stdin stdout
    96101        code-string script-context)
  • trunk/abcl/src/org/armedbear/lisp/scripting/lisp/packages.lisp

    r11629 r11894  
    3232  (:use :cl :java)
    3333  (:export
    34    #:eval-script
    3534   #:compile-script
    3635   #:*compile-using-temp-files*
    3736   #:configure-abcl
    3837   #:eval-compiled-script
     38   #:eval-function
     39   #:eval-script
    3940   #:define-java-interface-implementation
    4041   #:find-java-interface-implementation
Note: See TracChangeset for help on using the changeset viewer.