Packages

Boa-powered JavaScript runtimes for Elixir with BEAM dispatch hooks.

Current section

Files

Jump to
boam doc Boam.html
Raw

doc/Boam.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.40.1">
<meta name="project" content="boam v0.1.4">
<title>Boam — boam v0.1.4</title>
<link rel="stylesheet" href="dist/html-elixir-YJO4MOOW.css" />
<script defer src="dist/sidebar_items-CF3A9E4F.js"></script>
<script defer src="docs_config.js"></script>
<script defer src="dist/html-YU4BZFVS.js"></script>
</head>
<body>
<script>(()=>{var t="ex_doc:settings",e="dark";var o="dark",s="light";var E="sidebar_state",n="closed";var r="sidebar_width";var a="sidebar-open";var i=new URLSearchParams(window.location.search),S=i.get("theme")||JSON.parse(localStorage.getItem(t)||"{}").theme;(S===o||S!==s&&window.matchMedia("(prefers-color-scheme: dark)").matches)&&document.body.classList.add(e);var d=sessionStorage.getItem(E),A=d!==n&&!window.matchMedia(`screen and (max-width: ${768}px)`).matches;document.body.classList.toggle(a,A);var c=sessionStorage.getItem(r);c&&document.body.style.setProperty("--sidebarWidth",`${c}px`);var p=/(Macintosh|iPhone|iPad|iPod)/.test(window.navigator.userAgent);document.documentElement.classList.toggle("apple-os",p);})();
</script>
<div class="body-wrapper">
<button id="sidebar-menu" class="sidebar-button sidebar-toggle" aria-label="toggle sidebar" aria-controls="sidebar">
<i class="ri-menu-line ri-lg" title="Collapse/expand sidebar"></i>
</button>
<nav id="sidebar" class="sidebar">
<div class="sidebar-header">
<div class="sidebar-projectInfo">
<div>
<a href="readme.html" class="sidebar-projectName" translate="no">
boam
</a>
<div class="sidebar-projectVersion" translate="no">
v0.1.4
</div>
</div>
</div>
<ul id="sidebar-list-nav" class="sidebar-list-nav" role="tablist" data-extras="Pages"></ul>
</div>
</nav>
<output role="status" id="toast"></output>
<main class="content page-module" id="main" data-type="modules">
<div id="content" class="content-inner">
<div class="top-search">
<div class="search-settings">
<form class="search-bar" action="search.html">
<label class="search-label">
<span class="sr-only">Search documentation of boam</span>
<div class="search-input-wrapper">
<input name="q" type="text" class="search-input" placeholder="Press / to search" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
<button type="button" tabindex="-1" class="search-close-button" aria-hidden="true">
<i class="ri-close-line ri-lg" title="Cancel search"></i>
</button>
</div>
</label>
</form>
<div class="autocomplete">
</div>
<div class="engine-selector" data-multiple="false">
<button type="button" class="engine-button" aria-label="Select search engine" aria-haspopup="true" aria-expanded="false">
<i class="ri-search-2-line" aria-hidden="true"></i>
<span class="engine-name">Default</span>
<i class="ri-arrow-down-s-line" aria-hidden="true"></i>
</button>
<div class="engine-dropdown" hidden role="menu">
<button type="button"
class="engine-option"
data-engine-url="search.html?q="
role="menuitemradio"
aria-checked="true">
<span class="name">Default</span>
<span class="help">In-browser search</span>
</button>
</div>
</div>
<button class="icon-settings display-settings">
<i class="ri-settings-3-line"></i>
<span class="sr-only">Settings</span>
</button>
</div>
</div>
<div id="top-content">
<div class="heading-with-actions top-heading">
<h1>
<span translate="no">Boam</span>
<small class="app-vsn" translate="no">(boam v0.1.4)</small>
</h1>
<a href="Boam.md" title="Copy Markdown (hold Ctrl/Cmd and click it to open as Markdown)" class="copy-markdown icon-action" rel="help">
<i class="ri-markdown-line" aria-hidden="true"></i>
<span class="sr-only">Copy Markdown</span>
</a>
</div>
<section id="moduledoc">
<p>High-level entrypoint for running JavaScript through Boa from Elixir.</p><p><a href="Boam.html"><code class="inline">Boam</code></a> wraps a dedicated Boa runtime behind a <a href="https://hexdocs.pm/elixir/GenServer.html"><code class="inline">GenServer</code></a>. Each runtime owns a
single Rust worker thread because Boa <code class="inline">Context</code> values are not thread-safe.</p><p>JavaScript code can call back into Elixir with <code class="inline">beam.call(name, ...args)</code>. The
dispatch itself happens through a separate BEAM process, which keeps the
runtime process focused on driving the JavaScript engine.</p><p>You can either:</p><ul><li>pass your own dispatcher with <code class="inline">:dispatcher</code></li><li>install JavaScript shims with <code class="inline">:js_expose</code></li><li>register Elixir handlers with <code class="inline">:exports</code> when Boam manages the dispatcher</li><li>use legacy combined config with <code class="inline">:expose</code></li><li>run startup JavaScript with <code class="inline">:prelude</code></li><li>generate shim code manually with <a href="Boam.JS.html#export_prelude/1"><code class="inline">Boam.JS.export_prelude/1</code></a></li></ul><h2 id="module-quick-start" class="section-heading"><a href="#module-quick-start" class="hover-link"><i class="ri-link-m" aria-hidden="true"></i></a><span class="text">Quick Start</span></h2><pre><code class="makeup elixir" translate="no"><span class="p" data-group-id="9295013952-1">{</span><span class="ss">:ok</span><span class="p">,</span><span class="w"> </span><span class="c">_dispatcher</span><span class="p" data-group-id="9295013952-1">}</span><span class="w"> </span><span class="o">=</span><span class="w">
</span><span class="nc">MyApp.JsDispatcher</span><span class="o">.</span><span class="n">start_link</span><span class="p" data-group-id="9295013952-2">(</span><span class="ss">name</span><span class="p">:</span><span class="w"> </span><span class="nc">MyApp.JsDispatcher</span><span class="p" data-group-id="9295013952-2">)</span><span class="w">
</span><span class="p" data-group-id="9295013952-3">{</span><span class="ss">:ok</span><span class="p">,</span><span class="w"> </span><span class="n">runtime</span><span class="p" data-group-id="9295013952-3">}</span><span class="w"> </span><span class="o">=</span><span class="w">
</span><span class="nc">Boam</span><span class="o">.</span><span class="n">start_link</span><span class="p" data-group-id="9295013952-4">(</span><span class="w">
</span><span class="ss">dispatcher</span><span class="p">:</span><span class="w"> </span><span class="nc">MyApp.JsDispatcher</span><span class="p">,</span><span class="w">
</span><span class="ss">js_expose</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="9295013952-5">%{</span><span class="ss">math</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="9295013952-6">%{</span><span class="ss">sum</span><span class="p">:</span><span class="w"> </span><span class="no">true</span><span class="p" data-group-id="9295013952-6">}</span><span class="p" data-group-id="9295013952-5">}</span><span class="w">
</span><span class="p" data-group-id="9295013952-4">)</span><span class="w">
</span><span class="nc">Boam</span><span class="o">.</span><span class="n">eval</span><span class="p" data-group-id="9295013952-7">(</span><span class="n">runtime</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;1 + 2&quot;</span><span class="p" data-group-id="9295013952-7">)</span><span class="w">
</span><span class="c1">#=&gt; {:ok, 3}</span><span class="w">
</span><span class="nc">Boam</span><span class="o">.</span><span class="n">eval</span><span class="p" data-group-id="9295013952-8">(</span><span class="n">runtime</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;math.sum(2, 3)&quot;</span><span class="p" data-group-id="9295013952-8">)</span><span class="w">
</span><span class="c1">#=&gt; {:ok, 5}</span></code></pre><p>The built-in dispatcher is available for simpler cases:</p><pre><code class="makeup elixir" translate="no"><span class="p" data-group-id="8084930632-1">{</span><span class="ss">:ok</span><span class="p">,</span><span class="w"> </span><span class="n">runtime</span><span class="p" data-group-id="8084930632-1">}</span><span class="w"> </span><span class="o">=</span><span class="w">
</span><span class="nc">Boam</span><span class="o">.</span><span class="n">start_link</span><span class="p" data-group-id="8084930632-2">(</span><span class="w">
</span><span class="ss">exports</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="8084930632-3">%{</span><span class="w">
</span><span class="s">&quot;console.log&quot;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k" data-group-id="8084930632-4">fn</span><span class="w"> </span><span class="p" data-group-id="8084930632-5">[</span><span class="n">message</span><span class="p" data-group-id="8084930632-5">]</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="s">&quot;logged: </span><span class="si" data-group-id="8084930632-6">#{</span><span class="n">message</span><span class="si" data-group-id="8084930632-6">}</span><span class="s">&quot;</span><span class="w"> </span><span class="k" data-group-id="8084930632-4">end</span><span class="w">
</span><span class="p" data-group-id="8084930632-3">}</span><span class="p">,</span><span class="w">
</span><span class="ss">js_expose</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="8084930632-7">%{</span><span class="w">
</span><span class="ss">console</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="8084930632-8">%{</span><span class="w">
</span><span class="ss">log</span><span class="p">:</span><span class="w"> </span><span class="no">true</span><span class="w">
</span><span class="p" data-group-id="8084930632-8">}</span><span class="w">
</span><span class="p" data-group-id="8084930632-7">}</span><span class="w">
</span><span class="p" data-group-id="8084930632-2">)</span><span class="w">
</span><span class="nc">Boam</span><span class="o">.</span><span class="n">eval</span><span class="p" data-group-id="8084930632-9">(</span><span class="n">runtime</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;console.log(&#39;hello&#39;)&quot;</span><span class="p" data-group-id="8084930632-9">)</span><span class="w">
</span><span class="c1">#=&gt; {:ok, &quot;logged: hello&quot;}</span></code></pre><h2 id="module-value-bridge" class="section-heading"><a href="#module-value-bridge" class="hover-link"><i class="ri-link-m" aria-hidden="true"></i></a><span class="text">Value Bridge</span></h2><p>Values crossing between JavaScript and Elixir are intentionally restricted to
JSON-compatible data:</p><ul><li><code class="inline">null</code> maps to <code class="inline">nil</code></li><li>booleans, numbers, strings, arrays, and objects round-trip normally</li><li>top-level JavaScript <code class="inline">undefined</code> is returned as <code class="inline">:undefined</code></li><li><code class="inline">undefined</code> cannot be passed through <code class="inline">beam.call/1</code></li></ul><p>Returned JavaScript objects are serialized through Boa's JSON conversion, so
object keys come back to Elixir as strings.</p><h2 id="module-architecture" class="section-heading"><a href="#module-architecture" class="hover-link"><i class="ri-link-m" aria-hidden="true"></i></a><span class="text">Architecture</span></h2><ul><li><a href="Boam.Runtime.html"><code class="inline">Boam.Runtime</code></a> owns the Rust runtime resource</li><li><a href="Boam.Dispatcher.html"><code class="inline">Boam.Dispatcher</code></a> receives <code class="inline">beam.call(...)</code> requests</li><li><a href="Boam.JS.html"><code class="inline">Boam.JS</code></a> generates JavaScript shim preludes</li><li><a href="Boam.html"><code class="inline">Boam</code></a> provides the small public entrypoint for the common case</li></ul>
</section>
</div>
<section id="summary" class="details-list">
<h2 class="section-heading">
<a class="hover-link" href="#summary">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<span class="text">Summary</span>
</h2>
<div class="summary-types summary">
<h3>
<a href="#types">Types</a>
</h3>
<div class="summary-row">
<div class="summary-signature">
<a href="#t:start_option/0" data-no-tooltip="" translate="no">start_option()</a>
</div>
</div>
</div>
<div class="summary-functions summary">
<h3>
<a href="#functions">Functions</a>
</h3>
<div class="summary-row">
<div class="summary-signature">
<a href="#dispatcher/1" data-no-tooltip="" translate="no">dispatcher(runtime)</a>
</div>
<div class="summary-synopsis"><p>Returns the dispatcher pid attached to a runtime.</p></div>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#eval/2" data-no-tooltip="" translate="no">eval(runtime, source)</a>
</div>
<div class="summary-synopsis"><p>Evaluates JavaScript source code inside a running runtime.</p></div>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#expose_js/2" data-no-tooltip="" translate="no">expose_js(runtime, tree)</a>
</div>
<div class="summary-synopsis"><p>Installs JavaScript shim functions on <code class="inline">globalThis</code>.</p></div>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#notify_expose_js/2" data-no-tooltip="" translate="no">notify_expose_js(runtime, tree)</a>
</div>
<div class="summary-synopsis"><p>Queues JavaScript shim installation through an asynchronous message.</p></div>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#start_link/1" data-no-tooltip="" translate="no">start_link(opts \\ [])</a>
</div>
<div class="summary-synopsis"><p>Starts a Boa runtime process.</p></div>
</div>
</div>
</section>
<section id="types" class="details-list">
<h2 class="section-heading">
<a class="hover-link" href="#types">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<span class="text">Types</span>
</h2>
<div class="types-list">
<section class="detail" id="t:start_option/0">
<div class="detail-header">
<a href="#t:start_option/0" class="detail-link" data-no-tooltip="" aria-label="Link to this type">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">start_option()</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@type</span> start_option() ::
{:name, <a href="https://hexdocs.pm/elixir/GenServer.html#t:name/0">GenServer.name</a>()}
| {:dispatcher, <a href="https://hexdocs.pm/elixir/GenServer.html#t:server/0">GenServer.server</a>()}
| {:exports, <a href="https://hexdocs.pm/elixir/typespecs.html#basic-types">map</a>() | <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">keyword</a>()}
| {:js_expose, <a href="Boam.JS.html#t:export_tree/0">Boam.JS.export_tree</a>()}
| {:expose, <a href="https://hexdocs.pm/elixir/typespecs.html#basic-types">map</a>() | <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">keyword</a>()}
| {:prelude, <a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>() | [<a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>()]}
| {:fallback, (<a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>(), <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">list</a>() -&gt; <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">term</a>())}
| {:dispatch_timeout, <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">timeout</a>()}</pre>
</div>
</section>
</section>
</div>
</section>
<section id="functions" class="details-list">
<h2 class="section-heading">
<a class="hover-link" href="#functions">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<span class="text">Functions</span>
</h2>
<div class="functions-list">
<section class="detail" id="dispatcher/1">
<div class="detail-header">
<a href="#dispatcher/1" class="detail-link" data-no-tooltip="" aria-label="Link to this function">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">dispatcher(runtime)</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@spec</span> dispatcher(<a href="https://hexdocs.pm/elixir/GenServer.html#t:server/0">GenServer.server</a>()) :: <a href="https://hexdocs.pm/elixir/typespecs.html#basic-types">pid</a>()</pre>
</div>
<p>Returns the dispatcher pid attached to a runtime.</p>
</section>
</section>
<section class="detail" id="eval/2">
<div class="detail-header">
<a href="#eval/2" class="detail-link" data-no-tooltip="" aria-label="Link to this function">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">eval(runtime, source)</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@spec</span> eval(<a href="https://hexdocs.pm/elixir/GenServer.html#t:server/0">GenServer.server</a>(), <a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>()) :: {:ok, <a href="https://hexdocs.pm/elixir/typespecs.html#built-in-types">term</a>()} | {:error, <a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>()}</pre>
</div>
<p>Evaluates JavaScript source code inside a running runtime.</p><p>This is a convenience wrapper around <a href="Boam.Runtime.html#eval/2"><code class="inline">Boam.Runtime.eval/2</code></a>.</p>
</section>
</section>
<section class="detail" id="expose_js/2">
<div class="detail-header">
<a href="#expose_js/2" class="detail-link" data-no-tooltip="" aria-label="Link to this function">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">expose_js(runtime, tree)</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@spec</span> expose_js(<a href="https://hexdocs.pm/elixir/GenServer.html#t:server/0">GenServer.server</a>(), <a href="Boam.JS.html#t:export_tree/0">Boam.JS.export_tree</a>()) ::
:ok | {:error, <a href="https://hexdocs.pm/elixir/String.html#t:t/0">String.t</a>()}</pre>
</div>
<p>Installs JavaScript shim functions on <code class="inline">globalThis</code>.</p>
</section>
</section>
<section class="detail" id="notify_expose_js/2">
<div class="detail-header">
<a href="#notify_expose_js/2" class="detail-link" data-no-tooltip="" aria-label="Link to this function">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">notify_expose_js(runtime, tree)</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@spec</span> notify_expose_js(<a href="https://hexdocs.pm/elixir/GenServer.html#t:server/0">GenServer.server</a>(), <a href="Boam.JS.html#t:export_tree/0">Boam.JS.export_tree</a>()) :: :ok</pre>
</div>
<p>Queues JavaScript shim installation through an asynchronous message.</p>
</section>
</section>
<section class="detail" id="start_link/1">
<span id="start_link/0"></span>
<div class="detail-header">
<a href="#start_link/1" class="detail-link" data-no-tooltip="" aria-label="Link to this function">
<i class="ri-link-m" aria-hidden="true"></i>
</a>
<div class="heading-with-actions">
<h1 class="signature" translate="no">start_link(opts \\ [])</h1>
</div>
</div>
<section class="docstring">
<div class="specs">
<pre translate="no"><span class="attribute">@spec</span> start_link([<a href="#t:start_option/0">start_option</a>()]) :: <a href="https://hexdocs.pm/elixir/GenServer.html#t:on_start/0">GenServer.on_start</a>()</pre>
</div>
<p>Starts a Boa runtime process.</p><p>This is a convenience wrapper around <a href="Boam.Runtime.html#start_link/1"><code class="inline">Boam.Runtime.start_link/1</code></a>.</p><p>See <a href="Boam.Runtime.html"><code class="inline">Boam.Runtime</code></a> for the full option contract.</p>
</section>
</section>
</div>
</section>
<footer class="footer">
<p>
<span class="line">
<a href="https://hex.pm/packages/boam/0.1.4">Hex Package</a>
<a href="https://preview.hex.pm/preview/boam/0.1.4">Hex Preview</a>
</span>
<span class="line">
<button class="a-main footer-button display-quick-switch" title="Go to package docs">
Go to package docs
</button>
<a href="llms.txt" target="_blank">
View llms.txt
</a>
<a href="boam.epub">
Download ePub version
</a>
</span>
</p>
<p class="built-using">
Built using
<a href="https://github.com/elixir-lang/ex_doc" title="ExDoc" target="_blank" rel="help noopener" translate="no">ExDoc</a> (v0.40.1) for the
<a href="https://elixir-lang.org" title="Elixir" target="_blank" translate="no">Elixir programming language</a>
</p>
</footer>
</div>
</main>
</div>
</body>
</html>