Skip to content

fix(wasm): provide C stdlib for tree-sitter, fix playground abort#40

Merged
JuanMarchetto merged 1 commit into
mainfrom
fix/wasm-env-implementations
Feb 26, 2026
Merged

fix(wasm): provide C stdlib for tree-sitter, fix playground abort#40
JuanMarchetto merged 1 commit into
mainfrom
fix/wasm-env-implementations

Conversation

@jeanclawdbotdamn

Copy link
Copy Markdown
Collaborator

Summary

Fixes the playground showing "Error: abort" on every validation.

Root cause: PR #39 added JS shims for tree-sitter's C stdlib imports, but malloc returning 0 (NULL) caused tree-sitter to call abort() when allocating memory.

Fix: Provide real #[no_mangle] C stdlib implementations in Rust (crates/truss-wasm/lib.rs):

  • Memory functions (malloc/calloc/realloc/free): route to Rust's global allocator with an 8-byte size-prefix header so free/realloc can reconstruct the Layout
  • I/O stubs (fprintf/snprintf/vsnprintf/fwrite/fputc/fclose/clock): no-op — tree-sitter declares but never calls these in the validation path
  • abort: triggers wasm32::unreachable trap

The linker resolves these at build time, so the generated JS will no longer contain import * from "env". Removes the now-unnecessary env.js shim and import map from PR #39.

Files changed

File Change
crates/truss-wasm/lib.rs Add wasm_compat module with 12 C stdlib implementations
playground/env.js Deleted (no longer needed)
playground/index.html Remove import map
.github/workflows/pages.yml Remove env.js copy step

Test plan

  • CI passes (WASM build must succeed with the new #[no_mangle] symbols)
  • After Pages deploy: open playground, type YAML, validation runs without abort
  • Verify import * from "env" no longer appears in generated truss_wasm.js

🤖 Generated with Claude Code

tree-sitter's C code references malloc/free/fprintf/etc. which become
unresolved "env" imports in the WASM binary. Browsers cannot resolve the
bare "env" module specifier, causing the playground to fail.

Previous fix (PR #39) added JS shims via import map, but malloc returning
NULL caused tree-sitter to call abort() at runtime.

This fix provides real #[no_mangle] implementations in Rust:
- malloc/calloc/realloc/free: route to Rust's global allocator with an
  8-byte size header so free/realloc can reconstruct the Layout
- fprintf/snprintf/vsnprintf/fwrite/fputc/fclose: no-op stubs (tree-sitter
  declares but never calls these in the validation path)
- clock: returns 0
- abort: triggers wasm32::unreachable trap

The linker resolves these at build time, eliminating all "env" imports
from the generated JS. Removes the now-unnecessary env.js shim and
import map.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Feb 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.65%. Comparing base (ee4fc67) to head (603e88d).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #40   +/-   ##
=======================================
  Coverage   77.65%   77.65%           
=======================================
  Files          50       50           
  Lines        4408     4408           
=======================================
  Hits         3423     3423           
  Misses        985      985           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JuanMarchetto JuanMarchetto merged commit 7394162 into main Feb 26, 2026
10 checks passed
@JuanMarchetto JuanMarchetto deleted the fix/wasm-env-implementations branch February 26, 2026 18:51
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.

2 participants