34
35:- module(pce_autoload,
36 [ pce_autoload/2
37 , pce_autoload_all/0
38 ]). 39
40:- use_module(pce_boot(pce_principal)). 41:- use_module(pce_boot(pce_realise),
42 [ pce_realise_class/1,
43 pce_prolog_class/1
44 ]). 45:- require([ is_absolute_file_name/1
46 , atomic_list_concat/2
47 , absolute_file_name/3
48 , get/3
49 ]). 50
51:- dynamic
52 autoload/2.
60pce_autoload(Class, PathAlias) :- 61 functor(PathAlias, _, 1),
62 !,
63 retractall(autoload(Class, _)),
64 assert(autoload(Class, PathAlias)).
65pce_autoload(Class, Abs) :-
66 is_absolute_file_name(Abs),
67 !,
68 absolute_file_name(Abs, Canonical),
69 retractall(autoload(Class, _)),
70 assert(autoload(Class, Canonical)).
71pce_autoload(Class, Local) :-
72 prolog_load_context(directory, Dir),
73 atomic_list_concat([Dir, /, Local], File),
74 pce_host:property(file_extensions(Exts)),
75 absolute_file_name(File,
76 [ extensions(Exts),
77 access(exist)
78 ], Abs),
79 retractall(autoload(Class, _)),
80 assert(autoload(Class, Abs)).
87pce_autoload_all :-
88 autoload(Class, File),
89 \+ get(@classes, member, Class, _),
90 \+ pce_prolog_class(Class),
91 ensure_loaded(user:File),
92 fail.
93pce_autoload_all.
94
95
96register_handler :-
97 send(@pce?exception_handlers, append(
98 attribute(undefined_class,
99 message(@prolog, call, trap_autoload, @arg1)))).
100
101:- initialization
102 register_handler. 103
104pce_ifhostproperty(prolog(swi),
105 (:- '$hide'(trap_autoload/1)),
106 (notrace(G) :- G)).
107
108trap_autoload(Class) :-
109 notrace(do_trap_autoload(Class)).
110
111do_trap_autoload(Class) :-
112 pce_realise_class(Class),
113 !.
114do_trap_autoload(Class) :-
115 autoload(Class, File),
116 load_files(user:File,
117 [ autoload(true)
118 ]),
119 pce_realise_class(Class)