2024-07-02 17:16:27 +10:00
|
|
|
import {
|
|
|
|
|
Program,
|
2025-01-08 12:46:05 +11:00
|
|
|
executor,
|
2024-07-02 17:16:27 +10:00
|
|
|
ProgramMemory,
|
|
|
|
|
kclLint,
|
2024-10-09 19:38:40 -04:00
|
|
|
emptyExecState,
|
|
|
|
|
ExecState,
|
2024-07-02 17:16:27 +10:00
|
|
|
} from 'lang/wasm'
|
|
|
|
|
import { enginelessExecutor } from 'lib/testHelpers'
|
|
|
|
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
|
|
|
|
import { KCLError } from 'lang/errors'
|
|
|
|
|
import { Diagnostic } from '@codemirror/lint'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2024-07-02 17:16:27 +10:00
|
|
|
|
|
|
|
|
export type ToolTip =
|
|
|
|
|
| 'lineTo'
|
|
|
|
|
| 'line'
|
|
|
|
|
| 'angledLine'
|
|
|
|
|
| 'angledLineOfXLength'
|
|
|
|
|
| 'angledLineOfYLength'
|
|
|
|
|
| 'angledLineToX'
|
|
|
|
|
| 'angledLineToY'
|
|
|
|
|
| 'xLine'
|
|
|
|
|
| 'yLine'
|
|
|
|
|
| 'xLineTo'
|
|
|
|
|
| 'yLineTo'
|
|
|
|
|
| 'angledLineThatIntersects'
|
|
|
|
|
| 'tangentialArcTo'
|
2024-09-23 22:42:51 +10:00
|
|
|
| 'circle'
|
2024-07-02 17:16:27 +10:00
|
|
|
|
2024-09-23 22:42:51 +10:00
|
|
|
export const toolTips: Array<ToolTip> = [
|
2024-07-02 17:16:27 +10:00
|
|
|
'line',
|
|
|
|
|
'lineTo',
|
|
|
|
|
'angledLine',
|
|
|
|
|
'angledLineOfXLength',
|
|
|
|
|
'angledLineOfYLength',
|
|
|
|
|
'angledLineToX',
|
|
|
|
|
'angledLineToY',
|
|
|
|
|
'xLine',
|
|
|
|
|
'yLine',
|
|
|
|
|
'xLineTo',
|
|
|
|
|
'yLineTo',
|
|
|
|
|
'angledLineThatIntersects',
|
|
|
|
|
'tangentialArcTo',
|
2024-09-13 21:14:14 +10:00
|
|
|
]
|
2024-07-02 17:16:27 +10:00
|
|
|
|
|
|
|
|
export async function executeAst({
|
|
|
|
|
ast,
|
2025-01-28 15:43:39 -08:00
|
|
|
path,
|
2024-07-02 17:16:27 +10:00
|
|
|
engineCommandManager,
|
2024-12-05 19:51:06 -08:00
|
|
|
// If you set programMemoryOverride we assume you mean mock mode. Since that
|
|
|
|
|
// is the only way to go about it.
|
2024-07-02 17:16:27 +10:00
|
|
|
programMemoryOverride,
|
|
|
|
|
}: {
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>
|
2025-01-28 15:43:39 -08:00
|
|
|
path?: string
|
2024-07-02 17:16:27 +10:00
|
|
|
engineCommandManager: EngineCommandManager
|
|
|
|
|
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[]
|
2024-10-09 19:38:40 -04:00
|
|
|
execState: ExecState
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
}> {
|
|
|
|
|
try {
|
2024-12-05 19:51:06 -08:00
|
|
|
const execState = await (programMemoryOverride
|
2025-01-28 15:43:39 -08:00
|
|
|
? enginelessExecutor(ast, programMemoryOverride, path)
|
|
|
|
|
: executor(ast, engineCommandManager, path))
|
2024-12-05 19:51:06 -08:00
|
|
|
|
2024-12-12 18:06:26 -08:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2024-12-16 10:34:11 -05:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
return {
|
|
|
|
|
logs: [],
|
|
|
|
|
errors: [],
|
2024-10-09 19:38:40 -04:00
|
|
|
execState,
|
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-10-09 19:38:40 -04:00
|
|
|
execState: emptyExecState(),
|
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-10-09 19:38:40 -04:00
|
|
|
execState: emptyExecState(),
|
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 []
|
|
|
|
|
}
|
|
|
|
|
}
|