Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,25 @@ jobs:
continue-on-error: true
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-latest, ubuntu-latest, windows-latest, macos-13]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: install
if: ${{ matrix.os != 'windows-latest' }}
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s 'bleeding'
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s bleeding
echo "$HOME/.moon/bin" >> $GITHUB_PATH

- name: install on windows
env:
MOONBIT_INSTALL_VERSION: bleeding
if: ${{ matrix.os == 'windows-latest' }}
run: |
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser; irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
"C:\Users\runneradmin\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Append

- name: moon version
run: |
moon version --all
Expand All @@ -259,13 +268,25 @@ jobs:
run: moon check --deny-warn

- name: Set ulimit and run moon test
if: ${{ matrix.os != 'windows-latest' }}
run: |
ulimit -s 8176
moon test --target all
moon test --release --target all
moon test --target native
moon test --target native --release

- name: Setup MSVC
if: ${{ matrix.os == 'windows-latest' }}
uses: ilammy/msvc-dev-cmd@v1

- name: Run moon test on Windows
if: ${{ matrix.os == 'windows-latest' }}
run: |
moon test --target all
moon test --release --target all
moon test --target native

- name: moon test --doc
run: |
moon test --doc
Expand Down
27 changes: 27 additions & 0 deletions fs_sync/blackbox_test.mbt
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)
}
73 changes: 73 additions & 0 deletions fs_sync/fs_sync.mbt
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~)
}
19 changes: 19 additions & 0 deletions fs_sync/fs_sync.mbti
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

99 changes: 99 additions & 0 deletions fs_sync/fs_sync_js.mbt
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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var

#| 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)
}
114 changes: 114 additions & 0 deletions fs_sync/fs_sync_native.mbt
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)
}
Loading
Loading