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

fix: lambda this closure value and config #1677

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion kclvm/evaluator/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'ctx> Evaluator<'ctx> {
) {
// Capture function schema this reference.
if let Some(this) = &lambda_ctx.this {
self.push_schema(this.clone());
self.push_schema(this.eval_ctx());
}
// Inner scope function calling.
// Note the minimum lambda.ctx.level is 2 for the top level lambda definitions.
Expand Down
27 changes: 25 additions & 2 deletions kclvm/evaluator/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,37 @@ pub type FunctionEvalContextRef = Arc<FunctionEvalContext>;
pub struct FunctionEvalContext {
/// AST node.
pub node: ast::LambdaExpr,
/// Captured schema or rule value.
pub this: Option<EvalContext>,
/// Captured schema or rule eval context.
pub this: Option<FunctionEvalThis>,
/// Captured closure local variables.
pub closure: ClosureMap,
/// The scope level of the function definition.
pub level: usize,
}

#[derive(Clone)]
pub struct FunctionEvalThis {
pub ctx: EvalContext,
pub value: ValueRef,
pub config: ValueRef,
}

impl FunctionEvalThis {
#[inline]
pub fn eval_ctx(&self) -> EvalContext {
match &self.ctx {
EvalContext::Schema(schema_ctx) => EvalContext::Schema(
schema_ctx
.borrow()
.new_with_value(&self.value, &self.config),
),
EvalContext::Rule(rule_ctx) => {
EvalContext::Rule(rule_ctx.borrow().new_with_value(&self.value, &self.config))
}
}
}
}

/// Proxy functions represent the saved functions of the runtime itself,
/// rather than executing KCL defined functions or plugin functions.
#[derive(Clone)]
Expand Down
18 changes: 18 additions & 0 deletions kclvm/evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ pub enum EvalContext {
Rule(RuleEvalContextRef),
}

impl EvalContext {
#[inline]
pub fn value(&self) -> ValueRef {
match self {
EvalContext::Schema(schema) => schema.borrow().value.clone(),
EvalContext::Rule(rule) => rule.borrow().value.clone(),
}
}

#[inline]
pub fn config(&self) -> ValueRef {
match self {
EvalContext::Schema(schema) => schema.borrow().config.clone(),
EvalContext::Rule(rule) => rule.borrow().config.clone(),
}
}
}

impl<'ctx> Evaluator<'ctx> {
/// New aa Evaluator using the AST program
#[inline]
Expand Down
12 changes: 10 additions & 2 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use kclvm_sema::{builtin, pkgpath_without_prefix, plugin};
use scopeguard::defer;

use crate::error::INTERNAL_ERROR_MSG;
use crate::func::{func_body, FunctionCaller, FunctionEvalContext};
use crate::func::{func_body, FunctionCaller, FunctionEvalContext, FunctionEvalThis};
use crate::lazy::Setter;
use crate::proxy::Proxy;
use crate::rule::{rule_body, rule_check, RuleCaller, RuleEvalContext};
Expand Down Expand Up @@ -949,7 +949,15 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
let proxy = FunctionCaller::new(
FunctionEvalContext {
node: lambda_expr.clone(),
this: self.schema_stack.borrow().last().cloned(),
this: self
.schema_stack
.borrow()
.last()
.map(|ctx| FunctionEvalThis {
ctx: ctx.clone(),
value: ctx.value(),
config: ctx.config(),
}),
closure: self.get_current_closure_map(),
level: self.scope_level() + 1,
},
Expand Down
17 changes: 13 additions & 4 deletions kclvm/evaluator/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use kclvm_runtime::ValueRef;
use scopeguard::defer;

use crate::error as kcl_error;
use crate::lazy::LazyEvalScope;

use crate::proxy::{call_rule_check, call_schema_body_from_rule};
use crate::Evaluator;
Expand All @@ -23,7 +22,6 @@ pub type RuleEvalContextRef = Rc<RefCell<RuleEvalContext>>;
#[derive(Clone, Debug)]
pub struct RuleEvalContext {
pub node: Rc<ast::RuleStmt>,
pub scope: LazyEvalScope,
pub value: ValueRef,
pub config: ValueRef,
pub config_meta: ValueRef,
Expand All @@ -36,7 +34,6 @@ impl RuleEvalContext {
pub fn new_with_node(node: ast::RuleStmt) -> Self {
RuleEvalContext {
node: Rc::new(node),
scope: LazyEvalScope::default(),
value: ValueRef::dict(None),
config: ValueRef::dict(None),
config_meta: ValueRef::dict(None),
Expand All @@ -45,6 +42,19 @@ impl RuleEvalContext {
}
}

/// New a rule evaluation context with schema value and config.
#[inline]
pub fn new_with_value(&self, value: &ValueRef, config: &ValueRef) -> RuleEvalContextRef {
Rc::new(RefCell::new(Self {
node: self.node.clone(),
value: value.clone(),
config: config.clone(),
config_meta: ValueRef::dict(None),
optional_mapping: ValueRef::dict(None),
is_sub_schema: true,
}))
}

/// Reset rule evaluation context state.
pub fn reset(&mut self) {
self.value = ValueRef::dict(None);
Expand All @@ -59,7 +69,6 @@ impl RuleEvalContext {
pub fn snapshot(&self, config: ValueRef, config_meta: ValueRef) -> RuleEvalContextRef {
Rc::new(RefCell::new(Self {
node: self.node.clone(),
scope: LazyEvalScope::default(),
value: ValueRef::dict(None),
config,
config_meta,
Expand Down
17 changes: 17 additions & 0 deletions kclvm/evaluator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ impl SchemaEvalContext {
}))
}

/// New a schema evaluation context with schema value and config.
#[inline]
pub fn new_with_value(&self, value: &ValueRef, config: &ValueRef) -> SchemaEvalContextRef {
Rc::new(RefCell::new(Self {
node: self.node.clone(),
index: self.index,
parent: self.parent,
mixins: self.mixins.clone(),
scope: None,
value: value.clone(),
config: config.clone(),
config_meta: ValueRef::dict(None),
optional_mapping: ValueRef::dict(None),
is_sub_schema: true,
}))
}

/// Pass value references from other schema eval context.
/// Note that do not change the schema node.
pub fn set_info_with_schema(&mut self, other: &SchemaEvalContext) {
Expand Down
20 changes: 20 additions & 0 deletions test/grammar/lambda/in_schema_11/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
protocol StringProtocol:
s: str

mixin StringMixin for StringProtocol:
add = lambda pref: str {
pref + s
}

schema String:
mixin [StringMixin]
s: str
add: (str) -> str

s1 = String { s: "hello" }
s2 = String { s: "world" }

output = {
s1_add: s1.add("foo ")
s2_add: s2.add("bar ")
}
7 changes: 7 additions & 0 deletions test/grammar/lambda/in_schema_11/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
s1:
s: hello
s2:
s: world
output:
s1_add: foo hello
s2_add: bar world
Loading