Skip to content

feat(wasm): add some common used defs to wasm stdlib#5199

Merged
clason merged 1 commit intotree-sitter:masterfrom
trim21:fix/rust-wasm32
Jan 6, 2026
Merged

feat(wasm): add some common used defs to wasm stdlib#5199
clason merged 1 commit intotree-sitter:masterfrom
trim21:fix/rust-wasm32

Conversation

@trim21
Copy link
Contributor

@trim21 trim21 commented Jan 4, 2026

No description provided.

@clason
Copy link
Contributor

clason commented Jan 4, 2026

Why?

@trim21
Copy link
Contributor Author

trim21 commented Jan 4, 2026

this stdlib will be used when users write a scanner.c themselves and targeting wasm32

@clason
Copy link
Contributor

clason commented Jan 4, 2026

Can you point to a single scanner where those are used?

@trim21
Copy link
Contributor Author

trim21 commented Jan 4, 2026

Can you point to a single scanner where those are used?

Yeah, I just fix a scanner that doesn't compile on wasm32, which use UINT8_MAX...

https://github.com/polarmutex/tree-sitter-beancount/pull/127/files#diff-bbcf6c785b007d9655b443abc204cc3cc32aa03cea190cb865fe44252dc6f1df

@clason
Copy link
Contributor

clason commented Jan 4, 2026

The Rust bindings are auto-generated and not meant to target wasm. To build a WASM-compliant parser, use tree-sitter build --wasm.

@clason
Copy link
Contributor

clason commented Jan 4, 2026

(This isn't saying it's impossible to add wasm support to the bindings, but this is not the way.)

@trim21
Copy link
Contributor Author

trim21 commented Jan 4, 2026

(This isn't saying it's impossible to add wasm support to the bindings, but this is not the way.)

The Rust bindings are auto-generated and not meant to target wasm. To build a WASM-compliant parser, use tree-sitter build --wasm.

not really, we already support it #4820 , and it's currently in our testing matrix.

we just have a very small stdlib that users need to be careful when they write their own scanner.c

I don't think we need to support large set of stdlib, for example, there is no need to support printf, but add these static def to stdint.h won't do any harm

@clason
Copy link
Contributor

clason commented Jan 4, 2026

My point is that this needs to be done carefully -- you also need to provide the implementation for any stdlib function you want to add. (The stdlib is minimal by design; we can't simply add any function somebody thinks they may want -- we'd rather invest in providing a better purpose-built infrastructure for external scanners.)

@clason
Copy link
Contributor

clason commented Jan 4, 2026

(This is a general discussion to be had and not specific to this PR, mind you.)

@trim21
Copy link
Contributor Author

trim21 commented Jan 4, 2026

I'm ok to not add strlen, I just find that it's already in the stdio.h, I simply move it. so I won't argue for it if someone think it's a bad idea to add it.

I mainly want to add missing defs on stdint.h, which are simple #define

@clason
Copy link
Contributor

clason commented Jan 5, 2026

@maxbrunsfeld this change looks innocuous so I am leaning towards merging it, but I think it would be helpful to have some sort of "design goal" for the wasm stdlib (since wasm parsers are the future -- and it would be very good if we could start supporting multiple runtimes for which this could serve as a specification).

@maxbrunsfeld
Copy link
Contributor

I'm ok with all of these changes. Overall, I just want to provide a minimal subset of libc that enables writing external scanners, so that those scanners compile and behave the same when they are compiled to native code, and to wasm. I don't know exactly what the subset should be. I do want the code size to be reasonable. Constants like don't have any cost AFAICT.

@clason
Copy link
Contributor

clason commented Jan 5, 2026

I agree; this was a more general question (including for guidance on, e.g., the iswpunct addition).

Copy link
Contributor

@clason clason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this with a few other grammars and it seems to work fine, so this should be a low-risk fix.

@clason clason merged commit f4ca3d9 into tree-sitter:master Jan 6, 2026
17 checks passed
github-actions bot pushed a commit that referenced this pull request Jan 6, 2026
Also expose `strlen` through `string.h` instead of `stdio.h`.

(cherry picked from commit f4ca3d9)
@tree-sitter-ci-bot
Copy link

Successfully created backport PR for release-0.26:

@trim21 trim21 deleted the fix/rust-wasm32 branch January 6, 2026 11:05
clason pushed a commit that referenced this pull request Jan 6, 2026
Also expose `strlen` through `string.h` instead of `stdio.h`.

(cherry picked from commit f4ca3d9)

Co-authored-by: Trim21 <trim21.me@gmail.com>
@clason
Copy link
Contributor

clason commented Jan 6, 2026

@trim21 you (and I) forgot to actually add this to the stdlib (wasm-stdlib.h).

@clason
Copy link
Contributor

clason commented Jan 6, 2026

(we really need some documentation about this... @WillLillis )

@trim21
Copy link
Contributor Author

trim21 commented Jan 6, 2026

@trim21 you (and I) forgot to actually add this to the stdlib (wasm-stdlib.h).

What's inside this file? contents of stdlib files?

@clason
Copy link
Contributor

clason commented Jan 6, 2026

Yes. That's the part that actually ends up in the wasm blob for the parser.

@trim21
Copy link
Contributor Author

trim21 commented Jan 6, 2026

If it's generated from other sources we may need a ci to check it or update it

@clason
Copy link
Contributor

clason commented Jan 6, 2026

Something like that, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants