Skip to content

Add Foreign Function Interface for JavaScript interoperability #317

@bryanjos

Description

@bryanjos

The idea is to have something similar to how PureScript handles FFI.

Will need to make an ElixirScript.FFI module. This module will contain a foreign macro for defining functions to call in a JavaScript module.

The corresponding JavaScript modules must be placed in priv/elixir_script/. The convention will be that for each . in a module name, this corresponds to a folder level.

An Example, I want to interop with JavaScript in order to use JSON functions. I would make a module like so

defmodule MyApp.JSON do
  use ElixirScript.FFI

  foreign stringify(map)
  foreign parse(string)
end

I have a JavaScript module defined at priv/elixir_script/my_app/json.js that looks like this

export default {
  stringify: JSON.stringify,
  parse: JSON.parse
}

If my code calls functions on MyApp.JSON, priv/elixir_script/my_app/json.js will be copied to the output folder. In our bundled output file, there will be an import statement added to include the JavaScript module above. All calls to MyApp.JSON will be translated to calls to the JavaScript module.

Note: Will need to reach into different app's priv folder. This can be done with :code.priv_dir(app). Can get the current app's name with Mix.Project.config()[:app]. Can get dependency app names with Mix.Project.deps_paths()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions