Skip to content

Commit 8699a14

Browse files
Feat: Optimize VM with template memoization, scope chain, version table, and error handling.
1 parent 27f560d commit 8699a14

7 files changed

Lines changed: 187 additions & 362 deletions

File tree

.github/workflows/wasm.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
- name: Unused Code
3131
run: cargo shear && cargo clippy --lib --target wasm32-unknown-unknown --features wasm
3232

33+
- name: Test
34+
run: cargo test --features wasm-tests
35+
3336
- name: Install WASM Tools
3437
run: cargo install twiggy
3538

compiler/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ panic = "abort"
4343
strip = true
4444

4545
[features]
46-
wasm = ["lol_alloc"]
46+
wasm = ["lol_alloc"]
47+
wasm-tests = []

compiler/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#![cfg_attr(target_arch = "wasm32", no_std)] // Enables no_std only for WASM builds.
2-
2+
33
extern crate alloc; // Enables heap allocation without the standard library.
4-
4+
55
/*
66
Webassembly architecture entry point.
77
*/
88

9-
#[cfg(target_arch = "wasm32")]
9+
#[cfg(any(target_arch = "wasm32", test))]
1010
pub mod wasm;
1111

1212
/*
1313
Internal modules accessed through all the package.
1414
*/
15-
15+
1616
pub mod modules {
1717
pub mod lexer;
1818
pub mod parser;
1919
pub mod vm;
20-
}
20+
}
21+

compiler/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use compiler_lib::modules::{lexer::lexer, parser::Parser, vm::VM};
1+
use compiler_lib::modules::{lexer::lexer, parser::Parser, vm::{VM, Limits}};
22
use std::{env, fs, process::exit};
33
use log::{debug, info, error};
44

5-
fn parse_args() -> (String, usize, bool) {
5+
fn parse_args() -> (String, usize, bool, bool) {
66
let args: Vec<_> = env::args().skip(1).collect();
7-
if args.is_empty() || args.contains(&"-h".into()) {
8-
println!("usage: edge [-d] [-dd] [-q] <file>"); exit(0)
7+
if args.is_empty() || args.contains(&"-h".into()) {
8+
println!("usage: edge [-d] [-dd] [-q] [--sandbox] <file>"); exit(0)
99
}
1010
let p = args.iter().find(|&a| !a.starts_with('-')).cloned().unwrap_or_else(|| {
1111
eprintln!("abort: execution failed because no input target was specified"); exit(1)
1212
});
1313
let v = args.iter().filter(|&a| a == "-d").count() + (args.iter().filter(|&a| a == "-dd").count() * 2);
14-
(p, if v > 0 { v + 2 } else { 0 }, args.contains(&"-q".into()))
14+
(p, if v > 0 { v + 2 } else { 0 }, args.contains(&"-q".into()), args.contains(&"--sandbox".into()))
1515
}
1616

17-
fn run(path: &str, v: usize, q: bool) -> Result<(), Box<dyn std::error::Error>> {
17+
fn run(path: &str, v: usize, q: bool, sandbox: bool) -> Result<(), Box<dyn std::error::Error>> {
1818
stderrlog::new().module(module_path!()).verbosity(v).quiet(q).init().ok();
1919

2020
let src = fs::read_to_string(path).map_err(|e| format!("io: cannot access '{}' because {}", path, e))?;
@@ -29,9 +29,10 @@ fn run(path: &str, v: usize, q: bool) -> Result<(), Box<dyn std::error::Error>>
2929

3030
info!("emit: snapshot created [ops={} consts={}]", chunk.instructions.len(), chunk.constants.len());
3131

32-
let mut vm = VM::new(&chunk);
32+
let limits = if sandbox { Limits::sandbox() } else { Limits::none() };
33+
let mut vm = VM::with_limits(&chunk, limits);
3334
vm.run().map_err(|e| format!("trap: cpu-stop triggered by '{}' (illegal operation in main thread)", e))?;
34-
35+
3536
vm.output.iter().for_each(|l| println!("{l}"));
3637

3738
let (sp, tot) = vm.cache_stats();
@@ -40,9 +41,9 @@ fn run(path: &str, v: usize, q: bool) -> Result<(), Box<dyn std::error::Error>>
4041
}
4142

4243
fn main() {
43-
let (p, v, q) = parse_args();
44-
if let Err(e) = run(&p, v, q) {
45-
error!("process terminated: {}", e);
46-
exit(1)
44+
let (p, v, q, sandbox) = parse_args();
45+
if let Err(e) = run(&p, v, q, sandbox) {
46+
error!("process terminated: {}", e);
47+
exit(1)
4748
}
4849
}

0 commit comments

Comments
 (0)