#Strings in WASM -- JS to Zig and Zig to JS

1 messages · Page 1 of 1 (latest)

night minnow
#

Would anyone happen to have an example to point towards of how to marshal strings in JS into a []u8 in Zig, and how to allocate and return a []u8 from Zig back to JS? I'm ideally hoping that someone has the string version of https://github.com/meheleventyone/zig-wasm-test/ somewhere where I could then template off of?

I've searched and found some of the related questions before (most usefully #zig-help-deprecated message), but between zig strings being weird (export requires [*]u8, which I don't otherwise use?), my unfamiliarity with the WASM/JS model, and what to do with zig's allocator, I've been struggling.

(My variety of searches tell me that I think I basically want the zig version of wasm-bindgen for rust?)

GitHub

A minimal Web Assembly example using Zig's build system. - GitHub - meheleventyone/zig-wasm-test: A minimal Web Assembly example using Zig's build system.

Discord

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

cedar sedge
#

Hi

#

give me a few secs

#

You get the memory as an array of u8

#

and use textEncoder to get they bytes encoded right

#

For reading from wasm to js

           function readString(addr, len) {
                let utf8Decoder = new TextDecoder();
                let view = new Uint8Array(obj.instance.exports.memory.buffer);

                return utf8Decoder.decode(view.slice(addr, addr + len));
            }

You use a decoder and get the memory buffer and get the slice of memory where the wasm string is

#

This is the method i used in that project long ago 😛

night minnow
#

std.heap.wasm_allocator 🤦‍♂️

#

But I think _start also answers the allocator puzzle I had. Let me play with this for a bit...

cedar sedge
#
                const {
                    encode,
                    decode,
                    _start
                } = obj.instance.exports;

This is how I get start

#

I just called like that because I am used to using it haha

#

could be better named as setup or something

#

=V

night minnow
#

No I was just having the "How the hell do I keep the allocator alive" confusion. I also didn't know you could just = undefined things at global scope. So... helpful example 🙂

cedar sedge
#

Ah

#

haha

night minnow
#

Any chance you also have some code that returns a [*]u8 back to JS?

cedar sedge
#

encode

#

and decode

#

technically returns to js

#

You would need two functions

#

one that returns the address of the memory

#

and one that returns the len of that ptr

cedar sedge
#

just needs the function call for the addr and one for the len

night minnow
#

Is there... some technical limitation that I'm missing as to why encode/decode doesn't just return a [*]u8, rather than calling into JS to store its return value...?

cedar sedge
#

Skill issue from me

night minnow
#

I assumed it was just the interaction style of whatever this is embedded into as to why it fetches the arguments via JS method calls, rather than encode/decode accepting ([*]u8, u32) as parameters

#

hahaha, alright. Well, you're clearly still ahead of me here :p

cedar sedge
#

felt right for me too, did not want to do much in the JS

#

All the JS does is give the wasm tools

blazing storm
cedar sedge
#

yup

night minnow
#

Oh! This is great!

#

In this post, they define an allocUint8 to allocate memory on the zig side for a string in JS. In other examples, and the code linked by @cedar sedge above, it just does new Uint8Array(obj.instance.exports.memory.buffer) on the JS side. Is there some meaningful difference that I should be aware between these two?

cedar sedge
#

in readString or wher?

#

in getString, as the name suggest, is just a view of the memory

night minnow
#

That snippet was copied from your getText. I'm assuming that somehow just grabbing a bugger from exports.memory.buffer doesn't interfere with allocations from std.heap.wasm_allocator?

cedar sedge
#

Not sure

#

I just touch the zig code from time to time making it compile haha

night minnow
#

The article does seem to lowkey imply that doing so isn't safe, so I guess I'll follow that route.

night minnow
#

Okay, so I crashed the compiler? https://zigbin.io/e1bd89

thread 1 panic: Zig compiler bug: attempted to destroy declaration with an attached error
cedar sedge
#

It expected a pointer to an array OR a slice

#

Doubled returns a [*]u8

#

allocator.free need a slice