diff --git a/src/io.ts b/src/io.ts new file mode 100644 index 00000000..e95fada6 --- /dev/null +++ b/src/io.ts @@ -0,0 +1,11 @@ +import * as core from "io-core"; + +/** + * Load the specified file into memory. + * + * @param path The path of the file to load. + * @returns The contents of the file. + */ +export function load(path: string): Promise { + return core.load(path); +} diff --git a/src/script/io.rs b/src/script/io.rs index 3d9aaa9a..bfda8333 100644 --- a/src/script/io.rs +++ b/src/script/io.rs @@ -71,7 +71,12 @@ impl IoImpl { self.thread_pool.execute(Box::new(move || { // TODO: Actually read the path. let path = path; - promise.resolve(move |ctx: &ContextRef| ctx.new_string(&path)); + + let result = std::fs::read(path); + promise.resolve(move |ctx: &ContextRef| match result { + Ok(v) => ctx.new_array_buffer(v), + Err(err) => Err(err.into()), + }); })); Ok(value) diff --git a/types/io-core.d.ts b/types/io-core.d.ts new file mode 100644 index 00000000..e5114f02 --- /dev/null +++ b/types/io-core.d.ts @@ -0,0 +1,3 @@ +// These are the functions exposed by the native IO module. +// +export function load(path: string): Promise;