Remove ProgramReturn (#3382)
`ProgramReturn::Arguments` variant is never instantiated and should go away. This would make `ProgramReturn` an unnecessary wrapper around `KclValue`, so the whole type should go away. Part of https://github.com/KittyCAD/modeling-app/issues/3379
This commit is contained in:
@ -21,7 +21,6 @@ import init, {
|
||||
import { KCLError } from './errors'
|
||||
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
||||
import { EngineCommandManager } from './std/engineConnection'
|
||||
import { ProgramReturn } from '../wasm-lib/kcl/bindings/ProgramReturn'
|
||||
import { Discovered } from '../wasm-lib/kcl/bindings/Discovered'
|
||||
import { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
|
||||
import type { Program } from '../wasm-lib/kcl/bindings/Program'
|
||||
@ -159,7 +158,7 @@ function emptyEnvironment(): Environment {
|
||||
interface RawProgramMemory {
|
||||
environments: Environment[]
|
||||
currentEnv: EnvironmentRef
|
||||
return: ProgramReturn | null
|
||||
return: KclValue | null
|
||||
}
|
||||
|
||||
/**
|
||||
@ -170,7 +169,7 @@ interface RawProgramMemory {
|
||||
export class ProgramMemory {
|
||||
private environments: Environment[]
|
||||
private currentEnv: EnvironmentRef
|
||||
private return: ProgramReturn | null
|
||||
private return: KclValue | null
|
||||
|
||||
/**
|
||||
* Empty memory doesn't include prelude definitions.
|
||||
@ -186,7 +185,7 @@ export class ProgramMemory {
|
||||
constructor(
|
||||
environments: Environment[] = [emptyEnvironment()],
|
||||
currentEnv: EnvironmentRef = ROOT_ENVIRONMENT_REF,
|
||||
returnVal: ProgramReturn | null = null
|
||||
returnVal: KclValue | null = null
|
||||
) {
|
||||
this.environments = environments
|
||||
this.currentEnv = currentEnv
|
||||
|
@ -1479,8 +1479,6 @@ impl CallExpression {
|
||||
source_ranges: vec![self.into()],
|
||||
})
|
||||
})?;
|
||||
|
||||
let result = result.get_value()?;
|
||||
Ok(result)
|
||||
}
|
||||
FunctionKind::UserDefined => {
|
||||
@ -1505,7 +1503,6 @@ impl CallExpression {
|
||||
source_ranges,
|
||||
})
|
||||
})?;
|
||||
let result = result.get_value()?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ pub struct ProgramMemory {
|
||||
pub environments: Vec<Environment>,
|
||||
pub current_env: EnvironmentRef,
|
||||
#[serde(rename = "return")]
|
||||
pub return_: Option<ProgramReturn>,
|
||||
pub return_: Option<KclValue>,
|
||||
}
|
||||
|
||||
impl ProgramMemory {
|
||||
@ -255,35 +255,6 @@ impl DynamicState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum ProgramReturn {
|
||||
Arguments,
|
||||
Value(KclValue),
|
||||
}
|
||||
|
||||
impl From<ProgramReturn> for Vec<SourceRange> {
|
||||
fn from(item: ProgramReturn) -> Self {
|
||||
match item {
|
||||
ProgramReturn::Arguments => Default::default(),
|
||||
ProgramReturn::Value(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ProgramReturn {
|
||||
pub fn get_value(&self) -> Result<KclValue, KclError> {
|
||||
match self {
|
||||
ProgramReturn::Value(v) => Ok(v.clone()),
|
||||
ProgramReturn::Arguments => Err(KclError::Semantic(KclErrorDetails {
|
||||
message: "Cannot get value from arguments".to_owned(),
|
||||
source_ranges: self.clone().into(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A memory item.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
@ -685,7 +656,7 @@ pub type MemoryFunction =
|
||||
metadata: Vec<Metadata>,
|
||||
dynamic_state: DynamicState,
|
||||
ctx: ExecutorContext,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Option<ProgramReturn>, KclError>> + Send>>;
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Option<KclValue>, KclError>> + Send>>;
|
||||
|
||||
fn force_memory_function<
|
||||
F: Fn(
|
||||
@ -695,7 +666,7 @@ fn force_memory_function<
|
||||
Vec<Metadata>,
|
||||
DynamicState,
|
||||
ExecutorContext,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Option<ProgramReturn>, KclError>> + Send>>,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Option<KclValue>, KclError>> + Send>>,
|
||||
>(
|
||||
f: F,
|
||||
) -> F {
|
||||
@ -864,7 +835,7 @@ impl KclValue {
|
||||
args: Vec<KclValue>,
|
||||
dynamic_state: &DynamicState,
|
||||
ctx: ExecutorContext,
|
||||
) -> Result<Option<ProgramReturn>, KclError> {
|
||||
) -> Result<Option<KclValue>, KclError> {
|
||||
let KclValue::Function {
|
||||
func,
|
||||
expression,
|
||||
@ -1773,46 +1744,46 @@ impl ExecutorContext {
|
||||
BodyItem::ReturnStatement(return_statement) => match &return_statement.argument {
|
||||
Expr::BinaryExpression(bin_expr) => {
|
||||
let result = bin_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::UnaryExpression(unary_expr) => {
|
||||
let result = unary_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::Identifier(identifier) => {
|
||||
let value = memory.get(&identifier.name, identifier.into())?.clone();
|
||||
memory.return_ = Some(ProgramReturn::Value(value));
|
||||
memory.return_ = Some(value);
|
||||
}
|
||||
Expr::Literal(literal) => {
|
||||
memory.return_ = Some(ProgramReturn::Value(literal.into()));
|
||||
memory.return_ = Some(literal.into());
|
||||
}
|
||||
Expr::TagDeclarator(tag) => {
|
||||
memory.return_ = Some(ProgramReturn::Value(tag.into()));
|
||||
memory.return_ = Some(tag.into());
|
||||
}
|
||||
Expr::ArrayExpression(array_expr) => {
|
||||
let result = array_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::ObjectExpression(obj_expr) => {
|
||||
let result = obj_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::CallExpression(call_expr) => {
|
||||
let result = call_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::MemberExpression(member_expr) => {
|
||||
let result = member_expr.get_result(memory)?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::PipeExpression(pipe_expr) => {
|
||||
let result = pipe_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
||||
memory.return_ = Some(ProgramReturn::Value(result));
|
||||
memory.return_ = Some(result);
|
||||
}
|
||||
Expr::PipeSubstitution(_) => {}
|
||||
Expr::FunctionExpression(_) => {}
|
||||
Expr::None(none) => {
|
||||
memory.return_ = Some(ProgramReturn::Value(KclValue::from(none)));
|
||||
memory.return_ = Some(KclValue::from(none));
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use schemars::JsonSchema;
|
||||
use crate::{
|
||||
ast::types::FunctionExpression,
|
||||
errors::KclError,
|
||||
executor::{DynamicState, ExecutorContext, KclValue, MemoryFunction, Metadata, ProgramMemory, ProgramReturn},
|
||||
executor::{DynamicState, ExecutorContext, KclValue, MemoryFunction, Metadata, ProgramMemory},
|
||||
};
|
||||
|
||||
/// A function being used as a parameter into a stdlib function.
|
||||
@ -17,7 +17,7 @@ pub struct FunctionParam<'a> {
|
||||
}
|
||||
|
||||
impl<'a> FunctionParam<'a> {
|
||||
pub async fn call(&self, args: Vec<KclValue>) -> Result<Option<ProgramReturn>, KclError> {
|
||||
pub async fn call(&self, args: Vec<KclValue>) -> Result<Option<KclValue>, KclError> {
|
||||
(self.inner)(
|
||||
args,
|
||||
self.memory.clone(),
|
||||
|
@ -9,8 +9,8 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::{
|
||||
errors::{KclError, KclErrorDetails},
|
||||
executor::{
|
||||
ExtrudeGroup, ExtrudeGroupSet, Geometries, Geometry, KclValue, Point3d, ProgramReturn, SketchGroup,
|
||||
SketchGroupSet, SourceRange, UserVal,
|
||||
ExtrudeGroup, ExtrudeGroupSet, Geometries, Geometry, KclValue, Point3d, SketchGroup, SketchGroupSet,
|
||||
SourceRange, UserVal,
|
||||
},
|
||||
function_param::FunctionParam,
|
||||
std::{types::Uint, Args},
|
||||
@ -218,12 +218,6 @@ async fn make_transform<'a>(
|
||||
source_ranges: source_ranges.clone(),
|
||||
})
|
||||
})?;
|
||||
let ProgramReturn::Value(transform_fn_return) = transform_fn_return else {
|
||||
return Err(KclError::Semantic(KclErrorDetails {
|
||||
message: "Transform function must return a value".to_string(),
|
||||
source_ranges: source_ranges.clone(),
|
||||
}));
|
||||
};
|
||||
let KclValue::UserVal(transform) = transform_fn_return else {
|
||||
return Err(KclError::Semantic(KclErrorDetails {
|
||||
message: "Transform function must return a transform object".to_string(),
|
||||
|
Reference in New Issue
Block a user