Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Dynamic linking in CSL?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xleroy@p...>
Subject: Re: Dynamic linking in CSL?

> Is it possible, in CSL, to dynamically load and link a CSL module
> which matches a static signature?

Yes. The current distribution of Caml Special Light includes a dynamic
linking library (in otherlibs/dynlink), originally developed for
implementing applets in the MMM browser, but general enough (I think)
for your needs.

There is some documentation in otherlibs/dynlink/dynlink.mli. I'll try
to summarize the main points below.

First of all, the library provides no mechanism for accessing values
defined by a dynamically-linked module. (That would be hard to do in a
type-safe way.) So, the dynamically-linked module must register
explicitly the functions it exports with the application, e.g. by
storing these functions in mutable data structures. For instance:

        let f x = ...
        let g y = ...
        let _ =
          App.extension_functions :=
            ["f", f; "g", g] @ !App1.extension_functions

Here, "App" is assumed to be a module from the application, that defines

        let extension_functions = ref ([] : (string * (int -> int)) list)

Calling an extension function given its name is a simple "assoc" in
that list.

For this reason, the signature of the dynamically-linked module does
not matter. What matters, for type safety, is the signatures of the
modules from the application that the dynamically-linked module references.
The dynlink library checks that the dynamically-linked module has been
compiled against the same interfaces for these application modules as
the application modules themselves. This ensures type safety.

Say your application is composed of three modules, App App2 App3.
You would initialize the dynamic linker as follows:

        Dynlink.init();
        Dynlink.add_interfaces
          ["Pervasives";"Array";"String";"Char";"App";"App2";"App3"]
          ["/the/sources/of/my/application";
           "/usr/local/lib/caml-special-light"]

The add_interfaces function sets the list of modules that
dynamically-linked code can reference. Here, we allow access to the
three application modules, plus a number of standard library modules.
The second argument is a list of directories where the .cmi files (the
compiled signatures) for these modules are found.

Then, linking a file foo.cmo is as easy as

        Dynlink.loadfile "foo.cmo"

There are some extra features in the library, but I hope you get the
general idea. Write caml-light@pauillac.inria.fr for more info.

Regards,

- Xavier Leroy