| 1 | /* |
|---|
| 2 | * JavaClassLoader.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2010 Alessio Stalla |
|---|
| 5 | * $Id: FaslClassLoader.java 14015 2012-07-23 11:58:34Z mevenson $ |
|---|
| 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 | |
|---|
| 34 | package org.armedbear.lisp; |
|---|
| 35 | |
|---|
| 36 | import java.io.InputStream; |
|---|
| 37 | import static org.armedbear.lisp.Lisp.*; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | public 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 = name.replace(".", "/"); |
|---|
| 63 | if (!internalName.contains("/")) internalName = "org/armedbear/lisp/" + internalName; |
|---|
| 64 | Class<?> c = this.findLoadedClass(internalName); |
|---|
| 65 | |
|---|
| 66 | if (c == null && checkPreCompiledClassLoader) { |
|---|
| 67 | c = findPrecompiledClassOrNull(name); |
|---|
| 68 | // Oh, we have to return here so we don't become the owning class loader? |
|---|
| 69 | if (c != null) |
|---|
| 70 | return c; |
|---|
| 71 | } |
|---|
| 72 | if (c == null) { |
|---|
| 73 | c = findClass(name); |
|---|
| 74 | } |
|---|
| 75 | if (c != null) { |
|---|
| 76 | if (resolve) { |
|---|
| 77 | resolveClass(c); |
|---|
| 78 | } |
|---|
| 79 | return c; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // Fall through to our super's default handling |
|---|
| 84 | return super.loadClass(name, resolve); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | @Override |
|---|
| 88 | protected Class<?> findClass(String name) throws ClassNotFoundException { |
|---|
| 89 | try { |
|---|
| 90 | if (checkPreCompiledClassLoader) { |
|---|
| 91 | Class<?> c = findPrecompiledClassOrNull(name); |
|---|
| 92 | if (c != null) |
|---|
| 93 | return c; |
|---|
| 94 | } |
|---|
| 95 | byte[] b = getFunctionClassBytes(name); |
|---|
| 96 | return defineLispClass(name, b, 0, b.length); |
|---|
| 97 | } catch(Throwable e) { //TODO handle this better, readFunctionBytes uses Debug.assert() but should return null |
|---|
| 98 | e.printStackTrace(); |
|---|
| 99 | if(e instanceof ControlTransfer) { throw (ControlTransfer) e; } |
|---|
| 100 | throw new ClassNotFoundException("Function class not found: " + name, e); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | @Override |
|---|
| 105 | public InputStream getResourceAsStream(String resourceName) { |
|---|
| 106 | final LispThread thread = LispThread.currentThread(); |
|---|
| 107 | |
|---|
| 108 | Pathname name = new Pathname(resourceName.substring("org/armedbear/lisp/".length())); |
|---|
| 109 | LispObject truenameFasl = Symbol.LOAD_TRUENAME_FASL.symbolValue(thread); |
|---|
| 110 | LispObject truename = Symbol.LOAD_TRUENAME.symbolValue(thread); |
|---|
| 111 | |
|---|
| 112 | if (truenameFasl instanceof Pathname) { |
|---|
| 113 | return Pathname.mergePathnames(name, (Pathname)truenameFasl, Keyword.NEWEST) |
|---|
| 114 | .getInputStream(); |
|---|
| 115 | } else if (truename instanceof Pathname) { |
|---|
| 116 | return Pathname.mergePathnames(name, (Pathname) truename, Keyword.NEWEST) |
|---|
| 117 | .getInputStream(); |
|---|
| 118 | } else if (!Pathname.truename(name).equals(NIL)) { |
|---|
| 119 | return name.getInputStream(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | return null; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | public LispObject loadFunction(int fnNumber) { |
|---|
| 126 | //Function name is fnIndex + 1 |
|---|
| 127 | String name = baseName + "_" + (fnNumber + 1); |
|---|
| 128 | try { |
|---|
| 129 | Class clz = loadClass(name); |
|---|
| 130 | Function f = (Function) clz.newInstance(); |
|---|
| 131 | if (clz.getClassLoader() instanceof JavaClassLoader) { |
|---|
| 132 | // Don't do this for system classes (though probably dont need this for other classes) |
|---|
| 133 | f.setClassBytes(getFunctionClassBytes(name)); |
|---|
| 134 | } |
|---|
| 135 | return f; |
|---|
| 136 | } catch(Throwable e) { |
|---|
| 137 | if(e instanceof ControlTransfer) { throw (ControlTransfer) e; } |
|---|
| 138 | Debug.trace(e); |
|---|
| 139 | return error(new LispError("Compiled function can't be loaded: " + name + " from " + Symbol.LOAD_TRUENAME.symbolValue())); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | private static final Primitive MAKE_FASL_CLASS_LOADER = new pf_make_fasl_class_loader(); |
|---|
| 144 | private static final class pf_make_fasl_class_loader extends Primitive { |
|---|
| 145 | pf_make_fasl_class_loader() { |
|---|
| 146 | super("make-fasl-class-loader", PACKAGE_SYS, false, "base-name"); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | @Override |
|---|
| 150 | public LispObject execute(LispObject baseName) { |
|---|
| 151 | return new FaslClassLoader(baseName.getStringValue()).boxedThis; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | private static final Primitive GET_FASL_FUNCTION = new pf_get_fasl_function(); |
|---|
| 157 | private static final class pf_get_fasl_function extends Primitive { |
|---|
| 158 | pf_get_fasl_function() { |
|---|
| 159 | super("get-fasl-function", PACKAGE_SYS, false, "loader function-number"); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | @Override |
|---|
| 163 | public LispObject execute(LispObject loader, LispObject fnNumber) { |
|---|
| 164 | FaslClassLoader l = (FaslClassLoader) loader.javaInstance(FaslClassLoader.class); |
|---|
| 165 | return l.loadFunction(fnNumber.intValue()); |
|---|
| 166 | } |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | } |
|---|