Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338. Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical. Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too. My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
This commit is contained in:
@ -38,6 +38,7 @@ import { err } from 'lib/trap'
|
||||
import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
|
||||
import { DeepPartial } from 'lib/types'
|
||||
import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration'
|
||||
import { SketchGroup } from '../wasm-lib/kcl/bindings/SketchGroup'
|
||||
|
||||
export type { Program } from '../wasm-lib/kcl/bindings/Program'
|
||||
export type { Expr } from '../wasm-lib/kcl/bindings/Expr'
|
||||
@ -126,6 +127,7 @@ export const parse = (code: string | Error): Program | Error => {
|
||||
const program: Program = parse_wasm(code)
|
||||
return program
|
||||
} catch (e: any) {
|
||||
// throw e
|
||||
const parsed: RustKclError = JSON.parse(e.toString())
|
||||
return new KCLError(
|
||||
parsed.kind,
|
||||
@ -312,7 +314,7 @@ export class ProgramMemory {
|
||||
*/
|
||||
hasSketchOrExtrudeGroup(): boolean {
|
||||
for (const node of this.visibleEntries().values()) {
|
||||
if (node.type === 'ExtrudeGroup' || node.type === 'SketchGroup') {
|
||||
if (node.type === 'ExtrudeGroup' || node.value?.type === 'SketchGroup') {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -332,6 +334,25 @@ export class ProgramMemory {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: In the future, make the parameter be a KclValue.
|
||||
export function sketchGroupFromKclValue(
|
||||
obj: any,
|
||||
varName: string | null
|
||||
): SketchGroup | Error {
|
||||
if (obj?.value?.type === 'SketchGroup') return obj.value
|
||||
if (!varName) {
|
||||
varName = 'a KCL value'
|
||||
}
|
||||
const actualType = obj?.value?.type ?? obj?.type
|
||||
if (actualType) {
|
||||
return new Error(
|
||||
`Expected ${varName} to be a sketchGroup, but it was ${actualType} instead.`
|
||||
)
|
||||
} else {
|
||||
return new Error(`Expected ${varName} to be a sketchGroup, but it wasn't.`)
|
||||
}
|
||||
}
|
||||
|
||||
export const executor = async (
|
||||
node: Program,
|
||||
programMemory: ProgramMemory | Error = ProgramMemory.empty(),
|
||||
|
Reference in New Issue
Block a user