2023-09-29 11:11:01 -07:00
|
|
|
import init, {
|
|
|
|
parse_wasm,
|
|
|
|
recast_wasm,
|
|
|
|
execute_wasm,
|
2024-06-26 00:04:52 -04:00
|
|
|
kcl_lint,
|
2023-09-29 11:11:01 -07:00
|
|
|
lexer_wasm,
|
|
|
|
modify_ast_for_sketch_wasm,
|
2024-02-11 12:59:00 +11:00
|
|
|
is_points_ccw,
|
|
|
|
get_tangential_arc_to_info,
|
2024-02-11 18:26:09 -08:00
|
|
|
program_memory_init,
|
2024-04-15 17:18:32 -07:00
|
|
|
make_default_planes,
|
2024-06-30 19:21:24 -07:00
|
|
|
modify_grid,
|
2024-04-09 18:05:36 -07:00
|
|
|
coredump,
|
2024-04-16 21:36:19 -07:00
|
|
|
toml_stringify,
|
2024-04-25 00:13:09 -07:00
|
|
|
default_app_settings,
|
|
|
|
parse_app_settings,
|
|
|
|
parse_project_settings,
|
|
|
|
default_project_settings,
|
2024-08-14 14:26:44 -04:00
|
|
|
base64_decode,
|
2023-09-29 11:11:01 -07:00
|
|
|
} from '../wasm-lib/pkg/wasm_lib'
|
|
|
|
import { KCLError } from './errors'
|
|
|
|
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
2023-10-14 03:47:46 +11:00
|
|
|
import { EngineCommandManager } from './std/engineConnection'
|
2024-06-26 00:04:52 -04:00
|
|
|
import { Discovered } from '../wasm-lib/kcl/bindings/Discovered'
|
2024-08-12 16:53:24 -05:00
|
|
|
import { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
|
2023-09-29 11:11:01 -07:00
|
|
|
import type { Program } from '../wasm-lib/kcl/bindings/Program'
|
|
|
|
import type { Token } from '../wasm-lib/kcl/bindings/Token'
|
2024-02-11 12:59:00 +11:00
|
|
|
import { Coords2d } from './std/sketch'
|
2024-02-12 12:18:37 -08:00
|
|
|
import { fileSystemManager } from 'lang/std/fileSystemManager'
|
2024-06-20 16:36:28 -07:00
|
|
|
import { CoreDumpInfo } from 'wasm-lib/kcl/bindings/CoreDumpInfo'
|
2024-04-09 18:05:36 -07:00
|
|
|
import { CoreDumpManager } from 'lib/coredump'
|
|
|
|
import openWindow from 'lib/openWindow'
|
2024-04-15 17:18:32 -07:00
|
|
|
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { TEST } from 'env'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-08-20 22:16:44 -04:00
|
|
|
import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
|
|
|
|
import { DeepPartial } from 'lib/types'
|
|
|
|
import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration'
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
import { SketchGroup } from '../wasm-lib/kcl/bindings/SketchGroup'
|
2023-09-29 11:11:01 -07:00
|
|
|
|
|
|
|
export type { Program } from '../wasm-lib/kcl/bindings/Program'
|
2024-08-12 15:38:42 -05:00
|
|
|
export type { Expr } from '../wasm-lib/kcl/bindings/Expr'
|
2023-09-29 11:11:01 -07:00
|
|
|
export type { ObjectExpression } from '../wasm-lib/kcl/bindings/ObjectExpression'
|
|
|
|
export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression'
|
|
|
|
export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression'
|
|
|
|
export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration'
|
2023-11-20 11:19:08 -06:00
|
|
|
export type { Parameter } from '../wasm-lib/kcl/bindings/Parameter'
|
2023-09-29 11:11:01 -07:00
|
|
|
export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution'
|
|
|
|
export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier'
|
|
|
|
export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression'
|
|
|
|
export type { BinaryExpression } from '../wasm-lib/kcl/bindings/BinaryExpression'
|
|
|
|
export type { ReturnStatement } from '../wasm-lib/kcl/bindings/ReturnStatement'
|
|
|
|
export type { ExpressionStatement } from '../wasm-lib/kcl/bindings/ExpressionStatement'
|
|
|
|
export type { CallExpression } from '../wasm-lib/kcl/bindings/CallExpression'
|
|
|
|
export type { VariableDeclarator } from '../wasm-lib/kcl/bindings/VariableDeclarator'
|
|
|
|
export type { BinaryPart } from '../wasm-lib/kcl/bindings/BinaryPart'
|
|
|
|
export type { Literal } from '../wasm-lib/kcl/bindings/Literal'
|
|
|
|
export type { ArrayExpression } from '../wasm-lib/kcl/bindings/ArrayExpression'
|
|
|
|
|
|
|
|
export type SyntaxType =
|
|
|
|
| 'Program'
|
|
|
|
| 'ExpressionStatement'
|
|
|
|
| 'BinaryExpression'
|
|
|
|
| 'CallExpression'
|
|
|
|
| 'Identifier'
|
|
|
|
| 'ReturnStatement'
|
|
|
|
| 'VariableDeclaration'
|
|
|
|
| 'VariableDeclarator'
|
|
|
|
| 'MemberExpression'
|
|
|
|
| 'ArrayExpression'
|
|
|
|
| 'ObjectExpression'
|
|
|
|
| 'ObjectProperty'
|
|
|
|
| 'FunctionExpression'
|
|
|
|
| 'PipeExpression'
|
|
|
|
| 'PipeSubstitution'
|
|
|
|
| 'Literal'
|
|
|
|
| 'NonCodeNode'
|
|
|
|
| 'UnaryExpression'
|
|
|
|
|
|
|
|
export type { SourceRange } from '../wasm-lib/kcl/bindings/SourceRange'
|
|
|
|
export type { Path } from '../wasm-lib/kcl/bindings/Path'
|
|
|
|
export type { SketchGroup } from '../wasm-lib/kcl/bindings/SketchGroup'
|
2024-03-22 10:23:04 +11:00
|
|
|
export type { ExtrudeGroup } from '../wasm-lib/kcl/bindings/ExtrudeGroup'
|
2024-08-12 16:53:24 -05:00
|
|
|
export type { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
|
2023-09-29 11:11:01 -07:00
|
|
|
export type { ExtrudeSurface } from '../wasm-lib/kcl/bindings/ExtrudeSurface'
|
|
|
|
|
2024-04-16 21:36:19 -07:00
|
|
|
export const wasmUrl = () => {
|
2024-08-16 07:15:42 -04:00
|
|
|
// For when we're in electron (file based) or web server (network based)
|
|
|
|
// For some reason relative paths don't work as expected. Otherwise we would
|
|
|
|
// just do /wasm_lib_bg.wasm. In particular, the issue arises when the path
|
|
|
|
// is used from within worker.ts.
|
|
|
|
const fullUrl = document.location.protocol.includes('http')
|
|
|
|
? document.location.origin + '/wasm_lib_bg.wasm'
|
|
|
|
: document.location.protocol +
|
|
|
|
document.location.pathname.split('/').slice(0, -1).join('/') +
|
|
|
|
'/wasm_lib_bg.wasm'
|
|
|
|
|
2024-04-16 21:36:19 -07:00
|
|
|
return fullUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the wasm module.
|
|
|
|
const initialise = async () => {
|
2024-04-19 14:24:40 -07:00
|
|
|
try {
|
|
|
|
const fullUrl = wasmUrl()
|
|
|
|
const input = await fetch(fullUrl)
|
|
|
|
const buffer = await input.arrayBuffer()
|
|
|
|
return await init(buffer)
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Error initialising WASM', e)
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(e)
|
2024-04-19 14:24:40 -07:00
|
|
|
}
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export const initPromise = initialise()
|
|
|
|
|
|
|
|
export const rangeTypeFix = (ranges: number[][]): [number, number][] =>
|
|
|
|
ranges.map(([start, end]) => [start, end])
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
export const parse = (code: string | Error): Program | Error => {
|
|
|
|
if (err(code)) return code
|
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
try {
|
|
|
|
const program: Program = parse_wasm(code)
|
|
|
|
return program
|
|
|
|
} catch (e: any) {
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
// throw e
|
2023-09-29 11:11:01 -07:00
|
|
|
const parsed: RustKclError = JSON.parse(e.toString())
|
2024-06-24 11:45:40 -04:00
|
|
|
return new KCLError(
|
2023-09-29 11:11:01 -07:00
|
|
|
parsed.kind,
|
|
|
|
parsed.msg,
|
|
|
|
rangeTypeFix(parsed.sourceRanges)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PathToNode = [string | number, string][]
|
|
|
|
|
|
|
|
interface Memory {
|
2024-08-12 16:53:24 -05:00
|
|
|
[key: string]: KclValue
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
|
2024-07-22 19:43:40 -04:00
|
|
|
type EnvironmentRef = number
|
|
|
|
|
|
|
|
const ROOT_ENVIRONMENT_REF: EnvironmentRef = 0
|
|
|
|
|
|
|
|
interface Environment {
|
|
|
|
bindings: Memory
|
|
|
|
parent: EnvironmentRef | null
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyEnvironment(): Environment {
|
|
|
|
return { bindings: {}, parent: null }
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RawProgramMemory {
|
|
|
|
environments: Environment[]
|
|
|
|
currentEnv: EnvironmentRef
|
2024-08-12 17:55:05 -05:00
|
|
|
return: KclValue | null
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
|
2024-07-22 19:43:40 -04:00
|
|
|
/**
|
|
|
|
* This duplicates logic in Rust. The hope is to keep ProgramMemory internals
|
|
|
|
* isolated from the rest of the TypeScript code so that we can move it to Rust
|
|
|
|
* in the future.
|
|
|
|
*/
|
|
|
|
export class ProgramMemory {
|
|
|
|
private environments: Environment[]
|
|
|
|
private currentEnv: EnvironmentRef
|
2024-08-12 17:55:05 -05:00
|
|
|
private return: KclValue | null
|
2024-07-22 19:43:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty memory doesn't include prelude definitions.
|
|
|
|
*/
|
|
|
|
static empty(): ProgramMemory {
|
|
|
|
return new ProgramMemory()
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromRaw(raw: RawProgramMemory): ProgramMemory {
|
|
|
|
return new ProgramMemory(raw.environments, raw.currentEnv, raw.return)
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
environments: Environment[] = [emptyEnvironment()],
|
|
|
|
currentEnv: EnvironmentRef = ROOT_ENVIRONMENT_REF,
|
2024-08-12 17:55:05 -05:00
|
|
|
returnVal: KclValue | null = null
|
2024-07-22 19:43:40 -04:00
|
|
|
) {
|
|
|
|
this.environments = environments
|
|
|
|
this.currentEnv = currentEnv
|
|
|
|
this.return = returnVal
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a deep copy.
|
|
|
|
*/
|
|
|
|
clone(): ProgramMemory {
|
2024-07-25 20:11:46 -04:00
|
|
|
return ProgramMemory.fromRaw(structuredClone(this.toRaw()))
|
2024-07-22 19:43:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
has(name: string): boolean {
|
|
|
|
let envRef = this.currentEnv
|
|
|
|
while (true) {
|
|
|
|
const env = this.environments[envRef]
|
|
|
|
if (env.bindings.hasOwnProperty(name)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (!env.parent) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
envRef = env.parent
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:53:24 -05:00
|
|
|
get(name: string): KclValue | null {
|
2024-07-22 19:43:40 -04:00
|
|
|
let envRef = this.currentEnv
|
|
|
|
while (true) {
|
|
|
|
const env = this.environments[envRef]
|
|
|
|
if (env.bindings.hasOwnProperty(name)) {
|
|
|
|
return env.bindings[name]
|
|
|
|
}
|
|
|
|
if (!env.parent) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
envRef = env.parent
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:53:24 -05:00
|
|
|
set(name: string, value: KclValue): Error | null {
|
2024-07-22 19:43:40 -04:00
|
|
|
if (this.environments.length === 0) {
|
|
|
|
return new Error('No environment to set memory in')
|
|
|
|
}
|
|
|
|
const env = this.environments[this.currentEnv]
|
|
|
|
env.bindings[name] = value
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-12 16:53:24 -05:00
|
|
|
* Returns a new ProgramMemory with only `KclValue`s that pass the
|
2024-07-22 19:43:40 -04:00
|
|
|
* predicate. Values are deep copied.
|
|
|
|
*
|
|
|
|
* Note: Return value of the returned ProgramMemory is always null.
|
|
|
|
*/
|
|
|
|
filterVariables(
|
|
|
|
keepPrelude: boolean,
|
2024-08-12 16:53:24 -05:00
|
|
|
predicate: (value: KclValue) => boolean
|
2024-07-22 19:43:40 -04:00
|
|
|
): ProgramMemory | Error {
|
|
|
|
const environments: Environment[] = []
|
|
|
|
for (const [i, env] of this.environments.entries()) {
|
|
|
|
let bindings: Memory
|
|
|
|
if (i === ROOT_ENVIRONMENT_REF && keepPrelude) {
|
|
|
|
// Get prelude definitions. Create these first so that they're always
|
|
|
|
// first in iteration order.
|
|
|
|
const memoryOrError = programMemoryInit()
|
|
|
|
if (err(memoryOrError)) return memoryOrError
|
|
|
|
bindings = memoryOrError.environments[0].bindings
|
|
|
|
} else {
|
|
|
|
bindings = emptyEnvironment().bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [name, value] of Object.entries(env.bindings)) {
|
|
|
|
// Check the predicate.
|
|
|
|
if (!predicate(value)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Deep copy.
|
2024-07-25 20:11:46 -04:00
|
|
|
bindings[name] = structuredClone(value)
|
2024-07-22 19:43:40 -04:00
|
|
|
}
|
|
|
|
environments.push({ bindings, parent: env.parent })
|
|
|
|
}
|
|
|
|
return new ProgramMemory(environments, this.currentEnv, null)
|
|
|
|
}
|
|
|
|
|
|
|
|
numEnvironments(): number {
|
|
|
|
return this.environments.length
|
|
|
|
}
|
|
|
|
|
|
|
|
numVariables(envRef: EnvironmentRef): number {
|
|
|
|
return Object.keys(this.environments[envRef]).length
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all variable entries in memory that are visible, in a flat
|
|
|
|
* structure. If variables are shadowed, they're not visible, and therefore,
|
|
|
|
* not included.
|
|
|
|
*
|
|
|
|
* This should only be used to display in the MemoryPane UI.
|
|
|
|
*/
|
2024-08-12 16:53:24 -05:00
|
|
|
visibleEntries(): Map<string, KclValue> {
|
|
|
|
const map = new Map<string, KclValue>()
|
2024-07-22 19:43:40 -04:00
|
|
|
let envRef = this.currentEnv
|
|
|
|
while (true) {
|
|
|
|
const env = this.environments[envRef]
|
|
|
|
for (const [name, value] of Object.entries(env.bindings)) {
|
|
|
|
// Don't include shadowed variables.
|
|
|
|
if (!map.has(name)) {
|
|
|
|
map.set(name, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!env.parent) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
envRef = env.parent
|
|
|
|
}
|
|
|
|
return map
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any visible variables are a SketchGroup or ExtrudeGroup.
|
|
|
|
*/
|
|
|
|
hasSketchOrExtrudeGroup(): boolean {
|
|
|
|
for (const node of this.visibleEntries().values()) {
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
if (node.type === 'ExtrudeGroup' || node.value?.type === 'SketchGroup') {
|
2024-07-22 19:43:40 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the representation that can be serialized to JSON. This should only
|
|
|
|
* be used within this module.
|
|
|
|
*/
|
|
|
|
toRaw(): RawProgramMemory {
|
|
|
|
return {
|
|
|
|
environments: this.environments,
|
|
|
|
currentEnv: this.currentEnv,
|
|
|
|
return: this.return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
// TODO: In the future, make the parameter be a KclValue.
|
|
|
|
export function sketchGroupFromKclValue(
|
|
|
|
obj: any,
|
|
|
|
varName: string | null
|
|
|
|
): SketchGroup | Error {
|
|
|
|
if (obj?.value?.type === 'SketchGroup') return obj.value
|
2024-08-22 15:00:20 -07:00
|
|
|
if (obj?.value?.type === 'ExtrudeGroup') return obj.value.sketchGroup
|
|
|
|
if (obj?.type === 'ExtrudeGroup') return obj.sketchGroup
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
if (!varName) {
|
|
|
|
varName = 'a KCL value'
|
|
|
|
}
|
|
|
|
const actualType = obj?.value?.type ?? obj?.type
|
|
|
|
if (actualType) {
|
2024-08-22 15:00:20 -07:00
|
|
|
console.log(obj)
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
return new Error(
|
2024-08-22 15:00:20 -07:00
|
|
|
`Expected ${varName} to be a sketchGroup or extrudeGroup, but it was ${actualType} instead.`
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return new Error(`Expected ${varName} to be a sketchGroup, but it wasn't.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
export const executor = async (
|
|
|
|
node: Program,
|
2024-07-22 19:43:40 -04:00
|
|
|
programMemory: ProgramMemory | Error = ProgramMemory.empty(),
|
2024-03-26 19:32:31 -07:00
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
isMock: boolean = false
|
2023-09-29 11:11:01 -07:00
|
|
|
): Promise<ProgramMemory> => {
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(programMemory)) return Promise.reject(programMemory)
|
|
|
|
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2023-09-29 11:11:01 -07:00
|
|
|
engineCommandManager.startNewSession()
|
|
|
|
const _programMemory = await _executor(
|
|
|
|
node,
|
|
|
|
programMemory,
|
2024-03-26 19:32:31 -07:00
|
|
|
engineCommandManager,
|
|
|
|
isMock
|
2023-09-29 11:11:01 -07:00
|
|
|
)
|
2023-10-14 03:47:46 +11:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2023-09-29 11:11:01 -07:00
|
|
|
|
|
|
|
engineCommandManager.endSession()
|
|
|
|
return _programMemory
|
|
|
|
}
|
|
|
|
|
|
|
|
export const _executor = async (
|
|
|
|
node: Program,
|
2024-07-22 19:43:40 -04:00
|
|
|
programMemory: ProgramMemory | Error = ProgramMemory.empty(),
|
2024-03-26 19:32:31 -07:00
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
isMock: boolean
|
2023-09-29 11:11:01 -07:00
|
|
|
): Promise<ProgramMemory> => {
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(programMemory)) return Promise.reject(programMemory)
|
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
try {
|
2024-04-19 14:24:40 -07:00
|
|
|
let baseUnit = 'mm'
|
|
|
|
if (!TEST) {
|
|
|
|
const getSettingsState = import('components/SettingsAuthProvider').then(
|
|
|
|
(module) => module.getSettingsState
|
|
|
|
)
|
|
|
|
baseUnit =
|
|
|
|
(await getSettingsState)()?.modeling.defaultUnit.current || 'mm'
|
|
|
|
}
|
2024-07-22 19:43:40 -04:00
|
|
|
const memory: RawProgramMemory = await execute_wasm(
|
2023-09-29 11:11:01 -07:00
|
|
|
JSON.stringify(node),
|
2024-07-22 19:43:40 -04:00
|
|
|
JSON.stringify(programMemory.toRaw()),
|
2024-02-20 17:55:06 -08:00
|
|
|
baseUnit,
|
2024-02-12 12:18:37 -08:00
|
|
|
engineCommandManager,
|
2024-03-26 19:32:31 -07:00
|
|
|
fileSystemManager,
|
|
|
|
isMock
|
2023-09-29 11:11:01 -07:00
|
|
|
)
|
2024-07-22 19:43:40 -04:00
|
|
|
return ProgramMemory.fromRaw(memory)
|
2023-09-29 11:11:01 -07:00
|
|
|
} catch (e: any) {
|
2024-02-11 15:08:54 -08:00
|
|
|
console.log(e)
|
2023-09-29 11:11:01 -07:00
|
|
|
const parsed: RustKclError = JSON.parse(e.toString())
|
|
|
|
const kclError = new KCLError(
|
|
|
|
parsed.kind,
|
|
|
|
parsed.msg,
|
|
|
|
rangeTypeFix(parsed.sourceRanges)
|
|
|
|
)
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(kclError)
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 00:04:52 -04:00
|
|
|
export const kclLint = async (ast: Program): Promise<Array<Discovered>> => {
|
|
|
|
try {
|
|
|
|
const discovered_findings: Array<Discovered> = await kcl_lint(
|
|
|
|
JSON.stringify(ast)
|
|
|
|
)
|
|
|
|
return discovered_findings
|
|
|
|
} catch (e: any) {
|
|
|
|
return Promise.reject(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
export const recast = (ast: Program): string | Error => {
|
|
|
|
return recast_wasm(JSON.stringify(ast))
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
export const makeDefaultPlanes = async (
|
|
|
|
engineCommandManager: EngineCommandManager
|
|
|
|
): Promise<DefaultPlanes> => {
|
|
|
|
try {
|
|
|
|
const planes: DefaultPlanes = await make_default_planes(
|
|
|
|
engineCommandManager
|
|
|
|
)
|
|
|
|
return planes
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: do something real with the error.
|
|
|
|
console.log('make default planes error', e)
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(e)
|
2024-04-15 17:18:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 19:21:24 -07:00
|
|
|
export const modifyGrid = async (
|
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
hidden: boolean
|
|
|
|
): Promise<void> => {
|
|
|
|
try {
|
|
|
|
await modify_grid(engineCommandManager, hidden)
|
|
|
|
return
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: do something real with the error.
|
|
|
|
console.log('modify grid error', e)
|
|
|
|
return Promise.reject(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
export function lexer(str: string): Token[] | Error {
|
|
|
|
return lexer_wasm(str)
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export const modifyAstForSketch = async (
|
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
ast: Program,
|
|
|
|
variableName: string,
|
2023-10-05 14:27:48 -07:00
|
|
|
currentPlane: string,
|
2023-09-29 11:11:01 -07:00
|
|
|
engineId: string
|
|
|
|
): Promise<Program> => {
|
|
|
|
try {
|
|
|
|
const updatedAst: Program = await modify_ast_for_sketch_wasm(
|
|
|
|
engineCommandManager,
|
|
|
|
JSON.stringify(ast),
|
|
|
|
variableName,
|
2023-10-05 14:27:48 -07:00
|
|
|
JSON.stringify(currentPlane),
|
2023-09-29 11:11:01 -07:00
|
|
|
engineId
|
|
|
|
)
|
|
|
|
|
|
|
|
return updatedAst
|
|
|
|
} catch (e: any) {
|
|
|
|
const parsed: RustKclError = JSON.parse(e.toString())
|
|
|
|
const kclError = new KCLError(
|
|
|
|
parsed.kind,
|
|
|
|
parsed.msg,
|
|
|
|
rangeTypeFix(parsed.sourceRanges)
|
|
|
|
)
|
|
|
|
|
|
|
|
console.log(kclError)
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(kclError)
|
2023-09-29 11:11:01 -07:00
|
|
|
}
|
|
|
|
}
|
2024-02-11 12:59:00 +11:00
|
|
|
|
|
|
|
export function isPointsCCW(points: Coords2d[]): number {
|
|
|
|
return is_points_ccw(new Float64Array(points.flat()))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTangentialArcToInfo({
|
|
|
|
arcStartPoint,
|
|
|
|
arcEndPoint,
|
|
|
|
tanPreviousPoint,
|
|
|
|
obtuse = true,
|
|
|
|
}: {
|
|
|
|
arcStartPoint: Coords2d
|
|
|
|
arcEndPoint: Coords2d
|
|
|
|
tanPreviousPoint: Coords2d
|
|
|
|
obtuse?: boolean
|
|
|
|
}): {
|
|
|
|
center: Coords2d
|
|
|
|
arcMidPoint: Coords2d
|
|
|
|
radius: number
|
|
|
|
startAngle: number
|
|
|
|
endAngle: number
|
|
|
|
ccw: boolean
|
2024-04-03 13:22:56 +11:00
|
|
|
arcLength: number
|
2024-02-11 12:59:00 +11:00
|
|
|
} {
|
|
|
|
const result = get_tangential_arc_to_info(
|
|
|
|
arcStartPoint[0],
|
|
|
|
arcStartPoint[1],
|
|
|
|
arcEndPoint[0],
|
|
|
|
arcEndPoint[1],
|
|
|
|
tanPreviousPoint[0],
|
|
|
|
tanPreviousPoint[1],
|
|
|
|
obtuse
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
center: [result.center_x, result.center_y],
|
|
|
|
arcMidPoint: [result.arc_mid_point_x, result.arc_mid_point_y],
|
|
|
|
radius: result.radius,
|
|
|
|
startAngle: result.start_angle,
|
|
|
|
endAngle: result.end_angle,
|
|
|
|
ccw: result.ccw > 0,
|
2024-04-03 13:22:56 +11:00
|
|
|
arcLength: result.arc_length,
|
2024-02-11 12:59:00 +11:00
|
|
|
}
|
|
|
|
}
|
2024-02-11 18:26:09 -08:00
|
|
|
|
2024-07-22 19:43:40 -04:00
|
|
|
/**
|
|
|
|
* Returns new ProgramMemory with prelude definitions.
|
|
|
|
*/
|
2024-06-24 11:45:40 -04:00
|
|
|
export function programMemoryInit(): ProgramMemory | Error {
|
2024-02-11 18:26:09 -08:00
|
|
|
try {
|
2024-07-22 19:43:40 -04:00
|
|
|
const memory: RawProgramMemory = program_memory_init()
|
|
|
|
return new ProgramMemory(
|
|
|
|
memory.environments,
|
|
|
|
memory.currentEnv,
|
|
|
|
memory.return
|
|
|
|
)
|
2024-02-11 18:26:09 -08:00
|
|
|
} catch (e: any) {
|
|
|
|
console.log(e)
|
|
|
|
const parsed: RustKclError = JSON.parse(e.toString())
|
2024-06-24 11:45:40 -04:00
|
|
|
return new KCLError(
|
2024-02-11 18:26:09 -08:00
|
|
|
parsed.kind,
|
|
|
|
parsed.msg,
|
|
|
|
rangeTypeFix(parsed.sourceRanges)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 17:50:31 -07:00
|
|
|
|
2024-04-09 18:05:36 -07:00
|
|
|
export async function coreDump(
|
|
|
|
coreDumpManager: CoreDumpManager,
|
|
|
|
openGithubIssue: boolean = false
|
2024-06-20 16:36:28 -07:00
|
|
|
): Promise<CoreDumpInfo> {
|
2024-04-09 18:05:36 -07:00
|
|
|
try {
|
2024-06-28 18:06:40 -07:00
|
|
|
console.warn('CoreDump: Initializing core dump')
|
2024-06-20 16:36:28 -07:00
|
|
|
const dump: CoreDumpInfo = await coredump(coreDumpManager)
|
|
|
|
/* NOTE: this console output of the coredump should include the field
|
|
|
|
`github_issue_url` which is not in the uploaded coredump file.
|
|
|
|
`github_issue_url` is added after the file is uploaded
|
|
|
|
and is only needed for the openWindow operation which creates
|
|
|
|
a new GitHub issue for the user.
|
|
|
|
*/
|
2024-04-09 18:05:36 -07:00
|
|
|
if (openGithubIssue && dump.github_issue_url) {
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-04-09 18:05:36 -07:00
|
|
|
openWindow(dump.github_issue_url)
|
2024-06-20 16:36:28 -07:00
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
'github_issue_url undefined. Unable to create GitHub issue for coredump.'
|
|
|
|
)
|
2024-04-09 18:05:36 -07:00
|
|
|
}
|
2024-06-20 16:36:28 -07:00
|
|
|
console.log('CoreDump: final coredump', dump)
|
|
|
|
console.log('CoreDump: final coredump JSON', JSON.stringify(dump))
|
2024-04-09 18:05:36 -07:00
|
|
|
return dump
|
|
|
|
} catch (e: any) {
|
2024-06-20 16:36:28 -07:00
|
|
|
console.error('CoreDump: error', e)
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error getting core dump: ${e}`))
|
2024-04-09 18:05:36 -07:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 21:36:19 -07:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
export function tomlStringify(toml: any): string | Error {
|
|
|
|
return toml_stringify(JSON.stringify(toml))
|
2024-04-16 21:36:19 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 22:16:44 -04:00
|
|
|
export function defaultAppSettings(): DeepPartial<Configuration> | Error {
|
|
|
|
return default_app_settings()
|
2024-04-25 00:13:09 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 22:16:44 -04:00
|
|
|
export function parseAppSettings(
|
|
|
|
toml: string
|
|
|
|
): DeepPartial<Configuration> | Error {
|
|
|
|
return parse_app_settings(toml)
|
2024-04-25 00:13:09 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 22:16:44 -04:00
|
|
|
export function defaultProjectSettings():
|
|
|
|
| DeepPartial<ProjectConfiguration>
|
|
|
|
| Error {
|
|
|
|
return default_project_settings()
|
2024-04-25 00:13:09 -07:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
export function parseProjectSettings(
|
|
|
|
toml: string
|
2024-08-20 22:16:44 -04:00
|
|
|
): DeepPartial<ProjectConfiguration> | Error {
|
|
|
|
return parse_project_settings(toml)
|
2024-04-16 21:36:19 -07:00
|
|
|
}
|
2024-04-25 11:55:11 -07:00
|
|
|
|
2024-08-14 14:26:44 -04:00
|
|
|
export function base64Decode(base64: string): ArrayBuffer | Error {
|
|
|
|
try {
|
|
|
|
const decoded = base64_decode(base64)
|
|
|
|
return new Uint8Array(decoded).buffer
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Caught error decoding base64 string: ' + e)
|
|
|
|
return new Error('Caught error decoding base64 string: ' + e)
|
|
|
|
}
|
|
|
|
}
|