-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support read_bytes_from_stdin ffi in moonrun (#517)
- Loading branch information
Showing
7 changed files
with
146 additions
and
2 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
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
2 changes: 2 additions & 0 deletions
2
crates/moonrun/tests/test_cases/test_read_bytes.in/.gitignore
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,2 @@ | ||
target/ | ||
.mooncakes/ |
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 @@ | ||
# username/hello |
63 changes: 63 additions & 0 deletions
63
crates/moonrun/tests/test_cases/test_read_bytes.in/main/main.mbt
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,63 @@ | ||
// moon: The build system and package manager for MoonBit. | ||
// Copyright (C) 2024 International Digital Economy Academy | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
// For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn. | ||
|
||
///| | ||
pub(all) type XByteArrayCreateHandle | ||
|
||
///| | ||
pub(all) type XByteArrayReadHandle | ||
|
||
///| | ||
pub(all) type XExternByteArray | ||
|
||
fn read_bytes_from_stdin_ffi() -> XExternByteArray = "__moonbit_io_unstable" "read_bytes_from_stdin" | ||
fn read_bytes_from_stdin() -> Bytes { | ||
let content = read_bytes_from_stdin_ffi() | ||
byte_array_from_extern(content) | ||
} | ||
|
||
pub fn byte_array_from_extern(e : XExternByteArray) -> Bytes { | ||
let buf = Array::new() | ||
let handle = begin_read_byte_array(e) | ||
while true { | ||
let ch = byte_array_read_byte(handle) | ||
if ch == -1 { | ||
break | ||
} else { | ||
buf.push(ch.to_byte()) | ||
} | ||
} | ||
finish_read_byte_array(handle) | ||
Bytes::from_array(buf) | ||
} | ||
|
||
///| | ||
fn begin_read_byte_array(s : XExternByteArray) -> XByteArrayReadHandle = "__moonbit_fs_unstable" "begin_read_byte_array" | ||
|
||
///| | ||
fn byte_array_read_byte(handle : XByteArrayReadHandle) -> Int = "__moonbit_fs_unstable" "byte_array_read_byte" | ||
|
||
///| | ||
fn finish_read_byte_array(handle : XByteArrayReadHandle) = "__moonbit_fs_unstable" "finish_read_byte_array" | ||
|
||
|
||
fn main { | ||
let bytes = read_bytes_from_stdin() | ||
println(bytes.iter().count()) | ||
} | ||
|
3 changes: 3 additions & 0 deletions
3
crates/moonrun/tests/test_cases/test_read_bytes.in/main/moon.pkg.json
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,3 @@ | ||
{ | ||
"is-main": true | ||
} |
9 changes: 9 additions & 0 deletions
9
crates/moonrun/tests/test_cases/test_read_bytes.in/moon.mod.json
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,9 @@ | ||
{ | ||
"name": "username/hello", | ||
"version": "0.1.0", | ||
"readme": "README.md", | ||
"repository": "", | ||
"license": "Apache-2.0", | ||
"keywords": [], | ||
"description": "" | ||
} |