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

feat: Enforce asynchronous operations on FS/IO #13

Merged
merged 1 commit into from
Sep 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 0 additions & 49 deletions ext/fs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,13 @@ pub async fn op_read_file(#[string] path: String) -> Result<Vec<u8>, AnyError> {
Ok(contents)
}

#[op2]
#[buffer]
pub fn op_read_file_sync(#[string] path: String) -> Result<Vec<u8>, AnyError> {
let contents = std::fs::read(path)?;
Ok(contents)
}

#[op2(async)]
#[string]
pub async fn op_read_text_file(#[string] path: String) -> Result<String, AnyError> {
let contents = tokio::fs::read_to_string(path).await?;
Ok(contents)
}

#[op2]
#[string]
pub fn op_read_text_file_sync(#[string] path: String) -> Result<String, AnyError> {
let contents = std::fs::read_to_string(path)?;
Ok(contents)
}

// Write files
#[op2(async)]
pub async fn op_write_file(
Expand All @@ -39,15 +25,6 @@ pub async fn op_write_file(
Ok(())
}

#[op2(fast)]
pub fn op_write_file_sync(
#[string] path: String,
#[buffer(copy)] contents: Vec<u8>,
) -> Result<(), AnyError> {
std::fs::write(path, contents)?;
Ok(())
}

#[op2(async)]
pub async fn op_write_text_file(
#[string] path: String,
Expand All @@ -57,28 +34,13 @@ pub async fn op_write_text_file(
Ok(())
}

#[op2(fast)]
pub fn op_write_text_file_sync(
#[string] path: String,
#[string] contents: String,
) -> Result<(), AnyError> {
std::fs::write(path, contents)?;
Ok(())
}

// Remove files
#[op2(async)]
pub async fn op_remove_file(#[string] path: String) -> Result<(), AnyError> {
tokio::fs::remove_file(path).await?;
Ok(())
}

#[op2(fast)]
pub fn op_remove_file_sync(#[string] path: String) -> Result<(), AnyError> {
std::fs::remove_file(path)?;
Ok(())
}

#[op2(async)]
pub async fn op_remove_dir(#[string] path: String, recursive: bool) -> Result<(), AnyError> {
if recursive {
Expand All @@ -89,14 +51,3 @@ pub async fn op_remove_dir(#[string] path: String, recursive: bool) -> Result<()

Ok(())
}

#[op2(fast)]
pub fn op_remove_dir_sync(#[string] path: String, recursive: bool) -> Result<(), AnyError> {
if recursive {
std::fs::remove_dir_all(path)?;
} else {
std::fs::remove_dir(path)?;
}

Ok(())
}
63 changes: 0 additions & 63 deletions ext/fs/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ function readFile(path) {
return core.ops.op_read_file(path);
}

/**
* Reads a file synchronously
* @param {string} path
* @returns {Uint8Array}
*/
function readFileSync(path) {
return core.ops.op_read_file_sync(path);
}

/**
* Reads a text file synchronously
* @param {string} path
Expand All @@ -27,15 +18,6 @@ function readTextFile(path) {
return core.ops.op_read_text_file(path);
}

/**
* Reads a text file synchronously
* @param {string} path
* @returns {string}
*/
function readTextFileSync(path) {
return core.ops.op_read_text_file_sync(path);
}

/**
* Writes a file asynchronously
* @param {string} path
Expand All @@ -46,16 +28,6 @@ function writeFile(path, contents) {
core.ops.op_write_file(path, contents);
}

/**
* Writes a file synchronously
* @param {string} path
* @param {Uint8Array} contents
* @returns {void}
*/
function writeFileSync(path, contents) {
core.ops.op_write_file_sync(path, contents);
}

/**
* Writes a text file asynchronously
* @param {string} path
Expand All @@ -66,16 +38,6 @@ function writeTextFile(path, contents) {
core.ops.op_write_text_file(path, contents);
}

/**
* Writes a text file synchronously
* @param {string} path
* @param {string} contents
* @returns {void}
*/
function writeTextFileSync(path, contents) {
core.ops.op_write_text_file_sync(path, contents);
}

/**
* Deletes file asynchronously
* @param {string} path
Expand All @@ -85,15 +47,6 @@ function removeFile(path) {
core.ops.op_remove_file(path);
}

/**
* Deletes file synchronously
* @param {string} path
* @returns {void}
*/
function removeFileSync(path) {
core.ops.op_remove_file_sync(path);
}

/**
* Deletes directory asynchronously
* @param {string} path
Expand All @@ -104,27 +57,11 @@ function removeDirectory(path, recursive) {
core.ops.op_remove_dir(path, recursive);
}

/**
* Deletes directory synchronously
* @param {string} path
* @param {boolean} recursive
* @returns {void}
*/
function removeDirectorySync(path, recursive) {
core.ops.op_remove_dir_sync(path, recursive);
}

Bueno.fs = {
readFile,
readFileSync,
readTextFile,
readTextFileSync,
writeFile,
writeFileSync,
writeTextFile,
writeTextFileSync,
removeFile,
removeFileSync,
removeDirectory,
removeDirectorySync,
};
24 changes: 0 additions & 24 deletions ext/io/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ async function read(rid, buffer) {
return (await core.read(rid)) || null;
}

/**
* Read resource id synchronously
* @param {number} rid resource id
* @param {Uint8Array} buffer buffer to read to
* @returns number of bytes that has been read or `null` (EOF)
*/
function readSync(rid, buffer) {
// Can't read into nothing
if (buffer.length === 0) return 0;
return core.readSync(rid) || null;
}

/**
* Write data to resource id asynchronously
* @param {number} rid resource id
Expand All @@ -36,16 +24,6 @@ function write(rid, data) {
return core.write(rid, data);
}

/**
* Write data to resource id synchronously
* @param {number} rid resource id
* @param {Uint8Array} data buffer to write
* @returns number of bytes that has been written
*/
function writeSync(rid, data) {
return core.writeSync(rid, data);
}

/**
* Closes resource id
* @param {number} rid resource id
Expand All @@ -56,9 +34,7 @@ function close(rid) {

Bueno.io = {
read,
readSync,
write,
writeSync,
close,

stdin: new Stdin(),
Expand Down
6 changes: 3 additions & 3 deletions ext/io/stdio.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class SharedStdio {
write(data) {
return write(this.rid, data);
return Bueno.io.write(this.rid, data);
}

read(buffer) {
return read(this.rid, buffer);
return Bueno.io.read(this.rid, buffer);
}

close() {
close(this.rid);
Bueno.io.close(this.rid);
}
}

Expand Down
6 changes: 1 addition & 5 deletions ext/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ pub mod extensions {
ops = [
// read
fs::op_read_file,
fs::op_read_file_sync,
fs::op_read_text_file,
fs::op_read_text_file_sync,
// write
fs::op_write_file,
fs::op_write_file_sync,
fs::op_write_text_file,
fs::op_write_text_file_sync,
// remove
fs::op_remove_file,
fs::op_remove_file_sync
fs::op_remove_dir,
],
esm_entry_point = "ext:bueno/runtime.js",
esm = [
Expand Down