-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal: add fs_sync package #1524
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7668101
feat: add fs_sync package
Young-Flash 6a8a49a
internal(fs): add wasm & wasm-gc support
Young-Flash 65901fb
internal(fs): add js support
Young-Flash a788199
internal(fs): add native support
Young-Flash c8f6ac5
minor: moon fmt
Young-Flash 6e0ca35
ci: add windows-latest & macos-13 for bleeding
Young-Flash 18b74d7
fix: windows fopen mode should end with \x00
Young-Flash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2025 International Digital Economy Academy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
///| | ||
test "write_and_read" { | ||
let path_1 = "./fs_sync/test_1.txt" | ||
let path_2 = "./fs_sync/test_2.txt" | ||
let content_1 = "Hello, World" | ||
let content_2 = "Hello, MoonBit" | ||
@fs_sync.write_bytes_to_file!(path_1, content_1.to_bytes()) | ||
let res_1 = @fs_sync.read_file_to_bytes!(path_1) | ||
inspect!(res_1.to_unchecked_string(), content=content_1) | ||
@fs_sync.write_string_to_file!(path_2, content_2) | ||
let res_2 = @fs_sync.read_file_to_string!(path_2) | ||
inspect!(res_2, content=content_2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2025 International Digital Economy Academy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
///| | ||
pub type! IOError String derive(Show) | ||
|
||
///| Reads the content of a file specified by the given path and returns its | ||
/// content as `Bytes` | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `path` : The path to the file to be read. | ||
/// | ||
/// # Returns | ||
/// | ||
/// - A `Bytes` representing the content of the file. | ||
pub fn read_file_to_bytes(path : String) -> Bytes! { | ||
read_file_to_bytes_internal!(path) | ||
} | ||
|
||
///| Reads the content of a file specified by the given path and returns its | ||
/// content as `String`. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `path` : The path to the file to be read. | ||
/// - `encoding~` : The encoding of the file. Only support `utf8` for now. | ||
/// | ||
/// # Returns | ||
/// | ||
/// - A `String` representing the content of the file. | ||
pub fn read_file_to_string( | ||
path : String, | ||
encoding~ : String = "utf8" | ||
) -> String! { | ||
read_file_to_string_internal!(path, encoding~) | ||
} | ||
|
||
///| Writes a `Bytes` to a file at the specified path. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `path` : The path to the file where the bytes will be written. | ||
/// - `content` : A `Bytes` to be written to the file. | ||
pub fn write_bytes_to_file(path : String, content : Bytes) -> Unit! { | ||
write_bytes_to_file_internal!(path, content) | ||
} | ||
|
||
///| Writes a `String` to a file at the specified path. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `path` : The path to the file where the string will be written. | ||
/// - `content` : A `String` to be written to the file. | ||
/// - `encoding~` : The encoding of the file. Only support `utf8` for now. | ||
pub fn write_string_to_file( | ||
path : String, | ||
content : String, | ||
encoding~ : String = "utf8" | ||
) -> Unit! { | ||
write_string_to_file_internal!(path, content, encoding~) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package moonbitlang/core/fs_sync | ||
|
||
// Values | ||
fn read_file_to_bytes(String) -> Bytes! | ||
|
||
fn read_file_to_string(String, encoding~ : String = ..) -> String! | ||
|
||
fn write_bytes_to_file(String, Bytes) -> Unit! | ||
|
||
fn write_string_to_file(String, String, encoding~ : String = ..) -> Unit! | ||
|
||
// Types and methods | ||
pub type! IOError String | ||
impl Show for IOError | ||
|
||
// Type aliases | ||
|
||
// Traits | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2025 International Digital Economy Academy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
///| | ||
extern "js" fn read_file_ffi(path : String) -> Int = | ||
#| function(path) { | ||
#| fs = require('fs'); | ||
#| try { | ||
#| const content = fs.readFileSync(path); | ||
#| globalThis.fileContent = content; | ||
#| return 0; | ||
#| } catch (error) { | ||
#| globalThis.errorMessage = error.message; | ||
#| return -1; | ||
#| } | ||
#| } | ||
|
||
///| | ||
extern "js" fn write_file_ffi(path : String, content : Bytes) -> Int = | ||
#| function(path, content) { | ||
#| fs = require('fs'); | ||
#| try { | ||
#| fs.writeFileSync(path, Buffer.from(content)); | ||
#| return 0; | ||
#| } catch (error) { | ||
#| globalThis.errorMessage = error.message; | ||
#| return -1; | ||
#| } | ||
#| } | ||
|
||
///| | ||
extern "js" fn get_file_content_ffi() -> FixedArray[Byte] = | ||
#| function() { | ||
#| return globalThis.fileContent; | ||
#| } | ||
|
||
///| | ||
extern "js" fn get_error_message_ffi() -> String = | ||
#| function() { | ||
#| return globalThis.errorMessage || ''; | ||
#| } | ||
|
||
///| | ||
fn read_file_to_bytes_internal(path : String) -> Bytes! { | ||
let res = read_file_ffi(path) | ||
if res == -1 { | ||
raise IOError(get_error_message_ffi()) | ||
} | ||
let content = get_file_content_ffi() | ||
Bytes::from_iter(content.iter()) | ||
} | ||
|
||
///| | ||
fn read_file_to_string_internal( | ||
path : String, | ||
encoding~ : String = "utf8" | ||
) -> String! { | ||
guard encoding == "utf8" else { | ||
raise IOError( | ||
"Unsupported encoding: \{encoding}, only utf8 is supported for now", | ||
) | ||
} | ||
let bytes = read_file_to_bytes_internal!(path) | ||
utf8_bytes_to_mbt_string(bytes) | ||
} | ||
|
||
///| | ||
fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! { | ||
let res = write_file_ffi(path, content) | ||
if res == -1 { | ||
raise IOError(get_error_message_ffi()) | ||
} | ||
} | ||
|
||
///| | ||
fn write_string_to_file_internal( | ||
path : String, | ||
content : String, | ||
encoding~ : String = "utf8" | ||
) -> Unit! { | ||
guard encoding == "utf8" else { | ||
raise IOError( | ||
"Unsupported encoding: \{encoding}, only utf8 is supported for now", | ||
) | ||
} | ||
let bytes = mbt_string_to_utf8_bytes(content, false) | ||
write_bytes_to_file_internal!(path, bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2025 International Digital Economy Academy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
///| | ||
priv type File_Handler | ||
|
||
///| | ||
fn fopen_ffi(path : Bytes, mode : Bytes) -> File_Handler = "$moonbit.fopen_ffi" | ||
|
||
///| | ||
fn is_null(ptr : File_Handler) -> Int = "$moonbit.is_null" | ||
|
||
///| | ||
fn fread_ffi( | ||
ptr : Bytes, | ||
size : Int, | ||
nitems : Int, | ||
stream : File_Handler | ||
) -> Int = "$moonbit.fread_ffi" | ||
|
||
///| | ||
fn fwrite_ffi( | ||
ptr : Bytes, | ||
size : Int, | ||
nitems : Int, | ||
stream : File_Handler | ||
) -> Int = "$moonbit.fwrite_ffi" | ||
|
||
///| | ||
fn get_error_message_ffi() -> Bytes = "$moonbit.get_error_message" | ||
|
||
///| | ||
fn fseek_ffi(file : File_Handler, offset : Int, whence : Int) -> Int = "$moonbit.fseek_ffi" | ||
|
||
///| | ||
fn ftell_ffi(file : File_Handler) -> Int = "$moonbit.ftell_ffi" | ||
|
||
///| | ||
fn fflush_ffi(stream : File_Handler) -> Int = "$moonbit.fflush_ffi" | ||
|
||
///| | ||
fn fclose_ffi(file : File_Handler) -> Int = "$moonbit.fclose_ffi" | ||
|
||
///| | ||
fn get_error_message() -> String { | ||
utf8_bytes_to_mbt_string(get_error_message_ffi()) | ||
} | ||
|
||
///| | ||
fn read_file_to_bytes_internal(path : String) -> Bytes! { | ||
let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"rb\x00") | ||
guard is_null(file) == 0 else { raise IOError(get_error_message()) } | ||
guard fseek_ffi(file, 0, 2) == 0 else { raise IOError(get_error_message()) } | ||
let size = ftell_ffi(file) | ||
guard size != -1 else { raise IOError(get_error_message()) } | ||
guard fseek_ffi(file, 0, 0) == 0 else { raise IOError(get_error_message()) } | ||
let bytes = Bytes::make(size, 0) | ||
let bytes_read = fread_ffi(bytes, 1, size, file) | ||
guard bytes_read == size else { raise IOError(get_error_message()) } | ||
guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) } | ||
bytes | ||
} | ||
|
||
///| | ||
fn read_file_to_string_internal( | ||
path : String, | ||
encoding~ : String = "utf8" | ||
) -> String! { | ||
guard encoding == "utf8" else { | ||
raise IOError( | ||
"Unsupported encoding: \{encoding}, only utf8 is supported for now", | ||
) | ||
} | ||
utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path)) | ||
} | ||
|
||
///| | ||
fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! { | ||
let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"wb\x00") | ||
guard is_null(file) == 0 else { raise IOError(get_error_message()) } | ||
let bytes_written = fwrite_ffi(content, 1, content.length(), file) | ||
guard bytes_written == content.length() else { | ||
raise IOError(get_error_message()) | ||
} | ||
guard fflush_ffi(file) == 0 else { raise IOError(get_error_message()) } | ||
guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) } | ||
|
||
} | ||
|
||
///| | ||
fn write_string_to_file_internal( | ||
path : String, | ||
content : String, | ||
encoding~ : String = "utf8" | ||
) -> Unit! { | ||
guard encoding == "utf8" else { | ||
raise IOError( | ||
"Unsupported encoding: \{encoding}, only utf8 is supported for now", | ||
) | ||
} | ||
let bytes = mbt_string_to_utf8_bytes(content, false) | ||
write_bytes_to_file_internal!(path, bytes) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var