Skip to content
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: 6 additions & 6 deletions xee-interpreter/src/context/dynamic_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub type Variables = AHashMap<xot::xmlname::OwnedName, sequence::Sequence>;

// a dynamic context is created for each xpath evaluation
#[derive(Debug)]
pub struct DynamicContext<'a> {
pub struct DynamicContext<'a, 'd> {
// we keep a reference to the program
program: &'a Program,

Expand All @@ -26,7 +26,7 @@ pub struct DynamicContext<'a> {
// we want to mutate documents during evaluation, and this happens in
// multiple spots. We use RefCell to manage that during runtime so we don't
// need to make the whole thing immutable.
documents: DocumentsRef,
documents: DocumentsRef<'d>,
variables: Variables,
// TODO: we want to be able to control the creation of this outside,
// as it needs to be the same for all evalutions of XSLT I believe
Expand All @@ -43,12 +43,12 @@ pub struct DynamicContext<'a> {
environment_variables: HashMap<String, String>,
}

impl<'a> DynamicContext<'a> {
impl<'a, 'd> DynamicContext<'a, 'd> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
program: &'a Program,
context_item: Option<sequence::Item>,
documents: DocumentsRef,
documents: DocumentsRef<'d>,
variables: Variables,
current_datetime: chrono::DateTime<chrono::offset::FixedOffset>,
default_collection: Option<sequence::Sequence>,
Expand Down Expand Up @@ -82,8 +82,8 @@ impl<'a> DynamicContext<'a> {
}

/// The documents in this context.
pub fn documents(&self) -> DocumentsRef {
self.documents.clone()
pub fn documents(&self) -> &DocumentsRef<'d> {
&self.documents
}

/// The variables in this context.
Expand Down
42 changes: 7 additions & 35 deletions xee-interpreter/src/context/dynamic_context_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, ops::Deref, rc::Rc};
use std::{cell::RefCell, ops::Deref};

use ahash::{HashMap, HashMapExt};
use iri_string::types::{IriStr, IriString};
Expand All @@ -18,7 +18,6 @@ use super::{DynamicContext, Variables};
pub struct DynamicContextBuilder<'a> {
program: &'a interpreter::Program,
context_item: Option<sequence::Item>,
documents: DocumentsRef,
variables: Variables,
current_datetime: chrono::DateTime<chrono::offset::FixedOffset>,
default_collection: Option<sequence::Sequence>,
Expand All @@ -30,42 +29,23 @@ pub struct DynamicContextBuilder<'a> {

/// A shallow wrapper around a collection of XML documents
/// [`xml::Documents`]
#[derive(Debug, Clone)]
pub struct DocumentsRef(Rc<RefCell<xml::Documents>>);
#[derive(Debug)]
pub struct DocumentsRef<'a>(pub RefCell<&'a mut xml::Documents>);

impl Deref for DocumentsRef {
type Target = RefCell<xml::Documents>;
impl<'a> Deref for DocumentsRef<'a> {
type Target = RefCell<&'a mut xml::Documents>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<xml::Documents> for DocumentsRef {
fn from(documents: xml::Documents) -> Self {
Self(Rc::new(RefCell::new(documents)))
}
}

impl DocumentsRef {
pub fn new() -> Self {
Self(Rc::new(RefCell::new(xml::Documents::new())))
}
}

impl Default for DocumentsRef {
fn default() -> Self {
Self::new()
}
}

impl<'a> DynamicContextBuilder<'a> {
/// Construct a new `DynamicContextBuilder` with the given `StaticContext`.
pub(crate) fn new(program: &'a interpreter::Program) -> Self {
Self {
program,
context_item: None,
documents: DocumentsRef::new(),
variables: Variables::new(),
current_datetime: chrono::offset::Local::now().into(),
default_collection: None,
Expand All @@ -90,14 +70,6 @@ impl<'a> DynamicContextBuilder<'a> {
self
}

/// Set the documents of the [`DynamicContext`].
///
/// You can give it either owned documents or a [`DocumentsRef`].
pub fn documents(&mut self, documents: impl Into<DocumentsRef>) -> &mut Self {
self.documents = documents.into();
self
}

/// Set the variables of the [`DynamicContext`].
///
/// Without this, the [`DynamicContext`] will have no variables.
Expand Down Expand Up @@ -170,11 +142,11 @@ impl<'a> DynamicContextBuilder<'a> {
}

/// Build the `DynamicContext`.
pub fn build(&self) -> DynamicContext<'_> {
pub fn build<'d>(&self, documents: DocumentsRef<'d>) -> DynamicContext<'a, 'd> {
DynamicContext::new(
self.program,
self.context_item.clone(),
self.documents.clone(),
documents,
self.variables.clone(),
self.current_datetime,
self.default_collection.clone(),
Expand Down
6 changes: 3 additions & 3 deletions xee-interpreter/src/function/static_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl FunctionKind {
}
}

pub(crate) type StaticFunctionType = fn(
context: &DynamicContext,
pub(crate) type StaticFunctionType = for<'a, 'd> fn(
context: &DynamicContext<'a, 'd>,
interpreter: &mut interpreter::Interpreter,
arguments: &[sequence::Sequence],
) -> error::Result<sequence::Sequence>;
Expand Down Expand Up @@ -193,7 +193,7 @@ impl StaticFunction {

pub(crate) fn invoke(
&self,
context: &DynamicContext,
context: &DynamicContext<'_, '_>,
interpreter: &mut interpreter::Interpreter,
arguments: Vec<sequence::Sequence>,
closure_values: &[stack::Value],
Expand Down
22 changes: 11 additions & 11 deletions xee-interpreter/src/interpreter/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use super::instruction::{read_i16, read_instruction, read_u16, read_u8, EncodedI
use super::runnable::Runnable;
use super::state::State;

pub struct Interpreter<'a> {
runnable: &'a Runnable<'a>,
pub struct Interpreter<'a, 'd> {
runnable: &'a Runnable<'a, 'd>,
pub(crate) state: State<'a>,
}

Expand All @@ -47,8 +47,8 @@ impl From<sequence::Item> for ContextInfo {
}
}

impl<'a> Interpreter<'a> {
pub fn new(runnable: &'a Runnable<'a>, xot: &'a mut Xot) -> Self {
impl<'a, 'd> Interpreter<'a, 'd> {
pub fn new(runnable: &'a Runnable<'a, 'd>, xot: &'a mut Xot) -> Self {
Interpreter {
runnable,
state: State::new(xot),
Expand All @@ -59,7 +59,7 @@ impl<'a> Interpreter<'a> {
self.state
}

pub(crate) fn runnable(&self) -> &Runnable<'_> {
pub(crate) fn runnable(&self) -> &'a Runnable<'a, 'd> {
self.runnable
}

Expand Down Expand Up @@ -283,7 +283,7 @@ impl<'a> Interpreter<'a> {
&b,
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(result);
Expand All @@ -299,7 +299,7 @@ impl<'a> Interpreter<'a> {
&b,
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(result);
Expand All @@ -311,7 +311,7 @@ impl<'a> Interpreter<'a> {
b,
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(combined);
Expand All @@ -323,7 +323,7 @@ impl<'a> Interpreter<'a> {
b,
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(combined);
Expand All @@ -335,7 +335,7 @@ impl<'a> Interpreter<'a> {
b,
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(combined);
Expand Down Expand Up @@ -370,7 +370,7 @@ impl<'a> Interpreter<'a> {
let value = value.deduplicate(
self.runnable
.documents()
.borrow()
.borrow_mut()
.document_order_access(self.xot()),
)?;
self.state.push(value);
Expand Down
2 changes: 1 addition & 1 deletion xee-interpreter/src/interpreter/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Program {
}

/// Obtain a runnable version of this program, with a particular dynamic context.
pub fn runnable<'a>(&'a self, dynamic_context: &'a context::DynamicContext) -> Runnable<'a> {
pub fn runnable<'a, 'd>(&'a self, dynamic_context: &'a context::DynamicContext<'a, 'd>) -> Runnable<'a, 'd> {
Runnable::new(self, dynamic_context)
}

Expand Down
12 changes: 6 additions & 6 deletions xee-interpreter/src/interpreter/runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ use super::Interpreter;
use super::Program;

#[derive(Debug)]
pub struct Runnable<'a> {
pub struct Runnable<'a, 'd> {
program: &'a Program,
// TODO: this should be private, but is needed right now
// to implement call_static without lifetime issues.
// We could possibly obtain context from the interpreter directly,
// but this leads to lifetime issues right now.
pub(crate) dynamic_context: &'a DynamicContext<'a>,
pub(crate) dynamic_context: &'a DynamicContext<'a, 'd>,
}

impl<'a> Runnable<'a> {
pub(crate) fn new(program: &'a Program, dynamic_context: &'a DynamicContext) -> Self {
impl<'a, 'd> Runnable<'a, 'd> {
pub(crate) fn new(program: &'a Program, dynamic_context: &'a DynamicContext<'a, 'd>) -> Self {
Self {
program,
dynamic_context,
Expand Down Expand Up @@ -106,11 +106,11 @@ impl<'a> Runnable<'a> {
self.program
}

pub fn dynamic_context(&self) -> &'a DynamicContext<'_> {
pub fn dynamic_context(&self) -> &'a DynamicContext<'a, 'd> {
self.dynamic_context
}

pub fn documents(&self) -> DocumentsRef {
pub fn documents(&self) -> &DocumentsRef<'d> {
self.dynamic_context.documents()
}

Expand Down
10 changes: 5 additions & 5 deletions xee-interpreter/src/library/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn id(
interpreter.xot(),
context
.documents()
.borrow()
.borrow_mut()
.document_order_access(interpreter.xot()),
)
}
Expand All @@ -47,7 +47,7 @@ fn element_with_id(
interpreter.xot(),
context
.documents()
.borrow()
.borrow_mut()
.document_order_access(interpreter.xot()),
)
}
Expand All @@ -56,7 +56,7 @@ fn ids_helper(
arg: impl Iterator<Item = Result<String, Error>>,
node: Node,
xot: &Xot,
annotations: xml::DocumentOrderAccess,
mut annotations: xml::DocumentOrderAccess,
) -> Result<Vec<Node>, Error> {
let document_node = xot.root(node);
let mut result: Vec<Node> = Vec::new();
Expand Down Expand Up @@ -89,8 +89,8 @@ fn generate_id(
) -> String {
if let Some(arg) = arg {
let documents = context.documents();
let documents = documents.borrow();
let annotations = documents.document_order_access(interpreter.xot());
let mut documents = documents.borrow_mut();
let mut annotations = documents.document_order_access(interpreter.xot());
let annotation = annotations.get(arg);
annotation.generate_id()
} else {
Expand Down
10 changes: 5 additions & 5 deletions xee-interpreter/src/pattern/pattern_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub struct PatternLookup<V: Clone> {
pub(crate) patterns: Vec<(Pattern<function::InlineFunctionId>, V)>,
}

pub(crate) struct InterpreterPredicateMatcher<'a> {
interpreter: &'a mut Interpreter<'a>,
pub(crate) struct InterpreterPredicateMatcher<'a, 'd> {
interpreter: &'a mut Interpreter<'a, 'd>,
}

impl<'a> InterpreterPredicateMatcher<'a> {
pub(crate) fn new(interpreter: &'a mut Interpreter<'a>) -> Self {
impl<'a, 'd> InterpreterPredicateMatcher<'a, 'd> {
pub(crate) fn new(interpreter: &'a mut Interpreter<'a, 'd>) -> Self {
Self { interpreter }
}
}

impl PredicateMatcher for Interpreter<'_> {
impl PredicateMatcher for Interpreter<'_, '_> {
fn match_predicate(
&mut self,
inline_function_id: function::InlineFunctionId,
Expand Down
2 changes: 1 addition & 1 deletion xee-interpreter/src/sequence/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Sequence {

pub(crate) fn process_set_result(
s: HashSet<xot::Node>,
annotations: xml::DocumentOrderAccess,
mut annotations: xml::DocumentOrderAccess,
) -> Self {
// sort nodes by document order
let mut nodes = s.into_iter().collect::<Vec<_>>();
Expand Down
4 changes: 2 additions & 2 deletions xee-interpreter/src/sequence/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ where
fn precedes<J>(
&'a self,
other: &'a impl SequenceOrder<'a, J>,
annotations: xml::DocumentOrderAccess,
mut annotations: xml::DocumentOrderAccess,
) -> error::Result<bool>
where
J: Iterator<Item = Item> + 'a,
Expand All @@ -214,7 +214,7 @@ where
fn follows<J>(
&'a self,
other: &'a impl SequenceOrder<'a, J>,
annotations: xml::DocumentOrderAccess,
mut annotations: xml::DocumentOrderAccess,
) -> error::Result<bool>
where
J: Iterator<Item = Item> + 'a,
Expand Down
2 changes: 1 addition & 1 deletion xee-interpreter/src/xml/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Documents {
&self.annotations
}

pub(crate) fn document_order_access<'a>(&'a self, xot: &'a Xot) -> DocumentOrderAccess<'a> {
pub(crate) fn document_order_access<'a>(&'a mut self, xot: &'a Xot) -> DocumentOrderAccess<'a> {
self.annotations.access(xot)
}
}
Expand Down
Loading