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

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

Repair incorrect last minute (uncompiled) change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/*
2 * MemoryClassLoader.java
3 *
4 * Copyright (C) 2011 Erik Huelsmann
5 * Copyright (C) 2010 Alessio Stalla
6 * $Id: MemoryClassLoader.java 13467 2011-08-12 12:27:14Z ehuelsmann $
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
35package org.armedbear.lisp;
36
37import static org.armedbear.lisp.Lisp.*;
38
39import java.util.*;
40
41public 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
46    public MemoryClassLoader() {
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 (hashtable.containsKey(name)) {
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        return (byte[])hashtable.get(name).javaInstance();
94    }
95
96    public byte[] getFunctionClassBytes(Class<?> functionClass) {
97        return getFunctionClassBytes(functionClass.getName());
98    }
99
100    public byte[] getFunctionClassBytes(Function f) {
101        byte[] b = getFunctionClassBytes(f.getClass());
102        f.setClassBytes(b);
103        return b;
104    }
105
106    public LispObject loadFunction(String name) {
107        try {
108            Function f = (Function) loadClass(name).newInstance();
109            f.setClassBytes(getFunctionClassBytes(name));
110            return f;
111        } catch(Throwable e) {
112            if(e instanceof ControlTransfer) { throw (ControlTransfer) e; }
113            Debug.trace(e);
114            return error(new LispError("Compiled function can't be loaded: " + name + " from memory"));
115        }
116    }
117
118    private static final Primitive MAKE_MEMORY_CLASS_LOADER = new pf_make_memory_class_loader();
119    private static final class pf_make_memory_class_loader extends Primitive {
120        pf_make_memory_class_loader() {
121            super("make-memory-class-loader", PACKAGE_SYS, false);
122        }
123
124        @Override
125        public LispObject execute() {
126            return new MemoryClassLoader().boxedThis;
127        }
128    };
129
130    private static final Primitive PUT_MEMORY_FUNCTION = new pf_put_memory_function();
131    private static final class pf_put_memory_function extends Primitive {
132  pf_put_memory_function() {
133            super("put-memory-function", PACKAGE_SYS, false, "loader class-name class-bytes");
134        }
135
136        @Override
137        public LispObject execute(LispObject loader, LispObject className, LispObject classBytes) {
138            MemoryClassLoader l = (MemoryClassLoader) loader.javaInstance(MemoryClassLoader.class);
139      return (LispObject)l.hashtable.put(className.getStringValue(), (JavaObject)classBytes);
140        }
141    };
142   
143    private static final Primitive GET_MEMORY_FUNCTION = new pf_get_memory_function();
144    private static final class pf_get_memory_function extends Primitive {
145  pf_get_memory_function() {
146            super("get-memory-function", PACKAGE_SYS, false, "loader class-name");
147        }
148
149        @Override
150        public LispObject execute(LispObject loader, LispObject name) {
151            MemoryClassLoader l = (MemoryClassLoader) loader.javaInstance(MemoryClassLoader.class);
152      return l.loadFunction(name.getStringValue());
153        }
154    };
155
156
157}
Note: See TracBrowser for help on using the repository browser.