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

Last change on this file was 13605, checked in by ehuelsmann, 14 years ago

Add properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/*
2 * JavaClassLoader.java
3 *
4 * Copyright (C) 2010 Alessio Stalla
5 * $Id: FaslClassLoader.java 13605 2011-09-20 20:27:05Z 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 java.io.InputStream;
37import static org.armedbear.lisp.Lisp.*;
38
39
40public class FaslClassLoader extends JavaClassLoader {
41
42    private final String baseName;
43    private final JavaObject boxedThis = new JavaObject(this);
44
45    public FaslClassLoader(String baseName) {
46        this.baseName = baseName;
47    }
48
49    @Override
50    protected Class<?> loadClass(String name, boolean resolve)
51            throws ClassNotFoundException {
52        /* First we check if we should load the class ourselves,
53         * allowing the default handlers to kick in if we don't...
54         *
55         * This strategy eliminates ClassNotFound exceptions inside
56         * the inherited loadClass() eliminated ~80k exceptions during
57         * Maxima compilation. Generally, creation of an exception object
58         * is a pretty heavy operation, because it processes the call stack,
59         * which - in ABCL - is pretty deep, most of the time.
60         */
61        if (name.startsWith(baseName + "_")) {
62            String internalName = "org/armedbear/lisp/" + name;
63            Class<?> c = this.findLoadedClass(internalName);
64
65            if (c == null) {
66                c = findClass(name);
67            }
68            if (c != null) {
69                if (resolve) {
70                    resolveClass(c);
71                }
72                return c;
73            }
74        }
75
76        // Fall through to our super's default handling
77        return super.loadClass(name, resolve);
78    }
79
80    @Override
81    protected Class<?> findClass(String name) throws ClassNotFoundException {
82        try {
83            byte[] b = getFunctionClassBytes(name);
84            return defineClass(name, b, 0, b.length);
85        } catch(Throwable e) { //TODO handle this better, readFunctionBytes uses Debug.assert() but should return null
86            e.printStackTrace();
87            if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
88            throw new ClassNotFoundException("Function class not found: " + name, e);
89        }
90    }
91
92    @Override
93    public InputStream getResourceAsStream(String resourceName) {
94      final LispThread thread = LispThread.currentThread();
95
96      Pathname name = new Pathname(resourceName.substring("org/armedbear/lisp/".length()));
97      LispObject truenameFasl = Symbol.LOAD_TRUENAME_FASL.symbolValue(thread);
98      LispObject truename = Symbol.LOAD_TRUENAME.symbolValue(thread);
99     
100      if (truenameFasl instanceof Pathname) {
101          return Pathname.mergePathnames(name, (Pathname)truenameFasl, Keyword.NEWEST)
102                    .getInputStream();
103      } else if (truename instanceof Pathname) {
104          return Pathname.mergePathnames(name, (Pathname) truename, Keyword.NEWEST)
105                  .getInputStream();
106      } else if (!Pathname.truename(name).equals(NIL)) {
107              return name.getInputStream();
108      }
109
110      return null;
111    }
112
113    public byte[] getFunctionClassBytes(String name) {
114        Pathname pathname = new Pathname(name.substring("org/armedbear/lisp/".length()) + ".cls");
115        return readFunctionBytes(pathname);
116    }
117
118    public byte[] getFunctionClassBytes(Class<?> functionClass) {
119        return getFunctionClassBytes(functionClass.getName());
120    }
121
122    public byte[] getFunctionClassBytes(Function f) {
123        byte[] b = getFunctionClassBytes(f.getClass());
124        f.setClassBytes(b);
125        return b;
126    }
127
128    public LispObject loadFunction(int fnNumber) {
129        //Function name is fnIndex + 1
130        String name = baseName + "_" + (fnNumber + 1);
131        try {
132            Function f = (Function) loadClass(name).newInstance();
133            f.setClassBytes(getFunctionClassBytes(name));
134            return f;
135        } catch(Throwable e) {
136            if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
137            Debug.trace(e);
138            return error(new LispError("Compiled function can't be loaded: " + name + " from " + Symbol.LOAD_TRUENAME.symbolValue()));
139        }
140    }
141
142    private static final Primitive MAKE_FASL_CLASS_LOADER = new pf_make_fasl_class_loader();
143    private static final class pf_make_fasl_class_loader extends Primitive {
144        pf_make_fasl_class_loader() {
145            super("make-fasl-class-loader", PACKAGE_SYS, false, "base-name");
146        }
147
148        @Override
149        public LispObject execute(LispObject baseName) {
150            return new FaslClassLoader(baseName.getStringValue()).boxedThis;
151        }
152
153    };
154
155    private static final Primitive GET_FASL_FUNCTION = new pf_get_fasl_function();
156    private static final class pf_get_fasl_function extends Primitive {
157  pf_get_fasl_function() {
158            super("get-fasl-function", PACKAGE_SYS, false, "loader function-number");
159        }
160
161        @Override
162        public LispObject execute(LispObject loader, LispObject fnNumber) {
163            FaslClassLoader l = (FaslClassLoader) loader.javaInstance(FaslClassLoader.class);
164      return l.loadFunction(fnNumber.intValue());
165        }
166    };
167
168}
Note: See TracBrowser for help on using the repository browser.