Skip to content

Commit

Permalink
Modules should grab the unmodified value when a prefix is present (#130)
Browse files Browse the repository at this point in the history
* modules should grab the unmodified value when a prefix is present
  • Loading branch information
mattwparas authored Dec 31, 2023
1 parent 39b2d61 commit 03fe335
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
11 changes: 10 additions & 1 deletion crates/steel-core/src/compiler/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ impl ModuleManager {

// TODO: If there is a prefix applied, use it here?
if let Some(prefix) = &require_object.prefix {
// println!("Found prefix: {}", prefix);

if let Some(existing) = owned_provide.atom_identifier_mut() {
let mut prefixed_identifier = prefix.clone();
prefixed_identifier.push_str(existing.resolve());
Expand All @@ -512,6 +514,8 @@ impl ModuleManager {
SyntaxObject::default(TokenType::Define),
)));

// println!("{}", define);

require_defines.push(define);
}
_ => {
Expand Down Expand Up @@ -1120,6 +1124,7 @@ impl CompiledModule {

// Mangle with a prefix if necessary
let mut provide = provide.clone();
let mut raw_provide = provide.clone();

// If we have the alias listed, we should use it
if !explicit_requires.is_empty() {
Expand Down Expand Up @@ -1155,13 +1160,17 @@ impl CompiledModule {
ExprKind::ident("%proto-hash-get%"),
ExprKind::atom("__module-".to_string() + &other_module_prefix),
ExprKind::Quote(Box::new(Quote::new(
provide.clone(),
raw_provide.clone(),
SyntaxObject::default(TokenType::Quote)
)))
],
SyntaxObject::default(TokenType::Define),
)));

// if require_object.prefix.is_some() {
// println!("{}", define);
// }

provide_definitions.push(define);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion crates/steel-core/src/primitives/hashmaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn hash_ref(map: &Gc<HashMap<SteelVal, SteelVal>>, key: &SteelVal) -> Result
if key.is_hashable() {
match map.get(key) {
Some(value) => Ok(value.clone()),
None => stop!(Generic => "key not found in hash map: {}", key),
None => stop!(Generic => "key not found in hash map: {} - map: {:?}", key, map),
}
} else {
stop!(TypeMismatch => "key not hashable: {}", key)
Expand Down
16 changes: 12 additions & 4 deletions crates/steel-core/src/steel_vm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,11 +1029,11 @@ impl Engine {
self.with_mut_reference(obj).consume(|engine, args| {
let mut args = args.into_iter();

engine.register_value(bind_to, args.next().unwrap());
engine.update_value(bind_to, args.next().unwrap());

let res = engine.compile_and_run_raw_program(script);

engine.register_value(bind_to, SteelVal::Void);
engine.update_value(bind_to, SteelVal::Void);

res.map(|x| x.into_iter().next().unwrap())
})
Expand All @@ -1057,11 +1057,11 @@ impl Engine {
self.with_mut_reference(obj).consume(move |engine, args| {
let mut args = args.into_iter();

engine.register_value(bind_to, args.next().unwrap());
engine.update_value(bind_to, args.next().unwrap());

let res = engine.compile_and_run_raw_program_with_path(script, path.clone());

engine.register_value(bind_to, SteelVal::Void);
engine.update_value(bind_to, SteelVal::Void);

res.map(|x| x.into_iter().next().unwrap())
})
Expand Down Expand Up @@ -1573,6 +1573,12 @@ impl Engine {
// self
}

pub fn update_value(&mut self, name: &str, value: SteelVal) -> Option<&mut Self> {
let idx = self.compiler.get_idx(name)?;
self.virtual_machine.global_env.repl_set_idx(idx, value);
Some(self)
}

/// Registers multiple values at once
pub fn register_values(
&mut self,
Expand Down Expand Up @@ -2092,6 +2098,7 @@ mod engine_api_tests {
let mut engine = Engine::new();
let mut external_object = ReferenceStruct { value: 10 };

engine.register_value("*external*", SteelVal::Void);
engine.register_fn("external-get-value", ReferenceStruct::get_value);

{
Expand All @@ -2112,6 +2119,7 @@ mod engine_api_tests {
let mut engine = Engine::new();
let mut external_object = ReferenceStruct { value: 10 };

engine.register_value("*external*", SteelVal::Void);
engine.register_fn("external-get-value", ReferenceStruct::get_value);

let res = engine
Expand Down

0 comments on commit 03fe335

Please sign in to comment.