From a59b0190ab1d529719641eb2187bbcf8e872e7af Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 11 Mar 2023 08:16:16 +0100 Subject: [PATCH] Fix fs writeFile & readFile not working with invalid utf8 --- CHANGELOG.md | 4 ++++ packages/lib/src/globals/fs.rs | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d4504..be820d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 print("Hello, world!") ``` +### Fixed + +- Fixed `fs.writeFile` and `fs.readFile` not working with strings / files that are invalid utf-8 + ## `0.5.5` - March 8th, 2023 ### Added diff --git a/packages/lib/src/globals/fs.rs b/packages/lib/src/globals/fs.rs index aec68fd..7c10370 100644 --- a/packages/lib/src/globals/fs.rs +++ b/packages/lib/src/globals/fs.rs @@ -19,8 +19,9 @@ pub fn create(lua: &'static Lua) -> LuaResult { .build_readonly() } -async fn fs_read_file(_: &'static Lua, path: String) -> LuaResult { - fs::read_to_string(&path).await.map_err(LuaError::external) +async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult { + let bytes = fs::read(&path).await.map_err(LuaError::external)?; + lua.create_string(&bytes) } async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { @@ -52,8 +53,11 @@ async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { Ok(dir_strings_no_prefix) } -async fn fs_write_file(_: &'static Lua, (path, contents): (String, String)) -> LuaResult<()> { - fs::write(&path, &contents) +async fn fs_write_file( + _: &'static Lua, + (path, contents): (String, LuaString<'_>), +) -> LuaResult<()> { + fs::write(&path, &contents.as_bytes()) .await .map_err(LuaError::external) }