tomaka / hlua

Rust library to interface with Lua
MIT License
510 stars 48 forks source link

Lua strings are not UTF-8 #135

Closed jack1243star closed 7 years ago

jack1243star commented 7 years ago

As Lua strings are plain bytes, it cannot be represented using Rust strings. The code:

let mut lua = Lua::new();
lua.execute::<hlua::AnyLuaValue>(r"return '\xff\xfe\xff\xfe'").unwrap();

results in:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error { bytes: [255, 254, 255, 254], error: Utf8Error { valid_up_to: 0 } }', C:\bot\slave\stable-dist-rustc-win-msvc-32\build\src\libcore\result.rs:868

Lua strings should be represented as vectors of u8.

tomaka commented 7 years ago

If you write for example this:

let _ = lua.execute::<String>(r"return '\xff\xfe\xff\xfe'").unwrap();

Then the unwrap will panic, but it's the job of the library user to handle the fact that the string may not be convertible to a Rust String.

However in your example we're talking about AnyLuaValue, and I agree that reading a AnyLuaValue should never panic (as its role is to accept any possible Lua type).

When it comes to representing strings, the problem with representing strings as Vec<u8> is that a Lua array of numbers may also be represented as a Vec<u8>. We probably need some sort of custom type (eg. LuaString) that holds a Vec<u8> internally.