Handle Lua strings in JavaScript
masterBecause Lua strings are 8-bit clean and can contain null bytes (\0), Fengari implements them using Uint8Array objects to preserve raw bytes.
When interacting between JS and Lua:
- Pushing JS strings to Lua: Use
lua_pushliteral(L, string)to convert a JS string into a byte array before pushing it to the stack. - Getting Lua strings as JS strings: Use
lua_tojsstring(L, idx)to attempt conversion to a UTF-16 JS string. Note that this may not yield expected results if the Lua string contains invalid UTF-16 sequences. - Other conversion utilities: You can also use
luastring_of,to_luastring,to_jsstring, andto_uristringfor string manipulations.