fix(wasm): provide C stdlib for tree-sitter, fix playground abort#40
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
JuanMarchetto
approved these changes
Feb 26, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
mallocreturning 0 (NULL) caused tree-sitter to callabort()when allocating memory.Fix: Provide real
#[no_mangle]C stdlib implementations in Rust (crates/truss-wasm/lib.rs):malloc/calloc/realloc/free): route to Rust's global allocator with an 8-byte size-prefix header sofree/realloccan reconstruct theLayoutfprintf/snprintf/vsnprintf/fwrite/fputc/fclose/clock): no-op — tree-sitter declares but never calls these in the validation pathabort: triggerswasm32::unreachabletrapThe linker resolves these at build time, so the generated JS will no longer contain
import * from "env". Removes the now-unnecessaryenv.jsshim and import map from PR #39.Files changed
crates/truss-wasm/lib.rswasm_compatmodule with 12 C stdlib implementationsplayground/env.jsplayground/index.html.github/workflows/pages.ymlTest plan
#[no_mangle]symbols)import * from "env"no longer appears in generatedtruss_wasm.js🤖 Generated with Claude Code