Skip to content

Commit 4666746

Browse files
committed
Document global option on FFI module
Add name option. The value is used as the module name when creating the function call in JavaScript
1 parent 8f61127 commit 4666746

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

lib/elixir_script/ffi.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ defmodule ElixirScript.FFI do
2929
parse: JSON.parse
3030
}
3131
```
32+
33+
`ElixirScript.FFI` takes the following options
34+
* `global`: If the module is defined in the global state or not. If this is set to `true`,
35+
nothing is imported and instead ElixirScript will use the name of the module to call a module and
36+
function in the global scope.
37+
* `name`: Only applicable with `global` is set to `true`. This will use the name defined here
38+
instead of the module name for calling modules and functions in the global scope
3239
"""
3340

3441
defmacro __using__(opts) do
@@ -37,7 +44,7 @@ defmodule ElixirScript.FFI do
3744
Module.register_attribute __MODULE__, :__foreign_info__, persist: true
3845
@__foreign_info__ %{
3946
path: Macro.underscore(__MODULE__),
40-
name: Enum.join(Module.split(__MODULE__), "_"),
47+
name: unquote(Keyword.get(opts, :name, nil)),
4148
global: unquote(Keyword.get(opts, :global, false))
4249
}
4350
end
@@ -54,7 +61,7 @@ defmodule ElixirScript.FFI do
5461
"""
5562
defmacro defexternal({name, _, args}) do
5663
args = Enum.map(args, fn
57-
{:\\, meta0, [{name, meta, atom}, value]}->
64+
{:\\, meta0, [{name, meta, atom}, value]} ->
5865
name = String.to_atom("_" <> Atom.to_string(name))
5966
{:\\, meta0, [{name, meta, atom}, value]}
6067

lib/elixir_script/passes/find_used_modules.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ defmodule ElixirScript.FindUsedModules do
2626
end
2727

2828
defp walk_module(module, %{attributes: [__foreign_info__: %{path: path, name: name, global: global}]} = info, pid) do
29-
path = if global, do: nil, else: path
30-
name = if global, do: module, else: name
29+
{name, path} = if global do
30+
name = if name, do: name, else: module
31+
path = nil
32+
{name, path}
33+
else
34+
name = Enum.join(Module.split(module), "_")
35+
{name, path}
36+
end
3137

3238
ModuleState.put_javascript_module(pid, module, name, path)
3339
ModuleState.put_module(pid, module, info)

lib/elixir_script/passes/translate/forms/remote.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ defmodule ElixirScript.Translate.Forms.Remote do
9999

100100
defp process_js_module_name(module, state) do
101101
case ModuleState.get_js_module_name(state.pid, module) do
102-
name when is_atom(name) ->
103-
members = Module.split(module)
104-
Identifier.make_namespace_members(members)
105-
name ->
102+
name when is_binary(name) ->
106103
J.identifier(name)
104+
name when is_atom(name) ->
105+
case to_string(name) do
106+
"Elixir." <> _ ->
107+
members = Module.split(module)
108+
Identifier.make_namespace_members(members)
109+
x ->
110+
J.identifier(x)
111+
end
107112
end
108113
end
109114

lib/elixir_script/passes/translate/module.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ defmodule ElixirScript.Translate.Module do
1414
end
1515

1616
def compile(module, %{attributes: [__foreign_info__: %{path: path, name: name, global: global}]}, pid) do
17-
path = if global, do: nil, else: path
18-
name = if global, do: module, else: name
17+
{name, path} = if global do
18+
name = if name, do: name, else: module
19+
path = nil
20+
{name, path}
21+
else
22+
name = Enum.join(Module.split(module), "_")
23+
{name, path}
24+
end
1925

2026
ModuleState.put_javascript_module(pid, module, name, path)
2127

0 commit comments

Comments
 (0)