ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
489 stars 32 forks source link

Binary data as lua string #122

Open fatal10110 opened 1 month ago

fatal10110 commented 1 month ago

I'm trying to mock Redis lua scripting behavior, and one of the challenges there is that Redis is "binary safe" meaning u can operate with binary data (e.g images), which is stored the same was as regular strings.

As an input to lua I can get any binary(e.g utf-8 bytes, binary data)

const imageBuffer = fs.readFileSync('./1px.png') // Binray data buffer
const utfBuffer = Buffer.from('фвфв') // Utf8 string buffer

lua.glob.set("ARGV", [imageBuffer, utfBuffer])

await lua.doString(`
    print(ARGV[2] == "фвфв")
    return ARGV[1]
`)

In case of redis utf-8 passed to lua as bytes, in lua u can compare these bytes with strings also it can return valid binary data from lua strings, in my example above, the compare will not work, but binry data will be returned properly from lua

in the other hand, If I will inject to lua the utf8 string as hex representation (with leading \x), it will successfully compare the string, but will not return valid binary data from lua script

So in general my question is, how do I pass binary data to lua? and return binary data from there

Not sure this is wasmoon issue, but I will appreciate any help :)

Thanks

fatal10110 commented 1 month ago

as a workaround, I'm passing hex encoded binary data, and decode it in the lua, same for output