Skip to content

Commit

Permalink
Propagate runner context (with THIS) to condition checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dove6 committed Aug 26, 2024
1 parent 266ea45 commit 22cb361
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pixlib_parser/src/runner/classes/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl BehaviorState {
CnvContent::ComplexCondition(c) => c,
_ => return Err(RunnerError::ExpectedConditionObject.into()),
};
if !condition.check()? {
if !condition.check(Some(context.clone()))? {
return Ok(CnvValue::Null);
}
}
Expand Down
17 changes: 11 additions & 6 deletions pixlib_parser/src/runner/classes/complexcondition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ impl ComplexCondition {
}

impl GeneralCondition for ComplexCondition {
fn check(&self) -> anyhow::Result<bool> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
fn check(&self, context: Option<RunnerContext>) -> anyhow::Result<bool> {
let context = context
.map(|c| c.with_current_object(self.parent.clone()))
.unwrap_or(RunnerContext::new_minimal(
&self.parent.parent.runner,
&self.parent,
));
self.state.borrow().check(context)
}
}
Expand Down Expand Up @@ -191,17 +196,17 @@ impl ComplexConditionState {
};
let result = match complex_condition.operator {
ComplexConditionOperator::And => {
if !left.check()? {
if !left.check(Some(context.clone()))? {
Ok(false)
} else {
Ok(right.check()?)
Ok(right.check(Some(context.clone()))?)
}
}
ComplexConditionOperator::Or => {
if left.check()? {
if left.check(Some(context.clone()))? {
Ok(true)
} else {
Ok(right.check()?)
Ok(right.check(Some(context.clone()))?)
}
}
};
Expand Down
9 changes: 7 additions & 2 deletions pixlib_parser/src/runner/classes/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ impl Condition {
}

impl GeneralCondition for Condition {
fn check(&self) -> anyhow::Result<bool> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
fn check(&self, context: Option<RunnerContext>) -> anyhow::Result<bool> {
let context = context
.map(|c| c.with_current_object(self.parent.clone()))
.unwrap_or(RunnerContext::new_minimal(
&self.parent.parent.runner,
&self.parent,
));
self.state.borrow().check(context)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pixlib_parser/src/runner/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl CnvTypeFactory {
}

pub trait GeneralCondition {
fn check(&self) -> anyhow::Result<bool>;
fn check(&self, context: Option<RunnerContext>) -> anyhow::Result<bool>;
}

pub trait GeneralGraphics {
Expand Down

0 comments on commit 22cb361

Please sign in to comment.