Skip to content

Commit

Permalink
Fixing GC + module loading bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Mar 29, 2024
1 parent 2faf09a commit 114465e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::Hash;
use std::ops::Deref;

use kempt::Map;
use refuse::{CollectionGuard, ContainsNoRefs};
use refuse::{CollectionGuard, ContainsNoRefs, Trace};
use regex::{Captures, Regex};

use crate::string::MuseString;
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Display for MuseRegex {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Trace)]
pub struct MuseCaptures {
matches: Vec<Value>,
by_name: Map<Symbol, usize>,
Expand Down Expand Up @@ -244,9 +244,7 @@ impl CustomType for MuseCaptures {
}
}

impl ContainsNoRefs for MuseCaptures {}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Trace)]
pub struct MuseMatch {
pub content: AnyDynamic,
pub start: usize,
Expand Down Expand Up @@ -283,5 +281,3 @@ impl CustomType for MuseMatch {
&TYPE
}
}

impl ContainsNoRefs for MuseMatch {}
11 changes: 11 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ impl Value {
}
}

#[must_use]
pub fn as_u16(&self) -> Option<u16> {
match self {
Value::Int(value) => u16::try_from(*value).ok(),
Value::UInt(value) => u16::try_from(*value).ok(),
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
Value::Float(value) => Some(*value as u16),
_ => None,
}
}

#[must_use]
pub fn as_usize(&self) -> Option<usize> {
match self {
Expand Down
16 changes: 11 additions & 5 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,11 @@ impl<'context, 'guard> VmContext<'context, 'guard> {
module_declarations = module_dynamic.declarations();
} else {
drop(module_declarations);
return decl
.call(self, arity)
.map_err(|err| ExecutionError::new(err, self));
return match decl.call(self, arity) {
Ok(result) => Ok(result),
Err(Fault::FrameChanged | Fault::Waiting) => self.resume(),
Err(other) => Err(ExecutionError::new(other, self)),
};
}
}
return Err(ExecutionError::new(Fault::UnknownSymbol, self));
Expand Down Expand Up @@ -923,6 +925,10 @@ impl<'a, 'guard> VmContext<'a, 'guard> {
self.check_timeout()
}
}

pub fn while_unlocked<R>(&mut self, func: impl FnOnce(&mut CollectionGuard<'_>) -> R) -> R {
MutexGuard::unlocked(&mut self.vm, || func(self.guard))
}
}

impl<'guard> AsRef<CollectionGuard<'guard>> for VmContext<'_, 'guard> {
Expand Down Expand Up @@ -999,7 +1005,7 @@ impl VmContext<'_, '_> {
Ordering::Equal => return Ok(StepResult::Complete),
Ordering::Greater => return Err(Fault::InvalidInstructionAddress),
};
// println!("Executing {instruction:?}");
println!("Executing {instruction:?}");
let next_instruction = StepResult::from(address.checked_add(1));
let result = match instruction {
LoadedOp::Return => return Ok(StepResult::Complete),
Expand Down Expand Up @@ -1215,6 +1221,7 @@ impl VmContext<'_, '_> {
vm.frames[vm.current_frame].module = module_index.get();
vm.frames[executing_frame].loading_module = Some(module_index);
let _init_result = self.resume_async_inner(self.current_frame)?;
self.frames[executing_frame].loading_module = None;
module_index
};

Expand Down Expand Up @@ -2239,7 +2246,6 @@ impl Trace for Module {
const MAY_CONTAIN_REFERENCES: bool = true;

fn trace(&self, tracer: &mut refuse::Tracer) {
println!("Tracing module {:?}", self as *const Self);
if let Some(parent) = self.parent {
tracer.mark(parent);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/mod.rsn
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,20 @@ sigil_root: {
foo.bar.baz()
"#,
output: Int(42),
}

mod_multi: {
src: r#"
pub mod a {
pub fn test() => 0;
};

pub mod b {
pub fn test() => 42;
};

let 0 = a.test();
b.test()
"#,
output: Int(42),
}
2 changes: 1 addition & 1 deletion tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::Deserialize;

fn main() {
let filter = std::env::args().nth(1).unwrap_or_default();
// let filter = String::from("regex_captures");
// let filter = String::from("mod_multi");
for entry in std::fs::read_dir("tests/cases").unwrap() {
let entry = entry.unwrap().path();
if entry.extension().map_or(false, |ext| ext == "rsn") {
Expand Down
31 changes: 16 additions & 15 deletions tests/hosted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,22 @@ fn run_test_cases(path: &Path, filter: &str) {
.to_string(vm)?
.try_upgrade(vm.guard())?;

let code =
Compiler::compile(&SourceCode::anonymous(&code), vm.guard()).unwrap();
let sandbox = Vm::new(vm.guard());
match sandbox.execute(&code, vm.guard_mut()) {
Ok(result) => Ok(result),
Err(err) => Err(Fault::Exception(Value::dynamic(
TestError {
err,
name,
offset,
source,
},
vm.guard(),
))),
}
vm.while_unlocked(|guard| {
let code = Compiler::compile(&SourceCode::anonymous(&code), guard).unwrap();
let sandbox = Vm::new(guard);
match sandbox.execute(&code, guard) {
Ok(result) => Ok(result),
Err(err) => Err(Fault::Exception(Value::dynamic(
TestError {
err,
name,
offset,
source,
},
guard,
))),
}
})
}),
context.guard(),
),
Expand Down

0 comments on commit 114465e

Please sign in to comment.