-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
253 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package moonbitlang/x/fs | ||
|
||
// Values | ||
fn exists(~path : String) -> Bool | ||
|
||
fn read_to_string(~path : String) -> String! | ||
|
||
fn write_string(~path : String, ~content : String) -> Unit | ||
|
||
// Types and methods | ||
|
||
// Type aliases | ||
|
||
// Traits | ||
|
||
// Extension Methods | ||
|
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,86 @@ | ||
// Copyright 2024 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. | ||
|
||
/// Reads the contents of a file into a string. | ||
/// | ||
/// # Parameters | ||
/// - `path`: An `XExternString` representing the file path. | ||
/// | ||
/// # Returns | ||
/// An `XExternString` containing the file contents. | ||
/// | ||
/// # Note | ||
/// This is an unstable function call and should be used internally only. | ||
fn read_file_to_string(path : @ffi.XExternString) -> @ffi.XExternString = "__moonbit_fs_unstable" "read_file_to_string" | ||
|
||
/// Writes a string to a file. | ||
/// | ||
/// # Parameters | ||
/// - `path`: An `XExternString` representing the file path. | ||
/// - `content`: An `XExternString` containing the content to be written to the file. | ||
/// | ||
/// # Note | ||
/// This is an unstable function call and should be used internally only. | ||
fn _write_string(path : @ffi.XExternString, content : @ffi.XExternString) = "__moonbit_fs_unstable" "write_string_to_file" | ||
|
||
/// Checks if a path exists. | ||
/// | ||
/// # Parameters | ||
/// - `path`: An `XExternString` representing the file path. | ||
/// | ||
/// # Returns | ||
/// A boolean indicating whether the path exists. | ||
/// | ||
/// # Note | ||
/// This is an unstable function call and should be used internally only. | ||
fn path_exists(path : @ffi.XExternString) -> Bool = "__moonbit_fs_unstable" "path_exists" | ||
|
||
fn _read_to_string(path : String) -> String { | ||
let content = read_file_to_string(@ffi.string_to_extern(path)) | ||
@ffi.string_from_extern(content) | ||
} | ||
|
||
/// Writes a string to a file. | ||
/// | ||
/// # Parameters | ||
/// - `path`: A `String` representing the file path. | ||
/// - `content`: A `String` containing the content to be written to the file. | ||
pub fn write_string(~path : String, ~content : String) -> Unit { | ||
_write_string(@ffi.string_to_extern(path), @ffi.string_to_extern(content)) | ||
} | ||
|
||
/// Checks if a path exists. | ||
/// | ||
/// # Parameters | ||
/// - `path`: A `String` representing the file path. | ||
/// | ||
/// # Returns | ||
/// A boolean indicating whether the path exists. | ||
pub fn exists(~path : String) -> Bool { | ||
path_exists(@ffi.string_to_extern(path)) | ||
} | ||
|
||
/// Reads the entire contents of a file into a string. | ||
/// | ||
/// # Parameters | ||
/// - `path`: A `String` representing the file path. | ||
/// | ||
/// # Returns | ||
/// A `String` containing the file contents if the file exists, otherwise raises an error. | ||
pub fn read_to_string(~path : String) -> String! { | ||
if not(exists(~path)) { | ||
fail!("File does not exist") | ||
} | ||
_read_to_string(path) | ||
} |
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,23 @@ | ||
// Copyright 2024 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 XJSArray | ||
|
||
pub fn array_len(arr : XJSArray) -> Int = "__moonbit_fs_unstable" "array_len" | ||
|
||
pub fn array_get(arr : XJSArray, idx : Int) -> XJSValue = "__moonbit_fs_unstable" "array_get" | ||
|
||
pub fn XJSArray::op_get(self : XJSArray, idx : Int) -> XJSValue { | ||
return array_get(self, idx) | ||
} |
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,35 @@ | ||
package moonbitlang/x/fs/internal/ffi | ||
|
||
// Values | ||
fn array_get(XJSArray, Int) -> XJSValue | ||
|
||
fn array_len(XJSArray) -> Int | ||
|
||
fn jsvalue_get_string(XJSValue) -> XExternString | ||
|
||
fn jsvalue_is_string(XJSValue) -> Bool | ||
|
||
fn string_from_extern(XExternString) -> String | ||
|
||
fn string_to_extern(String) -> XExternString | ||
|
||
// Types and methods | ||
pub type XExternString | ||
|
||
pub type XJSArray | ||
impl XJSArray { | ||
op_get(Self, Int) -> XJSValue | ||
} | ||
|
||
type XJSValue | ||
|
||
type XStringCreateHandle | ||
|
||
type XStringReadHandle | ||
|
||
// Type aliases | ||
|
||
// Traits | ||
|
||
// Extension Methods | ||
|
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 @@ | ||
// Copyright 2024 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. | ||
|
||
type XJSValue | ||
|
||
pub fn jsvalue_is_string(v : XJSValue) -> Bool = "__moonbit_fs_unstable" "jsvalue_is_string" | ||
|
||
pub fn jsvalue_get_string(v : XJSValue) -> XExternString = "%identity" |
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 @@ | ||
{} |
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,54 @@ | ||
// Copyright 2024 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. | ||
|
||
type XStringCreateHandle | ||
|
||
type XStringReadHandle | ||
|
||
pub type XExternString | ||
|
||
fn begin_create_string() -> XStringCreateHandle = "__moonbit_fs_unstable" "begin_create_string" | ||
|
||
fn string_append_char(handle : XStringCreateHandle, ch : Char) = "__moonbit_fs_unstable" "string_append_char" | ||
|
||
fn finish_create_string(handle : XStringCreateHandle) -> XExternString = "__moonbit_fs_unstable" "finish_create_string" | ||
|
||
pub fn string_to_extern(s : String) -> XExternString { | ||
let handle = begin_create_string() | ||
s.iter().each(fn(ch) { string_append_char(handle, ch) }) | ||
finish_create_string(handle) | ||
} | ||
|
||
fn begin_read_string(s : XExternString) -> XStringReadHandle = "__moonbit_fs_unstable" "begin_read_string" | ||
|
||
/// Read one char from the string, returns -1 if the end of the string is reached. | ||
/// The number returned is the unicode codepoint of the character. | ||
fn string_read_char(handle : XStringReadHandle) -> Int = "__moonbit_fs_unstable" "string_read_char" | ||
|
||
fn finish_read_string(handle : XStringReadHandle) = "__moonbit_fs_unstable" "finish_read_string" | ||
|
||
pub fn string_from_extern(e : XExternString) -> String { | ||
let buf = Buffer::new() | ||
let handle = begin_read_string(e) | ||
while true { | ||
let ch = string_read_char(handle) | ||
if ch == -1 { | ||
break | ||
} else { | ||
buf.write_char(Char::from_int(ch)) | ||
} | ||
} | ||
finish_read_string(handle) | ||
buf.to_string() | ||
} |
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,12 @@ | ||
package moonbitlang/x/fs/internal | ||
|
||
// Values | ||
|
||
// Types and methods | ||
|
||
// Type aliases | ||
|
||
// Traits | ||
|
||
// Extension Methods | ||
|
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 @@ | ||
{} |
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,5 @@ | ||
{ | ||
"import": [ | ||
"moonbitlang/x/fs/internal/ffi" | ||
] | ||
} |