Skip to content

Commit

Permalink
add fs ffi interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Sep 20, 2024
1 parent c175120 commit c0723ab
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 2 deletions.
182 changes: 180 additions & 2 deletions crates/moonrun/src/fs_api_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,31 @@ fn read_file_to_string(
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let contents = std::fs::read_to_string(path).expect("Failed to read file");
let contents = std::fs::read_to_string(&path).expect(&format!("Failed to read file: {}", path));
let contents = v8::String::new(scope, &contents).unwrap();
ret.set(contents.into());
}

fn read_file_to_bytes(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let contents = std::fs::read(&path).expect(&format!("Failed to read file: {}", path));
let len = contents.len();
let array_buffer = v8::ArrayBuffer::with_backing_store(
scope,
&v8::ArrayBuffer::new_backing_store_from_bytes(contents).make_shared(),
);

let uint8_array = v8::Uint8Array::new(scope, array_buffer, 0, len).unwrap();
ret.set(uint8_array.into());
}

/// `fn write_string_to_file(path: JSString, contents: JSString) -> Unit`
fn write_string_to_file(
scope: &mut v8::HandleScope,
Expand All @@ -47,11 +67,129 @@ fn write_string_to_file(
let contents = contents.to_string(scope).unwrap();
let contents = contents.to_rust_string_lossy(scope);

std::fs::write(path, contents).expect("Failed to write file");
std::fs::write(&path, contents).expect(&format!("Failed to write file: {}", path));

ret.set_undefined()
}

fn write_bytes_to_file(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let contents = args.get(1);

let uint8_array = v8::Local::<v8::Uint8Array>::try_from(contents).unwrap();
let length = uint8_array.byte_length();
let mut buffer = vec![0; length];
uint8_array.copy_contents(&mut buffer);

std::fs::write(&path, buffer).expect(&format!("Failed to write file: {}", path));

ret.set_undefined()
}

fn create_dir(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

std::fs::create_dir_all(&path).expect(&format!("Failed to create directory: {}", path));

ret.set_undefined()
}


fn read_dir(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let entries = std::fs::read_dir(&path).expect(&format!("Failed to read directory: {}", path));

let result = v8::Array::new(scope, 0);
let mut index = 0;

for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if let Some(path_str) = path.to_str() {
let js_string = v8::String::new(scope, path_str).unwrap();
result.set_index(scope, index, js_string.into()).unwrap();
index += 1;
}
}
}

ret.set(result.into());
}

fn is_file(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let is_file = std::path::Path::new(&path).is_file();
ret.set_bool(is_file);
}

fn is_dir(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

let is_dir = std::path::Path::new(&path).is_dir();
ret.set_bool(is_dir);
}

fn remove_file(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

std::fs::remove_file(&path).expect(&format!("Failed to remove file: {}", path));

ret.set_undefined();
}

fn remove_dir(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let path = args.get(0);
let path = path.to_string(scope).unwrap();
let path = path.to_rust_string_lossy(scope);

std::fs::remove_dir_all(&path).expect(&format!("Failed to remove directory: {}", path));

ret.set_undefined();
}

fn path_exists(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
Expand All @@ -74,11 +212,51 @@ pub fn init_fs<'s>(
let ident = v8::String::new(scope, "read_file_to_string").unwrap();
obj.set(scope, ident.into(), read_file_to_string.into());

let read_file_to_bytes = v8::FunctionTemplate::new(scope, read_file_to_bytes);
let read_file_to_bytes = read_file_to_bytes.get_function(scope).unwrap();
let ident = v8::String::new(scope, "read_file_to_bytes").unwrap();
obj.set(scope, ident.into(), read_file_to_bytes.into());

let write_string_to_file = v8::FunctionTemplate::new(scope, write_string_to_file);
let write_string_to_file = write_string_to_file.get_function(scope).unwrap();
let ident = v8::String::new(scope, "write_string_to_file").unwrap();
obj.set(scope, ident.into(), write_string_to_file.into());

let write_bytes_to_file = v8::FunctionTemplate::new(scope, write_bytes_to_file);
let write_bytes_to_file = write_bytes_to_file.get_function(scope).unwrap();
let ident = v8::String::new(scope, "write_bytes_to_file").unwrap();
obj.set(scope, ident.into(), write_bytes_to_file.into());

let create_dir = v8::FunctionTemplate::new(scope, create_dir);
let create_directory = create_dir.get_function(scope).unwrap();
let ident = v8::String::new(scope, "create_dir").unwrap();
obj.set(scope, ident.into(), create_directory.into());

let read_dir = v8::FunctionTemplate::new(scope, read_dir);
let read_directory = read_dir.get_function(scope).unwrap();
let ident = v8::String::new(scope, "read_dir").unwrap();
obj.set(scope, ident.into(), read_directory.into());

let is_file = v8::FunctionTemplate::new(scope, is_file);
let is_file = is_file.get_function(scope).unwrap();
let ident = v8::String::new(scope, "is_file").unwrap();
obj.set(scope, ident.into(), is_file.into());

let is_dir = v8::FunctionTemplate::new(scope, is_dir);
let is_dir = is_dir.get_function(scope).unwrap();
let ident = v8::String::new(scope, "is_dir").unwrap();
obj.set(scope, ident.into(), is_dir.into());

let remove_file = v8::FunctionTemplate::new(scope, remove_file);
let remove_file = remove_file.get_function(scope).unwrap();
let ident = v8::String::new(scope, "remove_file").unwrap();
obj.set(scope, ident.into(), remove_file.into());

let remove_dir = v8::FunctionTemplate::new(scope, remove_dir);
let remove_dir = remove_dir.get_function(scope).unwrap();
let ident = v8::String::new(scope, "remove_dir").unwrap();
obj.set(scope, ident.into(), remove_dir.into());

let path_exists = v8::FunctionTemplate::new(scope, path_exists);
let path_exists = path_exists.get_function(scope).unwrap();
let ident = v8::String::new(scope, "path_exists").unwrap();
Expand Down
53 changes: 53 additions & 0 deletions crates/moonrun/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ const INIT_JS_API: &str = r#"
return
}
function begin_read_byte_array(arr) {
return { arr: arr, i: 0 }
}
function byte_array_read_byte(handle) {
if (handle.i >= handle.arr.length) {
return -1
}
return handle.arr[handle.i++]
}
function finish_read_byte_array(handle) {
return
}
function begin_create_byte_array() {
return { arr: [] }
}
function byte_array_append_byte(handle, byte) {
handle.arr.push(byte)
}
function finish_create_byte_array(handle) {
return new Uint8Array(handle.arr)
}
function begin_read_string_array(arr) {
return { arr: arr, i: 0 }
}
function string_array_read_string(handle) {
if (handle.i >= handle.arr.length) {
return "ffi_end_of_/string_array"
}
return handle.arr[handle.i++]
}
function finish_read_string_array(handle) {
return
}
// Array ops
function array_len(arr) {
Expand All @@ -91,6 +133,17 @@ const INIT_JS_API: &str = r#"
obj.string_read_char = string_read_char
obj.finish_read_string = finish_read_string
obj.begin_read_byte_array = begin_read_byte_array
obj.byte_array_read_byte = byte_array_read_byte
obj.finish_read_byte_array = finish_read_byte_array
obj.begin_create_byte_array = begin_create_byte_array
obj.byte_array_append_byte = byte_array_append_byte
obj.finish_create_byte_array = finish_create_byte_array
obj.begin_read_string_array = begin_read_string_array
obj.string_array_read_string = string_array_read_string
obj.finish_read_string_array = finish_read_string_array
obj.array_len = array_len
obj.array_get = array_get
Expand Down

0 comments on commit c0723ab

Please sign in to comment.