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