2024-07-02 17:16:27 +10:00
|
|
|
import {
|
|
|
|
|
Program,
|
|
|
|
|
_executor,
|
|
|
|
|
ProgramMemory,
|
|
|
|
|
programMemoryInit,
|
|
|
|
|
kclLint,
|
|
|
|
|
} from 'lang/wasm'
|
|
|
|
|
import { enginelessExecutor } from 'lib/testHelpers'
|
|
|
|
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
|
|
|
|
import { KCLError } from 'lang/errors'
|
|
|
|
|
import { Diagnostic } from '@codemirror/lint'
|
|
|
|
|
|
|
|
|
|
export type ToolTip =
|
|
|
|
|
| 'lineTo'
|
|
|
|
|
| 'line'
|
|
|
|
|
| 'angledLine'
|
|
|
|
|
| 'angledLineOfXLength'
|
|
|
|
|
| 'angledLineOfYLength'
|
|
|
|
|
| 'angledLineToX'
|
|
|
|
|
| 'angledLineToY'
|
|
|
|
|
| 'xLine'
|
|
|
|
|
| 'yLine'
|
|
|
|
|
| 'xLineTo'
|
|
|
|
|
| 'yLineTo'
|
|
|
|
|
| 'angledLineThatIntersects'
|
|
|
|
|
| 'tangentialArcTo'
|
|
|
|
|
|
|
|
|
|
export const toolTips = [
|
|
|
|
|
'sketch_line',
|
|
|
|
|
'move',
|
|
|
|
|
// original tooltips
|
|
|
|
|
'line',
|
|
|
|
|
'lineTo',
|
|
|
|
|
'angledLine',
|
|
|
|
|
'angledLineOfXLength',
|
|
|
|
|
'angledLineOfYLength',
|
|
|
|
|
'angledLineToX',
|
|
|
|
|
'angledLineToY',
|
|
|
|
|
'xLine',
|
|
|
|
|
'yLine',
|
|
|
|
|
'xLineTo',
|
|
|
|
|
'yLineTo',
|
|
|
|
|
'angledLineThatIntersects',
|
|
|
|
|
'tangentialArcTo',
|
|
|
|
|
] as any as ToolTip[]
|
|
|
|
|
|
|
|
|
|
export async function executeAst({
|
|
|
|
|
ast,
|
|
|
|
|
engineCommandManager,
|
|
|
|
|
useFakeExecutor = false,
|
|
|
|
|
programMemoryOverride,
|
|
|
|
|
}: {
|
|
|
|
|
ast: Program
|
|
|
|
|
engineCommandManager: EngineCommandManager
|
|
|
|
|
useFakeExecutor?: boolean
|
|
|
|
|
programMemoryOverride?: ProgramMemory
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted?: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
}): Promise<{
|
|
|
|
|
logs: string[]
|
|
|
|
|
errors: KCLError[]
|
|
|
|
|
programMemory: ProgramMemory
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
}> {
|
|
|
|
|
try {
|
|
|
|
|
if (!useFakeExecutor) {
|
|
|
|
|
engineCommandManager.endSession()
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-07-02 17:16:27 +10:00
|
|
|
engineCommandManager.startNewSession()
|
|
|
|
|
}
|
|
|
|
|
const programMemory = await (useFakeExecutor
|
|
|
|
|
? enginelessExecutor(ast, programMemoryOverride || programMemoryInit())
|
|
|
|
|
: _executor(ast, programMemoryInit(), engineCommandManager, false))
|
|
|
|
|
|
|
|
|
|
await engineCommandManager.waitForAllCommands()
|
|
|
|
|
return {
|
|
|
|
|
logs: [],
|
|
|
|
|
errors: [],
|
|
|
|
|
programMemory,
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted: false,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
} catch (e: any) {
|
2024-08-30 05:14:24 -05:00
|
|
|
let isInterrupted = false
|
2024-07-02 17:16:27 +10:00
|
|
|
if (e instanceof KCLError) {
|
2024-08-30 05:14:24 -05:00
|
|
|
// Detect if it is a force interrupt error which is not a KCL processing error.
|
|
|
|
|
if (
|
|
|
|
|
e.msg ===
|
|
|
|
|
'Failed to wait for promise from engine: JsValue("Force interrupt, executionIsStale, new AST requested")'
|
|
|
|
|
) {
|
|
|
|
|
isInterrupted = true
|
|
|
|
|
}
|
2024-07-02 17:16:27 +10:00
|
|
|
return {
|
|
|
|
|
errors: [e],
|
|
|
|
|
logs: [],
|
2024-07-22 19:43:40 -04:00
|
|
|
programMemory: ProgramMemory.empty(),
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.log(e)
|
|
|
|
|
return {
|
|
|
|
|
logs: [e],
|
|
|
|
|
errors: [],
|
2024-07-22 19:43:40 -04:00
|
|
|
programMemory: ProgramMemory.empty(),
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function lintAst({
|
|
|
|
|
ast,
|
|
|
|
|
}: {
|
|
|
|
|
ast: Program
|
|
|
|
|
}): Promise<Array<Diagnostic>> {
|
|
|
|
|
try {
|
|
|
|
|
const discovered_findings = await kclLint(ast)
|
|
|
|
|
return discovered_findings.map((lint) => {
|
|
|
|
|
return {
|
|
|
|
|
message: lint.finding.title,
|
|
|
|
|
severity: 'info',
|
|
|
|
|
from: lint.pos[0],
|
|
|
|
|
to: lint.pos[1],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.log(e)
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
}
|