Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding modules to the standard library #143

Closed
bryanjos opened this issue Dec 28, 2015 · 2 comments
Closed

Adding modules to the standard library #143

bryanjos opened this issue Dec 28, 2015 · 2 comments

Comments

@bryanjos
Copy link
Collaborator

Things are at the point now where the standard library can be defined mostly in Elixir. Here are some steps on adding them.

  • Add the module to the lib/elixir_script/prelude folder.
  • Add an entry to the map created in the build_standard_lib_map function in ElixirScript.Translator.State.
  • If you need some functionality in JavaScript, create it within the src/javascript/core module and then use it within your Elixir module.

ex: I want to create the Tuple module

  • Step 1: Add tuple.ex to the lib/elixir_script/prelude folder
defmodule ElixirScript.Tuple do
...
  • Step 2: Add an entry for Tuple to the map created in the build_standard_lib_map function in ElixirScript.Translator.State.
|> Map.put(Tuple, ElixirScript.Tuple)
  • Step 3: If you need some functionality in JavaScript, create it within the src/javascript/core module and then use it within your Elixir module.

For the Tuple.duplicate I need a make_tuple function in JavaScript so I create it in src/javascript/core/functions.js. I also make sure to add it to the export declaration at the bottom of the file.

function make_tuple(data, size){
  let array = [];

  for (var i = size - 1; i >= 0; i--) {
    array.push(data);
  }

  return new Tuple(...array);
}

Now I use this function in my Tuple.duplicate function in Elixir

  def duplicate(data, size) do
    Elixir.Core.Functions.make_tuple(size, data)
  end

That's pretty much it. If anything is missing or incomplete, please report it.

@bryanjos
Copy link
Collaborator Author

There are still some standard library modules written in JavaScript that need to be converted over. The goal is to get them converted, leaving only the core module in JavaScript. When the compiler compiles the code, it will output all of your application code along with the standard libraries and the core javascript module. From there the starting module for an application is a good place for something like rollup or webpack 2 to bundle and tree shake, leaving only the used code.

@bryanjos
Copy link
Collaborator Author

Closing this as the new strategy should be to share standard library modules with Elixir #154

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant