Skip to content

Commit

Permalink
Start working on manifest loading
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Dec 5, 2021
1 parent dfdeb98 commit 16a470f
Show file tree
Hide file tree
Showing 21 changed files with 1,117 additions and 183 deletions.
5 changes: 5 additions & 0 deletions Rune.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = [
"benches",
"examples",
]
3 changes: 3 additions & 0 deletions benches/Rune.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "rune-benches"
version = "0.0.0"
4 changes: 2 additions & 2 deletions crates/rune-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ codespan-reporting = "0.11.1"
anyhow = { version = "1.0.49", features = ["std"] }
structopt = { version = "0.3.25", default-features = false, features = ["wrap_help", "suggestions", "color"] }

rune = {version = "0.10.0", path = "../rune"}
rune-modules = {version = "0.10.0", path = "../rune-modules", features = ["full", "experiments"]}
rune = { version = "0.10.0", path = "../rune", features = ["workspace"] }
rune-modules = { version = "0.10.0", path = "../rune-modules", features = ["full", "experiments"] }

[build-dependencies]
anyhow = "1.0.49"
Expand Down
39 changes: 19 additions & 20 deletions crates/rune-cli/src/benches.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{ExitCode, SharedFlags};
use crate::{ExitCode, Io, SharedFlags};
use rune::compile::{Item, Meta};
use rune::runtime::{Function, Unit, Value};
use rune::termcolor::StandardStream;
use rune::{Any, Context, ContextError, Hash, Module, Sources};
use rune_modules::capture_io::CaptureIo;
use std::fmt;
Expand Down Expand Up @@ -45,18 +44,18 @@ pub(crate) fn test_module() -> Result<Module, ContextError> {

/// Run benchmarks.
pub(crate) async fn run(
o: &mut StandardStream,
io: &mut Io<'_>,
args: &Flags,
context: &Context,
io: Option<&CaptureIo>,
capture_io: Option<&CaptureIo>,
unit: Arc<Unit>,
sources: &Sources,
fns: &[(Hash, Meta)],
) -> anyhow::Result<ExitCode> {
let runtime = Arc::new(context.runtime());
let mut vm = rune::Vm::new(runtime, unit);

writeln!(o, "Found {} benches...", fns.len())?;
writeln!(io.stdout, "Found {} benches...", fns.len())?;

let mut any_error = false;

Expand All @@ -65,14 +64,14 @@ pub(crate) async fn run(
let mut bencher = Bencher::default();

if let Err(error) = vm.call(*hash, (&mut bencher,)) {
writeln!(o, "{}: Error in benchmark", item)?;
error.emit(o, sources)?;
writeln!(io.stdout, "{}: Error in benchmark", item)?;
error.emit(io.stdout, sources)?;
any_error = true;

if let Some(io) = io {
writeln!(o, "-- output --")?;
io.drain_into(&mut *o)?;
writeln!(o, "-- end output --")?;
if let Some(capture_io) = capture_io {
writeln!(io.stdout, "-- output --")?;
capture_io.drain_into(&mut *io.stdout)?;
writeln!(io.stdout, "-- end output --")?;
}

continue;
Expand All @@ -81,13 +80,13 @@ pub(crate) async fn run(
let multiple = bencher.fns.len() > 1;

for (i, f) in bencher.fns.iter().enumerate() {
if let Err(e) = bench_fn(o, i, item, args, f, multiple) {
writeln!(o, "{}: Error in bench iteration: {}", item, e)?;
if let Err(e) = bench_fn(io, i, item, args, f, multiple) {
writeln!(io.stdout, "{}: Error in bench iteration: {}", item, e)?;

if let Some(io) = io {
writeln!(o, "-- output --")?;
io.drain_into(&mut *o)?;
writeln!(o, "-- end output --")?;
if let Some(capture_io) = capture_io {
writeln!(io.stdout, "-- output --")?;
capture_io.drain_into(&mut *io.stdout)?;
writeln!(io.stdout, "-- end output --")?;
}

any_error = true;
Expand All @@ -103,7 +102,7 @@ pub(crate) async fn run(
}

fn bench_fn(
o: &mut StandardStream,
io: &mut Io<'_>,
i: usize,
item: &Item,
args: &Flags,
Expand Down Expand Up @@ -145,9 +144,9 @@ fn bench_fn(
};

if multiple {
writeln!(o, "bench {}#{}: {}", item, i, format)?;
writeln!(io.stdout, "bench {}#{}: {}", item, i, format)?;
} else {
writeln!(o, "bench {}: {}", item, format)?;
writeln!(io.stdout, "bench {}: {}", item, format)?;
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions crates/rune-cli/src/check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{visitor, ExitCode, SharedFlags};
use crate::{visitor, Config, ExitCode, Io, SharedFlags};
use anyhow::{Context, Result};
use rune::compile::FileSourceLoader;
use rune::termcolor::StandardStream;
use rune::{Diagnostics, Options, Source, Sources};
use std::io::Write;
use std::path::Path;
Expand All @@ -18,14 +17,15 @@ pub(crate) struct Flags {
}

pub(crate) fn run(
o: &mut StandardStream,
io: &mut Io<'_>,
c: &Config,
flags: &Flags,
options: &Options,
path: &Path,
) -> Result<ExitCode> {
writeln!(o, "Checking: {}", path.display())?;
writeln!(io.stdout, "Checking: {}", path.display())?;

let context = flags.shared.context()?;
let context = flags.shared.context(c)?;

let source =
Source::from_path(path).with_context(|| format!("reading file: {}", path.display()))?;
Expand All @@ -51,7 +51,7 @@ pub(crate) fn run(
.with_source_loader(&mut source_loader)
.build();

diagnostics.emit(o, &sources)?;
diagnostics.emit(&mut io.stdout.lock(), &sources)?;

if diagnostics.has_error() || flags.warnings_are_errors && diagnostics.has_warning() {
Ok(ExitCode::Failure)
Expand Down
21 changes: 10 additions & 11 deletions crates/rune-cli/src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::{visitor, Args};
use crate::{visitor, Args, Io};
use anyhow::{anyhow, Context as _, Result};
use rune::compile::FileSourceLoader;
use rune::compile::Meta;
use rune::termcolor::StandardStream;
use rune::Diagnostics;
use rune::{Context, Hash, Options, Source, Sources, Unit};
use std::collections::VecDeque;
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::{path::Path, sync::Arc};

pub(crate) struct Load {
Expand All @@ -20,14 +18,14 @@ pub(crate) struct Load {

/// Load context and code for a given path
pub(crate) fn load(
o: &mut StandardStream,
io: &mut Io<'_>,
context: &Context,
args: &Args,
options: &Options,
path: &Path,
attribute: visitor::Attribute,
) -> Result<Load> {
let shared = args.shared();
let shared = args.cmd.shared();

let bytecode_path = path.with_extension("rnc");

Expand Down Expand Up @@ -79,7 +77,7 @@ pub(crate) fn load(
.with_source_loader(&mut source_loader)
.build();

diagnostics.emit(o, &sources)?;
diagnostics.emit(io.stdout, &sources)?;
let unit = result?;

if options.bytecode {
Expand Down Expand Up @@ -112,11 +110,12 @@ fn should_cache_be_used(source: &Path, cached: &Path) -> io::Result<bool> {
Ok(source.modified()? < cached.modified()?)
}

pub(crate) fn walk_paths(
pub(crate) fn recurse_paths(
recursive: bool,
paths: Vec<PathBuf>,
) -> impl Iterator<Item = io::Result<PathBuf>> {
let mut queue = paths.into_iter().collect::<VecDeque<_>>();
first: Box<Path>,
) -> impl Iterator<Item = io::Result<Box<Path>>> {
let mut queue = VecDeque::with_capacity(1);
queue.push_back(first);

std::iter::from_fn(move || loop {
let path = queue.pop_front()?;
Expand Down Expand Up @@ -144,7 +143,7 @@ pub(crate) fn walk_paths(
Err(error) => return Some(Err(error)),
};

queue.push_back(e.path());
queue.push_back(e.path().into());
}
})
}
Loading

0 comments on commit 16a470f

Please sign in to comment.