-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #678 from tessi/feature/using-component
Feature/using component
- Loading branch information
Showing
16 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use rustler::{NifResult, Term}; | ||
use wit_parser::{Resolve, WorldItem}; | ||
|
||
#[rustler::nif(name = "wit_exported_functions")] | ||
pub fn exported_functions(env: rustler::Env, path: String, wit: String) -> NifResult<Term> { | ||
let mut resolve = Resolve::new(); | ||
let id = resolve | ||
.push_str(path, &wit) | ||
.map_err(|e| rustler::Error::Term(Box::new(format!("Failed to parse WIT: {}", e))))?; | ||
let world_id = resolve | ||
.select_world(id, None) | ||
.map_err(|e| rustler::Error::Term(Box::new(format!("Failed to select world: {}", e))))?; | ||
let exports = &resolve.worlds[world_id].exports; | ||
let exported_functions = exports | ||
.iter() | ||
.filter_map(|(_key, value)| match value { | ||
WorldItem::Function(function) => Some((&function.name, function.params.len())), | ||
_ => None, | ||
}) | ||
.collect::<Vec<(&String, usize)>>(); | ||
Ok(Term::map_from_pairs(env, exported_functions.as_slice()).unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule HelloWorld do | ||
@moduledoc false | ||
|
||
use Wasmex.Components.Component, wit: "test/component_fixtures/hello_world/hello-world.wit" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npx jco componentize -w hello-world.wit -o hello_world.wasm hello-world.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package local:hello-world; | ||
|
||
world hello-world { | ||
record kebab-record { | ||
kebab-field: string | ||
} | ||
export greet: func(who: string) -> string; | ||
export multi-greet: func(who: string, times: u16) -> list<string>; | ||
export greet-many: func(people: list<string>) -> list<string>; | ||
export echo-kebab: func(kebab-record: kebab-record) -> kebab-record; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Components.WitParserTest do | ||
use ExUnit.Case | ||
|
||
describe "exports" do | ||
test "exported_functions" do | ||
wit = File.read!("test/component_fixtures/hello_world/hello-world.wit") | ||
|
||
assert %{"greet" => 1, "greet-many" => 1, "multi-greet" => 2} = | ||
Wasmex.Native.wit_exported_functions("hello-world.wit", wit) | ||
end | ||
|
||
test "wit parse errors" do | ||
wit = "goo goo" | ||
assert {:error, error} = Wasmex.Native.wit_exported_functions("hello-world.wit", wit) | ||
assert error =~ "Failed to parse WIT" | ||
end | ||
end | ||
end |