stlib: Add draft of some IO primitives
This commit is contained in:
parent
def4adba40
commit
02b759a05b
1 changed files with 40 additions and 0 deletions
40
stdlib/io.bolt
Normal file
40
stdlib/io.bolt
Normal file
|
@ -0,0 +1,40 @@
|
|||
|
||||
pub struct String;
|
||||
|
||||
pub struct Either<L, R> {
|
||||
is_right: bool,
|
||||
value: L | R,
|
||||
}
|
||||
|
||||
mod io {
|
||||
|
||||
pub type Result<T> = Either<Error, T>;
|
||||
|
||||
pub foreign "JS" fn print(message: String) {
|
||||
process.stdout.write(message);
|
||||
}
|
||||
|
||||
pub trait Read {
|
||||
fn read(self, count: usize, target: Vec<u8>) -> Result<usize>;
|
||||
}
|
||||
|
||||
impl Read for File {
|
||||
|
||||
foreign "JS" fn read(self, count, target) -> Result<usize> {
|
||||
|
||||
import * as fs from "fs"
|
||||
|
||||
try {
|
||||
return Ok(fs.readSync(self.fd, target, 0, count, null));
|
||||
} catch (e) {
|
||||
return Err(e.code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
print("Hello, world!");
|
||||
|
Loading…
Reference in a new issue