Grackle: Allow objects to be params into arrays (#1322)
This commit is contained in:
@ -232,7 +232,7 @@ impl Planner {
|
|||||||
} = match KclValueGroup::from(argument) {
|
} = match KclValueGroup::from(argument) {
|
||||||
KclValueGroup::Single(value) => self.plan_to_compute_single(ctx, value)?,
|
KclValueGroup::Single(value) => self.plan_to_compute_single(ctx, value)?,
|
||||||
KclValueGroup::ArrayExpression(expr) => self.plan_to_bind_array(ctx, *expr)?,
|
KclValueGroup::ArrayExpression(expr) => self.plan_to_bind_array(ctx, *expr)?,
|
||||||
KclValueGroup::ObjectExpression(_) => todo!(),
|
KclValueGroup::ObjectExpression(expr) => self.plan_to_bind_object(ctx, *expr)?,
|
||||||
};
|
};
|
||||||
acc_instrs.extend(new_instructions);
|
acc_instrs.extend(new_instructions);
|
||||||
acc_args.push(arg);
|
acc_args.push(arg);
|
||||||
@ -430,7 +430,15 @@ impl Planner {
|
|||||||
self.plan_to_compute_single(ctx, init_value)
|
self.plan_to_compute_single(ctx, init_value)
|
||||||
}
|
}
|
||||||
KclValueGroup::ArrayExpression(expr) => self.plan_to_bind_array(ctx, *expr),
|
KclValueGroup::ArrayExpression(expr) => self.plan_to_bind_array(ctx, *expr),
|
||||||
KclValueGroup::ObjectExpression(expr) => {
|
KclValueGroup::ObjectExpression(expr) => self.plan_to_bind_object(ctx, *expr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn plan_to_bind_object(
|
||||||
|
&mut self,
|
||||||
|
ctx: &mut Context,
|
||||||
|
expr: ast::types::ObjectExpression,
|
||||||
|
) -> Result<EvalPlan, CompileError> {
|
||||||
// Convert the object to a sequence of key-value pairs.
|
// Convert the object to a sequence of key-value pairs.
|
||||||
let mut kvs = expr.properties.into_iter().map(|prop| (prop.key, prop.value));
|
let mut kvs = expr.properties.into_iter().map(|prop| (prop.key, prop.value));
|
||||||
let (instructions, each_property_binding) = kvs.try_fold(
|
let (instructions, each_property_binding) = kvs.try_fold(
|
||||||
@ -451,8 +459,7 @@ impl Planner {
|
|||||||
.elements
|
.elements
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.try_fold(Vec::with_capacity(n), |mut seq, child_element| {
|
.try_fold(Vec::with_capacity(n), |mut seq, child_element| {
|
||||||
let EvalPlan { instructions, binding } =
|
let EvalPlan { instructions, binding } = self.plan_to_bind_one(ctx, child_element)?;
|
||||||
self.plan_to_bind_one(ctx, child_element)?;
|
|
||||||
seq.push(binding);
|
seq.push(binding);
|
||||||
acc_instrs.extend(instructions);
|
acc_instrs.extend(instructions);
|
||||||
Ok(seq)
|
Ok(seq)
|
||||||
@ -470,8 +477,7 @@ impl Planner {
|
|||||||
.properties
|
.properties
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.try_fold(HashMap::with_capacity(n), |mut map, property| {
|
.try_fold(HashMap::with_capacity(n), |mut map, property| {
|
||||||
let EvalPlan { instructions, binding } =
|
let EvalPlan { instructions, binding } = self.plan_to_bind_one(ctx, property.value)?;
|
||||||
self.plan_to_bind_one(ctx, property.value)?;
|
|
||||||
map.insert(property.key.name, binding);
|
map.insert(property.key.name, binding);
|
||||||
acc_instrs.extend(instructions);
|
acc_instrs.extend(instructions);
|
||||||
Ok(map)
|
Ok(map)
|
||||||
@ -488,8 +494,6 @@ impl Planner {
|
|||||||
binding: EpBinding::Map(each_property_binding),
|
binding: EpBinding::Map(each_property_binding),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn plan_to_bind_array(
|
fn plan_to_bind_array(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|||||||
@ -744,6 +744,25 @@ fn stdlib_api_calls() {
|
|||||||
must_plan(program);
|
must_plan(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn objects_as_parameters() {
|
||||||
|
let program = "fn identity = (x) => { return x }
|
||||||
|
let obj = identity({x: 1})";
|
||||||
|
let (plan, scope) = must_plan(program);
|
||||||
|
let expected_plan = vec![
|
||||||
|
// Object contents
|
||||||
|
Instruction::SetPrimitive {
|
||||||
|
address: Address::ZERO,
|
||||||
|
value: 1i64.into(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
assert_eq!(plan, expected_plan);
|
||||||
|
assert_eq!(
|
||||||
|
scope.get("obj").unwrap(),
|
||||||
|
&EpBinding::Map(HashMap::from([("x".to_owned(), EpBinding::Single(Address::ZERO)),]))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn arrays_as_parameters() {
|
fn arrays_as_parameters() {
|
||||||
let program = "fn identity = (x) => { return x }
|
let program = "fn identity = (x) => { return x }
|
||||||
|
|||||||
Reference in New Issue
Block a user