|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +/** |
| 4 | + * Javascript interoperability. Parses JS/TS exports into Modelica components. |
| 5 | + */ |
| 6 | + |
| 7 | +import type { Scope } from "../scope.js"; |
| 8 | +import { |
| 9 | + ModelicaClassInstance, |
| 10 | + ModelicaComponentInstance, |
| 11 | + type ModelicaElement, |
| 12 | + type ModelicaNamedElement, |
| 13 | +} from "./model.js"; |
| 14 | +import { ModelicaCausality, ModelicaClassKind, type ModelicaIdentifierSyntaxNode } from "./syntax.js"; |
| 15 | + |
| 16 | +/** |
| 17 | + * A Modelica class instance backed by a Javascript/Typescript file. |
| 18 | + */ |
| 19 | +export class ModelicaJavascriptEntity extends ModelicaClassInstance { |
| 20 | + jsPath: string; |
| 21 | + jsSource: string | null = null; |
| 22 | + #syntheticComponents: ModelicaComponentInstance[] = []; |
| 23 | + #loaded = false; |
| 24 | + |
| 25 | + constructor(parent: Scope, path: string) { |
| 26 | + super(parent); |
| 27 | + this.jsPath = path; |
| 28 | + this.classKind = ModelicaClassKind.PACKAGE; |
| 29 | + } |
| 30 | + |
| 31 | + override clone(): ModelicaClassInstance { |
| 32 | + if (!this.#loaded) this.load(); |
| 33 | + const cloned = new ModelicaJavascriptEntity(this.parent ?? this, this.jsPath); |
| 34 | + cloned.name = this.name; |
| 35 | + cloned.jsSource = this.jsSource; |
| 36 | + cloned.#loaded = true; |
| 37 | + cloned.instantiate(); |
| 38 | + return cloned; |
| 39 | + } |
| 40 | + |
| 41 | + override get elements(): IterableIterator<ModelicaElement> { |
| 42 | + if (!this.instantiated && !this.instantiating) this.instantiate(); |
| 43 | + const components = this.#syntheticComponents; |
| 44 | + return (function* () { |
| 45 | + yield* components; |
| 46 | + })(); |
| 47 | + } |
| 48 | + |
| 49 | + override resolveSimpleName( |
| 50 | + identifier: ModelicaIdentifierSyntaxNode | string | null | undefined, |
| 51 | + global = false, |
| 52 | + encapsulated = false, |
| 53 | + ): ModelicaNamedElement | null { |
| 54 | + const simpleName = typeof identifier === "string" ? identifier : identifier?.text; |
| 55 | + if (!simpleName) return null; |
| 56 | + if (!this.instantiated && !this.instantiating) this.instantiate(); |
| 57 | + |
| 58 | + for (const comp of this.#syntheticComponents) { |
| 59 | + if (comp.name === simpleName) return comp; |
| 60 | + } |
| 61 | + |
| 62 | + return super.resolveSimpleName(identifier, global, encapsulated); |
| 63 | + } |
| 64 | + |
| 65 | + override instantiate(): void { |
| 66 | + if (this.instantiated) return; |
| 67 | + if (this.instantiating) return; |
| 68 | + this.instantiating = true; |
| 69 | + try { |
| 70 | + if (!this.#loaded) this.load(); |
| 71 | + this.declaredElements = []; |
| 72 | + this.#syntheticComponents = []; |
| 73 | + |
| 74 | + if (!this.jsSource) return; |
| 75 | + |
| 76 | + const types = { |
| 77 | + number: this.root?.resolveSimpleName("Real") as ModelicaClassInstance | null, |
| 78 | + boolean: this.root?.resolveSimpleName("Boolean") as ModelicaClassInstance | null, |
| 79 | + string: this.root?.resolveSimpleName("String") as ModelicaClassInstance | null, |
| 80 | + }; |
| 81 | + |
| 82 | + // Extract functions |
| 83 | + // export function add(a, b) { ... } |
| 84 | + // export function multiply(a: number, b: number): number { ... } |
| 85 | + const funcRegex = /export\s+function\s+([a-zA-Z_$][\w$]*)\s*\(([^)]*)\)(?:\s*:\s*([a-zA-Z_$][\w$]*))?/g; |
| 86 | + let match; |
| 87 | + while ((match = funcRegex.exec(this.jsSource)) !== null) { |
| 88 | + const funcName = match[1]; |
| 89 | + const argsStr = match[2] ?? ""; |
| 90 | + const returnTypeStr = match[3] ?? "number"; |
| 91 | + |
| 92 | + if (!funcName) continue; |
| 93 | + |
| 94 | + const funcClass = new ModelicaClassInstance(this); |
| 95 | + funcClass.name = funcName; |
| 96 | + funcClass.classKind = ModelicaClassKind.FUNCTION; |
| 97 | + funcClass.instantiated = true; |
| 98 | + funcClass.declaredElements = []; |
| 99 | + |
| 100 | + // Parse arguments |
| 101 | + const args = argsStr |
| 102 | + .split(",") |
| 103 | + .map((s) => s.trim()) |
| 104 | + .filter((s) => s.length > 0); |
| 105 | + for (const arg of args) { |
| 106 | + const parts = arg.split(":").map((s) => s.trim()); |
| 107 | + const argName = parts[0]; |
| 108 | + const argType = parts[1] ?? "number"; |
| 109 | + |
| 110 | + if (argName) { |
| 111 | + const inputComp = new ModelicaComponentInstance(funcClass, null); |
| 112 | + inputComp.name = argName; |
| 113 | + inputComp.causality = ModelicaCausality.INPUT; |
| 114 | + const baseType = types[argType as keyof typeof types] || types.number; |
| 115 | + if (baseType) inputComp.classInstance = baseType.clone(); |
| 116 | + inputComp.instantiated = true; |
| 117 | + funcClass.declaredElements.push(inputComp); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Return value |
| 122 | + const outputComp = new ModelicaComponentInstance(funcClass, null); |
| 123 | + outputComp.name = "result"; |
| 124 | + outputComp.causality = ModelicaCausality.OUTPUT; |
| 125 | + const baseRetType = types[returnTypeStr as keyof typeof types] || types.number; |
| 126 | + if (baseRetType) outputComp.classInstance = baseRetType.clone(); |
| 127 | + outputComp.instantiated = true; |
| 128 | + funcClass.declaredElements.push(outputComp); |
| 129 | + |
| 130 | + const comp = new ModelicaComponentInstance(this, null); |
| 131 | + comp.name = funcName; |
| 132 | + comp.classInstance = funcClass; |
| 133 | + comp.instantiated = true; |
| 134 | + this.#syntheticComponents.push(comp); |
| 135 | + this.declaredElements.push(comp); |
| 136 | + } |
| 137 | + |
| 138 | + // Allow parsing: export const FOO = 42; |
| 139 | + const constRegex = /export\s+const\s+([a-zA-Z_$][\w$]*)\s*=/g; |
| 140 | + while ((match = constRegex.exec(this.jsSource)) !== null) { |
| 141 | + const constName = match[1]; |
| 142 | + if (!constName) continue; |
| 143 | + const comp = new ModelicaComponentInstance(this, null); |
| 144 | + comp.name = constName; |
| 145 | + const baseType = types.number; |
| 146 | + if (baseType) comp.classInstance = baseType.clone(); |
| 147 | + comp.instantiated = true; |
| 148 | + this.#syntheticComponents.push(comp); |
| 149 | + this.declaredElements.push(comp); |
| 150 | + } |
| 151 | + |
| 152 | + // Allow parsing export { a, b, c }; |
| 153 | + const blockExportRegex = /export\s*\{([^}]*)\}/g; |
| 154 | + while ((match = blockExportRegex.exec(this.jsSource)) !== null) { |
| 155 | + const names = (match[1] || "") |
| 156 | + .split(",") |
| 157 | + .map((s) => s.trim()) |
| 158 | + .filter((s) => s.length > 0); |
| 159 | + for (const name of names) { |
| 160 | + // just assume function/variable |
| 161 | + const comp = new ModelicaComponentInstance(this, null); |
| 162 | + comp.name = name; |
| 163 | + // By default, make it a function for simplicity |
| 164 | + const funcClass = new ModelicaClassInstance(this); |
| 165 | + funcClass.name = name; |
| 166 | + funcClass.classKind = ModelicaClassKind.FUNCTION; |
| 167 | + funcClass.instantiated = true; |
| 168 | + funcClass.declaredElements = []; |
| 169 | + comp.classInstance = funcClass; |
| 170 | + comp.instantiated = true; |
| 171 | + this.#syntheticComponents.push(comp); |
| 172 | + this.declaredElements.push(comp); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + this.instantiated = true; |
| 177 | + } finally { |
| 178 | + this.instantiating = false; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + load(): void { |
| 183 | + if (this.#loaded) return; |
| 184 | + this.#loaded = true; |
| 185 | + |
| 186 | + if (this.jsSource !== null) return; |
| 187 | + |
| 188 | + const context = this.context; |
| 189 | + if (context) { |
| 190 | + try { |
| 191 | + const data = context.fs.read(this.jsPath); |
| 192 | + this.jsSource = data; |
| 193 | + if (!this.name) { |
| 194 | + const basename = this.jsPath.split(/[/\\]/).pop() || this.jsPath; |
| 195 | + this.name = basename.replace(/\.[tj]s$/, ""); |
| 196 | + } |
| 197 | + } catch { |
| 198 | + console.warn(`[ModelicaJavascriptEntity] Failed to read JS file: ${this.jsPath}`); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments