Where MiXture came from
The implementation of MiXture is based on J.B. Matthews’ Ph.D. dissertation, where he describes the semantics of joining two languages. In his dissertation, he introduces two calculi (for an ML-like and a Scheme-like languages) to formally describe two theories for language interoperability. I won’t go into the details of his dissertation, and in fact I will only deal with the most integrated kind of interoperability: the natural embedding.
JSContext). I’m glad I saw this as it validates this project is not just done from the theoretical point of view I was coming from, but also has some real-word usages :).
Overview of the MiXture API
Let’s take a look at the API MiXture exposes to F# developers wanting to create applications using both languages.
JSContext is a wrapper for V8 contexts.
There are some functions in the module
JSUtils to manipulate
JSContexts, such as
create_context: unit -> JSValue,
set_current_context: JSContext -> unit and
register_values: (string * JSValue) list -> unit.
JSContext seen in F#.
embed and project
The two main functions to work with MiXture are
project, which use values of the type
embed converts any F# value into a
project performs the opposite operation. The type someone could expect for embed would be
'a -> JSValue, however, the type is
obj -> JSValue as embed does not behave polymorphically (it inspects the type of its argument in order to dispatch it to the appropriate converting function).
For the specific case in which we are embedding a polymorphic function, we must use the function
embed_poly_func, as we can only get the polymorphic type of a value using quotations. We’ll see an example of this in a code sample below.
Let’s move to the fun part, some examples that show off some of the features of MiXture:
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6
We can see the use of
embed_poly_func here to embed a polymorphic function.
poly_append is now registered in the current
1 2 3 4 5 6
Free memory management
Another highlight is that MiXture doesn’t require manual memory management as it uses F#’s and V8’s garbage collectors.
Consider the case in which F# has a
JSValue that is a V8 persistent handle. When this
JSValue comes out of scope, MiXture automatically calls
Persistent::MakeWeak on the V8 handle to trigger a callback from the V8’s garbage collector.
Here’s a table of type equivalences for MiXture:
If you are interested in knowing more, here’s my dissertation that goes into all the gory details.
You can also check out the source on GitHub.