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

add fs #34

Merged
merged 5 commits into from
Sep 6, 2024
Merged
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
17 changes: 17 additions & 0 deletions fs/fs.mbti
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

86 changes: 86 additions & 0 deletions fs/fs.wasm-gc.mbt
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)
}
23 changes: 23 additions & 0 deletions fs/internal/ffi/array.wasm-gc.mbt
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)
}
35 changes: 35 additions & 0 deletions fs/internal/ffi/ffi.mbti
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

19 changes: 19 additions & 0 deletions fs/internal/ffi/jsvalue.wasm-gc.mbt
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"
1 change: 1 addition & 0 deletions fs/internal/ffi/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
54 changes: 54 additions & 0 deletions fs/internal/ffi/string.wasm-gc.mbt
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()
}
12 changes: 12 additions & 0 deletions fs/internal/internal.mbti
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

1 change: 1 addition & 0 deletions fs/internal/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions fs/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"import": [
"moonbitlang/x/fs/internal/ffi"
]
}