Change setupSketch to use artifacts output from executor

This commit is contained in:
Jonathan Tran
2024-09-18 19:39:39 -04:00
parent 8e62f07d71
commit ca2fc1bf38
17 changed files with 273 additions and 97 deletions

View File

@ -44,6 +44,8 @@ import {
VariableDeclaration, VariableDeclaration,
VariableDeclarator, VariableDeclarator,
sketchGroupFromKclValue, sketchGroupFromKclValue,
ExecState,
sketchGroupFromArtifactId,
} from 'lang/wasm' } from 'lang/wasm'
import { import {
engineCommandManager, engineCommandManager,
@ -97,6 +99,7 @@ import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
import { import {
ArtifactGraph, ArtifactGraph,
ArtifactId, ArtifactId,
getPathFromSelection,
getPlaneOrFaceFromSelection, getPlaneOrFaceFromSelection,
} from 'lang/std/artifactGraph' } from 'lang/std/artifactGraph'
import { SegmentInputs } from 'lang/std/stdTypes' import { SegmentInputs } from 'lang/std/stdTypes'
@ -397,17 +400,37 @@ export class SceneEntities {
const { truncatedAst, programMemoryOverride, variableDeclarationName } = const { truncatedAst, programMemoryOverride, variableDeclarationName } =
prepared prepared
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast: truncatedAst, ast: truncatedAst,
useFakeExecutor: true, useFakeExecutor: true,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
programMemoryOverride, programMemoryOverride,
}) })
const sketchGroup = sketchGroupFromPathToNode({ const programMemory = execState.memory
pathToNode: sketchPathToNode, let sketchGroup: SketchGroup | null | Error = null
ast: maybeModdedAst, if (selectionRanges) {
programMemory, console.warn('setupSketch looking for sketch from selection')
}) sketchGroup = sketchGroupFromSelection({
selections: selectionRanges,
execState: kclManager.execState,
// execState,
artifactGraph: this.engineCommandManager.artifactGraph,
})
if (sketchGroup) {
console.warn('setupSketch found sketch from selection')
}
}
if (!sketchGroup) {
console.warn(
'setupSketch sketch not found from selection; falling back to program memory'
)
// Fall back to the sketch group from the program memory.
sketchGroup = sketchGroupFromPathToNode({
pathToNode: sketchPathToNode,
ast: maybeModdedAst,
programMemory,
})
}
if (err(sketchGroup)) return Promise.reject(sketchGroup) if (err(sketchGroup)) return Promise.reject(sketchGroup)
if (!sketchGroup) return Promise.reject('sketchGroup not found') if (!sketchGroup) return Promise.reject('sketchGroup not found')
@ -810,12 +833,13 @@ export class SceneEntities {
updateRectangleSketch(sketchInit, x, y, tags[0]) updateRectangleSketch(sketchInit, x, y, tags[0])
} }
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast: truncatedAst, ast: truncatedAst,
useFakeExecutor: true, useFakeExecutor: true,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
programMemoryOverride, programMemoryOverride,
}) })
const programMemory = execState.memory
this.sceneProgramMemory = programMemory this.sceneProgramMemory = programMemory
const sketchGroup = sketchGroupFromKclValue( const sketchGroup = sketchGroupFromKclValue(
programMemory.get(variableDeclarationName), programMemory.get(variableDeclarationName),
@ -864,12 +888,13 @@ export class SceneEntities {
await kclManager.executeAstMock(_ast) await kclManager.executeAstMock(_ast)
sceneInfra.modelingSend({ type: 'Finish rectangle' }) sceneInfra.modelingSend({ type: 'Finish rectangle' })
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast: _ast, ast: _ast,
useFakeExecutor: true, useFakeExecutor: true,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
programMemoryOverride, programMemoryOverride,
}) })
const programMemory = execState.memory
// Prepare to update the THREEjs scene // Prepare to update the THREEjs scene
this.sceneProgramMemory = programMemory this.sceneProgramMemory = programMemory
@ -988,12 +1013,13 @@ export class SceneEntities {
modded = moddedResult.modifiedAst modded = moddedResult.modifiedAst
} }
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast: modded, ast: modded,
useFakeExecutor: true, useFakeExecutor: true,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
programMemoryOverride, programMemoryOverride,
}) })
const programMemory = execState.memory
this.sceneProgramMemory = programMemory this.sceneProgramMemory = programMemory
const sketchGroup = sketchGroupFromKclValue( const sketchGroup = sketchGroupFromKclValue(
programMemory.get(variableDeclarationName), programMemory.get(variableDeclarationName),
@ -1347,12 +1373,13 @@ export class SceneEntities {
// don't want to mod the user's code yet as they have't committed to the change yet // don't want to mod the user's code yet as they have't committed to the change yet
// plus this would be the truncated ast being recast, it would be wrong // plus this would be the truncated ast being recast, it would be wrong
codeManager.updateCodeEditor(code) codeManager.updateCodeEditor(code)
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast: truncatedAst, ast: truncatedAst,
useFakeExecutor: true, useFakeExecutor: true,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
programMemoryOverride, programMemoryOverride,
}) })
const programMemory = execState.memory
this.sceneProgramMemory = programMemory this.sceneProgramMemory = programMemory
const maybeSketchGroup = programMemory.get(variableDeclarationName) const maybeSketchGroup = programMemory.get(variableDeclarationName)
@ -1855,6 +1882,43 @@ export async function planeOrFaceFromSelection({
return null return null
} }
export function sketchGroupFromSelection({
selections,
artifactGraph,
execState,
}: {
selections: Selections
artifactGraph: ArtifactGraph
execState: ExecState
}): SketchGroup | null {
if (selections.codeBasedSelections.length !== 1) {
// Give up if there isn't exactly one selection.
console.warn(
'sketchGroupFromSelection no single selection',
selections.codeBasedSelections.length,
selections
)
return null
}
const selection = selections.codeBasedSelections[0]
const artifactId = selection.artifactId
if (!artifactId) {
console.warn(
'sketchGroupFromSelection artifact ID not found',
selections.codeBasedSelections.length,
selections
)
}
if (!artifactId) return null
const path = getPathFromSelection(artifactId, artifactGraph)
if (!path) {
console.warn('sketchGroupFromSelection path not found', artifactId)
return null
}
const sketch = sketchGroupFromArtifactId(execState, path.id)
return sketch
}
export function sketchGroupFromPathToNode({ export function sketchGroupFromPathToNode({
pathToNode, pathToNode,
ast, ast,
@ -1949,6 +2013,9 @@ export async function getSketchOrientationDetails(
// We couldn't find the plane or face, so try to look at the AST, and find it // We couldn't find the plane or face, so try to look at the AST, and find it
// through there. // through there.
console.warn(
'getSketchOrientationDetails falling back to sketchGroupFromPathToNode'
)
const sketchGroup = sketchGroupFromPathToNode({ const sketchGroup = sketchGroupFromPathToNode({
pathToNode: sketchPathToNode, pathToNode: sketchPathToNode,
ast: kclManager.ast, ast: kclManager.ast,

View File

@ -157,7 +157,7 @@ export function useCalc({
engineCommandManager, engineCommandManager,
useFakeExecutor: true, useFakeExecutor: true,
programMemoryOverride: kclManager.programMemory.clone(), programMemoryOverride: kclManager.programMemory.clone(),
}).then(({ programMemory }) => { }).then(({ execState }) => {
const resultDeclaration = ast.body.find( const resultDeclaration = ast.body.find(
(a) => (a) =>
a.type === 'VariableDeclaration' && a.type === 'VariableDeclaration' &&
@ -166,7 +166,7 @@ export function useCalc({
const init = const init =
resultDeclaration?.type === 'VariableDeclaration' && resultDeclaration?.type === 'VariableDeclaration' &&
resultDeclaration?.declarations?.[0]?.init resultDeclaration?.declarations?.[0]?.init
const result = programMemory?.get('__result__')?.value const result = execState.memory?.get('__result__')?.value
setCalcResult(typeof result === 'number' ? String(result) : 'NAN') setCalcResult(typeof result === 'number' ? String(result) : 'NAN')
init && setValueNode(init) init && setValueNode(init)
}) })

View File

@ -29,8 +29,8 @@ describe('processMemory', () => {
|> lineTo([2.15, 4.32], %) |> lineTo([2.15, 4.32], %)
// |> rx(90, %)` // |> rx(90, %)`
const ast = parse(code) const ast = parse(code)
const programMemory = await enginelessExecutor(ast, ProgramMemory.empty()) const execState = await enginelessExecutor(ast, ProgramMemory.empty())
const output = processMemory(programMemory) const output = processMemory(execState.memory)
expect(output.myVar).toEqual(5) expect(output.myVar).toEqual(5)
expect(output.otherVar).toEqual(3) expect(output.otherVar).toEqual(3)
expect(output).toEqual({ expect(output).toEqual({

View File

@ -8,6 +8,8 @@ import { EXECUTE_AST_INTERRUPT_ERROR_MESSAGE } from 'lib/constants'
import { import {
CallExpression, CallExpression,
emptyExecState,
ExecState,
initPromise, initPromise,
parse, parse,
PathToNode, PathToNode,
@ -19,6 +21,8 @@ import {
import { getNodeFromPath } from './queryAst' import { getNodeFromPath } from './queryAst'
import { codeManager, editorManager, sceneInfra } from 'lib/singletons' import { codeManager, editorManager, sceneInfra } from 'lib/singletons'
import { Diagnostic } from '@codemirror/lint' import { Diagnostic } from '@codemirror/lint'
import { ArtifactId } from 'wasm-lib/kcl/bindings/ArtifactId'
import { Artifact } from 'wasm-lib/kcl/bindings/Artifact'
interface ExecuteArgs { interface ExecuteArgs {
ast?: Program ast?: Program
@ -43,6 +47,7 @@ export class KclManager {
digest: null, digest: null,
} }
private _programMemory: ProgramMemory = ProgramMemory.empty() private _programMemory: ProgramMemory = ProgramMemory.empty()
private _execState: ExecState = emptyExecState()
private _logs: string[] = [] private _logs: string[] = []
private _lints: Diagnostic[] = [] private _lints: Diagnostic[] = []
private _kclErrors: KCLError[] = [] private _kclErrors: KCLError[] = []
@ -71,11 +76,21 @@ export class KclManager {
get programMemory() { get programMemory() {
return this._programMemory return this._programMemory
} }
set programMemory(programMemory) { // This is private because callers should be setting the entire execState.
private set programMemory(programMemory) {
this._programMemory = programMemory this._programMemory = programMemory
this._programMemoryCallBack(programMemory) this._programMemoryCallBack(programMemory)
} }
set execState(execState) {
this._execState = execState
this.programMemory = execState.memory
}
get execState() {
return this._execState
}
get logs() { get logs() {
return this._logs return this._logs
} }
@ -252,7 +267,7 @@ export class KclManager {
// Make sure we clear before starting again. End session will do this. // Make sure we clear before starting again. End session will do this.
this.engineCommandManager?.endSession() this.engineCommandManager?.endSession()
await this.ensureWasmInit() await this.ensureWasmInit()
const { logs, errors, programMemory, isInterrupted } = await executeAst({ const { logs, errors, execState, isInterrupted } = await executeAst({
ast, ast,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
}) })
@ -263,7 +278,7 @@ export class KclManager {
this.lints = await lintAst({ ast: ast }) this.lints = await lintAst({ ast: ast })
sceneInfra.modelingSend({ type: 'code edit during sketch' }) sceneInfra.modelingSend({ type: 'code edit during sketch' })
defaultSelectionFilter(programMemory, this.engineCommandManager) defaultSelectionFilter(execState.memory, this.engineCommandManager)
if (args.zoomToFit) { if (args.zoomToFit) {
let zoomObjectId: string | undefined = '' let zoomObjectId: string | undefined = ''
@ -296,7 +311,7 @@ export class KclManager {
this.logs = logs this.logs = logs
// Do not add the errors since the program was interrupted and the error is not a real KCL error // Do not add the errors since the program was interrupted and the error is not a real KCL error
this.addKclErrors(isInterrupted ? [] : errors) this.addKclErrors(isInterrupted ? [] : errors)
this.programMemory = programMemory this.execState = execState
this.ast = { ...ast } this.ast = { ...ast }
this._executeCallback() this._executeCallback()
this.engineCommandManager.addCommandLog({ this.engineCommandManager.addCommandLog({
@ -333,7 +348,7 @@ export class KclManager {
await codeManager.writeToFile() await codeManager.writeToFile()
this._ast = { ...newAst } this._ast = { ...newAst }
const { logs, errors, programMemory } = await executeAst({ const { logs, errors, execState } = await executeAst({
ast: newAst, ast: newAst,
engineCommandManager: this.engineCommandManager, engineCommandManager: this.engineCommandManager,
useFakeExecutor: true, useFakeExecutor: true,
@ -341,7 +356,8 @@ export class KclManager {
this._logs = logs this._logs = logs
this._kclErrors = errors this._kclErrors = errors
this._programMemory = programMemory this._execState = execState
this._programMemory = execState.memory
if (updates !== 'artifactRanges') return if (updates !== 'artifactRanges') return
// TODO the below seems like a work around, I wish there's a comment explaining exactly what // TODO the below seems like a work around, I wish there's a comment explaining exactly what

View File

@ -445,6 +445,6 @@ async function exe(
) { ) {
const ast = parse(code) const ast = parse(code)
const result = await enginelessExecutor(ast, programMemory) const execState = await enginelessExecutor(ast, programMemory)
return result return execState.memory
} }

View File

@ -4,6 +4,8 @@ import {
ProgramMemory, ProgramMemory,
programMemoryInit, programMemoryInit,
kclLint, kclLint,
emptyExecState,
ExecState,
} from 'lang/wasm' } from 'lang/wasm'
import { enginelessExecutor } from 'lib/testHelpers' import { enginelessExecutor } from 'lib/testHelpers'
import { EngineCommandManager } from 'lang/std/engineConnection' import { EngineCommandManager } from 'lang/std/engineConnection'
@ -56,7 +58,7 @@ export async function executeAst({
}): Promise<{ }): Promise<{
logs: string[] logs: string[]
errors: KCLError[] errors: KCLError[]
programMemory: ProgramMemory execState: ExecState
isInterrupted: boolean isInterrupted: boolean
}> { }> {
try { try {
@ -65,7 +67,7 @@ export async function executeAst({
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
engineCommandManager.startNewSession() engineCommandManager.startNewSession()
} }
const programMemory = await (useFakeExecutor const execState = await (useFakeExecutor
? enginelessExecutor(ast, programMemoryOverride || programMemoryInit()) ? enginelessExecutor(ast, programMemoryOverride || programMemoryInit())
: _executor(ast, programMemoryInit(), engineCommandManager, false)) : _executor(ast, programMemoryInit(), engineCommandManager, false))
@ -73,7 +75,7 @@ export async function executeAst({
return { return {
logs: [], logs: [],
errors: [], errors: [],
programMemory, execState,
isInterrupted: false, isInterrupted: false,
} }
} catch (e: any) { } catch (e: any) {
@ -89,7 +91,7 @@ export async function executeAst({
return { return {
errors: [e], errors: [e],
logs: [], logs: [],
programMemory: ProgramMemory.empty(), execState: emptyExecState(),
isInterrupted, isInterrupted,
} }
} else { } else {
@ -97,7 +99,7 @@ export async function executeAst({
return { return {
logs: [e], logs: [e],
errors: [], errors: [],
programMemory: ProgramMemory.empty(), execState: emptyExecState(),
isInterrupted, isInterrupted,
} }
} }

View File

@ -220,11 +220,11 @@ const yo2 = hmm([identifierGuy + 5])`
it('should move a binary expression into a new variable', async () => { it('should move a binary expression into a new variable', async () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const startIndex = code.indexOf('100 + 100') + 1 const startIndex = code.indexOf('100 + 100') + 1
const { modifiedAst } = moveValueIntoNewVariable( const { modifiedAst } = moveValueIntoNewVariable(
ast, ast,
programMemory, execState.memory,
[startIndex, startIndex], [startIndex, startIndex],
'newVar' 'newVar'
) )
@ -235,11 +235,11 @@ const yo2 = hmm([identifierGuy + 5])`
it('should move a value into a new variable', async () => { it('should move a value into a new variable', async () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const startIndex = code.indexOf('2.8') + 1 const startIndex = code.indexOf('2.8') + 1
const { modifiedAst } = moveValueIntoNewVariable( const { modifiedAst } = moveValueIntoNewVariable(
ast, ast,
programMemory, execState.memory,
[startIndex, startIndex], [startIndex, startIndex],
'newVar' 'newVar'
) )
@ -250,11 +250,11 @@ const yo2 = hmm([identifierGuy + 5])`
it('should move a callExpression into a new variable', async () => { it('should move a callExpression into a new variable', async () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const startIndex = code.indexOf('def(') const startIndex = code.indexOf('def(')
const { modifiedAst } = moveValueIntoNewVariable( const { modifiedAst } = moveValueIntoNewVariable(
ast, ast,
programMemory, execState.memory,
[startIndex, startIndex], [startIndex, startIndex],
'newVar' 'newVar'
) )
@ -265,11 +265,11 @@ const yo2 = hmm([identifierGuy + 5])`
it('should move a binary expression with call expression into a new variable', async () => { it('should move a binary expression with call expression into a new variable', async () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const startIndex = code.indexOf('jkl(') + 1 const startIndex = code.indexOf('jkl(') + 1
const { modifiedAst } = moveValueIntoNewVariable( const { modifiedAst } = moveValueIntoNewVariable(
ast, ast,
programMemory, execState.memory,
[startIndex, startIndex], [startIndex, startIndex],
'newVar' 'newVar'
) )
@ -280,11 +280,11 @@ const yo2 = hmm([identifierGuy + 5])`
it('should move a identifier into a new variable', async () => { it('should move a identifier into a new variable', async () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const startIndex = code.indexOf('identifierGuy +') + 1 const startIndex = code.indexOf('identifierGuy +') + 1
const { modifiedAst } = moveValueIntoNewVariable( const { modifiedAst } = moveValueIntoNewVariable(
ast, ast,
programMemory, execState.memory,
[startIndex, startIndex], [startIndex, startIndex],
'newVar' 'newVar'
) )
@ -465,7 +465,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
|> line([306.21, 198.87], %)` |> line([306.21, 198.87], %)`
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const lineOfInterest = 'line([306.21, 198.85], %, $a)' const lineOfInterest = 'line([306.21, 198.85], %, $a)'
const range: [number, number] = [ const range: [number, number] = [
code.indexOf(lineOfInterest), code.indexOf(lineOfInterest),
@ -475,7 +475,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
const modifiedAst = deleteSegmentFromPipeExpression( const modifiedAst = deleteSegmentFromPipeExpression(
[], [],
ast, ast,
programMemory, execState.memory,
code, code,
pathToNode pathToNode
) )
@ -543,7 +543,7 @@ ${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
const code = makeCode(line) const code = makeCode(line)
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const lineOfInterest = line const lineOfInterest = line
const range: [number, number] = [ const range: [number, number] = [
code.indexOf(lineOfInterest), code.indexOf(lineOfInterest),
@ -554,7 +554,7 @@ ${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
const modifiedAst = deleteSegmentFromPipeExpression( const modifiedAst = deleteSegmentFromPipeExpression(
dependentSegments, dependentSegments,
ast, ast,
programMemory, execState.memory,
code, code,
pathToNode pathToNode
) )
@ -632,7 +632,7 @@ describe('Testing removeSingleConstraintInfo', () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const lineOfInterest = expectedFinish.split('(')[0] + '(' const lineOfInterest = expectedFinish.split('(')[0] + '('
const range: [number, number] = [ const range: [number, number] = [
code.indexOf(lineOfInterest) + 1, code.indexOf(lineOfInterest) + 1,
@ -661,7 +661,7 @@ describe('Testing removeSingleConstraintInfo', () => {
pathToNode, pathToNode,
argPosition, argPosition,
ast, ast,
programMemory execState.memory
) )
if (!mod) return new Error('mod is undefined') if (!mod) return new Error('mod is undefined')
const recastCode = recast(mod.modifiedAst) const recastCode = recast(mod.modifiedAst)
@ -686,7 +686,7 @@ describe('Testing removeSingleConstraintInfo', () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const lineOfInterest = expectedFinish.split('(')[0] + '(' const lineOfInterest = expectedFinish.split('(')[0] + '('
const range: [number, number] = [ const range: [number, number] = [
code.indexOf(lineOfInterest) + 1, code.indexOf(lineOfInterest) + 1,
@ -711,7 +711,7 @@ describe('Testing removeSingleConstraintInfo', () => {
pathToNode, pathToNode,
argPosition, argPosition,
ast, ast,
programMemory execState.memory
) )
if (!mod) return new Error('mod is undefined') if (!mod) return new Error('mod is undefined')
const recastCode = recast(mod.modifiedAst) const recastCode = recast(mod.modifiedAst)
@ -882,7 +882,7 @@ const sketch002 = startSketchOn({
// const lineOfInterest = 'line([-2.94, 2.7], %)' // const lineOfInterest = 'line([-2.94, 2.7], %)'
const ast = parse(codeBefore) const ast = parse(codeBefore)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
// deleteFromSelection // deleteFromSelection
const range: [number, number] = [ const range: [number, number] = [
@ -895,7 +895,7 @@ const sketch002 = startSketchOn({
range, range,
type, type,
}, },
programMemory, execState.memory,
async () => { async () => {
await new Promise((resolve) => setTimeout(resolve, 100)) await new Promise((resolve) => setTimeout(resolve, 100))
return { return {

View File

@ -45,11 +45,11 @@ const variableBelowShouldNotBeIncluded = 3
const rangeStart = code.indexOf('// selection-range-7ish-before-this') - 7 const rangeStart = code.indexOf('// selection-range-7ish-before-this') - 7
const ast = parse(code) const ast = parse(code)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const { variables, bodyPath, insertIndex } = findAllPreviousVariables( const { variables, bodyPath, insertIndex } = findAllPreviousVariables(
ast, ast,
programMemory, execState.memory,
[rangeStart, rangeStart] [rangeStart, rangeStart]
) )
expect(variables).toEqual([ expect(variables).toEqual([
@ -351,11 +351,11 @@ const part001 = startSketchAt([-1.41, 3.46])
const ast = parse(exampleCode) const ast = parse(exampleCode)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const result = hasExtrudeSketchGroup({ const result = hasExtrudeSketchGroup({
ast, ast,
selection: { type: 'default', range: [100, 101] }, selection: { type: 'default', range: [100, 101] },
programMemory, programMemory: execState.memory,
}) })
expect(result).toEqual(true) expect(result).toEqual(true)
}) })
@ -370,11 +370,11 @@ const part001 = startSketchAt([-1.41, 3.46])
const ast = parse(exampleCode) const ast = parse(exampleCode)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const result = hasExtrudeSketchGroup({ const result = hasExtrudeSketchGroup({
ast, ast,
selection: { type: 'default', range: [100, 101] }, selection: { type: 'default', range: [100, 101] },
programMemory, programMemory: execState.memory,
}) })
expect(result).toEqual(true) expect(result).toEqual(true)
}) })
@ -383,11 +383,11 @@ const part001 = startSketchAt([-1.41, 3.46])
const ast = parse(exampleCode) const ast = parse(exampleCode)
if (err(ast)) throw ast if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const result = hasExtrudeSketchGroup({ const result = hasExtrudeSketchGroup({
ast, ast,
selection: { type: 'default', range: [10, 11] }, selection: { type: 'default', range: [10, 11] },
programMemory, programMemory: execState.memory,
}) })
expect(result).toEqual(false) expect(result).toEqual(false)
}) })

View File

@ -849,3 +849,26 @@ export function getPlaneOrFaceFromSelection(
} }
return null return null
} }
/**
* Get the path from a selection.
*/
export function getPathFromSelection(
id: ArtifactId,
artifactGraph: ArtifactGraph
): PathArtifact | null {
const selection = artifactGraph.get(id)
if (!selection) return null
if (selection.type === 'solid2D') {
const path = artifactGraph.get(selection.pathId)
if (path?.type !== 'path') return null
return path
} else if (selection.type === 'wall' || selection.type === 'cap') {
const sweep = artifactGraph.get(selection.sweepId)
if (sweep?.type !== 'sweep') return null
const path = artifactGraph.get(sweep.pathId)
if (path?.type !== 'path') return null
return path
}
return null
}

View File

@ -117,11 +117,11 @@ describe('testing changeSketchArguments', () => {
const ast = parse(code) const ast = parse(code)
if (err(ast)) return ast if (err(ast)) return ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const sourceStart = code.indexOf(lineToChange) const sourceStart = code.indexOf(lineToChange)
const changeSketchArgsRetVal = changeSketchArguments( const changeSketchArgsRetVal = changeSketchArguments(
ast, ast,
programMemory, execState.memory,
{ {
type: 'sourceRange', type: 'sourceRange',
sourceRange: [sourceStart, sourceStart + lineToChange.length], sourceRange: [sourceStart, sourceStart + lineToChange.length],
@ -150,12 +150,12 @@ const mySketch001 = startSketchOn('XY')
const ast = parse(code) const ast = parse(code)
if (err(ast)) return ast if (err(ast)) return ast
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const sourceStart = code.indexOf(lineToChange) const sourceStart = code.indexOf(lineToChange)
expect(sourceStart).toBe(95) expect(sourceStart).toBe(95)
const newSketchLnRetVal = addNewSketchLn({ const newSketchLnRetVal = addNewSketchLn({
node: ast, node: ast,
programMemory, programMemory: execState.memory,
input: { input: {
type: 'straight-segment', type: 'straight-segment',
from: [0, 0], from: [0, 0],
@ -186,7 +186,7 @@ const mySketch001 = startSketchOn('XY')
const modifiedAst2 = addCloseToPipe({ const modifiedAst2 = addCloseToPipe({
node: ast, node: ast,
programMemory, programMemory: execState.memory,
pathToNode: [ pathToNode: [
['body', ''], ['body', ''],
[0, 'index'], [0, 'index'],
@ -230,7 +230,7 @@ describe('testing addTagForSketchOnFace', () => {
const pathToNode = getNodePathFromSourceRange(ast, sourceRange) const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
const sketchOnFaceRetVal = addTagForSketchOnFace( const sketchOnFaceRetVal = addTagForSketchOnFace(
{ {
// previousProgramMemory: programMemory, // redundant? // previousProgramMemory: execState.memory, // redundant?
pathToNode, pathToNode,
node: ast, node: ast,
}, },

View File

@ -40,7 +40,7 @@ async function testingSwapSketchFnCall({
const ast = parse(inputCode) const ast = parse(inputCode)
if (err(ast)) return Promise.reject(ast) if (err(ast)) return Promise.reject(ast)
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const selections = { const selections = {
codeBasedSelections: [range], codeBasedSelections: [range],
otherSelections: [], otherSelections: [],
@ -51,7 +51,7 @@ async function testingSwapSketchFnCall({
return Promise.reject(new Error('transformInfos undefined')) return Promise.reject(new Error('transformInfos undefined'))
const ast2 = transformAstSketchLines({ const ast2 = transformAstSketchLines({
ast, ast,
programMemory, programMemory: execState.memory,
selectionRanges: selections, selectionRanges: selections,
transformInfos, transformInfos,
referenceSegName: '', referenceSegName: '',
@ -366,10 +366,10 @@ const part001 = startSketchOn('XY')
|> line([2.14, 1.35], %) // normal-segment |> line([2.14, 1.35], %) // normal-segment
|> xLine(3.54, %)` |> xLine(3.54, %)`
it('normal case works', async () => { it('normal case works', async () => {
const programMemory = await enginelessExecutor(parse(code)) const execState = await enginelessExecutor(parse(code))
const index = code.indexOf('// normal-segment') - 7 const index = code.indexOf('// normal-segment') - 7
const sg = sketchGroupFromKclValue( const sg = sketchGroupFromKclValue(
programMemory.get('part001'), execState.memory.get('part001'),
'part001' 'part001'
) as SketchGroup ) as SketchGroup
const _segment = getSketchSegmentFromSourceRange(sg, [index, index]) const _segment = getSketchSegmentFromSourceRange(sg, [index, index])
@ -383,11 +383,11 @@ const part001 = startSketchOn('XY')
}) })
}) })
it('verify it works when the segment is in the `start` property', async () => { it('verify it works when the segment is in the `start` property', async () => {
const programMemory = await enginelessExecutor(parse(code)) const execState = await enginelessExecutor(parse(code))
const index = code.indexOf('// segment-in-start') - 7 const index = code.indexOf('// segment-in-start') - 7
const _segment = getSketchSegmentFromSourceRange( const _segment = getSketchSegmentFromSourceRange(
sketchGroupFromKclValue( sketchGroupFromKclValue(
programMemory.get('part001'), execState.memory.get('part001'),
'part001' 'part001'
) as SketchGroup, ) as SketchGroup,
[index, index] [index, index]

View File

@ -220,7 +220,7 @@ const part001 = startSketchOn('XY')
} }
}) })
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const transformInfos = getTransformInfos( const transformInfos = getTransformInfos(
makeSelections(selectionRanges.slice(1)), makeSelections(selectionRanges.slice(1)),
ast, ast,
@ -231,7 +231,7 @@ const part001 = startSketchOn('XY')
ast, ast,
selectionRanges: makeSelections(selectionRanges), selectionRanges: makeSelections(selectionRanges),
transformInfos, transformInfos,
programMemory, programMemory: execState.memory,
}) })
if (err(newAst)) return Promise.reject(newAst) if (err(newAst)) return Promise.reject(newAst)
@ -311,7 +311,7 @@ const part001 = startSketchOn('XY')
} }
}) })
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const transformInfos = getTransformInfos( const transformInfos = getTransformInfos(
makeSelections(selectionRanges), makeSelections(selectionRanges),
ast, ast,
@ -322,7 +322,7 @@ const part001 = startSketchOn('XY')
ast, ast,
selectionRanges: makeSelections(selectionRanges), selectionRanges: makeSelections(selectionRanges),
transformInfos, transformInfos,
programMemory, programMemory: execState.memory,
referenceSegName: '', referenceSegName: '',
}) })
if (err(newAst)) return Promise.reject(newAst) if (err(newAst)) return Promise.reject(newAst)
@ -373,7 +373,7 @@ const part001 = startSketchOn('XY')
} }
}) })
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const transformInfos = getTransformInfos( const transformInfos = getTransformInfos(
makeSelections(selectionRanges), makeSelections(selectionRanges),
ast, ast,
@ -384,7 +384,7 @@ const part001 = startSketchOn('XY')
ast, ast,
selectionRanges: makeSelections(selectionRanges), selectionRanges: makeSelections(selectionRanges),
transformInfos, transformInfos,
programMemory, programMemory: execState.memory,
referenceSegName: '', referenceSegName: '',
}) })
if (err(newAst)) return Promise.reject(newAst) if (err(newAst)) return Promise.reject(newAst)
@ -470,7 +470,7 @@ async function helperThing(
} }
}) })
const programMemory = await enginelessExecutor(ast) const execState = await enginelessExecutor(ast)
const transformInfos = getTransformInfos( const transformInfos = getTransformInfos(
makeSelections(selectionRanges.slice(1)), makeSelections(selectionRanges.slice(1)),
ast, ast,
@ -481,7 +481,7 @@ async function helperThing(
ast, ast,
selectionRanges: makeSelections(selectionRanges), selectionRanges: makeSelections(selectionRanges),
transformInfos, transformInfos,
programMemory, programMemory: execState.memory,
}) })
if (err(newAst)) return Promise.reject(newAst) if (err(newAst)) return Promise.reject(newAst)

View File

@ -17,9 +17,9 @@ describe('testing angledLineThatIntersects', () => {
offset: ${offset}, offset: ${offset},
}, %, $yo2) }, %, $yo2)
const intersect = segEndX(yo2)` const intersect = segEndX(yo2)`
const mem = await enginelessExecutor(parse(code('-1'))) const execState = await enginelessExecutor(parse(code('-1')))
expect(mem.get('intersect')?.value).toBe(1 + Math.sqrt(2)) expect(execState.memory.get('intersect')?.value).toBe(1 + Math.sqrt(2))
const noOffset = await enginelessExecutor(parse(code('0'))) const noOffset = await enginelessExecutor(parse(code('0')))
expect(noOffset.get('intersect')?.value).toBeCloseTo(1) expect(noOffset.memory.get('intersect')?.value).toBeCloseTo(1)
}) })
}) })

View File

@ -37,6 +37,8 @@ import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
import { DeepPartial } from 'lib/types' import { DeepPartial } from 'lib/types'
import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration' import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration'
import { SketchGroup } from '../wasm-lib/kcl/bindings/SketchGroup' import { SketchGroup } from '../wasm-lib/kcl/bindings/SketchGroup'
import { ArtifactId } from 'wasm-lib/kcl/bindings/ArtifactId'
import { Artifact } from 'wasm-lib/kcl/bindings/Artifact'
export type { Program } from '../wasm-lib/kcl/bindings/Program' export type { Program } from '../wasm-lib/kcl/bindings/Program'
export type { Expr } from '../wasm-lib/kcl/bindings/Expr' export type { Expr } from '../wasm-lib/kcl/bindings/Expr'
@ -136,6 +138,34 @@ export const parse = (code: string | Error): Program | Error => {
export type PathToNode = [string | number, string][] export type PathToNode = [string | number, string][]
interface RawExecState {
memory: RawProgramMemory
artifacts: { [key: ArtifactId]: Artifact }
}
export interface ExecState {
memory: ProgramMemory
artifacts: { [key: ArtifactId]: Artifact }
}
/**
* Create an empty ExecState. This is useful on init to prevent needing an
* Option.
*/
export function emptyExecState(): ExecState {
return {
memory: ProgramMemory.empty(),
artifacts: {},
}
}
function execStateFromRaw(raw: RawExecState): ExecState {
return {
memory: ProgramMemory.fromRaw(raw.memory),
artifacts: raw.artifacts,
}
}
interface Memory { interface Memory {
[key: string]: KclValue [key: string]: KclValue
} }
@ -353,12 +383,49 @@ export function sketchGroupFromKclValue(
} }
} }
export function optionalSketchGroupFromKclValue(
value: KclValue
): SketchGroup | null {
if (value.type === 'UserVal' && value.value.type === 'SketchGroup')
return value.value
return null
}
export function kclValueFromArtifactId(
execState: ExecState,
id: ArtifactId
): KclValue | null {
const artifact = execState.artifacts[id]
if (!artifact) {
console.warn('kclValueFromArtifactId id not found', id, execState)
}
if (!artifact) return null
return artifact.value
}
export function sketchGroupFromArtifactId(
execState: ExecState,
id: ArtifactId
): SketchGroup | null {
const kclValue = kclValueFromArtifactId(execState, id)
if (!kclValue) {
console.warn('sketchGroupFromArtifactId id not found', id)
return null
}
const sketch = optionalSketchGroupFromKclValue(kclValue)
if (!sketch) {
console.warn('sketchGroupFromArtifactId not a SketchGroup', kclValue)
return null
}
return sketch
}
export const executor = async ( export const executor = async (
node: Program, node: Program,
programMemory: ProgramMemory | Error = ProgramMemory.empty(), programMemory: ProgramMemory | Error = ProgramMemory.empty(),
engineCommandManager: EngineCommandManager, engineCommandManager: EngineCommandManager,
isMock: boolean = false isMock: boolean = false
): Promise<ProgramMemory> => { ): Promise<ExecState> => {
if (err(programMemory)) return Promise.reject(programMemory) if (err(programMemory)) return Promise.reject(programMemory)
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
@ -380,7 +447,7 @@ export const _executor = async (
programMemory: ProgramMemory | Error = ProgramMemory.empty(), programMemory: ProgramMemory | Error = ProgramMemory.empty(),
engineCommandManager: EngineCommandManager, engineCommandManager: EngineCommandManager,
isMock: boolean isMock: boolean
): Promise<ProgramMemory> => { ): Promise<ExecState> => {
if (err(programMemory)) return Promise.reject(programMemory) if (err(programMemory)) return Promise.reject(programMemory)
try { try {
@ -392,7 +459,7 @@ export const _executor = async (
baseUnit = baseUnit =
(await getSettingsState)()?.modeling.defaultUnit.current || 'mm' (await getSettingsState)()?.modeling.defaultUnit.current || 'mm'
} }
const memory: RawProgramMemory = await execute_wasm( const execState: RawExecState = await execute_wasm(
JSON.stringify(node), JSON.stringify(node),
JSON.stringify(programMemory.toRaw()), JSON.stringify(programMemory.toRaw()),
baseUnit, baseUnit,
@ -400,7 +467,7 @@ export const _executor = async (
fileSystemManager, fileSystemManager,
isMock isMock
) )
return ProgramMemory.fromRaw(memory) return execStateFromRaw(execState)
} catch (e: any) { } catch (e: any) {
console.log(e) console.log(e)
const parsed: RustKclError = JSON.parse(e.toString()) const parsed: RustKclError = JSON.parse(e.toString())

View File

@ -1,4 +1,10 @@
import { Program, ProgramMemory, _executor, SourceRange } from '../lang/wasm' import {
Program,
ProgramMemory,
_executor,
SourceRange,
ExecState,
} from '../lang/wasm'
import { import {
EngineCommandManager, EngineCommandManager,
EngineCommandManagerEvents, EngineCommandManagerEvents,
@ -78,7 +84,7 @@ class MockEngineCommandManager {
export async function enginelessExecutor( export async function enginelessExecutor(
ast: Program | Error, ast: Program | Error,
pm: ProgramMemory | Error = ProgramMemory.empty() pm: ProgramMemory | Error = ProgramMemory.empty()
): Promise<ProgramMemory> { ): Promise<ExecState> {
if (err(ast)) return Promise.reject(ast) if (err(ast)) return Promise.reject(ast)
if (err(pm)) return Promise.reject(pm) if (err(pm)) return Promise.reject(pm)
@ -88,15 +94,15 @@ export async function enginelessExecutor(
}) as any as EngineCommandManager }) as any as EngineCommandManager
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
mockEngineCommandManager.startNewSession() mockEngineCommandManager.startNewSession()
const programMemory = await _executor(ast, pm, mockEngineCommandManager, true) const execState = await _executor(ast, pm, mockEngineCommandManager, true)
await mockEngineCommandManager.waitForAllCommands() await mockEngineCommandManager.waitForAllCommands()
return programMemory return execState
} }
export async function executor( export async function executor(
ast: Program, ast: Program,
pm: ProgramMemory = ProgramMemory.empty() pm: ProgramMemory = ProgramMemory.empty()
): Promise<ProgramMemory> { ): Promise<ExecState> {
const engineCommandManager = new EngineCommandManager() const engineCommandManager = new EngineCommandManager()
engineCommandManager.start({ engineCommandManager.start({
setIsStreamReady: () => {}, setIsStreamReady: () => {},
@ -117,14 +123,9 @@ export async function executor(
toSync(async () => { toSync(async () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
engineCommandManager.startNewSession() engineCommandManager.startNewSession()
const programMemory = await _executor( const execState = await _executor(ast, pm, engineCommandManager, false)
ast,
pm,
engineCommandManager,
false
)
await engineCommandManager.waitForAllCommands() await engineCommandManager.waitForAllCommands()
resolve(programMemory) resolve(execState)
}, reportRejection) }, reportRejection)
) )
}) })

View File

@ -97,7 +97,7 @@ export function useCalculateKclExpression({
}) })
if (trap(error, { suppress: true })) return if (trap(error, { suppress: true })) return
} }
const { programMemory } = await executeAst({ const { execState } = await executeAst({
ast, ast,
engineCommandManager, engineCommandManager,
useFakeExecutor: true, useFakeExecutor: true,
@ -111,7 +111,7 @@ export function useCalculateKclExpression({
const init = const init =
resultDeclaration?.type === 'VariableDeclaration' && resultDeclaration?.type === 'VariableDeclaration' &&
resultDeclaration?.declarations?.[0]?.init resultDeclaration?.declarations?.[0]?.init
const result = programMemory?.get('__result__')?.value const result = execState.memory?.get('__result__')?.value
setCalcResult(typeof result === 'number' ? String(result) : 'NAN') setCalcResult(typeof result === 'number' ? String(result) : 'NAN')
init && setValueNode(init) init && setValueNode(init)
} }

View File

@ -59,7 +59,7 @@ pub async fn execute_wasm(
// gloo-serialize crate instead. // gloo-serialize crate instead.
// DO NOT USE serde_wasm_bindgen::to_value(&memory).map_err(|e| e.to_string()) // DO NOT USE serde_wasm_bindgen::to_value(&memory).map_err(|e| e.to_string())
// it will break the frontend. // it will break the frontend.
JsValue::from_serde(&exec_state.memory).map_err(|e| e.to_string()) JsValue::from_serde(&exec_state).map_err(|e| e.to_string())
} }
// wasm_bindgen wrapper for execute // wasm_bindgen wrapper for execute