Version française
Home     About     Download     Resources     Contact us    
Browse thread
Dynamic loading of bytecode
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pietro Abate <Pietro.Abate@a...>
Subject: Re: [Caml-list] Dynamic loading of bytecode
On Mon, Aug 14, 2006 at 05:21:25PM +0200, Tom wrote:
> How can you load the whole module hiearchy (many modules, dependent on one
> another) with a single command in toploop?

I use these two functions to resolve linear dependencies. 
I'm sure there is a better solution... 

let modules = Hashtbl.create 17;;
let are_loading = Hashtbl.create 17;;

let find_in_path path name =
 let filename = ((String.uncapitalize name) ^ ".cmo") in
 if not (Filename.is_implicit filename) then
 if Sys.file_exists filename then filename else raise Not_found
 else
 begin
 let rec try_dir = function
 | [] -> raise Not_found
 | dir::rem ->
 let fullname = Filename.concat dir filename in
 if Sys.file_exists fullname then fullname
 else try_dir rem
 in try_dir path
 end

let rec load_module modname path =
 try
 Hashtbl.find modules modname
 with
 Not_found ->
 try
 Hashtbl.add modules modname ();
 Hashtbl.add are_loading modname ();
 (* Printf.printf "Loading: %s ..." modname; *)
 Dynlink.loadfile (modname);
 (* print_endline "done."; *)
 Hashtbl.remove are_loading modname
 with
 | Dynlink.Error(Dynlink.Unavailable_unit(depend))
 | Dynlink.Error(
 Dynlink.Linking_error(_,Dynlink.Undefined_global(depend))
 ) ->
 begin
 try
 if Hashtbl.mem are_loading depend
 then failwith ("Crossing with "^depend);
 load_module (find_in_path path depend) path;
 Hashtbl.remove modules modname;
 load_module modname path
 with Not_found ->
 failwith ("Cannot find "
 ^String.lowercase(depend)^" in "^
 (List.fold_left (fun s x -> s^x) " " path))
 end
 | Dynlink.Error(e) -> failwith (Dynlink.error_message e)
;;

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html