Thread model

The bytecode interpreter runs in its own thread which is started once the interpreter has been successfully initialized and is automatically stopped if either It is guaranteed that the JavaScript instructions are executed in a different thread. This means that there must be a special facility that allows both threads to communicate.

In the Net_runtime library there is the function "sleep" that waits until JavaScript wants to talk with the JavaCaml thread and automatically calls the OCaml function that can answer. The function "sleep" is actually an event loop that sequentially replies to all requests coming from the JavaScript thread. You can force "sleep" to continue the program with the function "continue".

The Ocaml functions must have been registered earlier using "register".

The JavaScript thread is suspended until the JavaCaml function has been performed. This might cause user interaction to be blocked while JavaCaml is running.

Example: The JavaCaml part

open Net_runtime

let double ( [| s |] : string array) =
  s ^ s

  "double"            (* under this name JavaScript sees the function *)
  1                   (* function has one parameter (always strings) *)
  double              (* the function itself *)


Example: The JavaScript part

The applet must have a name (given with the NAME parameter). Assumed the name is "javacaml" the function "double" is called this way:

answer = document.javacaml.invoke1("double", parameter)

The number in "invoke1" refers to the number of parameters.