From 56986f40fbe4462c46cd618f06cb15cb0c0f7c2a Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Fri, 15 Nov 2024 17:43:54 +0800 Subject: [PATCH] feat: support get cli args & env var (#73) * split into internal/ffi * feat: support get cli args & env var * get env args in Map[String, String] * moon fmt * bump version * moon info --- .github/workflows/check.yml | 4 ++ benchmark/benchmark.mbt | 2 +- benchmark/benchmark.mbti | 10 ++-- crypto/chacha.mbt | 6 +-- crypto/crypto.mbti | 8 ++-- crypto/sha256.mbt | 6 +-- crypto/sm3.mbt | 4 +- crypto/utils.mbt | 4 +- fs/fs.mbt | 38 +++++++-------- fs/fs.mbti | 24 +++++----- fs/fs_test.mbt | 22 ++++----- fs/internal/ffi/fs_wasm.mbt | 38 ++++++++------- fs/internal/ffi/moon.pkg.json | 8 ++-- .../ffi/byte_array_wasm.mbt | 10 ++-- {fs/internal => internal}/ffi/dir_wasm.mbt | 6 +-- internal/ffi/ffi.mbti | 34 +++++++++++++ internal/ffi/moon.pkg.json | 7 +++ {fs/internal => internal}/ffi/string_wasm.mbt | 10 ++-- json5/internal_types.mbt | 2 +- json5/json5.mbti | 10 ++-- json5/lex_main.mbt | 2 +- json5/lex_number.mbt | 44 ++++++++--------- json5/lex_prop.mbt | 4 +- json5/parse_test.mbt | 2 +- json5/types.mbt | 6 +-- json5/util.mbt | 2 +- moon.mod.json | 5 +- stack/stack.mbt | 4 +- stack/stack.mbti | 2 +- sys/internal/ffi/ffi.mbti | 13 +++++ sys/internal/ffi/moon.pkg.json | 10 ++++ sys/internal/ffi/sys_js.mbt | 42 ++++++++++++++++ sys/internal/ffi/sys_native.mbt | 23 +++++++++ sys/internal/ffi/sys_wasm.mbt | 34 +++++++++++++ sys/moon.pkg.json | 8 ++++ sys/sys.mbt | 21 ++++++++ sys/sys.mbti | 13 +++++ sys/sys_test.mbt | 23 +++++++++ time/duration.mbt | 12 ++--- time/period.mbt | 16 +++---- time/plain_date.mbt | 2 +- time/plain_date_time.mbt | 16 +++---- time/plain_time.mbt | 2 +- time/time.mbti | 20 ++++---- time/weekday.mbt | 2 +- time/zone_offset.mbt | 6 +-- time/zoned_date_time.mbt | 32 ++++++------- time/zoned_date_time_test.mbt | 48 +++++++++---------- uuid/uuid.mbt | 2 +- uuid/uuid.mbti | 4 +- uuid/variant.mbt | 4 +- 51 files changed, 458 insertions(+), 219 deletions(-) rename {fs/internal => internal}/ffi/byte_array_wasm.mbt (88%) rename {fs/internal => internal}/ffi/dir_wasm.mbt (90%) create mode 100644 internal/ffi/ffi.mbti create mode 100644 internal/ffi/moon.pkg.json rename {fs/internal => internal}/ffi/string_wasm.mbt (89%) create mode 100644 sys/internal/ffi/ffi.mbti create mode 100644 sys/internal/ffi/moon.pkg.json create mode 100644 sys/internal/ffi/sys_js.mbt create mode 100644 sys/internal/ffi/sys_native.mbt create mode 100644 sys/internal/ffi/sys_wasm.mbt create mode 100644 sys/moon.pkg.json create mode 100644 sys/sys.mbt create mode 100644 sys/sys.mbti create mode 100644 sys/sys_test.mbt diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 248b177..7ff5400 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -6,6 +6,10 @@ on: - main pull_request: +env: + # just for testing env var + MOON_TEST: "yes" + jobs: build: strategy: diff --git a/benchmark/benchmark.mbt b/benchmark/benchmark.mbt index 05a5664..799bd4e 100644 --- a/benchmark/benchmark.mbt +++ b/benchmark/benchmark.mbt @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub fn Task::new(name : String, f : () -> Unit, ~count : Int = 10) -> Task { +pub fn Task::new(name : String, f : () -> Unit, count~ : Int = 10) -> Task { { name, f, count } } diff --git a/benchmark/benchmark.mbti b/benchmark/benchmark.mbti index 73663e3..b466ae9 100644 --- a/benchmark/benchmark.mbti +++ b/benchmark/benchmark.mbti @@ -12,15 +12,15 @@ impl Criterion { type Task impl Task { - new(String, () -> Unit, ~count : Int = ..) -> Self + new(String, () -> Unit, count~ : Int = ..) -> Self run(Self) -> TaskResult } pub(readonly) struct TaskResult { - pub(readonly) task : Task - pub(readonly) average : Double - pub(readonly) max : Double - pub(readonly) min : Double + task : Task + average : Double + max : Double + min : Double } impl TaskResult { output(Self, Logger) -> Unit diff --git a/crypto/chacha.mbt b/crypto/chacha.mbt index 644bbed..5b726e9 100644 --- a/crypto/chacha.mbt +++ b/crypto/chacha.mbt @@ -295,7 +295,7 @@ pub fn chacha8( key : FixedArray[UInt], counter : UInt, block : Bytes, - ~nonce : UInt = 0 + nonce~ : UInt = 0 ) -> Bytes!Error { if key.length() != 8 { fail!("Invalid key length -- key must be 8 32-bit unsigned integers") @@ -313,7 +313,7 @@ pub fn chacha12( key : FixedArray[UInt], counter : UInt, block : Bytes, - ~nonce : UInt = 0 + nonce~ : UInt = 0 ) -> Bytes!Error { if key.length() != 8 { fail!("Invalid key length -- key must be 8 32-bit unsigned integers") @@ -331,7 +331,7 @@ pub fn chacha20( key : FixedArray[UInt], counter : UInt, block : Bytes, - ~nonce : UInt = 0 + nonce~ : UInt = 0 ) -> Bytes!Error { if key.length() != 8 { fail!("Invalid key length -- key must be 8 32-bit unsigned integers") diff --git a/crypto/crypto.mbti b/crypto/crypto.mbti index 0f3d649..16b67f0 100644 --- a/crypto/crypto.mbti +++ b/crypto/crypto.mbti @@ -3,11 +3,11 @@ package moonbitlang/x/crypto // Values fn bytes_to_hex_string(Bytes) -> String -fn chacha12(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes! +fn chacha12(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes! -fn chacha20(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes! +fn chacha20(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes! -fn chacha8(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes! +fn chacha8(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes! fn md5(Bytes) -> Bytes @@ -46,7 +46,7 @@ impl SM3Context { type Sha256Context impl Sha256Context { finalize(Self) -> Bytes - new(~reg : FixedArray[UInt] = ..) -> Self + new(reg~ : FixedArray[UInt] = ..) -> Self update(Self, Bytes) -> Unit update_from_iter(Self, Iter[Byte]) -> Unit } diff --git a/crypto/sha256.mbt b/crypto/sha256.mbt index 324592d..4e32249 100644 --- a/crypto/sha256.mbt +++ b/crypto/sha256.mbt @@ -22,7 +22,7 @@ struct Sha256Context { /// Instantiate a Sha256 context /// `reg` is the initial hash value. Defaults to Sha256's. pub fn Sha256Context::new( - ~reg : FixedArray[UInt] = [ + reg~ : FixedArray[UInt] = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] @@ -71,7 +71,7 @@ fn pad(self : Sha256Context) -> Bytes { fn transform( self : Sha256Context, data : Bytes, - ~offset : Int = 0 + offset~ : Int = 0 ) -> FixedArray[UInt] { let w = FixedArray::make(64, 0U) let mut a = self.reg[0] @@ -161,7 +161,7 @@ pub fn update(self : Sha256Context, data : Bytes) -> Unit { fn sha256_compute( self : Sha256Context, - ~data : Iter[Byte] = Iter::empty() + data~ : Iter[Byte] = Iter::empty() ) -> FixedArray[UInt] { self.update_from_iter(data) let msg = self.pad() diff --git a/crypto/sm3.mbt b/crypto/sm3.mbt index 044c73e..8c1ae40 100644 --- a/crypto/sm3.mbt +++ b/crypto/sm3.mbt @@ -108,7 +108,7 @@ fn pad(self : SM3Context) -> Bytes { fn transform( self : SM3Context, data : Bytes, - ~offset : Int = 0 + offset~ : Int = 0 ) -> FixedArray[UInt] { let w_0 = FixedArray::make(68, 0U) let w_1 = FixedArray::make(64, 0U) @@ -222,7 +222,7 @@ pub fn update(self : SM3Context, data : Bytes) -> Unit { fn sm3_compute( self : SM3Context, - ~data : Iter[Byte] = Iter::empty() + data~ : Iter[Byte] = Iter::empty() ) -> FixedArray[UInt] { self.update_from_iter(data) let msg = self.pad() diff --git a/crypto/utils.mbt b/crypto/utils.mbt index eb52d54..3431f0c 100644 --- a/crypto/utils.mbt +++ b/crypto/utils.mbt @@ -54,7 +54,7 @@ fn uint32(x : Byte) -> UInt { /// convert 4 bytes a byte sequence to a UInt in little endian /// - `x` : A byte sequence consisting of 4 or more bytes /// - `i` : An offset. e.g. i = 4 will convert `x[4~7]` to a UInt. -fn u8_to_u32le(x : Bytes, ~i : Int = 0) -> UInt { +fn u8_to_u32le(x : Bytes, i~ : Int = 0) -> UInt { uint32(x[i]) | (uint32(x[i + 1]) << 8) | (uint32(x[i + 2]) << 16) | @@ -81,7 +81,7 @@ fn u8_to_u32le(x : Bytes, ~i : Int = 0) -> UInt { // ) // } -fn bytes_u8_to_u32be(x : Bytes, ~i : Int = 0) -> UInt { +fn bytes_u8_to_u32be(x : Bytes, i~ : Int = 0) -> UInt { (uint32(x[i]) << 24) | (uint32(x[i + 1]) << 16) | (uint32(x[i + 2]) << 8) | diff --git a/fs/fs.mbt b/fs/fs.mbt index 90c17b5..39d0ec8 100644 --- a/fs/fs.mbt +++ b/fs/fs.mbt @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub type! IOError { +pub(all) type! IOError { NotFound(String) } @@ -31,7 +31,7 @@ fn IOError::to_string(self : IOError) -> String { /// # Parameters /// - `path`: A `String` representing the file path. /// - `content`: A `String` containing the content to be written to the file. -pub fn write_string_to_file(~path : String, ~content : String) -> Unit { +pub fn write_string_to_file(path~ : String, content~ : String) -> Unit { @ffi.write_string_to_file(path, content) } @@ -41,7 +41,7 @@ pub fn write_string_to_file(~path : String, ~content : String) -> Unit { /// /// - `path` : The path to the file where the bytes will be written. /// - `content` : An array of bytes to be written to the file. -pub fn write_bytes_to_file(~path : String, ~content : Bytes) -> Unit { +pub fn write_bytes_to_file(path~ : String, content~ : Bytes) -> Unit { @ffi.write_bytes_to_file(path, content) } @@ -52,7 +52,7 @@ pub fn write_bytes_to_file(~path : String, ~content : Bytes) -> Unit { /// /// # Returns /// A boolean indicating whether the path exists. -pub fn path_exists(~path : String) -> Bool { +pub fn path_exists(path~ : String) -> Bool { @ffi.path_exists(path) } @@ -63,8 +63,8 @@ pub fn path_exists(~path : String) -> Bool { /// /// # Returns /// A `String` containing the file contents if the file exists, otherwise raises an error. -pub fn read_file_to_string(~path : String) -> String! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn read_file_to_string(path~ : String) -> String! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.read_file_to_string(path) } @@ -78,8 +78,8 @@ pub fn read_file_to_string(~path : String) -> String! { /// # Returns /// /// - An array of bytes representing the content of the file. -pub fn read_file_to_bytes(~path : String) -> Bytes! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn read_file_to_bytes(path~ : String) -> Bytes! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.read_file_to_bytes(path) } @@ -92,8 +92,8 @@ pub fn read_file_to_bytes(~path : String) -> Bytes! { /// # Returns /// /// - An array of strings representing the file name and directory name in the directory. -pub fn read_dir(~path : String) -> Array[String]! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn read_dir(path~ : String) -> Array[String]! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.read_dir(path) } @@ -103,7 +103,7 @@ pub fn read_dir(~path : String) -> Array[String]! { /// # Parameters /// /// - `path` : The path where the directory should be created. -pub fn create_dir(~path : String) -> Unit { +pub fn create_dir(path~ : String) -> Unit { @ffi.create_dir(path) } @@ -116,8 +116,8 @@ pub fn create_dir(~path : String) -> Unit { /// # Returns /// /// - `Bool` : `true` if the path is a directory, `false` otherwise. -pub fn is_dir(~path : String) -> Bool! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn is_dir(path~ : String) -> Bool! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.is_dir(path) } @@ -130,8 +130,8 @@ pub fn is_dir(~path : String) -> Bool! { /// # Returns /// /// - `Bool` : `true` if the path points to a file, `false` otherwise. -pub fn is_file(~path : String) -> Bool! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn is_file(path~ : String) -> Bool! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.is_file(path) } @@ -140,8 +140,8 @@ pub fn is_file(~path : String) -> Bool! { /// # Parameters /// /// - `path` : The string path to the directory that needs to be removed. -pub fn remove_dir(~path : String) -> Unit! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn remove_dir(path~ : String) -> Unit! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.remove_dir(path) } @@ -150,7 +150,7 @@ pub fn remove_dir(~path : String) -> Unit! { /// # Parameters /// /// - `path` : The path to the file that needs to be removed. -pub fn remove_file(~path : String) -> Unit! { - guard path_exists(~path) else { raise IOError::NotFound(path) } +pub fn remove_file(path~ : String) -> Unit! { + guard path_exists(path~) else { raise IOError::NotFound(path) } @ffi.remove_file(path) } diff --git a/fs/fs.mbti b/fs/fs.mbti index c221aca..0791ecd 100644 --- a/fs/fs.mbti +++ b/fs/fs.mbti @@ -1,30 +1,30 @@ package moonbitlang/x/fs // Values -fn create_dir(~path : String) -> Unit +fn create_dir(path~ : String) -> Unit -fn is_dir(~path : String) -> Bool! +fn is_dir(path~ : String) -> Bool! -fn is_file(~path : String) -> Bool! +fn is_file(path~ : String) -> Bool! -fn path_exists(~path : String) -> Bool +fn path_exists(path~ : String) -> Bool -fn read_dir(~path : String) -> Array[String]! +fn read_dir(path~ : String) -> Array[String]! -fn read_file_to_bytes(~path : String) -> Bytes! +fn read_file_to_bytes(path~ : String) -> Bytes! -fn read_file_to_string(~path : String) -> String! +fn read_file_to_string(path~ : String) -> String! -fn remove_dir(~path : String) -> Unit! +fn remove_dir(path~ : String) -> Unit! -fn remove_file(~path : String) -> Unit! +fn remove_file(path~ : String) -> Unit! -fn write_bytes_to_file(~path : String, ~content : Bytes) -> Unit +fn write_bytes_to_file(path~ : String, content~ : Bytes) -> Unit -fn write_string_to_file(~path : String, ~content : String) -> Unit +fn write_string_to_file(path~ : String, content~ : String) -> Unit // Types and methods -pub type! IOError { +pub(all) type! IOError { NotFound(String) } impl Show for IOError diff --git a/fs/fs_test.mbt b/fs/fs_test.mbt index 13c75a0..17603d3 100644 --- a/fs/fs_test.mbt +++ b/fs/fs_test.mbt @@ -22,32 +22,32 @@ test "write_and_read" { #| , ) - assert_true!(@fs.path_exists(~path)) - let byte = @fs.read_file_to_bytes!(~path) + assert_true!(@fs.path_exists(path~)) + let byte = @fs.read_file_to_bytes!(path~) inspect!( byte, content= #|b"\x74\x61\x72\x67\x65\x74\x2f\x0a\x2e\x6d\x6f\x6f\x6e\x63\x61\x6b\x65\x73\x2f\x0a" , ) - @fs.remove_file!(~path) - assert_false!(@fs.path_exists(~path)) + @fs.remove_file!(path~) + assert_false!(@fs.path_exists(path~)) try { - @fs.read_file_to_string!(~path) |> ignore + @fs.read_file_to_string!(path~) |> ignore } catch { @fs.IOError::NotFound(_) as e => inspect!(e, content="`1.txt` does not exist") _ => return } let bytes = Bytes::from_array([65, 97].map(fn(x) { x.to_byte() })) - @fs.write_bytes_to_file(~path, content=bytes) - assert_true!(@fs.path_exists(~path)) - let content = @fs.read_file_to_string!(~path) + @fs.write_bytes_to_file(path~, content=bytes) + assert_true!(@fs.path_exists(path~)) + let content = @fs.read_file_to_string!(path~) inspect!(content, content="Aa") - @fs.remove_file!(~path) - assert_false!(@fs.path_exists(~path)) + @fs.remove_file!(path~) + assert_false!(@fs.path_exists(path~)) try { - @fs.remove_file!(~path) |> ignore + @fs.remove_file!(path~) |> ignore } catch { @fs.IOError::NotFound(_) as e => inspect!(e, content="`1.txt` does not exist") diff --git a/fs/internal/ffi/fs_wasm.mbt b/fs/internal/ffi/fs_wasm.mbt index b38c325..4acb26e 100644 --- a/fs/internal/ffi/fs_wasm.mbt +++ b/fs/internal/ffi/fs_wasm.mbt @@ -12,83 +12,89 @@ // See the License for the specific language governing permissions and // limitations under the License. +typealias XExternString = @ffi.XExternString + +typealias XExternByteArray = @ffi.XExternByteArray + +typealias XExternStringArray = @ffi.XExternStringArray + pub fn read_file_to_string(path : String) -> String { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) let content = read_file_to_string_ffi(path) - string_from_extern(content) + @ffi.string_from_extern(content) } fn read_file_to_string_ffi(path : XExternString) -> XExternString = "__moonbit_fs_unstable" "read_file_to_string" pub fn read_file_to_bytes(path : String) -> Bytes { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) let content = read_file_to_bytes_ffi(path) - byte_array_from_extern(content) + @ffi.byte_array_from_extern(content) } fn read_file_to_bytes_ffi(path : XExternString) -> XExternByteArray = "__moonbit_fs_unstable" "read_file_to_bytes" pub fn write_string_to_file(path : String, content : String) -> Unit { - let path = string_to_extern(path) - let content = string_to_extern(content) + let path = @ffi.string_to_extern(path) + let content = @ffi.string_to_extern(content) write_string_to_file_ffi(path, content) } fn write_string_to_file_ffi(path : XExternString, content : XExternString) = "__moonbit_fs_unstable" "write_string_to_file" pub fn write_bytes_to_file(path : String, content : Bytes) -> Unit { - let path = string_to_extern(path) - let content = byte_array_to_extern(content) + let path = @ffi.string_to_extern(path) + let content = @ffi.byte_array_to_extern(content) write_bytes_to_file_ffi(path, content) } fn write_bytes_to_file_ffi(path : XExternString, content : XExternByteArray) = "__moonbit_fs_unstable" "write_bytes_to_file" pub fn path_exists(path : String) -> Bool { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) path_exists_ffi(path) } fn path_exists_ffi(path : XExternString) -> Bool = "__moonbit_fs_unstable" "path_exists" pub fn read_dir(path : String) -> Array[String] { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) let dirs = read_dir_ffi(path) - string_array_from_extern(dirs) + @ffi.string_array_from_extern(dirs) } fn read_dir_ffi(path : XExternString) -> XExternStringArray = "__moonbit_fs_unstable" "read_dir" pub fn create_dir(path : String) -> Unit { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) create_dir_ffi(path) } fn create_dir_ffi(path : XExternString) = "__moonbit_fs_unstable" "create_dir" pub fn is_dir(path : String) -> Bool { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) is_dir_ffi(path) } fn is_dir_ffi(path : XExternString) -> Bool = "__moonbit_fs_unstable" "is_dir" pub fn is_file(path : String) -> Bool { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) is_file_ffi(path) } fn is_file_ffi(path : XExternString) -> Bool = "__moonbit_fs_unstable" "is_file" pub fn remove_dir(path : String) -> Unit { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) remove_dir_ffi(path) } fn remove_dir_ffi(path : XExternString) = "__moonbit_fs_unstable" "remove_dir" pub fn remove_file(path : String) -> Unit { - let path = string_to_extern(path) + let path = @ffi.string_to_extern(path) remove_file_ffi(path) } diff --git a/fs/internal/ffi/moon.pkg.json b/fs/internal/ffi/moon.pkg.json index 0741837..85473c2 100644 --- a/fs/internal/ffi/moon.pkg.json +++ b/fs/internal/ffi/moon.pkg.json @@ -1,10 +1,10 @@ { + "import": [ + "moonbitlang/x/internal/ffi" + ], "targets": { - "string_wasm.mbt": ["wasm", "wasm-gc"], - "byte_array_wasm.mbt": ["wasm", "wasm-gc"], - "dir_wasm.mbt": ["wasm", "wasm-gc"], "fs_wasm.mbt": ["wasm", "wasm-gc"], "fs_js.mbt": ["js"], "fs_native.mbt": ["native"] } -} +} \ No newline at end of file diff --git a/fs/internal/ffi/byte_array_wasm.mbt b/internal/ffi/byte_array_wasm.mbt similarity index 88% rename from fs/internal/ffi/byte_array_wasm.mbt rename to internal/ffi/byte_array_wasm.mbt index acd63ad..fa93fcb 100644 --- a/fs/internal/ffi/byte_array_wasm.mbt +++ b/internal/ffi/byte_array_wasm.mbt @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -priv type XByteArrayCreateHandle +pub(all) type XByteArrayCreateHandle -priv type XByteArrayReadHandle +pub(all) type XByteArrayReadHandle -priv type XExternByteArray +pub(all) type XExternByteArray fn begin_read_byte_array(s : XExternByteArray) -> XByteArrayReadHandle = "__moonbit_fs_unstable" "begin_read_byte_array" @@ -32,7 +32,7 @@ fn finish_create_byte_array( handle : XByteArrayCreateHandle ) -> XExternByteArray = "__moonbit_fs_unstable" "finish_create_byte_array" -fn byte_array_to_extern(s : Bytes) -> XExternByteArray { +pub fn byte_array_to_extern(s : Bytes) -> XExternByteArray { let handle = begin_create_byte_array() for i = 0; i < s.length(); i = i + 1 { byte_array_append_byte(handle, s[i].to_int()) @@ -40,7 +40,7 @@ fn byte_array_to_extern(s : Bytes) -> XExternByteArray { finish_create_byte_array(handle) } -fn byte_array_from_extern(e : XExternByteArray) -> Bytes { +pub fn byte_array_from_extern(e : XExternByteArray) -> Bytes { let buf = Array::new() let handle = begin_read_byte_array(e) while true { diff --git a/fs/internal/ffi/dir_wasm.mbt b/internal/ffi/dir_wasm.mbt similarity index 90% rename from fs/internal/ffi/dir_wasm.mbt rename to internal/ffi/dir_wasm.mbt index 57f8037..db434b7 100644 --- a/fs/internal/ffi/dir_wasm.mbt +++ b/internal/ffi/dir_wasm.mbt @@ -12,9 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -priv type XStringArrayReadHandle +pub(all) type XStringArrayReadHandle -priv type XExternStringArray +pub(all) type XExternStringArray fn begin_read_string_array(sa : XExternStringArray) -> XStringArrayReadHandle = "__moonbit_fs_unstable" "begin_read_string_array" @@ -22,7 +22,7 @@ fn string_array_read_string(handle : XStringArrayReadHandle) -> XExternString = fn finish_read_string_array(handle : XStringArrayReadHandle) = "__moonbit_fs_unstable" "finish_read_string_array" -fn string_array_from_extern(e : XExternStringArray) -> Array[String] { +pub fn string_array_from_extern(e : XExternStringArray) -> Array[String] { let buf = Array::new() let handle = begin_read_string_array(e) while true { diff --git a/internal/ffi/ffi.mbti b/internal/ffi/ffi.mbti new file mode 100644 index 0000000..0923337 --- /dev/null +++ b/internal/ffi/ffi.mbti @@ -0,0 +1,34 @@ +package moonbitlang/x/internal/ffi + +// Values +fn byte_array_from_extern(XExternByteArray) -> Bytes + +fn byte_array_to_extern(Bytes) -> XExternByteArray + +fn string_array_from_extern(XExternStringArray) -> Array[String] + +fn string_from_extern(XExternString) -> String + +fn string_to_extern(String) -> XExternString + +// Types and methods +pub(all) type XByteArrayCreateHandle + +pub(all) type XByteArrayReadHandle + +pub(all) type XExternByteArray + +pub(all) type XExternString + +pub(all) type XExternStringArray + +pub(all) type XStringArrayReadHandle + +pub(all) type XStringCreateHandle + +pub(all) type XStringReadHandle + +// Type aliases + +// Traits + diff --git a/internal/ffi/moon.pkg.json b/internal/ffi/moon.pkg.json new file mode 100644 index 0000000..8bb97bf --- /dev/null +++ b/internal/ffi/moon.pkg.json @@ -0,0 +1,7 @@ +{ + "targets": { + "string_wasm.mbt": ["wasm", "wasm-gc"], + "byte_array_wasm.mbt": ["wasm", "wasm-gc"], + "dir_wasm.mbt": ["wasm", "wasm-gc"] + } +} diff --git a/fs/internal/ffi/string_wasm.mbt b/internal/ffi/string_wasm.mbt similarity index 89% rename from fs/internal/ffi/string_wasm.mbt rename to internal/ffi/string_wasm.mbt index 66174b4..54d2daa 100644 --- a/fs/internal/ffi/string_wasm.mbt +++ b/internal/ffi/string_wasm.mbt @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -priv type XStringCreateHandle +pub(all) type XStringCreateHandle -priv type XStringReadHandle +pub(all) type XStringReadHandle -priv type XExternString +pub(all) type XExternString fn begin_create_string() -> XStringCreateHandle = "__moonbit_fs_unstable" "begin_create_string" @@ -24,7 +24,7 @@ fn string_append_char(handle : XStringCreateHandle, ch : Char) = "__moonbit_fs_u fn finish_create_string(handle : XStringCreateHandle) -> XExternString = "__moonbit_fs_unstable" "finish_create_string" -fn string_to_extern(s : String) -> XExternString { +pub fn string_to_extern(s : String) -> XExternString { let handle = begin_create_string() for i = 0; i < s.length(); i = i + 1 { string_append_char(handle, s[i]) @@ -40,7 +40,7 @@ fn string_read_char(handle : XStringReadHandle) -> Int = "__moonbit_fs_unstable" fn finish_read_string(handle : XStringReadHandle) = "__moonbit_fs_unstable" "finish_read_string" -fn string_from_extern(e : XExternString) -> String { +pub fn string_from_extern(e : XExternString) -> String { let buf = StringBuilder::new() let handle = begin_read_string(e) while true { diff --git a/json5/internal_types.mbt b/json5/internal_types.mbt index 07ee410..6fda482 100644 --- a/json5/internal_types.mbt +++ b/json5/internal_types.mbt @@ -76,7 +76,7 @@ fn add_substring( start : Int, end : Int ) -> Unit { - self.buffer = self.buffer + s.substring(~start, ~end) + self.buffer = self.buffer + s.substring(start~, end~) } fn add_char(self : StringBuilder, c : Char) -> Unit { diff --git a/json5/json5.mbti b/json5/json5.mbti index 4bb1ac8..b300012 100644 --- a/json5/json5.mbti +++ b/json5/json5.mbti @@ -4,13 +4,13 @@ package moonbitlang/x/json5 fn parse(String) -> Json!ParseError // Types and methods -pub type! ParseError ParseErrorData +pub(all) type! ParseError ParseErrorData impl ParseError { output(Self, Logger) -> Unit to_string(Self) -> String } -pub enum ParseErrorData { +pub(all) enum ParseErrorData { InvalidChar(Position, Char) InvalidEof InvalidNumber(Position, String) @@ -21,9 +21,9 @@ impl ParseErrorData { } impl Eq for ParseErrorData -pub struct Position { - pub line : Int - pub column : Int +pub(all) struct Position { + line : Int + column : Int } impl Position { op_equal(Self, Self) -> Bool diff --git a/json5/lex_main.mbt b/json5/lex_main.mbt index 7b2a943..4bf221d 100644 --- a/json5/lex_main.mbt +++ b/json5/lex_main.mbt @@ -14,7 +14,7 @@ fn lex_value( ctx : ParseContext, - ~allow_rbracket : Bool = false + allow_rbracket~ : Bool = false ) -> Token!ParseError { for { match read_char(ctx) { diff --git a/json5/lex_number.mbt b/json5/lex_number.mbt index b609ac4..cb60981 100644 --- a/json5/lex_number.mbt +++ b/json5/lex_number.mbt @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -fn lex_decimal_integer(ctx : ParseContext, ~start : Int) -> Double!ParseError { +fn lex_decimal_integer(ctx : ParseContext, start~ : Int) -> Double!ParseError { for { match read_char(ctx) { - Some('.') => return lex_decimal_point!(ctx, ~start) - Some('e' | 'E') => return lex_decimal_exponent!(ctx, ~start) + Some('.') => return lex_decimal_point!(ctx, start~) + Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~) Some(c) => { if c >= '0' && c <= '9' { continue @@ -31,12 +31,12 @@ fn lex_decimal_integer(ctx : ParseContext, ~start : Int) -> Double!ParseError { fn lex_decimal_point_leading( ctx : ParseContext, - ~start : Int + start~ : Int ) -> Double!ParseError { match read_char(ctx) { Some(c) => { if c >= '0' && c <= '9' { - return lex_decimal_fraction!(ctx, ~start) + return lex_decimal_fraction!(ctx, start~) } ctx.offset -= 1 invalid_char!(ctx) @@ -45,12 +45,12 @@ fn lex_decimal_point_leading( } } -fn lex_decimal_point(ctx : ParseContext, ~start : Int) -> Double!ParseError { +fn lex_decimal_point(ctx : ParseContext, start~ : Int) -> Double!ParseError { match read_char(ctx) { - Some('e' | 'E') => return lex_decimal_exponent!(ctx, ~start) + Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~) Some(c) => { if c >= '0' && c <= '9' { - return lex_decimal_fraction!(ctx, ~start) + return lex_decimal_fraction!(ctx, start~) } ctx.offset -= 1 return lex_number_end!(ctx, start, ctx.offset) @@ -59,10 +59,10 @@ fn lex_decimal_point(ctx : ParseContext, ~start : Int) -> Double!ParseError { } } -fn lex_decimal_fraction(ctx : ParseContext, ~start : Int) -> Double!ParseError { +fn lex_decimal_fraction(ctx : ParseContext, start~ : Int) -> Double!ParseError { for { match read_char(ctx) { - Some('e' | 'E') => return lex_decimal_exponent!(ctx, ~start) + Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~) Some(c) => { if c >= '0' && c <= '9' { continue @@ -75,12 +75,12 @@ fn lex_decimal_fraction(ctx : ParseContext, ~start : Int) -> Double!ParseError { } } -fn lex_decimal_exponent(ctx : ParseContext, ~start : Int) -> Double!ParseError { +fn lex_decimal_exponent(ctx : ParseContext, start~ : Int) -> Double!ParseError { match read_char(ctx) { - Some('+') | Some('-') => return lex_decimal_exponent_sign!(ctx, ~start) + Some('+') | Some('-') => return lex_decimal_exponent_sign!(ctx, start~) Some(c) => { if c >= '0' && c <= '9' { - return lex_decimal_exponent_integer!(ctx, ~start) + return lex_decimal_exponent_integer!(ctx, start~) } ctx.offset -= 1 invalid_char!(ctx) @@ -91,12 +91,12 @@ fn lex_decimal_exponent(ctx : ParseContext, ~start : Int) -> Double!ParseError { fn lex_decimal_exponent_sign( ctx : ParseContext, - ~start : Int + start~ : Int ) -> Double!ParseError { match read_char(ctx) { Some(c) => { if c >= '0' && c <= '9' { - return lex_decimal_exponent_integer!(ctx, ~start) + return lex_decimal_exponent_integer!(ctx, start~) } ctx.offset -= 1 invalid_char!(ctx) @@ -107,7 +107,7 @@ fn lex_decimal_exponent_sign( fn lex_decimal_exponent_integer( ctx : ParseContext, - ~start : Int + start~ : Int ) -> Double!ParseError { for { match read_char(ctx) { @@ -123,11 +123,11 @@ fn lex_decimal_exponent_integer( } } -fn lex_zero(ctx : ParseContext, ~neg : Bool, ~start : Int) -> Double!ParseError { +fn lex_zero(ctx : ParseContext, neg~ : Bool, start~ : Int) -> Double!ParseError { match read_char(ctx) { - Some('.') => return lex_decimal_point!(ctx, ~start) - Some('e' | 'E') => return lex_decimal_exponent!(ctx, ~start) - Some('x' | 'X') => return lex_hexadecimal!(ctx, ~neg) + Some('.') => return lex_decimal_point!(ctx, start~) + Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~) + Some('x' | 'X') => return lex_hexadecimal!(ctx, neg~) Some(c) => { if c >= '0' && c <= '9' { ctx.offset -= 1 @@ -140,7 +140,7 @@ fn lex_zero(ctx : ParseContext, ~neg : Bool, ~start : Int) -> Double!ParseError } } -fn lex_hexadecimal(ctx : ParseContext, ~neg : Bool) -> Double!ParseError { +fn lex_hexadecimal(ctx : ParseContext, neg~ : Bool) -> Double!ParseError { match read_char(ctx) { Some(c) => { if (c >= '0' && c <= '9') || @@ -178,7 +178,7 @@ fn lex_number_end( start : Int, end : Int ) -> Double!ParseError { - let s = ctx.input.substring(~start, ~end) + let s = ctx.input.substring(start~, end~) match @strconv.parse_double?(s) { Ok(d) => d Err(_) => diff --git a/json5/lex_prop.mbt b/json5/lex_prop.mbt index 782c8ba..faf9bba 100644 --- a/json5/lex_prop.mbt +++ b/json5/lex_prop.mbt @@ -35,7 +35,7 @@ fn lex_property_name(ctx : ParseContext) -> Token!ParseError { (c > '\x7f' && non_ascii_id_start.contains(c)) { let buffer = StringBuilder::make() buffer.add_char(c) - let s = lex_ident!(ctx, ctx.offset, ~buffer) + let s = lex_ident!(ctx, ctx.offset, buffer~) String(s) } else { parse_error!( @@ -59,7 +59,7 @@ fn lex_property_name(ctx : ParseContext) -> Token!ParseError { fn lex_ident( ctx : ParseContext, start : Int, - ~buffer : StringBuilder = StringBuilder::make() + buffer~ : StringBuilder = StringBuilder::make() ) -> String!ParseError { let mut start = start fn flush(end : Int) { diff --git a/json5/parse_test.mbt b/json5/parse_test.mbt index 377b379..fb3d5f0 100644 --- a/json5/parse_test.mbt +++ b/json5/parse_test.mbt @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -fn test_parse(input : String, ~loc : SourceLoc = _) -> @json.JsonValue!Error { +fn test_parse(input : String, loc~ : SourceLoc = _) -> @json.JsonValue!Error { let v = @json5.parse?(input) match v { Ok(v) => v diff --git a/json5/types.mbt b/json5/types.mbt index d0a3bb6..e9d28b1 100644 --- a/json5/types.mbt +++ b/json5/types.mbt @@ -12,19 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub struct Position { +pub(all) struct Position { line : Int // 1-based column : Int // 0-based } derive(Eq, Show) -pub enum ParseErrorData { +pub(all) enum ParseErrorData { InvalidChar(Position, Char) InvalidEof InvalidNumber(Position, String) InvalidIdentEscape(Position) } derive(Eq) -pub type! ParseError ParseErrorData +pub(all) type! ParseError ParseErrorData fn parse_error[X](data : ParseErrorData) -> X!ParseError { raise ParseError(data) diff --git a/json5/util.mbt b/json5/util.mbt index 16276b8..bb021f8 100644 --- a/json5/util.mbt +++ b/json5/util.mbt @@ -27,7 +27,7 @@ fn offset_to_position(input : String, offset : Int) -> Position { return Position::{ line, column } } -fn invalid_char[X](ctx : ParseContext, ~shift : Int = 0) -> X!ParseError { +fn invalid_char[X](ctx : ParseContext, shift~ : Int = 0) -> X!ParseError { let offset = ctx.offset + shift parse_error!( InvalidChar(offset_to_position(ctx.input, offset), ctx.input[offset]), diff --git a/moon.mod.json b/moon.mod.json index 668de19..adbee8a 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,9 +1,10 @@ { "name": "moonbitlang/x", - "version": "0.4.11", + "version": "0.4.12", "readme": "README.md", "repository": "https://github.com/moonbitlang/x", "license": "Apache-2.0", "keywords": ["experimental", "moonbitlang", "json", "time", "crypto"], - "description": "experimental packages for moonbitlang/core" + "description": "experimental packages for moonbitlang/core", + "warn-list": "-29" } diff --git a/stack/stack.mbt b/stack/stack.mbt index 67b06a7..6b76afe 100644 --- a/stack/stack.mbt +++ b/stack/stack.mbt @@ -478,8 +478,8 @@ test "iter" { /// let sum = s.fold(~init=0, fn(acc, i) { acc + i }) /// println(sum) // 6 /// ``` -pub fn fold[T, U](self : Stack[T], ~init : U, f : (U, T) -> U) -> U { - self.elements.fold(~init, f) +pub fn fold[T, U](self : Stack[T], init~ : U, f : (U, T) -> U) -> U { + self.elements.fold(init~, f) } test "fold" { diff --git a/stack/stack.mbti b/stack/stack.mbti index de60a81..7f26907 100644 --- a/stack/stack.mbti +++ b/stack/stack.mbti @@ -13,7 +13,7 @@ impl Stack { drop_result[T](Self[T]) -> Result[Unit, Unit] each[T](Self[T], (T) -> Unit) -> Unit equal[T : Eq](Self[T], Self[T]) -> Bool - fold[T, U](Self[T], ~init : U, (U, T) -> U) -> U + fold[T, U](Self[T], init~ : U, (U, T) -> U) -> U from_array[T](Array[T]) -> Self[T] from_list[T](@list.T[T]) -> Self[T] from_stack[T](Self[T]) -> Self[T] diff --git a/sys/internal/ffi/ffi.mbti b/sys/internal/ffi/ffi.mbti new file mode 100644 index 0000000..12b3107 --- /dev/null +++ b/sys/internal/ffi/ffi.mbti @@ -0,0 +1,13 @@ +package moonbitlang/x/sys/internal/ffi + +// Values +fn get_cli_args() -> Array[String] + +fn get_env_var() -> Map[String, String] + +// Types and methods + +// Type aliases + +// Traits + diff --git a/sys/internal/ffi/moon.pkg.json b/sys/internal/ffi/moon.pkg.json new file mode 100644 index 0000000..70abf47 --- /dev/null +++ b/sys/internal/ffi/moon.pkg.json @@ -0,0 +1,10 @@ +{ + "import": [ + "moonbitlang/x/internal/ffi" + ], + "targets": { + "sys_wasm.mbt": ["wasm", "wasm-gc"], + "sys_js.mbt": ["js"], + "sys_native.mbt": ["native"] + } +} \ No newline at end of file diff --git a/sys/internal/ffi/sys_js.mbt b/sys/internal/ffi/sys_js.mbt new file mode 100644 index 0000000..e931ca1 --- /dev/null +++ b/sys/internal/ffi/sys_js.mbt @@ -0,0 +1,42 @@ +// 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 fn get_cli_args() -> Array[String] { + get_cli_args_internal() +} + +extern "js" fn get_cli_args_internal() -> Array[String] = + #| function() { + #| return process.argv.slice(2); + #| } + +pub fn get_env_var() -> Map[String, String] { + let tmp = get_env_var_internal() + let res = {} + for i = 0; i < tmp.length(); i = i + 2 { + res[tmp[i]] = tmp[i + 1] + } + res +} + +extern "js" fn get_env_var_internal() -> Array[String] = + #| function() { + #| const env = process.env; + #| const result = []; + #| for (const key in env) { + #| result.push(key); + #| result.push(env[key]); + #| } + #| return result; + #| } diff --git a/sys/internal/ffi/sys_native.mbt b/sys/internal/ffi/sys_native.mbt new file mode 100644 index 0000000..7c46531 --- /dev/null +++ b/sys/internal/ffi/sys_native.mbt @@ -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 fn get_cli_args() -> Array[String] { + // not implement yet + panic() +} + +pub fn get_env_var() -> Map[String, String] { + // not implement yet + panic() +} diff --git a/sys/internal/ffi/sys_wasm.mbt b/sys/internal/ffi/sys_wasm.mbt new file mode 100644 index 0000000..2e34f36 --- /dev/null +++ b/sys/internal/ffi/sys_wasm.mbt @@ -0,0 +1,34 @@ +// 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. + +typealias XExternStringArray = @ffi.XExternStringArray + +pub fn get_cli_args() -> Array[String] { + let args = get_cli_args_ffi() + @ffi.string_array_from_extern(args) +} + +fn get_cli_args_ffi() -> XExternStringArray = "__moonbit_fs_unstable" "args_get" + +pub fn get_env_var() -> Map[String, String] { + let env = get_env_var_ffi() + let tmp = @ffi.string_array_from_extern(env) + let res = {} + for i = 0; i < tmp.length(); i = i + 2 { + res[tmp[i]] = tmp[i + 1] + } + res +} + +fn get_env_var_ffi() -> XExternStringArray = "__moonbit_fs_unstable" "env_get_vars" diff --git a/sys/moon.pkg.json b/sys/moon.pkg.json new file mode 100644 index 0000000..6fa5364 --- /dev/null +++ b/sys/moon.pkg.json @@ -0,0 +1,8 @@ +{ + "import": [ + "moonbitlang/x/sys/internal/ffi" + ], + "targets": { + "sys_test.mbt": ["not", "native"] + } +} diff --git a/sys/sys.mbt b/sys/sys.mbt new file mode 100644 index 0000000..c8d825c --- /dev/null +++ b/sys/sys.mbt @@ -0,0 +1,21 @@ +// 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 fn get_cli_args() -> Array[String] { + @ffi.get_cli_args() +} + +pub fn get_env_var() -> Map[String, String] { + @ffi.get_env_var() +} diff --git a/sys/sys.mbti b/sys/sys.mbti new file mode 100644 index 0000000..848b103 --- /dev/null +++ b/sys/sys.mbti @@ -0,0 +1,13 @@ +package moonbitlang/x/sys + +// Values +fn get_cli_args() -> Array[String] + +fn get_env_var() -> Map[String, String] + +// Types and methods + +// Type aliases + +// Traits + diff --git a/sys/sys_test.mbt b/sys/sys_test.mbt new file mode 100644 index 0000000..0476f58 --- /dev/null +++ b/sys/sys_test.mbt @@ -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. + +test "get_env_var" { + let res = @sys.get_env_var()["MOON_TEST"] + inspect!( + res, + content= + #|Some("yes") + , + ) +} diff --git a/time/duration.mbt b/time/duration.mbt index 1fba7e0..0378a82 100644 --- a/time/duration.mbt +++ b/time/duration.mbt @@ -20,10 +20,10 @@ struct Duration { /// Creates a Duration from hours, minutes, seconds and nanoseconds. pub fn Duration::of( - ~hours : Int64 = 0L, - ~minutes : Int64 = 0L, - ~seconds : Int64 = 0L, - ~nanoseconds : Int64 = 0L + hours~ : Int64 = 0L, + minutes~ : Int64 = 0L, + seconds~ : Int64 = 0L, + nanoseconds~ : Int64 = 0L ) -> Duration!Error { let hours_sec = checked_mul_int64!(hours, seconds_per_hour) let minutes_sec = checked_mul_int64!(minutes, seconds_per_minute) @@ -94,7 +94,7 @@ pub fn Duration::from_string(str : String) -> Duration!Error { _ => fail!(invalid_duration_err) } } - Duration::of!(~hours, ~minutes, ~seconds, ~nanoseconds) + Duration::of!(hours~, minutes~, seconds~, nanoseconds~) } /// Returns a string representation of this duration using ISO 8601 representation. @@ -204,7 +204,7 @@ pub fn add_nanoseconds(self : Duration, nanoseconds : Int64) -> Duration!Error { nanoseconds / nanoseconds_per_second, ) let nanoseconds = nanoseconds % nanoseconds_per_second - Duration::of!(~seconds, ~nanoseconds) + Duration::of!(seconds~, nanoseconds~) } /// Adds other duration to this duration, and returns a new duration. diff --git a/time/period.mbt b/time/period.mbt index 52c54c4..209e0d8 100644 --- a/time/period.mbt +++ b/time/period.mbt @@ -21,9 +21,9 @@ struct Period { /// Creates a Period from years, months, and days. pub fn Period::of( - ~years : Int = 0, - ~months : Int = 0, - ~days : Int = 0 + years~ : Int = 0, + months~ : Int = 0, + days~ : Int = 0 ) -> Period { { years, months, days } } @@ -63,7 +63,7 @@ pub fn Period::from_string(str : String) -> Period!Error { _ => fail!(invalid_period_err) } } - Period::of(~years, ~months, ~days) + Period::of(years~, months~, days~) } /// Returns a string representation of this period using ISO 8601 representation. @@ -173,7 +173,7 @@ pub fn multiply(self : Period, n : Int) -> Period!Error { let years = checked_mul_int!(self.years, n) let months = checked_mul_int!(self.months, n) let days = checked_mul_int!(self.days, n) - Period::of(~years, ~months, ~days) + Period::of(years~, months~, days~) } /// Returns a new period with all elements in this period negated. @@ -186,7 +186,7 @@ pub fn with_years(self : Period, years : Int) -> Period { if years == self.years { self } else { - Period::of(~years, months=self.months, days=self.days) + Period::of(years~, months=self.months, days=self.days) } } @@ -195,7 +195,7 @@ pub fn with_months(self : Period, months : Int) -> Period { if months == self.months { self } else { - Period::of(years=self.years, ~months, days=self.days) + Period::of(years=self.years, months~, days=self.days) } } @@ -204,7 +204,7 @@ pub fn with_days(self : Period, days : Int) -> Period { if days == self.days { self } else { - Period::of(years=self.years, months=self.months, ~days) + Period::of(years=self.years, months=self.months, days~) } } diff --git a/time/plain_date.mbt b/time/plain_date.mbt index bf6c4e1..b82d243 100644 --- a/time/plain_date.mbt +++ b/time/plain_date.mbt @@ -336,7 +336,7 @@ pub fn until(self : PlainDate, end : PlainDate) -> Period!Error { } let years = (total_months / 12L).to_int() let months = (total_months % 12L).to_int() - Period::of(~years, ~months, ~days) + Period::of(years~, months~, days~) } // ***************************** diff --git a/time/plain_date_time.mbt b/time/plain_date_time.mbt index 3b202cc..7d89ac1 100644 --- a/time/plain_date_time.mbt +++ b/time/plain_date_time.mbt @@ -23,10 +23,10 @@ pub fn PlainDateTime::of( year : Int, month : Int, day : Int, - ~hour : Int = 0, - ~minute : Int = 0, - ~second : Int = 0, - ~nanosecond : Int = 0 + hour~ : Int = 0, + minute~ : Int = 0, + second~ : Int = 0, + nanosecond~ : Int = 0 ) -> PlainDateTime!Error { { date: PlainDate::of!(year, month, day), @@ -303,10 +303,10 @@ pub fn to_plain_time(self : PlainDateTime) -> PlainTime { fn overflowing_add( self : PlainDateTime, - ~h : Int64 = 0L, - ~m : Int64 = 0L, - ~s : Int64 = 0L, - ~ns : Int64 = 0L + h~ : Int64 = 0L, + m~ : Int64 = 0L, + s~ : Int64 = 0L, + ns~ : Int64 = 0L ) -> PlainDateTime!Error { if h == 0L && m == 0L && s == 0L && ns == 0L { return self diff --git a/time/plain_time.mbt b/time/plain_time.mbt index 08cf0cd..be46c99 100644 --- a/time/plain_time.mbt +++ b/time/plain_time.mbt @@ -264,7 +264,7 @@ pub fn with_nanosecond(self : PlainTime, nanosecond : Int) -> PlainTime!Error { /// Returns the duration between this time and another time. pub fn until(self : PlainTime, end : PlainTime) -> Duration!Error { let nanoseconds = end.nanosecond_of_day() - self.nanosecond_of_day() - Duration::of!(~nanoseconds) + Duration::of!(nanoseconds~) } /// Combines this time with a date to creates a PlainDateTime diff --git a/time/time.mbti b/time/time.mbti index 11eca50..73375b2 100644 --- a/time/time.mbti +++ b/time/time.mbti @@ -1,11 +1,11 @@ package moonbitlang/x/time // Values -fn date_time(Int, Int, Int, ~hour : Int = .., ~minute : Int = .., ~second : Int = .., ~zone : Zone = ..) -> ZonedDateTime! +fn date_time(Int, Int, Int, hour~ : Int = .., minute~ : Int = .., second~ : Int = .., zone~ : Zone = ..) -> ZonedDateTime! fn fixed_zone(String, Int) -> Zone! -fn unix(Int64, ~nanosecond : Int = .., ~zone : Zone = ..) -> ZonedDateTime! +fn unix(Int64, nanosecond~ : Int = .., zone~ : Zone = ..) -> ZonedDateTime! let utc_offset : ZoneOffset @@ -24,7 +24,7 @@ impl Duration { is_neg(Self) -> Bool is_zero(Self) -> Bool nanoseconds(Self) -> Int - of(~hours : Int64 = .., ~minutes : Int64 = .., ~seconds : Int64 = .., ~nanoseconds : Int64 = ..) -> Self! + of(hours~ : Int64 = .., minutes~ : Int64 = .., seconds~ : Int64 = .., nanoseconds~ : Int64 = ..) -> Self! op_add(Self, Self) -> Self! op_equal(Self, Self) -> Bool seconds(Self) -> Int64 @@ -53,7 +53,7 @@ impl Period { months(Self) -> Int multiply(Self, Int) -> Self! negated(Self) -> Self! - of(~years : Int = .., ~months : Int = .., ~days : Int = ..) -> Self + of(years~ : Int = .., months~ : Int = .., days~ : Int = ..) -> Self op_add(Self, Self) -> Self! op_equal(Self, Self) -> Bool op_sub(Self, Self) -> Self! @@ -131,7 +131,7 @@ impl PlainDateTime { month(Self) -> Int months_in_year(Self) -> Int nanosecond(Self) -> Int - of(Int, Int, Int, ~hour : Int = .., ~minute : Int = .., ~second : Int = .., ~nanosecond : Int = ..) -> Self! + of(Int, Int, Int, hour~ : Int = .., minute~ : Int = .., second~ : Int = .., nanosecond~ : Int = ..) -> Self! op_equal(Self, Self) -> Bool ordinal(Self) -> Int second(Self) -> Int @@ -185,7 +185,7 @@ impl Compare for PlainTime impl Eq for PlainTime impl Show for PlainTime -pub enum Weekday { +pub(all) enum Weekday { Monday Tuesday Wednesday @@ -216,7 +216,7 @@ impl ZoneOffset { from_seconds(Int) -> Self! id(Self) -> String is_dst(Self) -> Bool - of(~hours : Int = .., ~minutes : Int = .., ~seconds : Int = ..) -> Self! + of(hours~ : Int = .., minutes~ : Int = .., seconds~ : Int = ..) -> Self! op_equal(Self, Self) -> Bool seconds(Self) -> Int to_string(Self) -> String @@ -239,15 +239,15 @@ impl ZonedDateTime { days_in_year(Self) -> Int era(Self) -> String era_year(Self) -> Int - from_plain_datetime(PlainDateTime, ~zone : Zone = ..) -> Self - from_unix_second(Int64, ~nanosecond : Int = .., ~zone : Zone = ..) -> Self! + from_plain_datetime(PlainDateTime, zone~ : Zone = ..) -> Self + from_unix_second(Int64, nanosecond~ : Int = .., zone~ : Zone = ..) -> Self! hour(Self) -> Int in_leap_year(Self) -> Bool minute(Self) -> Int month(Self) -> Int months_in_year(Self) -> Int nanosecond(Self) -> Int - of(Int, Int, Int, ~hour : Int = .., ~minute : Int = .., ~second : Int = .., ~zone : Zone = ..) -> Self! + of(Int, Int, Int, hour~ : Int = .., minute~ : Int = .., second~ : Int = .., zone~ : Zone = ..) -> Self! offset(Self) -> ZoneOffset ordinal(Self) -> Int second(Self) -> Int diff --git a/time/weekday.mbt b/time/weekday.mbt index 4221180..9288462 100644 --- a/time/weekday.mbt +++ b/time/weekday.mbt @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub enum Weekday { +pub(all) enum Weekday { Monday Tuesday Wednesday diff --git a/time/zone_offset.mbt b/time/zone_offset.mbt index 7206655..5a372e5 100644 --- a/time/zone_offset.mbt +++ b/time/zone_offset.mbt @@ -36,9 +36,9 @@ pub let utc_offset : ZoneOffset = ZoneOffset::{ /// Creates a offset from hours, minutes and seconds. pub fn ZoneOffset::of( - ~hours : Int = 0, - ~minutes : Int = 0, - ~seconds : Int = 0 + hours~ : Int = 0, + minutes~ : Int = 0, + seconds~ : Int = 0 ) -> ZoneOffset!Error { validate_offset!(hours, minutes, seconds) let secs = hours * seconds_per_hour.to_int() + diff --git a/time/zoned_date_time.mbt b/time/zoned_date_time.mbt index edc01ce..aaf8a0f 100644 --- a/time/zoned_date_time.mbt +++ b/time/zoned_date_time.mbt @@ -25,22 +25,22 @@ pub fn date_time( year : Int, month : Int, day : Int, - ~hour : Int = 0, - ~minute : Int = 0, - ~second : Int = 0, - ~zone : Zone = utc_zone + hour~ : Int = 0, + minute~ : Int = 0, + second~ : Int = 0, + zone~ : Zone = utc_zone ) -> ZonedDateTime!Error { - ZonedDateTime::of!(year, month, day, ~hour, ~minute, ~second, ~zone) + ZonedDateTime::of!(year, month, day, hour~, minute~, second~, zone~) } /// Creates a ZonedDateTime from elapsed seconds since the unix epoch and a time zone. /// The default time zone is UTC+0. pub fn unix( second : Int64, - ~nanosecond : Int = 0, - ~zone : Zone = utc_zone + nanosecond~ : Int = 0, + zone~ : Zone = utc_zone ) -> ZonedDateTime!Error { - ZonedDateTime::from_unix_second!(second, ~nanosecond, ~zone) + ZonedDateTime::from_unix_second!(second, nanosecond~, zone~) } /// Creates a ZonedDateTime from year, month, day, hour, minute and second. @@ -49,12 +49,12 @@ pub fn ZonedDateTime::of( year : Int, month : Int, day : Int, - ~hour : Int = 0, - ~minute : Int = 0, - ~second : Int = 0, - ~zone : Zone = utc_zone + hour~ : Int = 0, + minute~ : Int = 0, + second~ : Int = 0, + zone~ : Zone = utc_zone ) -> ZonedDateTime!Error { - let datetime = PlainDateTime::of!(year, month, day, ~hour, ~minute, ~second) + let datetime = PlainDateTime::of!(year, month, day, hour~, minute~, second~) create_from_plain(datetime, zone) } @@ -62,7 +62,7 @@ pub fn ZonedDateTime::of( /// The default time zone is UTC+0. pub fn ZonedDateTime::from_plain_datetime( datetime : PlainDateTime, - ~zone : Zone = utc_zone + zone~ : Zone = utc_zone ) -> ZonedDateTime { create_from_plain(datetime, zone) } @@ -71,8 +71,8 @@ pub fn ZonedDateTime::from_plain_datetime( /// The default time zone is UTC+0. pub fn ZonedDateTime::from_unix_second( second : Int64, - ~nanosecond : Int = 0, - ~zone : Zone = utc_zone + nanosecond~ : Int = 0, + zone~ : Zone = utc_zone ) -> ZonedDateTime!Error { let offset = zone.lookup_offset(second) let datetime = PlainDateTime::from_unix_second!(second, nanosecond, offset) diff --git a/time/zoned_date_time_test.mbt b/time/zoned_date_time_test.mbt index 60124ee..9744c9c 100644 --- a/time/zoned_date_time_test.mbt +++ b/time/zoned_date_time_test.mbt @@ -15,7 +15,7 @@ test "date_time" { let zone = @time.fixed_zone!("Asia/Shanghai", 8 * 60 * 60) inspect!( - @time.date_time!(2000, 1, 1, hour=10, minute=20, second=30, ~zone), + @time.date_time!(2000, 1, 1, hour=10, minute=20, second=30, zone~), content="2000-01-01T10:20:30+08:00[Asia/Shanghai]", ) } @@ -23,7 +23,7 @@ test "date_time" { test "unix" { let zone = @time.fixed_zone!("Asia/Shanghai", 8 * 60 * 60) inspect!( - @time.unix!(1714227729L, nanosecond=1000, ~zone), + @time.unix!(1714227729L, nanosecond=1000, zone~), content="2024-04-27T22:22:09.000001+08:00[Asia/Shanghai]", ) } @@ -35,7 +35,7 @@ test "of" { ) let zone = @time.fixed_zone!("Asia/Shanghai", 8 * 60 * 60) inspect!( - @time.ZonedDateTime::of!(2000, 1, 1, hour=1, minute=2, second=3, ~zone), + @time.ZonedDateTime::of!(2000, 1, 1, hour=1, minute=2, second=3, zone~), content="2000-01-01T01:02:03+08:00[Asia/Shanghai]", ) } @@ -44,7 +44,7 @@ test "from_plain_datetime" { let zone = @time.fixed_zone!("Asia/Shanghai", 8 * 60 * 60) let datetime = @time.PlainDateTime::of!(2000, 1, 1) inspect!( - @time.ZonedDateTime::from_plain_datetime(datetime, ~zone), + @time.ZonedDateTime::from_plain_datetime(datetime, zone~), content="2000-01-01T00:00:00+08:00[Asia/Shanghai]", ) } @@ -60,7 +60,7 @@ test "from_unix_second" { content="1970-01-01T01:00:00.000001Z", ) inspect!( - @time.ZonedDateTime::from_unix_second!(0L, ~zone), + @time.ZonedDateTime::from_unix_second!(0L, zone~), content="1970-01-01T08:00:00+08:00[Asia/Shanghai]", ) } @@ -69,7 +69,7 @@ test "to_unix_second" { let zone = @time.fixed_zone!("Asia/Shanghai", 8 * 60 * 60) inspect!(@time.ZonedDateTime::of!(1970, 1, 1).to_unix_second(), content="0") inspect!( - @time.ZonedDateTime::of!(1970, 1, 1, ~zone).to_unix_second(), + @time.ZonedDateTime::of!(1970, 1, 1, zone~).to_unix_second(), content="-28800", ) } @@ -83,7 +83,7 @@ test "conversions" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!(zdt.to_unix_second(), content="946659723") inspect!(zdt.to_plain_date(), content="2000-01-01") @@ -100,7 +100,7 @@ test "getters" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!(zdt.era(), content="CE") inspect!(zdt.era_year(), content="2000") @@ -130,7 +130,7 @@ test "add_years" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_years!(1L), @@ -147,7 +147,7 @@ test "add_months" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_months!(1L), @@ -164,7 +164,7 @@ test "add_weeks" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_weeks!(1L), @@ -181,7 +181,7 @@ test "add_days" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_days!(1L), @@ -198,7 +198,7 @@ test "add_hours" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_hours!(1L), @@ -215,7 +215,7 @@ test "add_minutes" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_minutes!(1L), @@ -232,7 +232,7 @@ test "add_seconds" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_seconds!(1L), @@ -249,7 +249,7 @@ test "add_nanoseconds" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.add_nanoseconds!(1000L), @@ -266,7 +266,7 @@ test "with_year" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_year!(2024), @@ -283,7 +283,7 @@ test "with_month" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_month!(2), @@ -300,7 +300,7 @@ test "with_day" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_day!(31), @@ -317,7 +317,7 @@ test "with_ordinal" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_ordinal!(365), @@ -334,7 +334,7 @@ test "with_hour" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_hour!(12), @@ -351,7 +351,7 @@ test "with_minute" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_minute!(30), @@ -368,7 +368,7 @@ test "with_second" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_second!(30), @@ -385,7 +385,7 @@ test "with_nanosecond" { hour=1, minute=2, second=3, - ~zone, + zone~, ) inspect!( zdt.with_nanosecond!(999_999_999), diff --git a/uuid/uuid.mbt b/uuid/uuid.mbt index 2e01fa8..dba1275 100644 --- a/uuid/uuid.mbt +++ b/uuid/uuid.mbt @@ -186,7 +186,7 @@ pub fn to_string(self : UUID) -> String { rv[i - 1] = dash.to_byte() continue v, i - 2, i - 2, d - 1 } else { - break rv.to_string() + break rv.to_unchecked_string() } } } diff --git a/uuid/uuid.mbti b/uuid/uuid.mbti index 00278cb..230781c 100644 --- a/uuid/uuid.mbti +++ b/uuid/uuid.mbti @@ -21,7 +21,7 @@ impl Compare for UUID impl Eq for UUID impl Show for UUID -pub enum Variant { +pub(all) enum Variant { ReservedNCS RFC4122(Version) ReservedMicrosoft @@ -32,7 +32,7 @@ impl Variant { } impl Show for Variant -pub enum Version { +pub(all) enum Version { V1 V2 V3 diff --git a/uuid/variant.mbt b/uuid/variant.mbt index aa40797..9e44f09 100644 --- a/uuid/variant.mbt +++ b/uuid/variant.mbt @@ -16,7 +16,7 @@ // https://github.com/python/cpython/blob/3.12/Lib/uuid.py /// The UUID variant. -pub enum Variant { +pub(all) enum Variant { ReservedNCS RFC4122(Version) ReservedMicrosoft @@ -27,7 +27,7 @@ pub enum Variant { /// /// This is only used when the variant is `RFC4122`. /// -pub enum Version { +pub(all) enum Version { V1 V2 V3