Skip to content

Commit

Permalink
Merge pull request #284 from koto-lang/kmap-clear
Browse files Browse the repository at this point in the history
Add the ability to clear the runtime's exports map
  • Loading branch information
irh authored Feb 2, 2024
2 parents f3f887c + 518f7bd commit ab177ad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The Koto project adheres to
it easier to implement `KotoObject`s.
- `Koto::run_instance_function` has been added.
- `Ptr`/`PtrMut` now have an associated `ref_count` function.
- `KMap::clear` has been added.

#### Core Library

Expand Down
5 changes: 5 additions & 0 deletions crates/koto/src/koto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl Koto {
self.runtime.exports()
}

/// Returns a reference to the runtime's exports
pub fn exports_mut(&mut self) -> &mut KMap {
self.runtime.exports_mut()
}

/// Compiles a Koto script, returning the complied chunk if successful
///
/// On success, the chunk is cached as the current chunk for subsequent calls to [Koto::run].
Expand Down
6 changes: 6 additions & 0 deletions crates/runtime/src/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ impl KMap {
self.data().is_empty()
}

/// Removes all contents from the data map, and removes the meta map
pub fn clear(&mut self) {
self.data_mut().clear();
self.meta = None;
}

/// Returns true if the provided KMap occupies the same memory address
pub fn is_same_instance(&self, other: &Self) -> bool {
PtrMut::ptr_eq(&self.data, &other.data)
Expand Down
5 changes: 5 additions & 0 deletions crates/runtime/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ impl KotoVm {
&self.exports
}

/// Returns a mutable reference to the active module's exports map
pub fn exports_mut(&mut self) -> &mut KMap {
&mut self.exports
}

/// The stdin wrapper used by the VM
pub fn stdin(&self) -> &Ptr<dyn KotoFile> {
&self.context.settings.stdin
Expand Down

0 comments on commit ab177ad

Please sign in to comment.