Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading module with dynlink re-initialises top-level values
    primarykey
    data
    text
    <p>I have a problem where I have a global hashtable, and then I load a .cma file with Dynlink, which registers a function in the hashtable.</p> <p>However, the behaviour I seem to be see is that when the module is dynamically linked, all the global bindings get re-initialised, such that my hashtable is empty.</p> <p>E.g.:</p> <pre><code>Table.extensions : (string, string -&gt; string) Hashtbl.t Extensions.load : unit -&gt; unit (* loads the specified .cma files *) Extensions.register : string -&gt; (string -&gt; string) -&gt; unit (* adds entry to Table.extensions, prints name of extension registered *) Main: let () = Extensions.load (); Hashtbl.iter (fun x _ -&gt; print_endline x) Table.extensions; Printf.printf "%d extensions loaded\n" (Hashtbl.length Table.extensions) </code></pre> <p>My program loads one .cma file, so it <em>should</em> print:</p> <pre><code>Registered extension 'test' test 1 extensions loaded </code></pre> <p>Instead I get:</p> <pre><code>Registered extension 'test' 0 extensions loaded </code></pre> <p>I've been fighting this for several hours now; no matter how I refactor my code, I get no closer to a working solution.</p> <p>EDIT: Extensions.load:</p> <pre><code>Dynlink.allow_unsafe_modules true;; let load () = try let exts = Sys.readdir "exts" in Array.iter begin fun name -&gt; try Dynlink.loadfile (Filename.concat "exts" name); Printf.printf "Loaded %s\n" name; with | Dynlink.Error error -&gt; print_endline (Dynlink.error_message error) | exn -&gt; print_endline (Printexc.to_string exn) end exts with _ -&gt; () </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload