| 1 | /* |
|---|
| 2 | * MemoryClassLoader.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2011 Erik Huelsmann |
|---|
| 5 | * Copyright (C) 2010 Alessio Stalla |
|---|
| 6 | * $Id: MemoryClassLoader.java 14363 2013-01-29 08:36:14Z mevenson $ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 21 | * |
|---|
| 22 | * As a special exception, the copyright holders of this library give you |
|---|
| 23 | * permission to link this library with independent modules to produce an |
|---|
| 24 | * executable, regardless of the license terms of these independent |
|---|
| 25 | * modules, and to copy and distribute the resulting executable under |
|---|
| 26 | * terms of your choice, provided that you also meet, for each linked |
|---|
| 27 | * independent module, the terms and conditions of the license of that |
|---|
| 28 | * module. An independent module is a module which is not derived from |
|---|
| 29 | * or based on this library. If you modify this library, you may extend |
|---|
| 30 | * this exception to your version of the library, but you are not |
|---|
| 31 | * obligated to do so. If you do not wish to do so, delete this |
|---|
| 32 | * exception statement from your version. |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | package org.armedbear.lisp; |
|---|
| 36 | |
|---|
| 37 | import static org.armedbear.lisp.Lisp.*; |
|---|
| 38 | |
|---|
| 39 | import java.util.*; |
|---|
| 40 | |
|---|
| 41 | public class MemoryClassLoader extends JavaClassLoader { |
|---|
| 42 | |
|---|
| 43 | private final HashMap<String, JavaObject> hashtable = new HashMap<String, JavaObject>(); |
|---|
| 44 | private final JavaObject boxedThis = new JavaObject(this); |
|---|
| 45 | private final String internalNamePrefix; |
|---|
| 46 | |
|---|
| 47 | public MemoryClassLoader() { |
|---|
| 48 | this("org/armedbear/lisp/"); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public MemoryClassLoader(String internalNamePrefix) { |
|---|
| 52 | this.internalNamePrefix = internalNamePrefix; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public MemoryClassLoader(JavaClassLoader parent) { |
|---|
| 56 | super(parent); |
|---|
| 57 | this.internalNamePrefix = ""; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | @Override |
|---|
| 61 | protected Class<?> loadClass(String name, boolean resolve) |
|---|
| 62 | throws ClassNotFoundException { |
|---|
| 63 | /* First we check if we should load the class ourselves, |
|---|
| 64 | * allowing the default handlers to kick in if we don't... |
|---|
| 65 | * |
|---|
| 66 | * This strategy eliminates ClassNotFound exceptions inside |
|---|
| 67 | * the inherited loadClass() eliminated ~80k exceptions during |
|---|
| 68 | * Maxima compilation. Generally, creation of an exception object |
|---|
| 69 | * is a pretty heavy operation, because it processes the call stack, |
|---|
| 70 | * which - in ABCL - is pretty deep, most of the time. |
|---|
| 71 | */ |
|---|
| 72 | if (hashtable.containsKey(name)) { |
|---|
| 73 | String internalName = internalNamePrefix + name; |
|---|
| 74 | Class<?> c = this.findLoadedClass(internalName); |
|---|
| 75 | |
|---|
| 76 | if (c == null) { |
|---|
| 77 | c = findClass(name); |
|---|
| 78 | } |
|---|
| 79 | if (c != null) { |
|---|
| 80 | if (resolve) { |
|---|
| 81 | resolveClass(c); |
|---|
| 82 | } |
|---|
| 83 | return c; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if (checkPreCompiledClassLoader) { |
|---|
| 88 | Class<?> c = findPrecompiledClassOrNull(name); |
|---|
| 89 | if (c != null) { |
|---|
| 90 | return c; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // Fall through to our super's default handling |
|---|
| 95 | return super.loadClass(name, resolve); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | @Override |
|---|
| 99 | protected Class<?> findClass(String name) throws ClassNotFoundException { |
|---|
| 100 | try { |
|---|
| 101 | if (checkPreCompiledClassLoader) { |
|---|
| 102 | Class<?> c = findPrecompiledClassOrNull(name); |
|---|
| 103 | if (c != null) |
|---|
| 104 | return c; |
|---|
| 105 | } |
|---|
| 106 | byte[] b = getFunctionClassBytes(name); |
|---|
| 107 | return defineLispClass(name, b, 0, b.length); |
|---|
| 108 | } catch(Throwable e) { //TODO handle this better, readFunctionBytes uses Debug.assert() but should return null |
|---|
| 109 | e.printStackTrace(); |
|---|
| 110 | if(e instanceof ControlTransfer) { throw (ControlTransfer) e; } |
|---|
| 111 | throw new ClassNotFoundException("Function class not found: " + name, e); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public byte[] getFunctionClassBytes(String name) { |
|---|
| 116 | if (hashtable.containsKey(name)) { |
|---|
| 117 | return (byte[])hashtable.get(name).javaInstance(); |
|---|
| 118 | } |
|---|
| 119 | return super.getFunctionClassBytes(name); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public LispObject loadFunction(String name) { |
|---|
| 123 | try { |
|---|
| 124 | Class clz = loadClass(name); |
|---|
| 125 | Function f = (Function) clz.newInstance(); |
|---|
| 126 | getFunctionClassBytes(f); //as a side effect it sets them |
|---|
| 127 | return f; |
|---|
| 128 | } catch(Throwable e) { |
|---|
| 129 | if(e instanceof ControlTransfer) { throw (ControlTransfer) e; } |
|---|
| 130 | Debug.trace(e); |
|---|
| 131 | return error(new LispError("Compiled function can't be loaded: " + name + " from memory")); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | private static final Primitive MAKE_MEMORY_CLASS_LOADER = new pf_make_memory_class_loader(); |
|---|
| 136 | private static final class pf_make_memory_class_loader extends Primitive { |
|---|
| 137 | pf_make_memory_class_loader() { |
|---|
| 138 | super("make-memory-class-loader", PACKAGE_SYS, false); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | @Override |
|---|
| 142 | public LispObject execute() { |
|---|
| 143 | return new MemoryClassLoader().boxedThis; |
|---|
| 144 | } |
|---|
| 145 | }; |
|---|
| 146 | |
|---|
| 147 | private static final Primitive PUT_MEMORY_FUNCTION = new pf_put_memory_function(); |
|---|
| 148 | private static final class pf_put_memory_function extends Primitive { |
|---|
| 149 | pf_put_memory_function() { |
|---|
| 150 | super("put-memory-function", PACKAGE_SYS, false, "loader class-name class-bytes"); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | @Override |
|---|
| 154 | public LispObject execute(LispObject loader, LispObject className, LispObject classBytes) { |
|---|
| 155 | MemoryClassLoader l = (MemoryClassLoader) loader.javaInstance(MemoryClassLoader.class); |
|---|
| 156 | return (LispObject)l.hashtable.put(className.getStringValue(), (JavaObject)classBytes); |
|---|
| 157 | } |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | private static final Primitive GET_MEMORY_FUNCTION = new pf_get_memory_function(); |
|---|
| 161 | private static final class pf_get_memory_function extends Primitive { |
|---|
| 162 | pf_get_memory_function() { |
|---|
| 163 | super("get-memory-function", PACKAGE_SYS, false, "loader class-name"); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | @Override |
|---|
| 167 | public LispObject execute(LispObject loader, LispObject name) { |
|---|
| 168 | MemoryClassLoader l = (MemoryClassLoader) loader.javaInstance(MemoryClassLoader.class); |
|---|
| 169 | return l.loadFunction(name.getStringValue()); |
|---|
| 170 | } |
|---|
| 171 | }; |
|---|
| 172 | } |
|---|
| 173 | |
|---|