* all Signed-off-by: Jess Frazelle <github@jessfraz.com> * more Signed-off-by: Jess Frazelle <github@jessfraz.com> * side quests only Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
113 lines
3.1 KiB
TypeScript
113 lines
3.1 KiB
TypeScript
import { Program, ProgramMemory, _executor, SourceRange } from '../lang/wasm'
|
|
import {
|
|
EngineCommandManager,
|
|
EngineCommand,
|
|
} from '../lang/std/engineConnection'
|
|
import { Models } from '@kittycad/lib'
|
|
import { Themes } from './theme'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
|
|
|
|
type WebSocketResponse = Models['WebSocketResponse_type']
|
|
|
|
const defaultPlanes: DefaultPlanes = {
|
|
xy: uuidv4(),
|
|
xz: uuidv4(),
|
|
yz: uuidv4(),
|
|
negXy: uuidv4(),
|
|
negXz: uuidv4(),
|
|
negYz: uuidv4(),
|
|
}
|
|
|
|
class MockEngineCommandManager {
|
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
constructor(mockParams: {
|
|
setIsStreamReady: (isReady: boolean) => void
|
|
setMediaStream: (stream: MediaStream) => void
|
|
}) {}
|
|
startNewSession() {}
|
|
waitForAllCommands() {}
|
|
waitForReady = new Promise<void>((resolve) => resolve())
|
|
sendModelingCommand({
|
|
id,
|
|
range,
|
|
command,
|
|
}: {
|
|
id: string
|
|
range: SourceRange
|
|
command: EngineCommand
|
|
}): Promise<any> {
|
|
const response: WebSocketResponse = {
|
|
success: true,
|
|
resp: {
|
|
type: 'modeling',
|
|
data: {
|
|
modeling_response: { type: 'empty' },
|
|
},
|
|
},
|
|
}
|
|
return Promise.resolve(JSON.stringify(response))
|
|
}
|
|
async wasmGetDefaultPlanes(): Promise<string> {
|
|
return JSON.stringify(defaultPlanes)
|
|
}
|
|
sendModelingCommandFromWasm(
|
|
id: string,
|
|
rangeStr: string,
|
|
commandStr: string
|
|
): Promise<any> {
|
|
if (id === undefined) {
|
|
throw new Error('id is undefined')
|
|
}
|
|
if (rangeStr === undefined) {
|
|
throw new Error('rangeStr is undefined')
|
|
}
|
|
if (commandStr === undefined) {
|
|
throw new Error('commandStr is undefined')
|
|
}
|
|
const command: EngineCommand = JSON.parse(commandStr)
|
|
const range: SourceRange = JSON.parse(rangeStr)
|
|
|
|
return this.sendModelingCommand({ id, range, command })
|
|
}
|
|
sendSceneCommand() {}
|
|
}
|
|
|
|
export async function enginelessExecutor(
|
|
ast: Program,
|
|
pm: ProgramMemory = { root: {}, return: null }
|
|
): Promise<ProgramMemory> {
|
|
const mockEngineCommandManager = new MockEngineCommandManager({
|
|
setIsStreamReady: () => {},
|
|
setMediaStream: () => {},
|
|
}) as any as EngineCommandManager
|
|
await mockEngineCommandManager.waitForReady
|
|
mockEngineCommandManager.startNewSession()
|
|
const programMemory = await _executor(ast, pm, mockEngineCommandManager, true)
|
|
await mockEngineCommandManager.waitForAllCommands()
|
|
return programMemory
|
|
}
|
|
|
|
export async function executor(
|
|
ast: Program,
|
|
pm: ProgramMemory = { root: {}, return: null }
|
|
): Promise<ProgramMemory> {
|
|
const engineCommandManager = new EngineCommandManager()
|
|
engineCommandManager.start({
|
|
setIsStreamReady: () => {},
|
|
setMediaStream: () => {},
|
|
width: 0,
|
|
height: 0,
|
|
executeCode: () => {},
|
|
theme: Themes.Dark,
|
|
makeDefaultPlanes: () => {
|
|
return new Promise((resolve) => resolve(defaultPlanes))
|
|
},
|
|
})
|
|
await engineCommandManager.waitForReady
|
|
engineCommandManager.startNewSession()
|
|
const programMemory = await _executor(ast, pm, engineCommandManager, false)
|
|
await engineCommandManager.waitForAllCommands()
|
|
return programMemory
|
|
}
|