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

Separate build-time Context and RuntimeContext #177

Merged
merged 2 commits into from
Nov 29, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let context = Arc::new(rune_modules::default_context()?);
let context = rune_modules::default_context()?;
let options = rune::Options::default();

let mut sources = rune::Sources::new();
Expand All @@ -125,7 +125,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();

let unit = match rune::load_sources(&*context, &options, &mut sources, &mut errors, &mut warnings) {
let unit = match rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings) {
Ok(unit) => unit,
Err(rune::LoadSourcesError) => {
let mut writer = StandardStream::stderr(ColorChoice::Always);
Expand All @@ -139,7 +139,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
warnings.emit_diagnostics(&mut writer, &sources)?;
}

let vm = Vm::new(context.clone(), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut execution = vm.execute(&["calculate"], (10i64, 20i64))?;
let value = execution.async_complete().await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/rune-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
let source = runestick::Source::from_path(path)
.with_context(|| format!("reading file: {}", path.display()))?;

let context = Arc::new(context);
let runtime = Arc::new(context.runtime());
let mut sources = rune::Sources::new();

sources.insert(source);
Expand Down Expand Up @@ -297,7 +297,7 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
let mut warnings = rune::Warnings::new();

let unit = match rune::load_sources(
&*context,
&context,
&options,
&mut sources,
&mut errors,
Expand All @@ -324,7 +324,7 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
}
};

let vm = runestick::Vm::new(context.clone(), unit.clone());
let vm = runestick::Vm::new(runtime, unit.clone());

if args.dump_native_functions {
writeln!(out, "# functions")?;
Expand Down
11 changes: 2 additions & 9 deletions crates/rune-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,

let context = setup_context(config.experimental)?;

let context = Arc::new(context);
let mut options = rune::Options::default();

for option in &config.options {
Expand All @@ -202,13 +201,7 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,

let mut diagnostics = Vec::new();

let result = rune::load_sources(
&*context,
&options,
&mut sources,
&mut errors,
&mut warnings,
);
let result = rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings);

for warning in &warnings {
let span = warning.span();
Expand Down Expand Up @@ -340,7 +333,7 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
None
};

let vm = runestick::Vm::new(context, unit);
let vm = runestick::Vm::new(Arc::new(context.runtime()), unit);

let mut execution = match vm.execute(&["main"], ()) {
Ok(execution) => execution,
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let context = Arc::new(rune_modules::default_context()?);
let context = rune_modules::default_context()?;
let options = rune::Options::default();

let mut sources = rune::Sources::new();
Expand All @@ -125,7 +125,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();

let unit = match rune::load_sources(&*context, &options, &mut sources, &mut errors, &mut warnings) {
let unit = match rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings) {
Ok(unit) => unit,
Err(rune::LoadSourcesError) => {
let mut writer = StandardStream::stderr(ColorChoice::Always);
Expand All @@ -139,7 +139,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
warnings.emit_diagnostics(&mut writer, &sources)?;
}

let vm = Vm::new(context.clone(), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut execution = vm.execute(&["calculate"], (10i64, 20i64))?;
let value = execution.async_complete().await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/examples/basic_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> runestick::Result<()> {
&mut Warnings::disabled(),
)?;

let vm = Vm::new(Arc::new(context), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));
let output = vm.execute(&["main"], (33i64,))?.complete()?;
let output = i64::from_value(output)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/examples/custom_instance_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() -> runestick::Result<()> {

let unit = rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings)?;

let vm = Vm::new(Arc::new(context), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));
let output = vm.execute(&["main"], (33i64,))?.complete()?;
let output = i64::from_value(output)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/examples/custom_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() -> runestick::Result<()> {
&mut Warnings::disabled(),
)?;

let vm = Vm::new(Arc::new(context), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));
let output = vm.execute(&["main"], (Foo { field: 5 },))?.complete()?;
let output = Foo::from_value(output)?;

Expand Down
27 changes: 11 additions & 16 deletions crates/rune/examples/run_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let context = Arc::new(rune_modules::default_context()?);
let context = rune_modules::default_context()?;
let options = Options::default();
let mut sources = Sources::new();

Expand All @@ -24,27 +24,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut warnings = Warnings::new();
let mut errors = Errors::new();

let unit = match rune::load_sources(
&*context,
&options,
&mut sources,
&mut errors,
&mut warnings,
) {
Ok(unit) => unit,
Err(rune::LoadSourcesError) => {
let mut writer = StandardStream::stderr(ColorChoice::Always);
errors.emit_diagnostics(&mut writer, &sources)?;
return Ok(());
}
};
let unit =
match rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings) {
Ok(unit) => unit,
Err(rune::LoadSourcesError) => {
let mut writer = StandardStream::stderr(ColorChoice::Always);
errors.emit_diagnostics(&mut writer, &sources)?;
return Ok(());
}
};

if !warnings.is_empty() {
let mut writer = StandardStream::stderr(ColorChoice::Always);
warnings.emit_diagnostics(&mut writer, &sources)?;
}

let vm = Vm::new(context.clone(), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut execution = vm.execute(&["calculate"], (10i64, 20i64))?;
let value = execution.async_complete().await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/examples/run_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> runestick::Result<()> {
&mut Warnings::disabled(),
)?;

let vm = runestick::Vm::new(Arc::new(context), Arc::new(unit));
let vm = runestick::Vm::new(Arc::new(context.runtime()), Arc::new(unit));
let output = i64::from_value(vm.execute(&["main"], (1,))?.complete()?)?;

println!("output: {}", output);
Expand Down
5 changes: 1 addition & 4 deletions crates/rune/examples/tokio_spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ async fn main() -> runestick::Result<()> {
"#,
)?;

let context = Arc::new(context);

let main = runestick::Hash::type_hash(&["main"]);

let vm = runestick::Vm::new(context.clone(), unit.clone());
let vm = runestick::Vm::new(Arc::new(context.runtime()), unit.clone());

let execution = vm.clone().send_execute(main, (5,))?;
let t1 = tokio::spawn(async move {
Expand Down
4 changes: 1 addition & 3 deletions crates/rune/examples/use_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ fn main() -> runestick::Result<()> {
let mut context = Context::with_default_modules()?;
context.install(&module)?;

let context = Arc::new(context);

let mut sources = Sources::new();
sources.insert(Source::new(
"test",
Expand All @@ -43,7 +41,7 @@ fn main() -> runestick::Result<()> {
&mut Warnings::disabled(),
)?;

let vm = Vm::new(context, Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut foo = Foo::default();

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/examples/vec_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> runestick::Result<()> {

let unit = rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings)?;

let vm = Vm::new(Arc::new(context), Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));
let _ = vm.execute(&["main"], ())?.complete()?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! let context = Arc::new(rune_modules::default_context()?);
//! let context = rune_modules::default_context()?;
//! let options = rune::Options::default();
//!
//! let mut sources = rune::Sources::new();
Expand All @@ -123,7 +123,7 @@
//! let mut errors = rune::Errors::new();
//! let mut warnings = rune::Warnings::new();
//!
//! let unit = match rune::load_sources(&*context, &options, &mut sources, &mut errors, &mut warnings) {
//! let unit = match rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings) {
//! Ok(unit) => unit,
//! Err(rune::LoadSourcesError) => {
//! let mut writer = StandardStream::stderr(ColorChoice::Always);
Expand All @@ -137,7 +137,7 @@
//! warnings.emit_diagnostics(&mut writer, &sources)?;
//! }
//!
//! let vm = Vm::new(context.clone(), Arc::new(unit));
//! let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));
//!
//! let mut execution = vm.execute(&["calculate"], (10i64, 20i64))?;
//! let value = execution.async_complete().await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/load/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct LoadSourcesError;
/// use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = Arc::new(rune_modules::default_context()?);
/// let context = rune_modules::default_context()?;
/// let mut options = rune::Options::default();
/// let mut sources = rune::Sources::new();
/// sources.insert(Source::new("entry", r#"
Expand All @@ -56,7 +56,7 @@ pub struct LoadSourcesError;
/// let mut errors = rune::Errors::new();
/// let mut warnings = rune::Warnings::new();
///
/// let unit = match rune::load_sources(&*context, &options, &mut sources, &mut errors, &mut warnings) {
/// let unit = match rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings) {
/// Ok(unit) => unit,
/// Err(rune::LoadSourcesError) => {
/// let mut writer = StandardStream::stderr(ColorChoice::Always);
Expand All @@ -66,7 +66,7 @@ pub struct LoadSourcesError;
/// };
///
/// let unit = Arc::new(unit);
/// let vm = runestick::Vm::new(context.clone(), unit.clone());
/// let vm = runestick::Vm::new(Arc::new(context.runtime()), unit.clone());
///
/// if !warnings.is_empty() {
/// let mut writer = StandardStream::stderr(ColorChoice::Always);
Expand Down
3 changes: 2 additions & 1 deletion crates/rune/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ where
T: FromValue,
{
let (unit, _) = compile_source(context, &source).map_err(RunError::Errors)?;
let context = Arc::new(context.runtime());

let vm = runestick::Vm::new(context.clone(), Arc::new(unit));
let vm = runestick::Vm::new(context, Arc::new(unit));

let output = vm
.execute(&Item::with_item(function), args)
Expand Down
4 changes: 1 addition & 3 deletions crates/rune/tests/test_all/external_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ fn test_external_ops() {
let mut context = Context::with_default_modules().unwrap();
context.install(&module).unwrap();

let context = Arc::new(context);

let mut sources = Sources::new();
sources.insert(Source::new(
"test",
Expand All @@ -72,7 +70,7 @@ fn test_external_ops() {
.unwrap();
let unit = Arc::new(unit);

let vm = Vm::new(context, unit);
let vm = Vm::new(Arc::new(context.runtime()), unit);

{
let mut foo = External::default();
Expand Down
4 changes: 1 addition & 3 deletions crates/rune/tests/test_all/getter_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ fn test_getter_setter() {
let mut context = Context::with_default_modules().unwrap();
context.install(&module).unwrap();

let context = Arc::new(context);

let mut sources = Sources::new();
sources.insert(Source::new(
"test",
Expand All @@ -42,7 +40,7 @@ fn test_getter_setter() {
)
.unwrap();

let vm = Vm::new(context, Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut foo = Foo {
number: 42,
Expand Down
4 changes: 1 addition & 3 deletions crates/rune/tests/test_all/reference_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ fn test_reference_error() {
let mut context = Context::with_default_modules().unwrap();
context.install(&module).unwrap();

let context = Arc::new(context);

let mut sources = Sources::new();
sources.insert(Source::new(
"test",
Expand All @@ -40,7 +38,7 @@ fn test_reference_error() {
)
.unwrap();

let vm = Vm::new(context, Arc::new(unit));
let vm = Vm::new(Arc::new(context.runtime()), Arc::new(unit));

let mut foo = Foo::default();
assert_eq!(foo.value, 0);
Expand Down
1 change: 1 addition & 0 deletions crates/runestick/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ runestick-macros = {version = "0.7.0", path = "../runestick-macros"}
[dev-dependencies]
tokio = {version = "0.2.22", features = ["full"]}
checkers = "0.5.6"
static_assertions = "1.1.0"
Loading