source: branches/0.22.x/abcl/src/org/armedbear/lisp/FaslClassLoader.java

Last change on this file was 12754, checked in by astalla, 14 years ago

Don't bind *load-truename* to NIL while loading FASLs, or SLIME compilation breaks.

File size: 5.5 KB
Line 
1/*
2 * JavaClassLoader.java
3 *
4 * Copyright (C) 2010 Alessio Stalla
5 * $Id: JavaClassLoader.java 12298 2009-12-18 21:50:54Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38import java.util.*;
39
40public class FaslClassLoader extends JavaClassLoader {
41
42    private final LispObject[] functions;
43    private String baseName;
44    private LispObject loader; //The function used to load FASL functions by number
45    private final JavaObject boxedThis = new JavaObject(this);
46   
47    public FaslClassLoader(int functionCount, String baseName, boolean useLoaderFunction) {
48  functions = new LispObject[functionCount];
49  this.baseName = baseName;
50  if(useLoaderFunction) {
51      try {
52    this.loader = (LispObject) loadClass(baseName + "_0").newInstance();
53      } catch(Exception e) {
54    //e.printStackTrace();
55    Debug.trace("useLoaderFunction = true but couldn't fully init FASL loader, will fall back to reflection!");
56      }
57  }
58    }
59
60    protected Class<?> findClass(String name) throws ClassNotFoundException {
61  try {
62      byte[] b = getFunctionClassBytes(name);
63      return defineClass(name, b, 0, b.length);
64  } catch(Throwable e) { //TODO handle this better, readFunctionBytes uses Debug.assert() but should return null
65      e.printStackTrace();
66      if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
67      throw new ClassNotFoundException("Function class not found: " + name, e);
68  }
69    }
70
71    public byte[] getFunctionClassBytes(String name) {
72  Pathname pathname = new Pathname(name.substring("org/armedbear/lisp/".length()) + ".cls");
73  return readFunctionBytes(pathname);
74    }
75   
76    public byte[] getFunctionClassBytes(Class<?> functionClass) {
77  return getFunctionClassBytes(functionClass.getName());
78    }
79
80    public byte[] getFunctionClassBytes(Function f) {
81  byte[] b = getFunctionClassBytes(f.getClass());
82  f.setClassBytes(b);
83  return b;
84    }
85
86    public LispObject loadFunction(int fnNumber) {
87  try {
88      //Function name is fnIndex + 1
89      LispObject o = (LispObject) loadClass(baseName + "_" + (fnNumber + 1)).newInstance();
90      functions[fnNumber] = o;
91      return o;
92  } catch(Exception e) {
93      e.printStackTrace();
94      if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
95      throw new RuntimeException(e);
96  }
97    }
98   
99    public LispObject getFunction(int fnNumber) {
100  if(fnNumber >= functions.length) {
101      return error(new LispError("Compiled function not found: " + baseName + "_" + (fnNumber + 1) + " " + Symbol.LOAD_TRUENAME.symbolValue()));
102  }
103  LispObject o = functions[fnNumber];
104  if(o == null) {
105      if(loader != null) {
106    loader.execute(boxedThis, Fixnum.getInstance(fnNumber));
107    return functions[fnNumber];
108      } else { //Fallback to reflection
109    return loadFunction(fnNumber);
110      }
111  } else {
112      return o;
113  }
114    }
115
116    public LispObject putFunction(int fnNumber, LispObject fn) {
117  functions[fnNumber] = fn;
118  return fn;
119    }
120
121    private static final Primitive MAKE_FASL_CLASS_LOADER = new pf_make_fasl_class_loader();
122    private static final class pf_make_fasl_class_loader extends Primitive {
123  pf_make_fasl_class_loader() {
124            super("make-fasl-class-loader", PACKAGE_SYS, false, "function-count base-name");
125        }
126
127        @Override
128        public LispObject execute(LispObject functionCount, LispObject baseName) {
129            return execute(functionCount, baseName, T);
130        }
131
132        @Override
133        public LispObject execute(LispObject functionCount, LispObject baseName, LispObject init) {
134            return new FaslClassLoader(functionCount.intValue(), baseName.getStringValue(), init != NIL).boxedThis;
135        }
136    };
137
138    private static final Primitive GET_FASL_FUNCTION = new pf_get_fasl_function();
139    private static final class pf_get_fasl_function extends Primitive {
140  pf_get_fasl_function() {
141            super("get-fasl-function", PACKAGE_SYS, false, "loader function-number");
142        }
143
144        @Override
145        public LispObject execute(LispObject loader, LispObject fnNumber) {
146            FaslClassLoader l = (FaslClassLoader) loader.javaInstance(FaslClassLoader.class);
147      return l.getFunction(fnNumber.intValue());
148        }
149    };
150
151}
Note: See TracBrowser for help on using the repository browser.