1212
Update documentation for FFI module · elixirscript/elixirscript@f4d4587 · GitHub
Skip to content

Commit f4d4587

Browse files
committed
Update documentation for FFI module
1 parent ce9d1d3 commit f4d4587

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/elixir_script/ffi.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
defmodule ElixirScript.FFI do
22
@moduledoc """
33
The foreign function interface for interoperability with JavaScript.
4+
5+
Define foreign modules with `use ElixirScript.FFI`.
6+
Next to define functions for the foreign module, use the `foreign` macro.
7+
8+
Here is an example of a foreign module for a JSON module
9+
10+
```elixir
11+
defmodule MyApp.JSON do
12+
use ElixirScript.FFI
13+
14+
foreign stringify(map)
15+
foreign parse(string)
16+
end
17+
```
18+
19+
Foreign modules must have an equivalent JavaScript module.
20+
ElixirScript expects JavaScript modules to be in the `priv/elixir_script` directory.
21+
These modules will be copied to the output directory upon compilation.
22+
23+
In the example, a JavaScript file must be at `priv/elixir_script/my_app/json.js`.
24+
It looks like this
25+
```javascript
26+
export default {
27+
stringify: JSON.stringify,
28+
parse: JSON.parse
29+
}
30+
```
31+
32+
The JavaScript module must export a default object with the foreign functions defined in the Elixir module
433
"""
534

635
defmacro __using__(opts) do

test/support/main.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
defmodule Main do
22
def start(:normal, [callback]) do
33
callback.("started")
4-
Agent.start(fn() -> nil end)
5-
Data.JSON.stringify(%{})
64
end
75
end

0 commit comments

Comments
 (0)