<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.9">
<title>CallingFromSMLToCFunctionPointer</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>CallingFromSMLToCFunctionPointer</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Just as MLton can <a href="CallingFromSMLToC">directly call C functions</a>, it
is possible to make indirect function calls; that is, function calls
through a function pointer.  MLton extends the syntax of SML to allow
expressions like the following:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import * : MLton.Pointer.t -&gt; real * char -&gt; int;</pre>
</div></div>
<div class="paragraph"><p>This expression denotes a function of type</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="nn">MLton</span><span class="p">.</span><span class="nn">Pointer</span><span class="p">.</span><span class="n">t</span> <span class="p">-&gt;</span> <span class="n">real</span> <span class="n">*</span> <span class="n">char</span> <span class="p">-&gt;</span> <span class="n">int</span>
</pre></div></div></div>
<div class="paragraph"><p>whose behavior is implemented by calling the C function at the address
denoted by the <span class="monospaced">MLton.Pointer.t</span> argument, and supplying the C
function two arguments, a <span class="monospaced">double</span> and an <span class="monospaced">int</span>.  The C function
pointer may be obtained, for example, by the dynamic linking loader
(<span class="monospaced">dlopen</span>, <span class="monospaced">dlsym</span>, &#8230;).</p></div>
<div class="paragraph"><p>The general form of an indirect <span class="monospaced">_import</span> expression is:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import * attr... : cPtrTy -&gt; cFuncTy;</pre>
</div></div>
<div class="paragraph"><p>The type and the semicolon are not optional.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>This example uses <span class="monospaced">dlopen</span> and friends (imported using normal
<span class="monospaced">_import</span>) to dynamically load the math library (<span class="monospaced">libm</span>) and call the
<span class="monospaced">cos</span> function. Suppose <span class="monospaced">iimport.sml</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span>
</pre></div></div></div>
<div class="paragraph"><p>Compile and run <span class="monospaced">iimport.sml</span>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -default-ann 'allowFFI true'    \
        -target-link-opt linux -ldl     \
        -target-link-opt solaris -ldl   \
         iimport.sml
% iimport
    Math.cos(2.0) = ~0.416146836547
libm.so::cos(2.0) = ~0.416146836547</pre>
</div></div>
<div class="paragraph"><p>This example also shows the <span class="monospaced">-target-link-opt</span> option, which uses the
switch when linking only when on the specified platform.  Compile with
<span class="monospaced">-verbose 1</span> to see in more detail what&#8217;s being passed to <span class="monospaced">gcc</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_download">Download</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/iimport.sml"><span class="monospaced">iimport.sml</span></a>
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
