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 } from './errors'
|
||||||
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
||||||
import { EngineCommandManager } from './std/engineConnection'
|
import { EngineCommandManager } from './std/engineConnection'
|
||||||
import { ProgramReturn } from '../wasm-lib/kcl/bindings/ProgramReturn'
|
|
||||||
import { Discovered } from '../wasm-lib/kcl/bindings/Discovered'
|
import { Discovered } from '../wasm-lib/kcl/bindings/Discovered'
|
||||||
import { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
|
import { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
|
||||||
import type { Program } from '../wasm-lib/kcl/bindings/Program'
|
import type { Program } from '../wasm-lib/kcl/bindings/Program'
|
||||||
@ -159,7 +158,7 @@ function emptyEnvironment(): Environment {
|
|||||||
interface RawProgramMemory {
|
interface RawProgramMemory {
|
||||||
environments: Environment[]
|
environments: Environment[]
|
||||||
currentEnv: EnvironmentRef
|
currentEnv: EnvironmentRef
|
||||||
return: ProgramReturn | null
|
return: KclValue | null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -170,7 +169,7 @@ interface RawProgramMemory {
|
|||||||
export class ProgramMemory {
|
export class ProgramMemory {
|
||||||
private environments: Environment[]
|
private environments: Environment[]
|
||||||
private currentEnv: EnvironmentRef
|
private currentEnv: EnvironmentRef
|
||||||
private return: ProgramReturn | null
|
private return: KclValue | null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empty memory doesn't include prelude definitions.
|
* Empty memory doesn't include prelude definitions.
|
||||||
@ -186,7 +185,7 @@ export class ProgramMemory {
|
|||||||
constructor(
|
constructor(
|
||||||
environments: Environment[] = [emptyEnvironment()],
|
environments: Environment[] = [emptyEnvironment()],
|
||||||
currentEnv: EnvironmentRef = ROOT_ENVIRONMENT_REF,
|
currentEnv: EnvironmentRef = ROOT_ENVIRONMENT_REF,
|
||||||
returnVal: ProgramReturn | null = null
|
returnVal: KclValue | null = null
|
||||||
) {
|
) {
|
||||||
this.environments = environments
|
this.environments = environments
|
||||||
this.currentEnv = currentEnv
|
this.currentEnv = currentEnv
|
||||||
|
@ -1479,8 +1479,6 @@ impl CallExpression {
|
|||||||
source_ranges: vec![self.into()],
|
source_ranges: vec![self.into()],
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let result = result.get_value()?;
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
FunctionKind::UserDefined => {
|
FunctionKind::UserDefined => {
|
||||||
@ -1505,7 +1503,6 @@ impl CallExpression {
|
|||||||
source_ranges,
|
source_ranges,
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
let result = result.get_value()?;
|
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ pub struct ProgramMemory {
|
|||||||
pub environments: Vec<Environment>,
|
pub environments: Vec<Environment>,
|
||||||
pub current_env: EnvironmentRef,
|
pub current_env: EnvironmentRef,
|
||||||
#[serde(rename = "return")]
|
#[serde(rename = "return")]
|
||||||
pub return_: Option<ProgramReturn>,
|
pub return_: Option<KclValue>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProgramMemory {
|
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.
|
/// A memory item.
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
@ -685,7 +656,7 @@ pub type MemoryFunction =
|
|||||||
metadata: Vec<Metadata>,
|
metadata: Vec<Metadata>,
|
||||||
dynamic_state: DynamicState,
|
dynamic_state: DynamicState,
|
||||||
ctx: ExecutorContext,
|
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<
|
fn force_memory_function<
|
||||||
F: Fn(
|
F: Fn(
|
||||||
@ -695,7 +666,7 @@ fn force_memory_function<
|
|||||||
Vec<Metadata>,
|
Vec<Metadata>,
|
||||||
DynamicState,
|
DynamicState,
|
||||||
ExecutorContext,
|
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: F,
|
||||||
) -> F {
|
) -> F {
|
||||||
@ -864,7 +835,7 @@ impl KclValue {
|
|||||||
args: Vec<KclValue>,
|
args: Vec<KclValue>,
|
||||||
dynamic_state: &DynamicState,
|
dynamic_state: &DynamicState,
|
||||||
ctx: ExecutorContext,
|
ctx: ExecutorContext,
|
||||||
) -> Result<Option<ProgramReturn>, KclError> {
|
) -> Result<Option<KclValue>, KclError> {
|
||||||
let KclValue::Function {
|
let KclValue::Function {
|
||||||
func,
|
func,
|
||||||
expression,
|
expression,
|
||||||
@ -1773,46 +1744,46 @@ impl ExecutorContext {
|
|||||||
BodyItem::ReturnStatement(return_statement) => match &return_statement.argument {
|
BodyItem::ReturnStatement(return_statement) => match &return_statement.argument {
|
||||||
Expr::BinaryExpression(bin_expr) => {
|
Expr::BinaryExpression(bin_expr) => {
|
||||||
let result = bin_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
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) => {
|
Expr::UnaryExpression(unary_expr) => {
|
||||||
let result = unary_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
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) => {
|
Expr::Identifier(identifier) => {
|
||||||
let value = memory.get(&identifier.name, identifier.into())?.clone();
|
let value = memory.get(&identifier.name, identifier.into())?.clone();
|
||||||
memory.return_ = Some(ProgramReturn::Value(value));
|
memory.return_ = Some(value);
|
||||||
}
|
}
|
||||||
Expr::Literal(literal) => {
|
Expr::Literal(literal) => {
|
||||||
memory.return_ = Some(ProgramReturn::Value(literal.into()));
|
memory.return_ = Some(literal.into());
|
||||||
}
|
}
|
||||||
Expr::TagDeclarator(tag) => {
|
Expr::TagDeclarator(tag) => {
|
||||||
memory.return_ = Some(ProgramReturn::Value(tag.into()));
|
memory.return_ = Some(tag.into());
|
||||||
}
|
}
|
||||||
Expr::ArrayExpression(array_expr) => {
|
Expr::ArrayExpression(array_expr) => {
|
||||||
let result = array_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
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) => {
|
Expr::ObjectExpression(obj_expr) => {
|
||||||
let result = obj_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
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) => {
|
Expr::CallExpression(call_expr) => {
|
||||||
let result = call_expr.execute(memory, dynamic_state, &pipe_info, self).await?;
|
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) => {
|
Expr::MemberExpression(member_expr) => {
|
||||||
let result = member_expr.get_result(memory)?;
|
let result = member_expr.get_result(memory)?;
|
||||||
memory.return_ = Some(ProgramReturn::Value(result));
|
memory.return_ = Some(result);
|
||||||
}
|
}
|
||||||
Expr::PipeExpression(pipe_expr) => {
|
Expr::PipeExpression(pipe_expr) => {
|
||||||
let result = pipe_expr.get_result(memory, dynamic_state, &pipe_info, self).await?;
|
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::PipeSubstitution(_) => {}
|
||||||
Expr::FunctionExpression(_) => {}
|
Expr::FunctionExpression(_) => {}
|
||||||
Expr::None(none) => {
|
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::{
|
use crate::{
|
||||||
ast::types::FunctionExpression,
|
ast::types::FunctionExpression,
|
||||||
errors::KclError,
|
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.
|
/// A function being used as a parameter into a stdlib function.
|
||||||
@ -17,7 +17,7 @@ pub struct FunctionParam<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> 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)(
|
(self.inner)(
|
||||||
args,
|
args,
|
||||||
self.memory.clone(),
|
self.memory.clone(),
|
||||||
|
@ -9,8 +9,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use crate::{
|
use crate::{
|
||||||
errors::{KclError, KclErrorDetails},
|
errors::{KclError, KclErrorDetails},
|
||||||
executor::{
|
executor::{
|
||||||
ExtrudeGroup, ExtrudeGroupSet, Geometries, Geometry, KclValue, Point3d, ProgramReturn, SketchGroup,
|
ExtrudeGroup, ExtrudeGroupSet, Geometries, Geometry, KclValue, Point3d, SketchGroup, SketchGroupSet,
|
||||||
SketchGroupSet, SourceRange, UserVal,
|
SourceRange, UserVal,
|
||||||
},
|
},
|
||||||
function_param::FunctionParam,
|
function_param::FunctionParam,
|
||||||
std::{types::Uint, Args},
|
std::{types::Uint, Args},
|
||||||
@ -218,12 +218,6 @@ async fn make_transform<'a>(
|
|||||||
source_ranges: source_ranges.clone(),
|
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 {
|
let KclValue::UserVal(transform) = transform_fn_return else {
|
||||||
return Err(KclError::Semantic(KclErrorDetails {
|
return Err(KclError::Semantic(KclErrorDetails {
|
||||||
message: "Transform function must return a transform object".to_string(),
|
message: "Transform function must return a transform object".to_string(),
|
||||||
|
Reference in New Issue
Block a user