Skip to content

Commit

Permalink
Fix WASM sysroot breaking runtime behavior (#37)
Browse files Browse the repository at this point in the history
* Fix WASM sysroot breaking runtime behavior

* Add WASM tests to CI
  • Loading branch information
shadaj authored Mar 27, 2023
1 parent 22f6407 commit a0487cf
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 120 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
test-wasm:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install wasm32 target
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
- name: Install WebAssembly test runner
uses: actions-rs/cargo@v1
with:
command: install
args: wasm-bindgen-cli --version 0.2.83
- name: Run tests
uses: actions-rs/cargo@v1
env:
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
with:
command: test
args: -p rust-sitter-example --target wasm32-unknown-unknown --tests --no-fail-fast
lint:
runs-on: ubuntu-latest
steps:
Expand Down
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ rust-sitter-tool = { path = "../tool" }

[dev-dependencies]
insta = "1.7.1"
wasm-bindgen-test = "0.3.0"
81 changes: 70 additions & 11 deletions example/src/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[rust_sitter::grammar("arithmetic")]
pub mod grammar {
#[rust_sitter::language]
#[derive(Debug)]
#[derive(PartialEq, Eq, Debug)]
pub enum Expression {
Number(#[rust_sitter::leaf(pattern = r"\d+", transform = |v| v.parse().unwrap())] i32),
#[rust_sitter::prec_left(1)]
Expand All @@ -28,18 +28,77 @@ pub mod grammar {
#[cfg(test)]
mod tests {
use super::*;
use grammar::Expression;

#[wasm_bindgen_test::wasm_bindgen_test]
#[test]
fn arithmetic_grammar() {
// successful parses
insta::assert_debug_snapshot!(grammar::parse("1"));
insta::assert_debug_snapshot!(grammar::parse("1 - 2"));
insta::assert_debug_snapshot!(grammar::parse("1 - 2 - 3"));
insta::assert_debug_snapshot!(grammar::parse("1 - 2 * 3"));
insta::assert_debug_snapshot!(grammar::parse("1 * 2 * 3"));
insta::assert_debug_snapshot!(grammar::parse("1 * 2 - 3"));

// failed parses
fn successful_parses() {
assert_eq!(grammar::parse("1").unwrap(), Expression::Number(1));

assert_eq!(
grammar::parse("1 - 2").unwrap(),
Expression::Sub(
Box::new(Expression::Number(1)),
(),
Box::new(Expression::Number(2))
)
);

assert_eq!(
grammar::parse("1 - 2 - 3").unwrap(),
Expression::Sub(
Box::new(Expression::Sub(
Box::new(Expression::Number(1)),
(),
Box::new(Expression::Number(2))
)),
(),
Box::new(Expression::Number(3))
)
);

assert_eq!(
grammar::parse("1 - 2 * 3").unwrap(),
Expression::Sub(
Box::new(Expression::Number(1)),
(),
Box::new(Expression::Mul(
Box::new(Expression::Number(2)),
(),
Box::new(Expression::Number(3))
))
)
);

assert_eq!(
grammar::parse("1 * 2 * 3").unwrap(),
Expression::Mul(
Box::new(Expression::Mul(
Box::new(Expression::Number(1)),
(),
Box::new(Expression::Number(2))
)),
(),
Box::new(Expression::Number(3))
)
);

assert_eq!(
grammar::parse("1 * 2 - 3").unwrap(),
Expression::Sub(
Box::new(Expression::Mul(
Box::new(Expression::Number(1)),
(),
Box::new(Expression::Number(2))
)),
(),
Box::new(Expression::Number(3))
)
);
}

#[test]
fn failed_parses() {
insta::assert_debug_snapshot!(grammar::parse("1 + 2"));
insta::assert_debug_snapshot!(grammar::parse("1 - 2 -"));
insta::assert_debug_snapshot!(grammar::parse("a1"));
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion tool/src/wasm-sysroot/stdbool.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define bool _Bool
#define true 1
#define false 0
#define bool int

0 comments on commit a0487cf

Please sign in to comment.