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

feat(global) make huff_core wasm compatible and add a JS binding #232

Merged
merged 2 commits into from Feb 26, 2023

Conversation

kanewallmann
Copy link
Contributor

Overview

With some minor tweaks and shims, huff_core can be compiled to a wasm target and then be used with JavaScript which even works in the browser.

  1. Remove unused dependency prop-test which is not wasm compatible
  2. Shim for into_par_iter() for creating parallel iterators. Not supported on wasm but can just fallback to a regular iterator.
  3. SystemTime::now panics on wasm as it's not implemented so use a u64 unix timestamp on wasm target instead.

The biggest change required was to refactor out the file system stuff and create an alternative "in memory" version which loads files from a HashMap instead of from disk.

With that all working, I put together a new module huff_js which includes JavaScript bindings so you can interface with the compiler via a JSON interface similar to standard json in solc.

This would make Huff more accessible to integrate with web-based tools like https://www.evm.codes/ which is why I started looking at this in the first place. It would be awesome to have Huff available on there.

Example JS usage:

const { compile } = require('./pkg/huffc.js')

let files = {
    "add.huff": "#define function add(uint256,uint256) nonpayable returns (uint256)\n" +
        "\n" +
        "#define macro MAIN() = {\n" +
        "   // Load our numbers from calldata and add them together.\n" +
        "   0x04 calldataload // [number1]\n" +
        "   0x24 calldataload // [number2]\n" +
        "   add               // [number1+number2]\n" +
        "   // Return our new number.\n" +
        "   0x00 mstore // Store our number in memory.\n" +
        "   0x20 0x00 return // Return it.\n" +
        "}\n"
}

const result = compile({
    files,
    sources: ['add.huff'],
    // constructor_args: "..."
});

console.log(result)

Outputs:

{
  errors: undefined,
  contracts: Map(1) {
    'add.huff' => {
      bytecode: '600f8060093d393df36004356024350160005260206000f3',
      runtime: '6004356024350160005260206000f3',
      abi: [Object]
    }
  }
}

@Maddiaa0
Copy link
Member

Thank you so much for taking this up, so far this looks great!

@Maddiaa0
Copy link
Member

Maddiaa0 commented Feb 4, 2023

Hey @kanewallmann do you have a status update on this one? Let me know if you think its ready to review!

@refcell refcell marked this pull request as ready for review February 26, 2023 19:19
Copy link
Collaborator

@refcell refcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMMM

@refcell refcell merged commit c024a3c into huff-language:stage Feb 26, 2023
@refcell refcell mentioned this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants