Enforcing type safety at export
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ad5001 2024-04-02 00:11:56 +02:00
parent 5f8c756dc7
commit 781a0f4aae
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
13 changed files with 21 additions and 68 deletions

View file

@ -98,15 +98,15 @@ class IOAPI extends Module {
Modules.History.clear()
// Importing settings
this.settings.saveFilename = filename
this.settings.xzoom = data["xzoom"]
this.settings.yzoom = data["yzoom"]
this.settings.xmin = data["xmin"]
this.settings.ymax = data["ymax"]
this.settings.xaxisstep = data["xaxisstep"]
this.settings.yaxisstep = data["yaxisstep"]
this.settings.xlabel = data["xaxislabel"]
this.settings.ylabel = data["yaxislabel"]
this.settings.logscalex = data["logscalex"]
this.settings.xzoom = parseFloat(data["xzoom"]) || 100
this.settings.yzoom = parseFloat(data["yzoom"]) || 10
this.settings.xmin = parseFloat(data["xmin"]) || 5/10
this.settings.ymax = parseFloat(data["ymax"]) || 24
this.settings.xaxisstep = data["xaxisstep"] || "4"
this.settings.yaxisstep = data["yaxisstep"] || "4"
this.settings.xlabel = data["xaxislabel"] || ""
this.settings.ylabel = data["yaxislabel"] || ""
this.settings.logscalex = data["logscalex"] === true
if("showxgrad" in data)
this.settings.showxgrad = data["showxgrad"]
if("showygrad" in data)
@ -115,8 +115,8 @@ class IOAPI extends Module {
this.settings.linewidth = data["linewidth"]
if("textsize" in data)
this.settings.textsize = data["textsize"]
this.rootElement.height = data["height"]
this.rootElement.width = data["width"]
this.rootElement.height = parseFloat(data["height"]) || 500
this.rootElement.width = parseFloat(data["width"]) || 1000
// Importing objects
Modules.Objects.currentObjects = {}

View file

@ -139,11 +139,9 @@ export class DrawableObject {
*/
static import(name, visible, color, labelContent, ...args) {
let importedArgs = [name.toString(), visible === true, color.toString(), labelContent]
console.log('---')
console.log(this, name, args)
console.log('Importing', this.type(), name, args)
for(let [name, propType] of Object.entries(this.properties()))
if(!name.startsWith('comment')) {
console.log(name, propType, importedArgs.length-4, args[importedArgs.length-4])
importedArgs.push(ensureTypeSafety(propType, args[importedArgs.length-4]))
}
return new this(...importedArgs)

View file

@ -82,12 +82,6 @@ export default class Function extends ExecutableObject {
}
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.expression.toEditableString(), this.definitionDomain.toString(), this.destinationDomain.toString(),
this.displayMode, this.labelPosition, this.labelX, this.drawPoints, this.drawDashedLines]
}
execute(x = 1) {
if(this.definitionDomain.includes(x))
return this.expression.execute(x)

View file

@ -81,11 +81,6 @@ export default class GainBode extends ExecutableObject {
\\end{array}`
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.om_0.name, this.pass.toString(), this.gain.toEditableString(), this.labelPosition, this.labelX, this.omGraduation]
}
execute(x=1) {
if(typeof x == 'string') x = executeExpression(x)
if((this.pass === 'high' && x < this.om_0.x) || (this.pass === 'low' && x > this.om_0.x)) {

View file

@ -64,11 +64,6 @@ export default class PhaseBode extends ExecutableObject {
this.labelX = labelX
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.om_0.name, this.phase.toEditableString(), this.unit, this.labelPosition, this.labelX]
}
getReadableString() {
return `${this.name}: ${this.phase.toString(true)}${this.unit} at ${this.om_0.name} = ${this.om_0.x}`
}

View file

@ -54,10 +54,6 @@ export default class Point extends DrawableObject {
return `${Latex.variable(this.name)} = \\left(${this.x.latexMarkup}, ${this.y.latexMarkup}\\right)`
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent, this.x.toEditableString(), this.y.toEditableString(), this.labelPosition, this.pointStyle]
}
draw(canvas) {
let [canvasX, canvasY] = [canvas.x2px(this.x.execute()), canvas.y2px(this.y.execute())]
let pointSize = 8+(canvas.linewidth*2)

View file

@ -35,9 +35,12 @@ export default class RepartitionFunction extends ExecutableObject {
[QT_TRANSLATE_NOOP('prop','labelX')]: 'number',
}}
static import(name, visible, color, labelContent, ...args) {
if(args.length === 5)
console.log(args, args.length)
if(args.length === 5) {
// Two legacy values no longer included.
args.shift(); args.shift()
args.shift()
args.shift()
}
return super.import(name, visible, color, labelContent, ...args)
}
@ -51,12 +54,6 @@ export default class RepartitionFunction extends ExecutableObject {
this.update()
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.probabilities, this.labelPosition, this.labelX]
}
getReadableString() {
let keys = Object.keys(this.probabilities).sort((a,b) => a-b);
let varname = this.name.substring(this.name.indexOf("_")+1)

View file

@ -55,12 +55,6 @@ export default class Sequence extends ExecutableObject {
this.labelX = labelX
this.update()
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.drawPoints, this.drawDashedLines, this.defaultExpression, this.baseValues,
this.labelPosition, this.labelX]
}
update() {
console.log('Updating sequence', this.sequence)

View file

@ -44,10 +44,6 @@ export default class SommeGainsBode extends ExecutableObject {
this.recalculateCache()
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX]
}
getReadableString() {
return `${this.name} = ${Objects.getObjectsName('Gain Bode').join(' + ')}`
}

View file

@ -42,10 +42,6 @@ export default class SommePhasesBode extends ExecutableObject {
this.recalculateCache()
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX]
}
getReadableString() {
return `${this.name} = ${Objects.getObjectsName('Phase Bode').join(' + ')}`
}

View file

@ -78,10 +78,6 @@ export default class Text extends DrawableObject {
return `${Latex.variable(this.name)} = "\\textsf{${this.latexMarkupText()}}"`
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent, this.x.toEditableString(), this.y.toEditableString(), this.labelPosition, this.text, this.disableLatex]
}
getLabel() {
return this.text
}

View file

@ -60,12 +60,6 @@ export default class XCursor extends DrawableObject {
this.targetValuePosition = targetValuePosition
}
export() {
return [this.name, this.visible, this.color.toString(), this.labelContent,
this.x.toEditableString(), this.targetElement == null ? null : this.targetElement.name, this.labelPosition,
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
}
getReadableString() {
if(this.targetElement == null) return `${this.name} = ${this.x.toString()}`
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`

View file

@ -144,8 +144,10 @@ export class ObjectType extends PropertyType {
}
export(value) {
if(value.type === this.objType || (this.objType === 'ExecutableObject' && value.execute))
return value
if(value == null && this.allowNull)
return null
else if(value.type === this.objType || (this.objType === 'ExecutableObject' && value.execute))
return value.name
else
throw new TypeError(`Exportation error: ${value} not a ${this.objType}.`)
}