2023-09-29 11:11:01 -07:00
|
|
|
import { Program, ProgramMemory, _executor, SourceRange } from '../lang/wasm'
|
2023-08-24 15:34:51 -07:00
|
|
|
import {
|
|
|
|
EngineCommandManager,
|
|
|
|
EngineCommand,
|
|
|
|
} from '../lang/std/engineConnection'
|
2023-09-20 18:27:08 -07:00
|
|
|
import { Models } from '@kittycad/lib'
|
2024-03-22 09:35:07 -04:00
|
|
|
import { Themes } from './theme'
|
2023-09-20 18:27:08 -07:00
|
|
|
|
2024-04-09 11:51:41 -07:00
|
|
|
type WebSocketResponse = Models['WebSocketResponse_type']
|
2023-06-22 16:43:33 +10:00
|
|
|
|
2023-07-10 15:15:07 +10:00
|
|
|
class MockEngineCommandManager {
|
2023-11-01 07:39:31 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
2023-07-10 15:15:07 +10:00
|
|
|
constructor(mockParams: {
|
|
|
|
setIsStreamReady: (isReady: boolean) => void
|
|
|
|
setMediaStream: (stream: MediaStream) => void
|
|
|
|
}) {}
|
|
|
|
startNewSession() {}
|
|
|
|
waitForAllCommands() {}
|
|
|
|
waitForReady = new Promise<void>((resolve) => resolve())
|
2023-08-24 15:34:51 -07:00
|
|
|
sendModelingCommand({
|
|
|
|
id,
|
|
|
|
range,
|
|
|
|
command,
|
|
|
|
}: {
|
|
|
|
id: string
|
|
|
|
range: SourceRange
|
|
|
|
command: EngineCommand
|
|
|
|
}): Promise<any> {
|
2023-09-20 18:27:08 -07:00
|
|
|
const response: WebSocketResponse = {
|
2024-04-09 11:51:41 -07:00
|
|
|
success: true,
|
|
|
|
resp: {
|
|
|
|
type: 'modeling',
|
|
|
|
data: {
|
|
|
|
modeling_response: { type: 'empty' },
|
|
|
|
},
|
2023-09-20 18:27:08 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return Promise.resolve(JSON.stringify(response))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
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 })
|
|
|
|
}
|
2023-07-10 15:15:07 +10:00
|
|
|
sendSceneCommand() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function enginelessExecutor(
|
|
|
|
ast: Program,
|
2023-09-12 18:10:27 -07:00
|
|
|
pm: ProgramMemory = { root: {}, return: null }
|
2023-07-10 15:15:07 +10:00
|
|
|
): Promise<ProgramMemory> {
|
|
|
|
const mockEngineCommandManager = new MockEngineCommandManager({
|
|
|
|
setIsStreamReady: () => {},
|
|
|
|
setMediaStream: () => {},
|
|
|
|
}) as any as EngineCommandManager
|
|
|
|
await mockEngineCommandManager.waitForReady
|
|
|
|
mockEngineCommandManager.startNewSession()
|
2024-03-26 19:32:31 -07:00
|
|
|
const programMemory = await _executor(ast, pm, mockEngineCommandManager, true)
|
2023-07-10 15:15:07 +10:00
|
|
|
await mockEngineCommandManager.waitForAllCommands()
|
|
|
|
return programMemory
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
export async function executor(
|
|
|
|
ast: Program,
|
2023-09-12 18:10:27 -07:00
|
|
|
pm: ProgramMemory = { root: {}, return: null }
|
2023-06-22 16:43:33 +10:00
|
|
|
): Promise<ProgramMemory> {
|
2023-09-25 19:49:53 -07:00
|
|
|
const engineCommandManager = new EngineCommandManager()
|
|
|
|
engineCommandManager.start({
|
2023-06-23 14:19:15 +10:00
|
|
|
setIsStreamReady: () => {},
|
|
|
|
setMediaStream: () => {},
|
2023-09-25 19:49:53 -07:00
|
|
|
width: 0,
|
|
|
|
height: 0,
|
2023-09-29 12:41:58 -07:00
|
|
|
executeCode: () => {},
|
2024-03-22 09:35:07 -04:00
|
|
|
theme: Themes.Dark,
|
2023-06-23 14:19:15 +10:00
|
|
|
})
|
2023-07-10 15:15:07 +10:00
|
|
|
await engineCommandManager.waitForReady
|
2023-06-22 16:43:33 +10:00
|
|
|
engineCommandManager.startNewSession()
|
2024-03-26 19:32:31 -07:00
|
|
|
const programMemory = await _executor(ast, pm, engineCommandManager, false)
|
2023-10-14 03:47:46 +11:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2023-06-22 16:43:33 +10:00
|
|
|
return programMemory
|
|
|
|
}
|