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

Foreign garbage collection #2033

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

13 changes: 6 additions & 7 deletions native/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const MAXIMUM_ARGUMENT_COUNT: usize = 16;
/// A dynamic primitive set equipped with native functions in Rust.
pub struct DynamicPrimitiveSet<'a, const N: usize> {
functions: &'a mut [AnyFn<'a>],
objects: [Option<RefCell<Box<dyn Any>>>; N],
values: [Option<RefCell<Box<dyn Any>>>; N],
}

impl<'a, const N: usize> DynamicPrimitiveSet<'a, N> {
/// Creates a primitive set.
pub fn new(functions: &'a mut [AnyFn<'a>]) -> Self {
Self {
functions,
// TODO Garbage-collect foreign objects.
objects: [const { None }; N],
// TODO Garbage-collect foreign values.
values: [const { None }; N],
}
}
}
Expand All @@ -42,8 +42,7 @@ impl<const N: usize> PrimitiveSet for DynamicPrimitiveSet<'_, N> {

for _ in 0..function.arity() {
let value = memory.pop();
// TODO Convert Scheme values into Rust values automatically?
let value = self.objects
let value = self.values
[memory.car(value.assume_cons()).assume_number().to_i64() as usize]
.as_ref()
.ok_or(DynamicError::ObjectIndex)?;
Expand All @@ -55,14 +54,14 @@ impl<const N: usize> PrimitiveSet for DynamicPrimitiveSet<'_, N> {

(
value,
self.objects
self.values
.iter()
.position(Option::is_none)
.ok_or(Error::OutOfMemory)?,
)
};

self.objects[index] = Some(RefCell::new(value));
self.values[index] = Some(RefCell::new(value));

let cons = memory.cons(
Number::from_i64(index as _).into(),
Expand Down
Loading