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

Attempt FALLBACK_SET/GET on failure to find field via SET/GET #440

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions crates/rune/src/runtime/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ impl Protocol {
hash: Hash::new(0x418f5becbf885806),
};

/// The function to access a field by name when Protocol::GET fails.
pub const FALLBACK_GET: Protocol = Protocol {
name: "fallback_get",
hash: Hash::new(0x6dda58b140dfeaf9),
};

/// The function to set a field by name when Protocol::SET fails.
pub const FALLBACK_SET: Protocol = Protocol {
name: "fallback_set",
hash: Hash::new(0xbe28c02896ca0b64),
};

/// The function to access a field.
pub const GET: Protocol = Protocol {
name: "get",
Expand Down
54 changes: 39 additions & 15 deletions crates/rune/src/runtime/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,12 +926,18 @@ impl Vm {
}
}
target => {
let hash = index.hash();

return Ok(match self.call_field_fn(Protocol::GET, target, hash, ())? {
CallResult::Ok(()) => CallResult::Ok(self.stack.pop()?),
CallResult::Unsupported(target) => CallResult::Unsupported(target),
});
let index = index.clone();
return Ok(
match self.call_field_fn(Protocol::GET, target, index.hash(), ())? {
CallResult::Ok(()) => return Ok(CallResult::Ok(self.stack.pop()?)),
CallResult::Unsupported(target) => {
match self.call_instance_fn(target, Protocol::FALLBACK_GET, (index,))? {
CallResult::Ok(()) => CallResult::Ok(self.stack.pop()?),
CallResult::Unsupported(target) => CallResult::Unsupported(target),
}
}
},
);
}
}

Expand Down Expand Up @@ -983,15 +989,33 @@ impl Vm {
}));
}
target => {
let hash = field.hash();

match self.call_field_fn(Protocol::SET, target, hash, (value,))? {
CallResult::Ok(()) => {
self.stack.pop()?;
CallResult::Ok(())
}
result => result,
}
let index = field.clone();
return Ok(
match self.call_field_fn(
Protocol::SET,
target,
index.hash(),
(value.clone(),),
)? {
CallResult::Ok(()) => {
self.stack.pop()?;
CallResult::Ok(())
}
CallResult::Unsupported(target) => {
match self.call_instance_fn(
target,
Protocol::FALLBACK_SET,
(index, value),
)? {
CallResult::Ok(()) => {
self.stack.pop()?;
CallResult::Ok(())
}
CallResult::Unsupported(target) => CallResult::Unsupported(target),
}
}
},
);
}
})
}
Expand Down