wasmoon icon indicating copy to clipboard operation
wasmoon copied to clipboard

Binary data as lua string

Open fatal10110 opened this issue 1 year ago • 1 comments

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 avatar Sep 10 '24 07:09 fatal10110

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

fatal10110 avatar Sep 13 '24 20:09 fatal10110

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

I think it is more about how redis works with these binaries. Lua doesn't have a "Buffer" type, but you can represent the binaries as strings, table of numbers or even user_data depending on how it is implemented.

ceifa avatar Jan 18 '25 15:01 ceifa