001: /*
002: * Autoload.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: * $Id: Autoload.java,v 1.196 2004/09/09 12:44:21 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public class Autoload extends Function {
025: protected final String fileName;
026: protected final String className;
027:
028: protected Autoload(Symbol symbol) {
029: super (symbol);
030: fileName = null;
031: className = null;
032: symbol.setBuiltInFunction(false);
033: }
034:
035: protected Autoload(Symbol symbol, String fileName, String className) {
036: super (symbol);
037: this .fileName = fileName;
038: this .className = className;
039: symbol.setBuiltInFunction(false);
040: }
041:
042: public static void autoload(String symbolName, String className) {
043: autoload(PACKAGE_CL, symbolName, className);
044: }
045:
046: public static void autoload(Package pkg, String symbolName,
047: String className) {
048: autoload(pkg, symbolName, className, false);
049: }
050:
051: public static void autoload(Package pkg, String symbolName,
052: String className, boolean exported) {
053: Symbol symbol = intern(symbolName.toUpperCase(), pkg);
054: if (pkg != PACKAGE_CL && exported) {
055: try {
056: pkg.export(symbol);
057: } catch (ConditionThrowable t) {
058: Debug.assertTrue(false);
059: }
060: }
061: if (symbol.getSymbolFunction() == null)
062: symbol.setSymbolFunction(new Autoload(symbol, null,
063: "org.armedbear.lisp.".concat(className)));
064: }
065:
066: public void load() throws ConditionThrowable {
067: if (className != null) {
068: final LispThread thread = LispThread.currentThread();
069: final Environment oldDynEnv = thread
070: .getDynamicEnvironment();
071: int loadDepth = Fixnum.getInt(_LOAD_DEPTH_.symbolValue());
072: thread.bindSpecial(_LOAD_DEPTH_, new Fixnum(++loadDepth));
073: try {
074: if (_AUTOLOAD_VERBOSE_.symbolValue(thread) != NIL) {
075: final String prefix = Load
076: .getLoadVerbosePrefix(loadDepth);
077: Stream out = getStandardOutput();
078: out._writeString(prefix);
079: out._writeString(" Autoloading ");
080: out._writeString(className);
081: out._writeLine(" ...");
082: out._finishOutput();
083: long start = System.currentTimeMillis();
084: Class.forName(className);
085: long elapsed = System.currentTimeMillis() - start;
086: out._writeString(prefix);
087: out._writeString(" Autoloaded ");
088: out._writeString(className);
089: out._writeString(" (");
090: out._writeString(String
091: .valueOf(((float) elapsed) / 1000));
092: out._writeLine(" seconds)");
093: out._finishOutput();
094: } else
095: Class.forName(className);
096: } catch (ClassNotFoundException e) {
097: e.printStackTrace();
098: } finally {
099: thread.setDynamicEnvironment(oldDynEnv);
100: }
101: } else
102: Load.loadSystemFile(getFileName(), true);
103: }
104:
105: protected final String getFileName() {
106: if (fileName != null)
107: return fileName;
108: return getSymbol().getName().toLowerCase();
109: }
110:
111: public final int getFunctionalType() {
112: return FTYPE_AUTOLOAD;
113: }
114:
115: public String writeToString() throws ConditionThrowable {
116: StringBuffer sb = new StringBuffer("#<AUTOLOAD ");
117: sb.append(getSymbol().writeToString());
118: sb.append(" \"");
119: if (className != null) {
120: int index = className.lastIndexOf('.');
121: if (index >= 0)
122: sb.append(className.substring(index + 1));
123: else
124: sb.append(className);
125: sb.append(".class");
126: } else
127: sb.append(getFileName());
128: sb.append("\">");
129: return sb.toString();
130: }
131:
132: private static final Primitive AUTOLOAD = new Primitive("autoload",
133: PACKAGE_EXT, true) {
134: public LispObject execute(LispObject first)
135: throws ConditionThrowable {
136: if (first instanceof Symbol) {
137: Symbol symbol = (Symbol) first;
138: symbol.setSymbolFunction(new Autoload(symbol));
139: return T;
140: }
141: if (first instanceof Cons) {
142: for (LispObject list = first; list != NIL; list = list
143: .cdr()) {
144: Symbol symbol = checkSymbol(list.car());
145: symbol.setSymbolFunction(new Autoload(symbol));
146: }
147: return T;
148: }
149: return signal(new TypeError(first));
150: }
151:
152: public LispObject execute(LispObject first, LispObject second)
153: throws ConditionThrowable {
154: final String fileName = second.getStringValue();
155: if (first instanceof Symbol) {
156: Symbol symbol = (Symbol) first;
157: symbol.setSymbolFunction(new Autoload(symbol, fileName,
158: null));
159: return T;
160: }
161: if (first instanceof Cons) {
162: for (LispObject list = first; list != NIL; list = list
163: .cdr()) {
164: Symbol symbol = checkSymbol(list.car());
165: symbol.setSymbolFunction(new Autoload(symbol,
166: fileName, null));
167: }
168: return T;
169: }
170: return signal(new TypeError(first));
171: }
172: };
173:
174: // ### resolve
175: // Force autoload to be resolved.
176: private static final Primitive1 RESOLVE = new Primitive1("resolve",
177: PACKAGE_EXT, true, "symbol") {
178: public LispObject execute(LispObject arg)
179: throws ConditionThrowable {
180: Symbol symbol = checkSymbol(arg);
181: LispObject fun = symbol.getSymbolFunction();
182: if (fun instanceof Autoload) {
183: Autoload autoload = (Autoload) fun;
184: autoload.load();
185: return autoload.getSymbol().getSymbolFunction();
186: }
187: return fun;
188: }
189: };
190:
191: // ### autoloadp
192: private static final Primitive1 AUTOLOADP = new Primitive1(
193: "autoloadp", PACKAGE_EXT, true, "symbol") {
194: public LispObject execute(LispObject arg)
195: throws ConditionThrowable {
196: if (arg instanceof Symbol) {
197: if (arg.getSymbolFunction() instanceof Autoload)
198: return T;
199: }
200: return NIL;
201: }
202: };
203:
204: static {
205: autoload("acos", "MathFunctions");
206: autoload("acosh", "MathFunctions");
207: autoload("arithmetic-error-operands", "ArithmeticError");
208: autoload("arithmetic-error-operation", "ArithmeticError");
209: autoload("ash", "ash");
210: autoload("asin", "MathFunctions");
211: autoload("asinh", "MathFunctions");
212: autoload("atan", "MathFunctions");
213: autoload("atanh", "MathFunctions");
214: autoload("broadcast-stream-streams", "BroadcastStream");
215: autoload("ceiling", "ceiling");
216: autoload("cell-error-name", "cell_error_name");
217: autoload("char", "StringFunctions");
218: autoload("char-equal", "CharacterFunctions");
219: autoload("char-greaterp", "CharacterFunctions");
220: autoload("char-lessp", "CharacterFunctions");
221: autoload("char-not-greaterp", "CharacterFunctions");
222: autoload("char-not-lessp", "CharacterFunctions");
223: autoload("char<=", "CharacterFunctions");
224: autoload("char=", "CharacterFunctions");
225: autoload("clrhash", "HashTable");
226: autoload("concatenated-stream-streams", "ConcatenatedStream");
227: autoload("cos", "MathFunctions");
228: autoload("cosh", "MathFunctions");
229: autoload("delete-file", "delete_file");
230: autoload("delete-package", "PackageFunctions");
231: autoload("describe", "describe");
232: autoload("echo-stream-input-stream", "EchoStream");
233: autoload("echo-stream-output-stream", "EchoStream");
234: autoload("exp", "MathFunctions");
235: autoload("file-author", "file_author");
236: autoload("file-error-pathname", "file_error_pathname");
237: autoload("file-length", "file_length");
238: autoload("file-string-length", "file_string_length");
239: autoload("file-write-date", "file_write_date");
240: autoload("float-sign", "float_sign");
241: autoload("floor", "floor");
242: autoload("ftruncate", "ftruncate");
243: autoload("get-internal-real-time", "Time");
244: autoload("get-internal-run-time", "Time");
245: autoload("get-output-stream-string", "StringOutputStream");
246: autoload("get-properties", "get_properties");
247: autoload("get-universal-time", "Time");
248: autoload("gethash", "HashTable");
249: autoload("hash-table-count", "HashTable");
250: autoload("hash-table-p", "HashTable");
251: autoload("hash-table-rehash-size", "HashTable");
252: autoload("hash-table-rehash-threshold", "HashTable");
253: autoload("hash-table-size", "HashTable");
254: autoload("hash-table-test", "HashTable");
255: autoload("import", "PackageFunctions");
256: autoload("input-stream-p", "input_stream_p");
257: autoload("interactive-stream-p", "interactive_stream_p");
258: autoload("last", "last");
259: autoload("lisp-implementation-type", "lisp_implementation_type");
260: autoload("lisp-implementation-version",
261: "lisp_implementation_version");
262: autoload("list-all-packages", "PackageFunctions");
263: autoload("listen", "listen");
264: autoload("load-logical-pathname-translations",
265: "LogicalPathname");
266: autoload("log", "MathFunctions");
267: autoload("logand", "logand");
268: autoload("logandc1", "logandc1");
269: autoload("logandc2", "logandc2");
270: autoload("logbitp", "logbitp");
271: autoload("logcount", "logcount");
272: autoload("logeqv", "logeqv");
273: autoload("logical-pathname", "LogicalPathname");
274: autoload("logical-pathname-translations", "LogicalPathname");
275: autoload("logior", "logior");
276: autoload("lognand", "lognand");
277: autoload("lognor", "lognor");
278: autoload("lognot", "lognot");
279: autoload("logorc1", "logorc1");
280: autoload("logorc2", "logorc2");
281: autoload("logtest", "logtest");
282: autoload("logxor", "logxor");
283: autoload("long-site-name", "SiteName");
284: autoload("machine-instance", "SiteName");
285: autoload("machine-type", "machine_type");
286: autoload("machine-version", "machine_version");
287: autoload("make-broadcast-stream", "BroadcastStream");
288: autoload("make-concatenated-stream", "ConcatenatedStream");
289: autoload("make-echo-stream", "EchoStream");
290: autoload("make-string-input-stream", "StringInputStream");
291: autoload("make-synonym-stream", "SynonymStream");
292: autoload("mod", "mod");
293: autoload("open-stream-p", "open_stream_p");
294: autoload("output-stream-p", "output_stream_p");
295: autoload("package-error-package", "PackageError");
296: autoload("package-name", "PackageFunctions");
297: autoload("package-nicknames", "PackageFunctions");
298: autoload("package-shadowing-symbols", "PackageFunctions");
299: autoload("package-use-list", "PackageFunctions");
300: autoload("package-used-by-list", "PackageFunctions");
301: autoload("packagep", "PackageFunctions");
302: autoload("peek-char", "peek_char");
303: autoload("print-not-readable-object", "PrintNotReadable");
304: autoload("probe-file", "probe_file");
305: autoload("read-char-no-hang", "read_char_no_hang");
306: autoload("read-delimited-list", "read_delimited_list");
307: autoload("rem", "rem");
308: autoload("remhash", "HashTable");
309: autoload("rename-package", "PackageFunctions");
310: autoload("room", "room");
311: autoload("shadow", "PackageFunctions");
312: autoload("shadowing-import", "PackageFunctions");
313: autoload("short-site-name", "SiteName");
314: autoload("simple-condition-format-arguments", "SimpleCondition");
315: autoload("simple-condition-format-control", "SimpleCondition");
316: autoload("simple-string-p", "StringFunctions");
317: autoload("sin", "MathFunctions");
318: autoload("sinh", "MathFunctions");
319: autoload("software-type", "software_type");
320: autoload("software-version", "software_version");
321: autoload("sqrt", "MathFunctions");
322: autoload("stream-element-type", "stream_element_type");
323: autoload("stream-error-stream", "StreamError");
324: autoload("stream-external-format", "stream_external_format");
325: autoload("stringp", "StringFunctions");
326: autoload("sxhash", "HashTable");
327: autoload("synonym-stream-symbol", "SynonymStream");
328: autoload("tan", "MathFunctions");
329: autoload("tanh", "MathFunctions");
330: autoload("truename", "probe_file");
331: autoload("truncate", "truncate");
332: autoload("unbound-slot-instance", "unbound_slot_instance");
333: autoload("unexport", "PackageFunctions");
334: autoload("unuse-package", "PackageFunctions");
335: autoload(PACKAGE_EXT, "arglist", "arglist", true);
336: autoload(PACKAGE_EXT, "assq", "assq", true);
337: autoload(PACKAGE_EXT, "assql", "assql", true);
338: autoload(PACKAGE_EXT, "file-directory-p", "probe_file", true);
339: autoload(PACKAGE_EXT, "gc", "gc", true);
340: autoload(PACKAGE_EXT, "get-mutex", "Mutex", true);
341: autoload(PACKAGE_EXT, "mailbox-empty-p", "Mailbox", true);
342: autoload(PACKAGE_EXT, "mailbox-peek", "Mailbox", true);
343: autoload(PACKAGE_EXT, "mailbox-read", "Mailbox", true);
344: autoload(PACKAGE_EXT, "mailbox-send", "Mailbox", true);
345: autoload(PACKAGE_EXT, "make-mailbox", "Mailbox", true);
346: autoload(PACKAGE_EXT, "make-mutex", "Mutex", true);
347: autoload(PACKAGE_EXT, "make-thread-lock", "ThreadLock", true);
348: autoload(PACKAGE_EXT, "probe-directory", "probe_file", true);
349: autoload(PACKAGE_EXT, "release-mutex", "Mutex", true);
350: autoload(PACKAGE_EXT, "simple-string-fill", "StringFunctions");
351: autoload(PACKAGE_EXT, "simple-string-search", "StringFunctions");
352: autoload(PACKAGE_EXT, "string-input-stream-current",
353: "StringInputStream", true);
354: autoload(PACKAGE_EXT, "string-position", "StringFunctions");
355: autoload(PACKAGE_EXT, "thread-lock", "ThreadLock", true);
356: autoload(PACKAGE_EXT, "thread-unlock", "ThreadLock", true);
357: autoload(PACKAGE_JAVA, "%jnew-proxy", "JProxy");
358: autoload(PACKAGE_JAVA, "%jnew-runtime-class", "RuntimeClass");
359: autoload(PACKAGE_JAVA, "%jredefine-method", "RuntimeClass");
360: autoload(PACKAGE_JAVA, "%jregister-handler", "JHandler");
361: autoload(PACKAGE_JAVA, "%load-java-class-from-byte-array",
362: "RuntimeClass");
363: autoload(PACKAGE_PROF, "%start-profiler", "Profiler", true);
364: autoload(PACKAGE_PROF, "stop-profiler", "Profiler", true);
365: autoload(PACKAGE_SYS, "%%string=", "StringFunctions");
366: autoload(PACKAGE_SYS, "%adjust-array", "adjust_array");
367: autoload(PACKAGE_SYS, "%defpackage", "PackageFunctions");
368: autoload(PACKAGE_SYS, "%make-array", "make_array");
369: autoload(PACKAGE_SYS, "%make-condition", "make_condition");
370: autoload(PACKAGE_SYS, "%make-hash-table", "HashTable");
371: autoload(PACKAGE_SYS, "%make-server-socket",
372: "make_server_socket");
373: autoload(PACKAGE_SYS, "%make-socket", "make_socket");
374: autoload(PACKAGE_SYS, "%make-string", "StringFunctions");
375: autoload(PACKAGE_SYS, "%make-string-output-stream",
376: "StringOutputStream");
377: autoload(PACKAGE_SYS, "%nstring-capitalize", "StringFunctions");
378: autoload(PACKAGE_SYS, "%nstring-downcase", "StringFunctions");
379: autoload(PACKAGE_SYS, "%nstring-upcase", "StringFunctions");
380: autoload(PACKAGE_SYS, "%run-shell-command", "ShellCommand");
381: autoload(PACKAGE_SYS, "%server-socket-close",
382: "server_socket_close");
383: autoload(PACKAGE_SYS, "%set-arglist", "arglist");
384: autoload(PACKAGE_SYS, "%set-char", "StringFunctions");
385: autoload(PACKAGE_SYS, "%set-class-direct-slots", "SlotClass");
386: autoload(PACKAGE_SYS, "%set-class-slots", "SlotClass");
387: autoload(PACKAGE_SYS, "%set-function-info", "function_info");
388: autoload(PACKAGE_SYS,
389: "%set-generic-function-discriminating-function",
390: "GenericFunction");
391: autoload(PACKAGE_SYS, "%set-instance-ref", "StandardObject");
392: autoload(PACKAGE_SYS, "%set-logical-pathname-translations",
393: "LogicalPathname");
394: autoload(PACKAGE_SYS, "%socket-accept", "socket_accept");
395: autoload(PACKAGE_SYS, "%socket-close", "socket_close");
396: autoload(PACKAGE_SYS, "%socket-stream", "socket_stream");
397: autoload(PACKAGE_SYS, "%string-capitalize", "StringFunctions");
398: autoload(PACKAGE_SYS, "%string-downcase", "StringFunctions");
399: autoload(PACKAGE_SYS, "%string-equal", "StringFunctions");
400: autoload(PACKAGE_SYS, "%string-greaterp", "StringFunctions");
401: autoload(PACKAGE_SYS, "%string-lessp", "StringFunctions");
402: autoload(PACKAGE_SYS, "%string-not-equal", "StringFunctions");
403: autoload(PACKAGE_SYS, "%string-not-greaterp", "StringFunctions");
404: autoload(PACKAGE_SYS, "%string-not-lessp", "StringFunctions");
405: autoload(PACKAGE_SYS, "%string-upcase", "StringFunctions");
406: autoload(PACKAGE_SYS, "%string/=", "StringFunctions");
407: autoload(PACKAGE_SYS, "%string<", "StringFunctions");
408: autoload(PACKAGE_SYS, "%string<=", "StringFunctions");
409: autoload(PACKAGE_SYS, "%string=", "StringFunctions");
410: autoload(PACKAGE_SYS, "%string>", "StringFunctions");
411: autoload(PACKAGE_SYS, "%string>=", "StringFunctions");
412: autoload(PACKAGE_SYS, "%time", "Time");
413: autoload(PACKAGE_SYS, "allocate-slot-storage", "StandardObject");
414: autoload(PACKAGE_SYS, "allocate-std-instance", "StandardObject");
415: autoload(PACKAGE_SYS, "class-direct-slots", "SlotClass");
416: autoload(PACKAGE_SYS, "class-slots", "SlotClass");
417: autoload(PACKAGE_SYS, "condition-report", "Condition");
418: autoload(PACKAGE_SYS, "create-new-file", "create_new_file");
419: autoload(PACKAGE_SYS, "function-info", "function_info");
420: autoload(PACKAGE_SYS, "default-time-zone", "Time");
421: autoload(PACKAGE_SYS,
422: "generic-function-discriminating-function",
423: "GenericFunction");
424: autoload(PACKAGE_SYS, "hash-table-entries", "HashTable");
425: autoload(PACKAGE_SYS, "instance-ref", "StandardObject");
426: autoload(PACKAGE_SYS, "layout-class", "Layout");
427: autoload(PACKAGE_SYS, "layout-length", "Layout");
428: autoload(PACKAGE_SYS, "make-case-frob-stream", "CaseFrobStream");
429: autoload(PACKAGE_SYS, "make-file-stream", "FileStream");
430: autoload(PACKAGE_SYS, "make-fill-pointer-output-stream",
431: "FillPointerOutputStream");
432: autoload(PACKAGE_SYS, "make-instance-standard-class",
433: "StandardClass");
434: autoload(PACKAGE_SYS, "make-layout", "Layout");
435: autoload(PACKAGE_SYS, "make-structure-class", "StructureClass");
436: autoload(PACKAGE_SYS, "make-symbol-macro", "SymbolMacro");
437: autoload(PACKAGE_SYS, "puthash", "HashTable");
438: autoload(PACKAGE_SYS, "simple-list-remove-duplicates",
439: "simple_list_remove_duplicates");
440: autoload(PACKAGE_SYS, "std-instance-slots", "StandardObject");
441: }
442: }
|