Skip to content

Commit

Permalink
feat: Replace Compiler with the ThreadExt trait
Browse files Browse the repository at this point in the history
BREAKING CHANGE

Remove all use of `Compiler` and change each method call to use the
equivalent method on `ThreadExt`.
  • Loading branch information
Marwes committed Oct 1, 2019
1 parent ad7f063 commit c16132e
Show file tree
Hide file tree
Showing 47 changed files with 549 additions and 749 deletions.
57 changes: 35 additions & 22 deletions benches/check.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#[macro_use]
extern crate criterion;
extern crate env_logger;

extern crate gluon;
extern crate gluon_base as base;
extern crate gluon_check as check;
extern crate gluon_parser as parser;

use std::fs;

use criterion::{Bencher, Criterion};

use gluon::{compiler_pipeline::*, new_vm, Compiler};
use gluon::{base, check, compiler_pipeline::*, new_vm, parser, ThreadExt};

fn typecheck_prelude(b: &mut Bencher) {
let vm = new_vm();
let compiler = Compiler::new();
let text = fs::read_to_string("std/prelude.glu").unwrap();
let MacroValue { expr } = text
.expand_macro(&mut compiler.module_compiler(), &vm, "std.prelude", &text)
.expand_macro(
&mut vm.module_compiler(&vm.get_database()),
&vm,
"std.prelude",
&text,
)
.unwrap_or_else(|(_, err)| panic!("{}", err));
b.iter(|| {
let result = MacroValue { expr: expr.clone() }.typecheck(
&mut compiler.module_compiler(),
&mut vm.module_compiler(&vm.get_database()),
&vm,
"std.prelude",
&text,
Expand All @@ -37,25 +35,33 @@ fn typecheck_prelude(b: &mut Bencher) {

fn clone_prelude(b: &mut Bencher) {
let vm = new_vm();
let compiler = Compiler::new();
let TypecheckValue { expr, .. } = {
let text = fs::read_to_string("std/prelude.glu").unwrap();
text.typecheck(&mut compiler.module_compiler(), &vm, "std.prelude", &text)
.unwrap_or_else(|err| panic!("{}", err))
text.typecheck(
&mut vm.module_compiler(&vm.get_database()),
&vm,
"std.prelude",
&text,
)
.unwrap_or_else(|err| panic!("{}", err))
};
b.iter(|| expr.clone())
}

fn typecheck_24(b: &mut Bencher) {
let vm = new_vm();
let compiler = Compiler::new();
let text = fs::read_to_string("examples/24.glu").unwrap();
let MacroValue { expr } = text
.expand_macro(&mut compiler.module_compiler(), &vm, "examples.24", &text)
.expand_macro(
&mut vm.module_compiler(&vm.get_database()),
&vm,
"examples.24",
&text,
)
.unwrap_or_else(|(_, err)| panic!("{}", err));
b.iter(|| {
let result = MacroValue { expr: expr.clone() }.typecheck(
&mut compiler.module_compiler(),
&mut vm.module_compiler(&vm.get_database()),
&vm,
"examples.24",
&text,
Expand All @@ -70,11 +76,15 @@ fn typecheck_24(b: &mut Bencher) {

fn typecheck_file(b: &mut Bencher, file: &str) {
let vm = new_vm();
let compiler = Compiler::new();
let text = fs::read_to_string(file).unwrap();
let module_name = base::filename_to_module(file);
let reparsed = text
.reparse_infix(&mut compiler.module_compiler(), &vm, &module_name, &text)
.reparse_infix(
&mut vm.module_compiler(&vm.get_database()),
&vm,
&module_name,
&text,
)
.unwrap_or_else(|(_, err)| panic!("{}", err));
let InfixReparsed {
metadata,
Expand All @@ -88,7 +98,12 @@ fn typecheck_file(b: &mut Bencher, file: &str) {
expr: expr.clone(),
},
|input| {
let result = input.typecheck(&mut compiler.module_compiler(), &vm, &module_name, &text);
let result = input.typecheck(
&mut vm.module_compiler(&vm.get_database()),
&vm,
&module_name,
&text,
);
if let Err(ref err) = result {
println!("{}", err);
assert!(false);
Expand Down Expand Up @@ -117,9 +132,7 @@ fn typecheck_benchmark(c: &mut Criterion) {
c.bench_function("compile_lisp", move |b| {
b.iter(|| {
let vm = new_vm();
let mut compiler = Compiler::new();
compiler
.load_file(&vm, "examples/lisp/lisp.glu")
vm.load_file("examples/lisp/lisp.glu")
.unwrap_or_else(|err| panic!("{}", err))
})
});
Expand Down
23 changes: 11 additions & 12 deletions benches/function_call.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#[macro_use]
extern crate criterion;
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};

extern crate gluon;

use criterion::{black_box, Bencher, Criterion};

use gluon::vm::api::{primitive, FunctionRef, Primitive};
use gluon::vm::thread::{Status, Thread};
use gluon::{new_vm, Compiler};
use gluon::{
new_vm,
vm::{
api::{primitive, FunctionRef, Primitive},
thread::{Status, Thread},
},
};

// Benchmarks function calls
fn factorial(b: &mut Bencher) {
Expand All @@ -19,7 +18,7 @@ fn factorial(b: &mut Bencher) {
else n * factorial (n - 1)
factorial
"#;
Compiler::new().load_script(&vm, "factorial", text).unwrap();
vm.load_script("factorial", text).unwrap();
let mut factorial: FunctionRef<fn(i32) -> i32> = vm.get_global("factorial").unwrap();
b.iter(|| {
let result = factorial.call(20).unwrap();
Expand All @@ -36,7 +35,7 @@ fn factorial_tail_call(b: &mut Bencher) {
else factorial (a * n) (n - 1)
factorial 1
"#;
Compiler::new().load_script(&vm, "factorial", text).unwrap();
vm.load_script("factorial", text).unwrap();
let mut factorial: FunctionRef<fn(i32) -> i32> = vm.get_global("factorial").unwrap();
b.iter(|| {
let result = factorial.call(20).unwrap();
Expand Down Expand Up @@ -69,7 +68,7 @@ fn gluon_rust_boundary_overhead(b: &mut Bencher) {
for (n #Int- 10) f
for
"#;
Compiler::new().load_script(&vm, "test", text).unwrap();
vm.load_script("test", text).unwrap();

let mut test: FunctionRef<fn(i32, Primitive<fn(i32)>) -> ()> = vm.get_global("test").unwrap();
b.iter(|| {
Expand Down
20 changes: 6 additions & 14 deletions benches/precompiled.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#[macro_use]
extern crate criterion;

extern crate gluon;

extern crate bincode;
extern crate futures;
extern crate serde_json;
extern crate serde_state;

use std::fs::File;
use std::io::Read;
use std::{fs::File, io::Read};

use criterion::{Bencher, Criterion};

use futures::Future;
use gluon::compiler_pipeline::compile_to;
use gluon::{new_vm, Compiler};

use gluon::{compiler_pipeline::compile_to, new_vm, ThreadExt};

fn precompiled_prelude(b: &mut Bencher) {
let thread = new_vm();
Expand All @@ -32,7 +24,7 @@ fn precompiled_prelude(b: &mut Bencher) {
let mut serializer = serde_json::Serializer::new(&mut serialized_prelude);
compile_to(
&prelude[..],
&mut Compiler::new().module_compiler(),
&mut thread.module_compiler(&thread.get_database()),
&thread,
"std.prelude",
&prelude,
Expand All @@ -47,7 +39,7 @@ fn precompiled_prelude(b: &mut Bencher) {
let mut deserializer = serde_json::Deserializer::from_slice(&serialized_prelude);
Precompiled(&mut deserializer)
.run_expr(
&mut Compiler::new().module_compiler(),
&mut thread.module_compiler(&thread.get_database()),
&*thread,
"std.prelude",
"",
Expand All @@ -70,7 +62,7 @@ fn source_prelude(b: &mut Bencher) {

prelude_source
.run_expr(
&mut Compiler::new().module_compiler(),
&mut thread.module_compiler(&thread.get_database()),
&*thread,
"std.prelude",
&prelude_source,
Expand Down
32 changes: 14 additions & 18 deletions book/src/embedding-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ Before you are able to do anything with the library, you will need to create a v

### Compiling and running gluon code

Once in possession of a [RootedThread][], you can compile and execute code using the [run_expr][] method on the [Compiler][] builder type.
Once in possession of a [RootedThread][], you can compile and execute code using the [run_expr][] method on the [ThreadExt][] extension trait.

[ThreadExt]:https://docs.rs/gluon/*/gluon/trait.ThreadExt.html

```rust,ignore
let vm = new_vm();
let (result, _) = Compiler::new()
.run_expr::<i32>(&vm, "example", "1 + 2")
let (result, _) = vm
.run_expr::<i32>("example", "1 + 2")
.ok();
assert_eq!(result, Some(3));
```

Notably, if we were to execute a script with side effects the code above will actually not run the side effects. To make gluon run side effects we need to set the [run_io][] flag on [Compiler][].
Notably, if we were to execute a script with side effects the code above will actually not run the side effects. To make gluon run side effects we need to set the [run_io][] flag on [ThreadExt][].

[run_io]:https://docs.rs/gluon/*/gluon/struct.Compiler.html#method.run_io
[run_io]:https://docs.rs/gluon/*/gluon/trait.ThreadExt.html#method.run_io

```rust,ignore
let vm = new_vm();
Expand All @@ -30,13 +32,11 @@ let io = import! std.io
io.print "123"
"#;
// Returns an action which prints `123` when evaluated
Compiler::new()
.run_expr::<IO<()>>(&vm, "example", script)
vm.run_expr::<IO<()>>("example", script)
.unwrap();
// Prints `123` to stdout
Compiler::new()
.run_io(true)
.run_expr::<IO<()>>(&vm, "example", script)
vm.run_io(true);
vm.run_expr::<IO<()>>(&vm, "example", script)
.unwrap();
```

Expand All @@ -45,8 +45,7 @@ Often, it is either inconvenient or inefficient to compile and run code directly
```rust,ignore
let vm = new_vm();
// Ensure that the prelude module is loaded before trying to access something from it
Compiler::new()
.run_expr::<OpaqueValue<&Thread, Hole>>(&vm, "example", r#" import! std.prelude "#)
vm.run_expr::<OpaqueValue<&Thread, Hole>>("example", r#" import! std.prelude "#)
.unwrap();
let mut add: FunctionRef<fn (i32, i32) -> i32> = vm.get_global("std.prelude.num_Int.(+)")
.unwrap();
Expand Down Expand Up @@ -81,8 +80,7 @@ let expr = r#"
factorial 5
"#;
let (result, _) = Compiler::new()
.run_expr::<i32>(&vm, "factorial", expr)
let (result, _) = vm.run_expr::<i32>("factorial", expr)
.unwrap();
assert_eq!(result, 120);
Expand All @@ -92,8 +90,7 @@ assert_eq!(result, 120);

```rust,ignore
let vm = new_vm();
let (result, _) = Compiler::new()
.run_expr::<String>(&vm, "example", " let string = import! \"std/string.glu\" in string.trim \" Hello world \t\" ")
let (result, _) = vm.run_expr::<String>("example", " let string = import! \"std/string.glu\" in string.trim \" Hello world \t\" ")
.unwrap();
assert_eq!(result, "Hello world");
```
Expand All @@ -102,8 +99,7 @@ assert_eq!(result, "Hello world");
[new_vm]:https://docs.rs/gluon/*/gluon/fn.new_vm.html
[RootedThread]:https://docs.rs/gluon/*/gluon/struct.RootedThread.html
[Thread]:https://docs.rs/gluon/*/gluon/struct.Thread.html
[run_expr]:https://docs.rs/gluon/*/gluon/struct.Compiler.html#method.run_expr
[Compiler struct]:https://docs.rs/gluon/*/gluon/struct.Compiler.html
[run_expr]:https://docs.rs/gluon/*/gluon/trait.ThreadExt.html#method.run_expr
[add_extern_module]:https://docs.rs/gluon/*/gluon/import/fn.add_extern_module.html
[primitives]:https://github.com/gluon-lang/gluon/blob/master/vm/src/primitives.rs
[string]:http://doc.rust-lang.org/std/primitive.str.html
Expand Down
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ mod gen_skeptic {
```rust,skeptic-template
use gluon::vm::api::{Hole, OpaqueValue};
use gluon::{VmBuilder, Compiler, Thread};
use gluon::{VmBuilder, Thread, ThreadExt};
let _ = ::env_logger::try_init();
let text = r#"{{test}}"#;
let manifest_path = ::std::env::var("CARGO_MANIFEST_DIR").unwrap();
let vm = VmBuilder::new()
.import_paths(Some(vec![".".into(), manifest_path.into()]))
.build();
match Compiler::new().run_expr::<OpaqueValue<&Thread, Hole>>(&vm, "example", text) {
match vm.run_expr::<OpaqueValue<&Thread, Hole>>("example", text) {
Ok(_value) => (),
Err(err) => {
panic!("{}", err);
Expand Down
6 changes: 3 additions & 3 deletions c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use gluon::{
thread::{RootedThread, Status, Thread, ThreadInternal},
types::{VmIndex, VmInt},
},
Compiler,
ThreadExt,
};

pub type Function = extern "C" fn(&Thread) -> Status;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub unsafe extern "C" fn glu_run_expr(
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let result = Compiler::new().run_expr::<OpaqueValue<&Thread, Hole>>(&vm, module, expr);
let result = vm.run_expr::<OpaqueValue<&Thread, Hole>>(module, expr);
match result {
Ok(_) => Error::Ok,
Err(_) => Error::Unknown,
Expand All @@ -74,7 +74,7 @@ pub unsafe extern "C" fn glu_load_script(
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let result = Compiler::new().load_script(vm, module, expr);
let result = vm.load_script(module, expr);
match result {
Ok(_) => Error::Ok,
Err(_) => Error::Unknown,
Expand Down
Loading

0 comments on commit c16132e

Please sign in to comment.