[oden] IO: Load Strings
It's useful. The stock JS way is bad.
This commit is contained in:
parent
89045ccbcc
commit
17c701a7d6
4 changed files with 42 additions and 3 deletions
|
|
@ -82,6 +82,24 @@ impl IoImpl {
|
|||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn load_string(&self, context: &ContextRef, path: &str) -> ValueResult {
|
||||
let (value, promise) = context.new_promise()?;
|
||||
|
||||
let path = path.to_string();
|
||||
self.thread_pool.execute(Box::new(move || {
|
||||
let path = path;
|
||||
|
||||
let result = resolve_path(&path, &[]).and_then(|p| std::fs::read(p));
|
||||
promise.resolve(move |ctx: &ContextRef| {
|
||||
let result = result?;
|
||||
let string = std::str::from_utf8(&result)?;
|
||||
ctx.new_string(string)
|
||||
});
|
||||
}));
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IoAPI {}
|
||||
|
|
@ -97,6 +115,13 @@ impl IoAPI {
|
|||
ctx.new_fn(move |ctx: &ContextRef, p: String| io.load(ctx, &p))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let io = io.clone();
|
||||
builder.export(
|
||||
"load_string",
|
||||
ctx.new_fn(move |ctx: &ContextRef, p: String| io.load_string(ctx, &p))?,
|
||||
)?;
|
||||
}
|
||||
builder.build("io-core")?;
|
||||
Ok(IoAPI {})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue