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

Last change on this file was 13149, checked in by astalla, 15 years ago

Keep sys::make-fasl-class-loader API compatible to avoid changing the FASL version number.

File size: 5.9 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 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    public byte[] getFunctionClassBytes(String name) {
93        Pathname pathname = new Pathname(name.substring("org/armedbear/lisp/".length()) + ".cls");
94        return readFunctionBytes(pathname);
95    }
96
97    public byte[] getFunctionClassBytes(Class<?> functionClass) {
98        return getFunctionClassBytes(functionClass.getName());
99    }
100
101    public byte[] getFunctionClassBytes(Function f) {
102        byte[] b = getFunctionClassBytes(f.getClass());
103        f.setClassBytes(b);
104        return b;
105    }
106
107    public LispObject loadFunction(int fnNumber) {
108        try {
109            //Function name is fnIndex + 1
110            String name = baseName + "_" + (fnNumber + 1);
111            Function f = (Function) loadClass(name).newInstance();
112            f.setClassBytes(getFunctionClassBytes(name));
113            return f;
114        } catch(Exception e) {
115            if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
116            Debug.trace(e);
117            return error(new LispError("Compiled function can't be loaded: " + baseName + "_" + (fnNumber + 1) + " " + Symbol.LOAD_TRUENAME.symbolValue()));
118        }
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, "base-name");
125        }
126
127        @Override
128        public LispObject execute(LispObject baseName) {
129            return new FaslClassLoader(baseName.getStringValue()).boxedThis;
130        }
131
132        @Override
133        //TODO delete this next time the fasl version is bumbed
134        public LispObject execute(LispObject unused1, LispObject baseName, LispObject unused2) {
135            return execute(baseName);
136        }
137
138    };
139
140    private static final Primitive GET_FASL_FUNCTION = new pf_get_fasl_function();
141    private static final class pf_get_fasl_function extends Primitive {
142  pf_get_fasl_function() {
143            super("get-fasl-function", PACKAGE_SYS, false, "loader function-number");
144        }
145
146        @Override
147        public LispObject execute(LispObject loader, LispObject fnNumber) {
148            FaslClassLoader l = (FaslClassLoader) loader.javaInstance(FaslClassLoader.class);
149      return l.loadFunction(fnNumber.intValue());
150        }
151    };
152
153}
Note: See TracBrowser for help on using the repository browser.