-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add wit-codegen tool #13
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
Conversation
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a comprehensive WIT (WebAssembly Interface Types) code generator tool (wit-codegen) to the project, which parses WIT files using ANTLR4 grammar and generates type-safe C++ host function bindings.
Key changes:
- Adds ANTLR4-based WIT grammar parser with comprehensive test suite
- Implements WIT code generator that creates C++ interface declarations and WAMR bindings
- Integrates automated release process with cross-platform package generation
Reviewed Changes
Copilot reviewed 40 out of 44 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/wit-codegen/wit-codegen.cpp | Complete WIT parser and C++ code generator implementation using ANTLR |
| grammar/Wit.g4 | ANTLR grammar for WebAssembly Interface Types specification |
| test/test_grammar.cpp | Test suite validating grammar against 95+ official WIT files |
| samples/wamr/generated/* | Generated bindings demonstrating the tool's output |
| CMakeLists.txt | Packaging and installation configuration for distribution |
Comments suppressed due to low confidence (1)
tools/wit-codegen/wit-codegen.cpp:1
- Using assert(false) in production code will terminate the program when cross-encoding conversion is needed. Consider throwing a runtime exception with a descriptive message instead.
#include <iostream>
| #!/bin/bash#!/bin/bash | ||
|
|
||
| # Test the WIT grammar against all wit-bindgen test files# Test the WIT grammar against all wit-bindgen test files | ||
|
|
||
|
|
||
|
|
||
| set -eset -e | ||
|
|
||
|
|
||
|
|
||
| # Colors for output# Colors for output | ||
|
|
||
| RED='\033[0;31m'RED='\033[0;31m' | ||
|
|
||
| GREEN='\033[0;32m'GREEN='\033[0;32m' | ||
|
|
||
| YELLOW='\033[1;33m'YELLOW='\033[1;33m' | ||
|
|
||
| NC='\033[0m' # No ColorNC='\033[0m' # No Color | ||
|
|
||
|
|
||
|
|
||
| echo "WIT Grammar Test Script"echo "WIT Grammar Test Script" | ||
|
|
||
| echo "======================="echo "=======================" | ||
|
|
||
| echo ""echo "" | ||
|
|
||
|
|
||
|
|
||
| # Determine project root (script is in test/ directory)# Determine project root (script is in test/ directory) | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" | ||
|
|
||
|
|
||
|
|
||
| cd "$PROJECT_ROOT" || exit 1cd "$PROJECT_ROOT" || exit 1 | ||
|
|
||
|
|
||
|
|
||
| # Check if build directory exists# Check if build directory exists | ||
|
|
||
| if [ ! -d "build" ]; thenif [ ! -d "build" ]; then | ||
|
|
||
| echo -e "${RED}Error: build directory not found${NC}" echo -e "${RED}Error: build directory not found${NC}" | ||
|
|
||
| echo "Please run CMake first:" echo "Please run CMake first:" | ||
|
|
||
| echo " cmake -B build -DBUILD_GRAMMAR=ON" echo " cmake -B build -DBUILD_GRAMMAR=ON" | ||
|
|
||
| exit 1 exit 1 | ||
|
|
||
| fifi | ||
|
|
||
|
|
||
|
|
||
| # Check if grammar has been built# Check if grammar has been built | ||
|
|
||
| if [ ! -f "build/test/test-wit-grammar" ]; thenif [ ! -f "build/test/test-wit-grammar" ]; then | ||
|
|
||
| echo -e "${YELLOW}Grammar test not built. Building now...${NC}" echo -e "${YELLOW}Grammar test not built. Building now...${NC}" | ||
|
|
||
| echo "" echo "" | ||
|
|
||
| cmake --build build --target test-wit-grammar cmake --build build --target test-wit-grammar | ||
|
|
||
| echo "" echo "" | ||
|
|
||
| fifi | ||
|
|
||
|
|
||
|
|
||
| # Check if wit-bindgen submodule exists# Check if wit-bindgen submodule exists | ||
|
|
||
| if [ ! -d "ref/wit-bindgen/tests/codegen" ]; thenif [ ! -d "ref/wit-bindgen/tests/codegen" ]; then | ||
|
|
||
| echo -e "${RED}Error: wit-bindgen test files not found${NC}" echo -e "${RED}Error: wit-bindgen test files not found${NC}" | ||
|
|
||
| echo "Please ensure the wit-bindgen submodule is initialized:" echo "Please ensure the wit-bindgen submodule is initialized:" | ||
|
|
||
| echo " git submodule update --init ref/wit-bindgen" echo " git submodule update --init ref/wit-bindgen" | ||
|
|
||
| exit 1 exit 1 | ||
|
|
||
| fifi | ||
|
|
||
|
|
||
|
|
||
| # Run the test# Run the test | ||
|
|
||
| echo "Running grammar tests..."echo "Running grammar tests..." | ||
|
|
||
| echo ""echo "" | ||
|
|
||
|
|
||
|
|
||
| if ./build/test/test-wit-grammar "$@"; thenif ./build/test/test-wit-grammar "$@"; then | ||
|
|
||
| echo "" echo "" | ||
|
|
||
| echo -e "${GREEN}✓ All grammar tests passed!${NC}" echo -e "${GREEN}✓ All grammar tests passed!${NC}" | ||
|
|
||
| exit 0 exit 0 | ||
|
|
||
| elseelse | ||
|
|
||
| echo "" echo "" | ||
|
|
||
| echo -e "${RED}✗ Grammar tests failed${NC}" echo -e "${RED}✗ Grammar tests failed${NC}" | ||
|
|
||
| exit 1 exit 1 | ||
|
|
||
| fifi |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file content is duplicated - every line appears twice. This suggests a file corruption or merge error that needs to be fixed.
| #!/bin/bash#!/bin/bash | |
| # Test the WIT grammar against all wit-bindgen test files# Test the WIT grammar against all wit-bindgen test files | |
| set -eset -e | |
| # Colors for output# Colors for output | |
| RED='\033[0;31m'RED='\033[0;31m' | |
| GREEN='\033[0;32m'GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m'YELLOW='\033[1;33m' | |
| NC='\033[0m' # No ColorNC='\033[0m' # No Color | |
| echo "WIT Grammar Test Script"echo "WIT Grammar Test Script" | |
| echo "======================="echo "=======================" | |
| echo ""echo "" | |
| # Determine project root (script is in test/ directory)# Determine project root (script is in test/ directory) | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" | |
| cd "$PROJECT_ROOT" || exit 1cd "$PROJECT_ROOT" || exit 1 | |
| # Check if build directory exists# Check if build directory exists | |
| if [ ! -d "build" ]; thenif [ ! -d "build" ]; then | |
| echo -e "${RED}Error: build directory not found${NC}" echo -e "${RED}Error: build directory not found${NC}" | |
| echo "Please run CMake first:" echo "Please run CMake first:" | |
| echo " cmake -B build -DBUILD_GRAMMAR=ON" echo " cmake -B build -DBUILD_GRAMMAR=ON" | |
| exit 1 exit 1 | |
| fifi | |
| # Check if grammar has been built# Check if grammar has been built | |
| if [ ! -f "build/test/test-wit-grammar" ]; thenif [ ! -f "build/test/test-wit-grammar" ]; then | |
| echo -e "${YELLOW}Grammar test not built. Building now...${NC}" echo -e "${YELLOW}Grammar test not built. Building now...${NC}" | |
| echo "" echo "" | |
| cmake --build build --target test-wit-grammar cmake --build build --target test-wit-grammar | |
| echo "" echo "" | |
| fifi | |
| # Check if wit-bindgen submodule exists# Check if wit-bindgen submodule exists | |
| if [ ! -d "ref/wit-bindgen/tests/codegen" ]; thenif [ ! -d "ref/wit-bindgen/tests/codegen" ]; then | |
| echo -e "${RED}Error: wit-bindgen test files not found${NC}" echo -e "${RED}Error: wit-bindgen test files not found${NC}" | |
| echo "Please ensure the wit-bindgen submodule is initialized:" echo "Please ensure the wit-bindgen submodule is initialized:" | |
| echo " git submodule update --init ref/wit-bindgen" echo " git submodule update --init ref/wit-bindgen" | |
| exit 1 exit 1 | |
| fifi | |
| # Run the test# Run the test | |
| echo "Running grammar tests..."echo "Running grammar tests..." | |
| echo ""echo "" | |
| if ./build/test/test-wit-grammar "$@"; thenif ./build/test/test-wit-grammar "$@"; then | |
| echo "" echo "" | |
| echo -e "${GREEN}✓ All grammar tests passed!${NC}" echo -e "${GREEN}✓ All grammar tests passed!${NC}" | |
| exit 0 exit 0 | |
| elseelse | |
| echo "" echo "" | |
| echo -e "${RED}✗ Grammar tests failed${NC}" echo -e "${RED}✗ Grammar tests failed${NC}" | |
| exit 1 exit 1 | |
| fifi | |
| #!/bin/bash | |
| # Test the WIT grammar against all wit-bindgen test files | |
| set -e | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| echo "WIT Grammar Test Script" | |
| echo "=======================" | |
| echo "" | |
| # Determine project root (script is in test/ directory) | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" | |
| cd "$PROJECT_ROOT" || exit 1 | |
| # Check if build directory exists | |
| if [ ! -d "build" ]; then | |
| echo -e "${RED}Error: build directory not found${NC}" | |
| echo "Please run CMake first:" | |
| echo " cmake -B build -DBUILD_GRAMMAR=ON" | |
| exit 1 | |
| fi | |
| # Check if grammar has been built | |
| if [ ! -f "build/test/test-wit-grammar" ]; then | |
| echo -e "${YELLOW}Grammar test not built. Building now...${NC}" | |
| echo "" | |
| cmake --build build --target test-wit-grammar | |
| echo "" | |
| fi | |
| # Check if wit-bindgen submodule exists | |
| if [ ! -d "ref/wit-bindgen/tests/codegen" ]; then | |
| echo -e "${RED}Error: wit-bindgen test files not found${NC}" | |
| echo "Please ensure the wit-bindgen submodule is initialized:" | |
| echo " git submodule update --init ref/wit-bindgen" | |
| exit 1 | |
| fi | |
| # Run the test | |
| echo "Running grammar tests..." | |
| echo "" | |
| if ./build/test/test-wit-grammar "$@"; then | |
| echo "" | |
| echo -e "${GREEN}✓ All grammar tests passed!${NC}" | |
| exit 0 | |
| else | |
| echo "" | |
| echo -e "${RED}✗ Grammar tests failed${NC}" | |
| exit 1 | |
| fi |
| list_t<char_t> chars = {U'A', U'B', U'c', 0x1F30D}; // 🌍 Earth Globe emoji | ||
| store(*cx, chars, 900); | ||
| auto loaded = load<list_t<char_t>>(*cx, 900); | ||
| CHECK(loaded.size() == 4); | ||
| CHECK(loaded[0] == U'A'); | ||
| CHECK(loaded[1] == U'B'); | ||
| CHECK(loaded[2] == U'c'); | ||
| CHECK(loaded[3] == U'🌍'); // Verify Unicode support | ||
| CHECK(loaded[3] == 0x1F30D); // 🌍 Earth Globe emoji - Verify Unicode support |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using hex literal 0x1F30D instead of the Unicode literal U'🌍' reduces code readability. Consider using the Unicode literal with a comment explaining why the hex form was chosen if there's a specific technical reason.
| // Unknown type - return as-is with warning | ||
| std::cerr << "Warning: Unknown type '" << type << "', using as-is" << std::endl; | ||
| return type; |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using std::cerr for warnings in a library function may not be appropriate for all use cases. Consider adding a configurable warning callback or collecting warnings to return to the caller.
| for (const auto &enumDef : iface->enums) | ||
| { | ||
| if (enumDef.name == type) | ||
| return enumDef.name; // TODO: sanitize when type definitions are implemented |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple TODO comments indicate incomplete sanitization of type names. This could lead to invalid C++ identifiers being generated. Consider implementing basic sanitization (e.g., replacing hyphens with underscores) to prevent compilation errors.
No description provided.